Need help saving .wav file from RTP G711

Apr 19 at 4:12 AM
I'm having a problem with NAudio 1.8 when creating a .wav file from a G711 RTP stream. Can anyone help?

The wav files created by NAudio have loud machine-gun noise that obscures the voice audio. Intelligible audio is present, but it's masked by the pulsating, clicking noise. Wireshark plays audio from the pcap files without a problem. Wireshark shows the sample rate is 8K ulaw.

Here's my code:

if (wavOutput == null)
string today = DateTime.Now.ToString("yyyy-MM-dd");
wavOutput = new WaveFileWriter("D:\Calls\" + today + "\" + CallID + ".wav", new WaveFormat(8000, 16, 1));

for (int index = 0; index < udpPacket.PayloadData.Length; index++)
short pcm = MuLawDecoder.MuLawToLinearSample(udpPacket.PayloadData[index]);
wavOutput.WriteByte((byte)(pcm & 0xFF));
wavOutput.WriteByte((byte)(pcm >> 8));


Apr 21 at 8:29 PM
The error is in these lines I assume:
wavOutput.WriteByte((byte)(pcm & 0xFF));
wavOutput.WriteByte((byte)(pcm >> 8));
Rest seems correct to me. I recommend to use "bitconverter.getbytes()" instead.
Apr 22 at 2:33 AM
Freefall, thanks for the idea. Unfortunately, I get the same result when using bitconverter.getbytes(). Also, I confirmed I am calling wavOutput.Dispose(). Other ideas?
Apr 23 at 12:01 PM
Edited Apr 23 at 12:02 PM
Noise may be also caused by the g711 compression, anyway, if wireshark plays it better there might be a bug in the NAudio mulaw decoder...
May 4 at 12:16 AM
I am having the exact same issue. It does not matter if I convert my file using nAudio, Sox, Switch, Audacity, whatever, it comes out with the exact same sound as described by Daryl. The only way the audio comes out clean is when I play it or convert it in Wireshark.

I really want to make nAudio work as it is a simple clean solution I can put into my app.

Wireshark shows this:
Payload type: ITU-T G.711 PCMU (0)
Sample Rate (Hz): 8000

I am using the following nAudio code from my app:
        Stream tmpMemStream = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
        var waveFormat = WaveFormat.CreateMuLawFormat(8000, 1);
        var reader = new RawSourceWaveStream(tmpMemStream, waveFormat);
        using (WaveStream convertedStream = WaveFormatConversionStream.CreatePcmStream(reader))
            WaveFileWriter.CreateWaveFile(outFileName, convertedStream);
Still I get that ratcheting noise over my phone call audio which is barely audible in the background.

To assist I have posted the wav file here so you can give it a listen:

Any help would be greatly appreciated!
May 6 at 10:05 AM
Hmm, looks like a recurring noise in each paket. You could try to send a silent stream, extract the noise and then subtract it from your voice stream.
May 6 at 10:06 AM
Some other options: use the managed speex port (NSpeex), the managed vorbis port (NVorbis) or write your own encoder/decoder...