2
votes

I am trying to develop an application in which a sip call is established and then i am capturing rtp audio packets. As they are encoded so i need to decode them and save it has .wav file. Tried using NAudio but didnt worked. Is there any solution using NAudio or any other source to solve this problem...

the code i used is as follow. data is the byte array in which rtp packet data is.

System.IO.MemoryStream stream = new System.IO.MemoryStream(data);

RawSourceWaveStream rsws = new RawSourceWaveStream(stream, WaveFormat.CreateMuLawFormat(8000,1));

WaveStream conversionStream = WaveFormatConversionStream.CreatePcmStream(rsws);

WaveStream blockAlignedStream = new BlockAlignReductionStream(conversionStream);

byte[] buffer = new byte[udpHeader.Data.Length];
blockAlignedStream.Read(buffer, 0, udpHeader.Data.Length);
writer.WriteData(buffer, 0, buffer.Length);

Thanks in Advance.

1
To help us answer you better, are you able to provide the NAudio code that you tried?Adam Baxter
i have edited my question and added my code therewahab mehboob
what didn't work about the code? (btw, you don't need the BlockAlignReductionStream). You also need to use the return from the Read method - the converted audio will be twice the length of the raw dataMark Heath
You need to at least strip the RTP headers. But you'd better make sense of them as they contain timing information in case of clock skew or packet loss.Szocske
sorry for the late reply as i was away. The problem is that when i play the file it gives a continuous noise of same pattern. No voice is there. help neededwahab mehboob

1 Answers

1
votes

Better late than never!

Use the following helper classs by Mark Heath from Using NAudio to decode mu-law audio

    public class RawSourceWaveStream : WaveStream
    {
        private Stream sourceStream;
        private WaveFormat waveFormat;

        public RawSourceWaveStream(Stream sourceStream, WaveFormat waveFormat)
        {
            this.sourceStream = sourceStream;
            this.waveFormat = waveFormat;
        }

        public override WaveFormat WaveFormat
        {
            get { return this.waveFormat; }
        }

        public override long Length
        {
            get { return this.sourceStream.Length; }
        }

        public override long Position
        {
            get
            {
                return this.sourceStream.Position;
            }
            set
            {
                this.sourceStream.Position = value;
            }
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            return sourceStream.Read(buffer, offset, count);
        }
    }

Get the latest NAudio.dll and NAudio.WindowsMediaFormat.dll

from

http://naudio.codeplex.com/

Then do the following to convert from U-Law or Mu-Law to Wav:

Stream tmpMemStream = new FileStream(Server.MapPath("/input/") + "input.voc", FileMode.Open, FileAccess.Read);
var waveFormat = WaveFormat.CreateMuLawFormat(8000, 1);  // Feel free to tweak this number
var reader = new RawSourceWaveStream(tmpMemStream, waveFormat);
using (WaveStream convertedStream = WaveFormatConversionStream.CreatePcmStream(reader))
{
    WaveFileWriter.CreateWaveFile(Server.MapPath("/output/") + "output.wav", convertedStream);
}
tmpMemStream.Close();

Just remember to respect the privacy of the owners of those audio files!