use crate::convert::{
ConversionPlan, ConvertError, ConvertOptions, Converter, PcmFormat, Processed, SampleFormat,
};
const TAPS: usize = 704;
const HISTORY: usize = TAPS / 8;
const DELAY: usize = TAPS / 32;
const DSD_PCM_BATCH_FRAMES: usize = 128;
pub struct DsdPcmConverter {
channels: usize,
table: Vec<[f64; 256]>,
history: Vec<[u8; HISTORY]>,
head: usize,
first: Option<[u8; 8]>,
skipped: usize,
pending: Vec<u8>,
pending_offset: usize,
pcm: Converter,
finishing: bool,
tail_left: usize,
final_partial: bool,
input_frames: u64,
output_frames: u64,
input_rate: u32,
output_rate: u32,
output_frame_bytes: usize,
tail_scratch: Vec<u8>,
tail_read: usize,
tail_valid: usize,
first_stage_gain: f64,
}
impl DsdPcmConverter {
pub fn new(bit_rate: u32, channels: u16, target: PcmFormat) -> Result<Self, ConvertError> {
Self::with_options(bit_rate, channels, target, &ConvertOptions::default())
}
pub fn with_options(
bit_rate: u32,
channels: u16,
target: PcmFormat,
options: &ConvertOptions,
) -> Result<Self, ConvertError> {
if options.source_sample.is_some() {
return Err(ConvertError::InvalidArgument(
"PCM source layout is not valid for native DSD".into(),
));
}
if !matches!(
bit_rate,
2_822_400
| 3_072_000
| 5_644_800
| 6_144_000
| 11_289_600
| 12_288_000
| 22_579_200
| 24_576_000
) || !(1..=8).contains(&channels)
{
return Err(ConvertError::Unsupported(
"native DSD supports DSD64/128/256/512 in the 44.1 and 48 kHz families and 1 to 8 channels".into(),
));
}
let source = PcmFormat::new(bit_rate / 16, channels, SampleFormat::Float32);
let plan = ConversionPlan::pcm(source, target, options)?;
let first_stage_gain = if plan.resamples() {
1.0
} else {
10f64.powf(options.headroom_db / 20.0)
};
let pcm = Converter::new(&plan)?;
let tail_scratch = vec![0; pcm.min_output_bytes()];
let mut coefficients = [0.0; TAPS];
let cutoff = (0.020 + 0.03125) / 2.0;
let beta = crate::convert::resample::kaiser_beta(120.0);
let denominator = crate::convert::resample::bessel_i0(beta);
let mut sum = 0.0;
for (i, c) in coefficients.iter_mut().enumerate() {
let x = i as f64 - (TAPS as f64 - 1.0) / 2.0;
let sinc = (2.0 * std::f64::consts::PI * cutoff * x).sin() / (std::f64::consts::PI * x);
let position = 2.0 * i as f64 / (TAPS - 1) as f64 - 1.0;
let window = crate::convert::resample::bessel_i0(
beta * (1.0 - position * position).max(0.0).sqrt(),
) / denominator;
*c = sinc * window;
sum += *c;
}
for c in &mut coefficients {
*c /= sum;
}
let mut table = vec![[0.0; 256]; HISTORY];
for (group, entries) in table.iter_mut().enumerate() {
for (byte, entry) in entries.iter_mut().enumerate() {
*entry = (0..8)
.map(|bit| {
coefficients[group * 8 + bit]
* if byte & (1 << bit) != 0 { 1.0 } else { -1.0 }
})
.sum();
}
}
Ok(Self {
channels: channels as usize,
table,
history: vec![[0x69; HISTORY]; channels as usize],
head: 0,
first: None,
skipped: 0,
pending: Vec::with_capacity(channels as usize * 4 * DSD_PCM_BATCH_FRAMES),
pending_offset: 0,
pcm,
finishing: false,
tail_left: DELAY,
final_partial: false,
input_frames: 0,
output_frames: 0,
input_rate: bit_rate / 8,
output_rate: target.sample_rate,
output_frame_bytes: target.bytes_per_frame(),
tail_scratch,
tail_read: 0,
tail_valid: 0,
first_stage_gain,
})
}
pub fn min_output_bytes(&self) -> usize {
self.pcm.min_output_bytes()
}
pub fn kind(&self) -> crate::convert::ConversionKind {
crate::convert::ConversionKind::Lossy
}
pub fn clipped_samples(&self) -> u64 {
self.pcm.clipped_samples()
}
pub fn delay_byte_frames(&self) -> usize {
2 * DELAY + self.pcm.delay_frames() * 2
}
fn push_frame(&mut self, frame: &[u8]) {
self.head = (self.head + 1) % HISTORY;
for (ch, byte) in frame.iter().enumerate().take(self.channels) {
self.history[ch][self.head] = *byte;
}
}
fn synthesize(&mut self) {
if self.skipped < DELAY {
self.skipped += 1;
return;
}
for channel in &self.history {
let (older, newer) = channel.split_at(self.head + 1);
let sample: f64 = older
.iter()
.rev()
.chain(newer.iter().rev())
.enumerate()
.map(|(i, byte)| self.table[i][usize::from(*byte)])
.sum();
self.pending
.extend_from_slice(&((sample * self.first_stage_gain) as f32).to_le_bytes());
}
}
fn emit_pending(&mut self, output: &mut [u8]) -> Result<Processed, ConvertError> {
if self.pending.is_empty() {
return Ok(Processed {
consumed: 0,
produced: 0,
});
}
let done = self
.pcm
.process(&self.pending[self.pending_offset..], output)?;
self.pending_offset += done.consumed;
if self.pending_offset == self.pending.len() {
self.pending.clear();
self.pending_offset = 0;
}
Ok(done)
}
pub fn process(&mut self, input: &[u8], output: &mut [u8]) -> Result<Processed, ConvertError> {
if self.finishing {
return Err(ConvertError::InvalidState(
"finish called; reset before processing".into(),
));
}
let mut consumed = 0;
let mut produced = 0;
let batch_bytes = self.channels * 4 * DSD_PCM_BATCH_FRAMES;
loop {
let no_more_input = input.len() - consumed < self.channels;
if !self.pending.is_empty()
&& (self.pending_offset > 0 || self.pending.len() >= batch_bytes || no_more_input)
{
let done = self.emit_pending(&mut output[produced..])?;
produced += done.produced;
if !self.pending.is_empty() {
break;
}
continue;
}
if no_more_input || output.len() - produced < self.min_output_bytes() {
break;
}
let frame = &input[consumed..consumed + self.channels];
self.push_frame(frame);
consumed += self.channels;
if self.first.take().is_some() {
self.synthesize();
} else {
self.first = Some([0; 8]);
}
}
self.input_frames = self
.input_frames
.saturating_add((consumed / self.channels) as u64);
self.output_frames = self
.output_frames
.saturating_add((produced / self.output_frame_bytes) as u64);
Ok(Processed { consumed, produced })
}
pub fn finish(&mut self, output: &mut [u8]) -> Result<usize, ConvertError> {
self.finishing = true;
let expected = (u128::from(self.input_frames) * u128::from(self.output_rate))
.div_ceil(u128::from(self.input_rate));
let remaining = expected.saturating_sub(u128::from(self.output_frames));
let cap = (remaining
.saturating_mul(self.output_frame_bytes as u128)
.min(output.len() as u128) as usize
/ self.output_frame_bytes)
* self.output_frame_bytes;
if cap == 0 {
return Ok(0);
}
let mut made = 0;
while made < cap {
if self.tail_read == self.tail_valid {
self.tail_read = 0;
self.tail_valid = 0;
let mut scratch = std::mem::take(&mut self.tail_scratch);
let result = self.finish_inner(&mut scratch);
self.tail_scratch = scratch;
let generated = result?;
let legitimate = (remaining * self.output_frame_bytes as u128)
.saturating_sub(made as u128)
.min(usize::MAX as u128) as usize;
self.tail_valid = generated.min(legitimate);
if self.tail_valid == 0 {
break;
}
}
let copied = (self.tail_valid - self.tail_read).min(cap - made);
output[made..made + copied]
.copy_from_slice(&self.tail_scratch[self.tail_read..self.tail_read + copied]);
self.tail_read += copied;
made += copied;
}
self.output_frames = self
.output_frames
.saturating_add((made / self.output_frame_bytes) as u64);
Ok(made)
}
fn finish_inner(&mut self, output: &mut [u8]) -> Result<usize, ConvertError> {
let mut produced = 0;
loop {
if !self.pending.is_empty() {
let done = self.emit_pending(&mut output[produced..])?;
produced += done.produced;
if !self.pending.is_empty() {
return Ok(produced);
}
}
if output.len() - produced < self.min_output_bytes() {
return Ok(produced);
}
if self.first.take().is_some() && !self.final_partial {
self.push_frame(&[0x69; 8][..self.channels]);
self.synthesize();
self.final_partial = true;
continue;
}
if self.tail_left > 0 {
self.push_frame(&[0x69; 8][..self.channels]);
self.push_frame(&[0x69; 8][..self.channels]);
self.synthesize();
self.tail_left -= 1;
continue;
}
produced += self.pcm.finish(&mut output[produced..])?;
return Ok(produced);
}
}
pub fn reset(&mut self) {
for h in &mut self.history {
h.fill(0x69);
}
self.head = 0;
self.first = None;
self.skipped = 0;
self.pending.clear();
self.pending_offset = 0;
self.pcm.reset();
self.finishing = false;
self.tail_left = DELAY;
self.final_partial = false;
self.input_frames = 0;
self.output_frames = 0;
self.tail_read = 0;
self.tail_valid = 0;
}
}
#[cfg(test)]
mod tests {
use super::*;
fn converter() -> DsdPcmConverter {
DsdPcmConverter::new(
2_822_400,
2,
PcmFormat::new(44_100, 2, SampleFormat::Float32),
)
.unwrap()
}
fn legacy_process(
c: &mut DsdPcmConverter,
input: &[u8],
output: &mut [u8],
) -> Result<Processed, ConvertError> {
let mut consumed = 0;
let mut produced = 0;
loop {
if !c.pending.is_empty() {
let done = c.emit_pending(&mut output[produced..])?;
produced += done.produced;
if !c.pending.is_empty() {
break;
}
}
if input.len() - consumed < c.channels || output.len() - produced < c.min_output_bytes()
{
break;
}
c.push_frame(&input[consumed..consumed + c.channels]);
consumed += c.channels;
if c.first.take().is_some() {
c.synthesize();
} else {
c.first = Some([0; 8]);
}
}
c.input_frames += (consumed / c.channels) as u64;
c.output_frames += (produced / c.output_frame_bytes) as u64;
Ok(Processed { consumed, produced })
}
fn run_dsd512_pump(
bit_rate: u32,
input_frames: usize,
chunk_frames: usize,
output_frames: usize,
legacy: bool,
) -> (Vec<u8>, u64) {
let target = PcmFormat::new(48_000, 2, SampleFormat::I24);
let mut converter = DsdPcmConverter::new(bit_rate, 2, target).unwrap();
let input: Vec<u8> = (0..input_frames * 2)
.map(|i| ((i.wrapping_mul(73) ^ (i >> 4) ^ 0x69) & 255) as u8)
.collect();
let mut output = vec![0; output_frames * target.bytes_per_frame()];
assert!(output.len() >= converter.min_output_bytes());
let mut decoded = Vec::new();
let mut position = 0;
while position < input.len() {
let end = (position + chunk_frames * 2).min(input.len());
let done = if legacy {
legacy_process(&mut converter, &input[position..end], &mut output)
} else {
converter.process(&input[position..end], &mut output)
}
.unwrap();
assert!(done.consumed > 0 || done.produced > 0);
position += done.consumed;
decoded.extend_from_slice(&output[..done.produced]);
}
loop {
let count = converter.finish(&mut output).unwrap();
if count == 0 {
break;
}
decoded.extend_from_slice(&output[..count]);
}
let clipped = converter.clipped_samples();
converter.reset();
assert!(converter.process(&[0x69, 0x69], &mut output).is_ok());
(decoded, clipped)
}
#[test]
fn dsd512_batched_pump_matches_single_frame_reference_with_backpressure() {
for bit_rate in [22_579_200, 24_576_000] {
let reference = run_dsd512_pump(bit_rate, 24_000, 256, 256, true);
assert_eq!(
run_dsd512_pump(bit_rate, 24_000, 256, 256, false),
reference
);
assert_eq!(run_dsd512_pump(bit_rate, 24_000, 37, 1, false), reference);
let expected_frames = (24_000u64 * 48_000).div_ceil(u64::from(bit_rate / 8));
assert_eq!(reference.0.len(), expected_frames as usize * 6);
}
}
#[test]
fn filter_rejects_decimation_aliases_and_preserves_passband() {
let c = converter();
let response = |f: f64| {
let mut re = 0.0;
let mut im = 0.0;
for (j, table) in c.table.iter().enumerate() {
for bit in 0..8 {
let coeff = (table[1 << bit] - table[0]) / 2.0;
let angle = 2.0 * std::f64::consts::PI * f * (j * 8 + bit) as f64;
re += coeff * angle.cos();
im -= coeff * angle.sin();
}
}
re.hypot(im)
};
let mut ripple = 0.0f64;
for i in 0..=512 {
let f = 0.020 * i as f64 / 512.0;
ripple = ripple.max((response(f) - 1.0).abs());
}
let mut stop = 0.0f64;
for i in 0..=8192 {
let f = 0.03125 + i as f64 * (0.5 - 0.03125) / 8192.0;
stop = stop.max(response(f));
}
println!(
"production taps: ripple {ripple:e}, stop {:.3} dB",
20.0 * stop.log10()
);
assert!(ripple < 0.00001, "ripple {ripple}");
assert!(stop < 0.000002, "stop {stop}");
}
fn run(chunks: usize) -> Vec<u8> {
let mut c = converter();
let input: Vec<u8> = (0..8192)
.map(|i| if i % 2 == 0 { 0x69 } else { 0x96 })
.collect();
let mut result = Vec::new();
let mut offset = 0;
while offset < input.len() {
let end = (offset + chunks).min(input.len());
let mut out = [0u8; 512];
let done = c.process(&input[offset..end], &mut out).unwrap();
assert!(done.consumed > 0 || done.produced > 0);
offset += done.consumed;
result.extend_from_slice(&out[..done.produced]);
}
loop {
let mut out = [0u8; 512];
let n = c.finish(&mut out).unwrap();
result.extend_from_slice(&out[..n]);
if n == 0 {
break;
}
}
assert_eq!(c.kind(), crate::convert::ConversionKind::Lossy);
result
}
#[test]
fn streaming_chunks_and_eos_preserve_phase_and_length() {
let a = run(8192);
let b = run(18);
assert_eq!(a, b);
assert_eq!(a, run(3));
assert_eq!(a.len(), 512 * 8); }
#[test]
fn rejects_unsupported_rates_and_recovers_after_reset() {
assert!(DsdPcmConverter::new(
25_000_000,
2,
PcmFormat::new(44_100, 2, SampleFormat::Float32)
)
.is_err());
let mut c = converter();
let mut out = [0u8; 128];
let _ = c.finish(&mut out).unwrap();
assert!(c.process(&[0x69, 0x69], &mut out).is_err());
c.reset();
assert!(c.process(&[0x69, 0x69], &mut out).is_ok());
}
#[test]
fn channel_histories_are_independent_and_msb_is_oldest() {
let mut c = converter();
for _ in 0..HISTORY {
c.push_frame(&[0xff, 0x00]);
}
c.skipped = DELAY;
c.synthesize();
let left = f32::from_le_bytes(c.pending[0..4].try_into().unwrap());
let right = f32::from_le_bytes(c.pending[4..8].try_into().unwrap());
assert!((left - 1.0).abs() < 1e-6 && (right + 1.0).abs() < 1e-6);
let weight_newest = (c.table[0][1] - c.table[0][0]) / 2.0;
let weight_oldest = (c.table[0][128] - c.table[0][0]) / 2.0;
assert!((weight_newest - weight_oldest).abs() > 1e-9);
}
#[test]
fn odd_end_and_upsampling_return_exact_source_duration() {
for target in [48_000u32, 192_000] {
let mut c = DsdPcmConverter::new(
2_822_400,
1,
PcmFormat::new(target, 1, SampleFormat::Float32),
)
.unwrap();
let input = [0x69; 137];
let mut out = [0u8; 4096];
let done = c.process(&input, &mut out).unwrap();
assert_eq!(done.consumed, input.len());
let mut produced = done.produced;
loop {
let n = c.finish(&mut out).unwrap();
produced += n;
if n == 0 {
break;
}
}
let expected = (137u64 * u64::from(target)).div_ceil(2_822_400 / 8) as usize;
assert_eq!(produced, expected * 4, "target {target}");
}
}
#[test]
fn finish_preserves_tail_with_small_unaligned_buffers() {
for rate in [192_000, 352_800] {
for sample in [SampleFormat::Float32, SampleFormat::int(16, 2).unwrap()] {
let frame = PcmFormat::new(rate, 1, sample).bytes_per_frame();
let render = |capacity: usize| {
let mut c = DsdPcmConverter::new(2_822_400, 1, PcmFormat::new(rate, 1, sample))
.unwrap();
let mut result = Vec::new();
let input = [0x69; 137];
let mut out = vec![0; 8192];
let done = c.process(&input, &mut out).unwrap();
assert_eq!(done.consumed, input.len());
result.extend_from_slice(&out[..done.produced]);
assert_eq!(c.finish(&mut out[..frame - 1]).unwrap(), 0);
loop {
let n = c.finish(&mut out[..capacity]).unwrap();
assert_eq!(n % frame, 0);
result.extend_from_slice(&out[..n]);
if n == 0 {
break;
}
}
assert_eq!(c.finish(&mut out[..capacity]).unwrap(), 0);
assert_eq!(
result.len(),
(137u64 * rate as u64).div_ceil(352_800) as usize * frame
);
result
};
let reference = render(8192);
assert_eq!(reference, render(frame));
assert_eq!(reference, render(frame * 3 + 1));
}
}
}
#[test]
fn full_chain_preserves_audio_tone_and_suppresses_out_of_band_tone() {
fn level(freq: f64, measured_freq: f64) -> f64 {
let mut bits = Vec::new();
let mut acc = 0.0;
let mut byte = 0u8;
for n in 0..131_072 {
let signal =
0.5 * (2.0 * std::f64::consts::PI * freq * n as f64 / 2_822_400.0).sin();
acc += signal;
let high = acc >= 0.0;
acc -= if high { 1.0 } else { -1.0 };
byte = (byte << 1) | u8::from(high);
if n % 8 == 7 {
bits.push(byte);
byte = 0;
}
}
let options = ConvertOptions {
headroom_db: 0.0,
..ConvertOptions::default()
};
let mut c = DsdPcmConverter::with_options(
2_822_400,
1,
PcmFormat::new(44_100, 1, SampleFormat::Float32),
&options,
)
.unwrap();
let mut audio = Vec::new();
let mut i = 0;
let mut out = [0u8; 4096];
while i < bits.len() {
let done = c.process(&bits[i..], &mut out).unwrap();
assert!(done.consumed > 0 || done.produced > 0);
i += done.consumed;
audio.extend_from_slice(&out[..done.produced]);
}
loop {
let n = c.finish(&mut out).unwrap();
audio.extend_from_slice(&out[..n]);
if n == 0 {
break;
}
}
let samples: Vec<f64> = audio
.chunks_exact(4)
.map(|x| f32::from_le_bytes(x.try_into().unwrap()) as f64)
.collect();
let mut re = 0.0;
let mut im = 0.0;
let body = &samples[128..samples.len() - 128];
for (n, value) in body.iter().enumerate() {
let phase = 2.0 * std::f64::consts::PI * measured_freq * n as f64 / 44_100.0;
re += value * phase.cos();
im += value * phase.sin();
}
2.0 * re.hypot(im) / body.len() as f64
}
let in_band = level(1_378.125, 1_378.125);
let alias = level(30_000.0, 14_100.0);
assert!(
(0.47..0.53).contains(&in_band),
"in-band amplitude {in_band}"
);
assert!(alias < 0.002, "aliased amplitude {alias}");
}
#[test]
fn production_chain_preserves_half_modulation_across_rates_and_qualities() {
for source in [2_822_400, 5_644_800, 11_289_600] {
for target in [44_100, source / 16, 768_000] {
for quality in [crate::convert::Quality::High, crate::convert::Quality::Fast] {
let options = ConvertOptions {
quality,
headroom_db: 0.0,
..ConvertOptions::default()
};
let mut c = DsdPcmConverter::with_options(
source,
1,
PcmFormat::new(target, 1, SampleFormat::Float32),
&options,
)
.unwrap();
let input = [0xee; 8192]; let mut out = vec![0u8; 512 * 1024];
let done = c.process(&input, &mut out).unwrap();
assert_eq!(done.consumed, input.len());
let mut n = done.produced;
loop {
let produced = c.finish(&mut out[n..]).unwrap();
n += produced;
if produced == 0 {
break;
}
}
assert_eq!(
n / 4,
(input.len() as u64 * target as u64).div_ceil(source as u64 / 8) as usize
);
let middle = (n / 4) / 2;
let value =
f32::from_le_bytes(out[middle * 4..middle * 4 + 4].try_into().unwrap());
assert!(
(value - 0.5).abs() < 0.0001,
"{source} {target} {quality:?}: {value}"
);
}
}
}
}
#[test]
fn headroom_is_applied_once_with_and_without_pcm_resampling() {
fn amplitude(rate: u32, db: f64) -> f64 {
let options = ConvertOptions {
headroom_db: db,
..ConvertOptions::default()
};
let mut c = DsdPcmConverter::with_options(
2_822_400,
1,
PcmFormat::new(rate, 1, SampleFormat::Float32),
&options,
)
.unwrap();
let input = [0xff; 8192];
let mut out = vec![0; 128 * 1024];
let done = c.process(&input, &mut out).unwrap();
assert_eq!(done.consumed, input.len());
let samples: Vec<f32> = out[..done.produced]
.chunks_exact(4)
.map(|b| f32::from_le_bytes(b.try_into().unwrap()))
.collect();
samples[samples.len() / 2] as f64
}
let gain = 10f64.powf(-6.0 / 20.0);
for rate in [44_100, 176_400] {
let unity = amplitude(rate, 0.0);
let reduced = amplitude(rate, -6.0);
assert!(
(reduced / unity - gain).abs() < 0.001,
"rate {rate}: {}",
reduced / unity
);
}
let options = ConvertOptions {
source_sample: Some(SampleFormat::I16),
..ConvertOptions::default()
};
assert!(DsdPcmConverter::with_options(
2_822_400,
1,
PcmFormat::new(44_100, 1, SampleFormat::Float32),
&options
)
.is_err());
}
}