Files
javis_bot/app/src/frames.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

74 lines
2.3 KiB
Rust

//! Binary frame layout shared with the Python voice worker (`src/voice_worker/protocol.py`).
pub const KIND_AUDIO: u8 = 0x01;
pub const KIND_TTS: u8 = 0x02;
#[derive(Debug, PartialEq, Eq)]
pub struct TtsFrame {
pub id: String,
pub seq: u32,
pub pcm: Vec<u8>,
}
/// `0x01 | u8 n | speaker id | PCM16LE` in the hello "audio" format. None if the id does not fit.
pub fn encode_audio_frame(speaker: &str, pcm: &[i16]) -> Option<Vec<u8>> {
let id = speaker.as_bytes();
if id.is_empty() || id.len() > 255 {
return None;
}
let mut out = Vec::with_capacity(2 + id.len() + pcm.len() * 2);
out.push(KIND_AUDIO);
out.push(id.len() as u8);
out.extend_from_slice(id);
for s in pcm {
out.extend_from_slice(&s.to_le_bytes());
}
Some(out)
}
/// `0x02 | u8 n | request id | u32 LE seq | PCM16LE` in the hello "tts" format. None if malformed.
pub fn decode_tts_frame(frame: &[u8]) -> Option<TtsFrame> {
if frame.len() < 2 || frame[0] != KIND_TTS {
return None;
}
let n = frame[1] as usize;
let end = 2 + n;
if n == 0 || frame.len() < end + 4 {
return None;
}
let id = std::str::from_utf8(&frame[2..end]).ok()?.to_owned();
let seq = u32::from_le_bytes(frame[end..end + 4].try_into().ok()?);
Some(TtsFrame { id, seq, pcm: frame[end + 4..].to_vec() })
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn audio_frame_is_kind_id_length_id_then_pcm() {
let f = encode_audio_frame("유저", &[1, -2]).unwrap();
let id = "유저".as_bytes();
let mut want = vec![KIND_AUDIO, id.len() as u8];
want.extend_from_slice(id);
want.extend_from_slice(&[1, 0, 0xfe, 0xff]);
assert_eq!(f, want);
}
#[test]
fn ids_must_fit_in_a_byte() {
assert!(encode_audio_frame("", &[0]).is_none());
assert!(encode_audio_frame(&"x".repeat(256), &[0]).is_none());
}
#[test]
fn tts_frames_decode_and_truncated_ones_are_refused() {
let mut f = vec![KIND_TTS, 2, b'r', b'1'];
f.extend_from_slice(&7u32.to_le_bytes());
f.extend_from_slice(&[9, 9]);
assert_eq!(decode_tts_frame(&f), Some(TtsFrame { id: "r1".into(), seq: 7, pcm: vec![9, 9] }));
assert_eq!(decode_tts_frame(&f[..5]), None);
assert_eq!(decode_tts_frame(&[KIND_AUDIO, 1, b'a', 0, 0, 0, 0]), None);
}
}