feat: WarzonePhone lossy VoIP protocol — Phase 1 complete

Rust workspace with 7 crates implementing a custom VoIP protocol
designed for extremely lossy connections (5-70% loss, 100-500kbps,
300-800ms RTT). 89 tests passing across all crates.

Crates:
- wzp-proto: Wire format, traits, adaptive quality controller, jitter buffer, session FSM
- wzp-codec: Opus encoder/decoder (audiopus), Codec2 stubs, adaptive switching, resampling
- wzp-fec: RaptorQ fountain codes, interleaving, block management (proven 30-70% loss recovery)
- wzp-crypto: X25519+ChaCha20-Poly1305, Warzone identity compatible, anti-replay, rekeying
- wzp-transport: QUIC via quinn with DATAGRAM frames, path monitoring, signaling streams
- wzp-relay: Integration stub (Phase 2)
- wzp-client: Integration stub (Phase 2)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Siavash Sameni
2026-03-27 12:45:07 +04:00
commit 51e893590c
47 changed files with 7097 additions and 0 deletions

View File

@@ -0,0 +1,20 @@
[package]
name = "wzp-codec"
version.workspace = true
edition.workspace = true
license.workspace = true
rust-version.workspace = true
description = "WarzonePhone audio codec layer — Opus + Codec2 encoding/decoding"
[dependencies]
wzp-proto = { workspace = true }
tracing = { workspace = true }
# Opus bindings
audiopus = { workspace = true }
# TODO: Add codec2-sys when implementing Codec2 support
# codec2-sys = "0.1"
# rubato = "0.15" # resampling
[dev-dependencies]

View File

