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