Skip to content

Clone your own voice on your own computer: speak any text in 60+ languages and dub videos into another language in your voice. Fully local. Early version.

29.8 KB
Raw Blame
//! cpal streams are not `Send`, so each `Recorder` / `Player` owns a worker thread that creates and holds the stream.

#![allow(dead_code)]

use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU32, AtomicU64, Ordering};
use std::sync::mpsc::{self, Sender};
use std::sync::{Arc, Mutex};
use std::thread::{self, JoinHandle};

use anyhow::{Context, anyhow};
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
use cpal::{FromSample, SampleFormat, SizedSample};

pub mod stretch;
use stretch::{Flow, Stretcher};

pub struct RecInfo {
    pub path: PathBuf,
    pub duration_s: f32,
    pub peak: f32,
}

fn lock<T>(m: &Mutex<T>) -> std::sync::MutexGuard<'_, T> {
    m.lock().unwrap_or_else(|e| e.into_inner())
}

fn load_f32(a: &AtomicU32) -> f32 {
    f32::from_bits(a.load(Ordering::Relaxed))
}

fn fetch_max_f32(a: &AtomicU32, v: f32) {
    let mut cur = a.load(Ordering::Relaxed);
    while v > f32::from_bits(cur) {
        match a.compare_exchange_weak(cur, v.to_bits(), Ordering::Relaxed, Ordering::Relaxed) {
            Ok(_) => break,
            Err(actual) => cur = actual,
        }
    }
}

/// Names of the available input devices; except on Linux, the first is the system default.
pub fn input_devices() -> Vec<String> {
    let host = cpal::default_host();
    let names: Vec<String> =
        host.input_devices().map(|it| it.filter_map(|d| d.name().ok()).collect()).unwrap_or_default();
    #[cfg(target_os = "linux")]
    {
        alsa_cards(names)
    }
    #[cfg(not(target_os = "linux"))]
    {
        let mut names = names;
        if let Some(def) = host.default_input_device().and_then(|d| d.name().ok()) {
            names.retain(|n| *n != def);
            names.insert(0, def);
        }
        names.dedup();
        names
    }
}

/// ALSA also lists its plugins ("default", "pipewire", "dsnoop:...") and each card twice (hw: and plughw:):
/// keep one entry per capture device, plughw: (it converts the sample format and rate) when there is one.
#[cfg(target_os = "linux")]
fn alsa_cards(names: Vec<String>) -> Vec<String> {
    let mut out: Vec<String> = Vec::new();
    for name in &names {
        let Some(dev) = name.strip_prefix("plughw:").or_else(|| name.strip_prefix("hw:")) else { continue };
        let plug = format!("plughw:{dev}");
        let pick = if names.contains(&plug) { plug } else { name.clone() };
        if !out.contains(&pick) {
            out.push(pick);
        }
    }
    out
}

/// What the device list shows: the card and device names the kernel gives, for an ALSA "plughw:CARD=X,DEV=N".
pub fn device_label(name: &str) -> String {
    #[cfg(target_os = "linux")]
    if let Some(label) = alsa_label(name) {
        return label;
    }
    name.to_owned()
}

