![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
4 songs: rain, william, sun, and circus
Songs.cpp@0:c02f01feda7b, 2015-05-09 (annotated)
- Committer:
- Luminoscity
- Date:
- Sat May 09 21:29:00 2015 +0000
- Revision:
- 0:c02f01feda7b
- Child:
- 1:3595fce69880
Successful Music Test Code!!!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Luminoscity | 0:c02f01feda7b | 1 | #include "mbed.h" |
Luminoscity | 0:c02f01feda7b | 2 | #include "Songs.h" |
Luminoscity | 0:c02f01feda7b | 3 | |
Luminoscity | 0:c02f01feda7b | 4 | Serial sTerm(USBTX, USBRX); |
Luminoscity | 0:c02f01feda7b | 5 | |
Luminoscity | 0:c02f01feda7b | 6 | Song::Song() { |
Luminoscity | 0:c02f01feda7b | 7 | choose("rain"); |
Luminoscity | 0:c02f01feda7b | 8 | } |
Luminoscity | 0:c02f01feda7b | 9 | |
Luminoscity | 0:c02f01feda7b | 10 | Song::Song(char *choice) { |
Luminoscity | 0:c02f01feda7b | 11 | choose(choice); |
Luminoscity | 0:c02f01feda7b | 12 | } |
Luminoscity | 0:c02f01feda7b | 13 | |
Luminoscity | 0:c02f01feda7b | 14 | void Song::play() { |
Luminoscity | 0:c02f01feda7b | 15 | |
Luminoscity | 0:c02f01feda7b | 16 | for(int i = 0; i < length; i++) { |
Luminoscity | 0:c02f01feda7b | 17 | if(notes[i] == ' ') { |
Luminoscity | 0:c02f01feda7b | 18 | wait_us(beats[i] * tempo * 1000); |
Luminoscity | 0:c02f01feda7b | 19 | } else { |
Luminoscity | 0:c02f01feda7b | 20 | playNote(notes[i], range[i], beats[i] * tempo); |
Luminoscity | 0:c02f01feda7b | 21 | } |
Luminoscity | 0:c02f01feda7b | 22 | wait_us(betweenWait); /* delay between notes */ |
Luminoscity | 0:c02f01feda7b | 23 | } |
Luminoscity | 0:c02f01feda7b | 24 | } |
Luminoscity | 0:c02f01feda7b | 25 | |
Luminoscity | 0:c02f01feda7b | 26 | void Song::choose(char *choice) { |
Luminoscity | 0:c02f01feda7b | 27 | |
Luminoscity | 0:c02f01feda7b | 28 | if (!strcmp(choice, "sun")) { |
Luminoscity | 0:c02f01feda7b | 29 | int beatsRef[] = {2,4,4,5,2,2,2,5,4,2,4,4,8,1,2,4,4,1,4,4,4,4}; |
Luminoscity | 0:c02f01feda7b | 30 | notes = "CbCaCabC CbCa aba Cba "; |
Luminoscity | 0:c02f01feda7b | 31 | int rangeRef[] = {6,5,6,5,6,5,5,6,0,6,5,6,5,0,5,5,5,0,6,5,5,0}; |
Luminoscity | 0:c02f01feda7b | 32 | tempo = 150; |
Luminoscity | 0:c02f01feda7b | 33 | betweenWait = 1; |
Luminoscity | 0:c02f01feda7b | 34 | length = sizeof(beatsRef) / sizeof(beatsRef[0]); |
Luminoscity | 0:c02f01feda7b | 35 | for (int i = 0; i < length; ++i) { |
Luminoscity | 0:c02f01feda7b | 36 | beats[i] = beatsRef[i]; |
Luminoscity | 0:c02f01feda7b | 37 | range[i] = rangeRef[i]; |
Luminoscity | 0:c02f01feda7b | 38 | } |
Luminoscity | 0:c02f01feda7b | 39 | } |
Luminoscity | 0:c02f01feda7b | 40 | else if (!strcmp(choice, "william")) { |
Luminoscity | 0:c02f01feda7b | 41 | int beatsRef[] = {1,1,2,1,1,2,1,1,2,2,2,1,1,2,1,1,2,1,1,2,2,2,1,1,2,1,1,2,1,1,2,2,2,1,1,5,1,1,1,2,2,2,4}; |
Luminoscity | 0:c02f01feda7b | 42 | notes = "ddddddddgabdddddgbbaFdddddddddgabgbdcbagbg "; |
Luminoscity | 0:c02f01feda7b | 43 | int rangeRef[] = {5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6,6,5,5,5,5,5,0}; |
Luminoscity | 0:c02f01feda7b | 44 | tempo = 90; |
Luminoscity | 0:c02f01feda7b | 45 | betweenWait = 20000; |
Luminoscity | 0:c02f01feda7b | 46 | length = sizeof(beatsRef) / sizeof(beatsRef[0]); |
Luminoscity | 0:c02f01feda7b | 47 | for (int i = 0; i < length; ++i) { |
Luminoscity | 0:c02f01feda7b | 48 | beats[i] = beatsRef[i]; |
Luminoscity | 0:c02f01feda7b | 49 | range[i] = rangeRef[i]; |
Luminoscity | 0:c02f01feda7b | 50 | } |
Luminoscity | 0:c02f01feda7b | 51 | } |
Luminoscity | 0:c02f01feda7b | 52 | else if (!strcmp(choice, "circus")) { |
Luminoscity | 0:c02f01feda7b | 53 | int beatsRef[] = {2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,4}; |
Luminoscity | 0:c02f01feda7b | 54 | notes = "cCdDefFgGaAbcCdDefFgGaAbcCdDefFgGaAbc "; |
Luminoscity | 0:c02f01feda7b | 55 | int rangeRef[] = {4,4,4,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,7,0}; |
Luminoscity | 0:c02f01feda7b | 56 | tempo = 100; |
Luminoscity | 0:c02f01feda7b | 57 | betweenWait = 1; |
Luminoscity | 0:c02f01feda7b | 58 | length = sizeof(beatsRef) / sizeof(beatsRef[0]); |
Luminoscity | 0:c02f01feda7b | 59 | for (int i = 0; i < length; ++i) { |
Luminoscity | 0:c02f01feda7b | 60 | beats[i] = beatsRef[i]; |
Luminoscity | 0:c02f01feda7b | 61 | range[i] = rangeRef[i]; |
Luminoscity | 0:c02f01feda7b | 62 | } |
Luminoscity | 0:c02f01feda7b | 63 | } |
Luminoscity | 0:c02f01feda7b | 64 | else { //rain |
Luminoscity | 0:c02f01feda7b | 65 | int beatsRef[] = {4,2,4,2,4,2,4,2,4,2,3,1,2,2,2,2,2,2,2,4}; |
Luminoscity | 0:c02f01feda7b | 66 | notes = "ecdbcabgacbcdecdbca "; |
Luminoscity | 0:c02f01feda7b | 67 | int rangeRef[] = {6,6,6,5,6,5,5,5,5,6,5,6,6,6,6,6,5,6,5,0}; |
Luminoscity | 0:c02f01feda7b | 68 | tempo = 120; |
Luminoscity | 0:c02f01feda7b | 69 | betweenWait = 1; |
Luminoscity | 0:c02f01feda7b | 70 | length = sizeof(beatsRef) / sizeof(beatsRef[0]); |
Luminoscity | 0:c02f01feda7b | 71 | for (int i = 0; i < length; ++i) { |
Luminoscity | 0:c02f01feda7b | 72 | beats[i] = beatsRef[i]; |
Luminoscity | 0:c02f01feda7b | 73 | range[i] = rangeRef[i]; |
Luminoscity | 0:c02f01feda7b | 74 | } |
Luminoscity | 0:c02f01feda7b | 75 | } |
Luminoscity | 0:c02f01feda7b | 76 | } |
Luminoscity | 0:c02f01feda7b | 77 | |
Luminoscity | 0:c02f01feda7b | 78 | void Song::setTempo(int setT) { |
Luminoscity | 0:c02f01feda7b | 79 | tempo = setT; |
Luminoscity | 0:c02f01feda7b | 80 | } |
Luminoscity | 0:c02f01feda7b | 81 | |
Luminoscity | 0:c02f01feda7b | 82 |