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),
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;
},
_ => {}
}
}
}