#[cfg(target_os = "linux")]
fn alsa_label(name: &str) -> Option<String> {
    let spec = name.strip_prefix("plughw:").or_else(|| name.strip_prefix("hw:"))?;
    let field = |key: &str| spec.split(',').find_map(|kv| kv.strip_prefix(key)).map(str::to_owned);
    let (card, dev) = (field("CARD=")?, field("DEV=").unwrap_or_else(|| "0".into()));
    let cards = std::fs::read_to_string("/proc/asound/cards").ok()?;
    // " 2 [Generic        ]: HDA-Intel - HD-Audio Generic"
    let (index, card_name) = cards.lines().find_map(|line| {
        let (num, rest) = line.trim_start().split_once(' ')?;
        let (id, rest) = rest.trim_start().strip_prefix('[')?.split_once(']')?;
        (id.trim() == card).then(|| (num.parse::<u32>().ok(), rest.split_once(" - ").map(|(_, n)| n.trim().to_owned())))
    })?;
    let card_name = card_name.unwrap_or(card);
    let pcm = index.and_then(|i| {
        let dev: u32 = dev.parse().ok()?;
        let pcm = std::fs::read_to_string("/proc/asound/pcm").ok()?;
        // "02-00: ALC897 Analog : ALC897 Analog : playback 1 : capture 1"
        let prefix = format!("{i:02}-{dev:02}:");
        pcm.lines().find_map(|l| l.strip_prefix(&prefix).and_then(|r| r.split(':').nth(1)).map(|n| n.trim().to_owned()))
    });
    Some(match pcm.filter(|n| !n.is_empty() && *n != card_name) {
        Some(device) => format!("{card_name} · {device}"),
        None => card_name,
    })
}

fn pick_input(device: Option<&str>) -> anyhow::Result<cpal::Device> {
    let host = cpal::default_host();
    if let Some(wanted) = device {
        let found = host
            .input_devices()
            .map_err(|e| anyhow!("cannot list input devices: {e}"))?
            .find(|d| d.name().map(|n| n == wanted).unwrap_or(false));
        if let Some(d) = found {
            return Ok(d);
        }
    }
    host.default_input_device()
        .ok_or_else(|| anyhow!("no microphone found: connect an input device and check the system sound settings"))
}

pub const METER_HZ: u32 = 25;

#[derive(Clone, Debug)]
pub enum CaptureEvent {
    /// `level`: meter 0..1 (peak with a 0.25 s decay); `peak`: highest sample so far.
    Level {
        level: f32,
        peak: f32,
        elapsed_s: f32,
    },
    Error(String),
}

/// Called on the audio thread: keep it short.
pub type MeterSink = Arc<dyn Fn(CaptureEvent) + Send + Sync>;

pub const PROGRESS_HZ: u32 = 30;

/// `track` is the id from [`Player::play`]; reports about a replaced track can still arrive and must be ignored.
/// `jumped`: the position jumped since the last report (a seek, a start, a loop); otherwise it only moved on.
#[derive(Clone, Copy, Debug)]
pub enum PlayEvent {
    Progress { track: u64, pos_s: f32, len_s: f32, jumped: bool },
    Ended { track: u64 },
}

/// Called on the audio thread: keep it short.
pub type PlaySink = Arc<dyn Fn(PlayEvent) + Send + Sync>;

struct RecShared {
    buffer: Mutex<Vec<f32>>,
    /// Max |sample|, as f32 bits.
    peak: AtomicU32,
    samples: AtomicU64,
    error: Mutex<Option<String>>,
}

pub struct Recorder {
    shared: Arc<RecShared>,
    sample_rate: u32,
    device_name: String,
    stop: Option<Sender<()>>,
    thread: Option<JoinHandle<()>>,
}

fn build_input<T>(
    device: &cpal::Device,
    config: &cpal::StreamConfig,
    shared: Arc<RecShared>,
    meter: Option<MeterSink>,
) -> Result<cpal::Stream, cpal::BuildStreamError>
where
    T: SizedSample,
    f32: FromSample<T>,
{
    let channels = config.channels.max(1) as usize;
    let rate = config.sample_rate.0.max(1);
    let err_shared = shared.clone();
    let err_meter = meter.clone();
    let mut scratch: Vec<f32> = Vec::new();
    let every = (rate / METER_HZ).max(1) as u64;
    let (mut since, mut block_max, mut shown) = (0u64, 0.0f32, 0.0f32);
    device.build_input_stream(
        config,
        move |data: &[T], _| {
            scratch.clear();
            let mut block_peak = 0.0f32;
            for frame in data.chunks(channels) {
                let mut sum = 0.0f32;
                for s in frame {
                    sum += <f32 as FromSample<T>>::from_sample_(*s);
                }
                let mono = sum / frame.len() as f32;
                block_peak = block_peak.max(mono.abs());
                scratch.push(mono);
            }
            lock(&shared.buffer).extend_from_slice(&scratch);
            let total = shared.samples.fetch_add(scratch.len() as u64, Ordering::Relaxed) + scratch.len() as u64;
            fetch_max_f32(&shared.peak, block_peak);
            if let Some(sink) = &meter {
                since += scratch.len() as u64;
                block_max = block_max.max(block_peak);
                if since >= every {
                    let dt = since as f32 / rate as f32;
                    shown = block_max.max(shown * (-dt / 0.25).exp()).min(1.0);
                    sink(CaptureEvent::Level {
                        level: shown,
                        peak: load_f32(&shared.peak),
                        elapsed_s: total as f32 / rate as f32,
                    });
                    since = 0;
                    block_max = 0.0;
                }
            }
        },
        move |err| {
            *lock(&err_shared.error) = Some(err.to_string());
            if let Some(sink) = &err_meter {
                sink(CaptureEvent::Error(err.to_string()));
            }
        },
        None,
    )
}

impl Recorder {
    /// `device`: None = system default.
    pub fn start(device: Option<&str>, meter: Option<MeterSink>) -> anyhow::Result<Self> {
        let shared = Arc::new(RecShared {
            buffer: Mutex::new(Vec::with_capacity(48_000 * 120)),
            peak: AtomicU32::new(0),
            samples: AtomicU64::new(0),
            error: Mutex::new(None),
        });
        let (init_tx, init_rx) = mpsc::channel::<Result<(u32, String), String>>();
        let (stop_tx, stop_rx) = mpsc::channel::<()>();
        let device_wanted = device.map(str::to_string);
        let thread_shared = shared.clone();

        let thread = thread::Builder::new()
            .name("audio-capture".into())
            .spawn(move || {
                let setup = (|| -> anyhow::Result<(cpal::Stream, u32, String)> {
                    let dev = pick_input(device_wanted.as_deref())?;
                    let name = dev.name().unwrap_or_else(|_| "unknown device".into());
                    let supported = dev
                        .default_input_config()
                        .map_err(|e| anyhow!("cannot open the microphone \"{name}\": {e}"))?;
                    let rate = supported.sample_rate().0;
                    let format = supported.sample_format();
                    let config: cpal::StreamConfig = supported.into();
                    let stream = match format {
                        SampleFormat::F32 => build_input::<f32>(&dev, &config, thread_shared, meter),
                        SampleFormat::I16 => build_input::<i16>(&dev, &config, thread_shared, meter),
                        SampleFormat::U16 => build_input::<u16>(&dev, &config, thread_shared, meter),
                        SampleFormat::I32 => build_input::<i32>(&dev, &config, thread_shared, meter),
                        SampleFormat::I8 => build_input::<i8>(&dev, &config, thread_shared, meter),
                        SampleFormat::U8 => build_input::<u8>(&dev, &config, thread_shared, meter),
                        SampleFormat::F64 => build_input::<f64>(&dev, &config, thread_shared, meter),
                        other => return Err(anyhow!("unsupported microphone sample format {other:?}")),
                    }
                    .map_err(|e| anyhow!("cannot start capture on \"{name}\": {e}"))?;
                    stream.play().map_err(|e| anyhow!("cannot start capture on \"{name}\": {e}"))?;
                    Ok((stream, rate, name))
                })();
                match setup {
                    Ok((stream, rate, name)) => {
                        let _ = init_tx.send(Ok((rate, name)));
                        let _ = stop_rx.recv(); // returns on stop() or when the Recorder is dropped
                        drop(stream);
                    }
                    Err(e) => {
                        let _ = init_tx.send(Err(format!("{e:#}")));
                    }
                }
            })
            .context("cannot start the audio capture thread")?;

        match init_rx.recv() {
            Ok(Ok((sample_rate, device_name))) => {
                Ok(Self { shared, sample_rate, device_name, stop: Some(stop_tx), thread: Some(thread) })
            }
            Ok(Err(msg)) => {
                let _ = thread.join();
                Err(anyhow!(msg))
            }
            Err(_) => Err(anyhow!("audio capture thread ended unexpectedly")),
        }
    }

