CreateWaveFile and it's progress

Dec 18, 2011 at 4:26 PM
Edited Dec 18, 2011 at 4:26 PM

First off, I am a complete newb with NAudio but I am loving it so far!

One question, I am decoding long MP3's (an average 60 minutes or more) using the following:

 

using (NAudio.Wave.Mp3FileReader reader = new NAudio.Wave.Mp3FileReader(r.Cells["Path"].Value.ToString()))
{
     using (NAudio.Wave.WaveStream pcmStream = NAudio.Wave.WaveFormatConversionStream.CreatePcmStream(reader))
     {
          NAudio.Wave.WaveFileWriter.CreateWaveFile(Guid.NewGuid().ToString() + ".wav", pcmStream);
     }
}

 

My question is, it is possible to get the progress of the wav file creation so I can show that to the user in a progress bar or some other UI element?  If there isn't an out of the box implementation for this, can someone point me in the right direction on where to start?

Cheers and thanks NAudio guys / gals!

Coordinator
Dec 18, 2011 at 9:32 PM

The latest Mp3FileReader returns PCM already so the WaveFormatConversionStream is unnecessary. You should be able to use the position and length properties of the Mp3FileReader to show progress, but to do so, don't use CreateWaveFile directly. Instead copy the few lines of code from CreateWaveFile and put a progress update within the loop.

Dec 21, 2011 at 2:18 AM

Sweet deal, did exactly as you suggested and works like a charm.  Thanks!

Sep 2, 2012 at 9:03 PM
Edited Sep 2, 2012 at 9:04 PM

@markheath Could you show some example code of how to show progress bar value of conversion progress? I've been trying to perform your method and I'm lost.

Sep 4, 2012 at 11:43 PM

Background worker is too slow for file conversion to be as efficient.

I'm having trouble doing an invoke method. Can u please show us some code?

Coordinator
Sep 5, 2012 at 4:12 PM

all I meant was to do something like this:

progressBar.Maximum = mp3FileReader.Length;
using (WaveFileWriter writer = new WaveFileWriter(filename, mp3FileReader.WaveFormat))
{
    byte[] buffer = new byte[mp3FileReader.WaveFormat.AverageBytesPerSecond * 4];
    while (true)
    {
        int bytesRead = mp3FileReader.Read(buffer, 0, buffer.Length);
        progressBar.Value += bytesRead;
        if (bytesRead == 0)
            break;
        writer.Write(buffer, 0, bytesRead);
    }
}
Obviously you might want to work on a background thread, and so you would need to make changes to the progress bar values on the GUI thread

Sep 7, 2012 at 12:54 AM

thanks! works like a charm!