feat: ✨ speaker on/of capability
This commit is contained in:
parent
6efb12d3b0
commit
52115f649a
4 changed files with 87 additions and 30 deletions
|
@ -12,17 +12,17 @@ use tokio::{
|
|||
task::JoinHandle,
|
||||
};
|
||||
|
||||
use crate::{ClientConfig, SPEAKER_BUFFER_LENGHT, voice::play};
|
||||
use crate::ClientConfig;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ConnectReturn {
|
||||
play_audio_stop_signal_sender: oneshot::Sender<bool>,
|
||||
send_audio_task: JoinHandle<()>,
|
||||
receive_audio_task: JoinHandle<()>,
|
||||
}
|
||||
|
||||
pub async fn connect(
|
||||
microphone_receiver: broadcast::Receiver<f32>,
|
||||
speaker_sender: Arc<broadcast::Sender<f32>>,
|
||||
client_config: Arc<ClientConfig>,
|
||||
) -> Result<ConnectReturn, Error> {
|
||||
let client = Client::builder()
|
||||
|
@ -59,15 +59,10 @@ pub async fn connect(
|
|||
|
||||
let (receive_stream, send_stream) = stream.split();
|
||||
|
||||
let (speaker_sender, speaker_receiver) = broadcast::channel(SPEAKER_BUFFER_LENGHT);
|
||||
let (play_audio_stop_signal_sender, play_audio_stop_signal_receiver) = oneshot::channel();
|
||||
|
||||
tokio::spawn(play(speaker_receiver, play_audio_stop_signal_receiver));
|
||||
let receive_audio_task = tokio::spawn(receive_audio_data(receive_stream, speaker_sender));
|
||||
let send_audio_task = tokio::spawn(send_audio_data(send_stream, microphone_receiver));
|
||||
|
||||
Ok(ConnectReturn {
|
||||
play_audio_stop_signal_sender,
|
||||
send_audio_task,
|
||||
receive_audio_task,
|
||||
})
|
||||
|
@ -86,9 +81,6 @@ pub async fn disconnect_watcher(
|
|||
|
||||
connection_return.send_audio_task.abort();
|
||||
connection_return.receive_audio_task.abort();
|
||||
if let Err(err_val) = connection_return.play_audio_stop_signal_sender.send(true) {
|
||||
eprintln!("Error: Send Play Audio Stop Signal | Local | {}", err_val);
|
||||
}
|
||||
}
|
||||
|
||||
async fn send_audio_data(
|
||||
|
@ -120,16 +112,13 @@ async fn send_audio_data(
|
|||
}
|
||||
async fn receive_audio_data(
|
||||
mut receive_stream: ReceiveStream,
|
||||
speaker_sender: broadcast::Sender<f32>,
|
||||
speaker_sender: Arc<broadcast::Sender<f32>>,
|
||||
) {
|
||||
loop {
|
||||
match receive_stream.read_f32().await {
|
||||
Ok(received_data) => {
|
||||
// error only happens if there is no receiver, think about it
|
||||
if let Err(err_val) = speaker_sender.send(received_data) {
|
||||
eprintln!("Error: Send to Speaker | Local | {}", err_val);
|
||||
break;
|
||||
}
|
||||
// todo: error only happens if there is no receiver, think about it
|
||||
let _ = speaker_sender.send(received_data);
|
||||
}
|
||||
Err(err_val) => {
|
||||
eprintln!("Error: Receive Audio Data | Remote | {}", err_val);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue