SSブログ

Digisparkで音楽演奏【PLL OC1A編】 [Arduino]

以前のスケッチを手直し。
ボードマネージャは、ATTinyCore でも Digistump AVR Boards でも行けるようにしてみた。
8MHzでもなんとか遅延はなさそうで、乾電池2本でも動作できるはず。

dsoutput.jpg
結局もう1つ改造Digispark作成。
壊れたイヤホンからケーブルをリユースして録音用パーツを作成。



以下、スケッチ。
//  Digispark version Score Replay Sketch
//    Clock : 16.5/16/8 MHz, PLL : 66/64 MHz
#include "notes2.h"                     // Definition data of notes ( pitch (scale & octave), and note value (length))
#include "Kewpie3min.h"                 // "The Parade of the Tin Soldiers" by Leon Jessel
#define MAX_TRACK   4                   // Maximum number of tracks

// Number of cycles that are the wavelength of the basic 12-note scale (from C0 to B0) (@ 8 MHz, with an interrupt every 256 clocks)
static const uint16_t CYC_SCALE[] = { 1911, 1804, 1703, 1607, 1517, 1432, 1351, 1276, 1204, 1136, 1073, 1012 };

void setup(){
  pinMode( 1, OUTPUT );                 // Audio output (PB1,OC1A)
  pinMode( 0, INPUT_PULLUP );           // Switch       (PB0)
  
  // *** Register Settings (Timer1, for DAC)  *** PLL 64MHz, No prescaling. 8bit=256clock to make PWM, so 250kHz. 
  TIMSK  &= ~_BV(TOIE1);        // TIMSK(T/C Interrupt Mask Register)      - TOIE1(T/C1 Overflow Interrupt Enable) CLEAR : Disable ISR(TIMER1_OVF_vect) (for Digistump AVR Boards)
  PLLCSR |=  _BV(PCKE);         // PLLCSR(PLL Control and Status Register) - PCKE(PCK Enable) SET : PCK clock(fast 64 MHz) is used as T/C1 clock source
  TCCR1   = B01100001;          // TCCR1(T/C1 Control Register) : PWM A Enable. OC1A cleared on compare match. OC1A not connected. No prescale
  OCR1A   = 0;                  // OCR1A(T/C1 Output Compare RegisterA) : Set output to 0
  OCR1C   = 255;                // OCR1C(T/C1 Output Compare RegisterC) : Set upper limit to 255
  // *** Register Settings (Timer0, for Data) *** Data processing interval 31.25kHz(256clk@8MHz or 512clk@16MHz)
  TIMSK  &= ~_BV(TOIE0);        // TIMSK(T/C Interrupt Mask Register)      - TOIE0(T/C0 Overflow Interrupt Enable) CLEAR : Disable ISR(TIMER0_OVF_vect) (for ATTinyCore)
  TCCR0A  = B00000011;          // TCCR0A(T/C0 Control Register A) :  OC0A/OC0B disconnected, Fast PWM
  TCCR0B  = B01001010;          // TCCR0B(T/C0 Control Register B) :  Fast PWM, TOP:OCR0A, clk/8 (From prescaler)
  OCR0A   = F_CPU / 250000;     // OCR0A (T/C0 Output Compare A Register) : TOP 8MHz:32, 16MHz:64, 16.5MHz:66
}

void loop(){
  while(  digitalRead(0) );             // Wait until the switch is pressed
  while( !digitalRead(0) );             // Wait until the switch is released
  playDsPLL( Kewpie3min );              // Playback
}

