Add some tests for voice quality

Signed-off-by: Aron Heinecke <aron.heinecke@t-online.de>
This commit is contained in:
Aron Heinecke 2021-05-10 18:42:52 +02:00
parent d9cf674602
commit fc14e3e48a
2 changed files with 22 additions and 17 deletions

View file

@ -365,21 +365,21 @@ impl VoiceEventHandler for Receiver {
// SSRCs and map the SSRC to the User ID and maintain this state. // SSRCs and map the SSRC to the User ID and maintain this state.
// Using this map, you can map the `ssrc` in `voice_packet` // Using this map, you can map the `ssrc` in `voice_packet`
// to the user ID and handle their audio packets separately. // to the user ID and handle their audio packets separately.
println!( //println!(
"Speaking state update: user {:?} has SSRC {:?}, using {:?}", // "Speaking state update: user {:?} has SSRC {:?}, using {:?}",
user_id, // user_id,
ssrc, // ssrc,
speaking, // speaking,
); // );
}, },
Ctx::SpeakingUpdate {ssrc, speaking} => { Ctx::SpeakingUpdate {ssrc, speaking} => {
// You can implement logic here which reacts to a user starting // You can implement logic here which reacts to a user starting
// or stopping speaking. // or stopping speaking.
println!( //println!(
"Source {} has {} speaking.", // "Source {} has {} speaking.",
ssrc, // ssrc,
if *speaking {"started"} else {"stopped"}, // if *speaking {"started"} else {"stopped"},
); // );
}, },
Ctx::VoicePacket {audio, packet, payload_offset, payload_end_pad} => { Ctx::VoicePacket {audio, packet, payload_offset, payload_end_pad} => {
// An event which fires for every received audio packet, // An event which fires for every received audio packet,

View file

@ -217,8 +217,10 @@ async fn main() -> Result<()> {
Ok(()) Ok(())
} }
const STEREO_20MS: usize = 48000 * 2 * 20 / 1000;
async fn process_audio(voice_buffer: &AudioBufferDiscord) -> Option<OutPacket> { async fn process_audio(voice_buffer: &AudioBufferDiscord) -> Option<OutPacket> {
let buffer_map; let mut buffer_map;
{ {
let mut lock = voice_buffer.lock().await; let mut lock = voice_buffer.lock().await;
buffer_map = std::mem::replace(&mut *lock, HashMap::new()); buffer_map = std::mem::replace(&mut *lock, HashMap::new());
@ -226,20 +228,22 @@ async fn process_audio(voice_buffer: &AudioBufferDiscord) -> Option<OutPacket> {
if buffer_map.is_empty() { if buffer_map.is_empty() {
return None; return None;
} }
let mut encoded = [0; 256]; let mut encoded = [0; 1024];
let res = task::spawn_blocking(move || { let res = task::spawn_blocking(move || {
let start = std::time::Instant::now(); let start = std::time::Instant::now();
let mut data: Vec<i16> = Vec::new(); let mut data: Vec<i16> = Vec::with_capacity(STEREO_20MS);
for buffer in buffer_map.values() { for buffer in buffer_map.values_mut() {
//buffer.truncate(STEREO_20MS);
for i in 0..buffer.len() { for i in 0..buffer.len() {
if let Some(v) = data.get_mut(i) { if let Some(v) = data.get_mut(i) {
*v = *v + buffer[i]; *v = *v + buffer[i];
} else { } else {
data.push(buffer[i]); data.extend(&buffer[i..]);
break;
} }
} }
} }
//println!("Data size: {}",data.len());
let encoder = audiopus::coder::Encoder::new( let encoder = audiopus::coder::Encoder::new(
audiopus::SampleRate::Hz48000, audiopus::SampleRate::Hz48000,
audiopus::Channels::Stereo, audiopus::Channels::Stereo,
@ -250,6 +254,7 @@ async fn process_audio(voice_buffer: &AudioBufferDiscord) -> Option<OutPacket> {
Err(e) => {eprintln!("Failed to encode voice: {}",e); return None;}, Err(e) => {eprintln!("Failed to encode voice: {}",e); return None;},
Ok(size) => size, Ok(size) => size,
}; };
println!("Data size: {}/{} enc-length: {}",data.len(),STEREO_20MS,length);
//println!("length size: {}",length); //println!("length size: {}",length);
let duration = start.elapsed().as_millis(); let duration = start.elapsed().as_millis();
if duration > 15 { if duration > 15 {