如何解码 RTP 数据包并将其保存为 .wav 文件

发布于 2024-11-28 10:08:12 字数 694 浏览 2 评论 0原文

我正在尝试开发一个应用程序,在其中建立 sip 呼叫,然后捕获 rtp 音频数据包。由于它们是编码的,所以我需要解码它们并将其保存为 .wav 文件。尝试使用 NAudio 但没有成功。有没有使用 NAudio 或任何其他来源来解决这个问题的解决方案...

我使用的代码如下。 data是rtp包数据所在的字节数组。

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);

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我很坚强 2024-12-05 10:08:12

迟到总比不到好!

使用 Mark Heath 提供的以下辅助类

使用 NAudio 解码 mu-law 音频

    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);
        }
    }

获取最新的 NAudio.dll和 NAudio.WindowsMediaFormat.dll

来自

http://naudio.codeplex.com/

然后执行以下操作将 U-Law 或 Mu-Law 转换为 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();

请记住尊重这些音频文件所有者的隐私!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文