Ubuntu设设置默认外放和麦克风设备

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

最近在做一个项目,需要使用外放播放声音和麦克风拾取录音,然后还要用到摄像头。采购了一个麦克风和一个USB摄像头。结果部署过程中发现摄像头自带的麦克风被识别为默认麦克风,而且每次开机几乎都是这样,然后专业麦克风必须在声音页面选择才能指定为默认麦克风。

最终实现的需求是交付给客户后可以直接使用,肯定不可能让客户每次开机手动选择一下。

没错,我们的项目就是每天定时拉闸关机,早上拉闸自启动!


一、pulseaudio 是什么?

PulseAudio is a networked low-latency sound server for Linux, POSIX and Windows systems.

Ubuntu桌面版系统使用的就是pulseaudio ,我们的命令也和这个有关系。

二、配置外放

1.查看所有的外放设备

pacmd list-sinks

1 sink(s) available.
  * index: 1
	name: <alsa_output.pci-0000_00_1b.0.analog-stereo>
	driver: <module-alsa-card.c>
	flags: HARDWARE HW_MUTE_CTRL HW_VOLUME_CTRL DECIBEL_VOLUME LATENCY DYNAMIC_LATENCY
	state: SUSPENDED
	suspend cause: IDLE
	priority: 9039
	volume: front-left: 32768 /  50% / -18.06 dB,   front-right: 32768 /  50% / -18.06 dB
	        balance 0.00
	base volume: 65536 / 100% / 0.00 dB
	volume steps: 65537
	muted: no
	current latency: 0.00 ms
	max request: 0 KiB
	max rewind: 0 KiB
	monitor source: 1
	sample spec: s16le 2ch 44100Hz
	channel map: front-left,front-right
	             Stereo
	used by: 0
	linked by: 0
	configured latency: 0.00 ms; range is 0.50 .. 2000.00 ms
	card: 1 <alsa_card.pci-0000_00_1b.0>
	module: 23
	properties:
		alsa.resolution_bits = "16"
		device.api = "alsa"
		device.class = "sound"
		alsa.class = "generic"
		alsa.subclass = "generic-mix"
		alsa.name = "CX20751/2 Analog"
		alsa.id = "CX20751/2 Analog"
		alsa.subdevice = "0"
		alsa.subdevice_name = "subdevice #0"
		alsa.device = "0"
		alsa.card = "1"
		alsa.card_name = "HDA Intel PCH"
		alsa.long_card_name = "HDA Intel PCH at 0xc4314000 irq 50"
		alsa.driver_name = "snd_hda_intel"
		device.bus_path = "pci-0000:00:1b.0"
		sysfs.path = "/devices/pci0000:00/0000:00:1b.0/sound/card1"
		device.bus = "pci"
		device.vendor.id = "8086"
		device.vendor.name = "Intel Corporation"
		device.product.id = "9ca0"
		device.product.name = "Wildcat Point-LP High Definition Audio Controller"
		device.form_factor = "internal"
		device.string = "front:1"
		device.buffering.buffer_size = "352800"
		device.buffering.fragment_size = "176400"
		device.access_mode = "mmap+timer"
		device.profile.name = "analog-stereo"
		device.profile.description = "Analog Stereo"
		device.description = "Built-in Audio Analog Stereo"
		module-udev-detect.discovered = "1"
		device.icon_name = "audio-card-pci"
	ports:
		analog-output-speaker: Speakers (priority 10000, latency offset 0 usec, available: unknown)
			properties:
				device.icon_name = "audio-speakers"
		analog-output-headphones: Headphones (priority 9900, latency offset 0 usec, available: no)
			properties:
				device.icon_name = "audio-headphones"
	active port: <analog-output-speaker>

我使用的是笔记本内置外放,也没插额外的外放,所以列表里只有一个。

注意index前面带星号的设备,那就是你当前的选定设备。

2.设定默认的外放设备

设定方法有两个,一是根据index来设定,二是根据name来设定。这里推荐使用name,因为index会变,不是说随时会变,而是每次开机都可能不一样,所以这个方法确认不是好方法。但,也不是说完全不行,只是需要经过稍微复杂点的处理,比如根据awk等工具预处理,找到你想要的那个index再使用,麻烦肯定是麻烦点。

