This project has moved and is read-only. For the latest updates, please go here.

Using BinaryReader with Wavestream problem

Dec 21, 2014 at 1:10 AM
Good day everyone.

First of all a big thank you to Mark for doing such a great job on NAudio. The source code is very well written.

I seem to have hit a snag trying to get this to work. So here is the deal: I am reading an MP3 file with MP3FileReader... then converting with WaveStream... then using a binaryreader read a portion of the Wavestream converting the bytes to floats.

Here is the portion of the code that presumably does this:
 static void Main(string[] args)
        {
           
            WaveFormat wFormat = new WaveFormat(11025,16,1);

            int bSize = 11025*2*10;
            //short[] buffer = new short[bSize];
            float[] fBuff = new float[bSize*3];

            string file = args[0];

            using(Mp3FileReader mp = new Mp3FileReader(file))
            {
                using(WaveStream ws = new WaveFormatConversionStream(wFormat,mp))
                {
                    

                    using(BinaryReader bs = new BinaryReader(ws))
                    {
                        
                        int count = 0;
                        int pos = bSize*3; //skip 10 seconds
                        int length = (int)bs.BaseStream.Length;
                        int required = bSize * 3; // get 30 seconds
                     
                        bs.BaseStream.Seek(pos, SeekOrigin.Begin);
                       

                        while (pos < length && count < required)
                        {
                            fBuff[count] = (float)bs.ReadInt16();
                                // 5.
                            // Increment the variables.
                            pos++;
                            count++;
                        }
                        
                     }
                   

                 }
The code works fine but regardless of where I set "pos" the first approx 600 bytes (300 16 bit floats)are always 0.

Anyone have any idea why this is occurring and how to correct it.

Thanks in advance

Ray
Dec 28, 2014 at 3:27 PM
hi Ray,
first of all, no need for the conversion stream - MP3fileReader returns PCM by default.
Second, if you use the .ToSampleProvider() extension method on your reader, you can just call Read and get floats directly.

Mark
Jan 4, 2015 at 4:50 AM
Edited Jan 4, 2015 at 4:55 AM
Hi Mark,

Thanks so much for taking the time to address all these questions and helping all of us out with NAudio. I am still having some difficulty with the same issue I had previously asked. The reason that I am using the WaveFormatConversion Stream is because I am trying to convert from MP3 then to 11025hz, mono, single channel PCM. Then convert that to float[] which is then sent to Echoprint Codegen to make an audio fingerprint for use in identifying songs.
Depending on what I use for the sample.mp3 the result is always the same with the first approx 300 bytes from the wave conversion stream is always zeros. One of the key things is that you may need to know is that I am trying to extract 30 seconds of the mp3, 10 seconds into the audio.

The original Codegen.exe uses FFMPEG to convert an mp3 and return the requested float[] and when I run the sample.mp3 through ffmpeg and compare it to what i get using NAudio they are not the same.

Any advice you can give would be most helpful and thanks again for spending your time responding to these questions.

I should also say that I have not been able to figure out how to use the .ToSampleProvider() that you suggested I use.

BTW " I would be your friend as I like talking about C# and .Net programming :) I would even buy the coffee"

Regards

Ray
Jan 9, 2015 at 10:57 PM
hi Ray,
I'd suggest repositioning on the Mp3WaveFileReader before going through WaveFormatConversionStream
Then you can call .ToSampleProvider on your waveFormatConversionStream, and then call Read to read out some floating point samples. I wouldn't use BinaryReader at all.

If you are expecting byte for byte identical results to using ffmpeg though, you will likely be disappointed.