Configure the system's default sound card device (default audio output device, input device, system volume) using alsamixer under Linux

1, System environment

Operating system: Ubuntu 18.04 64 bit

2, See which sound cards (microphones and speakers) are available in the system

When there are multiple microphones or speakers in the system, it is necessary to configure the default device.

2.1 run the alsamixer command to open the graphical interface

wbyq@wbyq:~$ alsamixer

After opening the graphical interface, press F6 to view the default sound card of the system. A USB camera with audio input is inserted into my computer. You can see two sound cards in the figure below. Exit the graphical interface and press Esc (the upper left corner of the keyboard).

There is no alsamixer command, just install one: sudo apt get install alsa utils

wbyq@wbyq:~$ sudo apt-get install alsa-utils
[sudo] wbyq Password for: 
Reading package list... complete
 Analyzing dependency tree for package       
Reading status information... complete       
alsa-utils It's already the latest version (1.1.3-1ubuntu1). 
alsa-utils Set to manual installation.
The following packages are installed automatically and are no longer required:
  libavdevice57 libavfilter6 libavresample3 libdc1394-22 libflite1 libmysofa0 libnorm1 libpgm-5.2-0 librubberband2
  libsdl2-2.0-0 libwayland-egl1-mesa libzmq5
 use'sudo apt autoremove'To uninstall it(they). 
0 packages have been upgraded and 0 packages have been newly installed. To uninstall 0 packages, 88 packages have not been upgraded.

2.2 view / proc/asound directory view sound card

wbyq@wbyq:~$ cd /proc/asound
wbyq@wbyq:/proc/asound$ ls
AudioPCI  card0  card1  cards  devices  hwdep  modules  oss  pcm  seq  timers  U0x46d0x825  version

2.3 view / proc/asound/cards file view sound card

wbyq@wbyq:/proc/asound$ cat /proc/asound/cards
 0 [AudioPCI       ]: ENS1371 - Ensoniq AudioPCI
                      Ensoniq AudioPCI ENS1371 at 0x2040, irq 16
 1 [U0x46d0x825    ]: USB-Audio - USB Device 0x46d:0x825
                      USB Device 0x46d:0x825 at usb-0000:03:00.0-2, high speed

2.4 view the microphone / audio input device of the system through arecord -l command (the speaker will not appear)

wbyq@wbyq:/proc/asound$ arecord -l
**** CAPTURE Hardware device list ****
card 0: AudioPCI [Ensoniq AudioPCI], device 0: ES1371/1 [ES1371 DAC2/ADC]
  Sub equipment: 1/1
  Sub equipment #0: subdevice #0
card 1: U0x46d0x825 [USB Device 0x46d:0x825], device 0: USB Audio [USB Audio]
  Sub equipment: 1/1
  Sub equipment #0: subdevice #0

2.5 view the speaker audio output device of the system through aplay -l command

wbyq@wbyq:/proc/asound$ aplay -l
**** PLAYBACK Hardware device list ****
card 0: AudioPCI [Ensoniq AudioPCI], device 0: ES1371/1 [ES1371 DAC2/ADC]
  Sub equipment: 1/1
  Sub equipment #0: subdevice #0
card 0: AudioPCI [Ensoniq AudioPCI], device 1: ES1371/2 [ES1371 DAC1]
  Sub equipment: 1/1
  Sub equipment #0: subdevice #0

From the printed list, you can see that there are no USB devices in it, because the USB camera I inserted has only audio input function and no audio output function.

3, Set the system default volume

3.1. Manual setup using alsamixer graphical interface

After opening the graphical interface, press the direction key on the keyboard to set the volume.

3.2 automatic setting of input command

wbyq@wbyq:~$ amixer set Master 100%

The latter 100% is the percentage of audio output, and 100% is the maximum sound. It can be adjusted between 0 and 100.

4, Test whether the sound card equipment is normal (recording and playback)

4.1 use the system default sound card for recording and playback

wbyq@wbyq:~$ arecord -d 10 test.wav
 Recording WAVE 'test.wav' : Unsigned 8 bit, Frequency 8000 Hz, Mono
