diff --git a/src/core/CV_MIDI.cpp b/src/core/CV_MIDI.cpp index 278227dc..a463f159 100644 --- a/src/core/CV_MIDI.cpp +++ b/src/core/CV_MIDI.cpp @@ -44,7 +44,7 @@ struct CV_MIDI : Module { }; MidiOutput midiOutput; - float rateLimiterPhase = 0.f; + dsp::Timer rateLimiterTimer; CV_MIDI() { config(NUM_PARAMS, NUM_INPUTS, NUM_OUTPUTS, NUM_LIGHTS); @@ -68,14 +68,15 @@ struct CV_MIDI : Module { } void process(const ProcessArgs& args) override { - const float rateLimiterPeriod = 0.005f; - rateLimiterPhase += args.sampleTime / rateLimiterPeriod; - if (rateLimiterPhase >= 1.f) { - rateLimiterPhase -= 1.f; - } - else { - return; - } + // MIDI baud rate is 31250 b/s, or 3125 B/s. + // CC messages are 3 bytes, so we can send a maximum of 1041 CC messages per second. + // Since multiple CCs can be generated, play it safe and limit the CC rate to 200 Hz. + const float rateLimiterPeriod = 1 / 200.f; + bool rateLimiterTriggered = (rateLimiterTimer.process(args.sampleTime) >= rateLimiterPeriod); + if (rateLimiterTriggered) + rateLimiterTimer.time -= rateLimiterPeriod; + + midiOutput.setTimestamp(APP->engine->getFrameTime()); for (int c = 0; c < inputs[PITCH_INPUT].getChannels(); c++) { int vel = (int) std::round(inputs[VEL_INPUT].getNormalPolyVoltage(10.f * 100 / 127, c) / 10.f * 127); diff --git a/src/engine/Engine.cpp b/src/engine/Engine.cpp index e15eeb8c..d5f722b8 100644 --- a/src/engine/Engine.cpp +++ b/src/engine/Engine.cpp @@ -560,8 +560,10 @@ void Engine::step(int frames) { random::init(); internal->stepFrame = internal->frame; + int64_t oldStepTime = internal->stepTime; internal->stepTime = system::getNanoseconds(); internal->stepFrames = frames; + DEBUG("time %ld", internal->stepTime - oldStepTime); // Set sample rate if (internal->sampleRate != settings::sampleRate) {