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,71 +140,56 @@ 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( ) {
&mut self.midi_out_buf, phrase.process_out(
&mut self.notes_on, &mut self.midi_out_buf,
timebase, &mut self.notes_on,
(frame0.saturating_sub(start_frame), frames, period) timebase,
); (frame0.saturating_sub(start_frame), frames, period)
// Monitor and record input );
if self.recording || self.monitoring { // Monitor and record input
let notes_on = &mut self.notes_on; if self.recording || self.monitoring {
for (frame, event, bytes) in parse_midi_input(input) { // For highlighting keys and note repeat
match event { let notes_on = &mut self.notes_on;
LiveEvent::Midi { message, .. } => { for (frame, event, bytes) in parse_midi_input(input) {
if monitoring { match event {
self.midi_out_buf[frame].push(bytes.to_vec()) 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 { MidiMessage::NoteOff { key, .. } => {
phrase.record_event({ notes_on[key.as_int() as usize] = false;
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;
},
_ => {}
}
},
_ => {}
}
} }
} }
} }
} 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;
},
_ => {}
}
}
} }