@@ -0,0 +1,287 @@
//! Adaptive codec that wraps both Opus and Codec2, switching on the fly.
//!
//! `AdaptiveEncoder` and `AdaptiveDecoder` present a unified `AudioEncoder` /
//! `AudioDecoder` interface while transparently delegating to the appropriate
//! inner codec based on the current `QualityProfile`.
//!
//! Callers always work with 48 kHz PCM. When Codec2 is the active codec the
//! adaptive layer handles the 48 kHz ↔ 8 kHz resampling internally.
use tracing::debug;
use wzp_proto::{AudioDecoder, AudioEncoder, CodecError, CodecId, QualityProfile};
use crate::codec2_dec::Codec2Decoder;
use crate::codec2_enc::Codec2Encoder;
use crate::opus_dec::OpusDecoder;
use crate::opus_enc::OpusEncoder;
use crate::resample;
// ─── Helpers ─────────────────────────────────────────────────────────────────
/// Returns `true` when the codec operates at 8 kHz (i.e. a Codec2 variant).
fn is_codec2(codec: CodecId) -> bool {
matches!(codec, CodecId::Codec2_3200 | CodecId::Codec2_1200)
}
/// Build a `QualityProfile` that only contains Opus-relevant fields.
fn opus_profile(profile: QualityProfile) -> QualityProfile {
// Clamp to Opus24k if the caller somehow passes a Codec2 profile.
let codec = if is_codec2(profile.codec) {
CodecId::Opus24k
} else {
profile.codec
};
QualityProfile { codec, ..profile }
}
/// Build a `QualityProfile` that only contains Codec2-relevant fields.
fn codec2_profile(profile: QualityProfile) -> QualityProfile {
let codec = if is_codec2(profile.codec) {
profile.codec
} else {
CodecId::Codec2_3200
};
QualityProfile { codec, ..profile }
}
// ─── AdaptiveEncoder ─────────────────────────────────────────────────────────
/// Adaptive encoder that delegates to either Opus or Codec2.
///
/// Input PCM is always 48 kHz mono. When Codec2 is selected the encoder
/// downsamples to 8 kHz before encoding.
pub struct AdaptiveEncoder {
opus: OpusEncoder,
codec2: Codec2Encoder,
active: CodecId,
}
impl AdaptiveEncoder {
/// Create a new adaptive encoder starting at the given profile.
pub fn new(profile: QualityProfile) -> Result<Self, CodecError> {
let opus = OpusEncoder::new(opus_profile(profile))?;
let codec2 = Codec2Encoder::new(codec2_profile(profile))?;
Ok(Self {
opus,
codec2,
active: profile.codec,
})
}
}
impl AudioEncoder for AdaptiveEncoder {
fn encode(&mut self, pcm: &[i16], out: &mut [u8]) -> Result<usize, CodecError> {
if is_codec2(self.active) {
// Downsample 48 kHz → 8 kHz then encode via Codec2.
let pcm_8k = resample::resample_48k_to_8k(pcm);
self.codec2.encode(&pcm_8k, out)
} else {
self.opus.encode(pcm, out)
}
}
fn codec_id(&self) -> CodecId {
self.active
}
fn set_profile(&mut self, profile: QualityProfile) -> Result<(), CodecError> {
let prev = self.active;
self.active = profile.codec;
if is_codec2(profile.codec) {
debug!(from = ?prev, to = ?profile.codec, "adaptive encoder → Codec2");
self.codec2.set_profile(profile)
} else {
debug!(from = ?prev, to = ?profile.codec, "adaptive encoder → Opus");
self.opus.set_profile(profile)
}
}
fn max_frame_bytes(&self) -> usize {
if is_codec2(self.active) {
self.codec2.max_frame_bytes()
} else {
self.opus.max_frame_bytes()
}
}
fn set_inband_fec(&mut self, enabled: bool) {
self.opus.set_inband_fec(enabled);
// No-op for Codec2 (per trait doc).
}
fn set_dtx(&mut self, enabled: bool) {
self.opus.set_dtx(enabled);
}
}
// ─── AdaptiveDecoder ─────────────────────────────────────────────────────────
/// Adaptive decoder that delegates to either Opus or Codec2.
///
/// Output PCM is always 48 kHz mono. When Codec2 is selected the decoder
/// upsamples the 8 kHz output to 48 kHz before returning.
pub struct AdaptiveDecoder {
opus: OpusDecoder,
codec2: Codec2Decoder,
active: CodecId,
}
impl AdaptiveDecoder {
/// Create a new adaptive decoder starting at the given profile.
pub fn new(profile: QualityProfile) -> Result<Self, CodecError> {
let opus = OpusDecoder::new(opus_profile(profile))?;
let codec2 = Codec2Decoder::new(codec2_profile(profile))?;
Ok(Self {
opus,
codec2,
active: profile.codec,
})
}
}
impl AudioDecoder for AdaptiveDecoder {
fn decode(&mut self, encoded: &[u8], pcm: &mut [i16]) -> Result<usize, CodecError> {
if is_codec2(self.active) {
// Decode into a temporary 8 kHz buffer, then upsample.
let c2_samples = self.codec2_frame_samples();
let mut buf_8k = vec![0i16; c2_samples];
let n = self.codec2.decode(encoded, &mut buf_8k)?;
let pcm_48k = resample::resample_8k_to_48k(&buf_8k[..n]);
let out_len = pcm_48k.len().min(pcm.len());
pcm[..out_len].copy_from_slice(&pcm_48k[..out_len]);
Ok(out_len)
} else {
self.opus.decode(encoded, pcm)
}
}
fn decode_lost(&mut self, pcm: &mut [i16]) -> Result<usize, CodecError> {
if is_codec2(self.active) {
let c2_samples = self.codec2_frame_samples();
let mut buf_8k = vec![0i16; c2_samples];
let n = self.codec2.decode_lost(&mut buf_8k)?;
let pcm_48k = resample::resample_8k_to_48k(&buf_8k[..n]);
let out_len = pcm_48k.len().min(pcm.len());
pcm[..out_len].copy_from_slice(&pcm_48k[..out_len]);
Ok(out_len)
} else {
self.opus.decode_lost(pcm)
}
}
fn codec_id(&self) -> CodecId {
self.active
}
fn set_profile(&mut self, profile: QualityProfile) -> Result<(), CodecError> {
let prev = self.active;
self.active = profile.codec;
if is_codec2(profile.codec) {
debug!(from = ?prev, to = ?profile.codec, "adaptive decoder → Codec2");
self.codec2.set_profile(profile)
} else {
debug!(from = ?prev, to = ?profile.codec, "adaptive decoder → Opus");
self.opus.set_profile(profile)
}
}
}
impl AdaptiveDecoder {
/// Number of 8 kHz samples expected for the current Codec2 frame.
fn codec2_frame_samples(&self) -> usize {
self.codec2.frame_samples()
}
}
// ─── Tests ───────────────────────────────────────────────────────────────────
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn encoder_starts_with_correct_codec() {
let enc = AdaptiveEncoder::new(QualityProfile::GOOD).unwrap();
assert_eq!(enc.codec_id(), CodecId::Opus24k);
}
#[test]
fn decoder_starts_with_correct_codec() {
let dec = AdaptiveDecoder::new(QualityProfile::GOOD).unwrap();
assert_eq!(dec.codec_id(), CodecId::Opus24k);
}
#[test]
fn encoder_switches_opus_to_codec2() {
let mut enc = AdaptiveEncoder::new(QualityProfile::GOOD).unwrap();
assert_eq!(enc.codec_id(), CodecId::Opus24k);
enc.set_profile(QualityProfile::CATASTROPHIC).unwrap();
assert_eq!(enc.codec_id(), CodecId::Codec2_1200);
// Max frame bytes should reflect Codec2 now.
assert!(enc.max_frame_bytes() <= 16);
}
#[test]
fn encoder_switches_codec2_to_opus() {
let mut enc = AdaptiveEncoder::new(QualityProfile::CATASTROPHIC).unwrap();
assert_eq!(enc.codec_id(), CodecId::Codec2_1200);
enc.set_profile(QualityProfile::GOOD).unwrap();
assert_eq!(enc.codec_id(), CodecId::Opus24k);
assert!(enc.max_frame_bytes() > 16);
}
#[test]
fn decoder_switches_opus_to_codec2() {
let mut dec = AdaptiveDecoder::new(QualityProfile::GOOD).unwrap();
assert_eq!(dec.codec_id(), CodecId::Opus24k);
dec.set_profile(QualityProfile::CATASTROPHIC).unwrap();
assert_eq!(dec.codec_id(), CodecId::Codec2_1200);
}
#[test]
fn decoder_codec2_plc_produces_48k_silence() {
let mut dec = AdaptiveDecoder::new(QualityProfile::CATASTROPHIC).unwrap();
// Codec2 1200 @ 40ms → 320 samples at 8kHz → 1920 at 48kHz
let mut pcm = vec![0i16; 1920];
let n = dec.decode_lost(&mut pcm).unwrap();
assert_eq!(n, 1920);
// PLC from Codec2 stub is silence, upsampled silence is still silence.
assert!(pcm.iter().all(|&s| s == 0));
}
#[test]
fn encoder_opus_encode_works_after_switch() {
// Start on Codec2, switch to Opus, and encode a real frame.
let mut enc = AdaptiveEncoder::new(QualityProfile::CATASTROPHIC).unwrap();
enc.set_profile(QualityProfile::GOOD).unwrap();
// 20ms at 48kHz = 960 samples
let pcm = vec![0i16; 960];
let mut out = vec![0u8; 512];
let n = enc.encode(&pcm, &mut out).unwrap();
assert!(n > 0);
}
#[test]
fn encoder_roundtrip_opus() {
let mut enc = AdaptiveEncoder::new(QualityProfile::GOOD).unwrap();
let mut dec = AdaptiveDecoder::new(QualityProfile::GOOD).unwrap();
let pcm_in = vec![0i16; 960]; // 20ms silence
let mut encoded = vec![0u8; 512];
let enc_bytes = enc.encode(&pcm_in, &mut encoded).unwrap();
assert!(enc_bytes > 0);
let mut pcm_out = vec![0i16; 960];
let dec_samples = dec.decode(&encoded[..enc_bytes], &mut pcm_out).unwrap();
assert_eq!(dec_samples, 960);
}
}

