spatialaudio / python-sounddevice

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

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

Open phoenix-meadowlark opened 3 years ago

phoenix-meadowlark commented 3 years ago

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

mgeier commented 3 years ago

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?

phoenix-meadowlark commented 3 years ago

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.

mgeier commented 3 years ago

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!

imyxh commented 3 years ago

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.

mgeier commented 3 years ago

Thanks @imyxh, I totally overlooked this!

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