From d99a08bcf777afdc90b43f5819e34b25d48d214b Mon Sep 17 00:00:00 2001 From: unspeaker Date: Mon, 8 Jul 2024 19:44:40 +0300 Subject: [PATCH] refactor track callback --- src/model/track.rs | 134 ++++++++++++++++++++++++--------------------- 1 file changed, 72 insertions(+), 62 deletions(-) diff --git a/src/model/track.rs b/src/model/track.rs index c3a98d50..2e6da4a2 100644 --- a/src/model/track.rs +++ b/src/model/track.rs @@ -127,9 +127,6 @@ impl Track { (_usec0, _usecs): (usize, usize), period: f64, ) { - // Need to be borrowed outside the conditionals? - let recording = self.recording; - let monitoring = self.monitoring; // Clear the section of the output buffer that we will be using for frame in &mut self.midi_out_buf[0..frames] { frame.clear(); @@ -143,71 +140,56 @@ impl Track { } // For highlighting keys and note repeat // Currently playing phrase - let phrase = &mut self.sequence.map(|id|self.phrases.get_mut(id)).flatten(); - if playing == Some(TransportState::Rolling) { - if let (Some(phrase), Some((start_frame, _))) = (phrase, started) { - // Output playback from sequencer - phrase.process_out( - &mut self.midi_out_buf, - &mut self.notes_on, - timebase, - (frame0.saturating_sub(start_frame), frames, period) - ); - // Monitor and record input - if self.recording || self.monitoring { - let notes_on = &mut self.notes_on; - for (frame, event, bytes) in parse_midi_input(input) { - match event { - LiveEvent::Midi { message, .. } => { - if monitoring { - self.midi_out_buf[frame].push(bytes.to_vec()) + if let ( + Some(TransportState::Rolling), Some((start_frame, _)), Some(phrase) + ) = ( + playing, started, self.sequence.and_then(|id|self.phrases.get_mut(id)) + ) { + phrase.process_out( + &mut self.midi_out_buf, + &mut self.notes_on, + timebase, + (frame0.saturating_sub(start_frame), frames, period) + ); + // Monitor and record input + if self.recording || self.monitoring { + // For highlighting keys and note repeat + let notes_on = &mut self.notes_on; + for (frame, event, bytes) in parse_midi_input(input) { + match event { + LiveEvent::Midi { message, .. } => { + if self.monitoring { + self.midi_out_buf[frame].push(bytes.to_vec()) + } + if self.recording { + phrase.record_event({ + let pulse = timebase.frame_to_pulse( + (frame0 + frame - start_frame) as f64 + ); + let quantized = ( + pulse / quant as f64 + ).round() as usize * quant; + let looped = quantized % phrase.length; + looped + }, message); + } + match message { + MidiMessage::NoteOn { key, .. } => { + notes_on[key.as_int() as usize] = true; } - if recording { - phrase.record_event({ - let pulse = timebase.frame_to_pulse( - (frame0 + frame - start_frame) as f64 - ); - let quantized = ( - pulse / quant as f64 - ).round() as usize * quant; - let looped = quantized % phrase.length; - looped - }, message); - } - match message { - MidiMessage::NoteOn { key, .. } => { - notes_on[key.as_int() as usize] = true; - } - MidiMessage::NoteOff { key, .. } => { - notes_on[key.as_int() as usize] = false; - }, - _ => {} - } - }, - _ => {} - } + MidiMessage::NoteOff { key, .. } => { + notes_on[key.as_int() as usize] = false; + }, + _ => {} + } + }, + _ => {} } } } } else if self.monitoring { - // For highlighting keys and note repeat - let notes_on = &mut self.notes_on; for (frame, event, bytes) in parse_midi_input(input) { - match event { - LiveEvent::Midi { message, .. } => { - self.midi_out_buf[frame].push(bytes.to_vec()); - match message { - MidiMessage::NoteOn { key, .. } => { - notes_on[key.as_int() as usize] = true; - } - MidiMessage::NoteOff { key, .. } => { - notes_on[key.as_int() as usize] = false; - }, - _ => {} - } - }, - _ => {} - } + self.process_monitor_event(frame, &event, bytes) } } write_midi_output( @@ -216,4 +198,32 @@ impl Track { frames ); } + + #[inline] + fn process_monitor_event (&mut self, frame: usize, event: &LiveEvent, bytes: &[u8]) { + match event { + LiveEvent::Midi { message, .. } => { + self.write_to_output_buffer(frame, bytes); + self.process_monitor_message(&message); + }, + _ => {} + } + } + + #[inline] fn write_to_output_buffer (&mut self, frame: usize, bytes: &[u8]) { + self.midi_out_buf[frame].push(bytes.to_vec()); + } + + #[inline] + fn process_monitor_message (&mut self, message: &MidiMessage) { + match message { + MidiMessage::NoteOn { key, .. } => { + self.notes_on[key.as_int() as usize] = true; + } + MidiMessage::NoteOff { key, .. } => { + self.notes_on[key.as_int() as usize] = false; + }, + _ => {} + } + } }