void playDsPLL(const uint16_t *d){      // Digispark PLL version (16.5/16/8 MHz support)
  uint8_t  Tracks;                      // Number of tracks
  const uint16_t *NoteP[MAX_TRACK];     // Pointer for each track of the score
  uint16_t NoteCycle, n;                // Number of interrupt cycles required for the length of the reference note (96th note) and its counter (n)
  uint16_t note;                        // Note (pitch + note value) information read from PROGMEM
  uint8_t  len[MAX_TRACK];              // Length of note (how many 96th notes) (subtraction counter)
  uint16_t cyc[MAX_TRACK], c[MAX_TRACK];// Number of interrupt cycles (cyc) required for one sound wavelength cycle and its counter (c)
  uint16_t env[MAX_TRACK];              // Sound amplitude (envelope)
  uint16_t out[MAX_TRACK];              // Output value
  uint8_t  lap = 20;                    // Sound transitional cycles(laptime) (for Attenuation calculator)

  // *** Preparing Scores ***
  NoteCycle = 8000000 / 256 *4*60 / pgm_read_word_near(d++) / MIN_NOTE; // Number of cycles required for reference note
  for( Tracks = 0; Tracks < MAX_TRACK; ) {              // Get the number of tracks and the start position of each track from the song data
    if( pgm_read_word_near(d++) != 0 ) continue;        // Skip until the break comes
    if( pgm_read_word_near(d)   == 0 ) break;           // If two zeros follow, end of data
    len[ Tracks ] = 1;                                  // Initialize the note length subtraction counter to the remaining 1
    NoteP[ Tracks++ ] = d;                              // Get location in memory, Count up the number of tracks
  }
  n = Tracks;                           // Initialize the score processing so that it can be performed immediately after the start of the do loop

  // *** Playback ***
  do {
    // * Processing Scores *
    if( --n < Tracks ) {                // Processing of score for each reference note length
      if( !--len[n] ) {
        note   = pgm_read_word_near( NoteP[n]++ );
        len[n] = (uint8_t) note;        // The lower 8 bits are the length of the note (how many times the length of a 96th note)
        cyc[n] = (note>>8) ? CYC_SCALE[ (note>>8) & 0xf ] >> (note>>12) : 0;    // Upper 4 bits are octave, next 4 bits are pitch class (0-11), 0 for rests
          c[n] = 0;                     // Initialize the counter for the number of cycles to create one square wave cycle
        env[n] = 0xffff;                // Initially, the maximum amplitude
      }
      if( !n ) n = NoteCycle;
    }
    // * Waveform Processing / Output *
    switch( Tracks ) {                  // Create output data, Square wave with duty ratio of 1:1
      case 4:   out[3] = ( ( c[3] = (++c[3]==cyc[3]) ? 0 : c[3] ) < (cyc[3]>>1) ) ? env[3] : 0;   [[fallthrough]];
      case 3:   out[2] = ( ( c[2] = (++c[2]==cyc[2]) ? 0 : c[2] ) < (cyc[2]>>1) ) ? env[2] : 0;   [[fallthrough]];
      case 2:   out[1] = ( ( c[1] = (++c[1]==cyc[1]) ? 0 : c[1] ) < (cyc[1]>>1) ) ? env[1] : 0;   [[fallthrough]];
      case 1:   out[0] = ( ( c[0] = (++c[0]==cyc[0]) ? 0 : c[0] ) < (cyc[0]>>1) ) ? env[0] : 0;
    }
    switch( Tracks ) {                  // OC1A(ATtiny85:PB1) Change output by number of tracks
      case 1:   OCR1A =   out[0] >>8;                                   break;
      case 2:   OCR1A = ((out[0]>>1) + (out[1]>>1)) >>8;                break;
      case 3:   OCR1A = ((out[0]>>1) + (out[1]>>2) + (out[2]>>2)) >>8;  break;
      case 4:   OCR1A = ((out[0]>>2) + (out[1]>>2) + (out[2]>>2) + (out[3]>>2)) >>8;
    }
    switch( --lap ) {                   // Amplitude attenuation (Distribute processing per loop)
      case 4:   env[3] -= (env[3]>>9);    break;
      case 3:   env[2] -= (env[2]>>9);    break;
      case 2:   env[1] -= (env[1]>>9);    break;
      case 1:   env[0] -= (env[0]>>9);    break;
      case 0:   lap = 20;               // every 640usec(32usec*20) @16MHz (Adjustable)
    }
    while( !(TIFR & _BV(TOV0)) );       // Waiting for Timer0 overflow (every 32usec)
    TIFR |= _BV(TOV0);                  // Clear T/C0 Overflow Flag
  //if( ! digitalRead(0) ) break;       // Ends when PB0 is pressed.
    if( !(PINB & _BV(0)) ) break;       // Ends when PB0 is pressed.
  } while( note );                      // Exit if note data is 0
  OCR1A = 0;                            // Set output to 0
}

