launch pt.8: fixing switchover, still not right

This commit is contained in:
🪞👃🪞 2024-11-01 22:53:45 +02:00
parent 1fe4ea89af
commit e149d777ed

View file

@ -14,12 +14,10 @@ impl Audio for PhrasePlayer {
fn process (&mut self, _: &Client, scope: &ProcessScope) -> Control { fn process (&mut self, _: &Client, scope: &ProcessScope) -> Control {
let has_midi_outputs = self.has_midi_outputs(); let has_midi_outputs = self.has_midi_outputs();
let has_midi_inputs = self.has_midi_inputs(); let has_midi_inputs = self.has_midi_inputs();
//if has_midi_outputs { // Clear output buffer(s)
// Clear output buffer(s) self.clear(scope, false);
self.clear(scope, false); // Write chunk of phrase to output, handle switchover
// Write chunk of phrase to output self.play(scope);
self.play(scope);
//}
if has_midi_inputs { if has_midi_inputs {
if self.recording || self.monitoring { if self.recording || self.monitoring {
// Record and/or monitor input // Record and/or monitor input
@ -51,31 +49,16 @@ impl PhrasePlayer {
fn is_rolling (&self) -> bool { fn is_rolling (&self) -> bool {
*self.clock.playing.read().unwrap() == Some(TransportState::Rolling) *self.clock.playing.read().unwrap() == Some(TransportState::Rolling)
} }
/// Return playing phrase with starting point
fn playing (&self) -> Option<(usize, Arc<RwLock<Phrase>>)> {
if let (true, Some((started, Some(ref phrase)))) = (self.is_rolling(), &self.phrase) {
Some((started.sample().get() as usize, phrase.clone()))
} else {
None
}
}
/// Return next phrase with starting point
fn enqueued (&self) -> Option<(usize, Option<Arc<RwLock<Phrase>>>)> {
if let (true, Some((start_at, phrase))) = (self.is_rolling(), &self.next_phrase) {
Some((start_at.sample().get() as usize, phrase.clone()))
} else {
None
}
}
fn play (&mut self, scope: &ProcessScope) { fn play (&mut self, scope: &ProcessScope) {
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;
let mut next = false; // If no phrase is playing, prepare for switchover immediately
let mut next = self.phrase.is_none();
// 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
if let Some((start, ref phrase)) = self.playing() { if let (true, Some((started, phrase))) = (self.is_rolling(), &self.phrase) {
// First sample to populate. Greater than 0 means that the first // First sample to populate. Greater than 0 means that the first
// pulse of the phrase falls somewhere in the middle of the chunk. // pulse of the phrase falls somewhere in the middle of the chunk.
let sample = sample0.saturating_sub(start); let sample = sample0.saturating_sub(started.sample.get() as usize);
// Iterator that emits sample (index into output buffer at which to write MIDI event) // Iterator that emits sample (index into output buffer at which to write MIDI event)
// paired with pulse (index into phrase from which to take the MIDI event) for each // paired with pulse (index into phrase from which to take the MIDI event) for each
// sample of the output buffer that corresponds to a MIDI pulse. // sample of the output buffer that corresponds to a MIDI pulse.
@ -84,43 +67,52 @@ impl PhrasePlayer {
let output = &mut self.midi_out_buf; let output = &mut self.midi_out_buf;
// Buffer for bytes of a MIDI event. // Buffer for bytes of a MIDI event.
let mut mm = Vec::with_capacity(8); let mut mm = Vec::with_capacity(8);
// Source phrase from which the MIDI events will be taken.
let phrase = phrase.read().unwrap();
// Notes active during current chunk. // Notes active during current chunk.
let notes = &mut self.notes_out.write().unwrap(); let notes = &mut self.notes_out.write().unwrap();
for (sample, pulse) in pulses { for (sample, pulse) in pulses {
// If a next phrase is enqueued, and we're past the end of the current one, // If a next phrase is enqueued, and we're past the end of the current one,
// break the loop here (FIXME count pulse correctly) // break the loop here (FIXME count pulse correctly)
if self.next_phrase.is_some() && pulse >= phrase.length { next = self.next_phrase.is_some() && if let Some(ref phrase) = phrase {
next = true; pulse >= phrase.read().unwrap().length
} else {
true
};
if next {
break break
} }
let pulse = pulse % phrase.length; // If there's a currently playing phrase, output notes from it to buffer:
// Output each MIDI event from phrase at appropriate frames of output buffer: if let Some(ref phrase) = phrase {
for message in phrase.notes[pulse].iter() { // Source phrase from which the MIDI events will be taken.
// Clear output buffer for this MIDI event. let phrase = phrase.read().unwrap();
mm.clear(); // Current pulse index in source phrase
// TODO: support MIDI channels other than CH1. let pulse = pulse % phrase.length;
let channel = 0.into(); // Output each MIDI event from phrase at appropriate frames of output buffer:
// Serialize MIDI event into message buffer. for message in phrase.notes[pulse].iter() {
LiveEvent::Midi { channel, message: *message }.write(&mut mm).unwrap(); // Clear output buffer for this MIDI event.
// Append serialized message to output buffer. mm.clear();
output[sample].push(mm.clone()); // TODO: support MIDI channels other than CH1.
// Update the list of currently held notes. let channel = 0.into();
update_keys(notes, &message); // Serialize MIDI event into message buffer.
LiveEvent::Midi { channel, message: *message }.write(&mut mm).unwrap();
// Append serialized message to output buffer.
output[sample].push(mm.clone());
// Update the list of currently held notes.
update_keys(notes, &message);
}
} }
} }
} }
// If it's time for the next enqueued phrase, handle it here: // If it's time for the next enqueued phrase, handle it here:
if next { if next {
if let Some((start, phrase)) = self.enqueued() { if let (true, Some((start_at, phrase))) = (self.is_rolling(), &self.next_phrase) {
let start = start_at.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 { if start <= sample0 {
// 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 = Instant::from_sample(&self.clock.timebase(), start as f64); let started = Instant::from_sample(&self.clock.timebase(), start as f64);
self.phrase = Some((started, phrase)); self.phrase = Some((started, phrase.clone()));
// Unset enqueuement (TODO: where to implement looping?) // Unset enqueuement (TODO: where to implement looping?)
self.next_phrase = None self.next_phrase = None
} }
@ -131,10 +123,9 @@ impl PhrasePlayer {
} }
fn record (&mut self, scope: &ProcessScope) { fn record (&mut self, scope: &ProcessScope) {
let sample0 = scope.last_frame_time() as usize; let sample0 = scope.last_frame_time() as usize;
if let Some((start, ref phrase)) = self.playing() { if let (true, Some((started, phrase))) = (self.is_rolling(), &self.phrase) {
let quant = self.clock.quant().get(); let start = started.sample.get() as usize;
let mut phrase = phrase.write().unwrap(); let quant = self.clock.quant().get();
let length = phrase.length;
// For highlighting keys and note repeat // For highlighting keys and note repeat
let mut notes_in = self.notes_in.write().unwrap(); let mut notes_in = self.notes_in.write().unwrap();
// Record from each input // Record from each input
@ -145,20 +136,24 @@ impl PhrasePlayer {
self.midi_out_buf[sample].push(bytes.to_vec()) self.midi_out_buf[sample].push(bytes.to_vec())
} }
if self.recording { if self.recording {
phrase.record_event({ if let Some(phrase) = phrase {
let sample = (sample0 + sample - start) as f64; let mut phrase = phrase.write().unwrap();
let pulse = self.clock.timebase().samples_to_pulse(sample); let length = phrase.length;
let quantized = (pulse / quant).round() * quant; phrase.record_event({
let looped = quantized as usize % length; let sample = (sample0 + sample - start) as f64;
looped let pulse = self.clock.timebase().samples_to_pulse(sample);
}, message); let quantized = (pulse / quant).round() * quant;
let looped = quantized as usize % length;
looped
}, message);
}
} }
update_keys(&mut notes_in, &message); update_keys(&mut notes_in, &message);
} }
} }
} }
} }
if let Some((start, ref phrase)) = self.enqueued() { if let (true, Some((start_at, phrase))) = (self.is_rolling(), &self.next_phrase) {
// TODO switch to next phrase and record into it // TODO switch to next phrase and record into it
} }
} }