From 7bd9526177affde966d626060789989443d5fb30 Mon Sep 17 00:00:00 2001 From: Filip Kojro Date: Fri, 14 Aug 2026 16:38:54 +0200 Subject: [PATCH] change to ringbuf --- Cargo.lock | 33 +++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 30 ++++++++++++++++++++++++------ 3 files changed, 58 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e5f8fef..d8ed646 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -117,6 +117,12 @@ dependencies = [ "windows-core", ] +[[package]] +name = "crossbeam-utils" +version = "0.8.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61803da095bee82a81bb1a452ecc25d3b2f1416d1897eb86430c6159ef717c17" + [[package]] name = "dasp_sample" version = "0.11.0" @@ -451,6 +457,21 @@ version = "0.3.33" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "19f132c84eca552bf34cab8ec81f1c1dcc229b811638f9d283dceabe58c5569e" +[[package]] +name = "portable-atomic" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05c8b63e8d9609db387f0324918f81d68fe27748f084ef092fb35954d0539a85" + +[[package]] +name = "portable-atomic-util" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c2a106d1259c23fac8e543272398ae0e3c0b8d33c88ed73d0cc71b0f1d902618" +dependencies = [ + "portable-atomic", +] + [[package]] name = "proc-macro-crate" version = "3.5.0" @@ -478,6 +499,17 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "ringbuf" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a158e09ede21a14b172ca6cdd6208386c6ae2cb6acef58d774368ef8c450dfa7" +dependencies = [ + "crossbeam-utils", + "portable-atomic", + "portable-atomic-util", +] + [[package]] name = "rustc_version" version = "0.4.1" @@ -555,6 +587,7 @@ name = "smalltalk" version = "0.1.0" dependencies = [ "cpal", + "ringbuf", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index cb8ef57..111bbbf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,3 +5,4 @@ edition = "2024" [dependencies] cpal = "0.18.1" +ringbuf = "0.5.1" diff --git a/src/main.rs b/src/main.rs index 0f5c732..a081749 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,8 @@ use cpal::traits::{DeviceTrait, HostTrait, StreamTrait}; +use ringbuf::{ + HeapRb, + traits::{Consumer, Producer, Split}, +}; use std::sync::mpsc; fn main() { @@ -24,25 +28,39 @@ fn main() { let latency_ms = 150.0; let latency_frames = (latency_ms / 1000.0) * config.sample_rate as f32; let latency_samples = latency_frames as usize * config.channels as usize; - let (tx, rx) = mpsc::sync_channel::(latency_samples * 2); + let rb = HeapRb::::new(latency_samples * 2); + let (mut prod, mut cons) = rb.split(); for _ in 0..latency_samples { - tx.send(0.0).unwrap(); + prod.try_push(0.0).unwrap(); } let input_data_fn = move |data: &[f32], _: &cpal::InputCallbackInfo| { + let mut output_behind = false; for &sample in data { - if tx.try_send(sample).is_err() {} + if prod.try_push(sample).is_err() { + output_behind = true; + } + } + if output_behind { + eprintln!("output behind"); } }; let output_data_fn = move |data: &mut [f32], _: &cpal::OutputCallbackInfo| { + let mut input_behind = false; for sample in data { - *sample = match rx.try_recv() { - Ok(s) => s, - Err(_) => 0.0, + *sample = match cons.try_pop() { + Some(s) => s, + None => { + input_behind = true; + 0.0 + } }; } + if input_behind { + eprintln!("input behind") + } }; let err_fn = |err| eprintln!("stream error: {}", err);