Play() and Vibrato() - Functions for playing music


i've started coding arduino in earnest today, , needed speed on c, , learn how play sounds on props i'm building, decided take tone() example, , expand upon make easier input tunes.

i based function loosely on how play() command on commodore 128 worked, because it's easy find monophonic ringtones in formats similar this, don't have keep track of notes , durations in 2 seperate arrays, , it's lot less typing input notes.

i added ability play tones vibrato sound more pleasant.  

the code set output pin 16. (a2) you'll have change contant in 2 places in vibrato function use different pin.

also, requires pitches.h tone()/playmelody example.

anyway, code free use please.  enjoy!

code: [select]
#include "pitches.h"

int bpm = 300;
int octave = 3;
int duration = 4;


// format:
// o5 = set octave 5.
// 4#a = sharp quarter note in octave last selected.
// spaces may omitted, , need specify note duration when changes.

char song1[] = "o5 8g 8g 4b 4g 4a 4f 1r 8g 8g 8g 8g 4f 4g 2r 4r 8g 8g 4b 4g 4a 4f 1r 8g 8g 8g 8g 4f 4a 4g";
char song2[] = "o58gg4bgaf1r8gggg4fg2r4r8gg4bgaf1r8gggg4fag"; // same above!

void setup() {

 play(song1);

}


void loop() {


}


void play(char *song) {

 int n = 0;
 int playnote = false;
 int sharp = false;
 int frequency;
 
 while (song[n]) {

   switch (song[n]) {
     
     case ' ':
       break;

     case 'o':      
       n++;
       if (song[n]) { octave = song[n] - 48; }
       break;
       
     case '8': duration = 8; break;
     case '4': duration = 4; break;
     case '2': duration = 2; break;
     case '1': duration = 1; break;
     
     case '#': sharp = true; break;
     
     case 'a':
       playnote = true;        
       frequency = note_a4;
       if (sharp == true) { frequency = note_as4; }        
       break;

     case 'b':
       playnote = true;        
       frequency = note_b4;
       break;

     case 'c':
       playnote = true;        
       frequency = note_c4;
       if (sharp == true) { frequency = note_cs4; }        
       break;
       
     case 'd':
       playnote = true;        
       frequency = note_d4;
       if (sharp == true) { frequency = note_ds4; }        
       break;

     case 'e':
       playnote = true;        
       frequency = note_e4;
       break;

     case 'f':
       playnote = true;        
       frequency = note_f4;
       if (sharp == true) { frequency = note_fs4; }        
       break;

     case 'g':
       playnote = true;        
       frequency = note_g4;
       if (sharp == true) { frequency = note_gs4; }        
       break;
         
     case 'r':
       playnote = true;        
       frequency = 0;
       break;
     
   }
   
   if (playnote == true) {
     
     frequency = float(frequency) * pow(2, octave-4);
     
     // if don't wish use vibrato(), comment out, , uncomment following lines:
       /*
       int wholenote = 4000.0 / (bpm/60.0);
       tone(16, frequency, wholenote/duration);
       delay (wholenote / duration);
       */

     vibrato(frequency, duration);
     
     playnote = false;
     sharp = false;
     
   }
     
   n++;
   
 }  
 
}  



// function plays note while rapidly switching between 2 octaves.  
void vibrato(int frequency, int duration) {
 
 int wholenote = 4000.0 / (bpm/60.0);
 int i;
 int v;
 int time;
   
 switch (duration) {    

   case 8:
     v = 2;
     break;

   case 4:
     v = 4;
     break;

   case 2:
     v = 8;
     break;

   case 1:
     v = 16;
     break;    
 }
 
 time = (wholenote/duration) / v;
 
 for (int = 0; < v; i++) {

   switch (i % 2) {    

     case 0:
       tone(16, frequency/2, time);
       delay(time);
       break;
 
     case 1:      
       tone(16, frequency, time);
       delay(time);
       break;
 
     }
 }
 
 delay((wholenote/duration) * 0.3); // pause between notes.

}



Arduino Forum > Forum 2005-2010 (read only) > Software > Syntax & Programs > Play() and Vibrato() - Functions for playing music


arduino

Comments