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

streaming mp3 and fading

Feb 27, 2013 at 10:06 PM
Hi! Firstly i want to say big thanks for NAudio, it's very useful tool.
I am trying to get several icecast mp3 streams and show it on progressbar. It works fine, but i have a little problem, after some time my progressbar fade, looks like a frozen. My code is based on Mp3StreamingDemo and i need some help to understand reasons of this situation.

Could you help me to understand this part of code? It writes to my listbox "Buffer getting full, taking a break" many times.
                        if (bufferedWaveProvider != null && bufferedWaveProvider.BufferLength - bufferedWaveProvider.BufferedBytes < bufferedWaveProvider.WaveFormat.AverageBytesPerSecond / 4)
                        {
                            formobj.addtolistbox("Buffer getting full, taking a break ");
                            Thread.Sleep(500);
                        }
Maybe i should insert bufferedWaveProvider.ClearBuffer(); there?
Feb 27, 2013 at 10:14 PM
If you call Thread.Sleep on your GUI-Thread your progressbar will freeze because new WindowMessages won't be processed for the next 500 ms.
Don't call this on your GUI-Thread. But I don't know whats your exactly your problem. The bufferedWaveProvider buffers audiodata from your mp3-stream into a buffer(see the name -> bufferedWaveProvider -> buffers wave). If this buffer is getting full you stop buffering for a while so the buffer can get empty. If it is filled with less than 75% of the space you will buffer again.

But you may would have to explain us your problem a bit more in detail.
Feb 27, 2013 at 11:11 PM
Edited Feb 27, 2013 at 11:13 PM
Thanks for reply. The problem is that progressbar doesn't change (keep freeze state) and throws that message ("Buffer getting full, taking a break") in infinite loop. It seems that buffer can't get full. But if i add "bufferedWaveProvider.ClearBuffer();" after "Thread.Sleep(500);" it works ok. I can't understand the reason of that.

P.S. about 10 seconds it works fine but then occurs this problem
Feb 28, 2013 at 5:33 PM
Are you sure you are playing the buffer?
The princip of that buffering system is quite easy:
On the one end you have webstream and on the other there is your output device.
What you are doing ist just to read mp3-data from the webstream, decode the mp3 data to pcm and write them into a buffer. So the buffer is used by your buffering mechanism to store data in and is also used by your outputdevice like DirectSoundOut to read data from and send the raw data from the buffer to your speaker.

If you won't play the stream you will keep up buffering data into the buffer. But you won't remove any data from the buffer. -> "Buffer getting full".
If you call bufferedWaveProvider.ClearBuffer() you will remove ALL buffered data from your buffer so it won't be full anymore. But that should not be the goal to reach.
The buffer should be cleared by your outputdevice. Your outputdevice reads data and removes the read data. So if it removes the data you won't have to call ClearBuffer(). The 500 ms sleeping is built in if the buffer is getting filled up faster than the outputdevice requests and removes data from it. So you will pause the buffering mechanism for 500 ms by calling Thread.Sleep(500) and your outputdevice will have the chance to remove enough data. If not Sleep(500) is called again until the buffer is just filled up with about 75% of data.
Mar 1, 2013 at 8:31 AM
Thanks, filoe. In my case, i read the buffer, decode to pcm and write into a buffer. Then i take any one pcm point value and show its level on my progressbar (because firstly i need to indicate if that stream is working now). But i have a button on my form which provides oppotunity to listen this stream. So if i press the button my buffer clears by outputdevice and in cycle too. Is it bad decision?
Mar 1, 2013 at 8:41 AM
I don't know why do you try to readout the level of a pcm value. This is not necessary. If you can't create a connection to the stream you will get an webexception and if the stream is not a mp3-stream you will get an exception if you try to decode the frames.

So why?
Mar 1, 2013 at 9:10 AM
Edited Mar 1, 2013 at 9:15 AM
Well, because my program provides monitoring of some radio station, so i need to know that some music plays or if there is a silence right now. Connection could be ok, but with silence or some noise for listeners
Mar 1, 2013 at 9:15 AM
Hmmm double post?
Don't check the level of "pcm point value". Work with exceptions to indicate whether the stream works.
"In my case, i read the buffer, decode to pcm and write into a buffer." ist not correct.
"In my case, i read the stream, decode to pcm and write into a buffer." is correct.
Mar 1, 2013 at 9:39 AM
Edited Mar 1, 2013 at 9:45 AM
sorry, i just tried to renew the page. And yes, english isn't my native language :)

I work with exceptions but it's very desirable to indicate in progressbar because it's very informative. Besides, it works fine but not very steady (for a long time) yet
Mar 1, 2013 at 11:31 AM
I am german => my english is also very bad :).
But you may tell me what exactly fails after some time? If I am right you want to display the amount of buffered data? Where is your problem?

PS: Deutsch?