Skip to main content

AudioMixer

Struct AudioMixer 

Source
pub struct AudioMixer {
    pub sample_rate: u32,
    pub channels: u16,
    /* private fields */
}
Expand description

Multi-track, constant-power-panned stereo mixer.

Combines N mono tracks into a single interleaved stereo f32 output at 48 kHz. Per-track volume and pan adjustments take effect on the next call to mix.

§Pan law

For a pan position p ∈ [-1.0, +1.0]:

p_norm = (p + 1.0) * π / 4
l_gain = volume * cos(p_norm)
r_gain = volume * sin(p_norm)

At p = 0 (center): l_gain == r_gain ≈ 0.707 × volume (constant-power law — equal loudness in both ears).

§Example

let mut mixer = AudioMixer::new(48_000);
let track = mixer.add_track();

// Background audio-decode thread:
track.push_samples(&mono_pcm_chunk);

// Audio-device output callback:
let stereo = mixer.mix(output_buf.len());
output_buf[..stereo.len()].copy_from_slice(&stereo);

Fields§

§sample_rate: u32

Output sample rate in Hz.

§channels: u16

Number of output channels — always 2 (stereo).

Implementations§

Source§

impl AudioMixer

Source

pub fn new(sample_rate: u32) -> Self

Create a new mixer with no tracks.

Source

pub fn add_track(&mut self) -> AudioTrackHandle

Add a new mono track and return a cloneable handle.

The track starts with volume = 1.0 and pan = 0.0 (center).

Source

pub fn mix(&mut self, n_samples: usize) -> Vec<f32>

Mix n_samples interleaved stereo f32 values from all tracks.

n_samples is the total number of f32 elements to produce (L + R interleaved). Tracks with insufficient buffered data are zero-padded. The output is clipped to [-1.0, 1.0].

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.