C
C#3mo ago
nullptr

artifacted playback using `NAudio` with multiple raw pcm streams

hello there! i'm trying to implement voice chat into a custom discord library i'm writing. everything works great but the sound quality dips heavily if 2 or more users are speaking at once. you can see my player class below:
public class NAudioPlayer : IPlayer
{
private readonly ConcurrentDictionary<uint, BufferedWaveProvider> _waveProviders = new();
private readonly MixingSampleProvider _mixer;
private readonly WaveOutEvent _waveOut;
private readonly WaveFormat _format = new(48000, 16, 2);

public NAudioPlayer()
{
_mixer = new MixingSampleProvider(WaveFormat.CreateIeeeFloatWaveFormat(48000, 2));
_mixer.ReadFully = true;
_waveOut = new WaveOutEvent();
_waveOut.Init(_mixer);
_waveOut.Play();
}

public void AddSamples(byte[] pcmData, int pcmLength, uint ssrc)
{
if (!_waveProviders.TryGetValue(ssrc, out var waveProvider))
{
waveProvider = new BufferedWaveProvider(_format)
{
DiscardOnBufferOverflow = true
};
var sampleProvider = waveProvider.ToSampleProvider();
_waveProviders.TryAdd(ssrc, waveProvider);
_mixer.AddMixerInput(sampleProvider);
}

_waveProviders[ssrc].AddSamples(pcmData, 0, pcmLength);
}
}
public class NAudioPlayer : IPlayer
{
private readonly ConcurrentDictionary<uint, BufferedWaveProvider> _waveProviders = new();
private readonly MixingSampleProvider _mixer;
private readonly WaveOutEvent _waveOut;
private readonly WaveFormat _format = new(48000, 16, 2);

public NAudioPlayer()
{
_mixer = new MixingSampleProvider(WaveFormat.CreateIeeeFloatWaveFormat(48000, 2));
_mixer.ReadFully = true;
_waveOut = new WaveOutEvent();
_waveOut.Init(_mixer);
_waveOut.Play();
}

public void AddSamples(byte[] pcmData, int pcmLength, uint ssrc)
{
if (!_waveProviders.TryGetValue(ssrc, out var waveProvider))
{
waveProvider = new BufferedWaveProvider(_format)
{
DiscardOnBufferOverflow = true
};
var sampleProvider = waveProvider.ToSampleProvider();
_waveProviders.TryAdd(ssrc, waveProvider);
_mixer.AddMixerInput(sampleProvider);
}

_waveProviders[ssrc].AddSamples(pcmData, 0, pcmLength);
}
}
there is only one NAudioPlayer initialized for the entire voice session, but it uses ssrcs sent by discord's voice gateway and udp servers to split them into different streams and mix them together. however, this mixing seems to do absolutely nothing; it sounds exactly the same if i just add samples to the same wave provider for every user. what am i doing wrong?
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server