Tom Doodler
Tom Doodler

Reputation: 1559

Prevent audio lags on heavy cpu load

I'm creating an audio player with WPF and NAudio in C#. Whenever the performance of my computer is low, the audio starts to lag extremely which sounds aweful. I noticed that this does not seem to be the case for similar applications like Spotify or Windows Media Player.

How can I increase the performance of the audio thread? Is there a way to give it priority before other threads?

Edit: Code

WavePlayer = new WaveOut();
source = new AudioFileReader(Filepath)
WavePlayer.Init(source);
WavePlayer.Play();

Upvotes: 1

Views: 647

Answers (1)

Mark Heath
Mark Heath

Reputation: 49482

By default, in a WinForms / WPF app, WaveOut will use the UI thread to fill the audio buffers. If you use WaveOutEvent instead, you'll get a background thread doing that work for you. WasapiOut and DirectSoundOut also work this way.

Remember that if you can't fill buffers in a timely fashion you will get stuttering/drop outs in audio. So if switching driver model doesn't work for you, you might need to optimise your audio code, or increase the buffer durations.

Upvotes: 2

Related Questions