Skip to content

XP event5x XPListening, games, ratings, comments and chat earn five times the XP

leftLeaderboard

juicewrld-desktopsrc-taurihostaudio.rs

1.4.2Released Nov 21, 2026

audio.rs

517 lines19.0 KBRust
//! Each instance's audio thread: the one thread that calls process() and setProcessing(), as VST 3 wants. It serves the//! instance's ring (a stream from the page's AudioWorklet, see src/plugins/ring.rs) and render requests (a block of//! audio from the app, answered with the processed block: offline, no deadline), never both at once.//!//! A real-time stream is polled every half millisecond by a high-resolution waitable timer, at MMCSS "Pro Audio"//! priority; an offline stream is polled flat out, since the page's OfflineAudioContext waits on every block.use crate::ring::{self, at, Layout, Ring};use crate::shim::{Plugin, Transport};use std::collections::VecDeque;use std::sync::atomic::{AtomicBool, AtomicU32, AtomicU64, Ordering};use std::sync::{Arc, Condvar, Mutex, OnceLock};use std::time::{Duration, Instant};use windows::core::w;use windows::Win32::Foundation::{CloseHandle, HANDLE};use windows::Win32::System::Memory::{UnmapViewOfFile, MEMORY_MAPPED_VIEW_ADDRESS};use windows::Win32::System::Threading::{    AvRevertMmThreadCharacteristics, AvSetMmThreadCharacteristicsW, AvSetMmThreadPriority, CreateWaitableTimerExW, SetWaitableTimer,    WaitForSingleObject, AVRT_PRIORITY_HIGH, CREATE_WAITABLE_TIMER_HIGH_RESOLUTION, INFINITE, TIMER_ALL_ACCESS,};/// A stream with no block for this long (the page's AudioContext suspended, playback stopped) gets the parameter/// changes waiting by a parameter flush instead of with the next block.const FLUSH_AFTER: Duration = Duration::from_millis(50);/// Milliseconds since the host started, never 0 (0 means "not in a plugin call").pub fn now_ms() -> u64 {    static START: OnceLock<Instant> = OnceLock::new();    START.get_or_init(Instant::now).elapsed().as_millis() as u64 + 1}thread_local! {    /// The instance whose plugin this thread is calling into right now, for the crash report.    pub static CURRENT: std::cell::Cell<u32> = const { std::cell::Cell::new(0) };}pub type Reply = Box<dyn FnOnce(Result<Vec<u8>, String>) + Send>;/// A mapped view of a ring's file mapping, unmapped (and the handle closed) when dropped.#[allow(dead_code)]pub struct View {    pub base: *mut u8,    pub len: usize,    pub mapping: HANDLE,}unsafe impl Send for View {}impl Drop for View {    fn drop(&mut self) {        // SAFETY: mapped and opened by the host for this view alone.        unsafe {            let _ = UnmapViewOfFile(MEMORY_MAPPED_VIEW_ADDRESS { Value: self.base as *mut _ });            let _ = CloseHandle(self.mapping);        }    }}pub struct Stream {    /// Kept for its Drop: the ring's memory stays mapped while the stream lives.    #[allow(dead_code)]    pub view: View,    pub ring: Ring,    pub layout: Layout,}pub enum Job {    /// Serve this stream; the plugin's latency (read on the main thread, as VST 3 wants) goes in its header.    Attach(Stream, u32),    Detach,    /// Planar stereo f32 in, the same out.    Render { input: Vec<u8>, frames: u32, reply: Reply },    /// Stop processing (setProcessing(false)) and say so, then wait for Resume: the main thread reactivates meanwhile.    Suspend(std::sync::mpsc::Sender<()>),    Resume,    /// The host's own bypass, for a plugin without a bypass parameter: the dry signal, delayed by the latency.    Bypass(bool),    Latency(u32),    /// Hand the processor the parameter changes waiting, by a parameter flush when no audio flows (and say so, when    /// asked: the main thread reads the plugin's state after it).    Flush(Option<std::sync::mpsc::Sender<()>>),    Quit,}/// What the main thread shares with an audio thread.pub struct Shared {    jobs: Mutex<VecDeque<Job>>,    cv: Condvar,    /// When the plugin call under way started (now_ms), or 0.    pub busy_since: AtomicU64,    /// Blocks processed, and the slowest one in microseconds, for the logs and the tests.    pub blocks: AtomicU64,    pub slowest_us: AtomicU32,    /// A Flush job is queued: parameter changes in quick succession ask for one, not one each.    flush_queued: AtomicBool,}impl Shared {    pub fn send(&self, job: Job) {        self.jobs.lock().unwrap_or_else(|e| e.into_inner()).push_back(job);        self.cv.notify_one();    }    /// A parameter changed (the page, or the plugin's own editor): the processor gets it with the next block, or by a    /// flush if no audio is flowing.    pub fn request_flush(&self) {        if !self.flush_queued.swap(true, Ordering::AcqRel) {            self.send(Job::Flush(None));        }    }}pub struct AudioThread {    pub shared: Arc<Shared>,    handle: Option<std::thread::JoinHandle<()>>,}impl AudioThread {    pub fn start(plugin: Plugin, inst: u32, max_block: u32, latency: u32) -> AudioThread {        let shared = Arc::new(Shared {            jobs: Mutex::new(VecDeque::new()),            cv: Condvar::new(),            busy_since: AtomicU64::new(0),            blocks: AtomicU64::new(0),            slowest_us: AtomicU32::new(0),            flush_queued: AtomicBool::new(false),        });        let s = shared.clone();        let handle = std::thread::Builder::new()            .name(format!("audio {inst}"))            .spawn(move || run(plugin, inst, max_block, latency, &s))            .ok();        AudioThread { shared, handle }    }    /// Asks the thread to stop and waits up to `wait` for it; false if it's stuck in the plugin.    pub fn stop(&mut self, wait: Duration) -> bool {        self.shared.send(Job::Quit);        let t0 = Instant::now();        while let Some(h) = &self.handle {            if h.is_finished() {                let _ = self.handle.take().map(|h| h.join());                return true;            }            if t0.elapsed() > wait {                return false;            }            std::thread::sleep(Duration::from_millis(2));        }        true    }    /// Stops processing for a reactivation; false if the thread didn't answer in time (stuck in process()).    pub fn suspend(&self, wait: Duration) -> bool {        let (tx, rx) = std::sync::mpsc::channel();        self.shared.send(Job::Suspend(tx));        rx.recv_timeout(wait).is_ok()    }    /// The processor has every parameter change made so far (with a block or a flush), within `wait`; false if the    /// thread didn't get to it (stuck in the plugin).    pub fn flush_now(&self, wait: Duration) -> bool {        let (tx, rx) = std::sync::mpsc::channel();        self.shared.send(Job::Flush(Some(tx)));        rx.recv_timeout(wait).is_ok()    }}/// A delay line the length of the plugin's latency, for the host's own bypass (none for a plugin without latency).struct Delay {    buf: [Vec<f32>; 2],    pos: usize,}impl Delay {    fn new(len: u32) -> Delay {        Delay { buf: [vec![0.0; len as usize], vec![0.0; len as usize]], pos: 0 }    }    /// Feeds the input through; when `out` is given the delayed input is written there.    fn run(&mut self, l: &[f32], r: &[f32], out: Option<(&mut [f32], &mut [f32])>) {        let len = self.buf[0].len();        if len == 0 {            if let Some((ol, or)) = out {                ol.copy_from_slice(l);                or.copy_from_slice(r);            }            return;        }        let mut out = out;        for i in 0..l.len() {            if let Some((ol, or)) = out.as_mut() {                ol[i] = self.buf[0][self.pos];                or[i] = self.buf[1][self.pos];            }            self.buf[0][self.pos] = l[i];            self.buf[1][self.pos] = r[i];            self.pos = (self.pos + 1) % len;        }    }}struct Timer(HANDLE);impl Timer {    fn new() -> Option<Timer> {        // SAFETY: plain Win32 calls.        unsafe { CreateWaitableTimerExW(None, None, CREATE_WAITABLE_TIMER_HIGH_RESOLUTION, TIMER_ALL_ACCESS.0).ok().map(Timer) }    }    fn sleep(&self, us: i64) {        let due = -(us * 10);        // SAFETY: our own timer handle.        unsafe {            if SetWaitableTimer(self.0, &due, 0, None, None, false).is_ok() {                WaitForSingleObject(self.0, INFINITE);            }        }    }}impl Drop for Timer {    fn drop(&mut self) {        // SAFETY: our own handle.        unsafe {            let _ = CloseHandle(self.0);        }    }}/// The thread's MMCSS "Pro Audio" registration, given back when the thread ends (each one holds a handle in the/// process and a place in the scheduler's list until then).struct ProAudio(Option<HANDLE>);impl ProAudio {    fn new() -> ProAudio {        let mut index = 0u32;        // SAFETY: plain Win32 calls; failure leaves the thread at normal priority.        unsafe {            match AvSetMmThreadCharacteristicsW(w!("Pro Audio"), &mut index) {                Ok(h) => {                    let _ = AvSetMmThreadPriority(h, AVRT_PRIORITY_HIGH);                    ProAudio(Some(h))                }                Err(_) => ProAudio(None),            }        }    }}impl Drop for ProAudio {    fn drop(&mut self) {        if let Some(h) = self.0.take() {            // SAFETY: the handle AvSetMmThreadCharacteristicsW gave this thread.            unsafe {                let _ = AvRevertMmThreadCharacteristics(h);            }        }    }}struct Worker<'a> {    plugin: Plugin,    inst: u32,    max_block: usize,    shared: &'a Shared,    processing: bool,    bypass: bool,    delay: Delay,    buf: [Vec<f32>; 4],}impl Worker<'_> {    fn start_processing(&mut self) {        if !self.processing {            self.call(|p| p.set_processing(true));            self.processing = true;        }    }    fn stop_processing(&mut self) {        if self.processing {            self.call(|p| p.set_processing(false));            self.processing = false;        }    }    /// The parameter changes waiting, to the processor without audio (a parameter flush): processing starts for it if    /// it hadn't (it stays on, as after a render, until a stream ends or the instance closes).    fn flush_params(&mut self) {        if self.plugin.params_pending() {            self.start_processing();            self.call(|p| p.flush());        }    }    /// A call into the plugin, timed for the watchdog and marked for the crash report.    fn call<T>(&self, f: impl FnOnce(&Plugin) -> T) -> T {        CURRENT.with(|c| c.set(self.inst));        self.shared.busy_since.store(now_ms(), Ordering::Release);        let r = f(&self.plugin);        self.shared.busy_since.store(0, Ordering::Release);        CURRENT.with(|c| c.set(0));        r    }    /// Processes `n` frames from buf[0..2] into buf[2..4]; returns the time it took in microseconds.    fn block(&mut self, n: usize, t: &Transport) -> u32 {        let [il, ir, ol, or] = &mut self.buf;        let t0 = Instant::now();        let plugin = self.plugin;        CURRENT.with(|c| c.set(self.inst));        self.shared.busy_since.store(now_ms(), Ordering::Release);        plugin.process(&il[..n], &ir[..n], &mut ol[..n], &mut or[..n], t);        self.shared.busy_since.store(0, Ordering::Release);        CURRENT.with(|c| c.set(0));        if self.bypass {            self.delay.run(&il[..n], &ir[..n], Some((&mut ol[..n], &mut or[..n])));        } else {            self.delay.run(&il[..n], &ir[..n], None);        }        let us = t0.elapsed().as_micros().min(u32::MAX as u128) as u32;        self.shared.blocks.fetch_add(1, Ordering::Relaxed);        self.shared.slowest_us.fetch_max(us, Ordering::Relaxed);        us    }    fn render(&mut self, input: &[u8], frames: u32) -> Result<Vec<u8>, String> {        let frames = frames as usize;        if input.len() != frames * 8 {            return Err("the audio isn't the stereo length it says".into());        }        let sample = |i: usize| f32::from_le_bytes([input[i * 4], input[i * 4 + 1], input[i * 4 + 2], input[i * 4 + 3]]);        let mut out = vec![0u8; frames * 8];        self.start_processing();        let t = Transport { tempo: 0.0, playing: 1, position: 0 };        let mut done = 0;        while done < frames {            let n = (frames - done).min(self.max_block);            for i in 0..n {                self.buf[0][i] = sample(done + i);                self.buf[1][i] = sample(frames + done + i);            }            self.block(n, &Transport { position: done as i64, ..t });            for i in 0..n {                out[(done + i) * 4..(done + i) * 4 + 4].copy_from_slice(&self.buf[2][i].to_le_bytes());                out[(frames + done + i) * 4..(frames + done + i) * 4 + 4].copy_from_slice(&self.buf[3][i].to_le_bytes());            }            done += n;        }        Ok(out)    }    /// Everything the stream has waiting; returns the frames processed.    fn serve(&mut self, s: &Stream) -> u32 {        let l = s.layout;        let mut total = 0;        loop {            let avail = s.ring.available();            if avail == 0 {                break;            }            // Fallen behind past what the ring holds (the input was overwritten): start again from the newest.            if avail > l.capacity - l.max_block {                s.ring.skip_to_newest(&l);                break;            }            self.start_processing();            let n = (avail as usize).min(self.max_block);            let pos = s.ring.get(at::IN_READ);            {                let [il, ir, _, _] = &mut self.buf;                s.ring.read_input(&l, pos, &mut il[..n], &mut ir[..n]);            }            let (tempo, playing, position) = s.ring.transport();            let us = self.block(n, &Transport { tempo, playing: playing as i32, position });            s.ring.write_output(&l, pos, &self.buf[2][..n], &self.buf[3][..n]);            s.ring.set(at::LAST_US, us);            if us > s.ring.get(at::MAX_US) {                s.ring.set(at::MAX_US, us);            }            total += n as u32;        }        total    }}fn run(plugin: Plugin, inst: u32, max_block: u32, latency: u32, shared: &Shared) {    let _pro_audio = ProAudio::new();    let timer = Timer::new();    let max = max_block.max(1) as usize;    let mut w = Worker {        plugin,        inst,        max_block: max,        shared,        processing: false,        bypass: false,        delay: Delay::new(latency),        buf: [vec![0.0; max], vec![0.0; max], vec![0.0; max], vec![0.0; max]],    };    let mut stream: Option<Stream> = None;    let mut suspended = false;    let mut quiet = 0u32;    // When the stream last brought a block: until FLUSH_AFTER without one, parameter changes go with the next block.    let mut last_block = Instant::now();    loop {        // Jobs first: without a stream to serve, wait for one.        let job = {            let mut q = shared.jobs.lock().unwrap_or_else(|e| e.into_inner());            if q.is_empty() && (stream.is_none() || suspended) {                q = shared.cv.wait_timeout(q, Duration::from_millis(250)).unwrap_or_else(|e| e.into_inner()).0;            }            q.pop_front()        };        if let Some(job) = job {            match job {                Job::Attach(s, latency) => {                    // Processing starts now, before the page sends its first block: setProcessing(true) can take a                    // while, and the first block is due back within the ring's latency like any other.                    if !suspended {                        w.start_processing();                    }                    last_block = Instant::now();                    s.ring.set(at::PLUGIN_LATENCY, latency);                    s.ring.set(at::STATE, ring::STATE_RUNNING);                    if let Some(old) = stream.replace(s) {                        old.ring.set(at::STATE, ring::STATE_STOPPED);                    }                }                Job::Detach => {                    if let Some(old) = stream.take() {                        old.ring.set(at::STATE, ring::STATE_STOPPED);                    }                    w.stop_processing();                }                Job::Render { input, frames, reply } => {                    let r = if stream.is_some() {                        Err("this instance is playing a stream; open another for offline rendering".into())                    } else if suspended {                        Err("the plugin is being reset".into())                    } else {                        w.render(&input, frames)                    };                    reply(r);                }                Job::Suspend(ack) => {                    w.stop_processing();                    suspended = true;                    let _ = ack.send(());                }                Job::Resume => suspended = false,                Job::Bypass(on) => w.bypass = on,                Job::Latency(samples) => {                    w.delay = Delay::new(samples);                    if let Some(s) = &stream {                        s.ring.set(at::PLUGIN_LATENCY, samples);                    }                }                Job::Flush(ack) => {                    shared.flush_queued.store(false, Ordering::Release);                    // A stream bringing blocks carries the changes with its next one; otherwise a flush now. (Asked for                    // by the main thread, before it reads the plugin's state: now, whatever flows.)                    let flowing = stream.is_some() && last_block.elapsed() < FLUSH_AFTER;                    if !suspended && (ack.is_some() || !flowing) {                        w.flush_params();                    }                    if let Some(ack) = ack {                        let _ = ack.send(());                    }                }                Job::Quit => {                    if let Some(old) = stream.take() {                        old.ring.set(at::STATE, ring::STATE_STOPPED);                    }                    w.stop_processing();                    return;                }            }            continue;        }        if suspended {            continue;        }        let Some(s) = &stream else { continue };        if s.ring.get(at::CLOSED) != 0 {            s.ring.set(at::STATE, ring::STATE_STOPPED);            stream = None;            w.stop_processing();            continue;        }        let done = w.serve(s);        s.ring.word(at::HOST_BEAT).fetch_add(1, Ordering::Relaxed);        if done > 0 {            quiet = 0;            last_block = Instant::now();            continue;        }        quiet = quiet.saturating_add(1);        // No blocks for a while (the page stopped or suspended its context): changes made meanwhile reach the processor        // now, so it plays (and saves) what the controller shows.        if w.plugin.params_pending() && last_block.elapsed() >= FLUSH_AFTER {            w.flush_params();        }        if s.layout.mode == ring::MODE_OFFLINE && quiet < 200_000 {            // An export waits on every block: answer as soon as the next one is in.            std::hint::spin_loop();            if quiet % 64 == 0 {                std::thread::yield_now();            }        } else {            match &timer {                Some(t) => t.sleep(500),                None => std::thread::sleep(Duration::from_millis(1)),            }        }    }}