Tool for playing 8bit like music on Piezo speaker.
PlayTone.cpp@0:b8d34e475dd9, 2016-10-01 (annotated)
- Committer:
- TeaPack_CZ
- Date:
- Sat Oct 01 20:48:12 2016 +0000
- Revision:
- 0:b8d34e475dd9
- Child:
- 2:9a8ae3a8e5ea
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
TeaPack_CZ | 0:b8d34e475dd9 | 1 | #include "PlayTone.h" |
TeaPack_CZ | 0:b8d34e475dd9 | 2 | |
TeaPack_CZ | 0:b8d34e475dd9 | 3 | PlayTone::PlayTone(PinName PS):_speaker(PS) |
TeaPack_CZ | 0:b8d34e475dd9 | 4 | { |
TeaPack_CZ | 0:b8d34e475dd9 | 5 | offset = 0; |
TeaPack_CZ | 0:b8d34e475dd9 | 6 | |
TeaPack_CZ | 0:b8d34e475dd9 | 7 | max_BPM = 260; |
TeaPack_CZ | 0:b8d34e475dd9 | 8 | min_BPM = 40; |
TeaPack_CZ | 0:b8d34e475dd9 | 9 | |
TeaPack_CZ | 0:b8d34e475dd9 | 10 | tone[0] = 27.50; |
TeaPack_CZ | 0:b8d34e475dd9 | 11 | tone[1] = 29.14; |
TeaPack_CZ | 0:b8d34e475dd9 | 12 | tone[2] = 30.87; |
TeaPack_CZ | 0:b8d34e475dd9 | 13 | tone[3] = 32.70; |
TeaPack_CZ | 0:b8d34e475dd9 | 14 | tone[4] = 34.65; |
TeaPack_CZ | 0:b8d34e475dd9 | 15 | tone[5] = 36.71; |
TeaPack_CZ | 0:b8d34e475dd9 | 16 | tone[6] = 38.89; |
TeaPack_CZ | 0:b8d34e475dd9 | 17 | tone[7] = 41.20; |
TeaPack_CZ | 0:b8d34e475dd9 | 18 | tone[8] = 43.65; |
TeaPack_CZ | 0:b8d34e475dd9 | 19 | tone[9] = 46.25; |
TeaPack_CZ | 0:b8d34e475dd9 | 20 | tone[10] = 49.00; |
TeaPack_CZ | 0:b8d34e475dd9 | 21 | tone[11] = 51.91; |
TeaPack_CZ | 0:b8d34e475dd9 | 22 | tone[12] = 55.00; |
TeaPack_CZ | 0:b8d34e475dd9 | 23 | tone[13] = 58.27; |
TeaPack_CZ | 0:b8d34e475dd9 | 24 | tone[14] = 61.74; |
TeaPack_CZ | 0:b8d34e475dd9 | 25 | tone[15] = 65.41; |
TeaPack_CZ | 0:b8d34e475dd9 | 26 | tone[16] = 69.30; |
TeaPack_CZ | 0:b8d34e475dd9 | 27 | tone[17] = 73.42; |
TeaPack_CZ | 0:b8d34e475dd9 | 28 | tone[18] = 77.78; |
TeaPack_CZ | 0:b8d34e475dd9 | 29 | tone[19] = 82.41; |
TeaPack_CZ | 0:b8d34e475dd9 | 30 | tone[20] = 87.31; |
TeaPack_CZ | 0:b8d34e475dd9 | 31 | tone[21] = 92.50; |
TeaPack_CZ | 0:b8d34e475dd9 | 32 | tone[22] = 98.00; |
TeaPack_CZ | 0:b8d34e475dd9 | 33 | tone[23] = 103.83; |
TeaPack_CZ | 0:b8d34e475dd9 | 34 | tone[24] = 110.00; |
TeaPack_CZ | 0:b8d34e475dd9 | 35 | tone[25] = 116.54; |
TeaPack_CZ | 0:b8d34e475dd9 | 36 | tone[26] = 123.47; |
TeaPack_CZ | 0:b8d34e475dd9 | 37 | tone[27] = 130.81; |
TeaPack_CZ | 0:b8d34e475dd9 | 38 | tone[28] = 138.59; |
TeaPack_CZ | 0:b8d34e475dd9 | 39 | tone[29] = 146.83; |
TeaPack_CZ | 0:b8d34e475dd9 | 40 | tone[30] = 155.56; |
TeaPack_CZ | 0:b8d34e475dd9 | 41 | tone[31] = 164.81; |
TeaPack_CZ | 0:b8d34e475dd9 | 42 | tone[32] = 174.61; |
TeaPack_CZ | 0:b8d34e475dd9 | 43 | tone[33] = 185.00; |
TeaPack_CZ | 0:b8d34e475dd9 | 44 | tone[34] = 196.00; |
TeaPack_CZ | 0:b8d34e475dd9 | 45 | tone[35] = 207.65, |
TeaPack_CZ | 0:b8d34e475dd9 | 46 | tone[36] = 220.00; |
TeaPack_CZ | 0:b8d34e475dd9 | 47 | tone[37] = 233.08; |
TeaPack_CZ | 0:b8d34e475dd9 | 48 | tone[38] = 246.94; |
TeaPack_CZ | 0:b8d34e475dd9 | 49 | tone[39] = 261.63; |
TeaPack_CZ | 0:b8d34e475dd9 | 50 | tone[40] = 277.18; |
TeaPack_CZ | 0:b8d34e475dd9 | 51 | tone[41] = 293.66; |
TeaPack_CZ | 0:b8d34e475dd9 | 52 | tone[42] = 311.13; |
TeaPack_CZ | 0:b8d34e475dd9 | 53 | tone[43] = 329.63; |
TeaPack_CZ | 0:b8d34e475dd9 | 54 | tone[44] = 349.23; |
TeaPack_CZ | 0:b8d34e475dd9 | 55 | tone[45] = 369.99; |
TeaPack_CZ | 0:b8d34e475dd9 | 56 | tone[46] = 392.00; |
TeaPack_CZ | 0:b8d34e475dd9 | 57 | tone[47] = 415.30, |
TeaPack_CZ | 0:b8d34e475dd9 | 58 | tone[48] = 440.00; |
TeaPack_CZ | 0:b8d34e475dd9 | 59 | tone[49] = 466.16; |
TeaPack_CZ | 0:b8d34e475dd9 | 60 | tone[50] = 493.88; |
TeaPack_CZ | 0:b8d34e475dd9 | 61 | tone[51] = 523.25; |
TeaPack_CZ | 0:b8d34e475dd9 | 62 | tone[52] = 554.37; |
TeaPack_CZ | 0:b8d34e475dd9 | 63 | tone[53] = 587.33; |
TeaPack_CZ | 0:b8d34e475dd9 | 64 | tone[54] = 622.25; |
TeaPack_CZ | 0:b8d34e475dd9 | 65 | tone[55] = 659.26; |
TeaPack_CZ | 0:b8d34e475dd9 | 66 | tone[56] = 698.46; |
TeaPack_CZ | 0:b8d34e475dd9 | 67 | tone[57] = 739.99; |
TeaPack_CZ | 0:b8d34e475dd9 | 68 | tone[58] = 783.99; |
TeaPack_CZ | 0:b8d34e475dd9 | 69 | tone[59] = 830.61, |
TeaPack_CZ | 0:b8d34e475dd9 | 70 | tone[60] = 880.00; |
TeaPack_CZ | 0:b8d34e475dd9 | 71 | tone[61] = 932.33; |
TeaPack_CZ | 0:b8d34e475dd9 | 72 | tone[62] = 987.77; |
TeaPack_CZ | 0:b8d34e475dd9 | 73 | tone[63] = 1046.50; |
TeaPack_CZ | 0:b8d34e475dd9 | 74 | tone[64] = 1108.73; |
TeaPack_CZ | 0:b8d34e475dd9 | 75 | tone[65] = 1174.66; |
TeaPack_CZ | 0:b8d34e475dd9 | 76 | tone[66] = 1244.51; |
TeaPack_CZ | 0:b8d34e475dd9 | 77 | tone[67] = 1318.51; |
TeaPack_CZ | 0:b8d34e475dd9 | 78 | tone[68] = 1396.91; |
TeaPack_CZ | 0:b8d34e475dd9 | 79 | tone[69] = 1479.98; |
TeaPack_CZ | 0:b8d34e475dd9 | 80 | tone[70] = 1567.98; |
TeaPack_CZ | 0:b8d34e475dd9 | 81 | tone[71] = 1661.22, |
TeaPack_CZ | 0:b8d34e475dd9 | 82 | tone[72] = 1760.00; |
TeaPack_CZ | 0:b8d34e475dd9 | 83 | tone[73] = 1864.66; |
TeaPack_CZ | 0:b8d34e475dd9 | 84 | tone[74] = 1975.53; |
TeaPack_CZ | 0:b8d34e475dd9 | 85 | tone[75] = 2093.00; |
TeaPack_CZ | 0:b8d34e475dd9 | 86 | tone[76] = 2217.46; |
TeaPack_CZ | 0:b8d34e475dd9 | 87 | tone[77] = 2349.32; |
TeaPack_CZ | 0:b8d34e475dd9 | 88 | tone[78] = 2489.02; |
TeaPack_CZ | 0:b8d34e475dd9 | 89 | tone[79] = 2637.02; |
TeaPack_CZ | 0:b8d34e475dd9 | 90 | tone[80] = 2793.83; |
TeaPack_CZ | 0:b8d34e475dd9 | 91 | tone[81] = 2959.96; |
TeaPack_CZ | 0:b8d34e475dd9 | 92 | tone[82] = 3135.96; |
TeaPack_CZ | 0:b8d34e475dd9 | 93 | tone[83] = 3322.44; |
TeaPack_CZ | 0:b8d34e475dd9 | 94 | tone[84] = 3520.00; |
TeaPack_CZ | 0:b8d34e475dd9 | 95 | } |
TeaPack_CZ | 0:b8d34e475dd9 | 96 | |
TeaPack_CZ | 0:b8d34e475dd9 | 97 | void PlayTone::playTone(int ind, float value) |
TeaPack_CZ | 0:b8d34e475dd9 | 98 | { |
TeaPack_CZ | 0:b8d34e475dd9 | 99 | int pos = ind+offset; |
TeaPack_CZ | 0:b8d34e475dd9 | 100 | |
TeaPack_CZ | 0:b8d34e475dd9 | 101 | pos = (pos<0)? 0 : pos; |
TeaPack_CZ | 0:b8d34e475dd9 | 102 | pos = (pos>84)? 84 : pos; |
TeaPack_CZ | 0:b8d34e475dd9 | 103 | |
TeaPack_CZ | 0:b8d34e475dd9 | 104 | _speaker.play(tone[pos], _bpm_ms*value); |
TeaPack_CZ | 0:b8d34e475dd9 | 105 | } |
TeaPack_CZ | 0:b8d34e475dd9 | 106 | |
TeaPack_CZ | 0:b8d34e475dd9 | 107 | void PlayTone::playStaccato(int ind, float value) |
TeaPack_CZ | 0:b8d34e475dd9 | 108 | { |
TeaPack_CZ | 0:b8d34e475dd9 | 109 | int pos = ind+offset; |
TeaPack_CZ | 0:b8d34e475dd9 | 110 | |
TeaPack_CZ | 0:b8d34e475dd9 | 111 | pos = (pos<0)? 0 : pos; |
TeaPack_CZ | 0:b8d34e475dd9 | 112 | pos = (pos>84)? 84 : pos; |
TeaPack_CZ | 0:b8d34e475dd9 | 113 | |
TeaPack_CZ | 0:b8d34e475dd9 | 114 | _speaker.play(tone[pos], _bpm_ms*_duty); |
TeaPack_CZ | 0:b8d34e475dd9 | 115 | int rest = _bpm_ms*float(value - _duty); |
TeaPack_CZ | 0:b8d34e475dd9 | 116 | wait_ms(rest); |
TeaPack_CZ | 0:b8d34e475dd9 | 117 | } |
TeaPack_CZ | 0:b8d34e475dd9 | 118 | |
TeaPack_CZ | 0:b8d34e475dd9 | 119 | void PlayTone::silence(float value) |
TeaPack_CZ | 0:b8d34e475dd9 | 120 | { |
TeaPack_CZ | 0:b8d34e475dd9 | 121 | wait_ms(_bpm_ms*value); |
TeaPack_CZ | 0:b8d34e475dd9 | 122 | } |
TeaPack_CZ | 0:b8d34e475dd9 | 123 | |
TeaPack_CZ | 0:b8d34e475dd9 | 124 | void PlayTone::stop() |
TeaPack_CZ | 0:b8d34e475dd9 | 125 | { |
TeaPack_CZ | 0:b8d34e475dd9 | 126 | _speaker.stop(); |
TeaPack_CZ | 0:b8d34e475dd9 | 127 | } |
TeaPack_CZ | 0:b8d34e475dd9 | 128 | |
TeaPack_CZ | 0:b8d34e475dd9 | 129 | void PlayTone::transpose(int level) |
TeaPack_CZ | 0:b8d34e475dd9 | 130 | { |
TeaPack_CZ | 0:b8d34e475dd9 | 131 | offset += level; |
TeaPack_CZ | 0:b8d34e475dd9 | 132 | } |
TeaPack_CZ | 0:b8d34e475dd9 | 133 | |
TeaPack_CZ | 0:b8d34e475dd9 | 134 | void PlayTone::playSequence(int sequence_length, int notes[], float values[]) |
TeaPack_CZ | 0:b8d34e475dd9 | 135 | { |
TeaPack_CZ | 0:b8d34e475dd9 | 136 | for(int i=0; i< sequence_length; i++) |
TeaPack_CZ | 0:b8d34e475dd9 | 137 | { |
TeaPack_CZ | 0:b8d34e475dd9 | 138 | if(notes[i] > 1) |
TeaPack_CZ | 0:b8d34e475dd9 | 139 | playTone(notes[i],values[i]); |
TeaPack_CZ | 0:b8d34e475dd9 | 140 | else |
TeaPack_CZ | 0:b8d34e475dd9 | 141 | silence(values[i]); |
TeaPack_CZ | 0:b8d34e475dd9 | 142 | } |
TeaPack_CZ | 0:b8d34e475dd9 | 143 | } |
TeaPack_CZ | 0:b8d34e475dd9 | 144 | |
TeaPack_CZ | 0:b8d34e475dd9 | 145 | void PlayTone::playSequence(int sequence_length, Note notes[]) |
TeaPack_CZ | 0:b8d34e475dd9 | 146 | { |
TeaPack_CZ | 0:b8d34e475dd9 | 147 | for(int i=0; i< sequence_length; i++) |
TeaPack_CZ | 0:b8d34e475dd9 | 148 | { |
TeaPack_CZ | 0:b8d34e475dd9 | 149 | if(notes[i].idx > 1) |
TeaPack_CZ | 0:b8d34e475dd9 | 150 | playTone(notes[i].idx,notes[i].value); |
TeaPack_CZ | 0:b8d34e475dd9 | 151 | else |
TeaPack_CZ | 0:b8d34e475dd9 | 152 | silence(notes[i].value); |
TeaPack_CZ | 0:b8d34e475dd9 | 153 | } |
TeaPack_CZ | 0:b8d34e475dd9 | 154 | } |
TeaPack_CZ | 0:b8d34e475dd9 | 155 | |
TeaPack_CZ | 0:b8d34e475dd9 | 156 | void PlayTone::playStaccatoSequence(int sequence_length, int notes[], float values[]) |
TeaPack_CZ | 0:b8d34e475dd9 | 157 | { |
TeaPack_CZ | 0:b8d34e475dd9 | 158 | for(int i=0; i< sequence_length; i++) |
TeaPack_CZ | 0:b8d34e475dd9 | 159 | { |
TeaPack_CZ | 0:b8d34e475dd9 | 160 | if(notes[i] > 0) |
TeaPack_CZ | 0:b8d34e475dd9 | 161 | playStaccato(notes[i],values[i]); |
TeaPack_CZ | 0:b8d34e475dd9 | 162 | else |
TeaPack_CZ | 0:b8d34e475dd9 | 163 | silence(values[i]); |
TeaPack_CZ | 0:b8d34e475dd9 | 164 | } |
TeaPack_CZ | 0:b8d34e475dd9 | 165 | } |
TeaPack_CZ | 0:b8d34e475dd9 | 166 | |
TeaPack_CZ | 0:b8d34e475dd9 | 167 | void PlayTone::playStaccatoSequence(int sequence_length, Note notes[]) |
TeaPack_CZ | 0:b8d34e475dd9 | 168 | { |
TeaPack_CZ | 0:b8d34e475dd9 | 169 | for(int i=0; i< sequence_length; i++) |
TeaPack_CZ | 0:b8d34e475dd9 | 170 | { |
TeaPack_CZ | 0:b8d34e475dd9 | 171 | if(notes[i].idx > 0) |
TeaPack_CZ | 0:b8d34e475dd9 | 172 | playStaccato(notes[i].idx,notes[i].value); |
TeaPack_CZ | 0:b8d34e475dd9 | 173 | else |
TeaPack_CZ | 0:b8d34e475dd9 | 174 | silence(notes[i].value); |
TeaPack_CZ | 0:b8d34e475dd9 | 175 | } |
TeaPack_CZ | 0:b8d34e475dd9 | 176 | } |
TeaPack_CZ | 0:b8d34e475dd9 | 177 | |
TeaPack_CZ | 0:b8d34e475dd9 | 178 | int PlayTone::setBPM(int Bpm) |
TeaPack_CZ | 0:b8d34e475dd9 | 179 | { |
TeaPack_CZ | 0:b8d34e475dd9 | 180 | _bpm = (Bpm < min_BPM)? min_BPM : Bpm; |
TeaPack_CZ | 0:b8d34e475dd9 | 181 | _bpm = (Bpm > max_BPM)? max_BPM : Bpm; |
TeaPack_CZ | 0:b8d34e475dd9 | 182 | |
TeaPack_CZ | 0:b8d34e475dd9 | 183 | _bpm_ms = 60*1000/_bpm; |
TeaPack_CZ | 0:b8d34e475dd9 | 184 | |
TeaPack_CZ | 0:b8d34e475dd9 | 185 | return int(_bpm_ms); |
TeaPack_CZ | 0:b8d34e475dd9 | 186 | } |
TeaPack_CZ | 0:b8d34e475dd9 | 187 | |
TeaPack_CZ | 0:b8d34e475dd9 | 188 | void PlayTone::setStaccatoDuty(float duty) |
TeaPack_CZ | 0:b8d34e475dd9 | 189 | { |
TeaPack_CZ | 0:b8d34e475dd9 | 190 | _duty = (duty > 1)? 1 : duty; |
TeaPack_CZ | 0:b8d34e475dd9 | 191 | _duty = (duty < 0)? 0 : duty; |
TeaPack_CZ | 0:b8d34e475dd9 | 192 | } |