NAudio icon indicating copy to clipboard operation
NAudio copied to clipboard

MixingSampleProvider and BufferedWaveProvider overflow causation - question

Open SineVector241 opened this issue 1 year ago • 1 comments

This is a question as I have looked basically everywhere and have asked for help in discord servers to no avail so here's my question.

This is the environment: There are 5 - 20 people connected to the server and are talking all at the same time.

And this is the code that handles the VOIP packets and plays them based off the fire and forget example.

namespace VoiceCraftProximityChat.Models 
 { 
     class AudioPlaybackModel 
     { 
         private readonly IWavePlayer outputDevice; 
         private readonly MixingSampleProvider mixer; 
         private readonly BufferedWaveProvider waveProvider; 
  
         public AudioPlaybackModel() 
         { 
             outputDevice = new WaveOutEvent(); 
             mixer = new MixingSampleProvider(WaveFormat.CreateIeeeFloatWaveFormat(16000, 1)); 
             waveProvider = new BufferedWaveProvider(new WaveFormat(16000,1)); 
             waveProvider.DiscardOnBufferOverflow = false; 
             waveProvider.ReadFully = false; 
             waveProvider.BufferLength = 1024 * 16; 
             mixer.ReadFully = true; 
             outputDevice.Init(mixer); 
             outputDevice.Play(); 
         } 
  
         public void PlaySound(byte[] buffer, float Volume) 
         { 
             Task.Run(() => 
             { 
                 var provider = new RawSourceWaveStream(buffer, 0, 1600, new WaveFormat(16000, 1)); 
                 waveProvider.AddSamples(buffer, 0, 1600); 
                 var buff = new Wave16ToFloatProvider(waveProvider); 
                 buff.Volume = Volume; 
                 mixer.AddMixerInput(buff); 
             }); 
         } 
  
         public static readonly AudioPlaybackModel Instance = new AudioPlaybackModel(); 
     } 
 }

Will the BufferedWaveProvider and MixingSampleProvider overflow due to too many packets being sent and added? And if so would I need to create a new BufferedWaveProvider and MixingSampleProvider list where it is assigned one instance of the class per user to avoid this issue? Or would I have to look into multi threading each user?

SineVector241 avatar Dec 06 '22 03:12 SineVector241

Btw I have tested this with 2 people and it works fine

SineVector241 avatar Dec 06 '22 03:12 SineVector241

Ok so I have tested with 3 people and the BufferedWaveProvider gets full instantly. Is there any way to fix this?

SineVector241 avatar Dec 11 '22 02:12 SineVector241

I was able to improve the handling of multiple client audio by putting BufferedWaveProvider in a list everytime a new client connects and it works perfectly! Now I am worried if MixingSampleProvider will overflow

SineVector241 avatar Dec 11 '22 05:12 SineVector241