spatialaudio / python-sounddevice

:sound: Play and Record Sound with Python :snake:
https://python-sounddevice.readthedocs.io/
MIT License
980 stars 145 forks source link

check_input_settings does not raise exception for some unsupported sample rates on macOS #505

Open HaroldMills opened 7 months ago

HaroldMills commented 7 months ago

On my 2019 MacBook Pro running macOS 13.6.1, the following script:

import sounddevice as sd

settings = {'device': 1, 'channels': 1, 'samplerate': 44100, 'dtype': 'int16'}
sd.check_input_settings(**settings)
sd.rec(frames=settings['samplerate'], blocking=True, **settings)

(you might need to modify the device index for a different computer) first checks if the specified input settings are supported and then, if they are (i.e. if the call to check_input_settings does not raise an exception), attempts to record for one second with those settings. For my Mac's built-in microphone this works as expected for a sample rate of 44100 Hz. For various other sample rates, however, including 48000, 24000, and 22050 Hz, it does not. For those sample rates the call to check_input_settings does not raise an exception, but the script terminates inside the call to rec with the console message:

libc++abi: terminating

It seems that this may be a bug in check_input_settings, which should raise an exception for unsupported sample rates.

HaroldMills commented 7 months ago

Is there another way to determine whether or not a given sample rate is supported on macOS? I tried using a stream to record, like this:

stream = sd.InputStream(**settings)
stream.start()
stream.read(settings['samplerate'])

hoping that the initializer might raise an exception for an unsupported sample rate, but it does not. The read fails with the same console message that rec produces, as in my original comment.

HaroldMills commented 7 months ago

check_input_settings does reject some sample rates on macOS, namely ones outside of the range [1000, 384000].

mgeier commented 7 months ago

check_input_setting() only calls Pa_IsFormatSupported() from the underlying PortAudio library:

https://github.com/spatialaudio/python-sounddevice/blob/65d3bc0cd77a6df2d4e93daf41532de520b65790/sounddevice.py#L680-L683

Can you please ask your question at https://github.com/PortAudio/portaudio/issues or at the PortAudio mailing list?

HaroldMills commented 7 months ago

Thanks for the suggestion. I'll look into this as a PortAudio issue.