    pub fn peak(&self) -> f32 {
        load_f32(&self.shared.peak)
    }

    pub fn elapsed_s(&self) -> f32 {
        self.shared.samples.load(Ordering::Relaxed) as f32 / self.sample_rate as f32
    }

    pub fn sample_rate(&self) -> u32 {
        self.sample_rate
    }

    pub fn device_name(&self) -> &str {
        &self.device_name
    }

    pub fn error(&self) -> Option<String> {
        lock(&self.shared.error).clone()
    }

    fn halt(&mut self) {
        if let Some(tx) = self.stop.take() {
            let _ = tx.send(());
        }
        if let Some(t) = self.thread.take() {
            let _ = t.join();
        }
    }

    /// Stops and writes a mono 16-bit WAV at the device rate.
    pub fn stop(mut self, path: &Path) -> anyhow::Result<RecInfo> {
        self.halt();
        let samples = std::mem::take(&mut *lock(&self.shared.buffer));
        if samples.is_empty() {
            return Err(match self.error() {
                Some(e) => anyhow!("nothing was recorded: {e}"),
                None => anyhow!("nothing was recorded: the microphone delivered no audio"),
            });
        }
        if let Some(dir) = path.parent().filter(|d| !d.as_os_str().is_empty()) {
            std::fs::create_dir_all(dir).with_context(|| format!("cannot create {}", dir.display()))?;
        }
        write_wav(path, &samples, self.sample_rate)?;
        let peak = samples.iter().fold(0.0f32, |m, s| m.max(s.abs())).min(1.0);
        Ok(RecInfo { path: path.to_path_buf(), duration_s: samples.len() as f32 / self.sample_rate as f32, peak })
    }
}

impl Drop for Recorder {
    fn drop(&mut self) {
        self.halt();
    }
}

/// Writes mono 16-bit PCM.
pub fn write_wav(path: &Path, samples: &[f32], sample_rate: u32) -> anyhow::Result<()> {
    let spec =
        hound::WavSpec { channels: 1, sample_rate, bits_per_sample: 16, sample_format: hound::SampleFormat::Int };
    let mut w = hound::WavWriter::create(path, spec).with_context(|| format!("cannot write {}", path.display()))?;
    for s in samples {
        w.write_sample((s.clamp(-1.0, 1.0) * 32767.0).round() as i16)?;
    }
    w.finalize()?;
    Ok(())
}

/// Reads any PCM/float WAV as mono f32 (channels averaged). Returns the samples and the file's sample rate.
pub fn read_wav_mono(path: &Path) -> anyhow::Result<(Vec<f32>, u32)> {
    let mut reader = hound::WavReader::open(path).with_context(|| format!("cannot open {}", path.display()))?;
    let spec = reader.spec();
    let channels = spec.channels.max(1) as usize;
    let interleaved: Vec<f32> = match spec.sample_format {
        hound::SampleFormat::Float => reader.samples::<f32>().collect::<Result<_, _>>()?,
        hound::SampleFormat::Int => {
            let scale = 1.0 / (1u64 << (spec.bits_per_sample.clamp(1, 32) - 1)) as f32;
            reader.samples::<i32>().map(|s| s.map(|v| v as f32 * scale)).collect::<Result<_, _>>()?
        }
    };
    let mono = if channels == 1 {
        interleaved
    } else {
        interleaved.chunks(channels).map(|f| f.iter().sum::<f32>() / f.len() as f32).collect()
    };
    Ok((mono, spec.sample_rate))
}