而使用name设定就没有这个问题,前提是你没有改动过驱动等配置。

#必须使用当前登录桌面的用户
pactl set-default-sink <name>
#示例
pactl set-default-sink alsa_output.pci-0000_00_1b.0.analog-stereo

成功shell返回0,失败查看具体提示

3.设定外放设备的声音强度

先看下接受的数据格式:
VOLUME can be specified as an integer (e.g. 2000, 16384), a linear factor
(e.g. 0.4, 1.100), a percentage (e.g. 10%, 100%) or a decibel value (e.g. 0dB,
20dB). If the volume specification start with a + or - the volume adjustment will
be relative to the current sink volume. A single volume value affects all channels;
if multiple volume values are given their number has to match the sink’s number of
channels.

简单翻一下:支持整数,小数,百分比,分贝。这里我选择百分比,没别的原因,因为简单。其他的方式不要滥用,最好带上耳塞,免得震聋耳朵!

#必须使用当前登录桌面的用户
pactl set-sink-volume <外放name> <百分比>
#示例
pactl set-sink-volume alsa_output.pci-0000_00_1b.0.analog-stereo 50%

执行成功shell返回0,失败查看具体提示。

4.设定外放设备静音

理论上声音设置为0%也可以的,但是那不是真正的静音,至少0%音量或许只是声音特别小。

#必须使用当前登录桌面的用户
pactl set-sink-mute <name> 1
#示例
pactl set-sink-mute alsa_output.pci-0000_00_1b.0.analog-stereo 1

在这里插入图片描述
仔细看喇叭图标上多了个禁止斜杠。

下面是声音0%,不静音
在这里插入图片描述
这下看出差别了吧!

三、配置麦克风

1.查看所有的麦克风设备

#必须使用当前登录桌面的用户
pacmd list-sources

