Files
javis_bot/app/src/speakers.rs
javis-bot f25c26cf53 refactor(app): rewrite the M2 Discord voice core in Rust
The user asked for a faster, real app instead of Electron. The app is now a Rust crate (serenity +
songbird); the GUI in M5 will be a Tauri 2 shell on the system WebView2.

- Same behaviour as the Electron core: one voice channel, per-speaker PCM to the voice worker,
  speaking_end and speaker_leave, sentence playback with stop and cancel, transcription notice on
  join, slash command join/leave/stop (create-or-update), registered chat channels with live events,
  history and sends that never ping, worker process with a fresh token and restart backoff.
- songbird 0.6.1 garbles about a third of received frames under DAVE (its decoder keeps the bytes
  DAVE decryption leaves at the end of the payload; a 20 ms stepped test tone showed repeats, skips
  and reordering, while sending was exact). songbird now only decrypts; the core reorders packets in
  a small window (Opus FEC for the last lost frame) and decodes them with libopus itself.
- Live test on real Discord with two bots: five sentences heard (CER 0/0/0/5/12.5%), 0.71-0.90 s
  from end of speech to final, speech read back at 3.4%, sound gone 0.08 s after stop. The release
  binary uses about 16 MB of memory; a Windows build (MinGW) logs in under Wine.
- Spec moved to app/src/discord_core.spec.md; architecture, decisions, roadmap and pc-features updated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 11:57:53 +09:00

71 lines
1.7 KiB
Rust

//! Who is being transcribed right now. At most `max` people at once (settings `voice.maxSpeakers`);
//! someone who starts while the limit is reached is not heard until their next start after the
//! others free a place.
use std::collections::HashSet;
pub struct SpeakerGate {
max: usize,
active: HashSet<u64>,
}
impl SpeakerGate {
pub fn new(max: usize) -> Self {
Self { max, active: HashSet::new() }
}
/// `id` started speaking. True if their audio goes to the STT.
pub fn start(&mut self, id: u64) -> bool {
if self.active.contains(&id) {
return true;
}
if self.active.len() >= self.max {
return false;
}
self.active.insert(id);
true
}
/// `id` stopped (or left). True if they were being transcribed.
pub fn end(&mut self, id: u64) -> bool {
self.active.remove(&id)
}
pub fn is_active(&self, id: u64) -> bool {
self.active.contains(&id)
}
pub fn count(&self) -> usize {
self.active.len()
}
pub fn clear(&mut self) {
self.active.clear();
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn hears_up_to_the_limit_and_lets_the_next_in_when_someone_stops() {
let mut g = SpeakerGate::new(2);
assert!(g.start(1) && g.start(2));
assert!(!g.start(3));
assert!(!g.is_active(3));
assert!(g.end(1));
assert!(g.start(3));
assert_eq!(g.count(), 2);
}
#[test]
fn already_heard_stays_heard_and_ending_a_stranger_is_a_no_op() {
let mut g = SpeakerGate::new(1);
assert!(g.start(1) && g.start(1));
assert!(!g.end(9));
assert!(g.end(1));
assert!(!g.end(1));
}
}