If you call AudioDeviceManager::getAudioDeviceSetup(AudioDeviceSetup& setup) you can get an AudioDeviceSetup which represents the current device and contains a field called sampleRate.
The JUCE documentation, found in juce_AudioDeviceManager.h, warns you that this field will sometimes be 0, and says that in that case, it represents the “default rate” - but there is no idea of “default rate” in AudioIODevice or in AudioDeviceManager or AudioDeviceSetup.
So what do I do if I see a 0 in this field? Right now I replace it with a hard-coded 44100…
I think that this is a documentation error, perhaps? That sampleRate is only zero if the device hasn’t been opened yet - so if I’m trying to read a device and get a sampleRate of 0, it’s an error and I can’t proceed. Certainly, I haven’t been able to get a sample rate of zero with any devices I checked…
All it means is that if you pass 0 for the rate, then it’ll find out what rates the device supports, and will pick a sensible one - which will usually be the lowest rate that is >= 44100.
But I don’t think even your explanation is quite true - it doesn’t use the unit’s “default sample rate” - it simply doesn’t change the sample rate at all (which might not be the “default sample rate” if you’ve changed the rate), am I right?
I think the documentation is a little bit misleading because it gave me the impression that you might possibly read this parameter and get a 0, or that sending a 0 might reset the device’s sample rate to its initializated sample rate:
/** The current sample rate.
This rate is used for both the input and output devices.
A value of 0 indicates the default rate.
*/
Perhaps change it to something like:
/** The current sample rate.
This rate is used for both the input and output devices.
Set sampleRate to 0 to keep the existing sample rate unchanged.
*/
because this makes it clear that 0 is only a possibility on setting, not on reading.
?