use std::io;
use std::sync::Arc;
use std::sync::atomic::Ordering;
use std::time::Duration;
use plughost_core::internal::level;
use plughost_core::ipc::shared::clock;
use plughost_core::ipc::shared::realtime::{MAX_LANES, Outcome, Ring};
use plughost_core::{
AutomationEvent, BlockContext, Events, FailureKind, InputError, RoutedChainConfig, Sample,
SampleFormat,
};
use super::dry::DryDelay;
use super::notes::Notes;
use super::{Fallback, RealtimeConfig, State};
use crate::Error;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum RealtimeOutput {
Processed,
Fallback(FallbackReason),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum FallbackReason {
Stopped,
Priming,
Missed,
Resync,
}
pub struct RealtimeEndpoint {
ring: Box<Ring>,
state: Arc<State>,
quantum: usize,
latency: u64,
fallback: Fallback,
format: SampleFormat,
sample_rate: f64,
input_channels: usize,
output_channels: usize,
event_inputs: usize,
event_outputs: usize,
slots: usize,
generation: u64,
sequence: u64,
wet: bool,
crossfade: bool,
last: Vec<f64>,
completion: Option<u64>,
dry: Option<Box<DryDelay>>,
planned: Option<u32>,
notes: Notes,
ending: bool,
missed: [u64; MAX_LANES],
missed_len: usize,
late: Events,
scratch: Events,
}
impl std::fmt::Debug for RealtimeEndpoint {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("RealtimeEndpoint")
.field("quantum", &self.quantum)
.field("latency", &self.latency)
.field("fallback", &self.fallback)
.field("generation", &self.generation)
.field("sequence", &self.sequence)
.finish_non_exhaustive()
}
}
trait Wire: Sample {
fn from_f64(value: f64) -> Self;
fn submit(
ring: &mut Ring,
generation: u64,
sequence: u64,
context: &BlockContext,
input: &[&[Self]],
automation: &[AutomationEvent],
events: &Events,
) -> io::Result<()>;
fn read(
ring: &mut Ring,
generation: u64,
sequence: u64,
output: &mut [&mut [Self]],
event_ports: usize,
produced: &mut Events,
) -> io::Result<Option<(Outcome, usize)>>;
}
macro_rules! wire {
($sample:ty, $submit:ident, $read:ident) => {
impl Wire for $sample {
fn from_f64(value: f64) -> Self {
value as $sample
}
fn submit(
ring: &mut Ring,
generation: u64,
sequence: u64,
context: &BlockContext,
input: &[&[Self]],
automation: &[AutomationEvent],
events: &Events,
) -> io::Result<()> {
ring.$submit(generation, sequence, context, input, automation, events)
}
fn read(
ring: &mut Ring,
generation: u64,
sequence: u64,
output: &mut [&mut [Self]],
event_ports: usize,
produced: &mut Events,
) -> io::Result<Option<(Outcome, usize)>> {
ring.$read(generation, sequence, output, event_ports, produced)
}
}
};
}
wire!(f32, submit_f32, read_f32);
wire!(f64, submit_f64, read_f64);
impl RealtimeEndpoint {
pub(crate) fn new(
ring: Ring,
state: Arc<State>,
config: &RealtimeConfig,
chain: &RoutedChainConfig,
dry: Option<Box<DryDelay>>,
) -> RealtimeEndpoint {
let output_channels = chain.output_channels();
RealtimeEndpoint {
ring: Box::new(ring),
state,
quantum: config.quantum,
latency: config.latency as u64,
fallback: config.fallback,
format: chain.sample_format,
sample_rate: chain.sample_rate,
input_channels: chain.input_channels(),
output_channels,
event_inputs: chain.event_inputs,
event_outputs: chain.event_outputs(),
slots: chain.slots.len(),
generation: 0,
sequence: 0,
wet: false,
crossfade: false,
last: vec![0.0; output_channels],
completion: None,
dry,
planned: None,
notes: Notes::new(),
ending: false,
missed: [0; MAX_LANES],
missed_len: 0,
late: Events::for_block(),
scratch: Events::for_block(),
}
}
#[cfg(test)]
pub(super) fn ring(&mut self) -> &mut Ring {
&mut self.ring
}
fn validate(
&self,
context: &BlockContext,
automation: &[AutomationEvent],
events: &Events,
) -> Result<(), InputError> {
context.validate()?;
if let Some(transport) = context.transport {
transport.validate_at_rate(self.sample_rate)?;
}
plughost_core::internal::validate_event_budget(automation.len(), events)?;
if let Some(event) = events.iter().find(|event| event.port >= self.event_inputs) {
return Err(InputError::EventPort { port: event.port });
}
if !plughost_core::internal::events_fit(events, context.frames) {
return Err(InputError::Events);
}
if automation.iter().any(|event| {
event.slot >= self.slots
|| !plughost_core::internal::changes_fit(&[event.change], context.frames)
}) || automation
.windows(2)
.any(|pair| pair[0].change.offset > pair[1].change.offset)
{
return Err(InputError::Automation);
}
Ok(())
}
fn process<S: Wire>(
&mut self,
context: &BlockContext,
input: &[&[S]],
output: &mut [&mut [S]],
automation: &[AutomationEvent],
events: &Events,
produced: &mut Events,
) -> Result<RealtimeOutput, Error> {
let _audit = plughost_core::audit::processing();
let state = Arc::clone(&self.state);
let _call = state.enter();
let played = self.serve(context, input, output, automation, events, produced);
if played.is_ok() && std::mem::take(&mut self.ending) {
let dropped = self.notes.end_all(produced);
self.state
.dropped_events
.fetch_add(dropped, Ordering::Relaxed);
}
if !self.late.is_empty() {
self.deliver_late(produced);
}
played
}
fn take_late(&mut self, generation: u64) {
let mut kept = 0;
let mut dropped = 0;
for index in 0..self.missed_len {
let sequence = self.missed[index];
match self
.ring
.read_events(generation, sequence, self.event_outputs, &mut self.scratch)
{
Ok(None) => {
self.missed[kept] = sequence;
kept += 1;
}
Ok(Some(skipped)) => {
dropped += skipped as u64;
for event in self.scratch.iter() {
if !self.late.push_within_capacity(event.at(0)) {
dropped += 1;
}
}
}
Err(_) => {}
}
}
self.missed_len = kept;
self.notes.record(&self.late);
self.state
.dropped_events
.fetch_add(dropped, Ordering::Relaxed);
}
fn deliver_late(&mut self, produced: &mut Events) {
self.scratch.clear();
let mut dropped = 0;
for event in produced.iter() {
if !self.scratch.push_within_capacity(event) {
dropped += 1;
}
}
produced.clear();
for event in self.late.iter().chain(self.scratch.iter()) {
if !produced.push_within_capacity(event) {
dropped += 1;
}
}
self.late.clear();
self.state
.dropped_events
.fetch_add(dropped, Ordering::Relaxed);
}
fn serve<S: Wire>(
&mut self,
context: &BlockContext,
input: &[&[S]],
output: &mut [&mut [S]],
automation: &[AutomationEvent],
events: &Events,
produced: &mut Events,
) -> Result<RealtimeOutput, Error> {
produced.clear();
if S::FORMAT != self.format {
return Err(Error::Input {
slot: None,
error: InputError::AudioSampleFormat,
});
}
let frames = context.frames;
if output.len() != self.output_channels
|| output.iter().any(|channel| channel.len() != frames)
{
return Err(Error::Input {
slot: None,
error: InputError::AudioBuffers,
});
}
State::count(&self.state.callbacks);
let generation = self.state.generation.load(Ordering::Acquire);
if generation != self.generation {
self.begin(generation);
}
let input_fits = input.len() == self.input_channels
&& input.iter().all(|channel| channel.len() == frames);
if let Some(dry) = &mut self.dry {
dry.push(input_fits.then_some(input), frames);
}
if self.state.closed.load(Ordering::Acquire) {
return Ok(self.fallback(output, FallbackReason::Stopped));
}
if !self.state.running.load(Ordering::Acquire) {
return Ok(self.fallback(output, FallbackReason::Stopped));
}
if self.state.resync.load(Ordering::Relaxed) || self.state.halted(self.generation) {
return Ok(self.fallback(output, FallbackReason::Resync));
}
if frames != self.quantum {
State::count(&self.state.size_mismatches);
self.state.resync.store(true, Ordering::Relaxed);
return Ok(self.fallback(output, FallbackReason::Resync));
}
let invalid = if input_fits {
self.validate(context, automation, events)
.map_err(|error| Error::Input { slot: None, error })
} else {
Err(Error::Input {
slot: None,
error: InputError::AudioBuffers,
})
};
if let Err(error) = invalid {
self.state.resync.store(true, Ordering::Relaxed);
self.fallback(output, FallbackReason::Resync);
return Err(error);
}
let sequence = self.sequence + 1;
if !self.ring.lane_free(sequence) {
State::count(&self.state.overflows);
self.state.resync.store(true, Ordering::Relaxed);
return Ok(self.fallback(output, FallbackReason::Resync));
}
if self.missed_len > 0 {
self.take_late(generation);
}
let submitted = S::submit(
&mut self.ring,
generation,
sequence,
context,
input,
automation,
events,
);
if submitted.is_err() {
self.state.fail(None, FailureKind::Protocol);
return Ok(self.fallback(output, FallbackReason::Resync));
}
self.sequence = sequence;
if sequence <= self.latency {
return Ok(self.fallback(output, FallbackReason::Priming));
}
let read = S::read(
&mut self.ring,
generation,
sequence - self.latency,
output,
self.event_outputs,
produced,
);
match read {
Ok(Some((
Outcome::Done {
latency, elapsed, ..
},
dropped,
))) => {
if *self.planned.get_or_insert(latency) != latency {
produced.clear();
self.state.resync.store(true, Ordering::Relaxed);
return Ok(self.fallback(output, FallbackReason::Resync));
}
State::count(&self.state.processed);
self.notes.record(produced);
self.completion = Some(elapsed);
self.state
.dropped_events
.fetch_add(dropped as u64, Ordering::Relaxed);
self.state.latency.store(latency, Ordering::Relaxed);
self.play(output);
if level(output, 0..frames).non_finite.is_some() {
State::count(&self.state.non_finite_outputs);
}
Ok(RealtimeOutput::Processed)
}
Ok(Some((Outcome::Failed { slot, kind }, _))) => {
produced.clear();
match slot {
Some(slot) if slot >= self.slots => {
self.state.fail(None, FailureKind::Protocol);
}
_ => self.state.fail(slot, kind),
}
Ok(self.fallback(output, FallbackReason::Resync))
}
Ok(None) => {
produced.clear();
State::count(&self.state.missed);
if let Some(missed) = self.missed.get_mut(self.missed_len) {
*missed = sequence - self.latency;
self.missed_len += 1;
}
Ok(self.fallback(output, FallbackReason::Missed))
}
Err(_) => {
produced.clear();
self.state.fail(None, FailureKind::Protocol);
Ok(self.fallback(output, FallbackReason::Resync))
}
}
}
pub fn last_completion(&self) -> Option<Duration> {
self.completion.map(clock::duration)
}
fn begin(&mut self, generation: u64) {
self.generation = generation;
self.sequence = 0;
self.planned = None;
self.ending = true;
self.missed_len = 0;
self.state.ring.receive(&mut self.ring);
if let Some(dry) = &mut self.dry {
self.state.dry.receive(dry);
dry.set_delay(self.state.dry_delay.load(Ordering::Relaxed) as usize);
}
}
fn fallback_sample(&self, channel: usize, frame: usize, frames: usize) -> f64 {
match (&self.dry, self.fallback) {
(Some(dry), Fallback::Dry) => dry.delayed(channel, frame, frames),
_ => 0.0,
}
}
fn fallback<S: Wire>(
&mut self,
output: &mut [&mut [S]],
reason: FallbackReason,
) -> RealtimeOutput {
for (index, channel) in output.iter_mut().enumerate() {
let frames = channel.len();
let start = self.fallback_sample(index, 0, frames);
let held = if self.last[index].is_finite() {
self.last[index]
} else {
0.0
};
for (frame, sample) in channel.iter_mut().enumerate() {
let mut value = self.fallback_sample(index, frame, frames);
if self.wet {
let remaining = 1.0 - (frame + 1) as f64 / frames as f64;
value += (held - start) * remaining;
}
*sample = S::from_f64(value);
}
if let Some(&last) = channel.last() {
self.last[index] = last.to_f64();
}
}
self.wet = false;
self.crossfade = reason == FallbackReason::Missed;
self.completion = None;
RealtimeOutput::Fallback(reason)
}
fn play<S: Wire>(&mut self, output: &mut [&mut [S]]) {
for (index, channel) in output.iter_mut().enumerate() {
let frames = channel.len();
if self.crossfade {
for frame in 0..frames {
let gain = (frame + 1) as f64 / frames as f64;
let fallback = self.fallback_sample(index, frame, frames);
channel[frame] =
S::from_f64(fallback * (1.0 - gain) + channel[frame].to_f64() * gain);
}
}
if let Some(&last) = channel.last() {
self.last[index] = last.to_f64();
}
}
self.wet = true;
self.crossfade = false;
}
}
macro_rules! endpoint_process {
($sample:ty, $method:ident) => {
impl RealtimeEndpoint {
pub fn $method(
&mut self,
context: &BlockContext,
input: &[&[$sample]],
output: &mut [&mut [$sample]],
automation: &[AutomationEvent],
events: &Events,
produced: &mut Events,
) -> Result<RealtimeOutput, Error> {
self.process(context, input, output, automation, events, produced)
}
}
};
}
endpoint_process!(f32, process_audio_f32);
endpoint_process!(f64, process_audio_f64);