PortAudio / portaudio

PortAudio is a cross-platform, open-source C language library for real-time audio input and output.
Other
1.45k stars 303 forks source link

CoreAudio backend: best possible samplerate isn't always (usually not) the next higher one #901

Open RJVB opened 5 months ago

RJVB commented 5 months ago

If I may be a bit pedantic:

The description of setBestSampleRateForDevice() says that

* otherwise      : set the exact sample rate.
 *             If that fails, check for available sample rates, and choose one
 *             higher than the requested rate. If there isn't a higher one,
 *             just use the highest available.
 */

https://github.com/PortAudio/portaudio/blob/5a1920db58e4aafea5fd0291aec702ca331ab5e1/src/hostapi/coreaudio/pa_mac_core_utilities.c#L530

Let's consider a common range of supported sample rates: 44100, 48000, 88200, 92000, 176400, 192000, and let's assume that setting the sample rate to 44.1kHz fails for some reason (say, the request was for 44099.99Hz). Assuming no further errors, the current "best" samplerate that gets selected is 48kHz. That's probably the worst choice of all the other options, and under ideal listening conditions a trained listener can hear that.

IMHO the algorithm should pick the next higher rate that is an integer multiple of the desired rate. I once wrote a rather exhaustive function to determine the closest supported nominal sample rate that could be of use here: AudioDevice::ClosestNominalSampleRate() .

(I seem to recall that at the time I considered contributing a change that sets the sample rate on the device to portaudio, but never managed due to the site where the code was hosted at the time - is that possible or a fake memory?)