Skip to main content

gpui_component/speech/
mod.rs

1//! Speech input: capture audio, recognize it and hand the text to the caller.
2//!
3//! [`SpeechState`] owns a session and [`SpeechButton`], [`SpeechIndicator`] and
4//! [`SpeechWaveform`] render it. Recognition and audio capture are both replaceable:
5//! implement [`SpeechRecognizer`] to use any speech service and [`AudioInput`]
6//! to feed audio from anywhere.
7//!
8//! With the `speech` feature, a state without its own recognizer falls back to
9//! the `SystemRecognizer` of macOS or Windows, and captures from the
10//! `Microphone`. Linux has no system recognizer, so speech input works there
11//! only with an application recognizer.
12
13mod button;
14mod indicator;
15mod level;
16mod recognizer;
17mod state;
18mod surface;
19mod waveform;
20
21#[cfg(all(feature = "speech", not(target_family = "wasm")))]
22mod microphone;
23#[cfg(all(feature = "speech", not(target_family = "wasm")))]
24mod system;
25
26use std::rc::Rc;
27
28pub use button::SpeechButton;
29pub use indicator::SpeechIndicator;
30#[cfg(all(feature = "speech", not(target_family = "wasm")))]
31pub use microphone::Microphone;
32pub use recognizer::{
33    AudioFormat, AudioInput, AudioSink, RecognitionSession, SpeechError, SpeechRecognizer,
34    SpeechSink,
35};
36pub use state::{SpeechEvent, SpeechState, SpeechStatus};
37#[cfg(all(feature = "speech", not(target_family = "wasm")))]
38pub use system::SystemRecognizer;
39pub use waveform::SpeechWaveform;
40
41/// The input a [`SpeechState`] captures from unless told otherwise.
42fn default_input() -> Option<Rc<dyn AudioInput>> {
43    #[cfg(all(feature = "speech", not(target_family = "wasm")))]
44    return Some(Rc::new(Microphone::default()));
45    #[allow(unreachable_code)]
46    None
47}
48
49/// The recognizer a [`SpeechState`] falls back to, if this platform has one.
50fn system_recognizer() -> Option<Rc<dyn SpeechRecognizer>> {
51    #[cfg(all(feature = "speech", any(target_os = "macos", target_os = "windows")))]
52    return Some(Rc::new(SystemRecognizer::new()));
53    #[allow(unreachable_code)]
54    None
55}