From 359fba904b16c12b0c334a3d9e6a1790056db661 Mon Sep 17 00:00:00 2001 From: Harvie Date: Mon, 15 Nov 2010 02:04:55 +0100 Subject: [PATCH] Quick hack to get Arduino working with MIDI-rx.c, FIXME later... --- arduino/MIDI_synth/MIDI_synth.pde | 48 +++++++++++++++++++++++++++++++ c/ghetto-sound-system/midi-rx.c | 4 ++- 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 arduino/MIDI_synth/MIDI_synth.pde diff --git a/arduino/MIDI_synth/MIDI_synth.pde b/arduino/MIDI_synth/MIDI_synth.pde new file mode 100644 index 0000000..6585b00 --- /dev/null +++ b/arduino/MIDI_synth/MIDI_synth.pde @@ -0,0 +1,48 @@ +/* + * Simple monophonic MIDI synthesizer :-) + */ + +#define MIDI_NOTE_OFF 128 +#define MIDI_NOTE_ON 144 +#define MIDI_CONTROL 176 +#define MIDI_PITCH_BEND 224 + +#define sndout 13 + +double base_a4=440; //set A4=440Hz +double note_to_freq(double n) { + if( n>=0 && n<=119 ) { + return base_a4*pow(2,(n-57)/12); + } else { + return -1; + } +} + + +void setup() { + Serial.begin(19200); + pinMode(sndout, OUTPUT); +} + +void loop() { + int dela=0; + + while(1) { + + if(Serial.available() >= 3) { + double command=0, channel=0, pitch=0; + command = Serial.read(); + channel = Serial.read(); + pitch = Serial.read(); + if(command == MIDI_NOTE_ON && pitch > 0) dela=((1000000/2)/note_to_freq(pitch)); + if(command == MIDI_NOTE_OFF || pitch == 0) dela = 0; + + Serial.println(note_to_freq(pitch), DEC); + } + + if(dela > 0) digitalWrite(sndout, HIGH); delayMicroseconds(dela); + digitalWrite(sndout, LOW); delayMicroseconds(dela); + + } + +} diff --git a/c/ghetto-sound-system/midi-rx.c b/c/ghetto-sound-system/midi-rx.c index 7ee730e..0ce32d7 100644 --- a/c/ghetto-sound-system/midi-rx.c +++ b/c/ghetto-sound-system/midi-rx.c @@ -11,7 +11,8 @@ #define MIDI_NOTE_ON 144 #define MIDI_CONTROL 176 #define MIDI_PITCH_BEND 224 -#define MIDI_FORMAT "%d:%d:%d\n" +#define MIDI_FORMAT "%c%c%c" +#define MIDI_FORMAT_ERR "%d:%d:%d\n" snd_seq_t *open_seq(); void midi_action(snd_seq_t *seq_handle); @@ -55,6 +56,7 @@ void midi_action(snd_seq_t *seq_handle) { printf(MIDI_FORMAT, MIDI_NOTE_OFF, ev->data.control.channel, ev->data.note.note); break; } + fflush(stdout); fflush(stderr); snd_seq_free_event(ev); } while (snd_seq_event_input_pending(seq_handle, 0) > 0); } -- 2.30.2