start flattening the midi playback logic

This commit is contained in:
🪞👃🪞 2024-12-25 16:43:44 +01:00
parent 498acac9cc
commit e2172f287c

View file

@ -7,19 +7,19 @@ pub trait MidiPlaybackApi: HasPlayPhrase + HasClock + HasMidiOuts {
/// Clear the section of the output buffer that we will be using, /// Clear the section of the output buffer that we will be using,
/// emitting "all notes off" at start of buffer if requested. /// emitting "all notes off" at start of buffer if requested.
fn clear ( fn clear (
&mut self, scope: &ProcessScope, out_buf: &mut Vec<Vec<Vec<u8>>>, reset: bool &mut self, scope: &ProcessScope, out: &mut [Vec<Vec<u8>>], reset: bool
) { ) {
for frame in &mut out_buf[0..scope.n_frames() as usize] { for frame in &mut out[0..scope.n_frames() as usize] {
frame.clear(); frame.clear();
} }
if reset { if reset {
all_notes_off(out_buf); all_notes_off(out);
} }
} }
/// Output notes from phrase to MIDI output ports. /// Output notes from phrase to MIDI output ports.
fn play ( fn play (
&mut self, scope: &ProcessScope, note_buf: &mut Vec<u8>, out_buf: &mut Vec<Vec<Vec<u8>>> &mut self, scope: &ProcessScope, note_buf: &mut Vec<u8>, out: &mut [Vec<Vec<u8>>]
) -> bool { ) -> bool {
let mut next = false; let mut next = false;
// Write MIDI events from currently playing phrase (if any) to MIDI output buffer // Write MIDI events from currently playing phrase (if any) to MIDI output buffer
@ -58,6 +58,22 @@ pub trait MidiPlaybackApi: HasPlayPhrase + HasClock + HasMidiOuts {
} }
// If there's a currently playing phrase, output notes from it to buffer: // If there's a currently playing phrase, output notes from it to buffer:
if let Some(ref phrase) = phrase { if let Some(ref phrase) = phrase {
Self::play_pulse(phrase, pulse, sample, note_buf, out, notes)
}
}
}
}
next
}
fn play_pulse (
phrase: &RwLock<Phrase>,
pulse: usize,
sample: usize,
note_buf: &mut Vec<u8>,
out: &mut [Vec<Vec<u8>>],
notes: &mut [bool;128]
) {
// Source phrase from which the MIDI events will be taken. // Source phrase from which the MIDI events will be taken.
let phrase = phrase.read().unwrap(); let phrase = phrase.read().unwrap();
// Phrase with zero length is not processed // Phrase with zero length is not processed
@ -75,60 +91,60 @@ pub trait MidiPlaybackApi: HasPlayPhrase + HasClock + HasMidiOuts {
.write(note_buf) .write(note_buf)
.unwrap(); .unwrap();
// Append serialized message to output buffer. // Append serialized message to output buffer.
out_buf[sample].push(note_buf.clone()); out[sample].push(note_buf.clone());
// Update the list of currently held notes. // Update the list of currently held notes.
update_keys(&mut*notes, &message); update_keys(&mut*notes, &message);
} }
} }
} }
}
}
}
next
}
/// Handle switchover from current to next playing phrase. /// Handle switchover from current to next playing phrase.
fn switchover ( fn switchover (
&mut self, scope: &ProcessScope, note_buf: &mut Vec<u8>, out_buf: &mut Vec<Vec<Vec<u8>>> &mut self, scope: &ProcessScope, note_buf: &mut Vec<u8>, out: &mut [Vec<Vec<u8>>]
) { ) {
if self.clock().is_rolling() { if !self.clock().is_rolling() {
return
}
let sample0 = scope.last_frame_time() as usize; let sample0 = scope.last_frame_time() as usize;
//let samples = scope.n_frames() as usize; //let samples = scope.n_frames() as usize;
if let Some((start_at, phrase)) = &self.next_phrase() { if let Some((start_at, phrase)) = &self.next_phrase() {
let start = start_at.sample.get() as usize; let start = start_at.sample.get() as usize;
let sample = self.clock().started.read().unwrap().as_ref().unwrap().sample.get() as usize; let sample = self.clock().started.read().unwrap()
.as_ref().unwrap().sample.get() as usize;
// If it's time to switch to the next phrase: // If it's time to switch to the next phrase:
if start <= sample0.saturating_sub(sample) { if start <= sample0.saturating_sub(sample) {
// Samples elapsed since phrase was supposed to start // Samples elapsed since phrase was supposed to start
let skipped = sample0 - start; let skipped = sample0 - start;
// Switch over to enqueued phrase // Switch over to enqueued phrase
let started = Moment::from_sample(&self.clock().timebase(), start as f64); let started = Moment::from_sample(&self.clock().timebase(), start as f64);
// Launch enqueued phrase
*self.play_phrase_mut() = Some((started, phrase.clone())); *self.play_phrase_mut() = Some((started, phrase.clone()));
// Unset enqueuement (TODO: where to implement looping?) // Unset enqueuement (TODO: where to implement looping?)
*self.next_phrase_mut() = None *self.next_phrase_mut() = None
} }
// TODO fill in remaining ticks of chunk from next phrase. // TODO fill in remaining ticks of chunk from next phrase.
// ?? just call self.play(scope) again, since enqueuement is off ??? // ?? just call self.play(scope) again, since enqueuement is off ???
self.play(scope, note_buf, out_buf); self.play(scope, note_buf, out);
// ?? or must it be with modified scope ?? // ?? or must it be with modified scope ??
// likely not because start time etc // likely not because start time etc
} }
} }
}
/// Write a chunk of MIDI notes to the output buffer. /// Write a chunk of MIDI data from the output buffer to all assigned output ports.
fn write ( fn write (&mut self, scope: &ProcessScope, out: &[Vec<Vec<u8>>]) {
&mut self, scope: &ProcessScope, out_buf: &Vec<Vec<Vec<u8>>>
) {
let samples = scope.n_frames() as usize; let samples = scope.n_frames() as usize;
for port in self.midi_outs_mut().iter_mut() { for port in self.midi_outs_mut().iter_mut() {
let writer = &mut port.writer(scope); Self::write_port(&mut port.writer(scope), samples, out)
}
}
/// Write a chunk of MIDI data from the output buffer to an output port.
fn write_port (writer: &mut MidiWriter, samples: usize, out: &[Vec<Vec<u8>>]) {
for time in 0..samples { for time in 0..samples {
for event in out_buf[time].iter() { for event in out[time].iter() {
writer.write(&RawMidi { time: time as u32, bytes: &event }) writer.write(&RawMidi { time: time as u32, bytes: &event })
.expect(&format!("{event:?}")); .expect(&format!("{event:?}"));
} }
} }
} }
}
} }