Files
VoelinClient/crates/tsc-audio/src/resample.rs
T
Claude 90e05723da M2: tsc-audio, tsc-store and tsctl voice
- tsc-audio: Opus voice/music encoder producing TeamSpeak voice packets,
  framer, rubato resampling, WAV I/O, tone/Goertzel helpers, cpal
  capture/playback over lock-free ring buffers; the vendored
  AudioHandler is the jitter buffer/mixer.
- tsc-store: SQLite (bundled, WAL, 0600) with migrations for identities,
  bookmarks, settings and paged chat history; Secrets trait with memory
  and OS keyring backends.
- tsctl voice send (WAV or tone, real time) / record (mix to WAV,
  --expect-tone check). The smoke test now includes a voice round-trip:
  1 kHz tone ratio ~0.95 on TS3 and TS6.
- Dev profile: no debug info for dependencies (target/ 16 GB -> 2.4 GB).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NJvYyRmTjKcb9i8eyYXK5G
2026-09-25 13:56:01 +00:00

50 lines
1.6 KiB
Rust

//! Sample-rate conversion.
use rubato::audioadapter_buffers::direct::InterleavedSlice;
use rubato::{Fft, FixedSync, Resampler};
use crate::{Error, Result};
/// Resample a whole interleaved clip from `from` Hz to `to` Hz.
pub fn resample_all(samples: &[f32], channels: usize, from: u32, to: u32) -> Result<Vec<f32>> {
if from == to || samples.is_empty() {
return Ok(samples.to_vec());
}
if channels == 0 || !samples.len().is_multiple_of(channels) {
return Err(Error::Invalid(format!("{} samples for {channels} channels", samples.len())));
}
let frames = samples.len() / channels;
let mut resampler =
Fft::<f32>::new(from as usize, to as usize, 1024, channels, FixedSync::Input)
.map_err(|e| Error::Resample(e.to_string()))?;
let input = InterleavedSlice::new(samples, channels, frames)
.map_err(|e| Error::Resample(e.to_string()))?;
let output =
resampler.process_all(&input, frames, None).map_err(|e| Error::Resample(e.to_string()))?;
Ok(output.take_data())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::pcm;
#[test]
fn keeps_tone_across_rates() {
// 1 kHz at 44.1 kHz, resampled to 48 kHz.
let n = 44_100;
let tone: Vec<f32> = (0..n)
.map(|i| 0.5 * (std::f32::consts::TAU * 1000.0 * i as f32 / 44_100.0).sin())
.collect();
let out = resample_all(&tone, 1, 44_100, 48_000).unwrap();
assert!((out.len() as i64 - 48_000).abs() < 100, "len {}", out.len());
assert!(pcm::tone_ratio(&out, 1000.0, 48_000) > 0.95);
}
#[test]
fn passthrough_and_errors() {
assert_eq!(resample_all(&[1.0, 2.0], 1, 48_000, 48_000).unwrap(), vec![1.0, 2.0]);
assert!(resample_all(&[1.0, 2.0, 3.0], 2, 44_100, 48_000).is_err());
}
}