Flappy Bird game on mbed with a micro LCD screen, class D amp, speaker, SD card reader/writer, 5-button navigation switch, and potentiometer speed control
Dependencies: 4DGL-uLCD-SE PinDetect SDFileSystem mbed-rtos mbed
soundBuilder.cpp@0:cd1d2540aaf4, 2016-03-15 (annotated)
- Committer:
- Mpmart08
- Date:
- Tue Mar 15 01:11:07 2016 +0000
- Revision:
- 0:cd1d2540aaf4
added comments
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Mpmart08 | 0:cd1d2540aaf4 | 1 | #include "Speaker.h" |
Mpmart08 | 0:cd1d2540aaf4 | 2 | #include "soundBuilder.h" |
Mpmart08 | 0:cd1d2540aaf4 | 3 | |
Mpmart08 | 0:cd1d2540aaf4 | 4 | Note::Note(){ |
Mpmart08 | 0:cd1d2540aaf4 | 5 | setFreq(0); |
Mpmart08 | 0:cd1d2540aaf4 | 6 | setLength(0); |
Mpmart08 | 0:cd1d2540aaf4 | 7 | setVolume(0); |
Mpmart08 | 0:cd1d2540aaf4 | 8 | } |
Mpmart08 | 0:cd1d2540aaf4 | 9 | |
Mpmart08 | 0:cd1d2540aaf4 | 10 | Note::Note(float freq, float dur, float vol){ |
Mpmart08 | 0:cd1d2540aaf4 | 11 | setFreq(freq); |
Mpmart08 | 0:cd1d2540aaf4 | 12 | setLength(dur); |
Mpmart08 | 0:cd1d2540aaf4 | 13 | setVolume(vol); |
Mpmart08 | 0:cd1d2540aaf4 | 14 | } |
Mpmart08 | 0:cd1d2540aaf4 | 15 | |
Mpmart08 | 0:cd1d2540aaf4 | 16 | void Note::setFreq(float nfreq){ |
Mpmart08 | 0:cd1d2540aaf4 | 17 | freq = nfreq; |
Mpmart08 | 0:cd1d2540aaf4 | 18 | } |
Mpmart08 | 0:cd1d2540aaf4 | 19 | |
Mpmart08 | 0:cd1d2540aaf4 | 20 | void Note::setLength(float dur){ |
Mpmart08 | 0:cd1d2540aaf4 | 21 | length = dur; |
Mpmart08 | 0:cd1d2540aaf4 | 22 | } |
Mpmart08 | 0:cd1d2540aaf4 | 23 | |
Mpmart08 | 0:cd1d2540aaf4 | 24 | void Note::setVolume(float vol){ |
Mpmart08 | 0:cd1d2540aaf4 | 25 | volume = vol; |
Mpmart08 | 0:cd1d2540aaf4 | 26 | } |
Mpmart08 | 0:cd1d2540aaf4 | 27 | |
Mpmart08 | 0:cd1d2540aaf4 | 28 | float Note::getFreq(){ |
Mpmart08 | 0:cd1d2540aaf4 | 29 | return freq; |
Mpmart08 | 0:cd1d2540aaf4 | 30 | } |
Mpmart08 | 0:cd1d2540aaf4 | 31 | |
Mpmart08 | 0:cd1d2540aaf4 | 32 | float Note::getLength(){ |
Mpmart08 | 0:cd1d2540aaf4 | 33 | return length; |
Mpmart08 | 0:cd1d2540aaf4 | 34 | } |
Mpmart08 | 0:cd1d2540aaf4 | 35 | |
Mpmart08 | 0:cd1d2540aaf4 | 36 | float Note::getVolume(){ |
Mpmart08 | 0:cd1d2540aaf4 | 37 | return volume; |
Mpmart08 | 0:cd1d2540aaf4 | 38 | } |
Mpmart08 | 0:cd1d2540aaf4 | 39 | |
Mpmart08 | 0:cd1d2540aaf4 | 40 | SoundBuilder::SoundBuilder(float freq[], float dur[], float vol[], int numNotes, Speaker *mainspeaker){ |
Mpmart08 | 0:cd1d2540aaf4 | 41 | // add each note to the song |
Mpmart08 | 0:cd1d2540aaf4 | 42 | for (int i = 0; i < numNotes; i++){ |
Mpmart08 | 0:cd1d2540aaf4 | 43 | song[i].setFreq(freq[i]); |
Mpmart08 | 0:cd1d2540aaf4 | 44 | song[i].setLength(dur[i]); |
Mpmart08 | 0:cd1d2540aaf4 | 45 | song[i].setVolume(vol[i]); |
Mpmart08 | 0:cd1d2540aaf4 | 46 | } |
Mpmart08 | 0:cd1d2540aaf4 | 47 | speaker = &(*mainspeaker); |
Mpmart08 | 0:cd1d2540aaf4 | 48 | } |
Mpmart08 | 0:cd1d2540aaf4 | 49 | |
Mpmart08 | 0:cd1d2540aaf4 | 50 | void SoundBuilder::setNote(float freq, float dur, float vol, int noteNum){ |
Mpmart08 | 0:cd1d2540aaf4 | 51 | // set frequency, length, and volume of the specified note |
Mpmart08 | 0:cd1d2540aaf4 | 52 | song[noteNum].setFreq(freq); |
Mpmart08 | 0:cd1d2540aaf4 | 53 | song[noteNum].setLength(dur); |
Mpmart08 | 0:cd1d2540aaf4 | 54 | song[noteNum].setVolume(vol); |
Mpmart08 | 0:cd1d2540aaf4 | 55 | } |
Mpmart08 | 0:cd1d2540aaf4 | 56 | |
Mpmart08 | 0:cd1d2540aaf4 | 57 | void SoundBuilder::playNotes(int start, int stop){ |
Mpmart08 | 0:cd1d2540aaf4 | 58 | // play each note in the song successively from given start and stop positions |
Mpmart08 | 0:cd1d2540aaf4 | 59 | for (int i = start; i <= stop; i++) |
Mpmart08 | 0:cd1d2540aaf4 | 60 | speaker->PlayNote(song[i].getFreq(), song[i].getLength(), song[i].getVolume()); |
Mpmart08 | 0:cd1d2540aaf4 | 61 | } |
Mpmart08 | 0:cd1d2540aaf4 | 62 | |
Mpmart08 | 0:cd1d2540aaf4 | 63 | void SoundBuilder::playSong(){ |
Mpmart08 | 0:cd1d2540aaf4 | 64 | // play each note in the song successively from the beginning to the end |
Mpmart08 | 0:cd1d2540aaf4 | 65 | playNotes(0, sizeof(song)/sizeof(*song)-1); |
Mpmart08 | 0:cd1d2540aaf4 | 66 | } |
Mpmart08 | 0:cd1d2540aaf4 | 67 | |
Mpmart08 | 0:cd1d2540aaf4 | 68 | void SoundBuilder::clearSong(){ |
Mpmart08 | 0:cd1d2540aaf4 | 69 | // clear all note data from the song |
Mpmart08 | 0:cd1d2540aaf4 | 70 | for (int i = 0; i < sizeof(song)/sizeof(*song); i++){ |
Mpmart08 | 0:cd1d2540aaf4 | 71 | song[i].setFreq(0); |
Mpmart08 | 0:cd1d2540aaf4 | 72 | song[i].setLength(0); |
Mpmart08 | 0:cd1d2540aaf4 | 73 | song[i].setVolume(0); |
Mpmart08 | 0:cd1d2540aaf4 | 74 | } |
Mpmart08 | 0:cd1d2540aaf4 | 75 | } |