garage44.eu/jeroen/pinnacle

logclone: git clone https://garage44.eu/jeroen/pinnacle.git

.. /programs/src/bin/speech.rs

raw

//! The speech service, `system-speech`: says aloud what it is sent, in
//! the system's own voice (`pinnacle-speech`). The screen reader in the
//! display server is its one user so far: each message is the language
//! (a byte, as `pinnacle_locale::Language`) and the text. What comes while
//! it speaks cuts it short, as the person has moved on.

#![no_std]
#![no_main]

extern crate alloc;

use alloc::vec::Vec;
use pinnacle_rt::audio::Player;
use pinnacle_rt::{Channel, Received, println};
use pinnacle_speech::{Language, SAMPLE_RATE, say};

/// How much is queued at a time: a new message is looked for between.
const CHUNK_MS: usize = 60;

#[unsafe(no_mangle)]
fn main(_argument: u64) -> u64 {
    let Some(service) = pinnacle_rt::register(pinnacle_ui::access::SPEECH) else {
        println!("speech: {} is taken", pinnacle_ui::access::SPEECH);
        return 1;
    };
    let mut player = Player::open(SAMPLE_RATE, 1);
    if player.is_none() {
        println!("speech: no sound device: what it is sent is not heard");
    }
    let mut clients: Vec<Channel> = Vec::new();
    let mut buffer = alloc::vec![0u8; pinnacle_rt::abi::MAX_MESSAGE];
    // What is being said, and how far.
    let mut speaking: Vec<i16> = Vec::new();
    let mut at = 0;
    loop {
        let mut channels = alloc::vec![&service];
        channels.extend(clients.iter());
        // While speaking, only a look: the next piece is due.
        let deadline = (at < speaking.len()).then(pinnacle_rt::time);
        match pinnacle_rt::receive(&channels, &mut buffer, deadline) {
            Received::Message {
                index: 0,
                mut handles,
                ..
            } if !handles.is_empty() => clients.push(Channel(handles.remove(0))),
            Received::Message { index, len, .. } if index > 0 && len >= 1 => {
                let language = if buffer[0] == 1 {
                    Language::Dutch
                } else {
                    Language::English
                };
                let text = core::str::from_utf8(&buffer[1..len]).unwrap_or("");
                let started = pinnacle_rt::time();
                speaking = say(text, language);
                at = 0;
                println!(
                    "speech: {} ms of \"{text}\", made in {} ms",
                    speaking.len() * 1000 / SAMPLE_RATE as usize,
                    pinnacle_rt::time() - started
                );
            }
            Received::Closed { index } if index > 0 => {
                clients.remove(index - 1);
            }
            _ => {}
        }
        if at < speaking.len() {
            let end = (at + SAMPLE_RATE as usize * CHUNK_MS / 1000).min(speaking.len());
            // Without a sound device it is said to nobody, and done.
            at = match &mut player {
                Some(p) => {
                    p.write(&speaking[at..end]);
                    end
                }
                None => speaking.len(),
            };
        }
    }
}