次は、矩形波じゃないやつで。
タグ:Digispark 音楽 PLL
nice!(0)  コメント(0) 

Digisparkで音楽演奏【楽譜サンプル】 [Arduino]

あたらしい楽譜の記載法で入力はやや楽に。
kewpie3score2.jpg
「Kewpie3min.h」
イントロ部分を和音がきれいになるようにちょこっとアレンジしてみた。
static const uint16_t Kewpie3min[] PROGMEM = {  // "The Parade of the Tin Soldiers" by Leon Jessel
  168, 0 ,
  RSe , g5s , g5s , g5e , g5e , e5e , e5e , f5q , RSe , g5s , g5s , g5e , g5e , C5e , C5e , d5q , RSe , g5s , g5s , g5h , d5h , g4h , RSq , g5e , g5s , g5s ,
  e5e , RSe , g5e , RSe , g5e , RSe , RSe , g5s , g5s , f5e , RSe , g5e , RSe , g5e , RSe , RSe , g5s , g5s , e5e , RSe , g5e , RSe , b5e , RSe , a5e , RSe , g5e , F5e , f5e , e5e , d5e , RSe , RSe , g5s , g5s ,
  e5e , RSe , g5e , RSe , g5e , RSe , RSe , g5s , g5s , d5e , RSe , b5e , RSe , b5e , RSe , RSe , a5s , a5s , a5e , RSe , a5e , RSe , d6e , RSe , d6e , RSe , g5q , g5s , G5s , a5s , A5s , b5e , g5e , a5e , g5e ,
  e5e , RSe , g5e , RSe , e5e , g5e , RSe , g5e , f5e , RSe , g5e , RSe , f5e , g5e , RSe , g5e , e5e , RSe , g5e , RSe , b5e , RSe  ,a5e , RSe , g5e , F5e , f5e , e5e , d5e , RSe , RSe , g5s , g5s ,
  e5e , RSe , g5e , RSe , g5e , RSe , RSe , g5s , g5s , f5e , RSe , a5e , RSe , a5e , RSe , c6e , RSe , e6e , RSe , RSq , d6e , RSe , RSq ,
//c6e , RSe , g5e , RSe , c6e , RSe , RSq ,                     // OP
  c6e , g5s , G5s , a5s , A5s , b5e , c6e , RSe , c4q ,         // ED  
  0,
  RSq , RSq , C5e , C5e , d5q , RSq , RSq , A4e , A4e , b4q , RSq , RSe , F5h , c5h , RSe , g5h , RSq ,
  RSe , g3e , RSe , g3e , RSe , g3e , RSe , g3e , RSe , g3e , RSe , g3e , RSe , g3e , RSe , g3e , RSe , g3e , RSe , g3e , RSe , g3e , RSe , g3e , RSe , g3e , RSe , g3e , RSe , g3e , RSe , g3e ,
  RSe , g3e , RSe , g3e , RSe , g3e , RSe , g3e , RSe , b3e , RSe , b3e , RSe , b3e , RSe , b3e , RSe , a3e , RSe , a3e , RSe , a3e , RSe , a3e , g3q , RSq , RSh ,
  RSe , g3e , RSe , g3e , RSe , g3e , RSe , g3e , RSe , g3e , RSe , g3e , RSe , g3e , RSe , g3e , RSe , g3e , RSe , g3e , RSe , g3e , RSe , g3e , RSe , g3e , RSe , g3e , RSe , g3e , RSe , g3e ,
  RSe , g3e , RSe , g3e , RSe , g3e , RSe , g3e , RSe , a3e , RSe , a3e , RSe , a3e , RSe , a3e , RSe , g3e , RSe , g3e , RSe , g3e , RSe , g3e ,
//RSe , g3e , RSe , g3e , g3e , RSe , RSq ,                     // OP
  g3e , RSe , RSq , c4e , RSe , c3q ,                           // ED
  0,
  RSq , RSw , RSw , RSe , RSe , f5h , b4h , RSq , g4h ,
  RSe , e3e , RSe , e3e , RSe , e3e , RSe , e3e , RSe , f3e , RSe , f3e , RSe , f3e , RSe , f3e , RSe , e3e , RSe , e3e , RSe , e3e , RSe , e3e , RSe , f3e , RSe , f3e , RSe , f3e , RSe , f3e ,
  RSe , e3e , RSe , e3e , RSe , e3e , RSe , e3e , RSe , g3e , RSe , g3e , RSe , g3e , RSe , g3e , RSe , F3e , RSe , F3e , RSe , F3e , RSe , F3e , RSw ,
  RSe , e3e , RSe , e3e , RSe , e3e , RSe , e3e , RSe , f3e , RSe , f3e , RSe , f3e , RSe , f3e , RSe , e3e , RSe , e3e , RSe , e3e , RSe , e3e , RSe , f3e , RSe , f3e , RSe , f3e , RSe , f3e ,
  RSe , e3e , RSe , e3e , RSe , e3e , RSe , e3e , RSe , f3e , RSe , f3e , RSe , f3e , RSe , f3e , RSe , e3e , RSe , d3e , RSe , f3e , RSe , f3e ,
//RSe , e3e , RSe , e3e , e3e , RSe , RSq ,                     // OP
  e3e , RSe , RSq , RSe , RSe , c2q ,                           // ED
  0,
  RSq , RSw , RSw , RSe , RSe , RSe , e5h , a4h , RSe , RSh ,
  c3q , g2q , c2q , g2q , d2q , g2q , d2q , g2q , c3q , g2q , c2q , g2q , d2q , g2q , d2q , g2q ,
  c3q , g2q , c2q , g2q , d2q , g2q , d2q , g2q , a2q , d2q , F2q , d2q , g2q , RSq , RSh ,
  c3q , g2q , c3q , c3q , g2q , d2q , g2q , g2q , c3q , g2q , c3q , c3q , g2q , d2q , g2q , g2q ,
  c3q , g2q , c3q , c3q , f2q , c2q , f2q , f2q , c3q , g2q , d2q , g2q ,
//c3q , g2q , c3q , RSq ,                                       // OP
  c3e , RSe , RSq , RSq , RSq ,                                 // ED
  0, 0 };