wbyq@wbyq:~$ aplay test.wav
 Playing WAVE 'test.wav' : Unsigned 8 bit, Frequency 8000 Hz, Mono

The first command is the recording command, - d 10 means recording 10 seconds of sound, test Wav is the name of the saved file.

The second command is to play a sound file.

4.2 record and play with the specified sound card

wbyq@wbyq:~$ arecord -D hw:0 -d 3 test.wav
 Recording WAVE 'test.wav' : Unsigned 8 bit, Frequency 8000 Hz, Mono
wbyq@wbyq:~$ aplay -D hw:0 test.wav 
Playing WAVE 'test.wav' : Unsigned 8 bit, Frequency 8000 Hz, Mono

Where - D hw:x indicates the specified sound card. Use the aplay -l and arecord -l commands to view the recording device list and speaker device list of the system.

In order to test the current situation, I inserted a USB sound card device into my computer.

This is a USB drive free sound card. After inserting into the computer, check the sound card device again, and you can see that there is an additional recording device and a speaker device.

wbyq@wbyq:~$ arecord -l
**** CAPTURE Hardware device list ****
card 0: AudioPCI [Ensoniq AudioPCI], device 0: ES1371/1 [ES1371 DAC2/ADC]
  Sub equipment: 1/1
  Sub equipment #0: subdevice #0
card 1: Device [USB PnP Sound Device], device 0: USB Audio [USB Audio]
  Sub equipment: 1/1
  Sub equipment #0: subdevice #0
wbyq@wbyq:~$ aplay -l
**** PLAYBACK Hardware device list ****
card 0: AudioPCI [Ensoniq AudioPCI], device 0: ES1371/1 [ES1371 DAC2/ADC]
  Sub equipment: 1/1
  Sub equipment #0: subdevice #0
card 0: AudioPCI [Ensoniq AudioPCI], device 1: ES1371/2 [ES1371 DAC1]
  Sub equipment: 1/1
  Sub equipment #0: subdevice #0
card 1: Device [USB PnP Sound Device], device 0: USB Audio [USB Audio]
  Sub equipment: 0/1
  Sub equipment #0: subdevice #0

Use the inserted USB sound card for recording and playback:

wbyq@wbyq:~$ arecord -D hw:1 -f S16_LE -r 44100 -d 3 test.wav
 Recording WAVE 'test.wav' : Signed 16 bit Little Endian, Frequency 44100 Hz, Mono
wbyq@wbyq:~$ aplay -D hw:1 test.wav
 Playing WAVE 'test.wav' : Signed 16 bit Little Endian, Frequency 44100 Hz, Mono

Where - r specifies the sampling rate and - f specifies the number of bits per sampling point -- sample size.

If you are playing a PCM file, you can set it as follows:

aplay -t raw -c 1 -f S16_LE -r 8000 test.pcm 

-t: type raw Means yes PCM 
-c: channel 1 
-f S16_LE: Signed 16bit-width Little-Endian 
-r: sample rate 8000 

4.3 specifying the default sound card problem solving

Note: if you specify the sound card directly with - D hw:X, in some cases, it will be unable to play due to sampling rate and various problems. It is recommended to specify the sound card device in the following way.

To view the sound card details supported by the system:

wbyq@wbyq:~$ aplay -L
null
    Discard all samples (playback) or generate zero samples (capture)
pulse
    PulseAudio Sound Server
default
    Playback/recording through the PulseAudio sound server
sysdefault:CARD=AudioPCI
    Ensoniq AudioPCI, ES1371 DAC2/ADC
    Default Audio Device
front:CARD=AudioPCI,DEV=0
    Ensoniq AudioPCI, ES1371 DAC2/ADC
    Front speakers
rear:CARD=AudioPCI,DEV=0
    Ensoniq AudioPCI, ES1371 DAC1
    Rear speakers
surround40:CARD=AudioPCI,DEV=0
    Ensoniq AudioPCI, ES1371 DAC2/ADC
    4.0 Surround output to Front and Rear speakers
iec958:CARD=AudioPCI,DEV=0
    Ensoniq AudioPCI, ES1371 DAC2/ADC
    IEC958 (S/PDIF) Digital Audio Output
