简体   繁体   English

使用NAudio将Mp3转换为数组

[英]Manipulating Mp3's as Array Using NAudio

I'm trying to reimplement an existing Matlab 8-band equalizer GUI I created for a project last week in C#. 我正在尝试重新实现上周在C#中为项目创建的现有Matlab 8频段均衡器GUI。 In Matlab, songs load as a dynamic array into memory, where they can be freely manipulated and playing is as easy as sound(array). 在Matlab中,歌曲作为动态数组加载到内存中,可以在其中自由操作它们,并且播放与声音(数组)一样容易。

I found the NAudio library which conveniently already has Mp3 extractors, players, and both convolution and FFT defined. 我找到了NAudio库,该库已经方便地具有Mp3提取器,播放器以及已定义的卷积和FFT。 I was able to open the Mp3 and read all its data into an array (though I'm not positive I'm going about it correctly.) However, even after looking through a couple of examples, I'm struggling to figure out how to take the array and write it back into a stream in such a way as to play it properly (I don't need to write to file). 我能够打开Mp3并将其所有数据读取到一个数组中(尽管我不是很肯定,我正在正确地解决它。)但是,即使在看了几个示例之后,我仍在努力寻找如何采取数组并将其写回到流中,以使其能够正常播放(我无需写入文件)。

Following the examples I found, I read my mp3's like this: 按照我发现的示例,我像这样阅读mp3:

   private byte[] CreateInputStream(string fileName)
    {
        byte[] stream;
        if (fileName.EndsWith(".mp3"))
        {
            WaveStream mp3Reader = new Mp3FileReader(fileName);
            songFormat = mp3Reader.WaveFormat; // songFormat is a class field
            long sizeOfStream = mp3Reader.Length;
            stream = new byte[sizeOfStream];
            mp3Reader.Read(stream, 0, (int) sizeOfStream);
        }
        else
        {
            throw new InvalidOperationException("Unsupported Exception");
        }
        return stream;
    }

Now I have an array of bytes presumably containing raw audio data, which I intend to eventually covert to floats so as to run through the DSP module. 现在,我有一个字节数组,大概包含原始音频数据,我打算将它们最终转换为浮点数,以便通过DSP模块运行。 Right now, however, I'm simply trying to see if I can play the array of bytes. 但是,现在,我只是在尝试查看是否可以播放字节数组。

        Stream outstream = new MemoryStream(stream);
        WaveFileWriter wfr = new WaveFileWriter(outstream, songFormat);
        // outputStream is an array of bytes and a class variable
        wfr.Write(outputStream, 0, (int)outputStream.Length);
        WaveFileReader wr = new WaveFileReader(outstream);
        volumeStream = new WaveChannel32(wr);
        waveOutDevice.Init(volumeStream);
        waveOutDevice.Play();

Right now I'm getting errors thrown in WaveFileReader(outstream) which say that it can't read past the end of the stream. 现在,我在WaveFileReader(outstream)中引发了错误,该错误表示无法读取流的末尾。 I suspect that's not the only thing I'm not doing correctly. 我怀疑这不是我做不正确的唯一事情。 Any insights? 有什么见解吗?

Your code isn't working because you never close the WaveFileWriter so its headers aren't written correctly, and you also would need to rewind the MemoryStream . 您的代码无法正常工作,因为您从未关闭WaveFileWriter因此其标题未正确编写,并且还需要倒带MemoryStream

However, there is no need for writing a WAV file if you want to play back an array of byes. 但是,如果要播放再见数组,则无需编写WAV文件。 Just use a RawSourceWaveStream and pass in your MemoryStream. 只需使用RawSourceWaveStream并传递您的MemoryStream。

You may also find the AudioFileReader class more suitable to your needs as it will provide the samples as floating point directly, and allow you to modify the volume. 您可能还会发现AudioFileReader类更适合您的需求,因为它将直接提供样本作为浮点,并允许您修改音量。

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

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