Skip to content

Commit

Permalink
Fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
DamienDeepgram committed Jul 25, 2024
1 parent e628cfa commit f8b495e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ categories = ["api-bindings", "multimedia::audio"]
audio = "0.2.0"
bytes = "1"
futures = "0.3"
futures-util = { version = "0.3" , optional = true }
futures-util = "0.3"
http = "0.2"
pin-project = "1"
reqwest = { version = "0.11.22", default-features = false, features = ["json", "rustls-tls", "stream"] }
Expand Down
13 changes: 7 additions & 6 deletions examples/transcription/websocket/microphone_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ async fn main() -> Result<(), DeepgramError> {
}
});

let mut transcription_stream = dg.transcription()
let mut transcription_stream = dg
.transcription()
.stream_request()
.keep_alive()
.stream(microphone_as_stream())
Expand All @@ -119,12 +120,12 @@ async fn main() -> Result<(), DeepgramError> {
.start(event_tx.clone())
.await?;

while let Some(response) = transcription_stream.next().await {
match response {
Ok(result) => println!("Transcription result: {:?}", result),
Err(e) => eprintln!("Transcription error: {:?}", e),
}
while let Some(response) = transcription_stream.next().await {
match response {
Ok(result) => println!("Transcription result: {:?}", result),
Err(e) => eprintln!("Transcription error: {:?}", e),
}
}

Ok(())
}
16 changes: 12 additions & 4 deletions examples/transcription/websocket/simple_stream.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use futures_util::stream::StreamExt;
use std::env;
use std::time::Duration;
use tokio::sync::mpsc;
use futures_util::stream::StreamExt;

use deepgram::{
common::options::{Encoding, Endpointing, Language, Options}, listen::websocket::Event, Deepgram, DeepgramError
common::options::{Encoding, Endpointing, Language, Options},
listen::websocket::Event,
Deepgram, DeepgramError,
};

static PATH_TO_FILE: &str = "examples/audio/bueller.wav";
Expand Down Expand Up @@ -33,7 +35,8 @@ async fn main() -> Result<(), DeepgramError> {
}
});

let mut transcription_stream = dg.transcription()
let mut transcription_stream = dg
.transcription()
.stream_request_with_options(Some(&options))
.keep_alive()
.encoding(Encoding::Linear16)
Expand All @@ -44,7 +47,12 @@ async fn main() -> Result<(), DeepgramError> {
.utterance_end_ms(1000)
.vad_events(true)
.no_delay(true)
.file(PATH_TO_FILE, AUDIO_CHUNK_SIZE, Duration::from_millis(16), event_tx.clone())
.file(
PATH_TO_FILE,
AUDIO_CHUNK_SIZE,
Duration::from_millis(16),
event_tx.clone()
)
.await?
.start(event_tx.clone())
.await?;
Expand Down
33 changes: 24 additions & 9 deletions src/listen/websocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ use std::task::{Context, Poll};
use std::time::Duration;

use bytes::{Bytes, BytesMut};
use futures::channel::mpsc::{self, Receiver};
use futures::channel::mpsc as futures_mpsc;
use futures::channel::mpsc::{self, Receiver};
use futures::stream::StreamExt;
use futures::{SinkExt, Stream};
use http::Request;
use pin_project::pin_project;
use tokio::fs::File;
use tokio::sync::Mutex;
use tokio::sync::mpsc::Sender;
use tokio::sync::Mutex;
use tokio::time;
use tokio_tungstenite::tungstenite::protocol::Message;
use tokio_util::io::ReaderStream;
Expand Down Expand Up @@ -260,7 +260,10 @@ where
pub async fn start(
self,
event_tx: Sender<Event>,
) -> std::result::Result<futures_mpsc::Receiver<std::result::Result<StreamResponse, DeepgramError>>, DeepgramError> {
) -> std::result::Result<
futures_mpsc::Receiver<std::result::Result<StreamResponse, DeepgramError>>,
DeepgramError,
> {
// This unwrap is safe because we're parsing a static.
let mut url = self.stream_url;
{
Expand Down Expand Up @@ -357,7 +360,9 @@ where
let mut write = write_clone.lock().await;
if let Err(e) = write.send(keep_alive_message).await {
eprintln!("Error Sending Keep Alive: {:?}", e);
let _ = event_tx_keep_alive.send(Event::Error(DeepgramError::from(e))).await;
let _ = event_tx_keep_alive
.send(Event::Error(DeepgramError::from(e)))
.await;
break;
}
}
Expand All @@ -373,13 +378,17 @@ where
let mut write = write_clone.lock().await;
if let Err(e) = write.send(frame).await {
println!("Error sending frame: {:?}", e);
let _ = event_tx_send.send(Event::Error(DeepgramError::from(e))).await;
let _ = event_tx_send
.send(Event::Error(DeepgramError::from(e)))
.await;
break;
}
}
Err(e) => {
println!("Error receiving from source: {:?}", e);
let _ = event_tx_send.send(Event::Error(DeepgramError::ReceiveError(format!("{:?}", e)))).await;
let _ = event_tx_send
.send(Event::Error(DeepgramError::ReceiveError(format!("{:?}", e))))
.await;
break;
}
}
Expand All @@ -388,7 +397,9 @@ where
let mut write = write_clone.lock().await;
if let Err(e) = write.send(Message::binary([])).await {
println!("Error sending final frame: {:?}", e);
let _ = event_tx_send.send(Event::Error(DeepgramError::from(e))).await;
let _ = event_tx_send
.send(Event::Error(DeepgramError::from(e)))
.await;
}
};

Expand All @@ -402,7 +413,9 @@ where
let resp = serde_json::from_str(&txt).map_err(DeepgramError::from);
if let Err(e) = tx.send(resp).await {
eprintln!("Failed to send message: {:?}", e);
let _ = event_tx_receive.send(Event::Error(DeepgramError::from(e))).await;
let _ = event_tx_receive
.send(Event::Error(DeepgramError::from(e)))
.await;
// Handle the error appropriately, e.g., log it, retry, or break the loop
break;
}
Expand All @@ -413,7 +426,9 @@ where
let mut write = write.lock().await;
if let Err(e) = write.send(Message::Close(None)).await {
eprintln!("Failed to send close frame: {:?}", e);
let _ = event_tx_receive.send(Event::Error(DeepgramError::from(e))).await;
let _ = event_tx_receive
.send(Event::Error(DeepgramError::from(e)))
.await;
}
break;
}
Expand Down

0 comments on commit f8b495e

Please sign in to comment.