使用 NAudio 解码 mu-law 音频

发布于 2024-10-03 08:40:15 字数 436 浏览 3 评论 0原文

我正在尝试使用 NAudio 将 mu-law 编码音频解码为 pcm 音频。我的服务已发布原始 mu-law 编码音频字节,但我从 NAudio 收到错误,表明数据没有 RIFF 标头。我需要以某种方式添加这个吗?我使用的代码是:

WaveFileReader reader = new WaveFileReader(tmpMemStream);
using (WaveStream convertedStream = WaveFormatConversionStream.CreatePcmStream(reader))
{
    WaveFileWriter.CreateWaveFile(recordingsPath + "/" + outputFileName, convertedStream);
}

我还将原始数据保存到磁盘并在 Matlab 中进行解码,这没有问题。谢谢。

I'm attempting to use NAudio to decode mu-law encoded audio into pcm audio. My service is POSTed the raw mu-law encoded audio bytes and I'm getting an error from NAudio that the data does not have a RIFF header. Do I need to add this somehow? The code I'm using is:

WaveFileReader reader = new WaveFileReader(tmpMemStream);
using (WaveStream convertedStream = WaveFormatConversionStream.CreatePcmStream(reader))
{
    WaveFileWriter.CreateWaveFile(recordingsPath + "/" + outputFileName, convertedStream);
}

I'm also saving the raw data to the disk and doing the decoding in Matlab which is working with no problem. Thanks.

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

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

发布评论

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

评论(1

太阳男子 2024-10-10 08:40:15

由于您只有原始 mu-law 数据,因此无法在其上使用 WaveFileReader。相反,创建一个继承自 WaveStream 的新类。

在其Read方法中,从tmpMemStream返回数据。作为 WaveFormat 返回 mu-law WaveFormat。

这是您可以使用的通用帮助程序类:

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

现在您可以像以前一样继续创建转换后的文件,传入 RawSourceWaveStream 作为输入:

var waveFormat = WaveFormat.CreateMuLawFormat(8000, 1);
var reader = new RawSourceWaveStream(tmpMemStream, waveFormat);
using (WaveStream convertedStream = WaveFormatConversionStream.CreatePcmStream(reader))
{
    WaveFileWriter.CreateWaveFile(recordingsPath + "/" + outputFileName, convertedStream);
}

Since you just have raw mu-law data, you can't use a WaveFileReader on it. Instead, create a new class that inherits from WaveStream.

In its Read method, return data from tmpMemStream. As a WaveFormat return a mu-law WaveFormat.

Here's a generic helper class that you could use:

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

Now you can proceed to create the converted file as you did before, passing in the RawSourceWaveStream as your input:

var waveFormat = WaveFormat.CreateMuLawFormat(8000, 1);
var reader = new RawSourceWaveStream(tmpMemStream, waveFormat);
using (WaveStream convertedStream = WaveFormatConversionStream.CreatePcmStream(reader))
{
    WaveFileWriter.CreateWaveFile(recordingsPath + "/" + outputFileName, convertedStream);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文