Synth with C64 like sounds. Played on two piezo buzzers with a PS/2 keyboard. Implemented on FRDM-KL46Z
Dependencies: PS2 TSI beep2 mbed
C64Synth.cpp@4:0f73a5d06177, 2014-01-24 (annotated)
- Committer:
- alexanderh
- Date:
- Fri Jan 24 10:13:23 2014 +0000
- Revision:
- 4:0f73a5d06177
- Parent:
- 3:2cd5dfcad0e6
Doxygen docs
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
alexanderh | 3:2cd5dfcad0e6 | 1 | |
alexanderh | 4:0f73a5d06177 | 2 | /* |
alexanderh | 0:00eec4d97228 | 3 | * C64Synth uses a PS/2 keyboard to play C64 arps and simple tones. |
alexanderh | 0:00eec4d97228 | 4 | * Implemented on a FRDM KL46Z board |
alexanderh | 0:00eec4d97228 | 5 | * |
alexanderh | 0:00eec4d97228 | 6 | * The keyboard is tuned in the C scale as of right now |
alexanderh | 0:00eec4d97228 | 7 | * |
alexanderh | 0:00eec4d97228 | 8 | * Author: Alexander Hentschel 2014 |
alexanderh | 0:00eec4d97228 | 9 | */ |
alexanderh | 0:00eec4d97228 | 10 | |
alexanderh | 0:00eec4d97228 | 11 | /* |
alexanderh | 0:00eec4d97228 | 12 | * TODO LIST |
alexanderh | 0:00eec4d97228 | 13 | * |
alexanderh | 0:00eec4d97228 | 14 | * Implemept sharp (#) notes |
alexanderh | 0:00eec4d97228 | 15 | * Implement scale change |
alexanderh | 0:00eec4d97228 | 16 | * |
alexanderh | 0:00eec4d97228 | 17 | * |
alexanderh | 0:00eec4d97228 | 18 | * |
alexanderh | 0:00eec4d97228 | 19 | */ |
alexanderh | 0:00eec4d97228 | 20 | |
alexanderh | 0:00eec4d97228 | 21 | |
alexanderh | 0:00eec4d97228 | 22 | |
alexanderh | 0:00eec4d97228 | 23 | |
alexanderh | 0:00eec4d97228 | 24 | #include "mbed.h" |
alexanderh | 0:00eec4d97228 | 25 | #include "beep2.h" |
alexanderh | 0:00eec4d97228 | 26 | #include "TSISensor.h" |
alexanderh | 0:00eec4d97228 | 27 | #include "PS2Keyboard.h" |
alexanderh | 0:00eec4d97228 | 28 | #include "stdio.h" |
alexanderh | 0:00eec4d97228 | 29 | #include "keys.h" |
alexanderh | 0:00eec4d97228 | 30 | |
alexanderh | 0:00eec4d97228 | 31 | DigitalOut myled1(LED1); |
alexanderh | 0:00eec4d97228 | 32 | DigitalOut myled2(LED2); |
alexanderh | 0:00eec4d97228 | 33 | |
alexanderh | 0:00eec4d97228 | 34 | Beep arpBuzzer(PTA5); |
alexanderh | 0:00eec4d97228 | 35 | Beep toneBuzzer(PTA12); |
alexanderh | 0:00eec4d97228 | 36 | PS2Keyboard ps2kb(PTC8,PTC9); |
alexanderh | 0:00eec4d97228 | 37 | TSISensor tsi; |
alexanderh | 0:00eec4d97228 | 38 | |
alexanderh | 0:00eec4d97228 | 39 | PS2Keyboard::keyboard_event_t evt_kb; |
alexanderh | 0:00eec4d97228 | 40 | |
alexanderh | 0:00eec4d97228 | 41 | void keyboardSynth(); |
alexanderh | 0:00eec4d97228 | 42 | void playArp(char chord); |
alexanderh | 0:00eec4d97228 | 43 | void playNote(Beep buz,char note,float dc,float len); |
alexanderh | 0:00eec4d97228 | 44 | void playContNote(char note,int oct,float dc); |
alexanderh | 0:00eec4d97228 | 45 | |
alexanderh | 0:00eec4d97228 | 46 | void getNotes(char note, char* point); |
alexanderh | 0:00eec4d97228 | 47 | float getFq(char note, bool sharp); |
alexanderh | 0:00eec4d97228 | 48 | |
alexanderh | 0:00eec4d97228 | 49 | |
alexanderh | 0:00eec4d97228 | 50 | float toneDc = 0.5; |
alexanderh | 0:00eec4d97228 | 51 | char lastNote; |
alexanderh | 0:00eec4d97228 | 52 | |
alexanderh | 0:00eec4d97228 | 53 | bool arpAttack = false; |
alexanderh | 0:00eec4d97228 | 54 | float arpDc = 0.5; |
alexanderh | 0:00eec4d97228 | 55 | float arpSpeed = 0.02; |
alexanderh | 0:00eec4d97228 | 56 | float arpTempovar = 0.1; |
alexanderh | 0:00eec4d97228 | 57 | float arpSpeed2; |
alexanderh | 0:00eec4d97228 | 58 | float arpTime = 0.025; |
alexanderh | 0:00eec4d97228 | 59 | |
alexanderh | 0:00eec4d97228 | 60 | float masterscale=1; |
alexanderh | 0:00eec4d97228 | 61 | |
alexanderh | 0:00eec4d97228 | 62 | int main() |
alexanderh | 0:00eec4d97228 | 63 | { |
alexanderh | 0:00eec4d97228 | 64 | printf("--Program Start--"); |
alexanderh | 0:00eec4d97228 | 65 | myled2 = !myled2; |
alexanderh | 0:00eec4d97228 | 66 | keyboardSynth(); |
alexanderh | 0:00eec4d97228 | 67 | } |
alexanderh | 0:00eec4d97228 | 68 | |
alexanderh | 0:00eec4d97228 | 69 | |
alexanderh | 0:00eec4d97228 | 70 | void keyboardSynth() |
alexanderh | 0:00eec4d97228 | 71 | { |
alexanderh | 0:00eec4d97228 | 72 | int arpOn = 0; |
alexanderh | 0:00eec4d97228 | 73 | int toneOn =0; |
alexanderh | 0:00eec4d97228 | 74 | char keytone = ' '; |
alexanderh | 0:00eec4d97228 | 75 | char playtone = ' '; |
alexanderh | 0:00eec4d97228 | 76 | char playchor = ' '; |
alexanderh | 0:00eec4d97228 | 77 | char chor; |
alexanderh | 0:00eec4d97228 | 78 | char test; |
alexanderh | 0:00eec4d97228 | 79 | int octave; |
alexanderh | 0:00eec4d97228 | 80 | |
alexanderh | 2:a8ec77eed4cc | 81 | |
alexanderh | 0:00eec4d97228 | 82 | //PS/2 keyboard test |
alexanderh | 0:00eec4d97228 | 83 | while(1) { |
alexanderh | 0:00eec4d97228 | 84 | |
alexanderh | 0:00eec4d97228 | 85 | if (ps2kb.processing(&evt_kb)) { |
alexanderh | 0:00eec4d97228 | 86 | |
alexanderh | 0:00eec4d97228 | 87 | //DEBUG keyboard terminalprint |
alexanderh | 0:00eec4d97228 | 88 | //printf("[%d]:", evt_kb.type); |
alexanderh | 0:00eec4d97228 | 89 | //for (int i = 0; i < evt_kb.length; i++) { |
alexanderh | 0:00eec4d97228 | 90 | // printf("%02x ", evt_kb.scancode[i]); |
alexanderh | 0:00eec4d97228 | 91 | // } |
alexanderh | 0:00eec4d97228 | 92 | //printf("\n"); |
alexanderh | 0:00eec4d97228 | 93 | |
alexanderh | 0:00eec4d97228 | 94 | //KEY PRESS PART |
alexanderh | 0:00eec4d97228 | 95 | if (!evt_kb.type) { |
alexanderh | 0:00eec4d97228 | 96 | |
alexanderh | 0:00eec4d97228 | 97 | switch(evt_kb.scancode[0]) { |
alexanderh | 0:00eec4d97228 | 98 | |
alexanderh | 0:00eec4d97228 | 99 | //FUNCTION KEYS |
alexanderh | 0:00eec4d97228 | 100 | case Space_key: |
alexanderh | 0:00eec4d97228 | 101 | arpOn = 0; |
alexanderh | 0:00eec4d97228 | 102 | toneOn = 0; |
alexanderh | 0:00eec4d97228 | 103 | break; |
alexanderh | 0:00eec4d97228 | 104 | |
alexanderh | 0:00eec4d97228 | 105 | //MULTICODE KEYS |
alexanderh | 0:00eec4d97228 | 106 | case Mod_fn: |
alexanderh | 0:00eec4d97228 | 107 | switch(evt_kb.scancode[1]) { |
alexanderh | 0:00eec4d97228 | 108 | |
alexanderh | 0:00eec4d97228 | 109 | case Up_key: |
alexanderh | 0:00eec4d97228 | 110 | masterscale = masterscale*2; |
alexanderh | 0:00eec4d97228 | 111 | break; |
alexanderh | 0:00eec4d97228 | 112 | //Tune down 1 octave |
alexanderh | 0:00eec4d97228 | 113 | case Down_key: |
alexanderh | 0:00eec4d97228 | 114 | masterscale = masterscale/2; |
alexanderh | 0:00eec4d97228 | 115 | break; |
alexanderh | 0:00eec4d97228 | 116 | //Faster arp speed |
alexanderh | 0:00eec4d97228 | 117 | case Pgup_key: |
alexanderh | 0:00eec4d97228 | 118 | arpTime = arpTime/1.2; |
alexanderh | 0:00eec4d97228 | 119 | break; |
alexanderh | 0:00eec4d97228 | 120 | //Slower arp speed |
alexanderh | 0:00eec4d97228 | 121 | case Pgdown_key: |
alexanderh | 0:00eec4d97228 | 122 | arpTime = arpTime*1.2; |
alexanderh | 0:00eec4d97228 | 123 | break; |
alexanderh | 0:00eec4d97228 | 124 | //Brighter sound |
alexanderh | 0:00eec4d97228 | 125 | case Left_key: |
alexanderh | 0:00eec4d97228 | 126 | toneDc = 0.98; |
alexanderh | 0:00eec4d97228 | 127 | break; |
alexanderh | 0:00eec4d97228 | 128 | //Square wave |
alexanderh | 0:00eec4d97228 | 129 | case Right_key: |
alexanderh | 0:00eec4d97228 | 130 | toneDc = 0.5; |
alexanderh | 0:00eec4d97228 | 131 | break; |
alexanderh | 0:00eec4d97228 | 132 | //Arp Attack |
alexanderh | 0:00eec4d97228 | 133 | case End_key: |
alexanderh | 0:00eec4d97228 | 134 | if (!arpAttack) { |
alexanderh | 0:00eec4d97228 | 135 | arpDc = 0; |
alexanderh | 0:00eec4d97228 | 136 | arpSpeed = 0.02; |
alexanderh | 0:00eec4d97228 | 137 | } else { |
alexanderh | 0:00eec4d97228 | 138 | arpDc = 0.5; |
alexanderh | 0:00eec4d97228 | 139 | } |
alexanderh | 0:00eec4d97228 | 140 | arpAttack = !arpAttack; |
alexanderh | 0:00eec4d97228 | 141 | break; |
alexanderh | 0:00eec4d97228 | 142 | } |
alexanderh | 0:00eec4d97228 | 143 | break; |
alexanderh | 0:00eec4d97228 | 144 | |
alexanderh | 0:00eec4d97228 | 145 | //ARPS |
alexanderh | 0:00eec4d97228 | 146 | case Z_key: |
alexanderh | 0:00eec4d97228 | 147 | chor = 'a'; |
alexanderh | 0:00eec4d97228 | 148 | break; |
alexanderh | 0:00eec4d97228 | 149 | case X_key: |
alexanderh | 0:00eec4d97228 | 150 | // chor = 'b' |
alexanderh | 0:00eec4d97228 | 151 | //arpOn = true; |
alexanderh | 0:00eec4d97228 | 152 | break; |
alexanderh | 0:00eec4d97228 | 153 | case C_key: |
alexanderh | 0:00eec4d97228 | 154 | chor = 'C'; |
alexanderh | 0:00eec4d97228 | 155 | break; |
alexanderh | 0:00eec4d97228 | 156 | case V_key: |
alexanderh | 0:00eec4d97228 | 157 | chor = 'd'; |
alexanderh | 0:00eec4d97228 | 158 | break; |
alexanderh | 0:00eec4d97228 | 159 | case B_key: |
alexanderh | 0:00eec4d97228 | 160 | chor = 'e'; |
alexanderh | 0:00eec4d97228 | 161 | break; |
alexanderh | 0:00eec4d97228 | 162 | case N_key: |
alexanderh | 0:00eec4d97228 | 163 | chor = 'F'; |
alexanderh | 0:00eec4d97228 | 164 | break; |
alexanderh | 0:00eec4d97228 | 165 | case M_key: |
alexanderh | 0:00eec4d97228 | 166 | chor = 'G'; |
alexanderh | 0:00eec4d97228 | 167 | break; |
alexanderh | 0:00eec4d97228 | 168 | |
alexanderh | 0:00eec4d97228 | 169 | // -- TONES -- |
alexanderh | 0:00eec4d97228 | 170 | //ROW 1 |
alexanderh | 0:00eec4d97228 | 171 | case A_key: |
alexanderh | 0:00eec4d97228 | 172 | keytone = 'a'; |
alexanderh | 0:00eec4d97228 | 173 | octave = 1; |
alexanderh | 0:00eec4d97228 | 174 | break; |
alexanderh | 0:00eec4d97228 | 175 | case S_key: |
alexanderh | 0:00eec4d97228 | 176 | keytone = 'b'; |
alexanderh | 0:00eec4d97228 | 177 | octave = 1; |
alexanderh | 0:00eec4d97228 | 178 | break; |
alexanderh | 0:00eec4d97228 | 179 | case D_key: |
alexanderh | 0:00eec4d97228 | 180 | keytone = 'c'; |
alexanderh | 0:00eec4d97228 | 181 | octave = 1; |
alexanderh | 0:00eec4d97228 | 182 | break; |
alexanderh | 0:00eec4d97228 | 183 | case F_key: |
alexanderh | 0:00eec4d97228 | 184 | keytone = 'd'; |
alexanderh | 0:00eec4d97228 | 185 | octave = 1; |
alexanderh | 0:00eec4d97228 | 186 | break; |
alexanderh | 0:00eec4d97228 | 187 | case G_key: |
alexanderh | 0:00eec4d97228 | 188 | keytone = 'e'; |
alexanderh | 0:00eec4d97228 | 189 | octave = 1; |
alexanderh | 0:00eec4d97228 | 190 | break; |
alexanderh | 0:00eec4d97228 | 191 | case H_key: |
alexanderh | 0:00eec4d97228 | 192 | keytone = 'f'; |
alexanderh | 0:00eec4d97228 | 193 | octave = 1; |
alexanderh | 0:00eec4d97228 | 194 | break; |
alexanderh | 0:00eec4d97228 | 195 | case J_key: |
alexanderh | 0:00eec4d97228 | 196 | keytone = 'g'; |
alexanderh | 0:00eec4d97228 | 197 | octave = 1; |
alexanderh | 0:00eec4d97228 | 198 | break; |
alexanderh | 0:00eec4d97228 | 199 | |
alexanderh | 0:00eec4d97228 | 200 | //ROW 2 |
alexanderh | 0:00eec4d97228 | 201 | //TODO speaker 3 |
alexanderh | 0:00eec4d97228 | 202 | case Q_key: |
alexanderh | 0:00eec4d97228 | 203 | keytone = 'a'; |
alexanderh | 0:00eec4d97228 | 204 | octave = 2; |
alexanderh | 0:00eec4d97228 | 205 | break; |
alexanderh | 0:00eec4d97228 | 206 | case W_key: |
alexanderh | 0:00eec4d97228 | 207 | keytone = 'b'; |
alexanderh | 0:00eec4d97228 | 208 | octave = 2; |
alexanderh | 0:00eec4d97228 | 209 | break; |
alexanderh | 0:00eec4d97228 | 210 | case E_key: |
alexanderh | 0:00eec4d97228 | 211 | keytone = 'c'; |
alexanderh | 0:00eec4d97228 | 212 | octave = 2; |
alexanderh | 0:00eec4d97228 | 213 | break; |
alexanderh | 0:00eec4d97228 | 214 | case R_key: |
alexanderh | 0:00eec4d97228 | 215 | keytone = 'd'; |
alexanderh | 0:00eec4d97228 | 216 | octave = 2; |
alexanderh | 0:00eec4d97228 | 217 | break; |
alexanderh | 0:00eec4d97228 | 218 | case T_key: |
alexanderh | 0:00eec4d97228 | 219 | keytone = 'e'; |
alexanderh | 0:00eec4d97228 | 220 | octave = 2; |
alexanderh | 0:00eec4d97228 | 221 | break; |
alexanderh | 0:00eec4d97228 | 222 | case Y_key: |
alexanderh | 0:00eec4d97228 | 223 | keytone = 'f'; |
alexanderh | 0:00eec4d97228 | 224 | octave = 2; |
alexanderh | 0:00eec4d97228 | 225 | break; |
alexanderh | 0:00eec4d97228 | 226 | case U_key: |
alexanderh | 0:00eec4d97228 | 227 | keytone = 'g'; |
alexanderh | 0:00eec4d97228 | 228 | octave = 2; |
alexanderh | 0:00eec4d97228 | 229 | break; |
alexanderh | 0:00eec4d97228 | 230 | } |
alexanderh | 0:00eec4d97228 | 231 | |
alexanderh | 0:00eec4d97228 | 232 | //Set the tone that should be played. Only do this if the key is not already playing |
alexanderh | 0:00eec4d97228 | 233 | if(keytone != playtone) { |
alexanderh | 0:00eec4d97228 | 234 | playtone = keytone; |
alexanderh | 0:00eec4d97228 | 235 | toneOn++; |
alexanderh | 0:00eec4d97228 | 236 | } |
alexanderh | 0:00eec4d97228 | 237 | |
alexanderh | 0:00eec4d97228 | 238 | //Set the chord that should be played. Only do this if the key is not already playing |
alexanderh | 0:00eec4d97228 | 239 | if(chor != playchor) { |
alexanderh | 0:00eec4d97228 | 240 | playchor = chor; |
alexanderh | 0:00eec4d97228 | 241 | arpOn++; |
alexanderh | 0:00eec4d97228 | 242 | } |
alexanderh | 0:00eec4d97228 | 243 | |
alexanderh | 0:00eec4d97228 | 244 | |
alexanderh | 0:00eec4d97228 | 245 | //KEY RELEASE PART |
alexanderh | 0:00eec4d97228 | 246 | } else { |
alexanderh | 0:00eec4d97228 | 247 | test = evt_kb.scancode[1]; |
alexanderh | 0:00eec4d97228 | 248 | |
alexanderh | 0:00eec4d97228 | 249 | //Arp key is released |
alexanderh | 0:00eec4d97228 | 250 | if ( test == Z_key || /*test == S_key ||*/ test == C_key || test == V_key || test == B_key || test == N_key || test == M_key) { |
alexanderh | 0:00eec4d97228 | 251 | |
alexanderh | 0:00eec4d97228 | 252 | //ATTACK MODE |
alexanderh | 0:00eec4d97228 | 253 | if (arpAttack && arpOn == 1) { |
alexanderh | 0:00eec4d97228 | 254 | arpDc = 0; |
alexanderh | 0:00eec4d97228 | 255 | arpSpeed = 0.02; |
alexanderh | 0:00eec4d97228 | 256 | } |
alexanderh | 0:00eec4d97228 | 257 | arpOn--; |
alexanderh | 0:00eec4d97228 | 258 | } |
alexanderh | 0:00eec4d97228 | 259 | //Tone key is released |
alexanderh | 0:00eec4d97228 | 260 | if ( test == Q_key || test == W_key || test == E_key || test == R_key || test == T_key || test == Y_key || test == U_key || |
alexanderh | 0:00eec4d97228 | 261 | test == A_key || test == S_key || test == D_key || test == F_key || test == G_key || test == H_key || test == J_key) { |
alexanderh | 0:00eec4d97228 | 262 | toneOn--; |
alexanderh | 0:00eec4d97228 | 263 | } |
alexanderh | 0:00eec4d97228 | 264 | } |
alexanderh | 0:00eec4d97228 | 265 | } |
alexanderh | 0:00eec4d97228 | 266 | |
alexanderh | 0:00eec4d97228 | 267 | //TONE HANDLER |
alexanderh | 0:00eec4d97228 | 268 | if (toneOn> 0) { |
alexanderh | 3:2cd5dfcad0e6 | 269 | playContNote(playtone,octave,0.9); |
alexanderh | 0:00eec4d97228 | 270 | } else { |
alexanderh | 0:00eec4d97228 | 271 | keytone = ' '; |
alexanderh | 0:00eec4d97228 | 272 | playtone = ' '; |
alexanderh | 0:00eec4d97228 | 273 | lastNote = ' '; |
alexanderh | 0:00eec4d97228 | 274 | toneBuzzer.nobeep(); |
alexanderh | 0:00eec4d97228 | 275 | toneOn = 0; |
alexanderh | 0:00eec4d97228 | 276 | } |
alexanderh | 0:00eec4d97228 | 277 | |
alexanderh | 0:00eec4d97228 | 278 | //CHORD/ARP HANDLER |
alexanderh | 0:00eec4d97228 | 279 | if (arpOn>0) { |
alexanderh | 0:00eec4d97228 | 280 | playArp(playchor); |
alexanderh | 0:00eec4d97228 | 281 | } else { |
alexanderh | 0:00eec4d97228 | 282 | chor = ' '; |
alexanderh | 0:00eec4d97228 | 283 | playchor = ' '; |
alexanderh | 0:00eec4d97228 | 284 | arpOn = 0; |
alexanderh | 0:00eec4d97228 | 285 | } |
alexanderh | 0:00eec4d97228 | 286 | } |
alexanderh | 0:00eec4d97228 | 287 | } |
alexanderh | 0:00eec4d97228 | 288 | |
alexanderh | 4:0f73a5d06177 | 289 | /** |
alexanderh | 0:00eec4d97228 | 290 | * |
alexanderh | 1:9cc595290b2f | 291 | * Plays an arpeggio of the provided chord. Arp speed can be controlled by touch slider or pgup/pgdown setting. PWM duty cycle is sweeped automatically |
alexanderh | 0:00eec4d97228 | 292 | * |
alexanderh | 1:9cc595290b2f | 293 | * @param chord the chord to be played UPPER CASE is major chords, lower case is minor chords. |
alexanderh | 0:00eec4d97228 | 294 | * |
alexanderh | 0:00eec4d97228 | 295 | */ |
alexanderh | 0:00eec4d97228 | 296 | void playArp(char chord) |
alexanderh | 0:00eec4d97228 | 297 | { |
alexanderh | 0:00eec4d97228 | 298 | static int arpCounter; |
alexanderh | 0:00eec4d97228 | 299 | static char theChord[3]; |
alexanderh | 0:00eec4d97228 | 300 | getNotes(chord, theChord); |
alexanderh | 0:00eec4d97228 | 301 | |
alexanderh | 0:00eec4d97228 | 302 | //keep duty cycle between 0.03 and 0.97. |
alexanderh | 0:00eec4d97228 | 303 | if((arpDc > 0.98 && arpSpeed >0)|| (arpDc < 0.02 && arpSpeed <0)) { |
alexanderh | 0:00eec4d97228 | 304 | arpSpeed = arpSpeed*-1; |
alexanderh | 0:00eec4d97228 | 305 | } |
alexanderh | 0:00eec4d97228 | 306 | |
alexanderh | 0:00eec4d97228 | 307 | arpSpeed2 = tsi.readPercentage()/15; |
alexanderh | 0:00eec4d97228 | 308 | //Play the correct note in the chord. Vary the duty cycle and tempo for cool C64 arp effect |
alexanderh | 0:00eec4d97228 | 309 | playNote(arpBuzzer,theChord[arpCounter],arpDc,arpTime+arpTempovar*arpSpeed+arpSpeed2); |
alexanderh | 0:00eec4d97228 | 310 | |
alexanderh | 0:00eec4d97228 | 311 | arpDc = arpDc+arpSpeed; |
alexanderh | 0:00eec4d97228 | 312 | |
alexanderh | 0:00eec4d97228 | 313 | //Reset the notes |
alexanderh | 0:00eec4d97228 | 314 | arpCounter++; |
alexanderh | 0:00eec4d97228 | 315 | if (arpCounter == 3) { |
alexanderh | 0:00eec4d97228 | 316 | arpCounter= 0; |
alexanderh | 0:00eec4d97228 | 317 | } |
alexanderh | 0:00eec4d97228 | 318 | } |
alexanderh | 0:00eec4d97228 | 319 | |
alexanderh | 4:0f73a5d06177 | 320 | /** |
alexanderh | 0:00eec4d97228 | 321 | * |
alexanderh | 3:2cd5dfcad0e6 | 322 | * Plays a note on the provided buzzer |
alexanderh | 0:00eec4d97228 | 323 | * |
alexanderh | 1:9cc595290b2f | 324 | * @param buz the buzzer to play the note on, |
alexanderh | 1:9cc595290b2f | 325 | * @param note the note to be played |
alexanderh | 1:9cc595290b2f | 326 | * @param dc the duty cycle value |
alexanderh | 3:2cd5dfcad0e6 | 327 | * @param len the length of the note |
alexanderh | 0:00eec4d97228 | 328 | * |
alexanderh | 0:00eec4d97228 | 329 | */ |
alexanderh | 0:00eec4d97228 | 330 | void playNote(Beep buz, char note,float dc,float len) |
alexanderh | 0:00eec4d97228 | 331 | { |
alexanderh | 0:00eec4d97228 | 332 | buz.beep(getFq(note,false),len,dc); |
alexanderh | 0:00eec4d97228 | 333 | myled1 = !myled1; |
alexanderh | 0:00eec4d97228 | 334 | myled2 = !myled2; |
alexanderh | 0:00eec4d97228 | 335 | if (note=='w') { |
alexanderh | 0:00eec4d97228 | 336 | wait(0.05); |
alexanderh | 0:00eec4d97228 | 337 | } |
alexanderh | 0:00eec4d97228 | 338 | wait (len); //wait while the note plays. |
alexanderh | 0:00eec4d97228 | 339 | } |
alexanderh | 0:00eec4d97228 | 340 | |
alexanderh | 0:00eec4d97228 | 341 | |
alexanderh | 3:2cd5dfcad0e6 | 342 | /** -- PlayContNote -- |
alexanderh | 0:00eec4d97228 | 343 | * |
alexanderh | 1:9cc595290b2f | 344 | * Plays a continuous note on the second buzzer |
alexanderh | 0:00eec4d97228 | 345 | * |
alexanderh | 1:9cc595290b2f | 346 | * @param note the note to be played |
alexanderh | 1:9cc595290b2f | 347 | * @param oct the octave |
alexanderh | 3:2cd5dfcad0e6 | 348 | * @param dc the duty cycle of the note |
alexanderh | 0:00eec4d97228 | 349 | */ |
alexanderh | 0:00eec4d97228 | 350 | void playContNote(char note,int oct,float dc) |
alexanderh | 0:00eec4d97228 | 351 | { |
alexanderh | 0:00eec4d97228 | 352 | static float fq; |
alexanderh | 0:00eec4d97228 | 353 | |
alexanderh | 0:00eec4d97228 | 354 | if(lastNote != note) { |
alexanderh | 0:00eec4d97228 | 355 | fq = getFq(note,false); |
alexanderh | 0:00eec4d97228 | 356 | toneBuzzer.beepNoStop(fq*oct/2,toneDc); |
alexanderh | 0:00eec4d97228 | 357 | myled1 = !myled1; |
alexanderh | 0:00eec4d97228 | 358 | myled2 = !myled2; |
alexanderh | 0:00eec4d97228 | 359 | } else { |
alexanderh | 0:00eec4d97228 | 360 | toneBuzzer.changeDc(toneDc); |
alexanderh | 0:00eec4d97228 | 361 | } |
alexanderh | 0:00eec4d97228 | 362 | |
alexanderh | 0:00eec4d97228 | 363 | //wait (len); //wait while the note plays. |
alexanderh | 0:00eec4d97228 | 364 | lastNote = note; |
alexanderh | 0:00eec4d97228 | 365 | } |
alexanderh | 0:00eec4d97228 | 366 | |
alexanderh | 3:2cd5dfcad0e6 | 367 | /** |
alexanderh | 0:00eec4d97228 | 368 | * |
alexanderh | 1:9cc595290b2f | 369 | * Gets the notes in a chord |
alexanderh | 0:00eec4d97228 | 370 | * |
alexanderh | 1:9cc595290b2f | 371 | * @param chord the chord |
alexanderh | 1:9cc595290b2f | 372 | * @param notes the array to save the notes in |
alexanderh | 0:00eec4d97228 | 373 | * |
alexanderh | 0:00eec4d97228 | 374 | */ |
alexanderh | 0:00eec4d97228 | 375 | |
alexanderh | 0:00eec4d97228 | 376 | void getNotes(char chord, char* notes) |
alexanderh | 0:00eec4d97228 | 377 | { |
alexanderh | 0:00eec4d97228 | 378 | if (chord=='a') { |
alexanderh | 0:00eec4d97228 | 379 | notes[0] = 'a'; |
alexanderh | 0:00eec4d97228 | 380 | notes[1] = 'c'; |
alexanderh | 0:00eec4d97228 | 381 | notes[2] = 'e'; |
alexanderh | 0:00eec4d97228 | 382 | } |
alexanderh | 0:00eec4d97228 | 383 | if (chord=='C') { |
alexanderh | 0:00eec4d97228 | 384 | notes[0] = 'g'; |
alexanderh | 0:00eec4d97228 | 385 | notes[1] = 'c'; |
alexanderh | 0:00eec4d97228 | 386 | notes[2] = 'e'; |
alexanderh | 0:00eec4d97228 | 387 | } |
alexanderh | 0:00eec4d97228 | 388 | if (chord=='F') { |
alexanderh | 0:00eec4d97228 | 389 | notes[0] = 'a'; |
alexanderh | 0:00eec4d97228 | 390 | notes[1] = 'c'; |
alexanderh | 0:00eec4d97228 | 391 | notes[2] = 'f'; |
alexanderh | 0:00eec4d97228 | 392 | } |
alexanderh | 0:00eec4d97228 | 393 | if (chord=='G') { |
alexanderh | 0:00eec4d97228 | 394 | notes[0] = 'g'; |
alexanderh | 0:00eec4d97228 | 395 | notes[1] = 'b'; |
alexanderh | 0:00eec4d97228 | 396 | notes[2] = 'd'; |
alexanderh | 0:00eec4d97228 | 397 | } |
alexanderh | 0:00eec4d97228 | 398 | if (chord=='d') { |
alexanderh | 0:00eec4d97228 | 399 | notes[0] = 'a'; |
alexanderh | 0:00eec4d97228 | 400 | notes[1] = 'd'; |
alexanderh | 0:00eec4d97228 | 401 | notes[2] = 'f'; |
alexanderh | 0:00eec4d97228 | 402 | } |
alexanderh | 0:00eec4d97228 | 403 | if (chord=='e') { |
alexanderh | 0:00eec4d97228 | 404 | notes[0] = 'g'; |
alexanderh | 0:00eec4d97228 | 405 | notes[1] = 'b'; |
alexanderh | 0:00eec4d97228 | 406 | notes[2] = 'e'; |
alexanderh | 0:00eec4d97228 | 407 | } |
alexanderh | 0:00eec4d97228 | 408 | } |
alexanderh | 0:00eec4d97228 | 409 | |
alexanderh | 0:00eec4d97228 | 410 | |
alexanderh | 3:2cd5dfcad0e6 | 411 | /** |
alexanderh | 0:00eec4d97228 | 412 | * |
alexanderh | 1:9cc595290b2f | 413 | * Gets the frequency of the note |
alexanderh | 0:00eec4d97228 | 414 | * |
alexanderh | 1:9cc595290b2f | 415 | * @param note the note |
alexanderh | 0:00eec4d97228 | 416 | * |
alexanderh | 1:9cc595290b2f | 417 | * @return the frequency as float |
alexanderh | 0:00eec4d97228 | 418 | */ |
alexanderh | 0:00eec4d97228 | 419 | float getFq(char note, bool sharp) |
alexanderh | 0:00eec4d97228 | 420 | { |
alexanderh | 0:00eec4d97228 | 421 | if(!sharp ) { |
alexanderh | 0:00eec4d97228 | 422 | if (note=='a') { |
alexanderh | 0:00eec4d97228 | 423 | return 880*masterscale; |
alexanderh | 0:00eec4d97228 | 424 | } |
alexanderh | 0:00eec4d97228 | 425 | if (note=='b') { |
alexanderh | 0:00eec4d97228 | 426 | return 987.76*masterscale; |
alexanderh | 0:00eec4d97228 | 427 | } |
alexanderh | 0:00eec4d97228 | 428 | if (note=='c') { |
alexanderh | 0:00eec4d97228 | 429 | return 1046.5*masterscale; |
alexanderh | 0:00eec4d97228 | 430 | } |
alexanderh | 0:00eec4d97228 | 431 | if (note=='d') { |
alexanderh | 0:00eec4d97228 | 432 | return 1174.66*masterscale; |
alexanderh | 0:00eec4d97228 | 433 | } |
alexanderh | 0:00eec4d97228 | 434 | if (note=='e') { |
alexanderh | 0:00eec4d97228 | 435 | return 1318.51*masterscale; |
alexanderh | 0:00eec4d97228 | 436 | } |
alexanderh | 0:00eec4d97228 | 437 | if (note=='f') { |
alexanderh | 0:00eec4d97228 | 438 | return 1396.91*masterscale; |
alexanderh | 0:00eec4d97228 | 439 | } |
alexanderh | 0:00eec4d97228 | 440 | if (note=='g') { |
alexanderh | 0:00eec4d97228 | 441 | return 1567.98*masterscale; |
alexanderh | 0:00eec4d97228 | 442 | } |
alexanderh | 0:00eec4d97228 | 443 | |
alexanderh | 0:00eec4d97228 | 444 | //Sharps # |
alexanderh | 0:00eec4d97228 | 445 | } else { |
alexanderh | 0:00eec4d97228 | 446 | if (note=='f') { |
alexanderh | 0:00eec4d97228 | 447 | return 1479.98*masterscale; |
alexanderh | 0:00eec4d97228 | 448 | } |
alexanderh | 0:00eec4d97228 | 449 | } |
alexanderh | 0:00eec4d97228 | 450 | return 0; |
alexanderh | 0:00eec4d97228 | 451 | } |
alexanderh | 0:00eec4d97228 | 452 |