mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 12:16:42 +01:00
launch pt.8: fixing switchover, still not right
This commit is contained in:
parent
1fe4ea89af
commit
e149d777ed
1 changed files with 51 additions and 56 deletions
|
|
@ -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
|
// Write chunk of phrase to output, handle switchover
|
||||||
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,17 +67,24 @@ 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
|
||||||
}
|
}
|
||||||
|
// If there's a currently playing phrase, output notes from it to buffer:
|
||||||
|
if let Some(ref phrase) = phrase {
|
||||||
|
// Source phrase from which the MIDI events will be taken.
|
||||||
|
let phrase = phrase.read().unwrap();
|
||||||
|
// Current pulse index in source phrase
|
||||||
let pulse = pulse % phrase.length;
|
let pulse = pulse % phrase.length;
|
||||||
// Output each MIDI event from phrase at appropriate frames of output buffer:
|
// Output each MIDI event from phrase at appropriate frames of output buffer:
|
||||||
for message in phrase.notes[pulse].iter() {
|
for message in phrase.notes[pulse].iter() {
|
||||||
|
|
@ -111,16 +101,18 @@ impl PhrasePlayer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// 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 start = started.sample.get() as usize;
|
||||||
let quant = self.clock.quant().get();
|
let quant = self.clock.quant().get();
|
||||||
let mut phrase = phrase.write().unwrap();
|
|
||||||
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,6 +136,9 @@ 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 {
|
||||||
|
if let Some(phrase) = phrase {
|
||||||
|
let mut phrase = phrase.write().unwrap();
|
||||||
|
let length = phrase.length;
|
||||||
phrase.record_event({
|
phrase.record_event({
|
||||||
let sample = (sample0 + sample - start) as f64;
|
let sample = (sample0 + sample - start) as f64;
|
||||||
let pulse = self.clock.timebase().samples_to_pulse(sample);
|
let pulse = self.clock.timebase().samples_to_pulse(sample);
|
||||||
|
|
@ -153,12 +147,13 @@ impl PhrasePlayer {
|
||||||
looped
|
looped
|
||||||
}, message);
|
}, 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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue