Guest User

dp

a guest
Feb 26th, 2011
612
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ///h///
  2. ///////
  3.  
  4. #ifndef Sound_h
  5. #include "WProgram.h"
  6. #define Sound_h
  7. #define OCTAVE_OFFSET 0
  8. #define TONE_PIN 13
  9. #define NOTE_C4  262
  10. #define NOTE_CS4 277
  11. #define NOTE_D4  294
  12. #define NOTE_DS4 311
  13. #define NOTE_E4  330
  14. #define NOTE_F4  349
  15. #define NOTE_FS4 370
  16. #define NOTE_G4  392
  17. #define NOTE_GS4 415
  18. #define NOTE_A4  440
  19. #define NOTE_AS4 466
  20. #define NOTE_B4  494
  21. #define NOTE_C5  523
  22. #define NOTE_CS5 554
  23. #define NOTE_D5  587
  24. #define NOTE_DS5 622
  25. #define NOTE_E5  659
  26. #define NOTE_F5  698
  27. #define NOTE_FS5 740
  28. #define NOTE_G5  784
  29. #define NOTE_GS5 831
  30. #define NOTE_A5  880
  31. #define NOTE_AS5 932
  32. #define NOTE_B5  988
  33. #define NOTE_C6  1047
  34. #define NOTE_CS6 1109
  35. #define NOTE_D6  1175
  36. #define NOTE_DS6 1245
  37. #define NOTE_E6  1319
  38. #define NOTE_F6  1397
  39. #define NOTE_FS6 1480
  40. #define NOTE_G6  1568
  41. #define NOTE_GS6 1661
  42. #define NOTE_A6  1760
  43. #define NOTE_AS6 1865
  44. #define NOTE_B6  1976
  45. #define NOTE_C7  2093
  46. #define NOTE_CS7 2217
  47. #define NOTE_D7  2349
  48. #define NOTE_DS7 2489
  49. #define NOTE_E7  2637
  50. #define NOTE_F7  2794
  51. #define NOTE_FS7 2960
  52. #define NOTE_G7  3136
  53. #define NOTE_GS7 3322
  54. #define NOTE_A7  3520
  55. #define NOTE_AS7 3729
  56. #define NOTE_B7  3951
  57. #define isdigit(n) (n >= '0' && n <= '9')
  58.  
  59. class Sound
  60. {
  61. public:
  62.    
  63. void play_rtttl(char *SoundP);
  64. int dirfrq;
  65. char* songs[];
  66. char* goods[];
  67. char* bads[];
  68.  
  69.  
  70. private:
  71.     void woop(void);
  72.   byte default_dur;
  73.   byte default_oct;
  74.   int bpm;
  75.   int num;
  76.   long wholenote;
  77.   long duration;
  78.   byte note;
  79.   byte scale;
  80. };
  81. static const int notes[]={0, NOTE_C4, NOTE_CS4, NOTE_D4, NOTE_DS4, NOTE_E4, NOTE_F4, NOTE_FS4, NOTE_G4, NOTE_GS4, NOTE_A4, NOTE_AS4, NOTE_B4, NOTE_C5, NOTE_CS5, NOTE_D5, NOTE_DS5, NOTE_E5, NOTE_F5, NOTE_FS5, NOTE_G5, NOTE_GS5, NOTE_A5, NOTE_AS5, NOTE_B5, NOTE_C6, NOTE_CS6, NOTE_D6, NOTE_DS6, NOTE_E6, NOTE_F6, NOTE_FS6, NOTE_G6, NOTE_GS6, NOTE_A6, NOTE_AS6, NOTE_B6, NOTE_C7, NOTE_CS7, NOTE_D7, NOTE_DS7, NOTE_E7, NOTE_F7, NOTE_FS7, NOTE_G7, NOTE_GS7, NOTE_A7, NOTE_AS7, NOTE_B7};
  82. #endif
  83.  
  84. //////////
  85. ///cpp////
  86. #include "WProgram.h"
  87. #include "Sound.h"
  88.  
  89. void Sound::play_rtttl(char *SoundP){
  90.   // Absolutely no error checking in here
  91. ;
  92.  
  93.   default_dur = 4;
  94.   default_oct = 6;
  95.   bpm = 63;
  96.   num;
  97.   wholenote;
  98.   duration;
  99.   note;
  100.   scale;
  101.  
  102.   // format: d=N,o=N,b=NNN:
  103.   // find the start (skip name, etc)
  104.  
  105.   while(*SoundP != ':') SoundP++;    // ignore name
  106.   SoundP++;                     // skip ':'
  107.  
  108.   // get default duration
  109.   if(*SoundP == 'd')
  110.   {
  111.     SoundP++; SoundP++;              // skip "d="
  112.     num = 0;
  113.     while(isdigit(*SoundP))
  114.     {
  115.       num = (num * 10) + (*SoundP++ - '0');
  116.     }
  117.     if(num > 0) default_dur = num;
  118.     SoundP++;                   // skip comma
  119.   }
  120.  
  121.   Serial.print("ddur: "); Serial.println(default_dur, 10);
  122.  
  123.   // get default octave
  124.   if(*SoundP == 'o')
  125.   {
  126.     SoundP++; SoundP++;              // skip "o="
  127.     num = *SoundP++ - '0';
  128.     if(num >= 3 && num <=7) default_oct = num;
  129.     SoundP++;                   // skip comma
  130.   }
  131.  
  132.   Serial.print("doct: "); Serial.println(default_oct, 10);
  133.  
  134.   // get BPM
  135.   if(*SoundP == 'b')
  136.   {
  137.     SoundP++; SoundP++;              // skip "b="
  138.     num = 0;
  139.     while(isdigit(*SoundP))
  140.     {
  141.       num = (num * 10) + (*SoundP++ - '0');
  142.     }
  143.     bpm = num;
  144.     SoundP++;                   // skip colon
  145.   }
  146.  
  147.   Serial.print("bpm: "); Serial.println(bpm, 10);
  148.  
  149.   // BPM usually expresses the number of quarter notes per minute
  150.   wholenote = (60 * 1000L / bpm) * 4;  // this is the time for whole note (in milliseconds)
  151.  
  152.   Serial.print("wn: "); Serial.println(wholenote, 10);
  153.  
  154.  
  155.   // now begin note loop
  156.   while(*SoundP)
  157.   {
  158.     // first, get note duration, if available
  159.     num = 0;
  160.     while(isdigit(*SoundP))
  161.     {
  162.       num = (num * 10) + (*SoundP++ - '0');
  163.     }
  164.    
  165.     if(num) duration = wholenote / num;
  166.     else duration = wholenote / default_dur;  // we will need to check if we are a dotted note after
  167.  
  168.     // now get the note
  169.     note = 0;
  170.  
  171.     switch(*SoundP)
  172.     {
  173.       case 'c':
  174.         note = 1;
  175.         break;
  176.       case 'd':
  177.         note = 3;
  178.         break;
  179.       case 'e':
  180.         note = 5;
  181.         break;
  182.       case 'f':
  183.         note = 6;
  184.         break;
  185.       case 'g':
  186.         note = 8;
  187.         break;
  188.       case 'a':
  189.         note = 10;
  190.         break;
  191.       case 'b':
  192.         note = 12;
  193.         break;
  194.       case 'w':
  195.         woop();
  196.         break;
  197.       case 'p':
  198.       default:
  199.         note = 0;
  200.     }
  201.     SoundP++;
  202.  
  203.     // now, get optional '#' sharp
  204.     if(*SoundP == '#')
  205.     {
  206.       note++;
  207.       SoundP++;
  208.     }
  209.  
  210.     // now, get optional '.' dotted note
  211.     if(*SoundP == '.')
  212.     {
  213.       duration += duration/2;
  214.       SoundP++;
  215.     }
  216.  
  217.     // now, get scale
  218.     if(isdigit(*SoundP))
  219.     {
  220.       scale = *SoundP - '0';
  221.       SoundP++;
  222.     }
  223.     else
  224.     {
  225.       scale = default_oct;
  226.     }
  227.  
  228.     scale += OCTAVE_OFFSET;
  229.  
  230.     if(*SoundP == ',')
  231.       SoundP++;       // skip comma for next note (or we may be at the end)
  232.  
  233.     // now play the note
  234.  
  235.     if(note)
  236.     {
  237.       Serial.print("Playing: ");
  238.       Serial.print(scale, 10); Serial.print(' ');
  239.       Serial.print(note, 10); Serial.print(" (");
  240.       Serial.print(notes[(scale - 4) * 12 + note], 10);
  241.       Serial.print(") ");
  242.       Serial.println(duration, 10);
  243.       tone(TONE_PIN, notes[(scale - 4) * 12 + note]);
  244.       delay(duration);
  245.       noTone(TONE_PIN);
  246.     }
  247.     else
  248.     {
  249.       Serial.print("Pausing: ");
  250.       Serial.println(duration, 10);
  251.       delay(duration);
  252.     }
  253.   }
  254. }
  255.  
  256. void Sound::woop(){
  257.   while(dirfrq<4500)
  258.  {
  259.   tone(13,dirfrq,5);
  260.   delay(1);
  261.   dirfrq+=300;
  262.   noTone(13);
  263.   Serial.println(dirfrq);
  264.   tone(13,dirfrq,2);
  265.   delay(1);
  266.   dirfrq-=50;
  267.   noTone(13);
  268.     Serial.println(dirfrq);
  269.  }
  270.  tone(13,4500);
  271.  delay(1);
  272.  noTone(13);
  273.  dirfrq=220;
  274.   }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×