From db8a3ec796e0efad434944379cc91debdf5f9216 Mon Sep 17 00:00:00 2001 From: Aron Heinecke Date: Sat, 22 May 2021 19:28:56 +0200 Subject: [PATCH] Add audio processing docs Signed-off-by: Aron Heinecke --- src/main.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index bbf42e2..273b456 100644 --- a/src/main.rs +++ b/src/main.rs @@ -253,9 +253,11 @@ async fn process_discord_audio(voice_buffer: &AudioBufferDiscord, encoder: &Arc< } let mut encoded = [0; 1024]; let encoder_c = encoder.clone(); + // don't block the async runtime let res = task::spawn_blocking(move || { let start = std::time::Instant::now(); let mut data: Vec = Vec::with_capacity(STEREO_20MS); + // merge all audio buffers (clients) to one for buffer in buffer_map.values_mut() { //buffer.truncate(STEREO_20MS); for i in 0..buffer.len() { @@ -268,7 +270,7 @@ async fn process_discord_audio(voice_buffer: &AudioBufferDiscord, encoder: &Arc< } } - + // encode back to opus let lock = encoder_c.try_lock().expect("Can't reach encoder!"); let length = match lock.encode(&data, &mut encoded) { Err(e) => {eprintln!("Failed to encode voice: {}",e); return None;}, @@ -276,11 +278,12 @@ async fn process_discord_audio(voice_buffer: &AudioBufferDiscord, encoder: &Arc< }; //println!("Data size: {}/{} enc-length: {}",data.len(),STEREO_20MS,length); //println!("length size: {}",length); + // warn on high encoding times let duration = start.elapsed().as_millis(); - if duration > 15 { + if duration > 5 { eprintln!("Took too {}ms for processing audio!",duration); } - + // package into teamspeak audio structure Some(OutAudio::new(&AudioData::C2S { id: 0, codec: CodecType::OpusMusic, data: &encoded[..length] })) }).await.expect("Join error for audio processing thread!"); res