//! 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, } /// `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> { 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 { 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); } }