diff --git a/src/discord.rs b/src/discord.rs index f891fde..73a3a76 100644 --- a/src/discord.rs +++ b/src/discord.rs @@ -365,21 +365,21 @@ impl VoiceEventHandler for Receiver { // SSRCs and map the SSRC to the User ID and maintain this state. // Using this map, you can map the `ssrc` in `voice_packet` // to the user ID and handle their audio packets separately. - println!( - "Speaking state update: user {:?} has SSRC {:?}, using {:?}", - user_id, - ssrc, - speaking, - ); + //println!( + // "Speaking state update: user {:?} has SSRC {:?}, using {:?}", + // user_id, + // ssrc, + // speaking, + // ); }, Ctx::SpeakingUpdate {ssrc, speaking} => { // You can implement logic here which reacts to a user starting // or stopping speaking. - println!( - "Source {} has {} speaking.", - ssrc, - if *speaking {"started"} else {"stopped"}, - ); + //println!( + // "Source {} has {} speaking.", + // ssrc, + // if *speaking {"started"} else {"stopped"}, + // ); }, Ctx::VoicePacket {audio, packet, payload_offset, payload_end_pad} => { // An event which fires for every received audio packet, diff --git a/src/main.rs b/src/main.rs index df122c2..77cf642 100644 --- a/src/main.rs +++ b/src/main.rs @@ -217,8 +217,10 @@ async fn main() -> Result<()> { Ok(()) } +const STEREO_20MS: usize = 48000 * 2 * 20 / 1000; + async fn process_audio(voice_buffer: &AudioBufferDiscord) -> Option { - let buffer_map; + let mut buffer_map; { let mut lock = voice_buffer.lock().await; buffer_map = std::mem::replace(&mut *lock, HashMap::new()); @@ -226,20 +228,22 @@ async fn process_audio(voice_buffer: &AudioBufferDiscord) -> Option { if buffer_map.is_empty() { return None; } - let mut encoded = [0; 256]; + let mut encoded = [0; 1024]; let res = task::spawn_blocking(move || { let start = std::time::Instant::now(); - let mut data: Vec = Vec::new(); - for buffer in buffer_map.values() { + let mut data: Vec = Vec::with_capacity(STEREO_20MS); + for buffer in buffer_map.values_mut() { + //buffer.truncate(STEREO_20MS); for i in 0..buffer.len() { if let Some(v) = data.get_mut(i) { *v = *v + buffer[i]; } else { - data.push(buffer[i]); + data.extend(&buffer[i..]); + break; } } } - //println!("Data size: {}",data.len()); + let encoder = audiopus::coder::Encoder::new( audiopus::SampleRate::Hz48000, audiopus::Channels::Stereo, @@ -250,6 +254,7 @@ async fn process_audio(voice_buffer: &AudioBufferDiscord) -> Option { Err(e) => {eprintln!("Failed to encode voice: {}",e); return None;}, Ok(size) => size, }; + println!("Data size: {}/{} enc-length: {}",data.len(),STEREO_20MS,length); //println!("length size: {}",length); let duration = start.elapsed().as_millis(); if duration > 15 {