mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-24 01:55:16 +08:00
* feat(windows): /dictations records with Hex's own microphone, and a dictation can stream its live text
Windows Hex started its local API without a dictation control, so
POST /dictations answered 404 service-capture-unavailable and apps had to
record the microphone themselves for /streams, ignoring the microphone
chosen in Hex.
The Windows listener now drives the same dictation::service owner as
Linux: one capture at a time beside the shortcut, the saved microphone,
pre-roll, While dictating, the lease, levels, and the HUD.
A start may also ask for the text so far ("partials": true). Hex then
holds a live-text demand for the recording's stream and sends
{stable, tentative} on GET /dictations/{id}/partials until the dictation
ends. /capabilities reports dictationPartials: true on Linux and Windows;
macOS doesn't stream a client's recording, so it reports false.
* fix(macos): no dead /dictations partials code where the recognition loop has none
264 lines
7.7 KiB
Rust
264 lines
7.7 KiB
Rust
//! What the dictation being recorded has said so far, for the HUDs (the
|
||
//! Labs feature live text) and the first run's practice field, which
|
||
//! [`demand`]s it while it shows and reads it with [`current`].
|
||
//!
|
||
//! The capture loop publishes through its recording's [`Publisher`], which
|
||
//! the recording owns: dropping the recording (release, a short tap, a lost
|
||
//! microphone) clears the text, so no ending can leave it on screen.
|
||
//! A HUD registers a [`watch`]er and is told each change from the capture
|
||
//! loop's thread; it must hand the text to its own thread and never wait.
|
||
//! The text is provisional: it is only ever shown, never pasted or kept.
|
||
|
||
use std::sync::atomic::{AtomicU64, AtomicUsize, Ordering};
|
||
use std::sync::{Arc, Mutex, MutexGuard};
|
||
|
||
use crate::transcription::engine::Partial;
|
||
|
||
type Watcher = Arc<dyn Fn(Option<&Partial>) + Send + Sync>;
|
||
|
||
struct Board {
|
||
/// The publisher whose text is current, or 0 when none is.
|
||
owner: u64,
|
||
/// The current publisher's latest text.
|
||
text: Option<Partial>,
|
||
watchers: Vec<Watcher>,
|
||
}
|
||
|
||
static BOARD: Mutex<Board> = Mutex::new(Board {
|
||
owner: 0,
|
||
text: None,
|
||
watchers: Vec::new(),
|
||
});
|
||
static NEXT_PUBLISHER: AtomicU64 = AtomicU64::new(1);
|
||
static DEMANDS: AtomicUsize = AtomicUsize::new(0);
|
||
#[cfg(target_os = "linux")]
|
||
static TYPING: AtomicUsize = AtomicUsize::new(0);
|
||
static CLIENTS: AtomicUsize = AtomicUsize::new(0);
|
||
|
||
fn board() -> MutexGuard<'static, Board> {
|
||
BOARD.lock().unwrap_or_else(|error| error.into_inner())
|
||
}
|
||
|
||
/// Calls `watcher` with each new text, and with `None` once the recording
|
||
/// that published it ends.
|
||
pub(crate) fn watch(watcher: impl Fn(Option<&Partial>) + Send + Sync + 'static) {
|
||
board().watchers.push(Arc::new(watcher));
|
||
}
|
||
|
||
/// The text the recording in progress has shown, if any.
|
||
pub(crate) fn current() -> Option<Partial> {
|
||
board().text.clone()
|
||
}
|
||
|
||
/// Asks dictation streams to publish their text while it is held, without
|
||
/// the experiment (the first run's practice field). The HUD plate still
|
||
/// shows only with the experiment.
|
||
pub(crate) struct Demand(());
|
||
|
||
pub(crate) fn demand() -> Demand {
|
||
DEMANDS.fetch_add(1, Ordering::AcqRel);
|
||
Demand(())
|
||
}
|
||
|
||
impl Drop for Demand {
|
||
fn drop(&mut self) {
|
||
DEMANDS.fetch_sub(1, Ordering::AcqRel);
|
||
}
|
||
}
|
||
|
||
/// Asks dictation streams to publish their text for Live typing's
|
||
/// input-method preedit, without the Live text plate. It shares the engine
|
||
/// demand with [`Demand`], but never publishes to the plate.
|
||
#[cfg(target_os = "linux")]
|
||
pub(crate) struct TypingDemand(());
|
||
|
||
#[cfg(target_os = "linux")]
|
||
pub(crate) fn typing_demand() -> TypingDemand {
|
||
TYPING.fetch_add(1, Ordering::AcqRel);
|
||
TypingDemand(())
|
||
}
|
||
|
||
#[cfg(target_os = "linux")]
|
||
impl Drop for TypingDemand {
|
||
fn drop(&mut self) {
|
||
TYPING.fetch_sub(1, Ordering::AcqRel);
|
||
}
|
||
}
|
||
|
||
/// Asks dictation streams to publish their text for a `/dictations` client
|
||
/// that asked for partials, without the Live text plate. It shares the
|
||
/// engine demand with [`Demand`], but never publishes to the plate.
|
||
#[cfg(any(test, target_os = "linux", target_os = "windows"))]
|
||
pub(crate) struct ClientDemand(());
|
||
|
||
#[cfg(any(test, target_os = "linux", target_os = "windows"))]
|
||
pub(crate) fn client_demand() -> ClientDemand {
|
||
CLIENTS.fetch_add(1, Ordering::AcqRel);
|
||
ClientDemand(())
|
||
}
|
||
|
||
#[cfg(any(test, target_os = "linux", target_os = "windows"))]
|
||
impl Drop for ClientDemand {
|
||
fn drop(&mut self) {
|
||
CLIENTS.fetch_sub(1, Ordering::AcqRel);
|
||
}
|
||
}
|
||
|
||
/// Whether anything holds a [`Demand`], a `ClientDemand`, or a
|
||
/// `TypingDemand` on Linux.
|
||
pub(crate) fn demanded() -> bool {
|
||
DEMANDS.load(Ordering::Acquire) > 0 || CLIENTS.load(Ordering::Acquire) > 0 || typing_demanded()
|
||
}
|
||
|
||
/// Whether a `TypingDemand` is held: Live typing is Linux only.
|
||
#[cfg(target_os = "linux")]
|
||
fn typing_demanded() -> bool {
|
||
TYPING.load(Ordering::Acquire) > 0
|
||
}
|
||
|
||
#[cfg(not(target_os = "linux"))]
|
||
fn typing_demanded() -> bool {
|
||
false
|
||
}
|
||
|
||
/// Whether the HUD's live text plate wants the published text (Labs › Live
|
||
/// text, or the first run's practice field).
|
||
pub(crate) fn board_demanded() -> bool {
|
||
DEMANDS.load(Ordering::Acquire) > 0
|
||
}
|
||
|
||
/// One recording's hold on the live text.
|
||
pub(crate) struct Publisher {
|
||
id: u64,
|
||
}
|
||
|
||
impl Publisher {
|
||
pub(crate) fn new() -> Self {
|
||
Self {
|
||
id: NEXT_PUBLISHER.fetch_add(1, Ordering::Relaxed),
|
||
}
|
||
}
|
||
|
||
/// Shows `partial`, replacing any other recording's text.
|
||
pub(crate) fn show(&self, partial: &Partial) {
|
||
let watchers = {
|
||
let mut board = board();
|
||
board.owner = self.id;
|
||
board.text = Some(partial.clone());
|
||
board.watchers.clone()
|
||
};
|
||
for watcher in watchers {
|
||
watcher(Some(partial));
|
||
}
|
||
}
|
||
}
|
||
|
||
impl Drop for Publisher {
|
||
/// Clears the text if it is still this recording's.
|
||
fn drop(&mut self) {
|
||
let watchers = {
|
||
let mut board = board();
|
||
if board.owner != self.id {
|
||
return;
|
||
}
|
||
board.owner = 0;
|
||
board.text = None;
|
||
board.watchers.clone()
|
||
};
|
||
for watcher in watchers {
|
||
watcher(None);
|
||
}
|
||
}
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
|
||
/// The board is process-wide; these tests take turns with it.
|
||
static TURN: Mutex<()> = Mutex::new(());
|
||
|
||
fn turn() -> MutexGuard<'static, ()> {
|
||
TURN.lock().unwrap_or_else(|error| error.into_inner())
|
||
}
|
||
|
||
fn said(stable: &str) -> Partial {
|
||
Partial {
|
||
stable: stable.into(),
|
||
tentative: String::new(),
|
||
}
|
||
}
|
||
|
||
/// Records every call a watcher gets, as the stable text or `None`.
|
||
fn watched() -> Arc<Mutex<Vec<Option<String>>>> {
|
||
let seen = Arc::new(Mutex::new(Vec::new()));
|
||
let log = seen.clone();
|
||
watch(move |partial| {
|
||
log.lock()
|
||
.unwrap()
|
||
.push(partial.map(|partial| partial.stable.clone()));
|
||
});
|
||
seen
|
||
}
|
||
|
||
#[test]
|
||
fn a_recording_shows_its_text_and_clears_it_when_it_ends() {
|
||
let _turn = turn();
|
||
let seen = watched();
|
||
let recording = Publisher::new();
|
||
recording.show(&said("so the plan"));
|
||
recording.show(&said("so the plan for today"));
|
||
assert_eq!(current().unwrap().stable, "so the plan for today");
|
||
drop(recording);
|
||
assert_eq!(current(), None);
|
||
assert_eq!(
|
||
*seen.lock().unwrap(),
|
||
[
|
||
Some("so the plan".into()),
|
||
Some("so the plan for today".into()),
|
||
None
|
||
]
|
||
);
|
||
}
|
||
|
||
#[test]
|
||
fn an_older_recording_ending_never_clears_a_newer_ones_text() {
|
||
let _turn = turn();
|
||
let seen = watched();
|
||
let older = Publisher::new();
|
||
older.show(&said("old"));
|
||
let newer = Publisher::new();
|
||
newer.show(&said("new"));
|
||
drop(older);
|
||
assert_eq!(
|
||
*seen.lock().unwrap(),
|
||
[Some("old".into()), Some("new".into())]
|
||
);
|
||
drop(newer);
|
||
assert_eq!(seen.lock().unwrap().last(), Some(&None));
|
||
}
|
||
|
||
#[cfg(target_os = "linux")]
|
||
#[test]
|
||
fn a_typing_demand_feeds_the_engine_without_wanting_the_plate() {
|
||
let _turn = turn();
|
||
let before = board_demanded();
|
||
let demand = typing_demand();
|
||
assert!(demanded(), "the engine publishes for a typing demand");
|
||
assert_eq!(
|
||
board_demanded(),
|
||
before,
|
||
"typing alone never asks for the plate"
|
||
);
|
||
drop(demand);
|
||
}
|
||
|
||
#[test]
|
||
fn a_recording_that_never_showed_text_clears_nothing() {
|
||
let _turn = turn();
|
||
let seen = watched();
|
||
drop(Publisher::new());
|
||
assert!(seen.lock().unwrap().is_empty());
|
||
}
|
||
}
|