thestk / rtaudio

A set of C++ classes that provide a common API for realtime audio input/output across Linux (native ALSA, JACK, PulseAudio and OSS), Macintosh OS X (CoreAudio and JACK), and Windows (DirectSound, ASIO, and WASAPI) operating systems.
Other
1.49k stars 318 forks source link

get notification when getDefaultOutputDevice() changes #348

Closed hayguen closed 2 years ago

hayguen commented 2 years ago

looks, WASAPI API doesn't offer the Mapper device, which would automatically react/handle changing default output device. is it possible to get a notification (new callback or notification bit in existing callback) if this happens?

hayguen commented 2 years ago

targeted is to have "playback follow active output device" - as in issue https://github.com/thestk/rtaudio/issues/338 - on option .. and getting a notification.

hayguen commented 2 years ago

meanwhile had a look at DirectSound backend: this one does offer a differently labeled "Mapper" device. but MME and DirectSound are labelled "legacy" from Microsoft: https://docs.microsoft.com/en-us/windows/win32/coreaudio/interoperability-with-legacy-audio-apis thus, doubting that DirectSound would be the right way to go!

garyscavone commented 2 years ago

Attempting to deal with device removals or connections in a clean way across multiple APIs is difficult. Most APIs will tell you when a device is removed and the newdeviceselection branch generally tries to deal with that if it is a device currently in use by closing the stream. From my investigations a few months ago, I did not see any API that would tell you (through a notifications) when new devices were connected ... only device removals.

Regarding a possible automatic switch to a different device when the default is changed, that would be quite challenging to handle in a robust and clean way with most APIs. I'm not saying it isn't possible but it would involve detecting the change, closing a stream with the old device and restarting it with the new device. For sure, data would be lost. There are many things that could go wrong in the process (insufficient number of channels, ...).

hayguen commented 2 years ago

Attempting to deal with device removals or connections in a clean way across multiple APIs is difficult.

i understand the problems .. and i'm not expecting "no loss" of data. in fact, i even won't expect automatic following as in issue https://github.com/thestk/rtaudio/issues/338. issue https://github.com/thestk/rtaudio/issues/338 looks sort of similar BUT orthogonal: with this issue, i do NOT expect to detect ANY change in device list - just a change of default device.

currently, i need to check default device's name (hoping it's sort of unique) and some additional device properties - periodically, to detect a changed default device. for sure, this might have some more problems (bugs) - compared to some clean(er) interface in RtAudio. With access to the native API, checking change of default device might use more details/properties.

different channel size: yes, that's why a notification is required, that an error message may get shown - if application requires same or minimum number of channels. similar: new default device doesn't support a required samplerate, or ..

how to proceed on this issue, to get a notification?

garyscavone commented 2 years ago

At this point, I think you would need to periodically poll the getDefaultDevice functions. If the devices are not actually in use by an open stream, then RtAudio doesn't know about possible changes to the default devices until the devices are queried. And even if one was in use, RtAudio would know that it was changed from the default (as things are currently written).

garyscavone commented 2 years ago

I'm closing this, as I don't see a way to implement it internally given the behaviour of the various APIs.