Raspberry Pi4 Buster Alsa Mics Not Working

Tried all configurations but cannot get pyaudio to record :frowning:

trying on this example
https://matrix-io.github.io/matrix-documentation/matrix-lite/py-reference/alsa-mics/

I keep getting this error

Expression ‘alsa_snd_pcm_hw_params_set_period_size_near( pcm, hwParams, &alsaPeriodFrames, &dir )’ failed in ‘src/hostapi/alsa/pa_linux_alsa.c’, line: 924

Expression ‘alsa_snd_pcm_hw_params_set_period_size_near( pcm, hwParams, &alsaPeriodFrames, &dir )’ failed in ‘src/hostapi/alsa/pa_linux_alsa.c’, line: 924

ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.front

ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear

ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe

ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side

ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround21

ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround21

ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround40

ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround41

ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround50

ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround51

ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround71

ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958

ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958

ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958

ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi

ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi

ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem

ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem

ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline

ALSA lib pcm.c:2565:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline

Expression ‘alsa_snd_pcm_hw_params_set_period_size_near( pcm, hwParams, &alsaPeriodFrames, &dir )’ failed in ‘src/hostapi/alsa/pa_linux_alsa.c’, line: 924

ALSA lib pcm_dsnoop.c:575:(snd_pcm_dsnoop_open) The dsnoop plugin supports only capture stream

ALSA lib pcm_dsnoop.c:575:(snd_pcm_dsnoop_open) The dsnoop plugin supports only capture stream

ALSA lib pcm_dsnoop.c:575:(snd_pcm_dsnoop_open) The dsnoop plugin supports only capture stream

ALSA lib pcm_dsnoop.c:575:(snd_pcm_dsnoop_open) The dsnoop plugin supports only capture stream

ALSA lib pcm_dsnoop.c:575:(snd_pcm_dsnoop_open) The dsnoop plugin supports only capture stream

ALSA lib pcm_dsnoop.c:575:(snd_pcm_dsnoop_open) The dsnoop plugin supports only capture stream

ALSA lib pcm_dsnoop.c:575:(snd_pcm_dsnoop_open) The dsnoop plugin supports only capture stream

ALSA lib pcm_dsnoop.c:575:(snd_pcm_dsnoop_open) The dsnoop plugin supports only capture stream

ALSA lib pcm_dsnoop.c:575:(snd_pcm_dsnoop_open) The dsnoop plugin supports only capture stream

Expression ‘alsa_snd_pcm_hw_params_set_period_size_near( pcm, hwParams, &alsaPeriodFrames, &dir )’ failed in ‘src/hostapi/alsa/pa_linux_alsa.c’, line: 924

Expression ‘parameters->channelCount <= maxChans’ failed in ‘src/hostapi/alsa/pa_linux_alsa.c’, line: 1514

Expression ‘ValidateParameters( inputParameters, hostApi, StreamDirection_In )’ failed in ‘src/hostapi/alsa/pa_linux_alsa.c’, line: 2818

Traceback (most recent call last):

File “vad.py”, line 17, in

frames_per_buffer=CHUNK)

File “/home/pi/env/lib/python3.7/site-packages/pyaudio.py”, line 750, in open

stream = Stream(self, *args, **kwargs)

File “/home/pi/env/lib/python3.7/site-packages/pyaudio.py”, line 441, in init

self._stream = pa.open(**arguments)

OSError: [Errno -9998] Invalid number of channels

@varungujjar,

Let me test this and get back to you. I haven’t actually tried this specific example on the Pi 4 yet.

Best,
Samreen

@Samreen any luck with this…? am yet unable to get this work… rest everything else works on rpi4 … except for pi4 python alsa picking up the microphone :slight_smile:

@varungujjar,

@Carlos or I will test today and get back to you. Thank you for your patience!

Best,
Samreen

Hi @varungujjar,

@Carlos was able to replicate your issue and we found something that works for us.

Create a file called index.py with the following code:

import pyaudio

p = pyaudio.PyAudio()
info = p.get_host_api_info_by_index(0)
numdevices = info.get('deviceCount')
for i in range(0, numdevices):
    if (p.get_device_info_by_host_api_device_index(0, i).get('maxInputChannels')) > 0:
        print("Input Device id ", i, " - ",
              p.get_device_info_by_host_api_device_index(0, i).get('name'))

Run the code with python3 index.py. The output should end with something like the following:
image

As you can see, in my case, MATRIXIO-SOUND which is the direct MATRIX mics has device id 3 and so that is the number I will use.

In your recording python file, include the following amendment (make sure to change the input_device_index number if yours comes out different).

stream = audio.open(format=FORMAT, channels=CHANNELS,
                    rate=RATE, input=True,
                    frames_per_buffer=CHUNK,
                    input_device_index=3)

Now recording should work. You’ll likely see some of the same warnings regarding ALSA dsnoop but the recording should go through smoothly.

Let me know how it goes!

Best,
Samreen

you guyz are amazing… it works! :smiley: thankx will try a few python projects and let u know how it goes …

1 Like

@Samreen @Carlos is there a way i can increase the gain of the micrphone from python… ? mozilla deepspeech works pretty good but i need to go really close and speak for a sentence and it to be detected accurately .

@varungujjar,

Try adding the line max_dB 20.0 in the /etc/asound.conf file like below.
image

Let me know if that helps. Increasing the mic gain can sometimes lead to more noise which could make it worse but the recording is louder!

Best,
Samreen

1 Like

@Samreen hey, i just tried it makes no difference…:frowning: even after a reboot…

The gain works for me when I test with arecord.

arecord recording.wav -f S16_LE -r 16000 -d 5

Here’s a recording where I set the gain to 75.0 as an extreme example. Generally, the gain should be set significantly lower for higher quality audio.

PS it’s loud with a lot of noise
recording.zip (8.5 KB)

@Samreen tested with pyaudio as well.

Let us know if the gain setting works for you when recording with arecord and pyaudio!

@Carlos thkx for getting back… it still doesnt work for me…:frowning: however i’ll check bck again tommorow morning…