diff --git a/include/dsp/midi.hpp b/include/dsp/midi.hpp index b8103f42..38892abb 100644 --- a/include/dsp/midi.hpp +++ b/include/dsp/midi.hpp @@ -16,6 +16,7 @@ CHANNELS is the number of polyphony channels. Use 1 for monophonic. */ template struct MidiGenerator { + uint8_t channels; int8_t vels[CHANNELS]; int8_t notes[CHANNELS]; bool gates[CHANNELS]; @@ -34,6 +35,7 @@ struct MidiGenerator { } void reset() { + channels = CHANNELS; for (int c = 0; c < CHANNELS; c++) { vels[c] = 100; notes[c] = 60; @@ -65,6 +67,16 @@ struct MidiGenerator { } } + void setChannels(uint8_t channels) { + if (this->channels == channels) + return; + // Disable notes when channels decreases + for (uint8_t c = channels; c < this->channels; c++) { + setNoteGate(notes[c], false, c); + } + this->channels = channels; + } + /** Must be called before setNoteGate(). */ void setVelocity(int8_t vel, int c) { vels[c] = vel; diff --git a/src/core/CV_MIDI.cpp b/src/core/CV_MIDI.cpp index 0dc503d0..0329ebd0 100644 --- a/src/core/CV_MIDI.cpp +++ b/src/core/CV_MIDI.cpp @@ -7,6 +7,7 @@ namespace core { struct MidiOutput : dsp::MidiGenerator, midi::Output { void onMessage(const midi::Message& message) override { + // DEBUG("MIDI: %ld %s", message.getFrame(), message.toString().c_str()); Output::sendMessage(message); } @@ -78,7 +79,10 @@ struct CV_MIDI : Module { midiOutput.setFrame(args.frame); - for (int c = 0; c < inputs[PITCH_INPUT].getChannels(); c++) { + uint8_t channels = inputs[PITCH_INPUT].getChannels(); + midiOutput.setChannels(channels); + + for (int c = 0; c < channels; c++) { int vel = (int) std::round(inputs[VEL_INPUT].getNormalPolyVoltage(10.f * 100 / 127, c) / 10.f * 127); vel = clamp(vel, 0, 127); midiOutput.setVelocity(vel, c);