spatialaudio / python-sounddevice

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

InputStream receiving zero amplitude samples from mic #458

Open arthurschiro opened 1 year ago

arthurschiro commented 1 year ago

Hi, I am able to use sounddevice to play sound but the samples I acquire are all zero in amplitude. I have shown that I can play sound by running the play_sine.py example, which generated a sine tone from my computer speakers. When I run spectrogram.py or plot_input.py however, I find that the acquired samples are all zero in amplitude. I am able to both acquire microphone samples and play sound through audacity. I have set up another laptop with the same configuration and I am able to both acquire and play samples using sounddevice.

My computer is an Acer Aspire AV15-52 I am running Ubuntu 22.04

I get the following output print(sd.query_devices()) 0 sysdefault, ALSA (128 in, 128 out) (*) 1 default, ALSA (128 in, 128 out) 2 dmix, ALSA (0 in, 2 out)

print(sd.get_portaudio_version()) (1246720, 'PortAudio V19.6.0-devel, revision 396fe4b6699ae929d3a685b3ef8a7e97396139a4')

I have run the alsamixer command and made sure that my mics are all unmuted and set to the maximum volume

From alsamixer I see: Card: sof-hda-dsp Chip: Realtek ALC256

I am managing my python environment with conda so I have performed the following operations in order to get sounddevice working in this environment: sudo apt install libportaudio2
conda install -c conda-forge python-sounddevice
conda uninstall --force portaudio

If anyone has a suggestion of what I might try next, it would be greatly appreciated. Thanks, Arthur

mgeier commented 1 year ago

Sorry for the late response.

Your device list looks suspiciously short. Do you see more devices in the device list provided by Audacity?

What does aplay -L show?

arthurschiro commented 1 year ago

I downloaded the portaudio source code and used their example programs to confirm that I am able to use portaudio to record and play back audio. The portaudio source code is version V19.7.0-devl. Using their examples, written in c, I can see that portaudio detects 14 devices and the default inpu/output devices are both device 13.

Audacity fails to run when I attempt to run it using the libportaudio.so.2 dependency compiled from the portaudio source code. The error is "audacity: symbol lookup error: audacity: undefined symbol: Pa_GetStreamHostApiType".

Audacity runs fine when I run it using the libportaudio.so.2 shared object that was installed with audacity. This portaudio version is V19.6.0-devl. And this is the same version that is installed by conda when I use conda to install sounddevice.

When running Audacity with the correct portaudio dependencies, I am able to record and play back audio. It also detects 10 input devices and 12 output devices.

The output from aplay -L is as follows:

null
    Discard all samples (playback) or generate zero samples (capture)
default
    Playback/recording through the PulseAudio sound server
samplerate
    Rate Converter Plugin Using Samplerate Library
speexrate
    Rate Converter Plugin Using Speex Resampler
jack
    JACK Audio Connection Kit
oss
    Open Sound System
pulse
    PulseAudio Sound Server
upmix
    Plugin for channel upmix (4,6,8)
vdownmix
    Plugin for channel downmix (stereo) with a simple spacialization
hw:CARD=sofhdadsp,DEV=0
    sof-hda-dsp, 
    Direct hardware device without any conversions
hw:CARD=sofhdadsp,DEV=1
    sof-hda-dsp, 
    Direct hardware device without any conversions
hw:CARD=sofhdadsp,DEV=3
    sof-hda-dsp, 
    Direct hardware device without any conversions
hw:CARD=sofhdadsp,DEV=4
    sof-hda-dsp, 
    Direct hardware device without any conversions
hw:CARD=sofhdadsp,DEV=5
    sof-hda-dsp, 
    Direct hardware device without any conversions
plughw:CARD=sofhdadsp,DEV=0
    sof-hda-dsp, 
    Hardware device with all software conversions
plughw:CARD=sofhdadsp,DEV=1
    sof-hda-dsp, 
    Hardware device with all software conversions
plughw:CARD=sofhdadsp,DEV=3
    sof-hda-dsp, 
    Hardware device with all software conversions
plughw:CARD=sofhdadsp,DEV=4
    sof-hda-dsp, 
    Hardware device with all software conversions
plughw:CARD=sofhdadsp,DEV=5
    sof-hda-dsp, 
    Hardware device with all software conversions
sysdefault:CARD=sofhdadsp
    sof-hda-dsp, 
    Default Audio Device
dmix:CARD=sofhdadsp,DEV=0
    sof-hda-dsp, 
    Direct sample mixing device
dmix:CARD=sofhdadsp,DEV=1
    sof-hda-dsp, 
    Direct sample mixing device
dmix:CARD=sofhdadsp,DEV=3
    sof-hda-dsp, 
    Direct sample mixing device
dmix:CARD=sofhdadsp,DEV=4
    sof-hda-dsp, 
    Direct sample mixing device
dmix:CARD=sofhdadsp,DEV=5
    sof-hda-dsp, 
    Direct sample mixing device
usbstream:CARD=sofhdadsp
    sof-hda-dsp
    USB Stream Output
mgeier commented 1 year ago

Thanks for the update.

You should definitely get a longer list of devices with sd.query_devices().

Did you try uninstalling the conda stuff and use pip install sounddevice and install libportaudio2 with your package manager?

What does print(sd._libname) show?

arthurschiro commented 1 year ago

Hi Matthias, sounddevice is now working.

I created a new conda environment and used pip to install the latest sounddevice version (0.4.6) into it. I already had the portaudio dependencies installed (V19.6.0) from when I installed audacity so I did not use apt to re-install or update the portaudio version (since apt informs me that V19.6.0 is the newest portaudio version it has access to).

So, sounddevice version 0.4.6, installed with pip, is working with portaudio version 19.6.0, installed with apt.

Do you have any idea why the conda version of sounddevice (version 0.4.1) is unable to record audio on my machine? Are other people running into this issue? Maybe the conda sounddevice package needs an update?

Thanks, Arthur

mgeier commented 1 year ago

So, sounddevice version 0.4.6, installed with pip, is working with portaudio version 19.6.0, installed with apt.

I'm glad it works for you now!

Do you have any idea why the conda version of sounddevice (version 0.4.1) is unable to record audio on my machine? Are other people running into this issue?

I don't know why it doesn't work, but there have been several issues here, where people had problems with the PortAudio library from conda.

The typical solution is not to use the PortAudio library from conda.

Maybe the conda sounddevice package needs an update?

Maybe. There are several un-merged PRs for newer versions: https://github.com/conda-forge/python-sounddevice-feedstock/pulls

The package seems to be unmaintained, maybe I should remove it from the installation instructions (https://python-sounddevice.readthedocs.io/en/0.4.6/installation.html)?

The problem might also be in the portaudio package: https://github.com/conda-forge/portaudio-feedstock.