use crate::CapacityError;
use crate::schema::{
ByteOrder, Channel, DitherMode, Format, InputConversionPolicy, Layout, OnFormatChange,
OutputConverterConfig, PlanError, Reason, SampleType,
};
use std::sync::Arc;
pub const POLYPHASE_RESAMPLER_HALF_WING_TAPS: usize = 32;
pub const POLYPHASE_RESAMPLER_TAPS: usize = 64; pub const POLYPHASE_RESAMPLER_STEPS: usize = 1024;
pub const INPUT_NORMALIZER_HALF_WING_TAPS: usize = POLYPHASE_RESAMPLER_HALF_WING_TAPS;
pub const INPUT_NORMALIZER_TAPS: usize = POLYPHASE_RESAMPLER_TAPS;
pub const INPUT_NORMALIZER_POLYPHASE_STEPS: usize = POLYPHASE_RESAMPLER_STEPS;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ChannelMappingKind {
Identity,
MonoToStereo,
StereoToMono,
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct ConversionStats {
pub input_frames: u64,
pub output_frames: u64,
pub clipped_samples: u64,
pub nonfinite_samples: u64,
pub reset_boundaries: u64,
pub destination_writes: u64,
}
pub fn tpdf_dither_lsb(seed: u64, sample_index: u64) -> f64 {
fn splitmix64(mut x: u64) -> u64 {
x = x.wrapping_add(0x9E37_79B9_7F4A_7C15);
x = (x ^ (x >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
x = (x ^ (x >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
x ^ (x >> 31)
}
let u1 = splitmix64(seed ^ sample_index.wrapping_mul(2));
let u2 = splitmix64(seed ^ sample_index.wrapping_mul(2).wrapping_add(1));
let r1 = ((u1 >> 11) as f64) * (1.0 / ((1u64 << 53) as f64));
let r2 = ((u2 >> 11) as f64) * (1.0 / ((1u64 << 53) as f64));
r1 - r2
}
pub fn validate_channel_mapping(
input_format: &Format,
output_format: &Format,
) -> Result<ChannelMappingKind, Reason> {
if input_format.channels() == output_format.channels() {
match (input_format.layout(), output_format.layout()) {
(Layout::Known(in_labels), Layout::Known(out_labels)) if in_labels != out_labels => {
return Err(Reason::UnsupportedChannelMapping);
}
_ => return Ok(ChannelMappingKind::Identity),
}
}
match (input_format.layout(), output_format.layout()) {
(Layout::Known(in_labels), Layout::Known(out_labels)) => {
if in_labels.as_ref() == [Channel::Mono]
&& out_labels.as_ref() == [Channel::FrontLeft, Channel::FrontRight]
{
Ok(ChannelMappingKind::MonoToStereo)
} else if in_labels.as_ref() == [Channel::FrontLeft, Channel::FrontRight]
&& out_labels.as_ref() == [Channel::Mono]
{
Ok(ChannelMappingKind::StereoToMono)
} else {
Err(Reason::UnsupportedChannelMapping)
}
}
_ => Err(Reason::UnsupportedChannelMapping),
}
}
pub struct PolyphaseResampler {
in_rate: u32,
out_rate: u32,
channels: usize,
step_p: u64,
step_q: u64,
phase_num: u64,
cursor_in: usize,
has_unflushed_tail: bool,
polyphase_table: Vec<[f32; POLYPHASE_RESAMPLER_TAPS]>,
history: Vec<f32>,
}
impl PolyphaseResampler {
pub fn new(
in_rate: u32,
out_rate: u32,
channels: usize,
max_input_frames_per_call: usize,
) -> Result<Self, Reason> {
if in_rate == 0 || out_rate == 0 || channels == 0 || max_input_frames_per_call == 0 {
return Err(Reason::InvalidFormat);
}
let g = gcd_u64(u64::from(in_rate), u64::from(out_rate));
let step_p = u64::from(in_rate) / g;
let step_q = u64::from(out_rate) / g;
let lead_zeros = POLYPHASE_RESAMPLER_HALF_WING_TAPS - 1;
let is_resampled = in_rate != out_rate;
let polyphase_table = build_polyphase_sinc_table(in_rate, out_rate);
let history_cap = if is_resampled {
(POLYPHASE_RESAMPLER_TAPS
+ max_input_frames_per_call.max(POLYPHASE_RESAMPLER_HALF_WING_TAPS)
+ 64)
.checked_mul(channels)
.ok_or(Reason::Capacity(CapacityError::Overflow))?
} else {
0
};
let mut history = Vec::with_capacity(history_cap);
if is_resampled {
history.resize(lead_zeros * channels, 0.0);
}
Ok(Self {
in_rate,
out_rate,
channels,
step_p,
step_q,
phase_num: 0,
cursor_in: lead_zeros,
has_unflushed_tail: false,
polyphase_table,
history,
})
}
#[inline]
pub fn in_rate(&self) -> u32 {
self.in_rate
}
#[inline]
pub fn out_rate(&self) -> u32 {
self.out_rate
}
#[inline]
pub fn channels(&self) -> usize {
self.channels
}
#[inline]
pub fn is_resampled(&self) -> bool {
self.in_rate != self.out_rate
}
#[inline]
pub fn has_unflushed_tail(&self) -> bool {
self.has_unflushed_tail
}
#[inline]
pub fn group_delay_input_frames(&self) -> usize {
if self.is_resampled() {
POLYPHASE_RESAMPLER_HALF_WING_TAPS
} else {
0
}
}
#[inline]
pub fn max_output_frames(&self, max_input_frames: usize) -> usize {
if !self.is_resampled() {
max_input_frames
} else {
let eff_in = max_input_frames.max(POLYPHASE_RESAMPLER_HALF_WING_TAPS);
((eff_in as u64)
.saturating_mul(u64::from(self.out_rate))
.div_ceil(u64::from(self.in_rate)) as usize)
+ 64
}
}
pub fn reset(&mut self) {
let lead_zeros = POLYPHASE_RESAMPLER_HALF_WING_TAPS - 1;
self.phase_num = 0;
self.cursor_in = lead_zeros;
self.has_unflushed_tail = false;
self.history.clear();
if self.is_resampled() {
self.history.resize(lead_zeros * self.channels, 0.0);
}
}
pub fn process_into(
&mut self,
in_samples: &[f32],
out_f32: &mut Vec<f32>,
) -> Result<usize, Reason> {
let out_ch = self.channels;
if !in_samples.len().is_multiple_of(out_ch) {
return Err(Reason::IncompleteFrame);
}
out_f32.clear();
let in_frames = in_samples.len() / out_ch;
if in_frames == 0 {
return Ok(0);
}
if !self.is_resampled() {
out_f32.extend_from_slice(in_samples);
return Ok(in_frames);
}
if self.history.len() + in_samples.len() > self.history.capacity() {
return Err(Reason::Capacity(CapacityError::ExceedsByteCeiling));
}
self.history.extend_from_slice(in_samples);
self.has_unflushed_tail = true;
Ok(self.drain_history_into(out_f32))
}
pub fn flush_tail_into(&mut self, out_f32: &mut Vec<f32>) -> Result<usize, Reason> {
out_f32.clear();
if !self.is_resampled() || !self.has_unflushed_tail {
return Ok(0);
}
let zero_samples = POLYPHASE_RESAMPLER_HALF_WING_TAPS * self.channels;
if self.history.len() + zero_samples > self.history.capacity() {
return Err(Reason::Capacity(CapacityError::ExceedsByteCeiling));
}
self.history.resize(self.history.len() + zero_samples, 0.0);
let produced = self.drain_history_into(out_f32);
self.reset();
Ok(produced)
}
fn drain_history_into(&mut self, out_f32: &mut Vec<f32>) -> usize {
let out_ch = self.channels;
let frames_in_buf = self.history.len() / out_ch;
let scale_num = self.step_q;
let inv_scale = 1.0 / (scale_num as f64);
let table_steps = POLYPHASE_RESAMPLER_STEPS as u128;
let mut produced_frames = 0usize;
while self.cursor_in + POLYPHASE_RESAMPLER_HALF_WING_TAPS < frames_in_buf {
let scaled = u128::from(self.phase_num) * table_steps;
let idx0 = (scaled / u128::from(scale_num)) as usize;
let rem = (scaled % u128::from(scale_num)) as f64;
let frac = (rem * inv_scale) as f32;
let row0 = &self.polyphase_table[idx0];
let row1 = &self.polyphase_table[idx0 + 1];
let base_frame = self.cursor_in - (POLYPHASE_RESAMPLER_HALF_WING_TAPS - 1);
let window = &self.history
[base_frame * out_ch..(base_frame + POLYPHASE_RESAMPLER_TAPS) * out_ch];
if out_ch == 2 {
let mut acc_l = 0.0_f64;
let mut acc_r = 0.0_f64;
for t_idx in 0..POLYPHASE_RESAMPLER_TAPS {
let w = f64::from(row0[t_idx] + frac * (row1[t_idx] - row0[t_idx]));
let pair_base = t_idx * 2;
acc_l += f64::from(window[pair_base]) * w;
acc_r += f64::from(window[pair_base + 1]) * w;
}
out_f32.push(acc_l as f32);
out_f32.push(acc_r as f32);
} else if out_ch == 1 {
let mut acc = 0.0_f64;
for t_idx in 0..POLYPHASE_RESAMPLER_TAPS {
let w = f64::from(row0[t_idx] + frac * (row1[t_idx] - row0[t_idx]));
acc += f64::from(window[t_idx]) * w;
}
out_f32.push(acc as f32);
} else {
for ch in 0..out_ch {
let mut acc = 0.0_f64;
for t_idx in 0..POLYPHASE_RESAMPLER_TAPS {
let w = f64::from(row0[t_idx] + frac * (row1[t_idx] - row0[t_idx]));
acc += f64::from(window[t_idx * out_ch + ch]) * w;
}
out_f32.push(acc as f32);
}
}
produced_frames += 1;
let next_num = self.phase_num + self.step_p;
self.cursor_in += (next_num / self.step_q) as usize;
self.phase_num = next_num % self.step_q;
}
let keep_lead = POLYPHASE_RESAMPLER_HALF_WING_TAPS - 1;
let consumed = self.cursor_in.saturating_sub(keep_lead);
if consumed > 0 {
let total_samples = self.history.len();
let drop_samples = consumed * out_ch;
self.history.copy_within(drop_samples..total_samples, 0);
self.history.truncate(total_samples - drop_samples);
self.cursor_in -= consumed;
}
produced_frames
}
}
pub struct OutputConverter {
input_format: Format,
config: OutputConverterConfig,
max_frames_per_block: usize,
mapping: ChannelMappingKind,
resampler: PolyphaseResampler,
sample_counter: u64,
stats: ConversionStats,
decode_scratch: Vec<f32>,
mapped_scratch: Vec<f32>,
resampled_scratch: Vec<f32>,
}
impl OutputConverter {
pub fn new(
input_format: Format,
config: OutputConverterConfig,
max_frames_per_block: usize,
) -> Result<Self, Reason> {
if max_frames_per_block == 0 {
return Err(Reason::InvalidFormat);
}
let mapping = validate_channel_mapping(&input_format, &config.target_format)?;
let in_ch = usize::from(input_format.channels());
let out_ch = usize::from(config.target_format.channels());
let resampler = PolyphaseResampler::new(
input_format.rate(),
config.target_format.rate(),
out_ch,
max_frames_per_block,
)?;
let resampled_cap = if resampler.is_resampled() {
resampler.max_output_frames(max_frames_per_block) * out_ch
} else {
0
};
Ok(Self {
input_format,
config,
max_frames_per_block,
mapping,
resampler,
sample_counter: 0,
stats: ConversionStats::default(),
decode_scratch: Vec::with_capacity(max_frames_per_block * in_ch),
mapped_scratch: Vec::with_capacity(max_frames_per_block * out_ch),
resampled_scratch: Vec::with_capacity(resampled_cap),
})
}
pub fn input_format(&self) -> &Format {
&self.input_format
}
pub fn output_format(&self) -> &Format {
&self.config.target_format
}
#[inline]
pub fn dither(&self) -> DitherMode {
self.config.dither
}
#[inline]
pub fn seed(&self) -> u64 {
self.config.seed
}
#[inline]
pub fn has_unflushed_tail(&self) -> bool {
self.resampler.has_unflushed_tail()
}
#[inline]
pub fn max_output_frames(&self, max_input_frames: usize) -> usize {
self.resampler.max_output_frames(max_input_frames)
}
pub fn group_delay_input_frames(&self) -> usize {
self.resampler.group_delay_input_frames()
}
pub fn stats(&self) -> ConversionStats {
self.stats
}
pub fn reset(&mut self) {
self.resampler.reset();
self.stats.reset_boundaries = self.stats.reset_boundaries.saturating_add(1);
}
pub fn reset_for_input_format(&mut self, new_input_format: Format) -> Result<(), Reason> {
let mapping = validate_channel_mapping(&new_input_format, &self.config.target_format)?;
let in_ch = usize::from(new_input_format.channels());
let out_ch = usize::from(self.config.target_format.channels());
let resampler = PolyphaseResampler::new(
new_input_format.rate(),
self.config.target_format.rate(),
out_ch,
self.max_frames_per_block,
)?;
let needed_decode = self.max_frames_per_block * in_ch;
if self.decode_scratch.capacity() < needed_decode {
self.decode_scratch
.reserve(needed_decode - self.decode_scratch.len());
}
if resampler.is_resampled() {
let needed_resampled = resampler.max_output_frames(self.max_frames_per_block) * out_ch;
if self.resampled_scratch.capacity() < needed_resampled {
self.resampled_scratch
.reserve(needed_resampled - self.resampled_scratch.len());
}
}
self.input_format = new_input_format;
self.mapping = mapping;
self.resampler = resampler;
self.stats.reset_boundaries = self.stats.reset_boundaries.saturating_add(1);
Ok(())
}
pub fn convert_into(
&mut self,
in_bytes: &[u8],
out_bytes: &mut Vec<u8>,
) -> Result<usize, Reason> {
let in_frames = self.input_format.frames_in(in_bytes.len())?;
if in_frames > self.max_frames_per_block {
return Err(Reason::Capacity(CapacityError::ExceedsByteCeiling));
}
if in_frames == 0 {
out_bytes.clear();
return Ok(0);
}
self.decode_scratch.clear();
decode_samples_sanitized(
in_bytes,
self.input_format.sample(),
self.input_format.order(),
&mut self.decode_scratch,
&mut self.stats.nonfinite_samples,
)?;
self.mapped_scratch.clear();
match self.mapping {
ChannelMappingKind::Identity => {
self.mapped_scratch.extend_from_slice(&self.decode_scratch);
}
ChannelMappingKind::MonoToStereo => {
for &sample in &self.decode_scratch {
self.mapped_scratch.push(sample);
self.mapped_scratch.push(sample);
}
}
ChannelMappingKind::StereoToMono => {
for pair in self.decode_scratch.as_chunks::<2>().0 {
self.mapped_scratch.push(0.5 * (pair[0] + pair[1]));
}
}
}
self.finish_mapped_into(in_frames, out_bytes)
}
pub fn convert_f32_into(
&mut self,
in_samples: &[f32],
out_bytes: &mut Vec<u8>,
) -> Result<usize, Reason> {
let in_ch = usize::from(self.input_format.channels());
if !in_samples.len().is_multiple_of(in_ch) {
return Err(Reason::IncompleteFrame);
}
let in_frames = in_samples.len() / in_ch;
if in_frames > self.max_frames_per_block {
return Err(Reason::Capacity(CapacityError::ExceedsByteCeiling));
}
if in_frames == 0 {
out_bytes.clear();
return Ok(0);
}
self.mapped_scratch.clear();
match self.mapping {
ChannelMappingKind::Identity => {
for &raw in in_samples {
if raw.is_finite() {
self.mapped_scratch.push(raw);
} else {
self.stats.nonfinite_samples =
self.stats.nonfinite_samples.saturating_add(1);
self.mapped_scratch.push(0.0);
}
}
}
ChannelMappingKind::MonoToStereo => {
for &raw in in_samples {
let clean = if raw.is_finite() {
raw
} else {
self.stats.nonfinite_samples =
self.stats.nonfinite_samples.saturating_add(1);
0.0
};
self.mapped_scratch.push(clean);
self.mapped_scratch.push(clean);
}
}
ChannelMappingKind::StereoToMono => {
for pair in in_samples.as_chunks::<2>().0 {
let l = if pair[0].is_finite() {
pair[0]
} else {
self.stats.nonfinite_samples =
self.stats.nonfinite_samples.saturating_add(1);
0.0
};
let r = if pair[1].is_finite() {
pair[1]
} else {
self.stats.nonfinite_samples =
self.stats.nonfinite_samples.saturating_add(1);
0.0
};
self.mapped_scratch.push(0.5 * (l + r));
}
}
}
self.finish_mapped_into(in_frames, out_bytes)
}
fn finish_mapped_into(
&mut self,
in_frames: usize,
out_bytes: &mut Vec<u8>,
) -> Result<usize, Reason> {
let out_ch = usize::from(self.config.target_format.channels());
debug_assert_eq!(self.mapped_scratch.len(), in_frames * out_ch);
out_bytes.clear();
let out_frames = if self.resampler.is_resampled() {
let produced = self
.resampler
.process_into(&self.mapped_scratch, &mut self.resampled_scratch)?;
encode_samples_quantized(
&self.resampled_scratch,
self.config.target_format.sample(),
self.config.target_format.order(),
self.config.dither,
self.config.seed,
&mut self.sample_counter,
&mut self.stats.clipped_samples,
out_bytes,
);
produced
} else {
encode_samples_quantized(
&self.mapped_scratch,
self.config.target_format.sample(),
self.config.target_format.order(),
self.config.dither,
self.config.seed,
&mut self.sample_counter,
&mut self.stats.clipped_samples,
out_bytes,
);
in_frames
};
self.stats.input_frames = self.stats.input_frames.saturating_add(in_frames as u64);
self.stats.output_frames = self.stats.output_frames.saturating_add(out_frames as u64);
self.stats.destination_writes = self.stats.destination_writes.saturating_add(1);
Ok(out_frames)
}
pub fn flush_tail_into(&mut self, out_bytes: &mut Vec<u8>) -> Result<usize, Reason> {
out_bytes.clear();
if !self.resampler.has_unflushed_tail() {
return Ok(0);
}
let produced = self
.resampler
.flush_tail_into(&mut self.resampled_scratch)?;
if produced == 0 {
return Ok(0);
}
encode_samples_quantized(
&self.resampled_scratch,
self.config.target_format.sample(),
self.config.target_format.order(),
self.config.dither,
self.config.seed,
&mut self.sample_counter,
&mut self.stats.clipped_samples,
out_bytes,
);
self.stats.output_frames = self.stats.output_frames.saturating_add(produced as u64);
self.stats.destination_writes = self.stats.destination_writes.saturating_add(1);
Ok(produced)
}
}
pub fn evaluate_format_change(
previous: &Format,
next: &Format,
policy: OnFormatChange,
converter: Option<&mut OutputConverter>,
) -> Result<bool, Reason> {
if previous == next {
return Ok(false);
}
if let Some(conv) = converter {
conv.reset_for_input_format(next.clone())?;
return Ok(true);
}
match policy {
OnFormatChange::Adapt => Ok(true),
OnFormatChange::Fail => Err(Reason::FormatChanged),
}
}
fn gcd_u64(mut a: u64, mut b: u64) -> u64 {
while b != 0 {
let r = a % b;
a = b;
b = r;
}
a.max(1)
}
#[inline]
fn blackman_harris_4term(v: f64) -> f64 {
if v.abs() >= 1.0 {
return 0.0;
}
let pi = std::f64::consts::PI;
0.35875
+ 0.48829 * (pi * v).cos()
+ 0.14128 * (2.0 * pi * v).cos()
+ 0.01168 * (3.0 * pi * v).cos()
}
pub fn resolve_mixer_working_format(
input_formats: &[Format],
output_converter: Option<&OutputConverterConfig>,
policy: InputConversionPolicy,
) -> Result<(Format, bool), PlanError> {
if input_formats.is_empty() {
return Err(PlanError::InvalidTarget);
}
let first = &input_formats[0];
let all_same_rate = input_formats.iter().all(|f| f.rate() == first.rate());
let all_same_channels = input_formats
.iter()
.all(|f| f.channels() == first.channels() && f.layout() == first.layout());
if !all_same_rate && policy == InputConversionPolicy::StrictNoResample {
return Err(PlanError::UnsupportedCapturePlan);
}
if all_same_rate && all_same_channels {
let fmt = Format::new(
SampleType::F32,
ByteOrder::Little,
first.rate(),
first.channels(),
first.layout().clone(),
)
.map_err(|_| PlanError::UnsupportedCapturePlan)?;
return Ok((fmt, false));
}
let mut has_mono = false;
let mut has_stereo = false;
for fmt in input_formats {
match fmt.layout() {
Layout::Known(labels) if labels.as_ref() == [Channel::Mono] => {
has_mono = true;
}
Layout::Known(labels)
if labels.as_ref() == [Channel::FrontLeft, Channel::FrontRight] =>
{
has_stereo = true;
}
_ => return Err(PlanError::UnsupportedCapturePlan),
}
}
let (out_channels, out_layout) = if has_stereo {
(
2u16,
Layout::Known(Arc::from([Channel::FrontLeft, Channel::FrontRight])),
)
} else if has_mono {
(1u16, Layout::Known(Arc::from([Channel::Mono])))
} else {
return Err(PlanError::UnsupportedCapturePlan);
};
let max_in_rate = input_formats
.iter()
.map(Format::rate)
.max()
.unwrap_or(48_000);
let cap_rate = output_converter
.map_or(48_000, |c| c.target_format.rate())
.max(48_000);
let working_rate = if max_in_rate > 48_000 {
max_in_rate.max(cap_rate)
} else {
max_in_rate.min(cap_rate)
};
let requires_resample = input_formats.iter().any(|f| f.rate() != working_rate);
let working = Format::new(
SampleType::F32,
ByteOrder::Little,
working_rate,
out_channels,
out_layout,
)
.map_err(|_| PlanError::UnsupportedCapturePlan)?;
Ok((working, requires_resample))
}
fn build_polyphase_sinc_table(in_rate: u32, out_rate: u32) -> Vec<[f32; INPUT_NORMALIZER_TAPS]> {
if in_rate == out_rate {
return Vec::new();
}
let ratio = f64::from(out_rate) / f64::from(in_rate);
let cutoff = 0.425 * ratio.min(1.0);
let radius = INPUT_NORMALIZER_HALF_WING_TAPS as f64;
let mut table = Vec::with_capacity(INPUT_NORMALIZER_POLYPHASE_STEPS + 1);
for phase_idx in 0..=INPUT_NORMALIZER_POLYPHASE_STEPS {
let mu = (phase_idx as f64) / (INPUT_NORMALIZER_POLYPHASE_STEPS as f64);
let mut raw_weights = [0.0_f64; INPUT_NORMALIZER_TAPS];
let mut sum = 0.0_f64;
for (t_idx, tap) in (-((INPUT_NORMALIZER_HALF_WING_TAPS as isize) - 1)
..=(INPUT_NORMALIZER_HALF_WING_TAPS as isize))
.enumerate()
{
let x = (tap as f64) - mu;
let w = blackman_harris_4term(x / radius);
let arg = 2.0 * cutoff * x;
let sinc = if arg.abs() < 1e-12 {
1.0
} else {
(std::f64::consts::PI * arg).sin() / (std::f64::consts::PI * arg)
};
let c = 2.0 * cutoff * sinc * w;
raw_weights[t_idx] = c;
sum += c;
}
let inv = if sum.abs() > 1e-15 { 1.0 / sum } else { 0.0 };
let mut row = [0.0_f32; INPUT_NORMALIZER_TAPS];
for (dst, &w) in row.iter_mut().zip(raw_weights.iter()) {
*dst = (w * inv) as f32;
}
table.push(row);
}
table
}
pub struct InputNormalizer {
input_format: Format,
working_format: Format,
mapping: ChannelMappingKind,
identity_bypass: bool,
resampler: PolyphaseResampler,
decode_scratch: Vec<f32>,
mapped_scratch: Vec<f32>,
stats: ConversionStats,
}
impl InputNormalizer {
pub fn new(
input_format: Format,
working_format: Format,
max_input_frames_per_call: usize,
) -> Result<Self, Reason> {
if max_input_frames_per_call == 0 || working_format.sample() != SampleType::F32 {
return Err(Reason::InvalidFormat);
}
let mapping = validate_channel_mapping(&input_format, &working_format)?;
let identity_bypass = input_format.sample() == SampleType::F32
&& input_format.order() == ByteOrder::Little
&& input_format.rate() == working_format.rate()
&& mapping == ChannelMappingKind::Identity;
let in_ch = usize::from(input_format.channels());
let out_ch = usize::from(working_format.channels());
let resampler = PolyphaseResampler::new(
input_format.rate(),
working_format.rate(),
out_ch,
max_input_frames_per_call,
)?;
let (decode_cap, mapped_cap) = if identity_bypass {
(0, 0)
} else if !resampler.is_resampled() {
(max_input_frames_per_call * in_ch, 0)
} else {
(
max_input_frames_per_call * in_ch,
max_input_frames_per_call * out_ch,
)
};
Ok(Self {
input_format,
working_format,
mapping,
identity_bypass,
resampler,
decode_scratch: Vec::with_capacity(decode_cap),
mapped_scratch: Vec::with_capacity(mapped_cap),
stats: ConversionStats::default(),
})
}
pub fn input_format(&self) -> &Format {
&self.input_format
}
pub fn working_format(&self) -> &Format {
&self.working_format
}
pub fn is_identity_bypass(&self) -> bool {
self.identity_bypass
}
pub fn is_resampled(&self) -> bool {
self.resampler.is_resampled()
}
#[inline]
pub fn has_unflushed_tail(&self) -> bool {
self.resampler.has_unflushed_tail()
}
pub fn group_delay_input_frames(&self) -> usize {
self.resampler.group_delay_input_frames()
}
pub fn max_output_frames(&self, max_input_frames: usize) -> usize {
self.resampler.max_output_frames(max_input_frames)
}
pub fn stats(&self) -> ConversionStats {
self.stats
}
pub fn reset(&mut self) {
self.resampler.reset();
self.stats.reset_boundaries = self.stats.reset_boundaries.saturating_add(1);
}
pub fn reset_for_input_format(
&mut self,
new_input_format: Format,
max_input_frames_per_call: usize,
) -> Result<(), Reason> {
*self = Self::new(
new_input_format,
self.working_format.clone(),
max_input_frames_per_call,
)?;
self.stats.reset_boundaries = 1;
Ok(())
}
pub fn normalize_into(
&mut self,
in_bytes: &[u8],
out_f32: &mut Vec<f32>,
) -> Result<usize, Reason> {
let in_frames = self.input_format.frames_in(in_bytes.len())?;
out_f32.clear();
if in_frames == 0 {
return Ok(0);
}
if self.identity_bypass {
for chunk in in_bytes.as_chunks::<4>().0 {
let val = f32::from_le_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]);
if val.is_finite() {
out_f32.push(val);
} else {
self.stats.nonfinite_samples = self.stats.nonfinite_samples.saturating_add(1);
out_f32.push(0.0);
}
}
self.stats.input_frames = self.stats.input_frames.saturating_add(in_frames as u64);
self.stats.output_frames = self.stats.output_frames.saturating_add(in_frames as u64);
self.stats.destination_writes = self.stats.destination_writes.saturating_add(1);
return Ok(in_frames);
}
if in_frames * usize::from(self.input_format.channels()) > self.decode_scratch.capacity() {
return Err(Reason::Capacity(CapacityError::ExceedsByteCeiling));
}
self.decode_scratch.clear();
decode_samples_sanitized(
in_bytes,
self.input_format.sample(),
self.input_format.order(),
&mut self.decode_scratch,
&mut self.stats.nonfinite_samples,
)?;
if !self.is_resampled() {
match self.mapping {
ChannelMappingKind::Identity => {
out_f32.extend_from_slice(&self.decode_scratch);
}
ChannelMappingKind::MonoToStereo => {
for &s in &self.decode_scratch {
out_f32.push(s);
out_f32.push(s);
}
}
ChannelMappingKind::StereoToMono => {
for pair in self.decode_scratch.as_chunks::<2>().0 {
out_f32.push(0.5 * (pair[0] + pair[1]));
}
}
}
self.stats.input_frames = self.stats.input_frames.saturating_add(in_frames as u64);
self.stats.output_frames = self.stats.output_frames.saturating_add(in_frames as u64);
self.stats.destination_writes = self.stats.destination_writes.saturating_add(1);
return Ok(in_frames);
}
self.mapped_scratch.clear();
match self.mapping {
ChannelMappingKind::Identity => {
self.mapped_scratch.extend_from_slice(&self.decode_scratch);
}
ChannelMappingKind::MonoToStereo => {
for &s in &self.decode_scratch {
self.mapped_scratch.push(s);
self.mapped_scratch.push(s);
}
}
ChannelMappingKind::StereoToMono => {
for pair in self.decode_scratch.as_chunks::<2>().0 {
self.mapped_scratch.push(0.5 * (pair[0] + pair[1]));
}
}
}
let produced_frames = self.resampler.process_into(&self.mapped_scratch, out_f32)?;
self.stats.input_frames = self.stats.input_frames.saturating_add(in_frames as u64);
self.stats.output_frames = self
.stats
.output_frames
.saturating_add(produced_frames as u64);
self.stats.destination_writes = self.stats.destination_writes.saturating_add(1);
Ok(produced_frames)
}
pub fn flush_tail_into(&mut self, out_f32: &mut Vec<f32>) -> Result<usize, Reason> {
out_f32.clear();
if !self.resampler.has_unflushed_tail() {
return Ok(0);
}
let produced = self.resampler.flush_tail_into(out_f32)?;
if produced > 0 {
self.stats.output_frames = self.stats.output_frames.saturating_add(produced as u64);
self.stats.destination_writes = self.stats.destination_writes.saturating_add(1);
}
Ok(produced)
}
}
pub struct FrameReblocker {
channels: usize,
block_frames: usize,
ring: Vec<f32>,
head: usize,
len_samples: usize,
}
impl FrameReblocker {
pub fn new(
channels: usize,
block_frames: usize,
max_buffered_frames: usize,
) -> Result<Self, Reason> {
if channels == 0 || block_frames == 0 || max_buffered_frames < block_frames {
return Err(Reason::InvalidFormat);
}
let cap_samples = max_buffered_frames
.checked_mul(channels)
.ok_or(Reason::Capacity(CapacityError::Overflow))?;
Ok(Self {
channels,
block_frames,
ring: vec![0.0_f32; cap_samples],
head: 0,
len_samples: 0,
})
}
pub fn channels(&self) -> usize {
self.channels
}
pub fn block_frames(&self) -> usize {
self.block_frames
}
pub fn buffered_frames(&self) -> usize {
self.len_samples / self.channels
}
pub fn clear(&mut self) {
self.head = 0;
self.len_samples = 0;
}
pub fn push_samples(&mut self, samples: &[f32]) -> Result<(), Reason> {
if !samples.len().is_multiple_of(self.channels) {
return Err(Reason::IncompleteFrame);
}
let cap = self.ring.len();
if self.len_samples + samples.len() > cap {
return Err(Reason::Capacity(CapacityError::ExceedsByteCeiling));
}
let tail = (self.head + self.len_samples) % cap;
let first = samples.len().min(cap - tail);
self.ring[tail..tail + first].copy_from_slice(&samples[..first]);
if first < samples.len() {
let rem = samples.len() - first;
self.ring[..rem].copy_from_slice(&samples[first..]);
}
self.len_samples += samples.len();
Ok(())
}
pub fn pop_block_into(&mut self, out_samples: &mut [f32]) -> Result<bool, Reason> {
let needed = self.block_frames * self.channels;
if out_samples.len() < needed {
return Err(Reason::IncompleteFrame);
}
if self.len_samples < needed {
return Ok(false);
}
let cap = self.ring.len();
let first = needed.min(cap - self.head);
out_samples[..first].copy_from_slice(&self.ring[self.head..self.head + first]);
if first < needed {
let rem = needed - first;
out_samples[first..needed].copy_from_slice(&self.ring[..rem]);
}
self.head = (self.head + needed) % cap;
self.len_samples -= needed;
Ok(true)
}
pub fn flush_partial_into(&mut self, out_samples: &mut [f32]) -> Result<usize, Reason> {
let full_samples = self.block_frames * self.channels;
if out_samples.len() < full_samples {
return Err(Reason::IncompleteFrame);
}
if self.len_samples == 0 {
return Ok(0);
}
let copy_samples = self.len_samples.min(full_samples);
let cap = self.ring.len();
let first = copy_samples.min(cap - self.head);
out_samples[..first].copy_from_slice(&self.ring[self.head..self.head + first]);
if first < copy_samples {
let rem = copy_samples - first;
out_samples[first..copy_samples].copy_from_slice(&self.ring[..rem]);
}
out_samples[copy_samples..full_samples].fill(0.0);
let real_frames = copy_samples / self.channels;
self.head = (self.head + copy_samples) % cap;
self.len_samples -= copy_samples;
if self.len_samples == 0 {
self.head = 0;
}
Ok(real_frames)
}
pub fn peek_into(&self, frame_offset: usize, out_samples: &mut [f32]) -> Result<bool, Reason> {
if !out_samples.len().is_multiple_of(self.channels) {
return Err(Reason::IncompleteFrame);
}
let offset_samples = frame_offset
.checked_mul(self.channels)
.ok_or(Reason::Capacity(CapacityError::Overflow))?;
let needed = out_samples.len();
if offset_samples.saturating_add(needed) > self.len_samples {
return Ok(false);
}
let cap = self.ring.len();
let start = (self.head + offset_samples) % cap;
let first = needed.min(cap - start);
out_samples[..first].copy_from_slice(&self.ring[start..start + first]);
if first < needed {
let rem = needed - first;
out_samples[first..needed].copy_from_slice(&self.ring[..rem]);
}
Ok(true)
}
pub fn discard_frames(&mut self, frames: usize) -> usize {
let drop_samples = frames.saturating_mul(self.channels).min(self.len_samples);
let cap = self.ring.len();
self.head = (self.head + drop_samples) % cap;
self.len_samples -= drop_samples;
drop_samples / self.channels
}
}
pub(crate) fn decode_samples_sanitized(
in_bytes: &[u8],
sample_type: SampleType,
order: ByteOrder,
out_f32: &mut Vec<f32>,
nonfinite_count: &mut u64,
) -> Result<(), Reason> {
match sample_type {
SampleType::F32 => {
if !in_bytes.len().is_multiple_of(4) {
return Err(Reason::IncompleteFrame);
}
for chunk in in_bytes.as_chunks::<4>().0 {
let b: [u8; 4] = [chunk[0], chunk[1], chunk[2], chunk[3]];
let val = match order {
ByteOrder::Little => f32::from_le_bytes(b),
ByteOrder::Big => f32::from_be_bytes(b),
};
if val.is_finite() {
out_f32.push(val);
} else {
*nonfinite_count = nonfinite_count.saturating_add(1);
out_f32.push(0.0);
}
}
}
SampleType::I16 => {
if !in_bytes.len().is_multiple_of(2) {
return Err(Reason::IncompleteFrame);
}
for chunk in in_bytes.as_chunks::<2>().0 {
let b: [u8; 2] = [chunk[0], chunk[1]];
let val = match order {
ByteOrder::Little => i16::from_le_bytes(b),
ByteOrder::Big => i16::from_be_bytes(b),
};
out_f32.push((f32::from(val) / 32768.0).clamp(-1.0, 1.0));
}
}
SampleType::I24 => {
if !in_bytes.len().is_multiple_of(3) {
return Err(Reason::IncompleteFrame);
}
for chunk in in_bytes.as_chunks::<3>().0 {
let signed = match order {
ByteOrder::Little => {
let raw = u32::from(chunk[0])
| (u32::from(chunk[1]) << 8)
| (u32::from(chunk[2]) << 16);
((raw << 8) as i32) >> 8
}
ByteOrder::Big => {
let raw = (u32::from(chunk[0]) << 16)
| (u32::from(chunk[1]) << 8)
| u32::from(chunk[2]);
((raw << 8) as i32) >> 8
}
};
out_f32.push(((signed as f64) / 8_388_608.0).clamp(-1.0, 1.0) as f32);
}
}
SampleType::I32 => {
if !in_bytes.len().is_multiple_of(4) {
return Err(Reason::IncompleteFrame);
}
for chunk in in_bytes.as_chunks::<4>().0 {
let b: [u8; 4] = [chunk[0], chunk[1], chunk[2], chunk[3]];
let val = match order {
ByteOrder::Little => i32::from_le_bytes(b),
ByteOrder::Big => i32::from_be_bytes(b),
};
out_f32.push(((val as f64) / 2_147_483_648.0).clamp(-1.0, 1.0) as f32);
}
}
}
Ok(())
}
#[allow(clippy::too_many_arguments)]
pub(crate) fn encode_samples_quantized(
samples: &[f32],
sample_type: SampleType,
order: ByteOrder,
dither: DitherMode,
seed: u64,
sample_counter: &mut u64,
clipped_count: &mut u64,
out_bytes: &mut Vec<u8>,
) {
for &raw in samples {
let idx = *sample_counter;
*sample_counter = sample_counter.wrapping_add(1);
let clean = if raw.is_finite() { f64::from(raw) } else { 0.0 };
if clean.abs() > 1.0 {
*clipped_count = clipped_count.saturating_add(1);
}
let dither_lsb = match dither {
DitherMode::Tpdf => tpdf_dither_lsb(seed, idx),
DitherMode::None => 0.0,
};
match sample_type {
SampleType::F32 => {
let val = clean as f32;
let bytes = match order {
ByteOrder::Little => val.to_le_bytes(),
ByteOrder::Big => val.to_be_bytes(),
};
out_bytes.extend_from_slice(&bytes);
}
SampleType::I16 => {
let q = (clean * 32767.0 + dither_lsb)
.round()
.clamp(-32768.0, 32767.0) as i16;
let bytes = match order {
ByteOrder::Little => q.to_le_bytes(),
ByteOrder::Big => q.to_be_bytes(),
};
out_bytes.extend_from_slice(&bytes);
}
SampleType::I24 => {
let q = (clean * 8_388_607.0 + dither_lsb)
.round()
.clamp(-8_388_608.0, 8_388_607.0) as i32;
let le = q.to_le_bytes();
match order {
ByteOrder::Little => out_bytes.extend_from_slice(&le[..3]),
ByteOrder::Big => {
out_bytes.push(le[2]);
out_bytes.push(le[1]);
out_bytes.push(le[0]);
}
}
}
SampleType::I32 => {
let q = (clean * 2_147_483_647.0 + dither_lsb)
.round()
.clamp(-2_147_483_648.0, 2_147_483_647.0) as i32;
let bytes = match order {
ByteOrder::Little => q.to_le_bytes(),
ByteOrder::Big => q.to_be_bytes(),
};
out_bytes.extend_from_slice(&bytes);
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::Arc;
use std::time::Instant;
#[test]
fn output_converter_quantizes_dithers_clamps_and_maps_layouts_with_zero_steady_allocations() {
let in_fmt = Format::new(
SampleType::F32,
ByteOrder::Little,
48000,
1,
Layout::Known(Arc::from([Channel::Mono])),
)
.unwrap();
let out_fmt = Format::new(
SampleType::I16,
ByteOrder::Little,
16000,
2,
Layout::Known(Arc::from([Channel::FrontLeft, Channel::FrontRight])),
)
.unwrap();
let mut conv = OutputConverter::new(
in_fmt.clone(),
OutputConverterConfig::new(out_fmt.clone(), DitherMode::Tpdf).with_seed(0xC59A_2026),
480,
)
.unwrap();
assert_eq!(conv.group_delay_input_frames(), 32);
let mut in_bytes = Vec::with_capacity(480 * 4);
for i in 0..480 {
let s = if i == 0 {
f32::NAN
} else if i == 1 {
1.5
} else if i == 2 {
-1.5
} else {
((2.0 * std::f32::consts::PI * 1000.0 * (i as f32) / 48000.0).sin()) * 0.5
};
in_bytes.extend_from_slice(&s.to_le_bytes());
}
let mut out_bytes = Vec::with_capacity(160 * 4);
let frames = conv.convert_into(&in_bytes, &mut out_bytes).unwrap();
assert_eq!(frames, 150);
assert_eq!(out_bytes.len(), 150 * 4);
assert_eq!(conv.stats().nonfinite_samples, 1);
let measured = allocation_counter::measure(|| {
for _ in 0..1000 {
let f = conv.convert_into(&in_bytes, &mut out_bytes).unwrap();
assert_eq!(f, 160);
}
});
assert_eq!(measured.count_total, 0);
}
#[test]
fn polyphase_output_converter_preserves_prime_chunk_seams_direct_f32_parity_and_speedup() {
fn legacy_stateless_sinc_reference(
input: &[f32],
channels: usize,
in_rate: u32,
out_rate: u32,
out: &mut Vec<f32>,
) -> usize {
let half_wing_taps = 16isize;
let in_frames = input.len() / channels;
let out_frames =
((in_frames as u64) * u64::from(out_rate) / u64::from(in_rate)) as usize;
out.clear();
let ratio = f64::from(out_rate) / f64::from(in_rate);
let cutoff = ratio.min(1.0) * 0.45;
for out_f in 0..out_frames {
let center = (out_f as f64) / ratio;
let base = center.floor() as isize;
for ch in 0..channels {
let mut acc = 0.0_f64;
let mut weight_sum = 0.0_f64;
for tap in -half_wing_taps..=half_wing_taps {
let src_f = base + tap;
let x = (src_f as f64) - center;
let window_pos = x / (half_wing_taps as f64);
if window_pos.abs() >= 1.0 {
continue;
}
let w = 0.42
+ 0.5 * (std::f64::consts::PI * window_pos).cos()
+ 0.08 * (2.0 * std::f64::consts::PI * window_pos).cos();
let sinc_arg = 2.0 * cutoff * x;
let sinc = if sinc_arg.abs() < 1e-12 {
1.0
} else {
(std::f64::consts::PI * sinc_arg).sin()
/ (std::f64::consts::PI * sinc_arg)
};
let coeff = 2.0 * cutoff * sinc * w;
weight_sum += coeff;
if src_f >= 0 && (src_f as usize) < in_frames {
acc += f64::from(input[(src_f as usize) * channels + ch]) * coeff;
}
}
if weight_sum.abs() > 1e-12 {
acc /= weight_sum;
}
out.push(acc as f32);
}
}
out_frames
}
let stereo_layout = Layout::Known(Arc::from([Channel::FrontLeft, Channel::FrontRight]));
let out_16k_f32 = Format::new(
SampleType::F32,
ByteOrder::Little,
16_000,
2,
stereo_layout.clone(),
)
.unwrap();
for &in_rate in &[48_000u32, 44_100u32] {
let in_fmt = Format::new(
SampleType::F32,
ByteOrder::Little,
in_rate,
2,
stereo_layout.clone(),
)
.unwrap();
let total_in_frames = (in_rate as usize) / 2;
let mut signal_f32 = Vec::with_capacity(total_in_frames * 2);
let mut signal_bytes = Vec::with_capacity(total_in_frames * 8);
for f in 0..total_in_frames {
let t = (f as f64) / f64::from(in_rate);
let l = (0.65 * (2.0 * std::f64::consts::PI * 997.0 * t).sin()) as f32;
let r = (0.45 * (2.0 * std::f64::consts::PI * 1_483.0 * t).cos()) as f32;
signal_f32.push(l);
signal_f32.push(r);
signal_bytes.extend_from_slice(&l.to_le_bytes());
signal_bytes.extend_from_slice(&r.to_le_bytes());
}
let cfg = OutputConverterConfig::new(out_16k_f32.clone(), DitherMode::None);
let mut single_conv =
OutputConverter::new(in_fmt.clone(), cfg.clone(), total_in_frames).unwrap();
let mut single_bytes = Vec::with_capacity(total_in_frames * 8);
let single_frames = single_conv
.convert_f32_into(&signal_f32, &mut single_bytes)
.unwrap();
assert!(single_frames > 7_900);
let mut chunk_f32_conv =
OutputConverter::new(in_fmt.clone(), cfg.clone(), 480).unwrap();
let mut chunk_byte_conv = OutputConverter::new(in_fmt, cfg, 480).unwrap();
let mut tmp_f32_bytes = Vec::with_capacity(480 * 8);
let mut tmp_raw_bytes = Vec::with_capacity(480 * 8);
let mut all_chunk_bytes = Vec::with_capacity(single_bytes.len());
let mut cursor = 0usize;
for &step in [73usize, 137, 480].iter().cycle() {
if cursor >= total_in_frames {
break;
}
let take = step.min(total_in_frames - cursor);
let f1 = chunk_f32_conv
.convert_f32_into(
&signal_f32[cursor * 2..(cursor + take) * 2],
&mut tmp_f32_bytes,
)
.unwrap();
let f2 = chunk_byte_conv
.convert_into(
&signal_bytes[cursor * 8..(cursor + take) * 8],
&mut tmp_raw_bytes,
)
.unwrap();
assert_eq!(f1, f2);
assert_eq!(tmp_f32_bytes, tmp_raw_bytes);
all_chunk_bytes.extend_from_slice(&tmp_f32_bytes);
cursor += take;
}
assert_eq!(all_chunk_bytes.len(), single_bytes.len());
let max_diff = single_bytes
.as_chunks::<4>()
.0
.iter()
.zip(all_chunk_bytes.as_chunks::<4>().0.iter())
.map(|(a, b)| (f32::from_le_bytes(*a) - f32::from_le_bytes(*b)).abs())
.fold(0.0_f32, f32::max);
assert!(
max_diff <= 1e-5,
"seam error {max_diff} exceeded 1e-5 for {in_rate} -> 16000"
);
}
let in_48k =
Format::new(SampleType::F32, ByteOrder::Little, 48_000, 2, stereo_layout).unwrap();
let out_16k_i16 = Format::new(
SampleType::I16,
ByteOrder::Little,
16_000,
2,
Layout::Known(Arc::from([Channel::FrontLeft, Channel::FrontRight])),
)
.unwrap();
let mut block_f32 = Vec::with_capacity(480 * 2);
for i in 0..480 {
let s = 0.5 * (2.0 * std::f32::consts::PI * 1000.0 * (i as f32) / 48_000.0).sin();
block_f32.push(s);
block_f32.push(-s);
}
let mut conv = OutputConverter::new(
in_48k,
OutputConverterConfig::new(out_16k_i16, DitherMode::Tpdf),
480,
)
.unwrap();
let mut out_bytes = Vec::with_capacity(160 * 4);
let _ = conv.convert_f32_into(&block_f32, &mut out_bytes).unwrap();
let t_poly_start = Instant::now();
let allocs = allocation_counter::measure(|| {
for _ in 0..1_000 {
let produced = conv.convert_f32_into(&block_f32, &mut out_bytes).unwrap();
assert_eq!(produced, 160);
}
});
let poly_ns = t_poly_start.elapsed().as_nanos().max(1);
assert_eq!(allocs.count_total, 0);
let mut legacy_out = Vec::with_capacity(160 * 2);
let _ = legacy_stateless_sinc_reference(&block_f32, 2, 48_000, 16_000, &mut legacy_out);
let t_legacy_start = Instant::now();
for _ in 0..200 {
let produced =
legacy_stateless_sinc_reference(&block_f32, 2, 48_000, 16_000, &mut legacy_out);
assert_eq!(produced, 160);
}
let legacy_scaled_ns = t_legacy_start.elapsed().as_nanos().saturating_mul(5);
let speedup = (legacy_scaled_ns as f64) / (poly_ns as f64);
assert!(
speedup >= 5.0,
"expected >= 5x CPU speedup over stateless sin/cos resampler, observed {speedup:.2}x (poly={poly_ns}ns, legacy_est={legacy_scaled_ns}ns)"
);
}
#[test]
fn output_converter_rejects_ambiguous_layouts_and_handles_on_format_change() {
let unknown_stereo = Format::new(
SampleType::F32,
ByteOrder::Little,
48000,
2,
Layout::Unknown,
)
.unwrap();
let unknown_mono = Format::new(
SampleType::F32,
ByteOrder::Little,
48000,
1,
Layout::Unknown,
)
.unwrap();
assert_eq!(
validate_channel_mapping(&unknown_stereo, &unknown_mono),
Err(Reason::UnsupportedChannelMapping)
);
let stereo_96k = Format::new(
SampleType::F32,
ByteOrder::Little,
96000,
2,
Layout::Unknown,
)
.unwrap();
assert_eq!(
evaluate_format_change(&unknown_stereo, &stereo_96k, OnFormatChange::Adapt, None),
Ok(true)
);
assert_eq!(
evaluate_format_change(&unknown_stereo, &stereo_96k, OnFormatChange::Fail, None),
Err(Reason::FormatChanged)
);
}
#[test]
fn input_normalizer_and_reblocker_preserve_seams_order_independence_and_zero_allocations() {
let mono_16k_i16 = Format::new(
SampleType::I16,
ByteOrder::Little,
16_000,
1,
Layout::Known(Arc::from([Channel::Mono])),
)
.unwrap();
let stereo_48k_f32 = Format::new(
SampleType::F32,
ByteOrder::Little,
48_000,
2,
Layout::Known(Arc::from([Channel::FrontLeft, Channel::FrontRight])),
)
.unwrap();
let (wf_ab, res_ab) = resolve_mixer_working_format(
&[mono_16k_i16.clone(), stereo_48k_f32.clone()],
None,
InputConversionPolicy::AutoWhenMixing,
)
.unwrap();
let (wf_ba, res_ba) = resolve_mixer_working_format(
&[stereo_48k_f32.clone(), mono_16k_i16.clone()],
None,
InputConversionPolicy::AutoWhenMixing,
)
.unwrap();
assert_eq!(wf_ab, stereo_48k_f32);
assert_eq!(wf_ab, wf_ba);
assert!(res_ab && res_ba);
assert_eq!(
resolve_mixer_working_format(
&[mono_16k_i16.clone(), stereo_48k_f32.clone()],
None,
InputConversionPolicy::StrictNoResample,
),
Err(PlanError::UnsupportedCapturePlan)
);
let bypass =
InputNormalizer::new(stereo_48k_f32.clone(), stereo_48k_f32.clone(), 1024).unwrap();
assert!(bypass.is_identity_bypass());
assert!(!bypass.is_resampled());
assert_eq!(bypass.group_delay_input_frames(), 0);
let total_in_frames = 8_000usize;
let mut raw_16k = Vec::with_capacity(total_in_frames * 2);
for f in 0..total_in_frames {
let t = (f as f64) / 16_000.0;
let s =
(0.75 * (2.0 * std::f64::consts::PI * 997.0 * t).sin() * 32767.0).round() as i16;
raw_16k.extend_from_slice(&s.to_le_bytes());
}
let mut single_norm = InputNormalizer::new(
mono_16k_i16.clone(),
stereo_48k_f32.clone(),
total_in_frames,
)
.unwrap();
assert_eq!(single_norm.group_delay_input_frames(), 32);
let mut single_out = Vec::with_capacity(total_in_frames * 6);
let single_frames = single_norm
.normalize_into(&raw_16k, &mut single_out)
.unwrap();
assert!(single_frames > 23_800);
let mut chunk_norm =
InputNormalizer::new(mono_16k_i16.clone(), stereo_48k_f32.clone(), 1024).unwrap();
let mut chunk_tmp = Vec::with_capacity(1024 * 8);
let mut chunk_all = Vec::with_capacity(single_out.len());
let mut cursor = 0usize;
for &step in [67usize, 137, 320, 441, 480, 1024].iter().cycle() {
if cursor >= total_in_frames {
break;
}
let take = step.min(total_in_frames - cursor);
chunk_norm
.normalize_into(&raw_16k[cursor * 2..(cursor + take) * 2], &mut chunk_tmp)
.unwrap();
chunk_all.extend_from_slice(&chunk_tmp);
cursor += take;
}
assert_eq!(chunk_all.len(), single_out.len());
let max_seam_diff = single_out
.iter()
.zip(chunk_all.iter())
.map(|(a, b)| (a - b).abs())
.fold(0.0_f32, f32::max);
assert!(
max_seam_diff <= 1e-6,
"unexpected seam diff: {max_seam_diff}"
);
let mut reblocker = FrameReblocker::new(2, 480, 480 * 8).unwrap();
let packet_160 = &raw_16k[..160 * 2]; let mut block_out = vec![0.0_f32; 480 * 2];
chunk_norm
.normalize_into(packet_160, &mut chunk_tmp)
.unwrap();
reblocker.push_samples(&chunk_tmp).unwrap();
let _ = reblocker.pop_block_into(&mut block_out).unwrap();
let allocs = allocation_counter::measure(|| {
for _ in 0..1_000 {
chunk_norm
.normalize_into(packet_160, &mut chunk_tmp)
.unwrap();
reblocker.push_samples(&chunk_tmp).unwrap();
while reblocker.pop_block_into(&mut block_out).unwrap() {}
}
});
assert_eq!(allocs.count_total, 0);
}
#[test]
fn causal_tail_flush_and_reblocker_partial_flush_preserve_completeness_with_zero_allocations() {
use crate::schema::{
DEFAULT_IDLE_GRACE_NS, MAX_IDLE_GRACE_NS, MIN_IDLE_GRACE_NS, MixerConfig,
};
let def_cfg = MixerConfig::default();
assert_eq!(def_cfg.idle_grace_ns, DEFAULT_IDLE_GRACE_NS);
assert!(def_cfg.validate().is_ok());
assert!(
MixerConfig {
idle_grace_ns: MIN_IDLE_GRACE_NS - 1,
..def_cfg
}
.validate()
.is_err()
);
assert!(
MixerConfig {
idle_grace_ns: MAX_IDLE_GRACE_NS + 1,
..def_cfg
}
.validate()
.is_err()
);
let stereo = Layout::Known(Arc::from([Channel::FrontLeft, Channel::FrontRight]));
let out_16k = Format::new(
SampleType::F32,
ByteOrder::Little,
16_000,
2,
stereo.clone(),
)
.unwrap();
for &(in_rate, block_in, num_blocks, expected_total_out) in &[
(48_000u32, 480usize, 3usize, 480usize),
(44_100u32, 441usize, 4usize, 640usize),
] {
let in_fmt = Format::new(
SampleType::F32,
ByteOrder::Little,
in_rate,
2,
stereo.clone(),
)
.unwrap();
let total_in = block_in * num_blocks;
let mut sig = Vec::with_capacity((total_in + POLYPHASE_RESAMPLER_HALF_WING_TAPS) * 2);
for f in 0..total_in {
let t = (f as f64) / f64::from(in_rate);
sig.push((0.7 * (2.0 * std::f64::consts::PI * 880.0 * t).sin()) as f32);
sig.push((0.5 * (2.0 * std::f64::consts::PI * 1_320.0 * t).cos()) as f32);
}
let cfg = OutputConverterConfig::new(out_16k.clone(), DitherMode::None);
let mut ref_conv =
OutputConverter::new(in_fmt.clone(), cfg.clone(), total_in + 64).unwrap();
let mut sig_with_wing = sig.clone();
sig_with_wing.resize((total_in + POLYPHASE_RESAMPLER_HALF_WING_TAPS) * 2, 0.0);
let mut ref_bytes = Vec::with_capacity(expected_total_out * 8);
let ref_frames = ref_conv
.convert_f32_into(&sig_with_wing, &mut ref_bytes)
.unwrap();
assert_eq!(ref_frames, expected_total_out);
let mut conv = OutputConverter::new(in_fmt.clone(), cfg, block_in).unwrap();
assert!(!conv.has_unflushed_tail());
let mut out_bytes = Vec::with_capacity(block_in * 8);
assert_eq!(conv.flush_tail_into(&mut out_bytes).unwrap(), 0);
let mut collected = Vec::with_capacity(expected_total_out * 8);
let mut pre_flush_frames = 0usize;
for b in 0..num_blocks {
let f = conv
.convert_f32_into(
&sig[b * block_in * 2..(b + 1) * block_in * 2],
&mut out_bytes,
)
.unwrap();
pre_flush_frames += f;
collected.extend_from_slice(&out_bytes);
}
assert!(conv.has_unflushed_tail());
assert!(pre_flush_frames < expected_total_out);
let tail_frames = conv.flush_tail_into(&mut out_bytes).unwrap();
assert!(tail_frames > 0);
assert!(!conv.has_unflushed_tail());
assert_eq!(pre_flush_frames + tail_frames, expected_total_out);
collected.extend_from_slice(&out_bytes);
assert_eq!(collected.len(), ref_bytes.len());
assert_eq!(conv.flush_tail_into(&mut out_bytes).unwrap(), 0);
let max_diff = ref_bytes
.as_chunks::<4>()
.0
.iter()
.zip(collected.as_chunks::<4>().0.iter())
.map(|(a, b)| (f32::from_le_bytes(*a) - f32::from_le_bytes(*b)).abs())
.fold(0.0_f32, f32::max);
assert!(
max_diff <= 1e-5,
"tail flush mismatch {max_diff} on {in_rate}"
);
let mut resumed_bytes = Vec::with_capacity(expected_total_out * 8);
let flush_allocs = allocation_counter::measure(|| {
for b in 0..num_blocks {
let _ = conv
.convert_f32_into(
&sig[b * block_in * 2..(b + 1) * block_in * 2],
&mut out_bytes,
)
.unwrap();
resumed_bytes.extend_from_slice(&out_bytes);
}
let tf = conv.flush_tail_into(&mut out_bytes).unwrap();
assert_eq!(tf, tail_frames);
resumed_bytes.extend_from_slice(&out_bytes);
});
assert_eq!(flush_allocs.count_total, 0);
assert_eq!(resumed_bytes, collected);
}
let mono_16k_i16 = Format::new(
SampleType::I16,
ByteOrder::Little,
16_000,
1,
Layout::Known(Arc::from([Channel::Mono])),
)
.unwrap();
let stereo_48k =
Format::new(SampleType::F32, ByteOrder::Little, 48_000, 2, stereo).unwrap();
let mut norm = InputNormalizer::new(mono_16k_i16, stereo_48k, 480).unwrap();
let mut raw_160 = Vec::with_capacity(160 * 2);
for _ in 0..160 {
raw_160.extend_from_slice(&16_384_i16.to_le_bytes());
}
let mut norm_out = Vec::with_capacity(600 * 2);
let f_pre = norm.normalize_into(&raw_160, &mut norm_out).unwrap();
assert_eq!(f_pre, (160 - 32) * 3);
assert!(norm.has_unflushed_tail());
let f_tail = norm.flush_tail_into(&mut norm_out).unwrap();
assert_eq!(f_pre + f_tail, 480);
assert!(!norm.has_unflushed_tail());
assert_eq!(norm.flush_tail_into(&mut norm_out).unwrap(), 0);
let mut reb = FrameReblocker::new(2, 480, 960).unwrap();
let mut blk = vec![1.0_f32; 480 * 2];
reb.push_samples(&vec![0.25_f32; 700 * 2]).unwrap();
assert!(reb.pop_block_into(&mut blk).unwrap());
assert_eq!(reb.buffered_frames(), 220);
reb.push_samples(&vec![0.5_f32; 300 * 2]).unwrap();
assert!(reb.pop_block_into(&mut blk).unwrap());
assert_eq!(reb.buffered_frames(), 40);
let partial_allocs = allocation_counter::measure(|| {
let real = reb.flush_partial_into(&mut blk).unwrap();
assert_eq!(real, 40);
});
assert_eq!(partial_allocs.count_total, 0);
assert_eq!(reb.buffered_frames(), 0);
assert!((blk[0] - 0.5).abs() <= 1e-6);
assert!((blk[39 * 2 + 1] - 0.5).abs() <= 1e-6);
assert!(blk[40 * 2..].iter().all(|&s| s == 0.0));
assert_eq!(reb.flush_partial_into(&mut blk).unwrap(), 0);
}
}