Simple monophonic synthesizer. - USB MIDI function PWM出力を使用した簡単なモノフォニックシンセです。 USB MIDI Functionとして動作します。 {{http://www.youtube.com/watch?v=UWonXdsNGkE}} {{https://lh4.googleusercontent.com/-LHPkdbs5pdw/TxA07cSVUmI/AAAAAAAACVY/2Bia4nz1ptI/s476/mbedDeMonoSynth.png}}
main.cpp@0:9c32c9c8ead4, 2011-12-29 (annotated)
- Committer:
- alaif
- Date:
- Thu Dec 29 12:44:19 2011 +0000
- Revision:
- 0:9c32c9c8ead4
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
alaif | 0:9c32c9c8ead4 | 1 | /* |
alaif | 0:9c32c9c8ead4 | 2 | * Simple monophonic synthesizer |
alaif | 0:9c32c9c8ead4 | 3 | * auther: alaif. |
alaif | 0:9c32c9c8ead4 | 4 | */ |
alaif | 0:9c32c9c8ead4 | 5 | #include "mbed.h" |
alaif | 0:9c32c9c8ead4 | 6 | #include "USBMIDI.h" |
alaif | 0:9c32c9c8ead4 | 7 | |
alaif | 0:9c32c9c8ead4 | 8 | /* MIDI note no -> FREQ */ |
alaif | 0:9c32c9c8ead4 | 9 | #include "math.h" |
alaif | 0:9c32c9c8ead4 | 10 | #define NOTE2FREQ(note) (440 * pow(2, ((note - 69) / 12.0f))) |
alaif | 0:9c32c9c8ead4 | 11 | |
alaif | 0:9c32c9c8ead4 | 12 | /* HARDWARE Config */ |
alaif | 0:9c32c9c8ead4 | 13 | PwmOut sp1(p21); |
alaif | 0:9c32c9c8ead4 | 14 | DigitalOut led_ready(LED1); |
alaif | 0:9c32c9c8ead4 | 15 | DigitalOut led_noteon(LED2); |
alaif | 0:9c32c9c8ead4 | 16 | |
alaif | 0:9c32c9c8ead4 | 17 | //NOTE ON event |
alaif | 0:9c32c9c8ead4 | 18 | void note_on(int note, int vel) |
alaif | 0:9c32c9c8ead4 | 19 | { |
alaif | 0:9c32c9c8ead4 | 20 | if (vel != 0) { |
alaif | 0:9c32c9c8ead4 | 21 | sp1.period(1.0f / NOTE2FREQ(note)); //Set freq |
alaif | 0:9c32c9c8ead4 | 22 | sp1.write(0.5f); //SQUARE WAVE ON |
alaif | 0:9c32c9c8ead4 | 23 | led_noteon = 1; |
alaif | 0:9c32c9c8ead4 | 24 | } else { |
alaif | 0:9c32c9c8ead4 | 25 | sp1.write(0.0f); //SQUARE WAVE OFF |
alaif | 0:9c32c9c8ead4 | 26 | led_noteon = 0; |
alaif | 0:9c32c9c8ead4 | 27 | } |
alaif | 0:9c32c9c8ead4 | 28 | } |
alaif | 0:9c32c9c8ead4 | 29 | |
alaif | 0:9c32c9c8ead4 | 30 | //NOTE OFF event |
alaif | 0:9c32c9c8ead4 | 31 | void note_off() |
alaif | 0:9c32c9c8ead4 | 32 | { |
alaif | 0:9c32c9c8ead4 | 33 | sp1.write(0.0f); |
alaif | 0:9c32c9c8ead4 | 34 | led_noteon = 0; |
alaif | 0:9c32c9c8ead4 | 35 | } |
alaif | 0:9c32c9c8ead4 | 36 | |
alaif | 0:9c32c9c8ead4 | 37 | //Recv midi message handler. |
alaif | 0:9c32c9c8ead4 | 38 | void do_message(MIDIMessage msg) |
alaif | 0:9c32c9c8ead4 | 39 | { |
alaif | 0:9c32c9c8ead4 | 40 | int key = msg.key(); |
alaif | 0:9c32c9c8ead4 | 41 | switch (msg.type()) { |
alaif | 0:9c32c9c8ead4 | 42 | case MIDIMessage::NoteOnType: |
alaif | 0:9c32c9c8ead4 | 43 | note_on(key, msg.velocity()); |
alaif | 0:9c32c9c8ead4 | 44 | break; |
alaif | 0:9c32c9c8ead4 | 45 | case MIDIMessage::NoteOffType: |
alaif | 0:9c32c9c8ead4 | 46 | note_off(); |
alaif | 0:9c32c9c8ead4 | 47 | break; |
alaif | 0:9c32c9c8ead4 | 48 | } |
alaif | 0:9c32c9c8ead4 | 49 | } |
alaif | 0:9c32c9c8ead4 | 50 | |
alaif | 0:9c32c9c8ead4 | 51 | /* Main */ |
alaif | 0:9c32c9c8ead4 | 52 | USBMIDI midi; |
alaif | 0:9c32c9c8ead4 | 53 | int main() { |
alaif | 0:9c32c9c8ead4 | 54 | led_ready = 0; |
alaif | 0:9c32c9c8ead4 | 55 | midi.attach(do_message); // call back for messages received |
alaif | 0:9c32c9c8ead4 | 56 | led_ready = 1; |
alaif | 0:9c32c9c8ead4 | 57 | |
alaif | 0:9c32c9c8ead4 | 58 | while (1) {} |
alaif | 0:9c32c9c8ead4 | 59 | } |