Skip to content

the ultimate vocal synthesizer - sing, speak, beatbox, and more

Rust

Voice oversampling keeps the airway above 60 kHz: a 16 kHz (Bluetooth headset) context went NaN, silent with no error

Jameson Hodge186b75a1a8cf
diff --git a/AGENTS.md b/AGENTS.md
index 1d06ad4..b42c69c 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -32,6 +32,9 @@ and the CLI) under a React UI. See the README.
- **The engine's contract** (`docs/rust-engine.md`): transcendental math only through
`chimera::math`, no allocation while rendering, output independent of block size. `cargo test`
enforces each.
+- **The voice must hold at any device sample rate** (a Bluetooth headset runs the page at 16 kHz,
+ and an airway simulated too slowly goes NaN: silence, no error). Check:
+ `cargo test -p chimera --test engine the_voice_holds`.
- **Any change to the sound or the plans fails the golden tests, by design.** Re-bless with
`CHIMERA_BLESS=1 cargo test --workspace --no-fail-fast`, run twice. Human-voice audio, plans
(including the beat sounds' gestures and the beat player), the tract geometry and the human
diff --git a/crates/chimera-wasm/src/api.rs b/crates/chimera-wasm/src/api.rs
index 6174d63..46975da 100644
--- a/crates/chimera-wasm/src/api.rs
+++ b/crates/chimera-wasm/src/api.rs
@@ -64,7 +64,7 @@ fn dispatch(st: &mut State, req: &Value) -> Res {
// the main-thread mirror of the worklet's airway: same topology and node counts
let body: BodyMix = arg(req, "body")?;
let fs: f64 = arg(req, "sampleRate")?;
- let m = if fs > 60000.0 { 1.0 } else { 2.0 };
+ let m = chimera::voice::human_voice::oversample_for(fs) as f64;
let i = match st.models.iter().position(|x| x.body == body && x.fs == fs * m) {
Some(i) => i,
None => {
diff --git a/crates/chimera/src/engine.rs b/crates/chimera/src/engine.rs
index 2cfd051..a703c88 100644
--- a/crates/chimera/src/engine.rs
+++ b/crates/chimera/src/engine.rs
@@ -15,7 +15,7 @@ use crate::control::score::{Gesture, GestureScore};
use crate::creatures::types::CreatureDef;
use crate::creatures::voice::{CreatureTelemetry, CreatureVoice};
use crate::phonetics::beatbox::{BeatStep, MAX_SOUND_GESTURES};
-use crate::voice::human_voice::{HumanVoice, SourcePatch, VoiceTelemetry};
+use crate::voice::human_voice::{HumanVoice, SourcePatch, VoiceTelemetry, oversample_for};
/// Samples per sub-block: the browser's render quantum, kept for every host.
pub const SUB_BLOCK: usize = 128;
@@ -66,7 +66,7 @@ impl Engine {
}
fn make_voice(fs: f64, body: BodyMix) -> HumanVoice {
- let mut v = HumanVoice::new(fs, body.body(), if fs > 60000.0 { 1 } else { 2 });
+ let mut v = HumanVoice::new(fs, body.body(), oversample_for(fs));
v.set_source(&SourcePatch::f0(default_f0(body)));
v
}
diff --git a/crates/chimera/src/voice/human_voice.rs b/crates/chimera/src/voice/human_voice.rs
index 9092506..cf1b655 100644
--- a/crates/chimera/src/voice/human_voice.rs
+++ b/crates/chimera/src/voice/human_voice.rs
@@ -198,6 +198,15 @@ pub struct HumanVoice {
pub max_penetration: f64,
}
+/// How many times the airway is oversampled at this output rate: the smallest factor that puts
+/// the simulation above 60 kHz (2× at 44.1 and 48 kHz, 1× at 88.2 and 96 kHz). The tubes keep a
+/// minimum section count, so a lower simulation rate makes the sections shorter than one step of
+/// sound and the airway blows up: at 2× a 16 kHz output (a Bluetooth headset's) went NaN within a
+/// second, and 24 kHz ran at the limiter ceiling (issue #33).
+pub fn oversample_for(fs_out: f64) -> usize {
+ js_max(1.0, (60000.0 / fs_out).floor() + 1.0) as usize
+}
+
impl HumanVoice {
/// A voice rendering at `fs_out`, simulating at `fs_out · oversample`.
pub fn new(fs_out: f64, body: HumanBody, oversample: usize) -> Self {
@@ -268,9 +277,9 @@ impl HumanVoice {
v
}
- /// The defaults the browser uses: male body, 2× oversampling below 60 kHz.
+ /// The defaults the browser uses: male body, oversampled by [`oversample_for`].
pub fn with_defaults(fs_out: f64) -> Self {
- Self::new(fs_out, BODY_MALE, if fs_out > 60000.0 { 1 } else { 2 })
+ Self::new(fs_out, BODY_MALE, oversample_for(fs_out))
}
pub fn set_articulation(&mut self, values: &ArticPatch, tip_free: Option<bool>) {
diff --git a/crates/chimera/tests/engine.rs b/crates/chimera/tests/engine.rs
index 4f29221..6ee60b0 100644
--- a/crates/chimera/tests/engine.rs
+++ b/crates/chimera/tests/engine.rs
@@ -85,3 +85,31 @@ fn output_does_not_depend_on_the_host_block_size() {
assert_eq!(first, None, "block size {block}: differs from 128-sample blocks at sample {first:?}");
}
}
+
+/// A held "ah" from the start button: two seconds at this output rate, its RMS over the second.
+fn held_voice_rms(fs: f64) -> f64 {
+ let mut e = Engine::new(fs);
+ e.set_gate(1.0);
+ let mut out = vec![0.0f32; 2 * fs as usize];
+ for chunk in out.chunks_mut(128) {
+ e.render(chunk);
+ }
+ let tail = &out[fs as usize..];
+ assert!(tail.iter().all(|x| x.is_finite()), "{fs} Hz: the voice went NaN");
+ (tail.iter().map(|&x| (x as f64) * (x as f64)).sum::<f64>() / tail.len() as f64).sqrt()
+}
+
+#[test]
+fn the_voice_holds_at_a_bluetooth_headsets_sample_rate() {
+ // Issue #33: the browser's context runs at the output device's rate. 2x oversampling put a
+ // 16 kHz device's airway at 32 kHz, below its stable rate: NaN within a second, then silence.
+ // 24 kHz ran at the limiter ceiling. Each rate now sounds like 48 kHz does.
+ let reference = held_voice_rms(48000.0);
+ for fs in [8000.0, 16000.0, 22050.0, 24000.0, 32000.0] {
+ let rms = held_voice_rms(fs);
+ assert!(rms > 0.5 * reference && rms < 2.0 * reference, "{fs} Hz: RMS {rms:.3}, 48 kHz {reference:.3}");
+ }
+ // the usual rates simulate as before (the golden fixtures stand)
+ use chimera::voice::human_voice::oversample_for;
+ assert_eq!([44100.0, 48000.0, 60000.0, 88200.0, 96000.0].map(oversample_for), [2, 2, 2, 1, 1]);
+}