使用n音讯脱codeμ律音频 [英] Using NAudio to decode mu-law audio

查看:201
本文介绍了使用n音讯脱codeμ律音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用n音讯脱codeμ律恩codeD音频成PCM音频。我服务过帐原料μ律恩codeD音频字节,我发现了一个错误,从n音讯,数据不具有RIFF头。我是否需要添加此不知何故?在code我使用的是:

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

我还原始数据保存到磁盘,做这正与没有问题的解码在Matlab。谢谢你。

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

推荐答案

由于您只需要原始μ律数据,则不能在其上使用WaveFileReader。相反,创建一个新的类继承自的WaveStream。

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.

在其方法,从tmpMemStream返回数据。作为 WAVEFORMAT 返回μ律WAVEFORMAT。

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

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

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

这篇关于使用n音讯脱codeμ律音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