refactor track callback

This commit is contained in:
🪞👃🪞 2024-07-08 19:44:40 +03:00
parent 14b504374f
commit d99a08bcf7

View file

@ -127,9 +127,6 @@ impl Track {
(_usec0, _usecs): (usize, usize), (_usec0, _usecs): (usize, usize),
period: f64, 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 // Clear the section of the output buffer that we will be using
for frame in &mut self.midi_out_buf[0..frames] { for frame in &mut self.midi_out_buf[0..frames] {
frame.clear(); frame.clear();
@ -143,10 +140,11 @@ impl Track {
} }
// For highlighting keys and note repeat // For highlighting keys and note repeat
// Currently playing phrase // Currently playing phrase
let phrase = &mut self.sequence.map(|id|self.phrases.get_mut(id)).flatten(); if let (
if playing == Some(TransportState::Rolling) { Some(TransportState::Rolling), Some((start_frame, _)), Some(phrase)
if let (Some(phrase), Some((start_frame, _))) = (phrase, started) { ) = (
// Output playback from sequencer playing, started, self.sequence.and_then(|id|self.phrases.get_mut(id))
) {
phrase.process_out( phrase.process_out(
&mut self.midi_out_buf, &mut self.midi_out_buf,
&mut self.notes_on, &mut self.notes_on,
@ -155,14 +153,15 @@ impl Track {
); );
// Monitor and record input // Monitor and record input
if self.recording || self.monitoring { if self.recording || self.monitoring {
// For highlighting keys and note repeat
let notes_on = &mut self.notes_on; let notes_on = &mut self.notes_on;
for (frame, event, bytes) in parse_midi_input(input) { for (frame, event, bytes) in parse_midi_input(input) {
match event { match event {
LiveEvent::Midi { message, .. } => { LiveEvent::Midi { message, .. } => {
if monitoring { if self.monitoring {
self.midi_out_buf[frame].push(bytes.to_vec()) self.midi_out_buf[frame].push(bytes.to_vec())
} }
if recording { if self.recording {
phrase.record_event({ phrase.record_event({
let pulse = timebase.frame_to_pulse( let pulse = timebase.frame_to_pulse(
(frame0 + frame - start_frame) as f64 (frame0 + frame - start_frame) as f64
@ -188,26 +187,9 @@ impl Track {
} }
} }
} }
}
} else if self.monitoring { } 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) { for (frame, event, bytes) in parse_midi_input(input) {
match event { self.process_monitor_event(frame, &event, bytes)
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;
},
_ => {}
}
},
_ => {}
}
} }
} }
write_midi_output( write_midi_output(
@ -216,4 +198,32 @@ impl Track {
frames 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;
},
_ => {}
}
}
} }