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: u32Samples per second, per channel.
channels: u16Number of interleaved channels.
Implementations§
Source§impl AudioSpec
impl AudioSpec
Sourcepub const fn new(sample_rate: u32, channels: u16) -> Self
pub const fn new(sample_rate: u32, channels: u16) -> Self
Describes audio at sample_rate with channels interleaved channels.
Examples found in repository?
More examples
Sourcepub const fn mono(sample_rate: u32) -> Self
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?
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
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}