the ultimate vocal synthesizer - sing, speak, beatbox, and more
Rust
Creature denormals: the breath gate, DC blocker and glottis are zeroed at control rate once decayed (40-100% of blocks were denormal after a creature stopped)
Jameson Hodgec7ddee5623b9
diff --git a/crates/chimera/src/creatures/alligator.rs b/crates/chimera/src/creatures/alligator.rsindex d1018f0..3ac44d1 100644--- a/crates/chimera/src/creatures/alligator.rs+++ b/crates/chimera/src/creatures/alligator.rs@@ -70,6 +70,9 @@ impl CreatureSim for Alligator {step_glottis(&mut self.a);self.a.net.step()}+ fn flush_tiny(&mut self) {+ self.a.glottis_src.flush_tiny();+ }fn source_state(&self, out: &mut Vec<(&'static str, f64)>) {out.clear();out.push(("glottis", js_min(1.0, self.a.glottis_src.area / 1.0)));diff --git a/crates/chimera/src/creatures/elephant.rs b/crates/chimera/src/creatures/elephant.rsindex f393fae..f325803 100644--- a/crates/chimera/src/creatures/elephant.rs+++ b/crates/chimera/src/creatures/elephant.rs@@ -105,6 +105,9 @@ impl CreatureSim for Elephant {}self.a.net.step()}+ fn flush_tiny(&mut self) {+ self.a.glottis_src.flush_tiny();+ }fn source_state(&self, out: &mut Vec<(&'static str, f64)>) {out.clear();out.push(("glottis", js_min(1.0, self.a.glottis_src.area / 1.6)));diff --git a/crates/chimera/src/creatures/seal.rs b/crates/chimera/src/creatures/seal.rsindex b269568..8707131 100644--- a/crates/chimera/src/creatures/seal.rs+++ b/crates/chimera/src/creatures/seal.rs@@ -74,6 +74,9 @@ impl CreatureSim for Seal {step_glottis(&mut self.a);self.a.net.step()}+ fn flush_tiny(&mut self) {+ self.a.glottis_src.flush_tiny();+ }fn source_state(&self, out: &mut Vec<(&'static str, f64)>) {out.clear();out.push(("glottis", js_min(1.0, self.a.glottis_src.area / 0.3)));diff --git a/crates/chimera/src/creatures/types.rs b/crates/chimera/src/creatures/types.rsindex 329b973..7010f3b 100644--- a/crates/chimera/src/creatures/types.rs+++ b/crates/chimera/src/creatures/types.rs@@ -236,6 +236,9 @@ pub trait CreatureSim: Send {}/// discrete events from calls (e.g. clicks, snaps), by index in [`CreatureDef::events`]fn trigger(&mut self, _event: usize, _amount: f64) {}+ /// zero the source's states that have decayed to nothing (control rate; the airway network+ /// does its own: see `AirwayNetwork::flush_tiny`)+ fn flush_tiny(&mut self) {}}#[derive(Serialize)]diff --git a/crates/chimera/src/creatures/voice.rs b/crates/chimera/src/creatures/voice.rsindex d175fed..4db0f54 100644--- a/crates/chimera/src/creatures/voice.rs+++ b/crates/chimera/src/creatures/voice.rs@@ -9,6 +9,7 @@use super::types::{CallTarget, CreatureDef, CreatureSim, Curve, GateTimes};use crate::airway::decimator::Decimator;+use crate::airway::network::tiny;use crate::math::{PI, exp, js_max, js_min, js_round, js_sign, pow, sqrt, tanh};use serde::ser::{SerializeMap, Serializer};use serde::Serialize;@@ -349,6 +350,12 @@ impl CreatureVoice {let gt = self.def.gate_times.unwrap_or(DEFAULT_GATE_TIMES);let tau = if goal > self.gate { gt.attack } else { gt.release };self.gate += (goal - self.gate) * (1.0 - exp(-dt / tau));+ // what dies away must end at zero, not among the denormals (see `AirwayNetwork::flush_tiny`):+ // a breath let go would leave a denormal lung pressure feeding the whole airway+ tiny(&mut self.gate);+ tiny(&mut self.dc_x);+ tiny(&mut self.dc_y);+ self.sim.flush_tiny();self.sim.control(&self.value, self.gate, dt);let every = self.ctrl_every;if let Some(net) = self.sim.net_mut() {diff --git a/crates/chimera/tests/denormals.rs b/crates/chimera/tests/denormals.rsindex 39f1c2f..a667e64 100644--- a/crates/chimera/tests/denormals.rs+++ b/crates/chimera/tests/denormals.rs@@ -131,3 +131,65 @@ fn the_processor_is_never_handed_a_denormal() {assert_eq!(dirty, 0, "{name}: {dirty} of {blocks} blocks computed with denormals");}}++/// The creatures die away the same way once their breath is let go: each, sounding for 3 s (a call+/// where it has one), then left with nothing for 30 s, never hands the processor a denormal.+/// (Before their states were zeroed too, 40-100% of blocks computed with denormals from ~5 s on.)+#[cfg(target_arch = "x86_64")]+fn assert_creature_dies_to_zero(id: &str) {+ use core::arch::asm;+ const DENORMAL_OPERAND: u32 = 0x0002;+ fn mxcsr() -> u32 {+ let mut v = 0u32;+ unsafe { asm!("stmxcsr [{}]", in(reg) &mut v, options(nostack)) };+ v+ }+ fn set_mxcsr(v: u32) {+ unsafe { asm!("ldmxcsr [{}]", in(reg) &v, options(nostack, readonly)) };+ }+ let def = chimera::creatures::creature_by_id(id).expect("a creature");+ let mut e = Engine::new(FS);+ e.set_creature(Some(def), &[]);+ e.set_gate(1.0);+ if !def.calls.is_empty() {+ e.play_call(0, None, None);+ }+ run(&mut e, 3.0);+ e.set_gate(0.0);+ e.stop_call();+ let mut buf = [0.0f32; 128];+ let per_second = (FS / 128.0) as usize;+ for second in 1..=30 {+ let mut dirty = 0usize;+ for _ in 0..per_second {+ set_mxcsr(mxcsr() & !0x3f);+ e.render(&mut buf);+ dirty += (mxcsr() & DENORMAL_OPERAND != 0) as usize;+ }+ assert_eq!(dirty, 0, "{id}, {second} s after letting go: {dirty} of {per_second} blocks computed with denormals");+ }+}++#[cfg(target_arch = "x86_64")]+#[test]+fn the_songbird_dies_to_zero() {+ assert_creature_dies_to_zero("songbird");+}++#[cfg(target_arch = "x86_64")]+#[test]+fn the_seal_dies_to_zero() {+ assert_creature_dies_to_zero("seal");+}++#[cfg(target_arch = "x86_64")]+#[test]+fn the_alligator_dies_to_zero() {+ assert_creature_dies_to_zero("alligator");+}++#[cfg(target_arch = "x86_64")]+#[test]+fn the_elephant_dies_to_zero() {+ assert_creature_dies_to_zero("elephant");+}