From a40cc226fdfc9beeff3ec9f8181c486097cebc15 Mon Sep 17 00:00:00 2001 From: Weird Constructor Date: Sat, 13 Aug 2022 08:49:49 +0200 Subject: [PATCH] Found a remaining bug in the MIDI handling --- src/dsp/node_midip.rs | 2 +- src/nodes/note_buffer.rs | 3 ++- tests/node_midip.rs | 29 +++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/dsp/node_midip.rs b/src/dsp/node_midip.rs index a9b53cd..c1adfd2 100644 --- a/src/dsp/node_midip.rs +++ b/src/dsp/node_midip.rs @@ -115,7 +115,7 @@ impl DspNode for MidiP { let note = note + denorm::MidiP::det(det, frame); freq.write(frame, note); - if chan.gate > 0 { + if chan.gate & 0x10 > 0 { // insert a single sample of silence, for retriggering // any envelopes if the note changed but no note-off came. if self.prev_gate > 0 && self.prev_gate != chan.gate { diff --git a/src/nodes/note_buffer.rs b/src/nodes/note_buffer.rs index f52a52e..4b272ad 100644 --- a/src/nodes/note_buffer.rs +++ b/src/nodes/note_buffer.rs @@ -85,6 +85,7 @@ impl NoteBuffer { pub fn note_on(&mut self, channel: u8, note: u8) { let mut chan = &mut self.interleaved_chans[(self.buf_idx * 16) + (channel as usize % 16)]; chan.gate = chan.gate % 2 + 1; + chan.gate |= 0x10; chan.note = note; } @@ -92,7 +93,7 @@ impl NoteBuffer { pub fn note_off(&mut self, channel: u8, note: u8) { let mut chan = &mut self.interleaved_chans[(self.buf_idx * 16) + (channel as usize % 16)]; if chan.gate > 0 && chan.note == note { - chan.gate = 0; + chan.gate = chan.gate & 0x0F; } } diff --git a/tests/node_midip.rs b/tests/node_midip.rs index 7c931f9..ba9392f 100644 --- a/tests/node_midip.rs +++ b/tests/node_midip.rs @@ -122,3 +122,32 @@ fn check_node_midip_vel_track() { let changes = collect_signal_changes_flt(&ch1[..], 0.01); assert_eq!(changes, vec![(5, 0.4), (10, 1.0), (130, 0.6)]); } + +#[test] +fn check_node_midip_off_on_test() { + let (node_conf, mut node_exec) = new_node_engine(); + let mut matrix = Matrix::new(node_conf, 3, 3); + + // Create a DSP matrix with a "MidiP" node and an Out node: + let mut chain = MatrixCellChain::new(CellDir::B); + chain + .node_out("midip", "gate") + .set_denorm("det", 0.1) + .node_inp("out", "ch1") + .place(&mut matrix, 0, 0) + .unwrap(); + matrix.sync().unwrap(); + + let (ch1, _) = node_exec.test_run( + 0.005, + false, + vec![ + HxTimedEvent::note_on(0, 0, 69, 1.0), + HxTimedEvent::note_off(5, 0, 69), + HxTimedEvent::note_on(5, 0, 68, 1.0), + ], + ); + + let changes = collect_signal_changes(&ch1[..], -1); + assert_eq!(changes, vec![(0, 100), (5, 0), (6, 100)]); +}