pub trait AudioOut: Send {
fn configure(&mut self, rate: u32, channels: u32);
fn push_le_f32(&mut self, bytes: &[u8]);
fn media_bus(&self) -> Option<std::sync::Arc<crate::media::MediaBus>> {
None
}
}
pub struct Resampler {
dst_rate: u32,
src_rate: u32,
channels: u32,
t: f64,
prev: [f32; 2],
}
impl Resampler {
pub fn new(dst_rate: u32) -> Resampler {
Resampler { dst_rate, src_rate: 44100, channels: 2, t: 0.0, prev: [0.0; 2] }
}
pub fn configure(&mut self, rate: u32, channels: u32) {
self.src_rate = rate;
self.channels = channels;
}
pub fn process(&mut self, bytes: &[u8], out: &mut Vec<f32>) {
let step = self.src_rate as f64 / self.dst_rate as f64;
let ch = self.channels as usize;
out.reserve(((bytes.len() / 4 / ch) as f64 / step) as usize * 2 + 4);
for frame in bytes.chunks_exact(4 * ch) {
let s = |i: usize| f32::from_le_bytes([frame[i * 4], frame[i * 4 + 1], frame[i * 4 + 2], frame[i * 4 + 3]]);
let cur = if ch == 1 { [s(0), s(0)] } else { [s(0), s(1)] };
while self.t < 1.0 {
let t = self.t as f32;
out.push(self.prev[0] + (cur[0] - self.prev[0]) * t);
out.push(self.prev[1] + (cur[1] - self.prev[1]) * t);
self.t += step;
}
self.t -= 1.0;
self.prev = cur;
}
}
}
#[cfg(feature = "window")]
pub use sink::{AudioSink, AudioStream};
#[cfg(feature = "window")]
mod sink {
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, AtomicU32, AtomicUsize, Ordering};
use std::time::{Duration, Instant};
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
use cpal::{FromSample, SizedSample};
use super::{AudioOut, Resampler};
use crate::media::MediaBus;
const TARGET_LATENCY_S: f32 = 0.06;
const TARGET_STEP_S: f32 = 0.03;
const TARGET_MAX_S: f32 = 0.15;
const HEADROOM_S: f32 = 0.14;
struct Ring {
slots: Box<[AtomicU32]>,
head: AtomicUsize,
tail: AtomicUsize,
trim: AtomicBool,
target: AtomicUsize,
underruns: AtomicUsize,
}
impl Ring {
fn new(capacity: usize) -> Ring {
Ring {
slots: (0..capacity).map(|_| AtomicU32::new(0)).collect(),
head: AtomicUsize::new(0),
tail: AtomicUsize::new(0),
trim: AtomicBool::new(false),
target: AtomicUsize::new(0),
underruns: AtomicUsize::new(0),
}
}
fn len(&self) -> usize {
self.head.load(Ordering::Acquire) - self.tail.load(Ordering::Acquire)
}
fn push(&self, samples: &[f32]) {
let head = self.head.load(Ordering::Relaxed);
let free = self.slots.len() - (head - self.tail.load(Ordering::Acquire));
let n = samples.len().min(free) & !1;
for (i, s) in samples[..n].iter().enumerate() {
self.slots[(head + i) % self.slots.len()].store(s.to_bits(), Ordering::Relaxed);
}
self.head.store(head + n, Ordering::Release);
}
fn pop(&self) -> Option<(f32, f32)> {
let tail = self.tail.load(Ordering::Relaxed);
if self.head.load(Ordering::Acquire) - tail < 2 {
return None;
}
let get = |i: usize| f32::from_bits(self.slots[i % self.slots.len()].load(Ordering::Relaxed));
let frame = (get(tail), get(tail + 1));
self.tail.store(tail + 2, Ordering::Release);
Some(frame)
}
fn skip_to(&self, keep: usize) {
let tail = self.tail.load(Ordering::Relaxed);
let len = self.head.load(Ordering::Acquire) - tail;
if len > keep {
self.tail.store(tail + ((len - keep) & !1), Ordering::Release);
}
}
}
pub struct AudioSink {
ring: Arc<Ring>,
bus: Arc<MediaBus>,
device_rate: u32,
resampler: Resampler,
scratch: Vec<f32>,
per_second: usize,
underruns_seen: usize,
last_underrun: Option<Instant>,
}
pub struct AudioStream {
stream: cpal::Stream,
ring: Arc<Ring>,
bus: Arc<MediaBus>,
device_name: Option<String>,
device_rate: u32,
broken: Arc<AtomicBool>,
last_check: Instant,
}
fn build<T: SizedSample + FromSample<f32>>(
device: &cpal::Device,
config: &cpal::StreamConfig,
ring: Arc<Ring>,
broken: Arc<AtomicBool>,
bus: Arc<MediaBus>,
) -> Result<cpal::Stream, cpal::Error> {
let channels = config.channels as usize;
let mut primed = false;
let mut mix: Vec<f32> = Vec::new();
device.build_output_stream::<T, _, _>(
*config,
move |out: &mut [T], _| {
let target = ring.target.load(Ordering::Relaxed);
if ring.trim.swap(false, Ordering::AcqRel) {
ring.skip_to(target);
}
if !primed && ring.len() >= target {
primed = true;
}
let frames = out.len() / channels.max(1);
mix.clear();
mix.resize(frames * 2, 0.0);
for f in 0..frames {
let (l, r) = match ring.pop().filter(|_| primed) {
Some(s) => s,
None => {
if primed {
ring.underruns.fetch_add(1, Ordering::Relaxed);
}
primed = false; (0.0, 0.0)
}
};
(mix[f * 2], mix[f * 2 + 1]) = (l, r);
}
bus.mix(&mut mix);
for (f, frame) in out.chunks_mut(channels).enumerate() {
let (l, r) = (mix[f * 2].clamp(-1.0, 1.0), mix[f * 2 + 1].clamp(-1.0, 1.0));
match frame.len() {
1 => frame[0] = T::from_sample(0.5 * (l + r)),
_ => {
frame[0] = T::from_sample(l);
frame[1] = T::from_sample(r);
for s in &mut frame[2..] {
*s = T::from_sample(0.0);
}
}
}
}
},
move |e| {
eprintln!("[audio] stream error: {e}");
broken.store(true, Ordering::Release);
},
None,
)
}
fn open_stream(ring: &Arc<Ring>, broken: &Arc<AtomicBool>, bus: &Arc<MediaBus>) -> Result<(cpal::Stream, Option<String>, u32), String> {
let host = cpal::default_host();
let device = host.default_output_device().ok_or("no audio output device")?;
let name = device.id().ok().map(|i| i.to_string());
if let Ok(d) = device.description() {
eprintln!("[audio] output: {}", d.name());
}
let supported = device.default_output_config().map_err(|e| e.to_string())?;
let format = supported.sample_format();
let config = supported.config();
let (r, b, m) = (ring.clone(), broken.clone(), bus.clone());
use cpal::SampleFormat as F;
let stream = match format {
F::F32 => build::<f32>(&device, &config, r, b, m),
F::F64 => build::<f64>(&device, &config, r, b, m),
F::I16 => build::<i16>(&device, &config, r, b, m),
F::U16 => build::<u16>(&device, &config, r, b, m),
F::I32 => build::<i32>(&device, &config, r, b, m),
F::U32 => build::<u32>(&device, &config, r, b, m),
F::I8 => build::<i8>(&device, &config, r, b, m),
F::U8 => build::<u8>(&device, &config, r, b, m),
other => return Err(format!("unsupported device sample format {other:?}")),
}
.map_err(|e| e.to_string())?;
stream.play().map_err(|e| e.to_string())?;
Ok((stream, name, config.sample_rate))
}
impl AudioStream {
pub fn check(&mut self) {
if self.last_check.elapsed() < Duration::from_secs(1) {
return;
}
self.last_check = Instant::now();
let now = cpal::default_host().default_output_device().and_then(|d| d.id().ok().map(|i| i.to_string()));
let changed = now.is_some() && now != self.device_name;
if !self.broken.load(Ordering::Acquire) && !changed {
return;
}
self.broken.store(false, Ordering::Release);
match open_stream(&self.ring, &self.broken, &self.bus) {
Ok((stream, name, rate)) if rate == self.device_rate => (self.stream, self.device_name) = (stream, name),
Ok((_, name, rate)) => {
eprintln!("[audio] the new output runs at {rate} Hz, not {} Hz: staying on the old one", self.device_rate);
self.device_name = name;
}
Err(e) => eprintln!("[audio] couldn't reopen the output: {e}"),
}
}
}
impl AudioSink {
pub fn open() -> Result<(AudioSink, AudioStream), String> {
let device_rate = cpal::default_host()
.default_output_device()
.ok_or("no audio output device")?
.default_output_config()
.map_err(|e| e.to_string())?
.config()
.sample_rate;
let per_second = device_rate as usize * 2;
let max = (per_second as f32 * (TARGET_MAX_S + HEADROOM_S)) as usize;
let ring = Arc::new(Ring::new(max * 2));
ring.target.store((per_second as f32 * TARGET_LATENCY_S) as usize & !1, Ordering::Relaxed);
let broken = Arc::new(AtomicBool::new(false));
let bus = MediaBus::new(device_rate);
let (stream, device_name, device_rate) = open_stream(&ring, &broken, &bus)?;
let sink = AudioSink {
ring: ring.clone(), bus: bus.clone(), device_rate, resampler: Resampler::new(device_rate), scratch: Vec::new(),
per_second, underruns_seen: 0, last_underrun: None,
};
Ok((sink, AudioStream { stream, ring, bus, device_name, device_rate, broken, last_check: Instant::now() }))
}
pub fn device_rate(&self) -> u32 {
self.device_rate
}
}
impl AudioOut for AudioSink {
fn configure(&mut self, rate: u32, channels: u32) {
self.resampler.configure(rate, channels);
}
fn media_bus(&self) -> Option<Arc<MediaBus>> {
Some(self.bus.clone())
}
fn push_le_f32(&mut self, bytes: &[u8]) {
self.scratch.clear();
self.resampler.process(bytes, &mut self.scratch);
self.ring.push(&self.scratch);
let underruns = self.ring.underruns.load(Ordering::Relaxed);
if underruns != self.underruns_seen {
self.underruns_seen = underruns;
let again = self.last_underrun.is_some_and(|t| t.elapsed() < Duration::from_secs(5));
self.last_underrun = Some(Instant::now());
let (target, max) = (self.ring.target.load(Ordering::Relaxed), (self.per_second as f32 * TARGET_MAX_S) as usize);
if again && target < max {
let more = (target + (self.per_second as f32 * TARGET_STEP_S) as usize).min(max) & !1;
self.ring.target.store(more, Ordering::Relaxed);
eprintln!("[audio] underruns: buffering {} ms", more * 1000 / self.per_second);
}
}
let target = self.ring.target.load(Ordering::Relaxed);
if self.ring.len() > target + (self.per_second as f32 * HEADROOM_S) as usize {
self.ring.trim.store(true, Ordering::Release);
}
}
}
}