A short two-tone chime, made in code, plays right before each voice answer starts and at once for a wake word said alone while Jarvis is free; the question that joins it gets no second one. call.earcon turns it off. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
66 lines
2.4 KiB
Rust
66 lines
2.4 KiB
Rust
//! The short "I heard you" sound (`app/src/conversation.spec.md`, "Earcon"): two rising bell tones,
|
|
//! made here so the app ships no audio file. 48 kHz stereo PCM16, the format the player takes.
|
|
|
|
use std::f32::consts::TAU;
|
|
use std::sync::OnceLock;
|
|
|
|
const RATE: f32 = 48_000.0;
|
|
/// Peak level, well below full scale so the cue never sounds louder than speech.
|
|
pub const PEAK: f32 = 0.3;
|
|
|
|
/// (start s, frequency Hz, length s) of each tone: "띠" then "링".
|
|
const TONES: [(f32, f32, f32); 2] = [(0.0, 1318.5, 0.11), (0.09, 1975.5, 0.22)];
|
|
|
|
/// The earcon as 48 kHz stereo PCM16 (little-endian), made once.
|
|
pub fn pcm() -> &'static [u8] {
|
|
static PCM: OnceLock<Vec<u8>> = OnceLock::new();
|
|
PCM.get_or_init(make)
|
|
}
|
|
|
|
fn make() -> Vec<u8> {
|
|
let total = TONES.iter().map(|(s, _, l)| s + l).fold(0.0, f32::max);
|
|
let frames = (total * RATE) as usize;
|
|
let mut out = Vec::with_capacity(frames * 4);
|
|
for i in 0..frames {
|
|
let t = i as f32 / RATE;
|
|
let mut v = 0.0;
|
|
for (start, freq, len) in TONES {
|
|
let u = t - start;
|
|
if !(0.0..len).contains(&u) {
|
|
continue;
|
|
}
|
|
// 5 ms rise and a bell-like decay that reaches silence at the tone's end: no clicks.
|
|
let rise = (u / 0.005).min(1.0);
|
|
let fall = (1.0 - u / len).powi(2);
|
|
let tone = (TAU * freq * u).sin() + 0.25 * (TAU * 2.0 * freq * u).sin();
|
|
v += rise * fall * tone / 1.25;
|
|
}
|
|
let s = (v.clamp(-1.0, 1.0) * PEAK * i16::MAX as f32) as i16;
|
|
let b = s.to_le_bytes();
|
|
out.extend_from_slice(&b);
|
|
out.extend_from_slice(&b);
|
|
}
|
|
out
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
fn samples() -> Vec<i16> {
|
|
pcm().chunks_exact(2).map(|b| i16::from_le_bytes([b[0], b[1]])).collect()
|
|
}
|
|
|
|
#[test]
|
|
fn is_short_stereo_audio_that_starts_and_ends_silent_and_stays_below_its_peak() {
|
|
let s = samples();
|
|
let seconds = s.len() as f32 / 2.0 / RATE;
|
|
assert!((0.2..0.5).contains(&seconds), "{seconds} s");
|
|
assert!(s.chunks_exact(2).all(|lr| lr[0] == lr[1]), "both channels carry the same sound");
|
|
let limit = (PEAK * i16::MAX as f32) as u16 + 1;
|
|
let loudest = s.iter().map(|v| v.unsigned_abs()).max().unwrap();
|
|
assert!(loudest <= limit && loudest > limit / 2, "loudest {loudest}");
|
|
assert!(s[..4].iter().all(|v| v.abs() < 200) && s[s.len() - 4..].iter().all(|v| v.abs() < 200));
|
|
}
|
|
}
|