2 source(s) available.
    index: 1
	name: <alsa_output.pci-0000_00_1b.0.analog-stereo.monitor>
	driver: <module-alsa-card.c>
	flags: DECIBEL_VOLUME LATENCY DYNAMIC_LATENCY
	state: IDLE
	suspend cause: (none)
	priority: 1030
	volume: front-left: 65536 / 100% / 0.00 dB,   front-right: 65536 / 100% / 0.00 dB
	        balance 0.00
	base volume: 65536 / 100% / 0.00 dB
	volume steps: 65537
	muted: no
	current latency: 0.00 ms
	max rewind: 344 KiB
	sample spec: s16le 2ch 44100Hz
	channel map: front-left,front-right
	             Stereo
	used by: 0
	linked by: 0
	configured latency: 2000.00 ms; range is 0.50 .. 2000.00 ms
	monitor_of: 1
	card: 1 <alsa_card.pci-0000_00_1b.0>
	module: 23
	properties:
		device.description = "Monitor of Built-in Audio Analog Stereo"
		device.class = "monitor"
		alsa.card = "1"
		alsa.card_name = "HDA Intel PCH"
		alsa.long_card_name = "HDA Intel PCH at 0xc4314000 irq 50"
		alsa.driver_name = "snd_hda_intel"
		device.bus_path = "pci-0000:00:1b.0"
		sysfs.path = "/devices/pci0000:00/0000:00:1b.0/sound/card1"
		device.bus = "pci"
		device.vendor.id = "8086"
		device.vendor.name = "Intel Corporation"
		device.product.id = "9ca0"
		device.product.name = "Wildcat Point-LP High Definition Audio Controller"
		device.form_factor = "internal"
		device.string = "1"
		module-udev-detect.discovered = "1"
		device.icon_name = "audio-card-pci"
  * index: 2
	name: <alsa_input.pci-0000_00_1b.0.analog-stereo>
	driver: <module-alsa-card.c>
	flags: HARDWARE HW_MUTE_CTRL HW_VOLUME_CTRL DECIBEL_VOLUME LATENCY DYNAMIC_LATENCY
	state: SUSPENDED
	suspend cause: IDLE
	priority: 9039
	volume: front-left: 32768 /  50% / -18.06 dB,   front-right: 32768 /  50% / -18.06 dB
	        balance 0.00
	base volume: 13076 /  20% / -42.00 dB
	volume steps: 65537
	muted: no
	current latency: 0.00 ms
	max rewind: 0 KiB
	sample spec: s16le 2ch 44100Hz
	channel map: front-left,front-right
	             Stereo
	used by: 0
	linked by: 0
	configured latency: 0.00 ms; range is 0.50 .. 2000.00 ms
	card: 1 <alsa_card.pci-0000_00_1b.0>
	module: 23
	properties:
		alsa.resolution_bits = "16"
		device.api = "alsa"
		device.class = "sound"
		alsa.class = "generic"
		alsa.subclass = "generic-mix"
		alsa.name = "CX20751/2 Analog"
		alsa.id = "CX20751/2 Analog"
		alsa.subdevice = "0"
		alsa.subdevice_name = "subdevice #0"
		alsa.device = "0"
		alsa.card = "1"
		alsa.card_name = "HDA Intel PCH"
		alsa.long_card_name = "HDA Intel PCH at 0xc4314000 irq 50"
		alsa.driver_name = "snd_hda_intel"
		device.bus_path = "pci-0000:00:1b.0"
		sysfs.path = "/devices/pci0000:00/0000:00:1b.0/sound/card1"
		device.bus = "pci"
		device.vendor.id = "8086"
		device.vendor.name = "Intel Corporation"
		device.product.id = "9ca0"
		device.product.name = "Wildcat Point-LP High Definition Audio Controller"
		device.form_factor = "internal"
		device.string = "front:1"
		device.buffering.buffer_size = "352800"
		device.buffering.fragment_size = "176400"
		device.access_mode = "mmap+timer"
		device.profile.name = "analog-stereo"
		device.profile.description = "Analog Stereo"
		device.description = "Built-in Audio Analog Stereo"
		module-udev-detect.discovered = "1"
		device.icon_name = "audio-card-pci"
	ports:
		analog-input-internal-mic: Internal Microphone (priority 8900, latency offset 0 usec, available: unknown)
			properties:
				device.icon_name = "audio-input-microphone"
		analog-input-mic: Microphone (priority 8700, latency offset 0 usec, available: no)
			properties:
				device.icon_name = "audio-input-microphone"
	active port: <analog-input-internal-mic>

2.设置默认麦克风设备

和外放一样不建议使用index,原因就是重启顺序可能会变,还是使用name。

假如你不止插入一个麦克风设备(比如3.5耳机孔+USB麦克风并存)。你只需要记住你需要设置的麦克风设备的名字即可。

我这里是index前面带星号的设备,就是我目前的麦克风设备。

#必须使用当前登录桌面的用户
pactl set-default-sources <name>
#示例
pactl set-default-sources alsa_input.pci-0000_00_1b.0.analog-stereo

执行成功shell返回0,失败查看具体提示。

3.设置麦克风设备声音强度

基本和外放一样,这里直接贴命令了,不赘述了。

多说一句,尽量不要调太高,会出现破音,基本能够满足需要就行了。

#必须使用当前登录桌面的用户
pactl set-source-volume <name> <百分比>
#示例
pactl set-source-volume alsa_input.pci-0000_00_1b.0.analog-stereo 50%

执行成功shell返回0,失败查看具体提示。

4.设置麦克风设备静音

基本和外放一样,这里直接贴命令了,不赘述了。

#必须使用当前登录桌面的用户
pactl set-source-mute <name> 1
#示例
pactl set-source-mute alsa_input.pci-0000_00_1b.0.analog-stereo 1

执行成功shell返回0,失败查看具体提示。


总结

1、注意不能用root权限,需要你登录desktop的那个用户执行命令
2、linux上搞外设比windows还是差点,你只能指望有声音,不能指望它能带给你什么惊喜,可能是糟心呢。小声告诉你bug不少!
3、有什么疑问的可以留言

猜你喜欢

转载自blog.csdn.net/jiexijihe945/article/details/134534840