const RATES: [u32; 13] = [
96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350,
];
pub fn sampling_frequency_index(rate: u32) -> u32 {
RATES.iter().position(|&r| r == rate).unwrap_or(4) as u32
}
pub fn audio_specific_config(sample_rate: u32, channels: u16) -> Vec<u8> {
let ch = channels.clamp(1, 7) as u32;
match RATES.iter().position(|&r| r == sample_rate) {
Some(index) => {
let bits = (2u32 << 11) | ((index as u32) << 7) | (ch << 3);
vec![(bits >> 8) as u8, (bits & 0xff) as u8]
}
None => {
let rate = u64::from(sample_rate.min((1 << 24) - 1));
let bits: u64 = (2u64 << 35) | (15u64 << 31) | (rate << 7) | (u64::from(ch) << 3);
bits.to_be_bytes()[3..].to_vec()
}
}
}
pub fn asc_sample_rate(asc: &[u8]) -> Option<u32> {
let bit = |i: usize| -> Option<u32> { Some(u32::from(asc.get(i / 8)? >> (7 - i % 8) & 1)) };
let read = |from: usize, n: usize| -> Option<u32> {
(from..from + n).try_fold(0u32, |v, i| Some(v << 1 | bit(i)?))
};
let mut pos = 5;
if read(0, 5)? == 31 {
pos += 6; }
match read(pos, 4)? {
15 => read(pos + 4, 24),
i => RATES.get(i as usize).copied(),
}
}
pub fn exact_frequency_index(rate: u32) -> Option<u32> {
RATES.iter().position(|&r| r == rate).map(|i| i as u32)
}
pub fn asc_sbr_rate(asc: &[u8]) -> Option<u32> {
let bit = |i: usize| -> Option<u32> { Some(u32::from(asc.get(i / 8)? >> (7 - i % 8) & 1)) };
let read = |from: usize, n: usize| -> Option<u32> {
(from..from + n).try_fold(0u32, |v, i| Some(v << 1 | bit(i)?))
};
let rate_at = |pos: usize| -> Option<(u32, usize)> {
match read(pos, 4)? {
15 => Some((read(pos + 4, 24)?, pos + 28)),
i => Some((*RATES.get(i as usize)?, pos + 4)),
}
};
let aot = read(0, 5)?;
let (_, mut pos) = rate_at(5)?;
if aot == 5 || aot == 29 {
pos += 4; return rate_at(pos).map(|(r, _)| r);
}
if aot != 2 {
return None;
}
pos += 4 + 3; if read(pos, 11)? == 0x2B7 && read(pos + 11, 5)? == 5 && read(pos + 16, 1)? == 1 {
return rate_at(pos + 17).map(|(r, _)| r);
}
None
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn every_rate_survives_the_asc() {
for rate in [7350, 44_100, 48_000, 88_200, 96_000, 192_000, 176_400] {
assert_eq!(
asc_sample_rate(&audio_specific_config(rate, 2)),
Some(rate),
"{rate}"
);
}
assert_eq!(audio_specific_config(48_000, 2), [0x11, 0x90]);
}
#[test]
fn sbr_rates_are_found_either_way() {
assert_eq!(
asc_sbr_rate(&[0x13, 0x90, 0x56, 0xE5, 0x80 | (4 << 3)]),
Some(44_100)
);
let bits: u32 = 5 << 27 | 6 << 23 | 2 << 19 | 3 << 15 | 2 << 10;
assert_eq!(asc_sbr_rate(&bits.to_be_bytes()), Some(48_000));
assert_eq!(asc_sbr_rate(&[0x11, 0x90]), None);
}
}