View File

@@ -0,0 +1,67 @@
//! Codec2 decoder — stub implementation.
//!
//! Codec2 operates at 8 kHz mono. Resampling back to 48 kHz is handled
//! externally (see `resample.rs` and `AdaptiveCodec`).
//!
//! This is a stub that returns an error on decode. When `codec2-sys`
//! is linked, replace the body of `decode()` with actual FFI calls.
use wzp_proto::{AudioDecoder, CodecError, CodecId, QualityProfile};
/// Stub Codec2 decoder implementing `AudioDecoder`.
///
/// Currently returns `CodecError::DecodeFailed` for decode operations.
/// PLC fills output with silence (zeros).
pub struct Codec2Decoder {
codec_id: CodecId,
frame_duration_ms: u8,
}
impl Codec2Decoder {
/// Create a new stub Codec2 decoder.
pub fn new(profile: QualityProfile) -> Result<Self, CodecError> {
Ok(Self {
codec_id: profile.codec,
frame_duration_ms: profile.frame_duration_ms,
})
}
/// Expected number of 8 kHz PCM output samples per frame.
pub fn frame_samples(&self) -> usize {
(8_000 * self.frame_duration_ms as usize) / 1000
}
}
impl AudioDecoder for Codec2Decoder {
fn decode(&mut self, _encoded: &[u8], _pcm: &mut [i16]) -> Result<usize, CodecError> {
Err(CodecError::DecodeFailed(
"codec2-sys not yet linked".to_string(),
))
}
fn decode_lost(&mut self, pcm: &mut [i16]) -> Result<usize, CodecError> {
let samples = self.frame_samples();
let n = samples.min(pcm.len());
// Fill with silence as basic PLC
pcm[..n].fill(0);
Ok(n)
}
fn codec_id(&self) -> CodecId {
self.codec_id
}
fn set_profile(&mut self, profile: QualityProfile) -> Result<(), CodecError> {
match profile.codec {
CodecId::Codec2_3200 | CodecId::Codec2_1200 => {
self.codec_id = profile.codec;
self.frame_duration_ms = profile.frame_duration_ms;
Ok(())
}
other => Err(CodecError::UnsupportedTransition {
from: self.codec_id,
to: other,
}),
}
}
}

