Jochen
Jochen

Reputation: 98

Reading Audio Data from MP3 using Naudio

I want to split the audio data of a mp3 file and perform an FFT transformation on each of the chunks.

All of my attempts to read from the audio stream did not work so far. Here is the code for my last attempt (it uses a wav file)

        using (var ms = File.OpenRead("reggae.wav"))
         using (var rdr = new WaveFileReader(ms)) 
         {
             int steps = 100;
             int stepSize = (int)rdr.Length / steps;

             byte[][] audData = new byte[steps][];


             for (int i = 0; i < 10; i++)
             {
                 audData[i] = new byte[stepSize];
                 rdr.Read(audData[i], i * steps, stepSize);
             }

         }

I get an array out of bounds exception with this code.

What kind of streams do I have to use and how do I read the data from it?

Is there any documentation for the naudio api?

Upvotes: 1

Views: 6026

Answers (1)

Mark Heath
Mark Heath

Reputation: 49522

the array out of bounds exception is caused by you passing in a non-zero value for offset to the Read method. It should be:

rdr.Read(audData[i], 0, stepSize);

There are several articles on the web explaining how to do different things in NAudio

For examples of using FFT with NAudio, look at the WpfDemo app in the NAudio source code, and also at the .NET voice recorder application source code (hopefully another article on Coding4Fun will appear shortly).

Upvotes: 3

Related Questions