pub fn resample_linear(input: &[f32], from: u32, to: u32) -> Vec<f32> {
    if from == to || input.is_empty() {
        return input.to_vec();
    }
    let ratio = from as f64 / to as f64;
    let out_len = ((input.len() as f64) / ratio).round().max(1.0) as usize;
    let last = input.len() - 1;
    (0..out_len)
        .map(|i| {
            let pos = i as f64 * ratio;
            let idx = (pos.floor() as usize).min(last);
            let frac = (pos - idx as f64) as f32;
            let a = input[idx];
            let b = input[(idx + 1).min(last)];
            a + (b - a) * frac
        })
        .collect()
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum State {
    Idle,
    Playing,
    Paused,
    Ended,
}

struct Track {
    id: u64,
    samples: Vec<f32>,
    state: State,
    /// Still growing: reaching the end waits for more instead of ending.
    live: bool,
    looped: bool,
    speed: f32,
    stretch: Stretcher,
    /// Counts the jumps of the position (every `stretch` reset).
    resets: u64,
}

impl Track {
    fn restart(&mut self, pos: f64) {
        self.stretch.reset(pos);
        self.resets += 1;
    }
}

struct PlayShared {
    track: Mutex<Track>,
    next_id: AtomicU64,
    error: Mutex<Option<String>>,
}

pub struct Player {
    shared: Arc<PlayShared>,
    sample_rate: u32,
    device_name: String,
    stop: Option<Sender<()>>,
    thread: Option<JoinHandle<()>>,
}

/// Returns the frames filled from the track and whether it reached its end (not looping) this call.
fn render(t: &mut Track, out: &mut [f32]) -> (usize, bool) {
    let mut filled = 0;
    if t.state == State::Playing {
        loop {
            let (n, flow) = t.stretch.process(&t.samples, t.speed, !t.live, &mut out[filled..]);
            filled += n;
            match flow {
                Flow::Full | Flow::Starved => break,
                Flow::End if t.looped && !t.samples.is_empty() => t.restart(0.0),
                Flow::End => {
                    t.state = State::Ended;
                    out[filled..].iter_mut().for_each(|v| *v = 0.0);
                    return (filled, true);
                }
            }
        }
    }
    out[filled..].iter_mut().for_each(|v| *v = 0.0);
    (filled, false)
}

/// The position reports, `PROGRESS_HZ` times a second of sound. The stretcher's position advances in uneven
/// blocks, so they count the samples played × speed and resync to it after a jump, or when the two drift more than
/// `SNAP_S` apart.
struct Reports {
    rate: f64,
    every: usize,
    since: usize,
    last_pos: f64,
    smooth: f64,
    resets: u64,
    jumped: bool,
}

impl Reports {
    const SNAP_S: f64 = 0.12;

    fn new(rate: u32) -> Self {
        let every = (rate / PROGRESS_HZ).max(1) as usize;
        Reports {
            rate: f64::from(rate),
            every,
            since: 0,
            last_pos: f64::NAN,
            smooth: 0.0,
            resets: u64::MAX,
            jumped: false,
        }
    }

    /// One block of `frames`, `filled` of them from the track (a live track waiting for its next part fills none);
    /// `raw` is the stretcher's position and `len` the track's length, in samples. Returns a report when one is due:
    /// the position in seconds and whether it jumped.
    fn played(
        &mut self,
        frames: usize,
        filled: usize,
        speed: f32,
        raw: f64,
        resets: u64,
        len: f64,
    ) -> Option<(f32, bool)> {
        self.since += frames;
        self.smooth += filled as f64 * f64::from(speed);
        if self.resets != resets {
            (self.smooth, self.resets, self.jumped) = (raw, resets, true);
        } else if (self.smooth - raw).abs() > Self::SNAP_S * self.rate {
            self.smooth = raw;
        }
        if self.since < self.every {
            return None;
        }
        self.since %= self.every;
        let pos = self.smooth.clamp(0.0, len);
        if pos == self.last_pos && !self.jumped {
            return None;
        }
        self.last_pos = pos;
        Some(((pos / self.rate) as f32, std::mem::take(&mut self.jumped)))
    }

    fn ended(&mut self) {
        (self.since, self.last_pos) = (0, f64::NAN);
    }
}

fn build_output<T>(
    device: &cpal::Device,
    config: &cpal::StreamConfig,
    shared: Arc<PlayShared>,
    sink: Option<PlaySink>,
) -> Result<cpal::Stream, cpal::BuildStreamError>
where
    T: SizedSample + FromSample<f32>,
{
    let channels = config.channels.max(1) as usize;
    let rate = config.sample_rate.0.max(1);
    let err_shared = shared.clone();
    let mut mono: Vec<f32> = Vec::new();
    let mut reports = Reports::new(rate);
    device.build_output_stream(
        config,
        move |data: &mut [T], _| {
            let frames = data.len() / channels;
            if mono.len() < frames {
                mono.resize(frames, 0.0);
            }
            let mut report = None;
            let mut ended = None;
            {
                let mut t = lock(&shared.track);
                let playing = t.state == State::Playing;
                let (filled, end) = render(&mut t, &mut mono[..frames]);
                if end {
                    ended = Some(t.id);
                    reports.ended();
                } else if playing {
                    let len = t.samples.len() as f64;
                    let raw = t.stretch.position().min(len);
                    report = reports
                        .played(frames, filled, t.speed, raw, t.resets, len)
                        .map(|(pos_s, jumped)| (t.id, pos_s, (len / f64::from(rate)) as f32, jumped));
                }
            }
            for (frame, s) in data.chunks_mut(channels).zip(&mono[..frames]) {
                let v = <T as FromSample<f32>>::from_sample_(*s);
                for o in frame {
                    *o = v;
                }
            }
            if let Some(sink) = &sink {
                if let Some((track, pos_s, len_s, jumped)) = report {
                    sink(PlayEvent::Progress { track, pos_s, len_s, jumped });
                }
                if let Some(track) = ended {
                    sink(PlayEvent::Ended { track });
                }
            }
        },
        move |err| {
            *lock(&err_shared.error) = Some(err.to_string());
        },
        None,
    )
}

impl Player {
    pub fn new(sink: Option<PlaySink>) -> anyhow::Result<Self> {
        let shared = Arc::new(PlayShared {
            // The real rate is only known once the device is open; the stretcher is rebuilt for it below.
            track: Mutex::new(Track {
                id: 0,
                samples: Vec::new(),
                state: State::Idle,
                live: false,
                looped: false,
                speed: 1.0,
                stretch: Stretcher::new(48_000),
                resets: 0,
            }),
            next_id: AtomicU64::new(1),
            error: Mutex::new(None),
        });
        let (init_tx, init_rx) = mpsc::channel::<Result<(u32, String), String>>();
        let (stop_tx, stop_rx) = mpsc::channel::<()>();
        let thread_shared = shared.clone();

        let thread = thread::Builder::new()
            .name("audio-playback".into())
            .spawn(move || {
                let setup = (|| -> anyhow::Result<(cpal::Stream, u32, String)> {
                    let dev = cpal::default_host()
                        .default_output_device()
                        .ok_or_else(|| anyhow!("no audio output device found: check the system sound settings"))?;
                    let name = dev.name().unwrap_or_else(|_| "unknown device".into());
                    let supported = dev
                        .default_output_config()
                        .map_err(|e| anyhow!("cannot open the audio output \"{name}\": {e}"))?;
                    let rate = supported.sample_rate().0;
                    lock(&thread_shared.track).stretch = Stretcher::new(rate);
                    let format = supported.sample_format();
                    let config: cpal::StreamConfig = supported.into();
                    let stream = match format {
                        SampleFormat::F32 => build_output::<f32>(&dev, &config, thread_shared, sink),
                        SampleFormat::I16 => build_output::<i16>(&dev, &config, thread_shared, sink),
                        SampleFormat::U16 => build_output::<u16>(&dev, &config, thread_shared, sink),
                        SampleFormat::I32 => build_output::<i32>(&dev, &config, thread_shared, sink),
                        SampleFormat::I8 => build_output::<i8>(&dev, &config, thread_shared, sink),
                        SampleFormat::U8 => build_output::<u8>(&dev, &config, thread_shared, sink),
                        SampleFormat::F64 => build_output::<f64>(&dev, &config, thread_shared, sink),
                        other => return Err(anyhow!("unsupported output sample format {other:?}")),
                    }
                    .map_err(|e| anyhow!("cannot start playback on \"{name}\": {e}"))?;
                    stream.play().map_err(|e| anyhow!("cannot start playback on \"{name}\": {e}"))?;
                    Ok((stream, rate, name))
                })();
                match setup {
                    Ok((stream, rate, name)) => {
                        let _ = init_tx.send(Ok((rate, name)));
                        let _ = stop_rx.recv();
                        drop(stream);
                    }
                    Err(e) => {
                        let _ = init_tx.send(Err(format!("{e:#}")));
                    }
                }
            })
            .context("cannot start the audio playback thread")?;

        match init_rx.recv() {
            Ok(Ok((sample_rate, device_name))) => {
                Ok(Self { shared, sample_rate, device_name, stop: Some(stop_tx), thread: Some(thread) })
            }
            Ok(Err(msg)) => {
                let _ = thread.join();
                Err(anyhow!(msg))
            }
            Err(_) => Err(anyhow!("audio playback thread ended unexpectedly")),
        }
    }

    /// Returns the new track's id (for [`PlayEvent`]s) and length in seconds. `live`: more audio will be appended with
    /// [`Self::enqueue`] until [`Self::set_live`]`(false)`.
    pub fn play(&self, path: &Path, live: bool) -> anyhow::Result<(u64, f32)> {
        let samples = self.load(path)?;
        let len_s = samples.len() as f32 / self.sample_rate as f32;
        let id = self.shared.next_id.fetch_add(1, Ordering::Relaxed);
        let old = {
            let mut t = lock(&self.shared.track);
            t.id = id;
            t.state = State::Playing;
            t.live = live;
            t.restart(0.0);
            std::mem::replace(&mut t.samples, samples)
        };
        drop(old); // freed outside the lock
        Ok((id, len_s))
    }

    /// Returns the track's new length in seconds.
    pub fn enqueue(&self, path: &Path) -> anyhow::Result<f32> {
        let samples = self.load(path)?;
        let mut t = lock(&self.shared.track);
        t.samples.extend_from_slice(&samples);
        if t.state == State::Idle || t.state == State::Ended {
            t.state = State::Playing;
        }
        Ok(t.samples.len() as f32 / self.sample_rate as f32)
    }

    pub fn set_live(&self, live: bool) {
        lock(&self.shared.track).live = live;
    }

    fn load(&self, path: &Path) -> anyhow::Result<Vec<f32>> {
        let (mono, rate) = read_wav_mono(path)?;
        let out = resample_linear(&mono, rate, self.sample_rate);
        if out.is_empty() {
            return Err(anyhow!("{} contains no audio", path.display()));
        }
        Ok(out)
    }

    pub fn stop(&self) {
        let old = {
            let mut t = lock(&self.shared.track);
            t.state = State::Idle;
            t.live = false;
            t.restart(0.0);
            std::mem::take(&mut t.samples)
        };
        drop(old);
    }

    pub fn pause(&self) {
        let mut t = lock(&self.shared.track);
        if t.state == State::Playing {
            t.state = State::Paused;
        }
    }

    /// Plays an ended track again from the start. False when nothing is loaded.
    pub fn resume(&self) -> bool {
        let mut t = lock(&self.shared.track);
        match t.state {
            State::Paused => t.state = State::Playing,
            State::Ended => {
                t.restart(0.0);
                t.state = State::Playing;
            }
            State::Playing => {}
            State::Idle => return false,
        }
        true
    }

    /// Clamped to the track; an ended track stays ended until [`Self::resume`].
    pub fn seek(&self, pos_s: f32) {
        let mut t = lock(&self.shared.track);
        let pos = (f64::from(pos_s.max(0.0)) * self.sample_rate as f64).min(t.samples.len() as f64);
        t.restart(pos);
        if t.state == State::Ended {
            t.state = State::Paused;
        }
    }

    pub fn set_loop(&self, on: bool) {
        lock(&self.shared.track).looped = on;
    }

    /// Pitch kept; 0.25..4.
    pub fn set_speed(&self, speed: f32) {
        lock(&self.shared.track).speed = speed.clamp(0.25, 4.0);
    }

    pub fn is_playing(&self) -> bool {
        lock(&self.shared.track).state == State::Playing
    }

    pub fn position_s(&self) -> f32 {
        let t = lock(&self.shared.track);
        (t.stretch.position().min(t.samples.len() as f64) / self.sample_rate as f64) as f32
    }

    /// For a live track, the length of what has arrived.
    pub fn duration_s(&self) -> f32 {
        lock(&self.shared.track).samples.len() as f32 / self.sample_rate as f32
    }

    pub fn sample_rate(&self) -> u32 {
        self.sample_rate
    }

    pub fn device_name(&self) -> &str {
        &self.device_name
    }

    pub fn error(&self) -> Option<String> {
        lock(&self.shared.error).clone()
    }
}

impl Drop for Player {
    fn drop(&mut self) {
        if let Some(tx) = self.stop.take() {
            let _ = tx.send(());
        }
        if let Some(t) = self.thread.take() {
            let _ = t.join();
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    const RATE: u32 = 48_000;
    const BLOCK: usize = 480;

    #[test]
    fn reports_come_thirty_times_a_second_of_sound() {
        let mut r = Reports::new(RATE);
        let mut raw = 0.0;
        let mut seen = Vec::new();
        for _ in 0..100 {
            raw += BLOCK as f64;
            seen.extend(r.played(BLOCK, BLOCK, 1.0, raw, 1, 1e9));
        }
        assert_eq!(seen.len(), 30);
        assert!(seen[0].1 && seen[1..].iter().all(|(_, jumped)| !jumped));
        assert!(seen.windows(2).all(|w| w[1].0 > w[0].0));
    }

    #[test]
    fn a_live_track_waiting_for_its_next_part_stays_put() {
        let mut r = Reports::new(RATE);
        let len = f64::from(RATE);
        let mut raw = 0.0;
        let mut last = None;
        while raw < len {
            raw += BLOCK as f64;
            if let Some((pos, _)) = r.played(BLOCK, BLOCK, 1.0, raw, 1, len) {
                last = Some(pos);
            }
        }
        for _ in 0..100 {
            assert_eq!(r.played(BLOCK, 0, 1.0, len, 1, len), None);
        }
        assert_eq!(last, Some(1.0));
    }

    #[test]
    fn a_seek_or_a_loop_is_reported_as_a_jump() {
        let mut r = Reports::new(RATE);
        let mut raw = 0.0;
        for _ in 0..50 {
            raw += BLOCK as f64;
            r.played(BLOCK, BLOCK, 1.0, raw, 1, 1e9);
        }
        raw = 1000.0;
        let mut seen = Vec::new();
        for _ in 0..8 {
            seen.extend(r.played(BLOCK, BLOCK, 1.0, raw, 2, 1e9));
            raw += BLOCK as f64;
        }
        assert!(seen[0].1, "{seen:?}");
        assert!(seen[0].0 < 0.1);
    }
}