From d27ea8a4b2e665ab1a401b9eea9c58b66139c472 Mon Sep 17 00:00:00 2001 From: Huck Boles Date: Thu, 6 Jul 2023 18:20:27 -0500 Subject: [PATCH] added: note off functionality --- src/midi.rs | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/src/midi.rs b/src/midi.rs index dd5cbe8..2b97ba9 100644 --- a/src/midi.rs +++ b/src/midi.rs @@ -1,4 +1,4 @@ -use std::fmt::Display; +use std::{collections::HashSet, fmt::Display}; use eyre::Result; use rand::random; @@ -38,6 +38,7 @@ impl Default for MidiInfo { pub struct MidiLink { buffer: Vec, channel: Sender, + notes_on: HashSet, } impl Default for MidiLink { @@ -46,6 +47,7 @@ impl Default for MidiLink { Self { channel: send, buffer: Vec::default(), + notes_on: HashSet::default(), } } } @@ -72,11 +74,28 @@ impl MidiLink { continue; } else { count += 1; - self.buffer.push(MidiMessage::On { - note: generate_note(info), - velocity: generate_velocity(info.velocity), - channel: info.channel, - }); + let note = generate_note(info); + + if self.notes_on.contains(¬e) { + self.notes_on.remove(¬e); + self.buffer.push(MidiMessage::Off { + note, + velocity: generate_velocity(info.velocity), + channel: info.channel, + }); + } else { + if self.notes_on.len() > info.voices.into() { + if let Some(elem) = self.notes_on.iter().next().cloned() { + self.notes_on.remove(&elem); + } + } + self.notes_on.insert(note); + self.buffer.push(MidiMessage::On { + note, + velocity: generate_velocity(info.velocity), + channel: info.channel, + }); + } } } } -- 2.45.2