Storing a wav file in an array

[亡魂溺海] 提交于 2019-12-10 15:23:40

问题


I need a fast method to store all samples of a wav file in an array. I am currently working around this problem by playing the music and storing the values from the Sample Provider, but this is not very elegant.

From the NAudio Demo I have the Audioplayer Class with this Method:

   private ISampleProvider CreateInputStream(string fileName)
    {
        if (fileName.EndsWith(".wav"))
        {
            fileStream = OpenWavStream(fileName);
        }
          throw new InvalidOperationException("Unsupported extension");
        }
        var inputStream = new SampleChannel(fileStream, true);
        var sampleStream = new NotifyingSampleProvider(inputStream);
        SampleRate = sampleStream.WaveFormat.SampleRate;
        sampleStream.Sample += (s, e) => { aggregator.Add(e.Left); }; // at this point the aggregator gets the current sample value, while playing the wav file
        return sampleStream;
    }

I want to skip this progress of getting the sample values while playing the file, instead I want the values immediatly without waiting till the end of the file. Basically like the wavread command in matlab.


回答1:


Use AudioFileReader to read the file. This will automatically convert to IEEE float samples. Then repeatedly call the Read method to read a block of samples into a float[] array.



来源:https://stackoverflow.com/questions/15419446/storing-a-wav-file-in-an-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!