View File

@@ -0,0 +1,66 @@
//! Codec2 encoder — stub implementation.
//!
//! Codec2 operates at 8 kHz mono. Resampling from 48 kHz is handled
//! externally (see `resample.rs` and `AdaptiveCodec`).
//!
//! This is a stub that returns an error on encode. When `codec2-sys`
//! is linked, replace the body of `encode()` with actual FFI calls.
use wzp_proto::{AudioEncoder, CodecError, CodecId, QualityProfile};
/// Stub Codec2 encoder implementing `AudioEncoder`.
///
/// Currently returns `CodecError::EncodeFailed` for all encode operations.
/// The structure is ready for drop-in replacement once `codec2-sys` is available.
pub struct Codec2Encoder {
codec_id: CodecId,
frame_duration_ms: u8,
}
impl Codec2Encoder {
/// Create a new stub Codec2 encoder.
pub fn new(profile: QualityProfile) -> Result<Self, CodecError> {
Ok(Self {
codec_id: profile.codec,
frame_duration_ms: profile.frame_duration_ms,
})
}
/// Expected number of 8 kHz PCM samples per frame.
pub fn frame_samples(&self) -> usize {
(8_000 * self.frame_duration_ms as usize) / 1000
}
}
impl AudioEncoder for Codec2Encoder {
fn encode(&mut self, _pcm: &[i16], _out: &mut [u8]) -> Result<usize, CodecError> {
Err(CodecError::EncodeFailed(
"codec2-sys not yet linked".to_string(),
))
}
fn codec_id(&self) -> CodecId {
self.codec_id
}
fn set_profile(&mut self, profile: QualityProfile) -> Result<(), CodecError> {
match profile.codec {
CodecId::Codec2_3200 | CodecId::Codec2_1200 => {
self.codec_id = profile.codec;
self.frame_duration_ms = profile.frame_duration_ms;
Ok(())
}
other => Err(CodecError::UnsupportedTransition {
from: self.codec_id,
to: other,
}),
}
}
fn max_frame_bytes(&self) -> usize {
// Codec2 3200bps @ 20ms = 64 bits = 8 bytes
// Codec2 1200bps @ 40ms = 48 bits = 6 bytes
// Allow generous headroom.
16
}
}

