Sample rate: return effective value, option to disable auto-conversion
I am writing a program based on LibAO that synthesizes sound. For this to work correctly, I need to know the exact sample rate that is in effect.
Unfortunately, this is at present not so straightforward in LibAO. Some drivers may not give you the exact rate you request, but something close. (Problem: You have no way of knowing what the real rate is.) Some drivers, like "alsa", will actually give you any arbitrary sample rate you choose (let's do 512 kHz!) but actually resample in software to whatever the hardware wants. (Problem: This eats up CPU, and messes up my synthesis.)
The attached patch (against git master) addresses these issues:
- As far as I can tell, only the "alsa", "macosx" and "oss" drivers can give you a sample rate different than what you requested. In these, I added code to update the ao_sample_format struct that was passed in to reflect the actual rate. This way, the calling program can subsequently examine the struct to see what it got.
(The same thing could potentially be done with other parameters in there, like the channels. I think this would be a good pattern to follow generally, as the struct is otherwise useless after the ao_open_*() call.)
- Pass in SND_PCM_NO_AUTO_RESAMPLE to the snd_pcm_open() call in ao_alsa.c, to disable the "dmix" resampling layer. If you request a rate of 512000, you should see a "sample rate not supported" warning and get an actual rate like 48000; you should not get a software simulation of a 512 kHz ultra-hi-def sound card.
(FYI: the snd_pcm_hw_params_set_rate_resample() function may be another way of going about this. I used the aforementioned open-mode flag only because I saw its use in a patch to VLC's ALSA driver.)