tek/crates/device/src/sequencer/seq_audio.rs
2025-05-14 14:42:13 +03:00

214 lines
8.5 KiB
Rust

use crate::*;
/// JACK process callback for a sequencer's clip sequencer/recorder.
impl Audio for Sequencer {
fn process (&mut self, _: &Client, scope: &ProcessScope) -> Control {
if self.clock().is_rolling() {
self.process_rolling(scope)
} else {
self.process_stopped(scope)
}
}
}
impl Sequencer {
fn process_rolling (&mut self, scope: &ProcessScope) -> Control {
self.process_clear(scope, false);
// Write chunk of clip to output, handle switchover
if self.process_playback(scope) {
self.process_switchover(scope);
}
// Monitor input to output
self.process_monitoring(scope);
// Record and/or monitor input
self.process_recording(scope);
// Emit contents of MIDI buffers to JACK MIDI output ports.
self.midi_outs_emit(scope);
Control::Continue
}
fn process_stopped (&mut self, scope: &ProcessScope) -> Control {
if self.monitoring() && self.midi_ins().len() > 0 && self.midi_outs().len() > 0 {
self.process_monitoring(scope)
}
Control::Continue
}
fn process_monitoring (&mut self, scope: &ProcessScope) {
let notes_in = self.notes_in().clone(); // For highlighting keys and note repeat
let monitoring = self.monitoring();
for input in self.midi_ins.iter() {
for (sample, event, bytes) in input.parsed(scope) {
if let LiveEvent::Midi { message, .. } = event {
if monitoring {
self.midi_buf[sample].push(bytes.to_vec());
}
// FIXME: don't lock on every event!
update_keys(&mut notes_in.write().unwrap(), &message);
}
}
}
}
/// Clear the section of the output buffer that we will be using,
/// emitting "all notes off" at start of buffer if requested.
fn process_clear (&mut self, scope: &ProcessScope, reset: bool) {
let n_frames = (scope.n_frames() as usize).min(self.midi_buf_mut().len());
for frame in &mut self.midi_buf_mut()[0..n_frames] {
frame.clear();
}
if reset {
all_notes_off(self.midi_buf_mut());
}
for port in self.midi_outs_mut().iter_mut() {
// Clear output buffer(s)
port.buffer_clear(scope, false);
}
}
fn process_recording (&mut self, scope: &ProcessScope) {
if self.monitoring() {
self.monitor(scope);
}
if let Some((started, ref clip)) = self.play_clip.clone() {
self.record_clip(scope, started, clip);
}
if let Some((_start_at, _clip)) = &self.next_clip() {
self.record_next();
}
}
fn process_playback (&mut self, scope: &ProcessScope) -> bool {
// If a clip is playing, write a chunk of MIDI events from it to the output buffer.
// If no clip is playing, prepare for switchover immediately.
if let Some((started, clip)) = &self.play_clip {
// Length of clip, to repeat or stop on end.
let length = clip.as_ref().map_or(0, |p|p.read().unwrap().length);
// Index of first sample to populate.
let offset = self.clock().get_sample_offset(scope, &started);
// Write MIDI events from clip at sample offsets corresponding to pulses.
for (sample, pulse) in self.clock().get_pulses(scope, offset) {
// If a next clip is enqueued, and we're past the end of the current one,
// break the loop here (FIXME count pulse correctly)
let past_end = if clip.is_some() { pulse >= length } else { true };
// Is it time for switchover?
if self.next_clip().is_some() && past_end {
return true
}
// If there's a currently playing clip, output notes from it to buffer:
if let Some(ref clip) = clip {
// Source clip from which the MIDI events will be taken.
let clip = clip.read().unwrap();
// Clip with zero length is not processed
if clip.length > 0 {
// Current pulse index in source clip
let pulse = pulse % clip.length;
// Output each MIDI event from clip at appropriate frames of output buffer:
for message in clip.notes[pulse].iter() {
for port in self.midi_outs.iter_mut() {
port.buffer_write(sample, LiveEvent::Midi {
channel: 0.into(), /* TODO */
message: *message
});
}
}
}
}
}
false
} else {
true
}
}
/// Handle switchover from current to next playing clip.
fn process_switchover (&mut self, scope: &ProcessScope) {
let midi_buf = self.midi_buf_mut();
let sample0 = scope.last_frame_time() as usize;
//let samples = scope.n_frames() as usize;
if let Some((start_at, clip)) = &self.next_clip() {
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 clip:
if start <= sample0.saturating_sub(sample) {
// Samples elapsed since clip was supposed to start
let _skipped = sample0 - start;
// Switch over to enqueued clip
let started = Moment::from_sample(self.clock().timebase(), start as f64);
// Launch enqueued clip
*self.play_clip_mut() = Some((started, clip.clone()));
// Unset enqueuement (TODO: where to implement looping?)
*self.next_clip_mut() = None;
// Fill in remaining ticks of chunk from next clip.
self.process_playback(scope);
}
}
}
}
pub trait HasMidiBuffers {
fn note_buf_mut (&mut self) -> &mut Vec<u8>;
fn midi_buf_mut (&mut self) -> &mut Vec<Vec<Vec<u8>>>;
}
impl HasMidiBuffers for Sequencer {
fn note_buf_mut (&mut self) -> &mut Vec<u8> {
&mut self.note_buf
}
fn midi_buf_mut (&mut self) -> &mut Vec<Vec<Vec<u8>>> {
&mut self.midi_buf
}
}
pub trait MidiMonitor: HasMidiIns + HasMidiBuffers {
fn notes_in (&self) -> &Arc<RwLock<[bool;128]>>;
fn monitoring (&self) -> bool;
fn monitoring_mut (&mut self) -> &mut bool;
fn toggle_monitor (&mut self) {
*self.monitoring_mut() = !self.monitoring();
}
fn monitor (&mut self, scope: &ProcessScope) {
}
}
pub trait MidiRecord: MidiMonitor + HasClock + HasPlayClip {
fn recording (&self) -> bool;
fn recording_mut (&mut self) -> &mut bool;
fn toggle_record (&mut self) {
*self.recording_mut() = !self.recording();
}
fn overdub (&self) -> bool;
fn overdub_mut (&mut self) -> &mut bool;
fn toggle_overdub (&mut self) {
*self.overdub_mut() = !self.overdub();
}
fn record_clip (
&mut self,
scope: &ProcessScope,
started: Moment,
clip: &Option<Arc<RwLock<MidiClip>>>,
) {
if let Some(clip) = clip {
let sample0 = scope.last_frame_time() as usize;
let start = started.sample.get() as usize;
let _recording = self.recording();
let timebase = self.clock().timebase().clone();
let quant = self.clock().quant.get();
let mut clip = clip.write().unwrap();
let length = clip.length;
for input in self.midi_ins_mut().iter() {
for (sample, event, _bytes) in parse_midi_input(input.port().iter(scope)) {
if let LiveEvent::Midi { message, .. } = event {
clip.record_event({
let sample = (sample0 + sample - start) as f64;
let pulse = timebase.samples_to_pulse(sample);
let quantized = (pulse / quant).round() * quant;
quantized as usize % length
}, message);
}
}
}
}
}
fn record_next (&mut self) {
// TODO switch to next clip and record into it
}
}