dmix:CARD=AudioPCI,DEV=0
    Ensoniq AudioPCI, ES1371 DAC2/ADC
    Direct sample mixing device
dmix:CARD=AudioPCI,DEV=1
    Ensoniq AudioPCI, ES1371 DAC1
    Direct sample mixing device
dsnoop:CARD=AudioPCI,DEV=0
    Ensoniq AudioPCI, ES1371 DAC2/ADC
    Direct sample snooping device
dsnoop:CARD=AudioPCI,DEV=1
    Ensoniq AudioPCI, ES1371 DAC1
    Direct sample snooping device
hw:CARD=AudioPCI,DEV=0
    Ensoniq AudioPCI, ES1371 DAC2/ADC
    Direct hardware device without any conversions
hw:CARD=AudioPCI,DEV=1
    Ensoniq AudioPCI, ES1371 DAC1
    Direct hardware device without any conversions
plughw:CARD=AudioPCI,DEV=0
    Ensoniq AudioPCI, ES1371 DAC2/ADC
    Hardware device with all software conversions
plughw:CARD=AudioPCI,DEV=1
    Ensoniq AudioPCI, ES1371 DAC1
    Hardware device with all software conversions
sysdefault:CARD=Device
    USB PnP Sound Device, USB Audio
    Default Audio Device
front:CARD=Device,DEV=0
    USB PnP Sound Device, USB Audio
    Front speakers
surround21:CARD=Device,DEV=0
    USB PnP Sound Device, USB Audio
    2.1 Surround output to Front and Subwoofer speakers
surround40:CARD=Device,DEV=0
    USB PnP Sound Device, USB Audio
    4.0 Surround output to Front and Rear speakers
surround41:CARD=Device,DEV=0
    USB PnP Sound Device, USB Audio
    4.1 Surround output to Front, Rear and Subwoofer speakers
surround50:CARD=Device,DEV=0
    USB PnP Sound Device, USB Audio
    5.0 Surround output to Front, Center and Rear speakers
surround51:CARD=Device,DEV=0
    USB PnP Sound Device, USB Audio
    5.1 Surround output to Front, Center, Rear and Subwoofer speakers
surround71:CARD=Device,DEV=0
    USB PnP Sound Device, USB Audio
    7.1 Surround output to Front, Center, Side, Rear and Woofer speakers
iec958:CARD=Device,DEV=0
    USB PnP Sound Device, USB Audio
    IEC958 (S/PDIF) Digital Audio Output
dmix:CARD=Device,DEV=0
    USB PnP Sound Device, USB Audio
    Direct sample mixing device
dsnoop:CARD=Device,DEV=0
    USB PnP Sound Device, USB Audio
    Direct sample snooping device
hw:CARD=Device,DEV=0
    USB PnP Sound Device, USB Audio
    Direct hardware device without any conversions
plughw:CARD=Device,DEV=0
    USB PnP Sound Device, USB Audio
    Hardware device with all software conversions
wbyq@wbyq:~$ 

Play audio files using the specified sound card:

wbyq@wbyq:~$ aplay -D plughw:CARD=Device,DEV=0 lu_xian_pian_li.wav

-For the sound card name specified after D, find the one at the beginning of the corresponding sound card plughw: field in the list for use.

5, Set the system default microphone device and speaker device

5.1 create sound card profile

For global configuration: create the file asound.com in the / etc directory conf

For the specified user configuration: create a file in the user directory asoundrc

5.2 configuration code

Write the following configuration code in the above file to take effect, where 1 represents the sound card number used. Previously described how to view the sound card number.

defaults.ctl.card 1
defaults.pcm.card 1
defaults.timer.card 1

The "pcm" option determines the device used to play audio, and the "ctl" option determines which sound card can be used by control tools such as alsamixer.

You can also specify the sound card name directly.

Get the device name of the available speakers in the current system:

wbyq@wbyq:~$ cat /proc/asound/cards
 0 [AudioPCI       ]: ENS1371 - Ensoniq AudioPCI
                      Ensoniq AudioPCI ENS1371 at 0x2040, irq 16
 1 [U0x46d0x825    ]: USB-Audio - USB Device 0x46d:0x825
                      USB Device 0x46d:0x825 at usb-0000:03:00.0-2, high speed
 2 [Device         ]: USB-Audio - USB PnP Sound Device
                      C-Media Electronics Inc. USB PnP Sound Device at usb-0000:03:00.0-3.1, full spe
