From 5661e4662cb4cb6a898dae6eee19808e0a5f7cf9 Mon Sep 17 00:00:00 2001 From: Blitblank Date: Wed, 14 Jan 2026 20:49:43 -0600 Subject: [PATCH] pedal working checkpoint --- src/MidiController.cpp | 22 +++++++++++++++------- src/synth/Synth.cpp | 10 +++++++++- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/MidiController.cpp b/src/MidiController.cpp index 267c1f5..ade4e40 100644 --- a/src/MidiController.cpp +++ b/src/MidiController.cpp @@ -7,7 +7,7 @@ MidiController::MidiController(NoteQueue& queue) : noteQueue_(queue) { try { midiIn_ = std::make_unique(); - midiIn_->ignoreTypes(true, true, true); + midiIn_->ignoreTypes(false, false, false); } catch (RtMidiError& e) { std::cerr << "RtMidi init failed: " << e.getMessage() << std::endl; } @@ -55,21 +55,23 @@ void MidiController::midiCallback(double /*deltaTime*/, std::vector& msg) { - unsigned char status = msg[0] & 0xF0; - unsigned char data1 = msg[1]; - unsigned char data2 = msg[2]; + + if(msg.size() <= 1) return; + + uint8_t status = msg[0] & 0xF0; + uint8_t data1 = msg[1]; + uint8_t data2 = msg[2]; if(status == 0xFE) return; if(status == 0xF8) return; if(status == 0xB0 && data1 == 64) { handleSustain(data2 >= 64); - std::cout << "sustain event: " << data2 << std::endl; return; } - unsigned char note = msg.size() > 1 ? msg[1] : 0; - unsigned char vel = msg.size() > 2 ? msg[2] : 0; + unsigned char note = msg.size() > 1 ? msg[1] : 0; + unsigned char vel = msg.size() > 2 ? msg[2] : 0; // Note On (velocity > 0) if (status == 0x90 && vel > 0) { @@ -83,6 +85,8 @@ void MidiController::handleMessage(const std::vector& msg) { } void MidiController::noteOn(uint8_t note, uint8_t vel) { + sustainedNotes_.erase(note); + noteQueue_.push({ NoteEventType::NoteOn, static_cast(note), @@ -92,6 +96,10 @@ void MidiController::noteOn(uint8_t note, uint8_t vel) { } void MidiController::noteOff(uint8_t note) { + if(sustainDown_) { + sustainedNotes_.insert(note); + return; + } noteQueue_.push({ NoteEventType::NoteOff, static_cast(note), diff --git a/src/synth/Synth.cpp b/src/synth/Synth.cpp index f0bff9d..90ab889 100644 --- a/src/synth/Synth.cpp +++ b/src/synth/Synth.cpp @@ -36,6 +36,15 @@ void Synth::handleNoteEvent(const NoteEvent& event) { if(event.type == NoteEventType::NoteOn) { + // TODO: this should get fixed if we tie voices to midi notes + // end all voices already playing this note + for(Voice& v : voices_) { + if(v.isActive() && v.note() == event.note) { + v.noteOff(); + break; + } + } + // TODO: find quietest voice and assign a note to it instead of just the first inactive one // find inactive voice and start it with the given note for(Voice& v : voices_) { @@ -44,7 +53,6 @@ void Synth::handleNoteEvent(const NoteEvent& event) { break; } } - } else {