From bbccf1c2a71c2166a924de46aca569ed70c26c6e Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 11 Mar 2022 19:08:39 +0800 Subject: [PATCH] reached a reasonable integration point for tts until braille protos are in tts is working fairly well and much more stably, menus and ui elements are wired up for tts. just waiting for the latest braille keyboard pcb to come in and then we'll be able to fire off some prototypes... --- services/keyboard/src/api.rs | 4 ++-- services/status/src/kbdmenu.rs | 11 +++++++++++ services/status/src/mainmenu.rs | 7 +++---- services/tts/src/main.rs | 24 +++++++++++++++++++----- 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/services/keyboard/src/api.rs b/services/keyboard/src/api.rs index cdf4abeb9..a37140911 100644 --- a/services/keyboard/src/api.rs +++ b/services/keyboard/src/api.rs @@ -28,7 +28,7 @@ impl From for KeyMap { 1 => KeyMap::Azerty, 2 => KeyMap::Qwertz, 3 => KeyMap::Dvorak, - 254 => KeyMap::Braille, // braille has only one mapping available + 4 => KeyMap::Braille, _ => KeyMap::Qwerty, } } @@ -41,7 +41,7 @@ impl Into for KeyMap { KeyMap::Azerty => 1, KeyMap::Qwertz => 2, KeyMap::Dvorak => 3, - KeyMap::Braille => 254, + KeyMap::Braille => 4, KeyMap::Undefined => 255, } } diff --git a/services/status/src/kbdmenu.rs b/services/status/src/kbdmenu.rs index d2178a8dd..bf2872a4e 100644 --- a/services/status/src/kbdmenu.rs +++ b/services/status/src/kbdmenu.rs @@ -44,6 +44,17 @@ pub fn create_kbd_menu(status_conn: xous::CID, kbd_mgr: xous::SID) -> MenuMatic action_payload: MenuPayload::Scalar([code as u32, 0, 0, 0]), close_on_select: true, }); + #[cfg(feature="tts")] + { + let code: usize = KeyMap::Braille.into(); + menu_items.push(MenuItem { + name: xous_ipc::String::from_str("Braille"), + action_conn: Some(status_conn), + action_opcode: StatusOpcode::SetKeyboard.to_u32().unwrap(), + action_payload: MenuPayload::Scalar([code as u32, 0, 0, 0]), + close_on_select: true, + }); + } menu_matic(menu_items, gam::KBD_MENU_NAME, Some(kbd_mgr)).expect("couldn't create MenuMatic manager") } diff --git a/services/status/src/mainmenu.rs b/services/status/src/mainmenu.rs index 2484b7463..c7bc9dfdb 100644 --- a/services/status/src/mainmenu.rs +++ b/services/status/src/mainmenu.rs @@ -13,7 +13,8 @@ pub fn create_main_menu(keys: Arc>, status_conn: xous::CID, com: let mut menuitems = Vec::::new(); - #[cfg(not(feature="tts"))] // no backlight on versions with no display + // no backlight on versions with no display + //#[cfg(not(feature="tts"))] menuitems.push(MenuItem { name: String::from_str(t!("mainmenu.backlighton", xous::LANG)), action_conn: Some(com.conn()), @@ -22,7 +23,7 @@ pub fn create_main_menu(keys: Arc>, status_conn: xous::CID, com: close_on_select: true, }); - #[cfg(not(feature="tts"))] + //#[cfg(not(feature="tts"))] menuitems.push(MenuItem { name: String::from_str(t!("mainmenu.backlightoff", xous::LANG)), action_conn: Some(com.conn()), @@ -98,7 +99,6 @@ pub fn create_main_menu(keys: Arc>, status_conn: xous::CID, com: close_on_select: true, }); - #[cfg(not(feature="tts"))] menuitems.push(MenuItem { name: String::from_str(t!("mainmenu.kbd", xous::LANG)), action_conn: Some(status_conn), @@ -106,7 +106,6 @@ pub fn create_main_menu(keys: Arc>, status_conn: xous::CID, com: action_payload: MenuPayload::Scalar([0, 0, 0, 0]), close_on_select: true, }); - menuitems.push(MenuItem { name: String::from_str(t!("mainmenu.closemenu", xous::LANG)), action_conn: None, diff --git a/services/tts/src/main.rs b/services/tts/src/main.rs index 694ddec37..4681f5cce 100644 --- a/services/tts/src/main.rs +++ b/services/tts/src/main.rs @@ -15,6 +15,8 @@ use std::collections::VecDeque; const DEFAULT_WPM: u32 = 350; const WAIT_INTERVAL: usize = 50; // milliseconds to wait before polling if a phrase is finished. +const MAX_BUF_DEPTH: usize = (8000 * core::mem::size_of::()) * 3; // 8000 samples/s * num seconds to buffer +const DRAIN_INTERVAL: usize = 100; // milliseconds to wait before checking if buffer has drained #[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] pub(crate) enum WaveOp { @@ -52,10 +54,22 @@ fn xmain() -> ! { // let tts_cid = tts_cid.clone(); let synth_done = synth_done.clone(); move || { + let tt = ticktimer_server::Ticktimer::new().unwrap(); loop { let msg = xous::receive_message(wav_sid).unwrap(); match FromPrimitive::from_usize(msg.body.id()) { Some(WaveOp::Return) => { + // check to see if we need to apply backpressure on the synthesizer. If so, this is where we pause + let mut capacity = { // put this in a block of its own to ensure the lock goes out of scope after we have measured the length + wavbuf.lock().unwrap().len() + }; + while capacity > MAX_BUF_DEPTH { + // this effectively stalls the tts engine because the buffer sent to us is a `lend`, which is blocking. + // by blocking this thread from copying the memory, we also block the synthesizer from generating more samples. + log::info!("synth backpressure"); + tt.sleep_ms(DRAIN_INTERVAL).unwrap(); + capacity = wavbuf.lock().unwrap().len(); + } let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; let wavdat = buffer.to_original::().unwrap(); let mut buf = wavbuf.lock().unwrap(); @@ -113,7 +127,7 @@ fn xmain() -> ! { free_play }; frame_count += frames_to_push as u32; - log::debug!("f{} p{}", frame_count, frames_to_push); + log::trace!("f{} p{}", frame_count, frames_to_push); let mut locked_buf = wavbuf.lock().unwrap(); if just_initiated.load(Ordering::SeqCst) { // prevent stutter if the synth buffer isn't ready yet and we got an early fill request from the codec @@ -181,23 +195,23 @@ fn xmain() -> ! { Some(Opcode::TextToSpeech) => { 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()); + log::debug!("tts 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.store(true, Ordering::SeqCst); - log::debug!("resuming codec"); + log::trace!("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::info!("tts blocking front end got string {}", msg.text.as_str().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.store(true, Ordering::SeqCst); - log::debug!("resuming codec (blocking)"); + log::trace!("resuming codec (blocking)"); codec.resume().unwrap(); while !synth_done.load(Ordering::SeqCst) { // this is done fairly "fast" because the synth buf fills quickly