SSブログ

RL78/G15 FPB の tone()関数で和音 [Arduino]

RL78/G15 のレジスタを直接いじるの大変そう。PWM作るのにもe2stdioとかいうのを使ってやるみたい。
そこでtone()関数をみてみると、、

RL78G15 20ピン Fast Prototyping Board ピンリスト · renesas_Arduino Wiki · GitHub
https://github.com/renesas/Arduino/wiki/RL78G15-20%E3%83%94%E3%83%B3-Fast-Prototyping-Board-%E3%83%94%E3%83%B3%E3%83%AA%E3%82%B9%E3%83%88

2-7. Tone のところで、Tone output pinに指定できるのは、Arduino 0番, 3番, 13番の3つ。
3つ同時に使えるかと思ったら、3番, 13番を同時に使うのはダメで2和音まででした。

rl78g15frogs.jpg

// RL78/G15 FPB tone function test
#include "notes2.h"                         // Definition data of notes ( pitch (scale & octave), and note value (length))
#define MAX_TRACK   2                       // Maximum number of tracks
const uint8_t  ToneOutputPin[] = { 0, 13 }; // two of 0,3,13 pins (No combination of 3 and 13)
const uint16_t F_SCALE[] = { 33488, 35479, 37589, 39824, 42192, 44701, 47359, 50175, 53159, 56320, 59669, 63217 };
                                            // basic frequency of 12-note scale (from C11 to B11)(Hz)
const uint16_t Frogs[] = {                  // melody data
  120 ,   0 ,
  c5q , d5q , e5q , f5q , e5q , d5q , c5q , RSq , e5q , f5q , g5q , a5q , g5q , f5q , e5q , RSq ,
  c5q , RSq , c5q , RSq , c5q , RSq , c5q , RSq , c5e , c5e , d5e , d5e , e5e , e5e , f5e , f5e ,
  e5q , d5q , c5q , RSq , RSd , RSd ,   0 ,
  RSd ,
  c5q , d5q , e5q , f5q , e5q , d5q , c5q , RSq , e5q , f5q , g5q , a5q , g5q , f5q , e5q , RSq ,
  c5q , RSq , c5q , RSq , c5q , RSq , c5q , RSq , c5e , c5e , d5e , d5e , e5e , e5e , f5e , f5e ,
  e5q , d5q , c5q , RSq , RSd ,   0 ,   0 };

void setup(){
}

void loop(){
  const uint16_t *d = Frogs;                // Pointer of melody data
  const uint16_t *p[MAX_TRACK];             // Pointer for each track of the score
  uint8_t  Tracks, t;                       // Number of tracks and counter
  uint16_t note;                            // Note (pitch + note value) information
  uint8_t  len[MAX_TRACK] = {};             // Length of note (how many 96th notes) (subtraction counter)
  uint32_t usInt, usExp;                    // sound division interval, sound expire time(usec)
  usInt = 1000000*60*4 / MIN_NOTE / *d++;   // Calculate standard 96th note length(usec) from tempo
  for( Tracks=0; Tracks<MAX_TRACK; ) {      // Get the number of tracks and the start position of each track from the song data
    if( *d++ != 0 ) continue;               // Skip until the break comes
    if( *d   == 0 ) break;                  // If two zeros follow, end of data
    p[ Tracks++ ] = d;                      // Get location in memory, Count up the number of tracks
  }
  usExp = micros();
  do {                                      // Processing of score for each reference note(96th note) length
    for( t=0; t<Tracks; t++) {
      if( !len[t]-- ) {                     // Subtract the length of the note, and when it reaches 0, go to the next note
        if( !(note = *p[t]++) ) return;     // end if note = 0
        len[t]  = (note & 0x00ff) - 1;      // The lower 8 bits are the length of the note (how many times the length of a 96th note)
        if(note & 0xff00)                   // note(16bit) : Upper 4 bits are octave, next 4 bits are pitch class (0-11)
                tone( ToneOutputPin[t], F_SCALE[(note>>8)&0x0f] >> (11-(note>>12)), 0 ); 
        else  noTone( ToneOutputPin[t] );   // 0 for rests
      }
    }
    usExp += usInt;
    while( micros() < usExp );              // wait until expiration
  } while( note );
  for( t=0; t<Tracks; t++) noTone( ToneOutputPin[t] );  // stop all sounds
  delay(1000);
}

第5オクターブで曲データを作っています。
これより低い音はうまく出せないみたい。

楽譜データの音符などは、notes2.h で。
Digisparkで音楽演奏【楽譜編】
https://hello-world.blog.ss-blog.jp/2022-07-04


タグ:RL78/G15
nice!(0)  コメント(0) 

nice! 0

コメント 0

コメントを書く

お名前:
URL:
コメント:
画像認証:
下の画像に表示されている文字を入力してください。

この広告は前回の更新から一定期間経過したブログに表示されています。更新すると自動で解除されます。