| 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 | |
| 13 | mod button; |
| 14 | mod level; |
| 15 | mod recognizer; |
| 16 | mod state; |
| 17 | mod waveform; |
| 18 | |
| 19 | #[cfg(all(feature = "speech", not(target_family = "wasm")))] |
| 20 | mod microphone; |
| 21 | #[cfg(all(feature = "speech", not(target_family = "wasm")))] |
| 22 | mod system; |
| 23 | |
| 24 | use std::rc::Rc; |
| 25 | |
| 26 | pub use button::SpeechButton; |
| 27 | #[cfg(all(feature = "speech", not(target_family = "wasm")))] |
| 28 | pub use microphone::Microphone; |
| 29 | pub use recognizer::{ |
| 30 | AudioFormat, AudioInput, AudioSink, RecognitionSession, SpeechError, SpeechRecognizer, |
| 31 | SpeechSink, |
| 32 | }; |
| 33 | pub use state::{SpeechEvent, SpeechState, SpeechStatus}; |
| 34 | #[cfg(all(feature = "speech", not(target_family = "wasm")))] |
| 35 | pub use system::SystemRecognizer; |
| 36 | pub use waveform::SpeechWaveform; |
| 37 | |
| 38 | /// The input a [`SpeechState`] captures from unless told otherwise. |
| 39 | fn 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. |
| 47 | fn 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 | } |