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`] and [`SpeechWaveform`]
4//! 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 level;
15mod recognizer;
16mod state;
17mod waveform;
18
19#[cfg(all(feature = "speech", not(target_family = "wasm")))]
20mod microphone;
21#[cfg(all(feature = "speech", not(target_family = "wasm")))]
22mod system;
23
24use std::rc::Rc;
25
26pub use button::SpeechButton;
27#[cfg(all(feature = "speech", not(target_family = "wasm")))]
28pub use microphone::Microphone;
29pub use recognizer::{
30    AudioFormat, AudioInput, AudioSink, RecognitionSession, SpeechError, SpeechRecognizer,
31    SpeechSink,
32};
33pub use state::{SpeechEvent, SpeechState, SpeechStatus};
34#[cfg(all(feature = "speech", not(target_family = "wasm")))]
35pub use system::SystemRecognizer;
36pub use waveform::SpeechWaveform;
37
38/// The input a [`SpeechState`] captures from unless told otherwise.
39fn default_input() -> Option<Rc<dyn AudioInput>> {
40    #[cfg(all(feature = "speech", not(target_family = "wasm")))]
41    return Some(Rc::new(Microphone::default()));
42    #[allow(unreachable_code)]
43    None
44}
45
46/// The recognizer a [`SpeechState`] falls back to, if this platform has one.
47fn system_recognizer() -> Option<Rc<dyn SpeechRecognizer>> {
48    #[cfg(all(feature = "speech", any(target_os = "macos", target_os = "windows")))]
49    return Some(Rc::new(SystemRecognizer::new()));
50    #[allow(unreachable_code)]
51    None
52}