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

sd.rec returning white noise with 10**38 amplitude on macOS Big Sur.

Open phoenix-meadowlark opened this issue 3 years ago • 5 comments

On macOS this code is producing a mixture of NaN, all-zero and ~10**38 amplitude waves:

import sounddevice as sd

sample_rate = 44100
duration = 1
audio = sd.rec(duration * sample_rate, samplerate=sample_rate, channels=1)
print(f"{np.mean(audio)=}, {np.std(audio)=}, {np.min(audio)=}, {np.max(audio)=}")
# Example outputs
np.mean(audio)=nan, np.std(audio)=nan, np.min(audio)=nan, np.max(audio)=nan
np.mean(audio)=nan, np.std(audio)=nan, np.min(audio)=-3.2219445e+38, np.max(audio)=3.2167317e+38
np.mean(audio)=0.0, np.std(audio)=0.0, np.min(audio)=0.0, np.max(audio)=0.0

image

phoenix-meadowlark avatar Feb 28 '21 22:02 phoenix-meadowlark

This is quite bad, but I don't know what I could do about it on the Python side.

What happens if you select a different dtype?

What happens if you use other software that uses PortAudio, e.g. Audacity?

mgeier avatar Mar 01 '21 18:03 mgeier

I downgraded to python 3.6.10 to use pyaudio earlier this week, and that is working as expected.

I installed sounddevice in the same venv and am observing the same behavior described above. pyaudio was 'recording' all zeros when I tried to run it from vscode's terminal, but it works when running from iterm (which I was able to give microphone permissions). sounddevice behaves the same in both terminal environments.

phoenix-meadowlark avatar Mar 06 '21 19:03 phoenix-meadowlark

Thanks for the update!

Both PyAudio and the sounddevice module use PortAudio, so I would expect the same behavior ...

Maybe you are using different versions?

What does sd.get_portaudio_version() say?

What is the value of sd._libname?

Have you installed portaudio with brew?

You can try to compile the latest version, or you can try to uninstall it, maybe that helps.

If it still doesn't work, there is hope nevertheless, because it looks like a new PortAudio release will happen within the next few weeks!

mgeier avatar Mar 08 '21 18:03 mgeier

Try the blocking=True argument. Fixed the same problem for me, because I was mistakenly drawing the graph before waiting for the recording to finish.

imyxh avatar Jun 17 '21 23:06 imyxh

Thanks @imyxh, I totally overlooked this!

Instead of blocking=True one can also use sd.wait().

mgeier avatar Jun 18 '21 14:06 mgeier