View File

@@ -0,0 +1,42 @@
//! WarzonePhone Codec Layer
//!
//! Provides audio encoding/decoding with adaptive codec switching:
//! - Opus (24kbps / 16kbps / 6kbps) for normal to degraded conditions
//! - Codec2 (3200bps / 1200bps) via C bindings for catastrophic conditions
//!
//! ## Usage
//!
//! Use the factory functions [`create_encoder`] and [`create_decoder`] to get
//! trait-object encoders/decoders that handle adaptive switching internally.
pub mod adaptive;
pub mod codec2_dec;
pub mod codec2_enc;
pub mod opus_dec;
pub mod opus_enc;
pub mod resample;
pub use adaptive::{AdaptiveDecoder, AdaptiveEncoder};
pub use wzp_proto::{AudioDecoder, AudioEncoder, CodecId, QualityProfile};
/// Create an adaptive encoder starting at the given quality profile.
///
/// The returned encoder accepts 48 kHz mono PCM regardless of the active
/// codec; resampling is handled internally when Codec2 is selected.
pub fn create_encoder(profile: QualityProfile) -> Box<dyn AudioEncoder> {
Box::new(
AdaptiveEncoder::new(profile)
.expect("failed to create adaptive encoder"),
)
}
/// Create an adaptive decoder starting at the given quality profile.
///
/// The returned decoder always produces 48 kHz mono PCM; upsampling from
/// Codec2's native 8 kHz is handled internally.
pub fn create_decoder(profile: QualityProfile) -> Box<dyn AudioDecoder> {
Box::new(
AdaptiveDecoder::new(profile)
.expect("failed to create adaptive decoder"),
)
}

View File

