diff --git a/Cargo.lock b/Cargo.lock index c7888acf8..a84804199 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2550,6 +2550,7 @@ dependencies = [ name = "tts-frontend" version = "0.1.0" dependencies = [ + "codec", "log", "log-server", "num-derive", diff --git a/services/tts/Cargo.toml b/services/tts/Cargo.toml index ddef6cdd4..44d17155e 100644 --- a/services/tts/Cargo.toml +++ b/services/tts/Cargo.toml @@ -17,6 +17,7 @@ num-derive = {version = "0.3.3", default-features = false} num-traits = {version = "0.2.14", default-features = false} rkyv = {version = "0.4.3", default-features = false, features = ["const_generics"]} xous-tts-backend = {path="../../../tts-backend"} +codec = {path = "../codec"} [target.'cfg(not(any(windows,unix)))'.dependencies] utralib = { path = "../../utralib"} diff --git a/services/tts/src/api.rs b/services/tts/src/api.rs index 202f7301f..b16dfec5c 100644 --- a/services/tts/src/api.rs +++ b/services/tts/src/api.rs @@ -6,6 +6,8 @@ pub(crate) enum Opcode { TextToSpeech, /// Callback for the codec crate CodecCb, + /// Stops audio playback immediately. Does not stop wave generation. + CodecStop, /// Exits the server Quit, } diff --git a/services/tts/src/main.rs b/services/tts/src/main.rs index d7f1ad2ac..5fd29f550 100644 --- a/services/tts/src/main.rs +++ b/services/tts/src/main.rs @@ -5,9 +5,19 @@ mod api; use api::*; use xous_ipc::Buffer; - +use xous::{msg_scalar_unpack, Message}; use num_traits::*; +use codec::{ZERO_PCM, VolumeOps, FrameRing}; use xous_tts_backend::*; +use std::sync::{Arc, Mutex}; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::collections::VecDeque; + +#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] +pub(crate) enum WaveOp { + Return, + Quit, +} #[xous::xous_main] fn xmain() -> ! { @@ -17,10 +27,67 @@ fn xmain() -> ! { let xns = xous_names::XousNames::new().unwrap(); let tts_sid = xns.register_name(api::SERVER_NAME_TTS, None).expect("can't register server"); + let tts_cid = xous::connect(tts_sid).unwrap(); log::trace!("registered with NS -- {:?}", tts_sid); let tts_be = TtsBackend::new(&xns).unwrap(); + let tt = ticktimer_server::Ticktimer::new().unwrap(); + let mut codec = codec::Codec::new(&xns).unwrap(); + codec.setup_8k_stream().expect("couldn't setup stream"); + tt.sleep_ms(50).unwrap(); + codec.set_speaker_volume(VolumeOps::Set, Some(0.0)).unwrap(); + codec.set_headphone_volume(VolumeOps::RestoreDefault, None).unwrap(); + codec.hook_frame_callback(Opcode::CodecCb.to_u32().unwrap(), tts_cid).unwrap(); + let mut frame_count = 0; + + let wav_sid = xous::create_server().unwrap(); + let wav_cid = xous::connect(wav_sid).unwrap(); + let wavbuf = Arc::new(Mutex::new(VecDeque::::new())); + let synth_done = Arc::new(AtomicBool::new(false)); + std::thread::spawn({ + let wav_sid = wav_sid.clone(); + let wavbuf = wavbuf.clone(); + let tts_cid = tts_cid.clone(); + let synth_done = synth_done.clone(); + move || { + loop { + let msg = xous::receive_message(wav_sid).unwrap(); + match FromPrimitive::from_usize(msg.body.id()) { + Some(WaveOp::Return) => { + let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; + let wavdat = buffer.to_original::().unwrap(); + let mut buf = wavbuf.lock().unwrap(); + for &d in wavdat.data[..wavdat.len as usize].iter() { + buf.push_back(d); + } + match wavdat.control { + Some(TtsBeControl::End) => { + synth_done.store(true, Ordering::SeqCst); + /* + xous::send_message(tts_cid, + Message::new_scalar(Opcode::CodecStop.to_usize().unwrap(), 0, 0, 0, 0) + ).expect("couldn't stop playback"); + */ + } + None => { + // do nothing + } + } + }, + Some(WaveOp::Quit) => { + xous::return_scalar(msg.sender, 1).unwrap(); + break; + }, + _ => { + log::warn!("message unknown: {:?}", msg); + } + } + } + } + }); + tts_be.tts_config(wav_sid.to_array(), WaveOp::Return.to_u32().unwrap(), None).unwrap(); + loop { let msg = xous::receive_message(tts_sid).unwrap(); match FromPrimitive::from_usize(msg.body.id()) { @@ -28,11 +95,45 @@ fn xmain() -> ! { let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; let msg = buffer.to_original::().unwrap(); log::info!("tts front end got string {}", msg.text.as_str().unwrap()); + synth_done.store(false, Ordering::SeqCst); tts_be.tts_simple(msg.text.as_str().unwrap()).unwrap(); + codec.resume().unwrap(); }, - Some(Opcode::CodecCb) => { - // tbd - }, + Some(Opcode::CodecCb) => msg_scalar_unpack!(msg, free_play, available_rec, _, routing_id, { + if routing_id == codec::AUDIO_CB_ROUTING_ID { + let mut frames: FrameRing = FrameRing::new(); + let frames_to_push = if frames.writeable_count() < free_play { + frames.writeable_count() + } else { + free_play + }; + frame_count += frames_to_push as u32; + + log::debug!("f{} p{}", frame_count, frames_to_push); + let mut locked_buf = wavbuf.lock().unwrap(); + for _ in 0..frames_to_push { + let mut frame: [u32; codec::FIFO_DEPTH] = [ZERO_PCM as u32 | (ZERO_PCM as u32) << 16; codec::FIFO_DEPTH]; + for sample in frame.iter_mut() { + let samp = locked_buf.pop_front().unwrap_or(ZERO_PCM); + let left = samp as u16; + let right = samp as u16; + *sample = right as u32 | (left as u32) << 16; + } + frames.nq_frame(frame).unwrap(); + } + codec.swap_frames(&mut frames).unwrap(); + // detect if the buffer is empty and the synthesizer has indicated it's finished + if (locked_buf.len() == 0) && synth_done.load(Ordering::SeqCst) { + codec.pause().unwrap(); + } + } else { + codec.pause().unwrap(); // this should stop callbacks from occurring too. + } + }), + Some(Opcode::CodecStop) => { + log::info!("stop called"); + codec.pause().unwrap(); + } Some(Opcode::Quit) => { log::warn!("Quit received, goodbye world!"); break;