sipsorcery
sipsorcery

Reputation: 30699

NAudio decoding ulaw to pcm

I'm attempting to decode a received RTP stream and save it to a .wav file using NAudio, similar to How to decode RTP packets and save it has .wav file.

I've tried a few different approaches but none have resulted in a .wav file that is recognised by Windows Media Player.

Here's how I define my wave file writer.

var waveFileWriter = new WaveFileWriter("out.wav", new WaveFormat(8000, 16, 1));

Then for each RTP packet I receive I use:

private void SampleReceived(byte[] sample)
{
    if (sample != null)
    {
        for (int index = 0; index < sample.Length; index++)
        {
            short pcm = MuLawDecoder.MuLawToLinearSample(sample[index]);
            waveFileWriter.WriteByte((byte)(pcm & 0xFF));
            waveFileWriter.WriteByte((byte)(pcm >> 8));
        }
    }
}

The code runs without an error but when I try to open the .wav file in Windows Media Player I get an error. I've used WireShark to confirm that the RTP stream is ulaw. Is this the right type of approach to use with NAudio?

Upvotes: 3

Views: 3275

Answers (1)

Mark Heath
Mark Heath

Reputation: 49492

You must Dispose your WaveFileWriter before attempting to play the WAV file, as WAV files have headers at the beginning that can only be filled in once all the data has been added.

Upvotes: 3

Related Questions