generated from milosgajdos/go-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
let rustbot speak first before sending a prompt to gobot
Signed-off-by: Milos Gajdos <[email protected]>
- Loading branch information
1 parent
5477f6b
commit 572ff02
Showing
4 changed files
with
69 additions
and
48 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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use crate::prelude::*; | ||
use bytes::BytesMut; | ||
use rodio::{Decoder, Sink}; | ||
use std::io::Cursor; | ||
use tokio::{ | ||
self, | ||
io::{self, AsyncReadExt}, | ||
}; | ||
|
||
pub async fn play(mut audio_rd: io::DuplexStream, sink: Sink) -> Result<()> { | ||
let mut audio_data = BytesMut::new(); | ||
while let Ok(chunk) = audio_rd.read_buf(&mut audio_data).await { | ||
if chunk == 0 { | ||
break; | ||
} | ||
if audio_data.len() > AUDIO_BUFFER_SIZE { | ||
let cursor = Cursor::new(audio_data.clone().freeze().to_vec()); | ||
match Decoder::new(cursor) { | ||
Ok(source) => { | ||
sink.append(source); | ||
audio_data.clear(); // Clear the buffer on successful append | ||
} | ||
Err(e) => { | ||
eprintln!("Failed to decode received audio: {}", e); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Flush any remaining data at the end | ||
if !audio_data.is_empty() { | ||
let cursor = Cursor::new(audio_data.to_vec()); | ||
match Decoder::new(cursor) { | ||
Ok(source) => sink.append(source), | ||
Err(e) => println!("Remaining data could not be decoded: {}", e), | ||
} | ||
} | ||
sink.sleep_until_end(); | ||
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