filoe / cscore

An advanced audio library, written in C#. Provides tons of features. From playing/recording audio to decoding/encoding audio streams/files to processing audio data in realtime (e.g. applying custom effects during playback, create visualizations,...). The possibilities are nearly unlimited.
Other
2.21k stars 458 forks source link

Question about playback interruptions #73

Closed digimezzo closed 8 years ago

digimezzo commented 8 years ago

I have a question about playback interruptions. in my music player, during some UI operations (refreshing of lists, opening of a popup which has an image on it) I notice that sometimes (not always) playback shortly gets interrupted.

I thought this is caused by the UI thread being busy doing other stuff, however it doesn't seem consistent. I simulated long blocking of the UI thread. However audio continued playing without interruption. Interruptions seem to happen mostly during very short operations.

How can this be explained? Can UI operations have an effect on the CSCore playback thread? Could it be related by thread priority? Do I have to increase the priority of CSCore playback thread? If yes, how is this done?

I'd like to add that increasing the latency doesn't seem to improve this.

filoe commented 8 years ago

Which SoundOut implementation are you using? WaveOut, DirectSoundOut, WasapiOut, XAudio2?

digimezzo commented 8 years ago

I'm using WasapiOut.

digimezzo commented 8 years ago

I'm suspecting that latency might play a role after all. It seems that the interruptions sporadically occur during UI operations when using a latency which is lower than 400 ms. With latency >= 400 ms I was not yet able to reproduce the audio interruptions.

I'm now trying to figure out why it is not possible to go lower than 400 ms. It doesn't seem linked to locking of the UI thread, but it seems more like some UI operations (even very short) interrupt the audio thread.

This thread seems interesting: https://social.msdn.microsoft.com/Forums/vstudio/en-US/73860618-21cb-459f-af6d-6ecb77c9c5f1/latency-for-realtime-audio-in-clr?forum=clr

I wonder if the function DwmEnableMMCSS(TRUE); might be of any help. This is the MS page referring to this function: https://msdn.microsoft.com/en-us/library/windows/desktop/aa969511(v=vs.85).aspx

I'll do some tests.

Do you have experience with low latency audio in .NET?

Side note: I tried running CSCore in a separate executable (without UI) and passing progress and other information to the main executable (the UI) using WCF. This allowed using a latency of 100 or even 50 ms without interruptions. It's workable, but I'd prefer to keep UI and audio processing in the same executable if possible.

digimezzo commented 8 years ago

Ok, it seems I missed the constructor that allows me to provide the WasapiOut thread priority. Shame on me. That answers my question. As for DwmEnableMMCSS: The multimedia scheduler should allow for glitch free playback, but there is almost no info to find on the net it. I'm abandoning this idea for now.

filoe commented 8 years ago

Sorry for the laaate answer: As you've already mentioned, WasapiOut already got a parameter for the ThreadPriority. I'm already using mmcss sheduling(see code). But to be honest, the DwmEnableMMCSS function is new to me. Thanks for that hint, I'm going to have a look at it. Do you have any experience with that function yet?

I would guess, it is a generell performance problem. I've got a pretty strong hardware, and I've never had these problems. I could imagine that there are a few factors:

But as I said before, I'm going to have a look at the DwmEnableMMCSS function.

digimezzo commented 8 years ago

I'm glad my question might be useful after all :) I could only find very dispersed bits of info on the DwmEnableMMCSS function. It seems not very well documented. If I understand correctly it could allow glitchless playback. But it's not clear how to use it to achieve that, at least for me. Hopefully you have better understanding of mmcss.

I think your right about hardware and application tasks as the main source of audio interruptions. At this point (by trial and error) it appears that WPF operations, even while not blocking the UI) might be interfering with the audio thread. I've increased the WasapiOut priority from AboveNormal to Highest to verify if that is an improvement.

filoe commented 8 years ago

Any news, whether increasing the thread priority was helpful?

digimezzo commented 8 years ago

Yes. Increasing the thread priority allows using lower latencies. I'm able to use a latency of 200ms now, without playback interruptions when browsing through the UI. Conclusion: it is helpful.

filoe commented 8 years ago

Is this still a problem?

digimezzo commented 8 years ago

This is not a problem anymore. I'm closing the issue.