From a29680387f42e376c477e4a383ecb9dc43e3f084 Mon Sep 17 00:00:00 2001 From: Sam Byng Date: Thu, 25 Apr 2024 17:08:38 +0100 Subject: [PATCH] Add example for embedded_continuous_recognition --- ...bedded_continuous_recognition_from_file.rs | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 examples/recognizer/embedded_continuous_recognition_from_file.rs diff --git a/examples/recognizer/embedded_continuous_recognition_from_file.rs b/examples/recognizer/embedded_continuous_recognition_from_file.rs new file mode 100644 index 0000000..6430a38 --- /dev/null +++ b/examples/recognizer/embedded_continuous_recognition_from_file.rs @@ -0,0 +1,45 @@ +use super::helpers; +use cognitive_services_speech_sdk_rs::audio::AudioConfig; +use cognitive_services_speech_sdk_rs::speech::{EmbeddedSpeechConfig, SpeechRecognizer}; +use log::*; +use std::env; +use std::time::Duration; +use tokio::time::sleep; + +#[allow(dead_code)] +pub async fn run_example() { + info!("------------------------------------------------------------"); + info!("running embedded continuous_recognition_from_file example..."); + info!("------------------------------------------------------------"); + + let filename = helpers::get_sample_file("turn_on_the_lamp.wav"); + + let audio_config = AudioConfig::from_wav_file_input(&filename).unwrap(); + + let mut speech_config = + EmbeddedSpeechConfig::from_path(env::var("ModelPath").unwrap()).unwrap(); + + let models = speech_config.get_speech_recognition_models().unwrap(); + let model = models.first().unwrap(); + info!("Using first model: {:?}", model); + + speech_config + .set_speech_recognition_model(model, env::var("ModelKey").unwrap()) + .unwrap(); + + let mut speech_recognizer = + SpeechRecognizer::from_embedded_config(speech_config, audio_config).unwrap(); + + helpers::set_callbacks(&mut speech_recognizer); + + if let Err(err) = speech_recognizer.start_continuous_recognition_async().await { + error!("start_continuous_recognition_async error {:?}", err); + } + sleep(Duration::from_millis(10000)).await; + speech_recognizer + .stop_continuous_recognition_async() + .await + .unwrap(); + + info!("example finished!"); +}