Skip to main content

AudioSpec

Struct AudioSpec 

Source
pub struct AudioSpec {
    pub sample_rate: u32,
    pub channels: u16,
}
Expand description

The sample rate and channel count of a block of audio.

Rate and layout are one value, not two, so they cannot be passed separately and cannot drift apart. decibri has been bitten repeatedly by a rate travelling independently of the samples it describes; the most expensive instance was the AEC reference path, where a reference at the wrong rate is accepted without complaint and cancels nothing.

Fields§

§sample_rate: u32

Samples per second, per channel.

§channels: u16

Number of interleaved channels.

Implementations§

Source§

impl AudioSpec

Source

pub const fn new(sample_rate: u32, channels: u16) -> Self

Describes audio at sample_rate with channels interleaved channels.

Examples found in repository?
examples/decode_stream.rs (line 139)
131fn demo_wav() -> Result<Vec<u8>, DecodeError> {
132    const FRAMES: usize = 4_800;
133    let mut samples = Vec::with_capacity(FRAMES * 2);
134    for frame in 0..FRAMES {
135        let value = ((frame * 41) % 1_024) as f32 / 1_024.0 - 0.5;
136        samples.push(value); // left
137        samples.push(value * 0.25); // right
138    }
139    WavWriter::new(AudioSpec::new(48_000, 2), WavCodec::Float32).to_bytes(&samples)
140}
More examples
Hide additional examples
examples/decode_whole_file.rs (line 104)
96fn demo_wav() -> Result<Vec<u8>, DecodeError> {
97    const FRAMES: usize = 4_410;
98    let mut samples = Vec::with_capacity(FRAMES * 2);
99    for frame in 0..FRAMES {
100        let value = ((frame * 37) % 2_000) as f32 / 2_048.0 - 0.488_281_25;
101        samples.push(value); // left
102        samples.push(-value * 0.5); // right
103    }
104    WavWriter::new(AudioSpec::new(44_100, 2), WavCodec::PcmI16).to_bytes(&samples)
105}
Source

pub const fn mono(sample_rate: u32) -> Self

Describes single-channel audio at sample_rate.

Named because single-channel audio is stated by the caller often enough to be worth one call rather than two arguments: a headerless G.711 or PCM stream, or a mono file being written. Nothing in the crate produces this shape on its own. A decode keeps the source’s own channel count, and mixing down to one channel is downmix_to_mono, called by whoever wants it.

Examples found in repository?
examples/decode_stream.rs (line 120)
108    fn finish(mut self) -> Result<AudioBuffer, DecodeError> {
109        self.batch.clear();
110        self.decoder.finish(&mut self.batch)?;
111        if let Some(spec) = self.decoder.spec() {
112            if !self.batch.is_empty() {
113                self.convert(spec)?;
114            }
115        }
116        if let Some(resampler) = self.resampler.as_mut() {
117            resampler.flush(&mut self.samples);
118        }
119        Ok(AudioBuffer::from_samples(
120            AudioSpec::mono(self.target_rate),
121            self.samples,
122        ))
123    }
More examples
Hide additional examples
examples/decode_whole_file.rs (line 58)
36fn decode_to_mono_at(bytes: &[u8], target_rate: u32) -> Result<AudioBuffer, DecodeError> {
37    // 1. Container to interleaved samples, at the file's own rate and layout.
38    //    The rate travels with the samples from here on; nothing downstream
39    //    infers it.
40    let decoded = decode(bytes)?;
41
42    // 2. Interleaved to mono. The channel count comes from the buffer itself,
43    //    and the downmix is the arithmetic mean of the channels, the same
44    //    formula decibri uses, read from its source rather than chosen.
45    let mut mono = Vec::new();
46    downmix_to_mono(decoded.samples(), decoded.channels(), &mut mono);
47
48    // 3. The file's rate to the declared target rate, through the one
49    //    resampler this crate defers all rate conversion to. When the rates
50    //    already match, the resampler is an identity passthrough and the
51    //    samples come back bit-identical with nothing appended at the flush.
52    let mut resampler = PolyphaseResampler::new(decoded.sample_rate(), target_rate)?;
53    let mut samples = Vec::new();
54    resampler.process(&mono, &mut samples)?;
55    resampler.flush(&mut samples);
56
57    Ok(AudioBuffer::from_samples(
58        AudioSpec::mono(target_rate),
59        samples,
60    ))
61}
Source

pub const fn is_mono(&self) -> bool

true when this describes single-channel audio.

Source

pub const fn frames(&self, sample_count: usize) -> usize

How many whole frames sample_count interleaved samples make up.

Returns 0 for a spec with no channels rather than dividing by zero.

Trait Implementations§

Source§

impl Clone for AudioSpec

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for AudioSpec

Source§

impl Debug for AudioSpec

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for AudioSpec

Source§

impl Hash for AudioSpec

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for AudioSpec

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for AudioSpec

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.