From 44138f9e3b30147ed63eca6c70863f1d91ca4102 Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Mon, 23 Nov 2026 03:50:43 +0000 Subject: [PATCH] docs: explain interleaved stream callback samples Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --- CHANGELOG.md | 1 + src/lib.rs | 17 +++++++++++++++++ src/traits.rs | 17 ++++++++++------- 3 files changed, 28 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 298d5c2ce5..c5ae918730 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **CoreAudio**: A `None` timeout now waits indefinitely for sample rate and format changes, instead of giving up after 1 or 2 seconds. - **Linux**: `realtime` can now promote threads without requiring `realtime-dbus`. - **PipeWire**: Set `node.rate` property so that `default.clock.allowed-rates` PipeWire config works. +- Document the interleaved sample layout in stream callbacks. ### Deprecated diff --git a/src/lib.rs b/src/lib.rs index 74ce58829a..9bcce5a6cb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -531,6 +531,23 @@ pub struct SupportedStreamConfig { /// A buffer of dynamically typed audio data, passed to raw stream callbacks. /// /// Raw input stream callbacks receive `&Data`, while raw output stream callbacks expect `&mut Data`. +/// +/// # Sample layout +/// +/// Samples are interleaved by frame: each frame contains one sample per channel, +/// and all samples of a frame appear before those of the next frame. Interleaving +/// does not specify which speaker or input corresponds to each channel index. +/// +/// Use the stream's channel count ([`StreamConfig::channels`]) as the chunk size +/// to iterate over frames in a sample slice. For example, a two-channel stream's +/// sample slice can be split like this: +/// +/// ``` +/// let samples = [0.1_f32, 0.2, 0.3, 0.4]; +/// let mut frames = samples.chunks_exact(2); +/// assert_eq!(frames.next(), Some(&[0.1, 0.2][..])); +/// assert_eq!(frames.next(), Some(&[0.3, 0.4][..])); +/// ``` #[derive(Debug)] pub struct Data { data: *mut (), diff --git a/src/traits.rs b/src/traits.rs index beeff09d29..2a7a1487ad 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -365,8 +365,9 @@ pub trait DeviceTrait: PartialEq + Eq + Hash + Debug + Display + Send + Sync { /// /// * `config` - The stream configuration including sample rate, channels, and buffer size. /// * `data_callback` - Called periodically with captured audio data. The callback receives - /// a slice of samples in the format `T` and timing information. It is real-time code: see - /// the [crate documentation](crate) for what you must not do in it. + /// a slice of [interleaved samples](Data#sample-layout) in the format `T` and timing + /// information. It is real-time code: see the [crate documentation](crate) for what you + /// must not do in it. /// * `error_callback` - Called when a stream error occurs (e.g., device disconnected). /// * `timeout` - Time to wait for the backend to initialize the stream. `None` waits /// indefinitely; `Some(duration)` limits how long to wait. Note: not all backends honor @@ -421,10 +422,11 @@ pub trait DeviceTrait: PartialEq + Eq + Hash + Debug + Display + Send + Sync { /// /// * `config` - The stream configuration including sample rate, channels, and buffer size. /// * `data_callback` - Called periodically to fill the output buffer. The callback receives - /// a mutable slice of samples in the format `T` to be filled with audio data, along with - /// timing information. The slice is pre-filled with silence, so a callback that writes - /// fewer samples than the slice holds leaves the remainder silent rather than stale. It is - /// real-time code: see the [crate documentation](crate) for what you must not do in it. + /// a mutable slice of [interleaved samples](Data#sample-layout) in the format `T` to be + /// filled with audio data, along with timing information. The slice is pre-filled with + /// silence, so a callback that writes fewer samples than the slice holds leaves the + /// remainder silent rather than stale. It is real-time code: see the + /// [crate documentation](crate) for what you must not do in it. /// * `error_callback` - Called when a stream error occurs (e.g., device disconnected). /// * `timeout` - Time to wait for the backend to initialize the stream. `None` waits /// indefinitely; `Some(duration)` limits how long to wait. Note: not all backends honor @@ -569,7 +571,8 @@ pub trait DeviceTrait: PartialEq + Eq + Hash + Debug + Display + Send + Sync { /// callback. /// /// cpal does not compensate for drift between the two directions, and does not mix or remap - /// channels between them. + /// channels between them. Each buffer contains [interleaved samples](Data#sample-layout) + /// grouped by its own channel count. /// /// # Parameters ///