working example

This commit is contained in:
Kuba Clark 2022-02-11 12:48:57 +01:00
parent 9ef6213ed9
commit 7262352247

View file

@ -42,7 +42,8 @@ async fn main() -> eyre::Result<()> {
StreamingRecognitionConfig {
config: Some(RecognitionConfig {
encoding: AudioEncoding::Flac.into(), // matching current example file
sample_rate_hertz: 48000, // matching current example file
sample_rate_hertz: 44_100, // matching current example file
audio_channel_count: 2,
language_code: "en-US".to_string(), // we only support en-US to start with
model: "video".to_string(), // dictate does not set this option
use_enhanced: true, // dictate does not set this option
@ -60,16 +61,17 @@ async fn main() -> eyre::Result<()> {
let file = tokio::fs::File::open("some-audio.flac").await.unwrap();
let mut audio_file = tokio::io::BufReader::new(file);
// read file chunk
let mut buffer = [0; 1024 * 5];
while audio_file.read(&mut buffer).await.is_ok() {
let mut buffer = [0; 1024 * 50];
while let Ok(n) = audio_file.read(&mut buffer[..]).await {
// send to server
let request = StreamingRecognizeRequest {
streaming_request: Some(StreamingRequest::AudioContent(
BytesMut::from(buffer.as_slice()).freeze(),
BytesMut::from(&buffer.as_slice()[..n]).freeze(),
)),
};
yield request;
debug!("added a buffer to the sender queue");
// debug!("added a buffer to the sender queue: {} bytes", n);
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
}
};