Search code examples
naudio

How to Get Audio sample data from mp3 using NAudio


I have an mp3 file into one large array of audio samples. I want the audio samples to be floats.

NAudio.Wave.WaveStream pcm=NAudio.Wave.WaveFormatConversionStream.CreatePcmStream(new NAudio.Wave.Mp3FileReader(OFD.FileName));

so far I get the pcm stream and can play that back fine but I don't know how to read the raw data out of the stream.


Solution

  • Use AudioFileReader. This implements ISampleProvider so the Read method allows you to read directly into a float array of samples.

    Alternatively use the ToSampleProvider method after your Mp3FileReader. You don't need to use WaveFormatConversionStream, since Mp3FileReader (and AudioFileReader) already decompress the MP3 frames.