❔ 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;
        }
    }
Was this page helpful?