cognitive-services-speech-sdk-rs


made-with-Rust Maintenance License License: MIT Build Status

Pure Rust implementation of Microsoft Cognitive Speech Services SDK. Provides thin abstraction around native C API. Heavily inspired by official Go library. Provides speech-to-text, text-to-speech and bot framework dialog management capabilities.

Pull requests welcome!

Speech to text

```rust use cognitiveservicesspeechsdkrs as msspeech; use log::*; use std::env;

async fn speechtotext() { let filename = env::var("WAVFILENAME").unwrap(); let audioconfig = msspeech::audio::AudioConfig::fromwavfileinput(&filename).unwrap();

let speech_config = msspeech::speech::SpeechConfig::from_subscription(
    env::var("MSSubscriptionKey").unwrap(),
    env::var("MSServiceRegion").unwrap(),
)
.unwrap();
let mut speech_recognizer =
    msspeech::speech::SpeechRecognizer::from_config(speech_config, audio_config).unwrap();

speech_recognizer
    .set_session_started_cb(|event| info!("set_session_started_cb {:?}", event))
    .unwrap();

speech_recognizer
    .set_session_stopped_cb(|event| info!("set_session_stopped_cb {:?}", event))
    .unwrap();

speech_recognizer
    .set_speech_start_detected_cb(|event| info!("set_speech_start_detected_cb {:?}", event))
    .unwrap();

speech_recognizer
    .set_speech_end_detected_cb(|event| info!("set_speech_end_detected_cb {:?}", event))
    .unwrap();

speech_recognizer
    .set_recognizing_cb(|event| info!("set_recognizing_cb {:?}", event.result.text))
    .unwrap();

speech_recognizer
    .set_recognized_cb(|event| info!("set_recognized_cb {:?}", event))
    .unwrap();

speech_recognizer
    .set_canceled_cb(|event| info!("set_canceled_cb {:?}", event))
    .unwrap();

let result = speech_recognizer.recognize_once_async().await.unwrap();
info!("got recognition {:?}", result);

} ```

Text to speech

```rust use cognitiveservicesspeechsdkrs as msspeech; use log::*; use std::env;

async fn texttospeech() { let pullstream = msspeech::audio::PullAudioOutputStream::createpullstream().unwrap(); let audioconfig = msspeech::audio::AudioConfig::fromstreamoutput(&pull_stream).unwrap();

let speech_config = msspeech::speech::SpeechConfig::from_subscription(
    env::var("MSSubscriptionKey").unwrap(),
    env::var("MSServiceRegion").unwrap(),
)
.unwrap();
let mut speech_synthesizer =
    msspeech::speech::SpeechSynthesizer::from_config(speech_config, audio_config).unwrap();

speech_synthesizer
    .set_synthesizer_started_cb(|event| info!("synthesizer_started_cb {:?}", event))
    .unwrap();

speech_synthesizer
    .set_synthesizer_synthesizing_cb(|event| info!("synthesizer_synthesizing_cb {:?}", event))
    .unwrap();

speech_synthesizer
    .set_synthesizer_completed_cb(|event| info!("synthesizer_completed_cb {:?}", event))
    .unwrap();

speech_synthesizer
    .set_synthesizer_canceled_cb(|event| info!("synthesizer_canceled_cb {:?}", event))
    .unwrap();

match speech_synthesizer.speak_text_async("Hello Rust!").await {
    Err(err) => error!("speak_text_async error {:?}", err),
    Ok(speech_audio_bytes) => {
        info!("speech_audio_bytes {:?}", speech_audio_bytes);
    }
}

} ```

For more see github integration tests (tests folder) and samples (examples folder).

Build prerequisites

Currently only build on Linux is supported. Uses Clang and Microsoft Speech SDK shared libraries. Details can be found here here.

Install following prerequisites before running cargo build:

sudo apt-get update sudo apt-get install clang build-essential libssl1.0.0 libasound2 wget