Accessing I2S microphone in python

I am trying to access the Adafruit I2S MEMS microphone (https://www.adafruit.com/product/3421) via PuleAudio through the PyAudio library in Python 3.8.5. I have done the following steps:

  1. Connected the I2S to the J21 expansion header and set it up with the Jetson IO script; output of “sudo grep dap1 /sys/kernel/debug/tegra_pinctrl_reg” is:

Bank: 0 Reg: 0x02431028 Val: 0x00000444 -> dap1_fs_pj3
Bank: 0 Reg: 0x02431030 Val: 0x00000454 -> dap1_din_pj2
Bank: 0 Reg: 0x02431038 Val: 0x00000055 -> dap1_dout_pj1
Bank: 0 Reg: 0x02431040 Val: 0x00000444 -> dap1_sclk_pj0

  1. I checked that audio is being recorded with the following commands and opened the file in numpy and values are being captured:

amixer -c tegrasndt186ref sset “ADMAIF1 Mux” “I2S1”
arecord -D hw:tegrasndt186ref,0 -r 48000 -f S32_LE -c 2 -d 10 cap.wav

  1. Now, I am trying to record audio in Python via PyAudio (which wraps PulseAudio as per my limited knowledge) with the following script:

import pyaudio
p = pyaudio.PyAudio()
stream = p.open(format = pyaudio.PaInt16, channels = 1, rate = 44100, input = True, frames_per_buffer = 1024)
frames =
try:
for i in range(0, int(44100 / 1024)):
frames.append(np.frombuffer(stream.read(1024), dtype = np.int16))
finally:
stream.stop_stream()
stream.close()
p.terminate()
audio = np.concatenate(frames)

Step 3, results in a numpy array of the correct size (# of samples X duration) but filled with all zeros. The following is the output printed to the screen when running the code in step 3:

ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition ‘cards.tegra-hda.pcm.front.0:CARD=0’
ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5007:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM front
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround21
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround21
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround40
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround41
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround50
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround51
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround71
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition ‘cards.tegra-hda.pcm.hdmi.0:CARD=0,AES0=4,AES1=130,AES2=0,AES3=2’
ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5007:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM hdmi
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition ‘cards.tegra-hda.pcm.hdmi.0:CARD=0,AES0=4,AES1=130,AES2=0,AES3=2’
ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5007:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM hdmi
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm_dmix.c:990:(snd_pcm_dmix_open) The dmix plugin supports only playback stream
ALSA lib pcm_hw.c:1713:(_snd_pcm_hw_open) Invalid value for card
ALSA lib pcm_hw.c:1713:(_snd_pcm_hw_open) Invalid value for card
ALSA lib pcm_hw.c:1713:(_snd_pcm_hw_open) Invalid value for card
ALSA lib pcm_hw.c:1713:(_snd_pcm_hw_open) Invalid value for card
ALSA lib pcm_dmix.c:1052:(snd_pcm_dmix_open) unable to open slave

I believe the above output means that the default audio card is not correct, however in checking the default card using this script: https://gist.github.com/mansam/9332445 I get:

ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition ‘cards.tegra-hda.pcm.front.0:CARD=0’
ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5007:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM front
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround21
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround21
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround40
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround41
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround50
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround51
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround71
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition ‘cards.tegra-hda.pcm.hdmi.0:CARD=0,AES0=4,AES1=130,AES2=0,AES3=2’
ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5007:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM hdmi
ALSA lib confmisc.c:1281:(snd_func_refer) Unable to find definition ‘cards.tegra-hda.pcm.hdmi.0:CARD=0,AES0=4,AES1=130,AES2=0,AES3=2’
ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
ALSA lib conf.c:5007:(snd_config_expand) Evaluate error: No such file or directory
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM hdmi
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline
ALSA lib pcm_dmix.c:990:(snd_pcm_dmix_open) The dmix plugin supports only playback stream
ALSA lib pcm_hw.c:1713:(_snd_pcm_hw_open) Invalid value for card
ALSA lib pcm_hw.c:1713:(_snd_pcm_hw_open) Invalid value for card
ALSA lib pcm_hw.c:1713:(_snd_pcm_hw_open) Invalid value for card
ALSA lib pcm_hw.c:1713:(_snd_pcm_hw_open) Invalid value for card
ALSA lib pcm_dmix.c:1052:(snd_pcm_dmix_open) unable to open slave
Output Device id 0 - tegra-hda: HDMI 0 (hw:0,3)
Output Device id 1 - tegra-hda: HDMI 0 (hw:0,7)
Input Device id 2 - tegra-snd-t186ref-mobile-rt565x: - (hw:1,0)
Output Device id 2 - tegra-snd-t186ref-mobile-rt565x: - (hw:1,0)
Input Device id 3 - tegra-snd-t186ref-mobile-rt565x: - (hw:1,1)
Output Device id 3 - tegra-snd-t186ref-mobile-rt565x: - (hw:1,1)
Input Device id 4 - tegra-snd-t186ref-mobile-rt565x: - (hw:1,2)
Output Device id 4 - tegra-snd-t186ref-mobile-rt565x: - (hw:1,2)
Input Device id 5 - tegra-snd-t186ref-mobile-rt565x: - (hw:1,3)
Output Device id 5 - tegra-snd-t186ref-mobile-rt565x: - (hw:1,3)
Input Device id 6 - tegra-snd-t186ref-mobile-rt565x: - (hw:1,4)
Output Device id 6 - tegra-snd-t186ref-mobile-rt565x: - (hw:1,4)
Input Device id 7 - tegra-snd-t186ref-mobile-rt565x: - (hw:1,5)
Output Device id 7 - tegra-snd-t186ref-mobile-rt565x: - (hw:1,5)
Input Device id 8 - tegra-snd-t186ref-mobile-rt565x: - (hw:1,6)
Output Device id 8 - tegra-snd-t186ref-mobile-rt565x: - (hw:1,6)
Input Device id 9 - tegra-snd-t186ref-mobile-rt565x: - (hw:1,7)
Output Device id 9 - tegra-snd-t186ref-mobile-rt565x: - (hw:1,7)
Input Device id 10 - tegra-snd-t186ref-mobile-rt565x: - (hw:1,8)
Output Device id 10 - tegra-snd-t186ref-mobile-rt565x: - (hw:1,8)
Input Device id 11 - tegra-snd-t186ref-mobile-rt565x: - (hw:1,9)
Output Device id 11 - tegra-snd-t186ref-mobile-rt565x: - (hw:1,9)
Input Device id 12 - tegra-snd-t186ref-mobile-rt565x: - (hw:1,10)
Output Device id 12 - tegra-snd-t186ref-mobile-rt565x: - (hw:1,10)
Input Device id 13 - tegra-snd-t186ref-mobile-rt565x: - (hw:1,11)
Output Device id 13 - tegra-snd-t186ref-mobile-rt565x: - (hw:1,11)
Input Device id 14 - tegra-snd-t186ref-mobile-rt565x: - (hw:1,12)
Output Device id 14 - tegra-snd-t186ref-mobile-rt565x: - (hw:1,12)
Input Device id 15 - tegra-snd-t186ref-mobile-rt565x: - (hw:1,13)
Output Device id 15 - tegra-snd-t186ref-mobile-rt565x: - (hw:1,13)
Input Device id 16 - tegra-snd-t186ref-mobile-rt565x: - (hw:1,14)
Output Device id 16 - tegra-snd-t186ref-mobile-rt565x: - (hw:1,14)
Input Device id 17 - tegra-snd-t186ref-mobile-rt565x: - (hw:1,15)
Output Device id 17 - tegra-snd-t186ref-mobile-rt565x: - (hw:1,15)
Input Device id 18 - tegra-snd-t186ref-mobile-rt565x: - (hw:1,16)
Output Device id 18 - tegra-snd-t186ref-mobile-rt565x: - (hw:1,16)
Input Device id 19 - tegra-snd-t186ref-mobile-rt565x: - (hw:1,17)
Output Device id 19 - tegra-snd-t186ref-mobile-rt565x: - (hw:1,17)
Input Device id 20 - tegra-snd-t186ref-mobile-rt565x: - (hw:1,18)
Output Device id 20 - tegra-snd-t186ref-mobile-rt565x: - (hw:1,18)
Input Device id 21 - tegra-snd-t186ref-mobile-rt565x: - (hw:1,19)
Output Device id 21 - tegra-snd-t186ref-mobile-rt565x: - (hw:1,19)
Input Device id 22 - pulse
Output Device id 22 - pulse
Input Device id 23 - music
Output Device id 23 - music
Output Device id 24 - demixer
Input Device id 25 - voice
Output Device id 25 - voice
Input Device id 26 - music_and_voice
Output Device id 26 - music_and_voice
Input Device id 27 - default
Output Device id 27 - default
Selected device is tegra-hda: HDMI 0 (hw:0,7)

ValueError Traceback (most recent call last)
in
14 devinfo = p.get_device_info_by_index(1)
15 print("Selected device is ",devinfo.get(‘name’))
—> 16 if p.is_format_supported(44100.0, # Sample rate
17 input_device=devinfo[“index”],
18 input_channels=devinfo[‘maxInputChannels’],

~/.pyenv/versions/3.8-dev/envs/pindrop/lib/python3.8/site-packages/pyaudio.py in is_format_supported(self, rate, input_device, input_channels, input_format, output_device, output_channels, output_format)
935 kwargs[‘output_format’] = output_format
936
–> 937 return pa.is_format_supported(rate, **kwargs)
938
939 def get_default_input_device_info(self):

ValueError: (‘Invalid number of channels’, -9998)

I tried device 2, which resulted in the same correct sized array with all 0’s.

So, I seem to missing something probably in configuration but I can’t seem to figure out what. Any help or even direction is much appreciated!

Hello!

I can’t say I have used, pyAudio, however, the input device you want is …

Input Device id 2 - tegra-snd-t186ref-mobile-rt565x: - (hw:1,0)

By “device 2”, was this the other input device that you tried?

Note that you could also select the default audio capture device under the Ubuntu Sound settings menu and change it from the HDA (HDMI) sound card to the analog soundcard.

Regards
Jon

Thanks for the response Jon.

I tried device 2 and got an array filled with 0’s. Also, it looks like I do not have the options to change the default output settings (see screenshot).

1 Like

Hello!

Sorry for the delay. I just went back and looked at the code from steps 2 and 3 above, and I see that in step 2 you are using 2 channel @ 32-bits (which is correct), but then in the pyAudio code you are using 1 channel @ 16-bits. Can you try using 2 channels @ 32-bits with pyAudio as well?

Regards,
Jon

There is no update from you for a period, assuming this is not an issue any more.
Hence we are closing this topic. If need further support, please open a new one.
Thanks

Hi nabusman,

Have you tried with our suggestion?
Any result can be shared? Or it’s not an issue any more?