From a16d21bbdfc6a874a45c7229e7970ef125606879 Mon Sep 17 00:00:00 2001 From: Huck Boles Date: Mon, 10 Jul 2023 18:41:00 -0500 Subject: [PATCH] fixed: send note offs on stop --- src/lib.rs | 19 ++++++++++++++----- src/midi.rs | 20 ++++++++++++++++++++ 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 91b40ac..8e5137b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -164,7 +164,7 @@ impl Application for CellSeq { } Message::Tick(_) => { let map = if self.song.is_looping && self.song.step_num >= self.song.loop_len { - self.song.step_num = 0; + self.song.step_num = 1; self.map.reset_loop() } else { self.song.step_num += 1; @@ -174,6 +174,9 @@ impl Application for CellSeq { let channel = self.midi.channel_handle(); let bytes = self.midi.tick(); + let new_map = map.clone(); + let hits = self.mask.tick(map); + let midi = tokio::spawn(async move { for byte in bytes { channel.send(byte).await.unwrap() @@ -181,8 +184,9 @@ impl Application for CellSeq { }); let mut commands = Vec::new(); - commands.push(Command::perform(async move { map }, Message::NewMap)); commands.push(Command::perform(midi, |_| Message::None)); + commands.push(Command::perform(async move { new_map }, Message::NewMap)); + commands.push(Command::perform(async move { hits }, Message::HitCount)); commands.push(Command::perform(async move {}, |_| { Message::MaskMessage(mask::Message::Ticked) })); @@ -190,15 +194,20 @@ impl Application for CellSeq { return Command::batch(commands); } Message::TogglePlayback => { - self.song.is_playing = !self.song.is_playing; if self.song.is_playing { - self.map.save() + let bytes = self.midi.all_off(self.info.channel); + let channel = self.midi.channel_handle(); + for byte in bytes { + channel.try_send(byte).unwrap(); + } } + self.song.is_playing = !self.song.is_playing; } Message::ToggleLoop => { self.song.is_looping = !self.song.is_looping; if self.song.is_looping { - self.song.step_num = 0; + self.map.set_loop(); + self.song.step_num = 1; } } Message::RandChanged(r) => { diff --git a/src/midi.rs b/src/midi.rs index 7c722e4..0f4330f 100644 --- a/src/midi.rs +++ b/src/midi.rs @@ -120,6 +120,26 @@ impl MidiLink { self.buffer.clear(); vec } + + pub fn all_off(&mut self, channel: u8) -> Vec { + let notes = self + .notes_on + .iter() + .flat_map(|note| { + MidiMessage::Off { + note: *note, + velocity: 0, + channel, + } + .as_bytes() + }) + .flatten() + .flatten() + .collect(); + + self.notes_on.clear(); + notes + } } #[derive(Clone, Copy, Debug, Error)] -- 2.44.2