How to identify the Linux audio input device index to use for recording? Sound Settings shows as the input device.
However, the code (bottom) shows four sof-hda-dsp. When using 0, it does not record. When using 4, it does record. Why 0 does not work and how to identify which index to use?
----------------------record device list---------------------
Input Device id 0 - sof-hda-dsp: - (hw:0,0)
Input Device id 4 - sof-hda-dsp: - (hw:0,6)
Input Device id 5 - sof-hda-dsp: - (hw:0,7)
Input Device id 6 - sysdefault
Input Device id 7 - samplerate
Input Device id 8 - speexrate
Input Device id 9 - jack
Input Device id 10 - pipewire
Input Device id 11 - pulse
Input Device id 12 - upmix
Input Device id 13 - vdownmix
Input Device id 14 - default
lshw
does not give a clue about audio input device index.
...
*-multimedia
description: Multimedia audio controller
product: Intel Corporation
vendor: Intel Corporation
physical id: 1f.3
bus info: pci@0000:00:1f.3
logical name: card0
logical name: /dev/snd/controlC0
logical name: /dev/snd/hwC0D0
logical name: /dev/snd/hwC0D2
logical name: /dev/snd/pcmC0D0c
logical name: /dev/snd/pcmC0D0p
logical name: /dev/snd/pcmC0D1c
logical name: /dev/snd/pcmC0D1p
logical name: /dev/snd/pcmC0D3p
logical name: /dev/snd/pcmC0D4p
logical name: /dev/snd/pcmC0D5p
logical name: /dev/snd/pcmC0D6c
logical name: /dev/snd/pcmC0D7c
version: 01
width: 64 bits
clock: 33MHz
capabilities: pm msi bus_master cap_list
configuration: driver=sof-audio-pci-intel-tgl latency=64
resources: iomemory:620-61f iomemory:620-61f irq:220 memory:62892a0000-62892a3fff memory:6289000000-62890fffff
...
*-input:10
product: sof-hda-dsp Headphone Mic
physical id: e
logical name: input24
logical name: /dev/input/event18
*-input:11
product: sof-hda-dsp HDMI/DP,pcm=3
physical id: f
logical name: input25
logical name: /dev/input/event19
*-input:12
product: sof-hda-dsp HDMI/DP,pcm=4
physical id: 10
logical name: input26
logical name: /dev/input/event20
*-input:13
product: sof-hda-dsp HDMI/DP,pcm=5
physical id: 11
logical name: input27
logical name: /dev/input/event21
speech_recognition shows the similar but I am not sure how to identify which is the input device index to use.
import speech_recognition as sr
for index, name in enumerate(sr.Microphone.list_microphone_names()):
print("Microphone with name \"{1}\" found for microphone(device_index{0})".format(index, name))
-----
0 sof-hda-dsp: - (hw:0,0), ALSA (2 in, 0 out)
1 sof-hda-dsp: - (hw:0,3), ALSA (0 in, 2 out)
2 sof-hda-dsp: - (hw:0,4), ALSA (0 in, 2 out)
3 sof-hda-dsp: - (hw:0,5), ALSA (0 in, 2 out)
4 sof-hda-dsp: - (hw:0,6), ALSA (2 in, 0 out)
5 sof-hda-dsp: - (hw:0,7), ALSA (2 in, 0 out)
6 sysdefault, ALSA (128 in, 0 out)
7 samplerate, ALSA (128 in, 0 out)
8 speexrate, ALSA (128 in, 0 out)
9 jack, ALSA (2 in, 2 out)
10 pipewire, ALSA (64 in, 64 out)
11 pulse, ALSA (32 in, 32 out)
12 upmix, ALSA (8 in, 0 out)
13 vdownmix, ALSA (6 in, 0 out)
* 14 default, ALSA (32 in, 32 out)
15 SOUNDPEATS TrueAir2, JACK Audio Connection Kit (2 in, 1 out)
16 sof-hda-dsp Headset Mono Microphone + Headphones Stereo Microphone, JACK Audio Connection Kit (0 in, 0 out)
17 sof-hda-dsp Digital Microphone, JACK Audio Connection Kit (2 in, 0 out)
18 sof-hda-dsp Speaker + Headphones, JACK Audio Connection Kit (0 in, 0 out)
19 sof-hda-dsp HDMI / DisplayPort 1 Output, JACK Audio Connection Kit (2 in, 2 out)
20 sof-hda-dsp HDMI / DisplayPort 2 Output, JACK Audio Connection Kit (2 in, 2 out)
21 sof-hda-dsp HDMI / DisplayPort 3 Output, JACK Audio Connection Kit (2 in, 2 out)
22 GNOME Settings, JACK Audio Connection Kit (6 in, 6 out)
Code
import pyaudio
import wave
from IPython.display import (
Audio,
display
)
import librosa
FORMAT = pyaudio.paInt16
CHANNELS = 1
# RATE = 44100
RATE = 48000
CHUNK = 512
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "recordedFile.wav"
device_index = 2
audio = pyaudio.PyAudio()
print("----------------------record device list---------------------")
info = audio.get_host_api_info_by_index(0)
numdevices = info.get('deviceCount')
for i in range(0, numdevices):
if (audio.get_device_info_by_host_api_device_index(0, i).get('maxInputChannels')) > 0:
print("Input Device id ", i, " - ", audio.get_device_info_by_host_api_device_index(0, i).get('name'))
print("-------------------------------------------------------------")
index = int(input())
print("recording via index "+str(index))
stream = audio.open(
format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
input_device_index = index,
frames_per_buffer=CHUNK
)
print ("recording started")
Recordframes = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
Recordframes.append(data)
print ("recording stopped")
stream.stop_stream()
stream.close()
audio.terminate()
waveFile = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
waveFile.setnchannels(CHANNELS)
waveFile.setsampwidth(audio.get_sample_size(FORMAT))
waveFile.setframerate(RATE)
waveFile.writeframes(b''.join(Recordframes))
waveFile.close()
Environment
VERSION="22.04 LTS"
ID_LIKE="ubuntu debian"
$ arecord -l
**** List of CAPTURE Hardware Devices ****
card 0: sofhdadsp [sof-hda-dsp], device 0: HDA Analog (*) []
Subdevices: 1/1
Subdevice #0: subdevice #0
card 0: sofhdadsp [sof-hda-dsp], device 1: HDA Digital (*) []
Subdevices: 1/1
Subdevice #0: subdevice #0
card 0: sofhdadsp [sof-hda-dsp], device 6: DMIC (*) []
Subdevices: 0/1
Subdevice #0: subdevice #0
card 0: sofhdadsp [sof-hda-dsp], device 7: DMIC16kHz (*) []
Subdevices: 1/1
Subdevice #0: subdevice #0