wbyq@wbyq:~$ 
wbyq@wbyq:~$ aplay -l | awk -F \: '/,/{print $2}' | awk '{print $1}' | uniq
AudioPCI
Device

Get the device name of the microphone available in the current system:

wbyq@wbyq:~$ arecord -l
**** CAPTURE Hardware device list ****
card 0: AudioPCI [Ensoniq AudioPCI], device 0: ES1371/1 [ES1371 DAC2/ADC]
  Sub equipment: 1/1
  Sub equipment #0: subdevice #0
card 1: U0x46d0x825 [USB Device 0x46d:0x825], device 0: USB Audio [USB Audio]
  Sub equipment: 1/1
  Sub equipment #0: subdevice #0
card 2: Device [USB PnP Sound Device], device 0: USB Audio [USB Audio]
  Sub equipment: 1/1
  Sub equipment #0: subdevice #0
wbyq@wbyq:~$ arecord -l | awk -F \: '/,/{print $2}' | awk '{print $1}' | uniq
AudioPCI
U0x46d0x825
Device

Syntax for specifying devices:

(for global configuration: create the file asound.conf in the / etc directory; for specified user configuration: create the file. asoundrc in the user directory)

pcm.!default {
type hw
card Sound card name
}
 
ctl.!default {
type hw
card Sound card name
}

Profile reference link: https://wiki.archlinux.org/index.php/PulseAudio#default.pa

Use to view the detailed configuration list of sound card: get the names of microphone and speaker

wbyq@wbyq:~$ pacmd list-cards
1 card(s) available.
    index: 0
	name: <alsa_card.pci-0000_02_02.0>
	driver: <module-alsa-card.c>
	owner module: 7
	properties:
		alsa.card = "0"
		alsa.card_name = "Ensoniq AudioPCI"
		alsa.long_card_name = "Ensoniq AudioPCI ENS1371 at 0x2040, irq 16"
		alsa.driver_name = "snd_ens1371"
		device.bus_path = "pci-0000:02:02.0"
		sysfs.path = "/devices/pci0000:00/0000:00:11.0/0000:02:02.0/sound/card0"
		device.bus = "pci"
		device.vendor.id = "1274"
		device.vendor.name = "Ensoniq"
		device.product.id = "1371"
		device.product.name = "ES1371/ES1373 / Creative Labs CT2518 (Audio PCI 64V/128/5200 / Creative CT4810/CT5803/CT5806 [Sound Blaster PCI])"
		device.string = "0"
		device.description = "ES1371/ES1373 / Creative Labs CT2518 (Audio PCI 64V/128/5200 / Creative CT4810/CT5803/CT5806 [Sound Blaster PCI])"
		module-udev-detect.discovered = "1"
		device.icon_name = "audio-card-pci"
	profiles:
		input:analog-mono: Analog mono input (priority 2, available: unknown)
		input:analog-stereo: Analog stereo input (priority 60, available: unknown)
		output:analog-mono: Analog mono output (priority 200, available: unknown)
		output:analog-mono+input:analog-mono: Analog mono duplex (priority 202, available: unknown)
		output:analog-mono+input:analog-stereo: Analog mono output + Analog stereo input (priority 260, available: unknown)
		output:analog-stereo: Analog stereo output (priority 6000, available: unknown)
		output:analog-stereo+input:analog-mono: Analog stereo output + Analog mono input (priority 6002, available: unknown)
		output:analog-stereo+input:analog-stereo: Analog stereo duplex (priority 6060, available: unknown)
		off: shut (priority 0, available: unknown)
	active profile: <output:analog-stereo+input:analog-stereo>
	sinks:
		alsa_output.pci-0000_02_02.0.analog-stereo/#0: ES1371/ES1373 / Creative Labs CT2518 (Audio PCI 64V/128/5200 / Creative CT4810/CT5803/CT5806 [Sound Blaster PCI]) analog stereo
	sources:
		alsa_output.pci-0000_02_02.0.analog-stereo.monitor/#0: Monitor of ES1371/ES1373 / Creative Labs CT2518 (Audio PCI 64V/128/5200 / Creative CT4810/CT5803/CT5806 [Sound Blaster PCI]) analog stereo
		alsa_input.pci-0000_02_02.0.analog-stereo/#1: ES1371/ES1373 / Creative Labs CT2518 (Audio PCI 64V/128/5200 / Creative CT4810/CT5803/CT5806 [Sound Blaster PCI]) analog stereo
	ports:
		analog-input-mic: Microphone (priority 8700, latency offset 0 usec, available: unknown)
			properties:
				device.icon_name = "audio-input-microphone"
		analog-input-linein: Input jack (priority 8100, latency offset 0 usec, available: unknown)
			properties:
				
		analog-input-aux: Analog input (priority 8000, latency offset 0 usec, available: unknown)
			properties:
				
		analog-input-video: video (priority 7000, latency offset 0 usec, available: unknown)
			properties:
				
		analog-output: Analog output (priority 9900, latency offset 0 usec, available: unknown)
			properties:
				