@@ -0,0 +1,93 @@
//! Opus decoder wrapping the `audiopus` crate.
use audiopus::coder::Decoder;
use audiopus::{Channels, MutSignals, SampleRate};
use audiopus::packet::Packet;
use wzp_proto::{AudioDecoder, CodecError, CodecId, QualityProfile};
/// Opus decoder implementing `AudioDecoder`.
///
/// Operates at 48 kHz mono output.
pub struct OpusDecoder {
inner: Decoder,
codec_id: CodecId,
frame_duration_ms: u8,
}
// SAFETY: Same reasoning as OpusEncoder — exclusive access via &mut self.
unsafe impl Sync for OpusDecoder {}
impl OpusDecoder {
/// Create a new Opus decoder for the given quality profile.
pub fn new(profile: QualityProfile) -> Result<Self, CodecError> {
let decoder = Decoder::new(SampleRate::Hz48000, Channels::Mono)
.map_err(|e| CodecError::DecodeFailed(format!("opus decoder init: {e}")))?;
Ok(Self {
inner: decoder,
codec_id: profile.codec,
frame_duration_ms: profile.frame_duration_ms,
})
}
/// Expected number of output PCM samples per frame.
pub fn frame_samples(&self) -> usize {
(48_000 * self.frame_duration_ms as usize) / 1000
}
}
impl AudioDecoder for OpusDecoder {
fn decode(&mut self, encoded: &[u8], pcm: &mut [i16]) -> Result<usize, CodecError> {
let expected = self.frame_samples();
if pcm.len() < expected {
return Err(CodecError::DecodeFailed(format!(
"output buffer too small: need {expected}, got {}",
pcm.len()
)));
}
let packet = Packet::try_from(encoded)
.map_err(|e| CodecError::DecodeFailed(format!("invalid packet: {e}")))?;
let signals = MutSignals::try_from(pcm)
.map_err(|e| CodecError::DecodeFailed(format!("output signals: {e}")))?;
let n = self
.inner
.decode(Some(packet), signals, false)
.map_err(|e| CodecError::DecodeFailed(format!("opus decode: {e}")))?;
Ok(n)
}
fn decode_lost(&mut self, pcm: &mut [i16]) -> Result<usize, CodecError> {
let expected = self.frame_samples();
if pcm.len() < expected {
return Err(CodecError::DecodeFailed(format!(
"output buffer too small: need {expected}, got {}",
pcm.len()
)));
}
let signals = MutSignals::try_from(pcm)
.map_err(|e| CodecError::DecodeFailed(format!("output signals: {e}")))?;
let n = self
.inner
.decode(None, signals, false)
.map_err(|e| CodecError::DecodeFailed(format!("opus PLC: {e}")))?;
Ok(n)
}
fn codec_id(&self) -> CodecId {
self.codec_id
}
fn set_profile(&mut self, profile: QualityProfile) -> Result<(), CodecError> {
match profile.codec {
CodecId::Opus24k | CodecId::Opus16k | CodecId::Opus6k => {
self.codec_id = profile.codec;
self.frame_duration_ms = profile.frame_duration_ms;
Ok(())
}
other => Err(CodecError::UnsupportedTransition {
from: self.codec_id,
to: other,
}),
}
}
}

View File

@@ -0,0 +1,109 @@
//! Opus encoder wrapping the `audiopus` crate.
use audiopus::coder::Encoder;
use audiopus::{Application, Bitrate, Channels, SampleRate, Signal};
use tracing::debug;
use wzp_proto::{AudioEncoder, CodecError, CodecId, QualityProfile};
/// Opus encoder implementing `AudioEncoder`.
///
/// Operates at 48 kHz mono. Supports frame sizes of 20 ms (960 samples)
/// and 40 ms (1920 samples).
pub struct OpusEncoder {
inner: Encoder,
codec_id: CodecId,
frame_duration_ms: u8,
}
// SAFETY: OpusEncoder is only used via `&mut self` methods. The inner
// audiopus Encoder contains a raw pointer that is !Sync, but we never
// share it across threads without exclusive access.
unsafe impl Sync for OpusEncoder {}
impl OpusEncoder {
/// Create a new Opus encoder for the given quality profile.
pub fn new(profile: QualityProfile) -> Result<Self, CodecError> {
let encoder = Encoder::new(SampleRate::Hz48000, Channels::Mono, Application::Voip)
.map_err(|e| CodecError::EncodeFailed(format!("opus encoder init: {e}")))?;
let mut enc = Self {
inner: encoder,
codec_id: profile.codec,
frame_duration_ms: profile.frame_duration_ms,
};
enc.apply_bitrate(profile.codec)?;
enc.set_inband_fec(true);
enc.set_dtx(true);
// Voice signal type hint for better compression
enc.inner
.set_signal(Signal::Voice)
.map_err(|e| CodecError::EncodeFailed(format!("set signal: {e}")))?;
Ok(enc)
}
fn apply_bitrate(&mut self, codec: CodecId) -> Result<(), CodecError> {
let bps = codec.bitrate_bps() as i32;
self.inner
.set_bitrate(Bitrate::BitsPerSecond(bps))
.map_err(|e| CodecError::EncodeFailed(format!("set bitrate: {e}")))?;
debug!(bitrate_bps = bps, "opus encoder bitrate set");
Ok(())
}
/// Expected number of PCM samples per frame at current settings.
pub fn frame_samples(&self) -> usize {
(48_000 * self.frame_duration_ms as usize) / 1000
}
}
impl AudioEncoder for OpusEncoder {
fn encode(&mut self, pcm: &[i16], out: &mut [u8]) -> Result<usize, CodecError> {
let expected = self.frame_samples();
if pcm.len() != expected {
return Err(CodecError::EncodeFailed(format!(
"expected {expected} samples, got {}",
pcm.len()
)));
}
let n = self
.inner
.encode(pcm, out)
.map_err(|e| CodecError::EncodeFailed(format!("opus encode: {e}")))?;
Ok(n)
}
fn codec_id(&self) -> CodecId {
self.codec_id
}
fn set_profile(&mut self, profile: QualityProfile) -> Result<(), CodecError> {
match profile.codec {
CodecId::Opus24k | CodecId::Opus16k | CodecId::Opus6k => {
self.codec_id = profile.codec;
self.frame_duration_ms = profile.frame_duration_ms;
self.apply_bitrate(profile.codec)?;
Ok(())
}
other => Err(CodecError::UnsupportedTransition {
from: self.codec_id,
to: other,
}),
}
}
fn max_frame_bytes(&self) -> usize {
// Opus max packet for mono voice: ~500 bytes is generous.
// For 40ms at 24kbps: ~120 bytes typical, but we allow headroom.
512
}
fn set_inband_fec(&mut self, enabled: bool) {
let _ = self.inner.set_inband_fec(enabled);
}
fn set_dtx(&mut self, enabled: bool) {
let _ = self.inner.set_dtx(enabled);
}
}

