Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///h///
- ///////
- #ifndef Sound_h
- #include "WProgram.h"
- #define Sound_h
- #define OCTAVE_OFFSET 0
- #define TONE_PIN 13
- #define NOTE_C4 262
- #define NOTE_CS4 277
- #define NOTE_D4 294
- #define NOTE_DS4 311
- #define NOTE_E4 330
- #define NOTE_F4 349
- #define NOTE_FS4 370
- #define NOTE_G4 392
- #define NOTE_GS4 415
- #define NOTE_A4 440
- #define NOTE_AS4 466
- #define NOTE_B4 494
- #define NOTE_C5 523
- #define NOTE_CS5 554
- #define NOTE_D5 587
- #define NOTE_DS5 622
- #define NOTE_E5 659
- #define NOTE_F5 698
- #define NOTE_FS5 740
- #define NOTE_G5 784
- #define NOTE_GS5 831
- #define NOTE_A5 880
- #define NOTE_AS5 932
- #define NOTE_B5 988
- #define NOTE_C6 1047
- #define NOTE_CS6 1109
- #define NOTE_D6 1175
- #define NOTE_DS6 1245
- #define NOTE_E6 1319
- #define NOTE_F6 1397
- #define NOTE_FS6 1480
- #define NOTE_G6 1568
- #define NOTE_GS6 1661
- #define NOTE_A6 1760
- #define NOTE_AS6 1865
- #define NOTE_B6 1976
- #define NOTE_C7 2093
- #define NOTE_CS7 2217
- #define NOTE_D7 2349
- #define NOTE_DS7 2489
- #define NOTE_E7 2637
- #define NOTE_F7 2794
- #define NOTE_FS7 2960
- #define NOTE_G7 3136
- #define NOTE_GS7 3322
- #define NOTE_A7 3520
- #define NOTE_AS7 3729
- #define NOTE_B7 3951
- #define isdigit(n) (n >= '0' && n <= '9')
- class Sound
- {
- public:
- void play_rtttl(char *SoundP);
- int dirfrq;
- char* songs[];
- char* goods[];
- char* bads[];
- private:
- void woop(void);
- byte default_dur;
- byte default_oct;
- int bpm;
- int num;
- long wholenote;
- long duration;
- byte note;
- byte scale;
- };
- 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};
- #endif
- //////////
- ///cpp////
- #include "WProgram.h"
- #include "Sound.h"
- void Sound::play_rtttl(char *SoundP){
- // Absolutely no error checking in here
- ;
- default_dur = 4;
- default_oct = 6;
- bpm = 63;
- num;
- wholenote;
- duration;
- note;
- scale;
- // format: d=N,o=N,b=NNN:
- // find the start (skip name, etc)
- while(*SoundP != ':') SoundP++; // ignore name
- SoundP++; // skip ':'
- // get default duration
- if(*SoundP == 'd')
- {
- SoundP++; SoundP++; // skip "d="
- num = 0;
- while(isdigit(*SoundP))
- {
- num = (num * 10) + (*SoundP++ - '0');
- }
- if(num > 0) default_dur = num;
- SoundP++; // skip comma
- }
- Serial.print("ddur: "); Serial.println(default_dur, 10);
- // get default octave
- if(*SoundP == 'o')
- {
- SoundP++; SoundP++; // skip "o="
- num = *SoundP++ - '0';
- if(num >= 3 && num <=7) default_oct = num;
- SoundP++; // skip comma
- }
- Serial.print("doct: "); Serial.println(default_oct, 10);
- // get BPM
- if(*SoundP == 'b')
- {
- SoundP++; SoundP++; // skip "b="
- num = 0;
- while(isdigit(*SoundP))
- {
- num = (num * 10) + (*SoundP++ - '0');
- }
- bpm = num;
- SoundP++; // skip colon
- }
- Serial.print("bpm: "); Serial.println(bpm, 10);
- // BPM usually expresses the number of quarter notes per minute
- wholenote = (60 * 1000L / bpm) * 4; // this is the time for whole note (in milliseconds)
- Serial.print("wn: "); Serial.println(wholenote, 10);
- // now begin note loop
- while(*SoundP)
- {
- // first, get note duration, if available
- num = 0;
- while(isdigit(*SoundP))
- {
- num = (num * 10) + (*SoundP++ - '0');
- }
- if(num) duration = wholenote / num;
- else duration = wholenote / default_dur; // we will need to check if we are a dotted note after
- // now get the note
- note = 0;
- switch(*SoundP)
- {
- case 'c':
- note = 1;
- break;
- case 'd':
- note = 3;
- break;
- case 'e':
- note = 5;
- break;
- case 'f':
- note = 6;
- break;
- case 'g':
- note = 8;
- break;
- case 'a':
- note = 10;
- break;
- case 'b':
- note = 12;
- break;
- case 'w':
- woop();
- break;
- case 'p':
- default:
- note = 0;
- }
- SoundP++;
- // now, get optional '#' sharp
- if(*SoundP == '#')
- {
- note++;
- SoundP++;
- }
- // now, get optional '.' dotted note
- if(*SoundP == '.')
- {
- duration += duration/2;
- SoundP++;
- }
- // now, get scale
- if(isdigit(*SoundP))
- {
- scale = *SoundP - '0';
- SoundP++;
- }
- else
- {
- scale = default_oct;
- }
- scale += OCTAVE_OFFSET;
- if(*SoundP == ',')
- SoundP++; // skip comma for next note (or we may be at the end)
- // now play the note
- if(note)
- {
- Serial.print("Playing: ");
- Serial.print(scale, 10); Serial.print(' ');
- Serial.print(note, 10); Serial.print(" (");
- Serial.print(notes[(scale - 4) * 12 + note], 10);
- Serial.print(") ");
- Serial.println(duration, 10);
- tone(TONE_PIN, notes[(scale - 4) * 12 + note]);
- delay(duration);
- noTone(TONE_PIN);
- }
- else
- {
- Serial.print("Pausing: ");
- Serial.println(duration, 10);
- delay(duration);
- }
- }
- }
- void Sound::woop(){
- while(dirfrq<4500)
- {
- tone(13,dirfrq,5);
- delay(1);
- dirfrq+=300;
- noTone(13);
- Serial.println(dirfrq);
- tone(13,dirfrq,2);
- delay(1);
- dirfrq-=50;
- noTone(13);
- Serial.println(dirfrq);
- }
- tone(13,4500);
- delay(1);
- noTone(13);
- dirfrq=220;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement