summaryrefslogtreecommitdiffstats
path: root/src/midi.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/midi.c')
-rw-r--r--src/midi.c60
1 files changed, 57 insertions, 3 deletions
diff --git a/src/midi.c b/src/midi.c
index e1797c8..0bd44a8 100644
--- a/src/midi.c
+++ b/src/midi.c
@@ -17,6 +17,9 @@ void midi_decode(uint32_t msg, synth_t * synth) {
if (synth->n.key == data1) {
synth->n.noteOff = Pa_GetStreamTime(synth->stream);
}
+
+ synth->midi_note[data1].noteOff = Pa_GetStreamTime(synth->stream);
+
break;
case 0x09:
printf("Note On: channel=%d, note=%d, velocity=%d\n", channel, data1, data2);
@@ -27,7 +30,17 @@ void midi_decode(uint32_t msg, synth_t * synth) {
synth->n.elapsed = 0;
synth->adsr.elapsed = 0;
synth->active = 1;
- synth->gain = data2 / 127.0;
+ //synth->gain = data2 / 127.0;
+
+
+ //synth->midi_note[i].n = -1;
+ synth->midi_note[data1].freq = notes[data1 % 12][(data1 / 12) % 8];
+ synth->midi_note[data1].channel = channel;
+ synth->midi_note[data1].noteOn = Pa_GetStreamTime(synth->stream);
+ synth->midi_note[data1].noteOff = 0;
+ synth->midi_note[data1].velocity = (float)data2 / 127.0;
+ synth->midi_note[data1].elapsed = 0;
+ synth->midi_note[data1].active = 1;
break;
case 0x0A:
@@ -35,6 +48,41 @@ void midi_decode(uint32_t msg, synth_t * synth) {
break;
case 0x0B:
printf("Control Change: channel=%d, controller=%d, value=%d\n", channel, data1, data2);
+ int x = data2 < 64 ? 1 : -1;
+ switch (data1) {
+ case 0:
+ synth->adsr.a = synth->adsr.a + (x * 0.01);
+ break;
+ case 1:
+ synth->adsr.d = synth->adsr.d + (x * 0.01);
+ break;
+ case 2:
+ synth->adsr.s = synth->adsr.s + (x * 0.01);
+ break;
+ case 3:
+ synth->adsr.r = synth->adsr.r + (x * 0.01);
+ break;
+ case 4:
+ synth->cutoff = synth->cutoff + (x * (10 * log10(synth->cutoff)));
+ break;
+ case 5:
+ synth->resonance = synth->resonance + (x * 0.02);
+ break;
+ case 6:
+ synth->lfo.freq = synth->lfo.freq + (x * 0.1);
+ break;
+ case 7:
+ synth->lfo.amp = synth->lfo.amp + (x * 0.002);
+ break;
+ break;
+ case 28:
+ synth->filter = x - 1;
+ break;
+ case 29:
+ synth->multi = x - 1;
+ break;
+ default:
+ }
break;
case 0x0C:
printf("Program Change: channel=%d, program=%d\n", channel, data1);
@@ -44,6 +92,7 @@ void midi_decode(uint32_t msg, synth_t * synth) {
break;
case 0x0E:
printf("Pitch Bend: channel=%d, value=%d\n", channel, ((data2 << 7) | data1) - 8192);
+ synth->n.freq = (((data2 << 7) | data1) - 8192);
break;
default:
printf("Unknown MIDI message\n");
@@ -58,10 +107,15 @@ midiCallback(PtTimestamp timestamp, void *userData) {
if (!m->stream) return;
- if (!Pm_Poll(m->stream)) return;
+ // if (!Pm_Poll(m->stream)) return;
PmEvent buf;
- Pm_Read(m->stream, &buf, 1);
+ int e = Pm_Read(m->stream, &buf, 1);
+ if (e == 0) return; // nothing to do
+ if (e < 0) {
+ printf("Pm_Read error %d\n", e);
+ return;
+ }
//printf("%d\n", buf.message);
midi_decode(buf.message, m->synth);