pub const MAX_BYTES_PER_SAMPLE: usize = 8;
pub const I24_MIN: i32 = -8_388_608;
pub const I24_MAX: i32 = 8_388_607;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum SampleFormat {
U8,
I8,
I16Le,
I16Be,
I24Le,
I24Be,
I32Le,
I32Be,
F32Le,
F32Be,
F64Le,
F64Be,
}
impl SampleFormat {
pub const fn bytes_per_sample(self) -> usize {
match self {
Self::U8 | Self::I8 => 1,
Self::I16Le | Self::I16Be => 2,
Self::I24Le | Self::I24Be => 3,
Self::I32Le | Self::I32Be | Self::F32Le | Self::F32Be => 4,
Self::F64Le | Self::F64Be => 8,
}
}
pub const fn bits_per_sample(self) -> u16 {
(self.bytes_per_sample() * 8) as u16
}
pub const fn is_float(self) -> bool {
matches!(self, Self::F32Le | Self::F32Be | Self::F64Le | Self::F64Be)
}
pub fn decode(self, bytes: &[u8], output: &mut Vec<f32>) -> usize {
let width = self.bytes_per_sample();
let whole = bytes.len() / width;
output.reserve(whole);
for sample in bytes.chunks_exact(width) {
output.push(self.decode_sample(sample));
}
whole
}
pub fn encode(self, samples: &[f32], output: &mut Vec<u8>) -> usize {
let written = samples.len() * self.bytes_per_sample();
output.reserve(written);
for &sample in samples {
self.encode_sample(sample, output);
}
written
}
fn decode_sample(self, bytes: &[u8]) -> f32 {
debug_assert_eq!(bytes.len(), self.bytes_per_sample());
match self {
Self::U8 => u8_to_f32(bytes[0]),
Self::I8 => i8_to_f32(bytes[0] as i8),
Self::I16Le => i16_to_f32(i16::from_le_bytes([bytes[0], bytes[1]])),
Self::I16Be => i16_to_f32(i16::from_be_bytes([bytes[0], bytes[1]])),
Self::I24Le => i24_to_f32(i32::from_le_bytes([0, bytes[0], bytes[1], bytes[2]]) >> 8),
Self::I24Be => i24_to_f32(i32::from_be_bytes([bytes[0], bytes[1], bytes[2], 0]) >> 8),
Self::I32Le => i32_to_f32(i32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]])),
Self::I32Be => i32_to_f32(i32::from_be_bytes([bytes[0], bytes[1], bytes[2], bytes[3]])),
Self::F32Le => f32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]),
Self::F32Be => f32::from_be_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]),
Self::F64Le => narrow(f64::from_le_bytes([
bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7],
])),
Self::F64Be => narrow(f64::from_be_bytes([
bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7],
])),
}
}
fn encode_sample(self, sample: f32, output: &mut Vec<u8>) {
match self {
Self::U8 => output.push(f32_to_u8(sample)),
Self::I8 => output.push(f32_to_i8(sample) as u8),
Self::I16Le => output.extend_from_slice(&f32_to_i16(sample).to_le_bytes()),
Self::I16Be => output.extend_from_slice(&f32_to_i16(sample).to_be_bytes()),
Self::I24Le => output.extend_from_slice(&f32_to_i24(sample).to_le_bytes()[..3]),
Self::I24Be => output.extend_from_slice(&f32_to_i24(sample).to_be_bytes()[1..]),
Self::I32Le => output.extend_from_slice(&f32_to_i32(sample).to_le_bytes()),
Self::I32Be => output.extend_from_slice(&f32_to_i32(sample).to_be_bytes()),
Self::F32Le => output.extend_from_slice(&sample.to_le_bytes()),
Self::F32Be => output.extend_from_slice(&sample.to_be_bytes()),
Self::F64Le => output.extend_from_slice(&f64::from(sample).to_le_bytes()),
Self::F64Be => output.extend_from_slice(&f64::from(sample).to_be_bytes()),
}
}
}
fn narrow(sample: f64) -> f32 {
if sample.is_nan() {
0.0
} else {
sample as f32
}
}
pub(crate) fn quantize(sample: f32, scale: f32, min: i64, max: i64) -> i64 {
let clamped = sample.clamp(-1.0, 1.0);
((clamped * scale) as i64).clamp(min, max)
}
pub fn u8_to_f32(sample: u8) -> f32 {
(i16::from(sample) - 128) as f32 / 128.0
}
pub fn f32_to_u8(sample: f32) -> u8 {
(quantize(sample, 128.0, -128, 127) + 128) as u8
}
pub fn i8_to_f32(sample: i8) -> f32 {
f32::from(sample) / 128.0
}
pub fn f32_to_i8(sample: f32) -> i8 {
quantize(sample, 128.0, -128, 127) as i8
}
pub fn i16_to_f32(sample: i16) -> f32 {
f32::from(sample) / 32768.0
}
pub fn f32_to_i16(sample: f32) -> i16 {
quantize(sample, 32768.0, i16::MIN as i64, i16::MAX as i64) as i16
}
pub fn i24_to_f32(sample: i32) -> f32 {
sample as f32 / 8_388_608.0
}
pub fn f32_to_i24(sample: f32) -> i32 {
quantize(sample, 8_388_608.0, I24_MIN as i64, I24_MAX as i64) as i32
}
pub fn i32_to_f32(sample: i32) -> f32 {
sample as f32 / 2_147_483_648.0
}
pub fn f32_to_i32(sample: f32) -> i32 {
quantize(sample, 2_147_483_648.0, i32::MIN as i64, i32::MAX as i64) as i32
}
pub fn downmix_to_mono(samples: &[f32], channels: u16, output: &mut Vec<f32>) -> usize {
if channels <= 1 {
output.extend_from_slice(samples);
return samples.len();
}
let channels = usize::from(channels);
let frames = samples.len() / channels;
output.reserve(frames);
for frame in samples.chunks_exact(channels) {
output.push(frame.iter().sum::<f32>() / channels as f32);
}
frames
}
pub fn deinterleave(samples: &[f32], channels: u16) -> Vec<Vec<f32>> {
let channels = usize::from(channels);
if channels == 0 {
return Vec::new();
}
let frames = samples.len() / channels;
let mut planes = vec![Vec::with_capacity(frames); channels];
for frame in samples.chunks_exact(channels) {
for (plane, &sample) in planes.iter_mut().zip(frame) {
plane.push(sample);
}
}
planes
}
pub fn interleave<P: AsRef<[f32]>>(planes: &[P], output: &mut Vec<f32>) -> usize {
let frames = planes
.iter()
.map(|plane| plane.as_ref().len())
.min()
.unwrap_or(0);
let appended = frames * planes.len();
output.reserve(appended);
for frame in 0..frames {
for plane in planes {
output.push(plane.as_ref()[frame]);
}
}
appended
}
#[cfg(test)]
mod tests {
use super::*;
const ALL: [SampleFormat; 12] = [
SampleFormat::U8,
SampleFormat::I8,
SampleFormat::I16Le,
SampleFormat::I16Be,
SampleFormat::I24Le,
SampleFormat::I24Be,
SampleFormat::I32Le,
SampleFormat::I32Be,
SampleFormat::F32Le,
SampleFormat::F32Be,
SampleFormat::F64Le,
SampleFormat::F64Be,
];
const ENDIAN_PAIRS: [(SampleFormat, SampleFormat); 5] = [
(SampleFormat::I16Le, SampleFormat::I16Be),
(SampleFormat::I24Le, SampleFormat::I24Be),
(SampleFormat::I32Le, SampleFormat::I32Be),
(SampleFormat::F32Le, SampleFormat::F32Be),
(SampleFormat::F64Le, SampleFormat::F64Be),
];
fn decode_one(format: SampleFormat, bytes: &[u8]) -> f32 {
let mut out = Vec::new();
assert_eq!(format.decode(bytes, &mut out), 1);
out[0]
}
fn encode_one(format: SampleFormat, sample: f32) -> Vec<u8> {
let mut out = Vec::new();
format.encode(&[sample], &mut out);
out
}
#[test]
fn every_i16_value_round_trips_bit_identically() {
for raw in i16::MIN..=i16::MAX {
let as_float = i16_to_f32(raw);
assert_eq!(
f32_to_i16(as_float),
raw,
"{raw} did not survive the round trip (became {as_float})"
);
}
}
#[test]
fn i16_is_scaled_by_32768_not_32767() {
assert_eq!(i16_to_f32(i16::MIN), -1.0);
assert_eq!(i16_to_f32(i16::MAX), 32767.0 / 32768.0);
assert!(i16_to_f32(i16::MAX) < 1.0);
assert_eq!(i16_to_f32(0), 0.0);
assert_eq!(i16_to_f32(16_384), 0.5);
assert_eq!(f32_to_i16(0.5), 16_384);
}
#[test]
fn every_u8_value_round_trips_bit_identically() {
for raw in u8::MIN..=u8::MAX {
let as_float = u8_to_f32(raw);
assert_eq!(
f32_to_u8(as_float),
raw,
"{raw} did not survive the round trip (became {as_float})"
);
}
}
#[test]
fn u8_is_unsigned_and_offset_by_128() {
assert_eq!(u8_to_f32(0), -1.0);
assert_eq!(u8_to_f32(128), 0.0);
assert_eq!(u8_to_f32(255), 127.0 / 128.0);
assert_eq!(decode_one(SampleFormat::U8, &[0x80]), 0.0);
assert_eq!(encode_one(SampleFormat::U8, 0.0), [0x80]);
}
#[test]
fn every_i8_value_round_trips_bit_identically() {
for raw in i8::MIN..=i8::MAX {
let as_float = i8_to_f32(raw);
assert_eq!(
f32_to_i8(as_float),
raw,
"{raw} did not survive the round trip (became {as_float})"
);
}
}
#[test]
fn i8_is_signed_and_the_exact_inverse_of_u8() {
assert_eq!(i8_to_f32(0), 0.0);
assert_eq!(i8_to_f32(i8::MIN), -1.0);
assert_eq!(i8_to_f32(i8::MAX), 127.0 / 128.0);
assert_eq!(i8_to_f32(64), 0.5);
assert_eq!(decode_one(SampleFormat::I8, &[0x00]), 0.0);
assert_eq!(encode_one(SampleFormat::I8, 0.0), [0x00]);
assert_eq!(decode_one(SampleFormat::I8, &[0x80]), -1.0);
for byte in 0..=u8::MAX {
assert_eq!(
decode_one(SampleFormat::I8, &[byte]),
decode_one(SampleFormat::U8, &[byte ^ 0x80]),
"byte {byte:#04x}: I8 and U8 are not sign-bit twins"
);
}
}
#[test]
fn every_i24_value_round_trips_bit_identically() {
for raw in I24_MIN..=I24_MAX {
assert_eq!(f32_to_i24(i24_to_f32(raw)), raw, "{raw} did not round trip");
}
}
#[test]
fn i32_round_trips_exactly_within_the_f32_significand_and_lands_near_outside_it() {
for raw in [0, 1, -1, 1 << 20, -(1 << 20), (1 << 24) - 1, -(1 << 24)] {
assert_eq!(f32_to_i32(i32_to_f32(raw)), raw, "{raw} must be exact");
}
assert_eq!(i32_to_f32(i32::MIN), -1.0);
assert_eq!(f32_to_i32(-1.0), i32::MIN);
assert_eq!(i32_to_f32(i32::MAX), 1.0);
assert_eq!(f32_to_i32(1.0), i32::MAX);
const STRIDE: i64 = 65_537;
let mut raw = i32::MIN as i64;
while raw <= i32::MAX as i64 {
let value = raw as i32;
let recovered = f32_to_i32(i32_to_f32(value));
let ulp = (value.unsigned_abs().max(1 << 24) >> 23) as i64;
let error = (recovered as i64 - raw).abs();
assert!(
error <= ulp,
"{value} recovered as {recovered}, off by {error} (ulp {ulp})"
);
assert!(
recovered.signum() == value.signum() || value.unsigned_abs() <= 1,
"{value} changed sign to {recovered}"
);
raw += STRIDE;
}
}
#[test]
fn i24_sign_extension_is_correct_at_the_boundaries() {
assert_eq!(i24_to_f32(I24_MIN), -1.0);
assert_eq!(i24_to_f32(I24_MAX), 8_388_607.0 / 8_388_608.0);
assert_eq!(i24_to_f32(0), 0.0);
assert_eq!(decode_one(SampleFormat::I24Le, &[0x00, 0x00, 0x80]), -1.0);
assert_eq!(decode_one(SampleFormat::I24Be, &[0x80, 0x00, 0x00]), -1.0);
assert_eq!(
decode_one(SampleFormat::I24Le, &[0xff, 0xff, 0xff]),
-1.0 / 8_388_608.0
);
assert_eq!(
decode_one(SampleFormat::I24Be, &[0xff, 0xff, 0xff]),
-1.0 / 8_388_608.0
);
assert_eq!(
decode_one(SampleFormat::I24Le, &[0xff, 0xff, 0x7f]),
8_388_607.0 / 8_388_608.0
);
}
#[test]
fn a_float_beyond_full_scale_clamps_and_never_wraps() {
for over in [1.000_01_f32, 1.41, 2.0, 8.5, 1e9, f32::MAX, f32::INFINITY] {
assert_eq!(f32_to_u8(over), u8::MAX, "{over} must clamp in u8");
assert_eq!(f32_to_u8(-over), u8::MIN, "-{over} must clamp in u8");
assert_eq!(f32_to_i8(over), i8::MAX, "{over} must clamp in i8");
assert_eq!(f32_to_i8(-over), i8::MIN, "-{over} must clamp in i8");
assert_eq!(f32_to_i16(over), i16::MAX, "{over} must clamp in i16");
assert_eq!(f32_to_i16(-over), i16::MIN, "-{over} must clamp in i16");
assert_eq!(f32_to_i24(over), I24_MAX, "{over} must clamp in i24");
assert_eq!(f32_to_i24(-over), I24_MIN, "-{over} must clamp in i24");
assert_eq!(f32_to_i32(over), i32::MAX, "{over} must clamp in i32");
assert_eq!(f32_to_i32(-over), i32::MIN, "-{over} must clamp in i32");
assert_eq!(encode_one(SampleFormat::I16Le, over), [0xff, 0x7f]);
assert_eq!(encode_one(SampleFormat::I16Le, -over), [0x00, 0x80]);
assert_eq!(encode_one(SampleFormat::I24Be, over), [0x7f, 0xff, 0xff]);
assert_eq!(encode_one(SampleFormat::I24Be, -over), [0x80, 0x00, 0x00]);
}
}
#[test]
fn the_float_formats_pass_values_through_unclamped() {
for over in [1.41_f32, -2.0, 100.0] {
for format in [SampleFormat::F32Le, SampleFormat::F32Be] {
let bytes = encode_one(format, over);
assert_eq!(decode_one(format, &bytes), over);
}
for format in [SampleFormat::F64Le, SampleFormat::F64Be] {
let bytes = encode_one(format, over);
assert_eq!(decode_one(format, &bytes), over);
}
}
}
#[test]
fn nan_quantizes_to_silence() {
assert_eq!(f32_to_i16(f32::NAN), 0);
assert_eq!(f32_to_i32(f32::NAN), 0);
assert_eq!(f32_to_i24(f32::NAN), 0);
assert_eq!(f32_to_i8(f32::NAN), 0);
assert_eq!(f32_to_u8(f32::NAN), 128);
}
#[test]
fn an_f64_nan_narrows_to_silence_rather_than_to_an_unspecified_nan() {
for format in [SampleFormat::F64Le, SampleFormat::F64Be] {
for nan in [f64::NAN, -f64::NAN, f64::from_bits(0x7ff8_0000_dead_beef)] {
let bytes = match format {
SampleFormat::F64Le => nan.to_le_bytes(),
_ => nan.to_be_bytes(),
};
let decoded = decode_one(format, &bytes);
assert!(!decoded.is_nan(), "{format:?} passed a NaN through");
assert_eq!(decoded.to_bits(), 0.0_f32.to_bits(), "{format:?}");
}
}
let signalling = f64::from_bits(0x7ff0_0000_0000_0001);
assert!(signalling.is_nan());
assert_eq!(
decode_one(SampleFormat::F64Le, &signalling.to_le_bytes()),
0.0
);
assert_eq!(
decode_one(SampleFormat::F64Le, &f64::INFINITY.to_le_bytes()),
f32::INFINITY
);
let bits = 0x7fc0_dead_u32;
assert_eq!(
decode_one(SampleFormat::F32Le, &bits.to_le_bytes()).to_bits(),
bits
);
}
#[test]
fn a_halfway_value_truncates_toward_zero() {
assert_eq!(f32_to_i16(1.5 / 32768.0), 1);
assert_eq!(f32_to_i16(-1.5 / 32768.0), -1);
assert_eq!(f32_to_i16(0.999_999 * 2.0 / 32768.0), 1);
}
#[test]
fn endianness_is_proven_against_hand_written_bytes() {
assert_eq!(
decode_one(SampleFormat::I16Le, &[0x34, 0x12]),
4660.0 / 32768.0
);
assert_eq!(
decode_one(SampleFormat::I16Be, &[0x12, 0x34]),
4660.0 / 32768.0
);
assert_eq!(
decode_one(SampleFormat::I24Le, &[0x56, 0x34, 0x12]),
1_193_046.0 / 8_388_608.0
);
assert_eq!(
decode_one(SampleFormat::I24Be, &[0x12, 0x34, 0x56]),
1_193_046.0 / 8_388_608.0
);
assert_eq!(
decode_one(SampleFormat::I32Le, &[0x78, 0x56, 0x34, 0x12]),
305_419_896_f32 / 2_147_483_648.0
);
assert_eq!(
decode_one(SampleFormat::I32Be, &[0x12, 0x34, 0x56, 0x78]),
305_419_896_f32 / 2_147_483_648.0
);
assert_eq!(
decode_one(SampleFormat::F32Le, &[0x00, 0x00, 0x80, 0x3f]),
1.0
);
assert_eq!(
decode_one(SampleFormat::F32Be, &[0x3f, 0x80, 0x00, 0x00]),
1.0
);
assert_eq!(
decode_one(SampleFormat::F32Le, &[0x00, 0x00, 0x00, 0xbf]),
-0.5
);
assert_eq!(
decode_one(SampleFormat::F32Be, &[0xbf, 0x00, 0x00, 0x00]),
-0.5
);
assert_eq!(
decode_one(
SampleFormat::F64Le,
&[0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f]
),
1.0
);
assert_eq!(
decode_one(
SampleFormat::F64Be,
&[0x3f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
),
1.0
);
assert_eq!(
decode_one(
SampleFormat::F64Be,
&[0xbf, 0xd0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
),
-0.25
);
}
#[test]
fn f64_narrows_to_the_nearest_f32() {
let mut bytes = Vec::new();
SampleFormat::F64Le.encode(&[0.1], &mut bytes);
assert_eq!(decode_one(SampleFormat::F64Le, &bytes), 0.1_f32);
let precise = 1.0_f64 / 3.0;
assert_eq!(
decode_one(SampleFormat::F64Be, &precise.to_be_bytes()),
precise as f32
);
assert_eq!(
decode_one(SampleFormat::F64Le, &1.0e300_f64.to_le_bytes()),
f32::INFINITY
);
}
#[test]
fn the_two_byte_orders_are_reversals_of_each_other() {
for (little, big) in ENDIAN_PAIRS {
for sample in [0.0_f32, 0.5, -0.5, 1.0, -1.0, 0.123_456_79, -0.987_654_3] {
let mut le = encode_one(little, sample);
let be = encode_one(big, sample);
le.reverse();
assert_eq!(le, be, "{little:?} and {big:?} disagree on {sample}");
}
}
}
#[test]
fn every_format_round_trips_through_bytes() {
for format in ALL {
let samples = [0.0_f32, 0.5, -0.5, 0.25, -0.75];
let mut bytes = Vec::new();
let written = format.encode(&samples, &mut bytes);
assert_eq!(written, samples.len() * format.bytes_per_sample());
assert_eq!(bytes.len(), written);
let mut back = Vec::new();
assert_eq!(format.decode(&bytes, &mut back), samples.len());
for (original, recovered) in samples.iter().zip(&back) {
let tolerance = 1.0 / 128.0;
assert!(
(original - recovered).abs() <= tolerance,
"{format:?}: {original} came back as {recovered}"
);
}
if format != SampleFormat::U8 {
assert_eq!(back, samples, "{format:?} was not exact");
}
}
}
#[test]
fn a_trailing_partial_sample_is_ignored_by_the_slice_api() {
let mut out = Vec::new();
assert_eq!(
SampleFormat::I24Le.decode(&[0, 0, 0, 0xff, 0xff], &mut out),
1
);
assert_eq!(out, [0.0]);
out.clear();
assert_eq!(SampleFormat::F64Be.decode(&[0; 7], &mut out), 0);
assert!(out.is_empty());
assert_eq!(SampleFormat::U8.decode(&[], &mut out), 0);
}
#[test]
fn decode_and_encode_append_rather_than_clear() {
let mut samples = vec![99.0_f32];
SampleFormat::I16Le.decode(&[0x00, 0x40], &mut samples);
assert_eq!(samples, [99.0, 0.5]);
let mut bytes = vec![0xaa];
SampleFormat::U8.encode(&[0.0], &mut bytes);
assert_eq!(bytes, [0xaa, 0x80]);
}
#[test]
fn format_widths_are_what_the_wire_carries() {
assert_eq!(SampleFormat::U8.bytes_per_sample(), 1);
assert_eq!(SampleFormat::I16Be.bytes_per_sample(), 2);
assert_eq!(SampleFormat::I24Le.bytes_per_sample(), 3);
assert_eq!(SampleFormat::I24Le.bits_per_sample(), 24);
assert_eq!(SampleFormat::I32Le.bytes_per_sample(), 4);
assert_eq!(SampleFormat::F64Le.bytes_per_sample(), 8);
for format in ALL {
assert!(
format.bytes_per_sample() <= MAX_BYTES_PER_SAMPLE,
"{format:?} does not fit the partial-sample buffer"
);
assert_eq!(
format.is_float(),
matches!(
format,
SampleFormat::F32Le
| SampleFormat::F32Be
| SampleFormat::F64Le
| SampleFormat::F64Be
),
"{format:?} is on the wrong side of is_float"
);
}
}
#[test]
fn the_downmix_matches_decibris_vectors_exactly() {
let mut mono = Vec::new();
assert_eq!(downmix_to_mono(&[0.5, 0.3, 0.4, 0.6], 2, &mut mono), 2);
assert!((mono[0] - 0.4).abs() < 1e-6);
assert!((mono[1] - 0.5).abs() < 1e-6);
mono.clear();
downmix_to_mono(&[0.0, 0.6, 0.3, -0.3, 1.2, -1.8], 6, &mut mono);
assert_eq!(mono.len(), 1);
assert!((mono[0] - 0.0).abs() < 1e-6);
mono.clear();
downmix_to_mono(&[1.0, -1.0], 2, &mut mono);
assert_eq!(mono, [0.0]);
mono.clear();
downmix_to_mono(&[-0.5, -0.5], 2, &mut mono);
assert_eq!(mono, [-0.5]);
mono.clear();
assert_eq!(downmix_to_mono(&[0.2, 0.4, 0.6, 0.8, 0.9], 2, &mut mono), 2);
assert!((mono[0] - 0.3).abs() < 1e-6);
assert!((mono[1] - 0.7).abs() < 1e-6);
mono.clear();
downmix_to_mono(&[0.1, -0.2, 0.3], 1, &mut mono);
assert_eq!(mono, [0.1, -0.2, 0.3]);
mono.clear();
downmix_to_mono(&[0.1, -0.2, 0.3], 0, &mut mono);
assert_eq!(mono, [0.1, -0.2, 0.3]);
mono.clear();
assert_eq!(downmix_to_mono(&[], 2, &mut mono), 0);
assert!(mono.is_empty());
}
#[test]
fn the_downmix_is_a_mean_and_not_a_sum() {
let mut mono = Vec::new();
downmix_to_mono(&[0.7, 0.7, -0.3, -0.3], 2, &mut mono);
assert_eq!(mono, [0.7, -0.3]);
}
#[test]
fn the_downmix_sums_left_to_right_in_f32() {
let a = 1.0_f32;
let b = 1e-8_f32;
let c = -1.0_f32;
let left_to_right = ((a + b) + c) / 3.0;
let mut mono = Vec::new();
downmix_to_mono(&[a, b, c], 3, &mut mono);
assert_eq!(mono[0].to_bits(), left_to_right.to_bits());
}
#[test]
fn interleaving_round_trips_through_planes() {
let interleaved = [1.0_f32, -1.0, 2.0, -2.0, 3.0, -3.0];
let planes = deinterleave(&interleaved, 2);
assert_eq!(planes, vec![vec![1.0, 2.0, 3.0], vec![-1.0, -2.0, -3.0]]);
let mut back = Vec::new();
assert_eq!(interleave(&planes, &mut back), interleaved.len());
assert_eq!(back, interleaved);
}
#[test]
fn deinterleaving_drops_a_trailing_partial_frame_and_handles_the_edges() {
let planes = deinterleave(&[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0], 3);
assert_eq!(planes, vec![vec![1.0, 4.0], vec![2.0, 5.0], vec![3.0, 6.0]]);
assert_eq!(deinterleave(&[1.0, 2.0], 1), vec![vec![1.0, 2.0]]);
assert!(deinterleave(&[1.0, 2.0], 0).is_empty());
assert_eq!(deinterleave(&[], 2), vec![Vec::<f32>::new(); 2]);
}
#[test]
fn interleaving_uneven_planes_covers_the_shortest() {
let mut out = Vec::new();
assert_eq!(
interleave(&[vec![1.0, 2.0, 3.0], vec![-1.0, -2.0]], &mut out),
4
);
assert_eq!(out, [1.0, -1.0, 2.0, -2.0]);
out.clear();
let empty: [Vec<f32>; 0] = [];
assert_eq!(interleave(&empty, &mut out), 0);
assert!(out.is_empty());
out.clear();
let left: &[f32] = &[1.0, 2.0];
let right: &[f32] = &[-1.0, -2.0];
assert_eq!(interleave(&[left, right], &mut out), 4);
assert_eq!(out, [1.0, -1.0, 2.0, -2.0]);
}
#[test]
fn interleave_appends_rather_than_clears() {
let mut out = vec![42.0_f32];
interleave(&[vec![1.0], vec![2.0]], &mut out);
assert_eq!(out, [42.0, 1.0, 2.0]);
}
}