-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Improved callbacks and minor fix (#13)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
206324f
commit 060ee6b
Showing
24 changed files
with
533 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
/target | ||
.idea | ||
**/.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
[package] | ||
name = "azure-speech" | ||
version = "0.2.2" | ||
version = "0.2.3" | ||
authors = ["Jure Bernava Prah <[email protected]>"] | ||
description = "Pure Rust implementation for Microsoft Speech Service" | ||
description = "Pure Rust SDK for Azure Speech Service" | ||
edition = "2021" | ||
rust-version = "1.71.0" | ||
license = "MIT" | ||
|
@@ -41,7 +41,7 @@ serde_json = "1.0.114" | |
os_info = "3" | ||
|
||
ssml = "0.1.0" | ||
async-channel = "1.9.0" | ||
async-channel = "1.9.0" # needed for ezsockets 0.6 for call_with; | ||
|
||
|
||
[dev-dependencies] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
use azure_speech::stream::Stream; | ||
use azure_speech::Auth; | ||
use azure_speech::{recognizer, StreamExt}; | ||
use std::env; | ||
use std::error::Error; | ||
use std::path::Path; | ||
use tokio::fs::File; | ||
use tokio::io::{AsyncReadExt, BufReader}; | ||
use tokio_stream::wrappers::ReceiverStream; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn Error>> { | ||
tracing_subscriber::fmt() | ||
.with_max_level(tracing::Level::INFO) | ||
.init(); | ||
|
||
// Check on the example recognize_simple.rs for more details on how to set the recognizer. | ||
let auth = Auth::from_subscription( | ||
env::var("AZURE_REGION").expect("Region set on AZURE_REGION env"), | ||
env::var("AZURE_SUBSCRIPTION_KEY").expect("Subscription set on AZURE_SUBSCRIPTION_KEY env"), | ||
); | ||
let config = recognizer::Config::default(); | ||
|
||
let client = recognizer::Client::connect(auth, config) | ||
.await | ||
.expect("to connect to azure"); | ||
|
||
// Create a callbacks for the recognizer. | ||
// The callbacks are used to get information about the recognition process. | ||
let callbacks = recognizer::Callback::default() | ||
.on_start_detected(|id, offset| async move { | ||
tracing::info!("Start detected: {:?} - {:?}", id, offset); | ||
}) | ||
.on_recognized(|id, result, _offset, _duration, _raw| async move { | ||
tracing::info!("Recognized: {:?} - {:?}", id, result); | ||
}) | ||
.on_session_end(|id| async move { | ||
tracing::info!("Session end: {:?}", id); | ||
}); | ||
//.on_... // check the other callbacks available. | ||
|
||
client | ||
.recognize( | ||
create_audio_stream("tests/audios/examples_sample_files_turn_on_the_lamp.wav").await, // Try also the mp3 version of the file. | ||
recognizer::ContentType::Wav, // Be sure to set it correctly. | ||
recognizer::Details::file(), | ||
) | ||
.await | ||
.expect("to recognize") | ||
// When you set the callbacks, the events will be sent to the callbacks and not to the stream. | ||
.use_callbacks(callbacks) | ||
.await; // it's important to await here. | ||
|
||
tracing::info!("Completed!"); | ||
|
||
Ok(()) | ||
} | ||
|
||
async fn create_audio_stream(path: impl AsRef<Path>) -> impl Stream<Item = Vec<u8>> { | ||
let (tx, rx) = tokio::sync::mpsc::channel(1024); | ||
let file = File::open(path).await.expect("Failed to open file"); | ||
let mut reader = BufReader::new(file); | ||
|
||
tokio::spawn(async move { | ||
let mut chunk = vec![0; 4096]; | ||
while let Ok(n) = reader.read(&mut chunk).await { | ||
if n == 0 { | ||
break; | ||
} | ||
if tx.send(chunk.clone()).await.is_err() { | ||
tracing::error!("Error sending data"); | ||
break; | ||
} | ||
} | ||
drop(tx); | ||
}); | ||
|
||
ReceiverStream::new(rx) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,62 @@ | ||
use azure_speech::{synthesizer, Auth}; | ||
use azure_speech::{synthesizer, Auth, StreamExt}; | ||
use std::env; | ||
use std::error::Error; | ||
use tokio_stream::StreamExt; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn Error>> { | ||
tracing_subscriber::fmt() | ||
.with_max_level(tracing::Level::INFO) | ||
.init(); | ||
|
||
// Check the examples/synthesize_simple.rs file for the full code. | ||
|
||
let auth = Auth::from_subscription( | ||
env::var("AZURE_REGION").expect("Region set on AZURE_REGION env"), | ||
env::var("AZURE_SUBSCRIPTION_KEY").expect("Subscription set on AZURE_SUBSCRIPTION_KEY env"), | ||
); | ||
|
||
let config = synthesizer::Config::default() | ||
.on_synthesising(|request_id, audio| { | ||
let config = synthesizer::Config::default(); | ||
let client = synthesizer::Client::connect(auth, config) | ||
.await | ||
.expect("to connect to azure"); | ||
|
||
// Create the callbacks for the synthesizer. | ||
let callbacks = synthesizer::Callback::default() | ||
.on_synthesising(|request_id, audio| async move { | ||
tracing::info!( | ||
"Callback - request: {:?}: Synthesising bytes {:?} ", | ||
request_id, | ||
audio.len() | ||
); | ||
}) | ||
.on_synthesised(|request_id| { | ||
.on_synthesised(|request_id| async move { | ||
tracing::info!("Callback - request: {:?}: Synthesised", request_id); | ||
}) | ||
.on_error(|request_id, error| { | ||
tracing::info!("Callback - request: {:?}: Error {:?}", request_id, error); | ||
}) | ||
.on_audio_metadata(|request_id, metadata| { | ||
.on_audio_metadata(|request_id, metadata| async move { | ||
tracing::info!( | ||
"Callback - request: {:?}: Audio metadata {:?}", | ||
request_id, | ||
metadata | ||
); | ||
}) | ||
.on_session_start(|request_id| { | ||
.on_session_start(|request_id| async move { | ||
tracing::info!("Callback - request: {:?}: Session started", request_id); | ||
}) | ||
.on_session_end(|request_id| { | ||
.on_session_end(|request_id| async move { | ||
tracing::info!("Callback - request: {:?}: Session ended", request_id); | ||
}) | ||
.on_error(|request_id, error| async move { | ||
tracing::info!("Callback - request: {:?}: Error {:?}", request_id, error); | ||
}); | ||
|
||
let client = synthesizer::Client::connect(auth, config) | ||
.await | ||
.expect("to connect to azure"); | ||
|
||
// you can use both the stream and callback in the same functions. | ||
let mut stream = client | ||
client | ||
// here you put your text to synthesize. | ||
.synthesize("Hello World!") | ||
.await | ||
.expect("to synthesize"); | ||
|
||
while let Some(event) = stream.next().await { | ||
tracing::info!("Synthesizer Event: {:?}", event); | ||
} | ||
.expect("to synthesize") | ||
.use_callbacks(callbacks) | ||
.await; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// src/callback.rs | ||
use crate::RequestId; | ||
use std::future::Future; | ||
use std::pin::Pin; | ||
|
||
pub(crate) type OnSessionStarted = Box<dyn Fn(RequestId) -> BoxFuture>; | ||
pub(crate) type OnSessionEnded = Box<dyn Fn(RequestId) -> BoxFuture>; | ||
pub(crate) type OnError = Box<dyn Fn(RequestId, crate::Error) -> BoxFuture>; | ||
pub(crate) type BoxFuture = Pin<Box<dyn Future<Output = ()> + Send + 'static>>; | ||
|
||
#[async_trait::async_trait] | ||
pub trait Callback { | ||
type Item; | ||
fn on_event(&self, item: Self::Item) -> impl Future<Output = ()>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.