- 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
102 lines
3.3 KiB
Rust
102 lines
3.3 KiB
Rust
//! PCM constants and helpers.
|
|
|
|
use std::f32::consts::TAU;
|
|
|
|
/// The sample rate TeamSpeak's Opus codecs run at.
|
|
pub const SAMPLE_RATE: u32 = 48_000;
|
|
/// Samples per channel in one 20 ms frame.
|
|
pub const FRAME_SAMPLES: usize = SAMPLE_RATE as usize / 50;
|
|
|
|
/// A sine tone, mono, at [`SAMPLE_RATE`].
|
|
pub fn sine(freq: f32, seconds: f32, amplitude: f32) -> Vec<f32> {
|
|
let n = (seconds * SAMPLE_RATE as f32) as usize;
|
|
(0..n).map(|i| amplitude * (TAU * freq * i as f32 / SAMPLE_RATE as f32).sin()).collect()
|
|
}
|
|
|
|
/// Average interleaved channels into mono.
|
|
pub fn to_mono(interleaved: &[f32], channels: usize) -> Vec<f32> {
|
|
if channels <= 1 {
|
|
return interleaved.to_vec();
|
|
}
|
|
interleaved.chunks_exact(channels).map(|f| f.iter().sum::<f32>() / channels as f32).collect()
|
|
}
|
|
|
|
/// Duplicate mono samples into `channels` interleaved channels.
|
|
pub fn from_mono(mono: &[f32], channels: usize) -> Vec<f32> {
|
|
mono.iter().flat_map(|&s| std::iter::repeat_n(s, channels)).collect()
|
|
}
|
|
|
|
/// Power of one frequency in `samples` (Goertzel algorithm), normalised so a
|
|
/// full-scale sine at `freq` gives about 0.25 per sample squared.
|
|
pub fn goertzel_power(samples: &[f32], freq: f32, rate: u32) -> f32 {
|
|
if samples.is_empty() {
|
|
return 0.0;
|
|
}
|
|
let coeff = 2.0 * (TAU * freq / rate as f32).cos();
|
|
let (mut s1, mut s2) = (0.0f32, 0.0f32);
|
|
for &x in samples {
|
|
let s = x + coeff * s1 - s2;
|
|
s2 = s1;
|
|
s1 = s;
|
|
}
|
|
let power = s1 * s1 + s2 * s2 - coeff * s1 * s2;
|
|
power / (samples.len() as f32 * samples.len() as f32)
|
|
}
|
|
|
|
/// Mean square of the signal.
|
|
pub fn energy(samples: &[f32]) -> f32 {
|
|
if samples.is_empty() {
|
|
return 0.0;
|
|
}
|
|
samples.iter().map(|s| s * s).sum::<f32>() / samples.len() as f32
|
|
}
|
|
|
|
/// Fraction of the signal energy at `freq`: close to 1 for a clean tone,
|
|
/// close to 0 for silence or noise.
|
|
pub fn tone_ratio(samples: &[f32], freq: f32, rate: u32) -> f32 {
|
|
let e = energy(samples);
|
|
if e < 1e-9 {
|
|
return 0.0;
|
|
}
|
|
// A sine of amplitude a has energy a²/2 and Goertzel power a²/4.
|
|
(2.0 * goertzel_power(samples, freq, rate) / e).min(1.0)
|
|
}
|
|
|
|
/// Like [`tone_ratio`], but only over the 100 ms windows that are not silent,
|
|
/// so leading and trailing silence in a recording do not dilute the result.
|
|
pub fn voiced_tone_ratio(samples: &[f32], freq: f32, rate: u32) -> f32 {
|
|
let window = rate as usize / 10;
|
|
let ratios: Vec<f32> = samples
|
|
.chunks_exact(window)
|
|
.filter(|w| energy(w) > 1e-4)
|
|
.map(|w| tone_ratio(w, freq, rate))
|
|
.collect();
|
|
if ratios.is_empty() { 0.0 } else { ratios.iter().sum::<f32>() / ratios.len() as f32 }
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn detects_tone() {
|
|
let tone = sine(1000.0, 0.5, 0.5);
|
|
assert!(tone_ratio(&tone, 1000.0, SAMPLE_RATE) > 0.95);
|
|
assert!(tone_ratio(&tone, 440.0, SAMPLE_RATE) < 0.05);
|
|
assert_eq!(tone_ratio(&[0.0; 100], 1000.0, SAMPLE_RATE), 0.0);
|
|
|
|
let mut padded = vec![0.0; SAMPLE_RATE as usize];
|
|
padded.extend(&tone);
|
|
padded.extend(vec![0.0; SAMPLE_RATE as usize]);
|
|
assert!(tone_ratio(&padded, 1000.0, SAMPLE_RATE) < 0.5);
|
|
assert!(voiced_tone_ratio(&padded, 1000.0, SAMPLE_RATE) > 0.95);
|
|
assert_eq!(voiced_tone_ratio(&[0.0; 48_000], 1000.0, SAMPLE_RATE), 0.0);
|
|
}
|
|
|
|
#[test]
|
|
fn channel_conversion() {
|
|
assert_eq!(to_mono(&[1.0, 3.0, 2.0, 4.0], 2), vec![2.0, 3.0]);
|
|
assert_eq!(from_mono(&[1.0, 2.0], 2), vec![1.0, 1.0, 2.0, 2.0]);
|
|
}
|
|
}
|