mbed working as a midi device through usb D-, D+
Dependencies: USBDevice beep mbed
main.cpp@0:f24bc002bd2e, 2013-11-19 (annotated)
- Committer:
- ghostaudio
- Date:
- Tue Nov 19 03:23:23 2013 +0000
- Revision:
- 0:f24bc002bd2e
Improvement on the beep method i used before
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ghostaudio | 0:f24bc002bd2e | 1 | // Edited from the Hello World example for the USBMIDI library |
ghostaudio | 0:f24bc002bd2e | 2 | // to play notes from the PwmOut using beep..... |
ghostaudio | 0:f24bc002bd2e | 3 | |
ghostaudio | 0:f24bc002bd2e | 4 | #include "mbed.h" |
ghostaudio | 0:f24bc002bd2e | 5 | #include "USBMIDI.h" |
ghostaudio | 0:f24bc002bd2e | 6 | #include "beep.h" |
ghostaudio | 0:f24bc002bd2e | 7 | |
ghostaudio | 0:f24bc002bd2e | 8 | //USBMIDI object |
ghostaudio | 0:f24bc002bd2e | 9 | USBMIDI midi; |
ghostaudio | 0:f24bc002bd2e | 10 | |
ghostaudio | 0:f24bc002bd2e | 11 | Beep buzzer(p22); // piezo one of two( this only needs one, |
ghostaudio | 0:f24bc002bd2e | 12 | Beep buzzer2(p21); // but piezo two made it more fun)..... |
ghostaudio | 0:f24bc002bd2e | 13 | AnalogIn pot (p19); // and a potentiometer..... |
ghostaudio | 0:f24bc002bd2e | 14 | |
ghostaudio | 0:f24bc002bd2e | 15 | int freq = 0; |
ghostaudio | 0:f24bc002bd2e | 16 | |
ghostaudio | 0:f24bc002bd2e | 17 | // an array of frequencies from C2-C4 for freq..... |
ghostaudio | 0:f24bc002bd2e | 18 | int freqs[] = {261,277,293,311,329,349,369,391,415,440,466,493,523, |
ghostaudio | 0:f24bc002bd2e | 19 | 554,587,622,659,698,739,783,830,880,932,987,1046}; |
ghostaudio | 0:f24bc002bd2e | 20 | |
ghostaudio | 0:f24bc002bd2e | 21 | //defining your midi notes as points in the array..... |
ghostaudio | 0:f24bc002bd2e | 22 | void show_message(MIDIMessage msg) { |
ghostaudio | 0:f24bc002bd2e | 23 | if (msg.type() == MIDIMessage::NoteOnType) { |
ghostaudio | 0:f24bc002bd2e | 24 | if (msg.velocity()!=0) { |
ghostaudio | 0:f24bc002bd2e | 25 | freq = freqs[(msg.key()-48)]; |
ghostaudio | 0:f24bc002bd2e | 26 | } |
ghostaudio | 0:f24bc002bd2e | 27 | else { |
ghostaudio | 0:f24bc002bd2e | 28 | freq = 0; |
ghostaudio | 0:f24bc002bd2e | 29 | } |
ghostaudio | 0:f24bc002bd2e | 30 | } |
ghostaudio | 0:f24bc002bd2e | 31 | } |
ghostaudio | 0:f24bc002bd2e | 32 | |
ghostaudio | 0:f24bc002bd2e | 33 | int main() { |
ghostaudio | 0:f24bc002bd2e | 34 | |
ghostaudio | 0:f24bc002bd2e | 35 | // call back for midi messages received |
ghostaudio | 0:f24bc002bd2e | 36 | midi.attach(show_message); |
ghostaudio | 0:f24bc002bd2e | 37 | |
ghostaudio | 0:f24bc002bd2e | 38 | while (1) { |
ghostaudio | 0:f24bc002bd2e | 39 | |
ghostaudio | 0:f24bc002bd2e | 40 | float time = (pot/2); // assign the pot to the time... |
ghostaudio | 0:f24bc002bd2e | 41 | if (freq > 0){ |
ghostaudio | 0:f24bc002bd2e | 42 | buzzer.beep(freq,0.1); // between this beep... |
ghostaudio | 0:f24bc002bd2e | 43 | wait (time); |
ghostaudio | 0:f24bc002bd2e | 44 | buzzer2.beep((freq*2),0.1); // and this beep (1 octave up). |
ghostaudio | 0:f24bc002bd2e | 45 | wait (time); |
ghostaudio | 0:f24bc002bd2e | 46 | } |
ghostaudio | 0:f24bc002bd2e | 47 | |
ghostaudio | 0:f24bc002bd2e | 48 | wait (0.0001); // wait a millesecond and carry on... |
ghostaudio | 0:f24bc002bd2e | 49 | } |
ghostaudio | 0:f24bc002bd2e | 50 | } |