スケッチも手直ししてみたので次回。
タグ:楽譜 音楽
nice!(0)  コメント(0) 

Digisparkで音楽演奏【楽譜編】 [JavaScript]

kewpie3score.jpg
以前につくった notes.h ですが、やっぱり楽譜から入力しづらい。大文字が多いこととアンダーバー('_')が入力しにくさの原因ではないかと考えました。
もうちょっと楽譜から入力しやすいようにしました。
変更点としては、小文字メインで、半音上がるシャープ(♯)がつくのを大文字にするのと、長さを数字ではなく英語での頭文字の小文字としました。
以下、再度楽譜のマイルール。

【マイコンで処理しやすい楽譜・音符情報を作ります。】
  • 音の高さ(音高、ピッチ)は、周波数(Hz)等ではなく、音階とオクターブで表す
  • 音の長さ(音価)は基準の最小音符の何倍かで表す
  • 一音で2byte(16bit)使用する
    • 上位8bitが音の高さ(音高)
      • 0xooの時は休符
      • 上位4ビットがオクターブ
        • 0-9まで
      • 次の4ビットが音階・スケール(ド~シの12音階で0-11)
        • 音名は、英語でCDEFGAB
        • 入力簡便化のため小文字とし、半音上げる際は大文字を使用
          • 'c','C','d','D','e','f','F','g','G','a','A','b'
        • 半音下げるのは、一音下げてから半音あげる対応で   
      • MIDIノート番号でいうとNo.13(C#0)~127(G9)まで網羅
      • C0は休符用に使用, A9, A#9, B9はMIDI番号にもないため除く
      • 音階とオクターブを分けることで12個の基本音階情報からシフト演算を用いて容易に算出できる
    • 下位8ビットが音の長さ(音価)
      • 96分音符を最小単位の1とする
        • 1-255までに各種音符の長さを入れるとすると32分音符(3)から倍全音符(192)まで入れることができる
        • 3連符には対応できるよう通常の音符は3の倍数
      • 音符の長さの表記は、英語訳の頭文字
        • 32分音符 thirty-second note
        • 16分音符 sixteenth note
        • 8分音符 eighth note
        • 4分音符 quarter note
        • 2分音符 half note
        • 全音符 whole note (breve)
        • 倍全音符 double whole note (longa)
        • 4倍全音符 4x whole note (maxima)
      • 入力簡便化のため小文字を使用する
      • 付点音符はさらに'd' (dotted) を付ける
      • 3連符用に三分の一の長さ(one third)には't'を付ける
        • 本来は3連符は3等分する前の音符の半分の長さの音符で書かれる   
      • タイによる音符の結合は、休符(長さ情報のみでできている)を加算することで可能
  • 楽譜としては、音符となるunsigned int(2byte)の情報を配列に羅列する
    • 先頭は、テンポ(♩=1~65535)を入れて、次に0で区切る
    • 1トラック内に和音は置けないため、必要な和音の分だけトラックを使用する
    • 各トラック内の終わりは0とし、つづけて次のトラック情報に入る
    • すべてのトラック情報のあとにさらに0を入れて楽譜データ終了とする

【楽譜定義のヘッダーファイルを作るjavascript】

前回はエクセルでしこしこ作りましたが、今回は修正もしやすいようにjavascriptを使用。
以下を「note2.html」として保存してブラウザで開く。
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="UTF-8">
<title>Musical notes 2</title>
<script>
// *** twelve-tone scale ***
//   capital letter : raise a semitone. (e.g. C means C#)
const twelveTone = ['c','C','d','D','e','f','F','g','G','a','A','b'];

// *** note value (sound length) ***
const noteValue  = [
  { note: 't' , value:   3 },   // thirty-second note
  { note: 's' , value:   6 },   // sixteenth note
  { note: 'e' , value:  12 },   // eighth note
  { note: 'q' , value:  24 },   // quarter note
  { note: 'h' , value:  48 },   // half note
  { note: 'w' , value:  96 },   // whole note (breve)
  { note: 'd' , value: 192 },   // double whole note (longa)

  { note: 'sd', value:   9 },   // dotted sixteenth note
  { note: 'ed', value:  18 },   // dotted eighth note
  { note: 'qd', value:  36 },   // dotted quarter note
  { note: 'hd', value:  72 },   // dotted half note
  { note: 'wd', value: 144 },   // dotted whole note (breve)

  { note: 'tt', value:   1 },   // 1/3 of a 32nd note
  { note: 'st', value:   2 },   // 1/3 of a 16th note
  { note: 'et', value:   4 },   // 1/3 of an eighth note
  { note: 'qt', value:   8 },   // 1/3 of a quarter note
  { note: 'ht', value:  16 },   // 1/3 of a half note
  { note: 'wt', value:  32 },   // 1/3 of a whole note (breve)
  { note: 'dt', value:  64 },   // 1/3 of a double whole note (longa)
  { note: 'mt', value: 128 },   // 1/3 of a 4x whole note (maxima)
 ];

$ = function(id) { return document.getElementById(id); }

function notes() {
    let  octave, scale;
    $('main').value  = $('explanation').value;
    $('main').value += '#define MIN_NOTE  96    // 96th note (reference note)\n';
    $('main').value += '\n// Rests of various lengths (rests are just Length, so it is also for connection by Tie)\n';
    for( let nv of noteValue ) $('main').value += '#define RS' + (nv.note + '   ').slice(0,2)  + "   "  + ('   ' + nv.value).slice(-3) + '\n';
    $('main').value += '\n// combination of pitch and length\n';
    for( octave = 0; octave < 10; octave++ ) {
        for( scale = 0; scale < 12; scale++ ) {
            if( octave == 0 && scale == 0) continue;
            if( octave == 9 && scale >  8) continue;
            for( let nv of noteValue ) {
                $('main').value += '#define ' + twelveTone[scale] + octave +  (nv.note + '  ').slice(0,2)
                                +  '   (0x' + octave + scale.toString(16) + '00 | ' +  ('   ' + nv.value).slice(-3) + ')\n';
            }
        }
    }
    $('main').style.display  = 'block';
    $('dlBtn').style.display = 'inline';
}

function downloadText(fileName) {
  const blob = new Blob([$('main').value], { type: 'text/plain' });
  const aTag = document.createElement('a');
  aTag.href = URL.createObjectURL(blob);
  aTag.target = '_blank';
  aTag.download = fileName;
  aTag.click();
  URL.revokeObjectURL(aTag.href);
}
</script>
</head>
<body>
<h1>Musical notes 2</h1>
<p>Creates musical score and note information that is easy to process with a microcomputer.
<p>
<button onClick="notes()">Create a note definition data</button>
<button onClick="downloadText('note2.h')" style="display:none;" id="dlBtn">Download the header file</button>
<p>
<textarea id="explanation" disabled style="display:none;">
// ****************************************************************
//  notes2.h
// ****************************************************************
//   upper 8 bits   : pitch of a sound (0x00 : rest)
//     upper 4 bits : octaves  [0-9]
//     next  4 bits : 12 tone scale (C,C#,D,D#,E,F,F#,G,G#,A,A#,B)
//                      ['c','C','d','D','e','f','F','g','G','a','A','b']
//   lower 8 bits   : length of the sound 
//                      Let the 96th note be 1
//                      't'  =   3   thirty-second note
//                      's'  =   6   sixteenth note
//                      'e'  =  12   eighth note
//                      'q'  =  24   quarter note
//                      'h'  =  48   half note
//                      'w'  =  96   whole note (breve)
//                      'd'  = 192   double whole note (longa)
//                      'sd' =   9   dotted sixteenth note
//                      'ed' =  18   dotted eighth note
//                      'qd' =  36   dotted quarter note
//                      'hd' =  72   dotted half note
//                      'wd' = 144   dotted whole note (breve)
//                      'tt' =   1   1/3 of a 32nd note
//                      'st' =   2   1/3 of a 16th note
//                      'et' =   4   1/3 of an eighth note
//                      'qt' =   8   1/3 of a quarter note
//                      'ht' =  16   1/3 of a half note
//                      'wt' =  32   1/3 of a whole note (breve)
//                      'dt' =  64   1/3 of a double whole note (longa)
//                      'mt' = 128   1/3 of a 4x whole note (maxima)
// ****************************************************************

</textarea>
<textarea id="main" style="display:none; width:90%; height: calc( 1.2em * 25 );  line-height: 1.2;"></textarea>
</body>
</html>

再生のスケッチは後日。
nice!(0)  コメント(0) 

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