View File

@@ -0,0 +1,82 @@
//! Simple linear resampler for 48 kHz <-> 8 kHz conversion.
//!
//! These are basic implementations suitable for voice. For higher quality,
//! replace with the `rubato` crate later.
/// Downsample from 48 kHz to 8 kHz (6:1 decimation with averaging).
///
/// Each output sample is the average of 6 consecutive input samples,
/// providing basic anti-aliasing via a box filter.
pub fn resample_48k_to_8k(input: &[i16]) -> Vec<i16> {
const RATIO: usize = 6;
let out_len = input.len() / RATIO;
let mut output = Vec::with_capacity(out_len);
for chunk in input.chunks_exact(RATIO) {
let sum: i32 = chunk.iter().map(|&s| s as i32).sum();
output.push((sum / RATIO as i32) as i16);
}
output
}
/// Upsample from 8 kHz to 48 kHz (1:6 interpolation with linear interp).
///
/// Linearly interpolates between each pair of input samples to produce
/// 6 output samples per input sample.
pub fn resample_8k_to_48k(input: &[i16]) -> Vec<i16> {
const RATIO: usize = 6;
if input.is_empty() {
return Vec::new();
}
let out_len = input.len() * RATIO;
let mut output = Vec::with_capacity(out_len);
for i in 0..input.len() {
let current = input[i] as i32;
let next = if i + 1 < input.len() {
input[i + 1] as i32
} else {
current // hold last sample
};
for j in 0..RATIO {
let interp = current + (next - current) * j as i32 / RATIO as i32;
output.push(interp as i16);
}
}
output
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn roundtrip_length() {
// 960 samples at 48kHz (20ms) -> 160 samples at 8kHz -> 960 samples at 48kHz
let input_48k = vec![0i16; 960];
let down = resample_48k_to_8k(&input_48k);
assert_eq!(down.len(), 160);
let up = resample_8k_to_48k(&down);
assert_eq!(up.len(), 960);
}
#[test]
fn dc_signal_preserved() {
// A constant signal should survive resampling
let input = vec![1000i16; 960];
let down = resample_48k_to_8k(&input);
assert!(down.iter().all(|&s| s == 1000));
let up = resample_8k_to_48k(&down);
assert!(up.iter().all(|&s| s == 1000));
}
#[test]
fn empty_input() {
assert!(resample_48k_to_8k(&[]).is_empty());
assert!(resample_8k_to_48k(&[]).is_empty());
}
}