❔ NAudio clicking sounds when playing from received packet.

Anyone have any experience with NAudio and sending the audio data over the network and playing it back. Currently I have audio being sent from the client when the microphone has data available and being played correctly through a custom server with 16kb audio at 50ms buffer. However when I play the audio I get these clicking sounds which I believe are either the waveforms not matching or when the audio stops. I have tried using a codec using G772 and ULaw but to no avail could not get it to work. What fixes are there to resolve this isssue?
public static class Audio
{
public static WaveIn waveIn = new WaveIn();
private static BufferedWaveProvider player;
private static WaveFormat wf = new WaveFormat(16000, 1);

public static void AddSamples(byte[] bytes)
{
player.AddSamples(bytes, 0, bytes.Length);
}

private static void OnDataAvailable(object? sender, WaveInEventArgs args)
{
float max = 0;
for (int index = 0; index < args.BytesRecorded; index += 2)
{
short sample = (short)((args.Buffer[index + 1] << 8) |
args.Buffer[index + 0]);
var sample32 = sample / 32768f;
if (sample32 < 0) sample32 = -sample32;
if (sample32 > max) max = sample32;
}

if (100 * max > 10)
{
Network.client.Send(new Packet() { VCAudioBuffer = args.Buffer, VCPacketDataIdentifier = PacketIdentifier.AudioStream, VCSessionKey = Network.KEY }.GetPacketDataStream());
}
}

public static void Init()
{
waveIn.BufferMilliseconds = 50;
waveIn.DeviceNumber = 0;
waveIn.WaveFormat = wf;
waveIn.DataAvailable += OnDataAvailable;
waveIn.StartRecording();
var wo = new WaveOutEvent();
BufferedWaveProvider pro = new BufferedWaveProvider(wf);
pro.DiscardOnBufferOverflow = true;
wo.Init(pro);
wo.Play();
player = pro;
}
}
public static class Audio
{
public static WaveIn waveIn = new WaveIn();
private static BufferedWaveProvider player;
private static WaveFormat wf = new WaveFormat(16000, 1);

public static void AddSamples(byte[] bytes)
{
player.AddSamples(bytes, 0, bytes.Length);
}

private static void OnDataAvailable(object? sender, WaveInEventArgs args)
{
float max = 0;
for (int index = 0; index < args.BytesRecorded; index += 2)
{
short sample = (short)((args.Buffer[index + 1] << 8) |
args.Buffer[index + 0]);
var sample32 = sample / 32768f;
if (sample32 < 0) sample32 = -sample32;
if (sample32 > max) max = sample32;
}

if (100 * max > 10)
{
Network.client.Send(new Packet() { VCAudioBuffer = args.Buffer, VCPacketDataIdentifier = PacketIdentifier.AudioStream, VCSessionKey = Network.KEY }.GetPacketDataStream());
}
}

public static void Init()
{
waveIn.BufferMilliseconds = 50;
waveIn.DeviceNumber = 0;
waveIn.WaveFormat = wf;
waveIn.DataAvailable += OnDataAvailable;
waveIn.StartRecording();
var wo = new WaveOutEvent();
BufferedWaveProvider pro = new BufferedWaveProvider(wf);
pro.DiscardOnBufferOverflow = true;
wo.Init(pro);
wo.Play();
player = pro;
}
}
2 Replies
SineѶeҀҬOӶ⒉⓸⎤ᚙ▟ ▞╸
Also I did try and use DirectSoundOut(); but the clicking sounds are still there. And yes I am using WPF to run the application.
Accord
Accord2y ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.