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,28 +58,7 @@ 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 {
// Source phrase from which the MIDI events will be taken. Self::play_pulse(phrase, pulse, sample, note_buf, out, notes)
let phrase = phrase.read().unwrap();
// Phrase with zero length is not processed
if phrase.length > 0 {
// Current pulse index in source phrase
let pulse = pulse % phrase.length;
// Output each MIDI event from phrase at appropriate frames of output buffer:
for message in phrase.notes[pulse].iter() {
// Clear output buffer for this MIDI event.
note_buf.clear();
// TODO: support MIDI channels other than CH1.
let channel = 0.into();
// Serialize MIDI event into message buffer.
LiveEvent::Midi { channel, message: *message }
.write(note_buf)
.unwrap();
// Append serialized message to output buffer.
out_buf[sample].push(note_buf.clone());
// Update the list of currently held notes.
update_keys(&mut*notes, &message);
}
}
} }
} }
} }
@ -87,47 +66,84 @@ pub trait MidiPlaybackApi: HasPlayPhrase + HasClock + HasMidiOuts {
next next
} }
/// Handle switchover from current to next playing phrase. fn play_pulse (
fn switchover ( phrase: &RwLock<Phrase>,
&mut self, scope: &ProcessScope, note_buf: &mut Vec<u8>, out_buf: &mut Vec<Vec<Vec<u8>>> pulse: usize,
sample: usize,
note_buf: &mut Vec<u8>,
out: &mut [Vec<Vec<u8>>],
notes: &mut [bool;128]
) { ) {
if self.clock().is_rolling() { // Source phrase from which the MIDI events will be taken.
let sample0 = scope.last_frame_time() as usize; let phrase = phrase.read().unwrap();
//let samples = scope.n_frames() as usize; // Phrase with zero length is not processed
if let Some((start_at, phrase)) = &self.next_phrase() { if phrase.length > 0 {
let start = start_at.sample.get() as usize; // Current pulse index in source phrase
let sample = self.clock().started.read().unwrap().as_ref().unwrap().sample.get() as usize; let pulse = pulse % phrase.length;
// If it's time to switch to the next phrase: // Output each MIDI event from phrase at appropriate frames of output buffer:
if start <= sample0.saturating_sub(sample) { for message in phrase.notes[pulse].iter() {
// Samples elapsed since phrase was supposed to start // Clear output buffer for this MIDI event.
let skipped = sample0 - start; note_buf.clear();
// Switch over to enqueued phrase // TODO: support MIDI channels other than CH1.
let started = Moment::from_sample(&self.clock().timebase(), start as f64); let channel = 0.into();
*self.play_phrase_mut() = Some((started, phrase.clone())); // Serialize MIDI event into message buffer.
// Unset enqueuement (TODO: where to implement looping?) LiveEvent::Midi { channel, message: *message }
*self.next_phrase_mut() = None .write(note_buf)
} .unwrap();
// TODO fill in remaining ticks of chunk from next phrase. // Append serialized message to output buffer.
// ?? just call self.play(scope) again, since enqueuement is off ??? out[sample].push(note_buf.clone());
self.play(scope, note_buf, out_buf); // Update the list of currently held notes.
// ?? or must it be with modified scope ?? update_keys(&mut*notes, &message);
// likely not because start time etc
} }
} }
} }
/// Write a chunk of MIDI notes to the output buffer. /// Handle switchover from current to next playing phrase.
fn write ( fn switchover (
&mut self, scope: &ProcessScope, out_buf: &Vec<Vec<Vec<u8>>> &mut self, scope: &ProcessScope, note_buf: &mut Vec<u8>, out: &mut [Vec<Vec<u8>>]
) { ) {
if !self.clock().is_rolling() {
return
}
let sample0 = scope.last_frame_time() as usize;
//let samples = scope.n_frames() as usize;
if let Some((start_at, phrase)) = &self.next_phrase() {
let start = start_at.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 start <= sample0.saturating_sub(sample) {
// Samples elapsed since phrase was supposed to start
let skipped = sample0 - start;
// Switch over to enqueued phrase
let started = Moment::from_sample(&self.clock().timebase(), start as f64);
// Launch enqueued phrase
*self.play_phrase_mut() = Some((started, phrase.clone()));
// Unset enqueuement (TODO: where to implement looping?)
*self.next_phrase_mut() = None
}
// TODO fill in remaining ticks of chunk from next phrase.
// ?? just call self.play(scope) again, since enqueuement is off ???
self.play(scope, note_buf, out);
// ?? or must it be with modified scope ??
// likely not because start time etc
}
}
/// Write a chunk of MIDI data from the output buffer to all assigned output ports.
fn write (&mut self, scope: &ProcessScope, out: &[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)
for time in 0..samples { }
for event in out_buf[time].iter() { }
writer.write(&RawMidi { time: time as u32, bytes: &event })
.expect(&format!("{event:?}")); /// 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 event in out[time].iter() {
writer.write(&RawMidi { time: time as u32, bytes: &event })
.expect(&format!("{event:?}"));
} }
} }
} }