diff --git a/FM/FM.ino b/FM/FM.ino index 5ac6690..be35e06 100644 --- a/FM/FM.ino +++ b/FM/FM.ino @@ -13,6 +13,8 @@ MIDI_CREATE_DEFAULT_INSTANCE(); #define MIDI_CHANNEL 3 bool gate = 0; +byte noteBuffer[8]; +byte bufferIndex = 0; // desired intensity max and min, for AutoMap, note they're inverted for reverse dynamics const int MIN_INTENSITY = 700; @@ -54,19 +56,51 @@ int carrier_freq; void noteOn(byte channel, byte note, byte velocity) { if (channel == MIDI_CHANNEL) { - carrier_freq = mtof((int) note); gate = 1; + updateBuffer(note, gate); + //playNote(); // this is called from within buffer update } } void noteOff(byte channel, byte note, byte velocity) { if (channel == MIDI_CHANNEL) { gate = 0; + updateBuffer(note, gate); } } +void updateBuffer(byte bNote, bool bGate) { + if (bGate == 1) { + noteBuffer[bufferIndex] = bNote; + playNote(); + bufferIndex++; + } + if (bGate == 0 && bufferIndex != 0) { + for (int i = 0; i <= bufferIndex; i++) { + if (noteBuffer[i] == bNote) { + // Removing the note from the array and shifting everything to close the gap + for (int n = i; n <= bufferIndex; n++) { + noteBuffer[n] = noteBuffer[n + 1]; // what happens if it's the last note in buffer? + } + if (i == bufferIndex) { + bufferIndex--; + playNote(); + } else { + bufferIndex--; + } + } + } + } +} + +void playNote() { + byte note = noteBuffer[bufferIndex]; + carrier_freq = mtof((int) note); +} + void setup(){ - pinMode(LED_BUILTIN_TX,INPUT); //switch rx and tx leds of, so they don't blink on midi + // switch rx and tx leds of, so they don't blink on midi + pinMode(LED_BUILTIN_TX,INPUT); pinMode(LED_BUILTIN_RX,INPUT); startMozzi();