ds_transcriber/
transcriber.rs

1//! Library's entry point. Converts the audio stream to text using DeepSpeech bindings
2
3use crate::stream::record_audio;
4use anyhow::Result;
5use deepspeech::Model;
6use std::fmt::Debug;
7
8///
9/// # Example
10///
11/// Creates preferences for the configuration of the audio stream and transcription.
12/// ```
13/// use std::env::args;
14/// use ds_transcriber::model::instance_model;
15/// # fn main()->Result<(),Box<dyn std::error::Error>> {
16///     if let Some(model_dir_str) = args().nth(1) {
17///         let mut model = instance_model(&model_dir_str, None)?;
18///         let mut config = ds_transcriber::StreamSettings::default();
19///     }
20///    # Ok(())
21/// # }
22/// ```
23#[derive(Debug, Clone, Copy)]
24pub struct StreamSettings {
25    /// value used for pause detection, a pause is detected when the amplitude is less than this
26    pub silence_level: i32,
27    /// show the amplitude values on stdout (helps you to find your silence level)
28    pub show_amplitudes: bool,
29    /// seconds of silence indicating end of speech
30    pub pause_length_millis: u32,
31}
32
33impl StreamSettings {
34    /// Create a new configuration for a mic stream
35    pub fn new(silence_level: i32, show_amplitudes: bool, pause_length_millis: u32) -> Self {
36        Self {
37            silence_level,
38            show_amplitudes,
39            pause_length_millis,
40        }
41    }
42}
43
44impl Default for StreamSettings {
45    fn default() -> Self {
46        Self {
47            silence_level: 200,
48            show_amplitudes: true,
49            pause_length_millis: 1000,
50        }
51    }
52}
53
54///
55/// # Usage
56/// After getting config ready, all you need to do is pass a ref of it to the function:
57/// ```
58/// use std::env::args;
59/// # use ds_transcriber::model::instance_model;
60/// # fn main()-> Result<(),Box<dyn std::error::Error>> {
61///    # if let Some(model_dir_str) = args().nth(1) {
62///    #    let mut model = instance_model(&model_dir_str, None)?;
63///    #    let config = ds_transcriber::StreamSettings::default();
64///         let i_said = ds_transcriber::transcribe(config,&mut model)?;
65///         println!("I said: {}", i_said);
66///    #  }
67///    # Ok(())
68/// # }
69/// ```
70
71pub fn transcribe(config: StreamSettings, model: &mut Model) -> Result<String> {
72    let pause_length_secs: f32 = (config.pause_length_millis / 1000) as f32;
73    match record_audio(
74        config.silence_level,
75        config.show_amplitudes,
76        pause_length_secs,
77    ) {
78        Ok(audio_stream) => convert(&audio_stream, model),
79        Err(e) => Err(anyhow::anyhow!(e)),
80    }
81}
82
83fn convert(audio_stream: &[i16], model: &mut Model) -> Result<String> {
84    let buf = model.speech_to_text(audio_stream)?;
85    Ok(buf)
86}