From ac059eae480eef7f97d48aa1e3de4754008543f0 Mon Sep 17 00:00:00 2001 From: bunnie Date: Thu, 10 Mar 2022 18:05:33 +0800 Subject: [PATCH] retrofit more tts in various corner cases --- Cargo.lock | 1 + services/gam/src/menu.rs | 10 ++++++---- services/root-keys/Cargo.toml | 4 ++++ services/root-keys/src/main.rs | 24 ++++++++++++++++++++++++ services/tts/src/api.rs | 4 +++- services/tts/src/lib.rs | 8 ++++++++ services/tts/src/main.rs | 15 +++++++++++++++ 7 files changed, 61 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fbbe06ef4..dd4e66c24 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1970,6 +1970,7 @@ dependencies = [ "susres", "ticktimer-server", "trng", + "tts-frontend", "utralib", "xous", "xous-ipc 0.9.7", diff --git a/services/gam/src/menu.rs b/services/gam/src/menu.rs index 3c1241fa0..d581068fd 100644 --- a/services/gam/src/menu.rs +++ b/services/gam/src/menu.rs @@ -279,10 +279,6 @@ impl<'a> Menu<'a> { } log::trace!("menu redraw##"); self.gam.redraw().unwrap(); - #[cfg(feature="tts")] - { - self.tts.tts_simple(&self.name).unwrap(); - } } fn num_items(&self) -> usize { self.items.len() @@ -299,6 +295,12 @@ impl<'a> Menu<'a> { } if let Some(action) = mi.action_conn { log::debug!("doing menu action for {}", mi.name); + #[cfg(feature="tts")] + { + let mut phrase = "select ".to_string(); + phrase.push_str(mi.name.as_str().unwrap()); + self.tts.tts_blocking(&phrase).unwrap(); + } match mi.action_payload { MenuPayload::Scalar(args) => { xous::send_message(action, diff --git a/services/root-keys/Cargo.toml b/services/root-keys/Cargo.toml index 0304d644f..91529049a 100644 --- a/services/root-keys/Cargo.toml +++ b/services/root-keys/Cargo.toml @@ -40,6 +40,9 @@ graphics-server = {path = "../graphics-server"} # for reboots and fuse operations jtag = {path = "../jtag"} +# for text to speech support +tts-frontend = {path="../tts"} + # private keys #sha2 = {version = "0.9.5", default-features = false, features = []} sha2 = {path = "../engine-sha512"} @@ -67,6 +70,7 @@ utralib = { path = "../../utralib"} [features] policy-menu = [] # provisions for users to set their password retention policies (on the block for deprecation) (note: to re-enable you need to add "rootkeys menu" to the tokens.rs in the GAM) hazardous-debug = [] # this feature enables the printing of secret materials for debug purposes +tts = [] default = [] [lib] diff --git a/services/root-keys/src/main.rs b/services/root-keys/src/main.rs index db73ad114..6a4be5a40 100644 --- a/services/root-keys/src/main.rs +++ b/services/root-keys/src/main.rs @@ -15,6 +15,9 @@ use gam::modal::*; #[cfg(feature = "policy-menu")] use gam::{MenuItem, MenuPayload}; +#[cfg(feature = "tts")] +use tts_frontend::*; + use locales::t; use std::format; use std::str; @@ -327,6 +330,9 @@ fn xmain() -> ! { let main_cid = xous::connect(keys_sid).expect("couldn't create suspend callback connection"); let mut susres = susres::Susres::new(None, &xns, api::Opcode::SuspendResume as u32, main_cid).expect("couldn't create suspend/resume object"); + #[cfg(feature="tts")] + let tts = TtsFrontend::new(&xns).unwrap(); + // create a policy menu object #[cfg(feature = "policy-menu")] { @@ -459,6 +465,8 @@ fn xmain() -> ! { if keys.is_initialized() { modals.show_notification(t!("rootkeys.already_init", xous::LANG)).expect("modals error"); + #[cfg(feature="tts")] + tts.tts_blocking(t!("rootkeys.already_init", xous::LANG)).unwrap(); keys.set_ux_password_type(None); continue; } else { @@ -489,6 +497,8 @@ fn xmain() -> ! { Some(t!("rootkeys.bootpass", xous::LANG)), false, None, true, None ); + #[cfg(feature="tts")] + tts.tts_blocking(t!("rootkeys.bootpass", xous::LANG)).unwrap(); rootkeys_modal.activate(); } }), @@ -511,6 +521,8 @@ fn xmain() -> ! { Some(t!("rootkeys.updatepass", xous::LANG)), false, None, true, None ); + #[cfg(feature="tts")] + tts.tts_blocking(t!("rootkeys.updatepass", xous::LANG)).unwrap(); rootkeys_modal.activate(); }, Some(Opcode::UxInitUpdatePasswordReturn) => { @@ -705,6 +717,8 @@ fn xmain() -> ! { Some(t!("rootkeys.get_update_password", xous::LANG)), false, None, true, None ); + #[cfg(feature="tts")] + tts.tts_blocking(t!("rootkeys.get_update_password", xous::LANG)).unwrap(); rootkeys_modal.activate(); } } @@ -780,6 +794,8 @@ fn xmain() -> ! { Some(t!("rootkeys.get_signing_password", xous::LANG)), false, None, true, None ); + #[cfg(feature="tts")] + tts.tts_blocking(t!("rootkeys.get_signing_password", xous::LANG)).unwrap(); rootkeys_modal.activate(); } }, @@ -858,6 +874,8 @@ fn xmain() -> ! { Some(t!("rootkeys.get_login_password", xous::LANG)), false, None, true, None ); + #[cfg(feature="tts")] + tts.tts_blocking(t!("rootkeys.get_login_password", xous::LANG)).unwrap(); rootkeys_modal.activate(); // note that the scalar is *not* yet returned, it will be returned by the opcode called by the password assurance } else { @@ -893,6 +911,8 @@ fn xmain() -> ! { Some(ActionType::RadioButtons(confirm_radiobox)), Some(t!("rootkeys.policy_request", xous::LANG)), false, None, true, None); + #[cfg(feature="tts")] + tts.tts_blocking(t!("rootkeys.policy_request", xous::LANG)).unwrap(); rootkeys_modal.activate(); }, Some(Opcode::UxAesEnsureReturn) => { @@ -968,6 +988,8 @@ fn xmain() -> ! { Some(ActionType::ConsoleInput(console_input)), Some(t!("rootkeys.console_input", xous::LANG)), false, None, true, None); + #[cfg(feature="tts")] + tts.tts_blocking(t!("rootkeys.console_input", xous::LANG)).unwrap(); rootkeys_modal.activate(); log::info!("{}check_conn", CONSOLE_SENTINEL); } @@ -990,6 +1012,8 @@ fn xmain() -> ! { Some(t!("rootkeys.get_signing_password", xous::LANG)), false, None, true, None ); + #[cfg(feature="tts")] + tts.tts_blocking(t!("rootkeys.get_signing_password", xous::LANG)).unwrap(); rootkeys_modal.activate(); } } else { diff --git a/services/tts/src/api.rs b/services/tts/src/api.rs index 41e093513..36d18caf4 100644 --- a/services/tts/src/api.rs +++ b/services/tts/src/api.rs @@ -2,8 +2,10 @@ pub(crate) const SERVER_NAME_TTS: &str = "_Text to speech front end_"; #[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] pub(crate) enum Opcode { - /// Basic, synchronous, non-abortable conversion of a string to audible speech + /// Basic, interruptable conversion of a string to audible speech TextToSpeech, + /// Non-interruptable conversion of a string to audible speech. Blocks until the phrase is finished. + TextToSpeechBlocking, /// Callback for the codec crate CodecCb, /// Stops audio playback immediately. Does not stop wave generation. diff --git a/services/tts/src/lib.rs b/services/tts/src/lib.rs index 13c9e8228..86cd94434 100644 --- a/services/tts/src/lib.rs +++ b/services/tts/src/lib.rs @@ -27,6 +27,14 @@ impl TtsFrontend { let buf = Buffer::into_buf(msg).or(Err(xous::Error::InternalError))?; buf.lend(self.conn, Opcode::TextToSpeech.to_u32().unwrap()).map(|_| ()) } + /// This blocks until the text is finished rendering + pub fn tts_blocking(&self, text: &str) -> Result<(), xous::Error> { + let msg = TtsFrontendMsg { + text: xous_ipc::String::from_str(text), + }; + let buf = Buffer::into_buf(msg).or(Err(xous::Error::InternalError))?; + buf.lend(self.conn, Opcode::TextToSpeechBlocking.to_u32().unwrap()).map(|_| ()) + } pub fn set_words_per_minute(&self, wpm: u32) -> Result<(), xous::Error> { send_message(self.conn, Message::new_scalar(Opcode::SetWordsPerMinute.to_usize().unwrap(), wpm as usize, 0, 0, 0) diff --git a/services/tts/src/main.rs b/services/tts/src/main.rs index c7c945e3b..e5c6412fb 100644 --- a/services/tts/src/main.rs +++ b/services/tts/src/main.rs @@ -14,6 +14,7 @@ use std::sync::atomic::{AtomicBool, Ordering}; use std::collections::VecDeque; const DEFAULT_WPM: u32 = 350; +const WAIT_INTERVAL: usize = 50; // milliseconds to wait before polling if a phrase is finished. #[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] pub(crate) enum WaveOp { @@ -114,6 +115,20 @@ fn xmain() -> ! { log::debug!("resuming codec"); codec.resume().unwrap(); }, + Some(Opcode::TextToSpeechBlocking) => { + let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; + let msg = buffer.to_original::().unwrap(); + log::debug!("tts blocking front end got string {}", msg.text.as_str().unwrap()); + wavbuf.lock().unwrap().clear(); // this will truncate any buffered audio that is playing + synth_done.store(false, Ordering::SeqCst); + tts_be.tts_simple(msg.text.as_str().unwrap()).unwrap(); + just_initiated = true; + log::debug!("resuming codec (blocking)"); + codec.resume().unwrap(); + while !synth_done.load(Ordering::SeqCst) { + tt.sleep_ms(WAIT_INTERVAL).unwrap(); + } + }, 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();