python-sounddevice icon indicating copy to clipboard operation
python-sounddevice copied to clipboard

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

Open HaroldMills opened this issue 2 years ago • 4 comments

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 avatar Nov 22 '23 16:11 HaroldMills

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 avatar Nov 22 '23 17:11 HaroldMills

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

HaroldMills avatar Nov 22 '23 17:11 HaroldMills

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?

mgeier avatar Nov 23 '23 17:11 mgeier

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

HaroldMills avatar Nov 24 '23 14:11 HaroldMills

Any news on this? Did you create a PortAudio issue?

mgeier avatar Aug 11 '24 13:08 mgeier

Thanks for the reminder. This slipped through the cracks, and I did not create a PortAudio issue. I'm reviewing the problem now and will follow up.

HaroldMills avatar Aug 14 '24 18:08 HaroldMills

When I opened this issue I was working with sounddevice 0.4.6 and PortAudio 19.6.0, and when I run the script I provided in my initial comment with those now it crashes with the same error message libc++abi: terminating. However, when I run the script with sounddevice 0.5.0 and PortAudio 19.7.0 (the latest versions) it does not crash, and when I write the samples returned by sd.rec to a .wav file they seem to be fine. So the issue appears to be fixed and I will close it. Thanks!

HaroldMills avatar Aug 15 '24 20:08 HaroldMills