naudio / NAudio

Audio and MIDI library for .NET
MIT License
5.37k stars 1.09k forks source link

Initializing a Wasapi device can tak a long time #1127

Open teekay opened 4 months ago

teekay commented 4 months ago

I have the following test code:

var enumerator = new MMDeviceEnumerator();
var endpoints = enumerator.EnumerateAudioEndPoints(DataFlow.Render, DeviceState.Active)
                .ToList();
var soundCard = endpoints.FirstOrDefault(ep => ep.FriendlyName == "Sound Blaster"); // getting an arbitrary device by name - this is an example
var watch = Stopwatch.StartNew();
var conn = soundCard.AudioClient;
var elapsed = watch.ElapsedMilliseconds;
watch.Stop();

I have observed that it may take as few as 5 ms and as many as 4000+ ms to "get" the AudioClient depending on which sound card I am initializing.

I wonder what may be the underlying cause for the delays?

PS I don't have this exact code in my app - there, I'm initializing WasapiOut and it's there where I observed the delays first. This is the place where I was able to pinpoint the source of the delay.