feat(app): an earcon tells the speaker Jarvis took the call
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>
This commit is contained in:
@@ -31,6 +31,8 @@ pub struct CallSettings {
|
||||
/// After a wake word said alone, how long the speaker's next utterance may take to start and
|
||||
/// still be joined to it (the pause after "Jarvis," often ends an utterance).
|
||||
pub follow_up: Duration,
|
||||
/// Play the earcon when Jarvis takes a voice call (`call.earcon`).
|
||||
pub earcon: bool,
|
||||
}
|
||||
|
||||
impl Default for CallSettings {
|
||||
@@ -44,6 +46,7 @@ impl Default for CallSettings {
|
||||
designated_no_wake_word: false,
|
||||
stop_words: ["멈춰", "그만", "정지", "stop"].map(String::from).to_vec(),
|
||||
follow_up: Duration::from_millis(1500),
|
||||
earcon: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
//! `Conversation` holds no Discord or worker types: it talks to the voice side through `Voice`, so the
|
||||
//! glue is tested with a fake. `run` drives it from the core's events with a timer for the policy.
|
||||
|
||||
use std::collections::{HashMap, VecDeque};
|
||||
use std::collections::{HashMap, HashSet, VecDeque};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use tokio::sync::mpsc;
|
||||
@@ -28,6 +28,8 @@ pub trait Voice {
|
||||
fn cancel(&self, id: &str);
|
||||
/// Stop all speech at once.
|
||||
fn stop_all(&self);
|
||||
/// Play the earcon now (queued like speech; dropped by a stop).
|
||||
fn earcon(&self);
|
||||
fn ask_addressed(&self, id: &str, user: u64, text: &str) -> bool;
|
||||
fn ask_rank(&self, id: &str, candidates: &[(String, u64, String)]) -> bool;
|
||||
/// Have the brain answer a chat message (not spoken); its text comes as `CoreEvent::ReplyText`.
|
||||
@@ -49,6 +51,9 @@ impl Voice for DiscordCore {
|
||||
fn stop_all(&self) {
|
||||
DiscordCore::stop(self);
|
||||
}
|
||||
fn earcon(&self) {
|
||||
DiscordCore::earcon(self);
|
||||
}
|
||||
fn ask_addressed(&self, id: &str, user: u64, text: &str) -> bool {
|
||||
DiscordCore::ask_addressed(self, id, user, text)
|
||||
}
|
||||
@@ -135,13 +140,16 @@ pub struct Conversation<V: Voice> {
|
||||
/// A wake word said alone, per speaker, waiting for what they say next; the deadline for the next
|
||||
/// utterance to start (None while it runs).
|
||||
wakes: HashMap<u64, (Request, Option<Instant>)>,
|
||||
/// Requests whose earcon already played (a wake word said alone was answered with it).
|
||||
chimed: HashSet<u64>,
|
||||
}
|
||||
|
||||
impl<V: Voice> Conversation<V> {
|
||||
pub fn new(voice: V, calls: CallSettings, policy: PolicySettings) -> Self {
|
||||
Self { voice, calls, policy: Policy::new(policy), next_id: 0, answers: HashMap::new(),
|
||||
by_speech: HashMap::new(), asks: HashMap::new(), missed: VecDeque::new(), chat: ChatCalls::default(),
|
||||
chat_answers: HashMap::new(), started: HashMap::new(), wakes: HashMap::new() }
|
||||
chat_answers: HashMap::new(), started: HashMap::new(), wakes: HashMap::new(),
|
||||
chimed: HashSet::new() }
|
||||
}
|
||||
|
||||
/// Answer the registered chat channels too.
|
||||
@@ -210,11 +218,14 @@ impl<V: Voice> Conversation<V> {
|
||||
self.started.remove(&speaker);
|
||||
}
|
||||
// What follows a wake word said alone is joined to it.
|
||||
let text = match self.wakes.remove(&speaker) {
|
||||
Some((wake, _)) => format!("{} {}", wake.text, f.text),
|
||||
None => f.text.clone(),
|
||||
let (text, chimed) = match self.wakes.remove(&speaker) {
|
||||
Some((wake, _)) => (format!("{} {}", wake.text, f.text), self.chimed.remove(&wake.id)),
|
||||
None => (f.text.clone(), false),
|
||||
};
|
||||
self.next_id += 1;
|
||||
if chimed {
|
||||
self.chimed.insert(self.next_id);
|
||||
}
|
||||
let req = Request { id: self.next_id, speaker, text: text.clone(), direct: false,
|
||||
ended_at: now.checked_sub(Duration::from_millis(f.wait_ms)).unwrap_or(now), arrived_at: now };
|
||||
let call = self.calls.judge(speaker, &text);
|
||||
@@ -226,6 +237,11 @@ impl<V: Voice> Conversation<V> {
|
||||
let a = self.policy.utterance_closed(speaker, now);
|
||||
self.apply(a, now);
|
||||
}
|
||||
// "I heard you" at once, as voice assistants do after their name; only when
|
||||
// Jarvis is free, since a busy Jarvis may not take the question.
|
||||
if self.calls.earcon && self.answers.is_empty() && self.chimed.insert(req.id) {
|
||||
self.voice.earcon();
|
||||
}
|
||||
let deadline = (!going_on).then(|| now + self.calls.follow_up);
|
||||
self.wakes.insert(speaker, (Request { direct: true, ..req }, deadline));
|
||||
return;
|
||||
@@ -295,7 +311,9 @@ impl<V: Voice> Conversation<V> {
|
||||
}
|
||||
CoreEvent::WorkerLost => {
|
||||
self.started.clear();
|
||||
self.wakes.clear();
|
||||
for (_, (req, _)) in self.wakes.drain() {
|
||||
self.chimed.remove(&req.id);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
@@ -306,6 +324,7 @@ impl<V: Voice> Conversation<V> {
|
||||
self.wakes.clear();
|
||||
let a = self.policy.stop();
|
||||
self.apply(a, now);
|
||||
self.chimed.clear(); // nothing is waiting any more
|
||||
for (_, (req, _)) in self.asks.drain().collect::<Vec<_>>() {
|
||||
let a = self.policy.utterance_closed(req.speaker, now);
|
||||
self.apply(a, now);
|
||||
@@ -346,7 +365,13 @@ impl<V: Voice> Conversation<V> {
|
||||
fn apply(&mut self, actions: Vec<Action>, now: Instant) {
|
||||
for action in actions {
|
||||
match action {
|
||||
Action::Answer(r) => match self.voice.answer(r.speaker, &r.text) {
|
||||
Action::Answer(r) => match {
|
||||
// The earcon plays first, then the answer as soon as it is written.
|
||||
if self.calls.earcon && !self.chimed.remove(&r.id) {
|
||||
self.voice.earcon();
|
||||
}
|
||||
self.voice.answer(r.speaker, &r.text)
|
||||
} {
|
||||
Some(sid) => {
|
||||
debug_log!("conversation", "answer req={} speaker={} speech={sid}", r.id, r.speaker);
|
||||
self.by_speech.insert(sid.clone(), r.id);
|
||||
@@ -364,6 +389,7 @@ impl<V: Voice> Conversation<V> {
|
||||
}
|
||||
}
|
||||
Action::Missed(r, reason) => {
|
||||
self.chimed.remove(&r.id);
|
||||
debug_log!("conversation", "missed req={} speaker={} reason={reason:?}", r.id, r.speaker);
|
||||
if self.missed.len() == MISSED_KEPT {
|
||||
self.missed.pop_front();
|
||||
@@ -420,6 +446,7 @@ mod tests {
|
||||
Answer(u64, String),
|
||||
Cancel(String),
|
||||
StopAll,
|
||||
Earcon,
|
||||
Addressed(String, u64),
|
||||
Rank(String, Vec<String>),
|
||||
ChatAnswer(String, u64, String),
|
||||
@@ -444,6 +471,9 @@ mod tests {
|
||||
fn stop_all(&self) {
|
||||
self.did.borrow_mut().push(Did::StopAll);
|
||||
}
|
||||
fn earcon(&self) {
|
||||
self.did.borrow_mut().push(Did::Earcon);
|
||||
}
|
||||
fn ask_addressed(&self, id: &str, user: u64, _text: &str) -> bool {
|
||||
self.did.borrow_mut().push(Did::Addressed(id.into(), user));
|
||||
true
|
||||
@@ -474,10 +504,62 @@ mod tests {
|
||||
|
||||
fn conv(kind: PolicyKind, mode: CallMode) -> (Conversation<Fake>, Fake, Instant) {
|
||||
let f = Fake::default();
|
||||
let calls = CallSettings { mode, ..CallSettings::default() };
|
||||
let calls = CallSettings { mode, earcon: false, ..CallSettings::default() };
|
||||
let policy = PolicySettings { kind, collect: Duration::ZERO, ..PolicySettings::default() };
|
||||
(Conversation::new(f.clone(), calls, policy), f, Instant::now())
|
||||
}
|
||||
fn conv_earcon(kind: PolicyKind) -> (Conversation<Fake>, Fake, Instant) {
|
||||
let f = Fake::default();
|
||||
let policy = PolicySettings { kind, collect: Duration::ZERO, ..PolicySettings::default() };
|
||||
(Conversation::new(f.clone(), CallSettings::default(), policy), f, Instant::now())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_earcon_plays_right_before_each_answer_is_asked_for() {
|
||||
let (mut c, f, t) = conv_earcon(PolicyKind::InOrder);
|
||||
c.on_event(&fin(A, "자비스 날씨 어때", 100), ms(t, 0));
|
||||
assert_eq!(f.take(), vec![Did::Earcon, Did::Answer(A, "자비스 날씨 어때".into())]);
|
||||
c.on_event(&fin(B, "자비스 불 꺼 줘", 100), ms(t, 200));
|
||||
assert!(f.take().is_empty(), "a waiting request gets its earcon when its answer starts");
|
||||
c.on_event(&end("s1", false), ms(t, 3000));
|
||||
assert_eq!(f.take(), vec![Did::Earcon, Did::Answer(B, "자비스 불 꺼 줘".into())]);
|
||||
c.on_event(&fin(B, "오늘 뭐 먹지", 100), ms(t, 3100));
|
||||
assert!(f.take().is_empty(), "no call, no earcon");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_wake_word_alone_gets_the_earcon_at_once_and_its_question_does_not_get_another() {
|
||||
let (mut c, f, t) = conv_earcon(PolicyKind::First);
|
||||
c.on_event(&start(A, 1), ms(t, 0));
|
||||
c.on_event(&fin_utt(A, 1, "자비스"), ms(t, 900));
|
||||
assert_eq!(f.take(), vec![Did::Earcon]);
|
||||
c.on_event(&start(A, 2), ms(t, 1200));
|
||||
c.on_event(&fin_utt(A, 2, "지금 몇 시야?"), ms(t, 3000));
|
||||
assert_eq!(f.take(), vec![Did::Answer(A, "자비스 지금 몇 시야?".into())]);
|
||||
// Nothing followed the next one: it is answered alone, still with one earcon.
|
||||
c.on_event(&end("s1", false), ms(t, 5000));
|
||||
c.on_event(&fin_utt(A, 3, "자비스"), ms(t, 6000));
|
||||
c.tick(ms(t, 6000) + CallSettings::default().follow_up);
|
||||
assert_eq!(f.take(), vec![Did::Earcon, Did::Answer(A, "자비스".into())]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_busy_jarvis_gives_no_earcon_for_a_call_it_does_not_take() {
|
||||
let (mut c, f, t) = conv_earcon(PolicyKind::First);
|
||||
c.on_event(&fin(A, "자비스 날씨", 100), ms(t, 0));
|
||||
assert_eq!(f.take(), vec![Did::Earcon, Did::Answer(A, "자비스 날씨".into())]);
|
||||
c.on_event(&fin(B, "자비스 안녕", 100), ms(t, 100));
|
||||
c.on_event(&fin_utt(B, 2, "자비스"), ms(t, 200));
|
||||
assert!(f.take().is_empty(), "missed while busy, and a wake word alone waits silently");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_earcon_can_be_turned_off() {
|
||||
let (mut c, f, t) = conv(PolicyKind::First, CallMode::WakeWord);
|
||||
c.on_event(&fin_utt(A, 1, "자비스"), ms(t, 0));
|
||||
c.on_event(&fin_utt(A, 2, "안녕"), ms(t, 500));
|
||||
assert_eq!(f.take(), vec![Did::Answer(A, "자비스 안녕".into())]);
|
||||
}
|
||||
fn fin(speaker: u64, text: &str, wait_ms: u64) -> CoreEvent {
|
||||
CoreEvent::Final(HeardFinal { heard: Heard { speaker_id: speaker, speaker_name: "x".into(), utt: 1 },
|
||||
text: text.into(), end_reason: "silence".into(), audio_ms: 1000, wait_ms, stt_ms: 100 })
|
||||
@@ -710,7 +792,8 @@ mod tests {
|
||||
(CH_ALL, ChatChannelCall { everything: true, send: true }),
|
||||
(CH_MUTE, ChatChannelCall { everything: true, send: false }),
|
||||
].into() };
|
||||
let c = Conversation::new(f.clone(), CallSettings::default(), PolicySettings::default()).with_chat(chat);
|
||||
let voice = CallSettings { earcon: false, ..CallSettings::default() };
|
||||
let c = Conversation::new(f.clone(), voice, PolicySettings::default()).with_chat(chat);
|
||||
(c, f, Instant::now())
|
||||
}
|
||||
fn chat(channel: u64, message: u64, author: u64, bot: bool, text: &str) -> CoreEvent {
|
||||
|
||||
@@ -32,6 +32,7 @@ Settings (`settings.json`, section `call`; wake words stay in `voice.wakeWords`)
|
||||
| `call.designatedOnly` | `false` | Answer only designated people. |
|
||||
| `call.designatedNoWakeWord` | `false` | Answer designated people without a wake word. |
|
||||
| `call.stopWords` | `["멈춰", "그만", "정지", "stop"]` | Said together with a wake word, they stop Jarvis in any policy. |
|
||||
| `call.earcon` | `true` | Play the earcon (below) when Jarvis takes a voice call. |
|
||||
| `call.followUpMs` | `1500` | After a wake word said alone, how long the speaker's next utterance may take to start and still be joined to it. 0-5000. |
|
||||
|
||||
Normalisation: Unicode NFKD, lower case, and only letters and digits kept (spaces, punctuation and
|
||||
@@ -68,6 +69,14 @@ connection (`WorkerLost`): the final it waited for will not come, and the restar
|
||||
utterances are not continuations of it. Chat messages are not joined:
|
||||
a message is complete as sent.
|
||||
|
||||
**Earcon.** A short two-tone "띠링" (about 0.3 s, made by the app, `app/src/earcon.rs`) tells the
|
||||
speaker Jarvis heard the call, long before the answer is written: it is the first sound after a call.
|
||||
It plays right before each voice answer starts (the policy's `Answer`), and at once when a wake word
|
||||
said alone is kept waiting while no answer is running (the question that joins it then gets no second
|
||||
earcon). It is queued like speech, so it waits behind speech already queued and a stop drops it. A
|
||||
call the policy does not take (busy, replaced, stopped) gets none, nor does a wake word alone while an
|
||||
answer runs. Chat answers get none. `call.earcon: false` turns it off.
|
||||
|
||||
## Response policy
|
||||
|
||||
Settings (section `response`):
|
||||
@@ -160,7 +169,7 @@ never takes the voice turn and never goes through the response policy.
|
||||
|
||||
`app/src/conversation.rs` runs between the core's events and everything else that reads them (every
|
||||
event is passed on unchanged). It talks to the core through a small `Voice` interface (answer, cancel
|
||||
one request, stop all, ask "addressed", ask "rank"), so the glue is tested with a fake.
|
||||
one request, stop all, earcon, ask "addressed", ask "rank"), so the glue is tested with a fake.
|
||||
|
||||
- `Chat` and `ReplyText`: chat calls, above.
|
||||
- `SpeechStart` opens the speaker's utterance; `SpeakingStopped` marks it ending.
|
||||
@@ -170,7 +179,8 @@ one request, stop all, ask "addressed", ask "rank"), so the glue is tested with
|
||||
goes to the policy. Ask sends `addressed` to the brain; a yes becomes the request, a no (or a failed
|
||||
judgement) closes the utterance, and no answer within 5 s counts as no, also when the answer arrives
|
||||
after that but before the timer ran.
|
||||
- Policy actions: `Answer` → `answer(user, text)` (the brain answers and the answer is spoken; when it
|
||||
- Policy actions: `Answer` → the earcon (unless it already played for this request), then
|
||||
`answer(user, text)` (the brain answers and the answer is spoken; when it
|
||||
cannot be queued the answer is done at once); `StopAnswer` → `cancel` of that answer's speech;
|
||||
`Rank { id }` → `rank` with id `k<id>` and the candidates' request ids; `Missed` → a debug log and
|
||||
the missed list (the last 100: speaker, reason, time and length, never the text).
|
||||
@@ -191,7 +201,8 @@ two letters away, wake word anywhere, stop with and without a wake word, designa
|
||||
modes) and every policy rule above with fake times (settling, ordering by end time, busy, queue limit,
|
||||
interruption, ranking tie, ranking timeout, stop dropping the queue), and the conversation loop with a
|
||||
fake voice side (answering and queueing, a busy drop recorded without text, voice stop, addressed yes,
|
||||
no and timeout, interruption cancelling the right speech, a ranking deciding, end times from
|
||||
no and timeout, the earcon before each answer, once for a wake word alone, none while busy or when
|
||||
off, interruption cancelling the right speech, a ranking deciding, end times from
|
||||
`wait_ms`, and chat: an answer posted as a reply once, per-channel switches, no answers to bots or to
|
||||
a stop, chat wake words and designated people, a failed answer posting nothing). `tests/worker_protocol.rs` checks an answer spoken under its id and both judgements against
|
||||
the real Python worker with a fake brain.
|
||||
|
||||
@@ -414,6 +414,13 @@ impl DiscordCore {
|
||||
Some(id)
|
||||
}
|
||||
|
||||
/// Play the earcon in the voice channel, queued like speech. The request id, or None when not in a
|
||||
/// voice channel.
|
||||
pub fn earcon(&self) -> Option<String> {
|
||||
self.voice_channel_id()?;
|
||||
Some(with_playback(&self.inner, |p| p.clip(crate::earcon::pcm().to_vec())))
|
||||
}
|
||||
|
||||
/// Have the brain answer a chat message under `id`; the text comes as `CoreEvent::ReplyText`.
|
||||
pub fn chat_answer(&self, id: &str, user: u64, text: &str) -> bool {
|
||||
self.inner.worker.reply(id, user, text, "chat", false)
|
||||
|
||||
@@ -34,6 +34,7 @@ app/
|
||||
├── src/discord.rs # Discord client (serenity), voice (songbird), slash command, chat channels
|
||||
├── src/receive.rs # per-speaker reorder window and Opus decoding
|
||||
├── src/playback.rs # speech queue (TTS requests -> sentence clips -> player)
|
||||
├── src/earcon.rs # the earcon sound, made in code
|
||||
├── src/speakers.rs # who is transcribed right now (max speakers)
|
||||
├── src/chat.rs # chat channel access rules and message splitting
|
||||
├── src/worker_client.rs # WebSocket client for the voice worker
|
||||
@@ -155,6 +156,9 @@ and the late "left" update cancelled the new join (seen on real Discord).
|
||||
`cancel(id)` stops one request (its clip if playing, the worker's unsent sentences, or the speech of
|
||||
an answer still being written) and the rest of the queue goes on. `ask_addressed(id, user, text)` and
|
||||
`ask_rank(id, candidates)` pass the conversation's questions to the brain.
|
||||
- **Earcon** (M4): `earcon()` queues the app's own earcon clip (`app/src/earcon.rs`, 48 kHz stereo,
|
||||
peak 0.3 of full scale) as a request of its own: no worker round trip, `SpeakStart`/`SpeakEnd` like
|
||||
any request, dropped by a stop. None when not in a voice channel.
|
||||
- **Stop** (slash command `stop`, or `stop()`): the current clip stops at the next 20 ms mix, every
|
||||
queued request is dropped, and unfinished requests are cancelled at the worker; each ends with
|
||||
`cancelled: true`. Stopping by voice ("자비스, 멈춰") is call detection's stop (`conversation.spec.md`),
|
||||
@@ -206,7 +210,8 @@ registered until it is removed (M5 setup).
|
||||
## Tests
|
||||
|
||||
- `cargo test`: unit tests for the receive window (with a tagging fake and real libopus), playback
|
||||
order, gaps, stop and cancel, speaker limit, chat access and splitting, settings, frames; the worker
|
||||
order, gaps, stop and cancel, the earcon clip (before an answer, dropped by a stop; its sound short,
|
||||
silent at both ends, below its peak), speaker limit, chat access and splitting, settings, frames; the worker
|
||||
link against a fake server (hello, events, reconnect, failed TTS); the worker process with a
|
||||
stand-in `voice_worker` package run by a real Python (token only in the environment, restart on the
|
||||
same port, stop).
|
||||
|
||||
65
app/src/earcon.rs
Normal file
65
app/src/earcon.rs
Normal file
@@ -0,0 +1,65 @@
|
||||
//! 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));
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ pub mod calls;
|
||||
pub mod chat;
|
||||
pub mod conversation;
|
||||
pub mod discord;
|
||||
pub mod earcon;
|
||||
pub mod frames;
|
||||
pub mod playback;
|
||||
pub mod policy;
|
||||
|
||||
@@ -92,6 +92,17 @@ impl<B: TtsBackend, S: ClipSink> Playback<B, S> {
|
||||
id
|
||||
}
|
||||
|
||||
/// Queue a clip the app already has (the earcon) as a request of its own. Returns its id.
|
||||
pub fn clip(&mut self, pcm: Vec<u8>) -> String {
|
||||
self.counter += 1;
|
||||
let id = format!("r{}", self.counter);
|
||||
self.queue.push_back(Request { id: id.clone(), chunks: BTreeMap::from([(0, pcm)]), next: 0, done: true,
|
||||
failed: false, played: 0, started: false });
|
||||
crate::debug_log!("play", "clip id={id} queued={}", self.queue.len());
|
||||
self.pump();
|
||||
id
|
||||
}
|
||||
|
||||
/// End a request that will get no audio (the brain gave no answer).
|
||||
pub fn fail(&mut self, id: &str) {
|
||||
if let Some(r) = self.queue.iter_mut().find(|r| r.id == id && !r.done) {
|
||||
@@ -397,4 +408,24 @@ mod tests {
|
||||
assert_eq!(f.0.borrow().played, vec!["b0"]);
|
||||
assert_eq!(p.take_events()[0], end(&a, 0, false, true));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_clip_plays_at_once_then_the_answer_queued_after_it() {
|
||||
let (mut p, f) = setup();
|
||||
let cue = p.clip(b("ding"));
|
||||
let id = p.adopt();
|
||||
assert_eq!(f.0.borrow().played, vec!["ding"], "the clip needs no worker");
|
||||
assert!(f.0.borrow().sent.is_empty());
|
||||
p.audio(&id, 0, b("a0"));
|
||||
assert_eq!(f.0.borrow().played, vec!["ding"]);
|
||||
p.clip_ended();
|
||||
assert_eq!(f.0.borrow().played, vec!["ding", "a0"]);
|
||||
assert_eq!(p.take_events(), vec![PlaybackEvent::Start(cue.clone()), end(&cue, 1, false, false),
|
||||
PlaybackEvent::Start(id.clone())]);
|
||||
// A stop drops a clip still waiting its turn, without asking the worker to cancel it.
|
||||
let cue2 = p.clip(b("ding"));
|
||||
p.stop();
|
||||
assert_eq!(f.0.borrow().cancelled, vec![id.clone()]);
|
||||
assert!(p.take_events().contains(&end(&cue2, 0, true, false)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -243,6 +243,7 @@ fn parse_call(raw: &Map<String, Value>, wake_words: &[String], d: &CallSettings)
|
||||
designated_no_wake_word: get_bool(call, "designatedNoWakeWord", "call", d.designated_no_wake_word)?,
|
||||
stop_words,
|
||||
follow_up: get_ms(call, "followUpMs", "call", d.follow_up, 5000)?,
|
||||
earcon: get_bool(call, "earcon", "call", d.earcon)?,
|
||||
},
|
||||
chat_wake_words,
|
||||
))
|
||||
@@ -478,7 +479,7 @@ mod tests {
|
||||
"voice": {"wakeWords": ["자비스", "javis"]},
|
||||
"call": {"similarity": 0.3, "mode": "addressed", "designated": [ID], "designatedOnly": true,
|
||||
"designatedNoWakeWord": true, "stopWords": ["멈춰", " "], "chatWakeWords": ["봇"],
|
||||
"followUpMs": 2500},
|
||||
"followUpMs": 2500, "earcon": false},
|
||||
"response": {"policy": "important", "queueMax": 3, "collectMs": 500, "settleMs": 0,
|
||||
"priorities": {ID: 2}}
|
||||
})).unwrap();
|
||||
@@ -490,6 +491,8 @@ mod tests {
|
||||
assert_eq!(s.call.stop_words, vec!["멈춰"]);
|
||||
assert_eq!(s.chat_wake_words, Some(vec!["봇".to_string()]));
|
||||
assert_eq!(s.call.follow_up, Duration::from_millis(2500));
|
||||
assert!(!s.call.earcon);
|
||||
assert!(parse_settings(&json!({})).unwrap().call.earcon, "on by default");
|
||||
assert_eq!(s.response.kind, PolicyKind::Important);
|
||||
assert_eq!(s.response.queue_max, 3);
|
||||
assert_eq!(s.response.collect, Duration::from_millis(500));
|
||||
@@ -503,6 +506,7 @@ mod tests {
|
||||
(json!({"call": {"mode": "sometimes"}}), "call.mode"),
|
||||
(json!({"call": {"designated": ["1"]}}), "call.designated"),
|
||||
(json!({"call": {"followUpMs": 6000}}), "call.followUpMs"),
|
||||
(json!({"call": {"earcon": "yes"}}), "call.earcon"),
|
||||
(json!({"response": {"policy": "random"}}), "response.policy"),
|
||||
(json!({"response": {"queueMax": 0}}), "response.queueMax"),
|
||||
(json!({"response": {"collectMs": 5000}}), "response.collectMs"),
|
||||
|
||||
Reference in New Issue
Block a user