[英]Using NAudio to decode mu-law audio
I'm attempting to use NAudio to decode mu-law encoded audio into pcm audio. 我正在尝试使用NAudio将mu-law编码的音频解码为pcm音频。 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. 我的服务发布了原始mu-law编码的音频字节,并且我从NAudio收到一个错误,即数据没有RIFF标头。 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. 我还将原始数据保存到磁盘,并在Matlab中进行解码,这没有问题。 Thanks. 谢谢。
Since you just have raw mu-law data, you can't use a WaveFileReader on it. 由于您仅具有原始的mu-law数据,因此无法在其上使用WaveFileReader。 Instead, create a new class that inherits from WaveStream. 而是,创建一个继承自WaveStream的新类。
In its Read
method, return data from tmpMemStream. 在其Read
方法中,从tmpMemStream返回数据。 As a WaveFormat
return a mu-law WaveFormat. 作为WaveFormat
返回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: 现在,您可以像以前一样继续创建转换后的文件,将RawSourceWaveStream作为输入传递:
var waveFormat = WaveFormat.CreateMuLawFormat(8000, 1);
var reader = new RawSourceWaveStream(tmpMemStream, waveFormat);
using (WaveStream convertedStream = WaveFormatConversionStream.CreatePcmStream(reader))
{
WaveFileWriter.CreateWaveFile(recordingsPath + "/" + outputFileName, convertedStream);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.