-use std::fmt::Display;
+use std::{collections::HashSet, fmt::Display};
use eyre::Result;
use rand::random;
pub struct MidiLink {
buffer: Vec<MidiMessage>,
channel: Sender<u8>,
+ notes_on: HashSet<u8>,
}
impl Default for MidiLink {
Self {
channel: send,
buffer: Vec::default(),
+ notes_on: HashSet::default(),
}
}
}
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,
+ });
+ }
}
}
}