wbyq@wbyq:~$
wbyq@wbyq:~$ gedit .asoundrc 

ctl.!default {
     type pulse
     sink "alsa_output.pci-0000_02_02.0.analog-stereo"
     source "alsa_input.pci-0000_02_02.0.analog-stereo"
 }

The name of the sound card can also be obtained through the code of QT:

 foreach (const QAudioDeviceInfo &deviceInfo, QAudioDeviceInfo::availableDevices(QAudio::AudioOutput))
          qDebug() << "Speakers currently available on the system: " << deviceInfo.deviceName();

    foreach (const QAudioDeviceInfo &deviceInfo, QAudioDeviceInfo::availableDevices(QAudio::AudioInput))
          qDebug() << "Microphones currently available in the system: " << deviceInfo.deviceName();



Speakers currently available on the system:  "default"
Speakers currently available on the system:  "pulse"
Speakers currently available on the system:  "sysdefault:CARD=AudioPCI"
Speakers currently available on the system:  "front:CARD=AudioPCI,DEV=0"
Speakers currently available on the system:  "rear:CARD=AudioPCI,DEV=0"
Speakers currently available on the system:  "surround40:CARD=AudioPCI,DEV=0"
Speakers currently available on the system:  "iec958:CARD=AudioPCI,DEV=0"
Speakers currently available on the system:  "dmix:CARD=AudioPCI,DEV=0"
Speakers currently available on the system:  "dmix:CARD=AudioPCI,DEV=1"
Speakers currently available on the system:  "dsnoop:CARD=AudioPCI,DEV=0"
Speakers currently available on the system:  "dsnoop:CARD=AudioPCI,DEV=1"
Speakers currently available on the system:  "hw:CARD=AudioPCI,DEV=0"
Speakers currently available on the system:  "hw:CARD=AudioPCI,DEV=1"
Speakers currently available on the system:  "plughw:CARD=AudioPCI,DEV=0"
Speakers currently available on the system:  "plughw:CARD=AudioPCI,DEV=1"
Speakers currently available on the system:  "alsa_output.pci-0000_02_02.0.analog-stereo"
Microphones currently available in the system:  "default"
Microphones currently available in the system:  "pulse"
Microphones currently available in the system:  "sysdefault:CARD=AudioPCI"
Microphones currently available in the system:  "front:CARD=AudioPCI,DEV=0"
Microphones currently available in the system:  "iec958:CARD=AudioPCI,DEV=0"
Microphones currently available in the system:  "dmix:CARD=AudioPCI,DEV=0"
Microphones currently available in the system:  "dsnoop:CARD=AudioPCI,DEV=0"
Microphones currently available in the system:  "hw:CARD=AudioPCI,DEV=0"
Microphones currently available in the system:  "plughw:CARD=AudioPCI,DEV=0"
Microphones currently available in the system:  "alsa_input.pci-0000_02_02.0.analog-stereo"
Microphones currently available in the system:  "alsa_output.pci-0000_02_02.0.analog-stereo.monitor"

Added by Kiwii on Wed, 12 Jan 2022 05:46:32 +0200