简体   繁体   English

使用NAudio从MP3文件获取音频数据

[英]Getting Audio Data From MP3 File using NAudio

I want to be able to get audio data from an MP3 file with NAudio, average out the data in the left and right channels to create one dataset and then resample the averaged 44.1KHz audio data to 8Khz but I am having trouble understanding how data is represented in an NAudio Wavestream. 我希望能够使用NAudio从MP3文件中获取音频数据,对左右声道中的数据求平均以创建一个数据集,然后将平均的44.1KHz音频数据重新采样为8Khz,但是我在理解数据的方式时遇到了麻烦在NAudio Wavestream中表示。

If I had 1 sec worth of MP3 audio, then how many bytes would I have in the WaveStream? 如果我有1秒的MP3音频值,那么WaveStream中将有多少字节? By looking at a few code samples it seems one sample is 4 bytes and audio is sampled at 44100Hz and we have 2 different channels, so would that mean we would have (44100 * 4 * 2) bytes in the wavestream, is that right? 通过查看一些代码样本,似乎一个样本是4个字节,音频是以44100Hz采样的,并且我们有2个不同的通道,所以这意味着我们在波流中将有(44100 * 4 * 2)个字节,对吗?

Which of the following 3 streams - AStream,PCM and inputStream - should I use to get audio data from? 我应该使用以下3个流中的哪个流(AStream,PCM和inputStream)从中获取音频数据? And how to I access left and right channel data separately? 以及如何分别访问左右声道数据?

var AStream = new MP3FileReader(myFilePath);
var PCM = new WaveConversionStream.Createpcm(AStream);
var inputStream = new WaveChannel32(new BlockAlignStream(PCM));

I have been thinking of converting the WaveStream using the WaveFormatConversionStream but the code below throws a NAudio.MmException with a message saying "AcmNotPossible calling Acmstreamopen". 我一直在考虑使用WaveFormatConversionStream转换WaveStream ,但是下面的代码引发NAudio.MmException消息,消息为“ AcmNotPossible调用Acmstreamopen”。

 var targetFormat = new WaveFormat(8000,1);
 var resampled = new WaveFormatConversionStream(targetFormat, inputStream);

The above code doesn't even work if targetFormat is equal to inputStream's format, so I don't know what I am doing wrong here. 如果targetFormat等于inputStream的格式,则上面的代码甚至不起作用,所以我不知道我在做什么错。

//Still throws NAudio.MmException
var resampled = new WaveFormatConversionStream(inputStream.WaveFormat, inputStream);

Other Info: VS2012, WPF, NAudio 1.6. 其他信息:VS2012,WPF,NAudio 1.6。

You seem to have copied a code sample that belongs to a much earlier version of NAudio. 您似乎已经复制了一个代码样本,该样本属于NAudio的早期版本。 The Mp3FileReader class will emit 16 bit samples, and uses the ACM MP3 frame decompressor by default. Mp3FileReader类将发出16位样本,并且默认情况下使用ACM MP3帧解压缩器。 If you'd prefer your samples directly in floating point, then you can make use of the AudioFileReader . 如果您希望直接在浮点中采样,则可以使用AudioFileReader

Resampling 44.1kHz straight down to 8kHz is not a particularly good idea, as you'd end up with a lot of aliasing, so a low pass filter would ideally be applied first. 直接将44.1kHz采样到8kHz并不是一个好主意,因为最终会产生很多混叠,因此理想情况下首先应使用低通滤波器。 Left and right channels are stored interleaved, so you get a left sample, followed by a right sample, and so on. 左右声道以交错方式存储,因此您会得到一个左采样,然后是一个右采样,依此类推。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM