PulseAudio 是一个通用的声音服务器,旨在使用ALSA或OSS作为应用程序和硬件设备之间的中间件运行。如果启用了Avahi,它还可以在本地设备之间提供轻松的网络流。虽然它的主要目的是简化音频配置,但它的模块化设计允许更高级的用户精确地配置守护程序,以最适合他们的需要。
提示:UI控制界面 pavucontrol。
a) debian10 ucm 音频路由配置
b) debian11 ucm2 音频路由配置
c) JACK 耦合关系
UCM 在 Debian10 路径为 /usr/share/alsa/ucm,该目录名字与声卡名是映射关系,其音频路由配置文件与文件关联。看如下目录结构如下:
`-- ucm
|-- rockchip,hdmi
| |-- HDMI.conf
| `-- rockchip,hdmi.conf
`-- rockchip,rk809-codec
|-- HiFi.conf
`-- rockchip,rk809-codec.conf
以 rockchip,hdmi 声卡为例,音频路由映射 ucm --> rockchip,hdmi --> rockchip,hdmi.conf --> HDMI.conf, HDMI.conf 为具体音频路由配置 。
HDMI 声卡音频路由为 rockchip,hdmi.conf 和 HDMI.conf
ucm/rockchip,hdmi/rockchip,hdmi.conf
内容为声明 HDMI.conf 引用和声卡描述。
SectionUseCase."HDMI" {
File "HDMI.conf" # 引用同级目录下的 HDMI.conf
Comment "HDMI output."
}
ucm/rockchip,hdmi/HDMI.conf
仅仅定义一些变量值。
# Use case for devices on rockchip,hdmi card.
SectionVerb {
EnableSequence [
cdev "hw:rockchiphdmi"
]
DisableSequence [
cdev "hw:rockchiphdmi"
]
}
SectionDevice."HDMI".0 {
Comment "HDMI playback"
Value {
PlaybackPCM "hw:rockchiphdmi"
PlaybackChannels "2"
PlaybackPriority "10"
}
EnableSequence [
cdev "hw:rockchiphdmi"
]
DisableSequence [
cdev "hw:rockchiphdmi"
]
}
接下来看看 rk809 声卡的音频路由配置,相同的引用关系:ucm --> rockchip,rk809-codec --> rockchip,rk809-codec.conf --> HiFi.conf。
ucm/rockchip,rk809-codec/rockchip,rk809-codec.conf
中同样声明引用配置文件和声卡描述。
SectionUseCase."HiFi" {
File "HiFi.conf"
Comment "Headset Mic input."
}
ucm/rockchip,rk809-codec/HiFi.conf
实际音频路由配置文件则是这样。
# Use case for devices on rockchip,rk809-codec card.
SectionVerb {
EnableSequence [ #开启时许
cdev "hw:rockchiprk809co"
]
DisableSequence [ #关闭时许
cdev "hw:rockchiprk809co"
]
}
SectionDevice."Headphone" { # 耳机场景配置
Comment "Headphones Playback"
EnableSequence [ # 耳机开启
cdev "hw:rockchiprk809co"
cset "name='Playback Path' HP" # 设定耳机音路由
]
DisableSequence [ #耳机关闭
cdev "hw:rockchiprk809co"
cset "name='Playback Path' OFF" # 关闭耳机音频路由
]
Value {
PlaybackPCM "hw:rockchiprk809co" # 播放声卡
PlaybackChannels "2" # 播放声道数
PlaybackPriority "1" # 播放极性
JackControl "Headphones Jack" # 耳机控制
JackHWMute "Speaker" # 禁用喇叭,此处可以是喇叭功放
}
}
SectionDevice."Speaker" { # 喇叭场景配置
Comment "Speaker Playback"
EnableSequence [
cdev "hw:rockchiprk809co"
cset "name='Playback Path' SPK_HP" #喇叭开启路由设定
]
DisableSequence [
cdev "hw:rockchiprk809co"
cset "name='Playback Path' OFF" #喇叭关闭路由设定
]
Value {
PlaybackPCM "hw:rockchiprk809co"
PlaybackChannels "2" # 播放声道数
PlaybackPriority "2" # 播放极性
}
}
SectionDevice."Headset" { # 耳麦场景
Comment "Headset Mic"
ConflictingDevice [
"MainMic" # 关联设备
]
EnableSequence [
cdev "hw:rockchiprk809co"
cset "name='Capture MIC Path' Hands Free Mic" # 耳麦开启路由设定
]
DisableSequence [
cdev "hw:rockchiprk809co"
cset "name='Capture MIC Path' MIC OFF" # 耳麦关闭路由设定
]
Value {
CapturePCM "hw:rockchiprk809co"
CaptureChannels "2" # 录音声道数
JackControl "Mic Jack" # JACK设定
JackHWMute "MainMic" # 屏蔽主麦
}
}
SectionDevice."MainMic" { # 主麦克风场景
Comment "Main Mic"
ConflictingDevice [
"Headset" # 关联设备
]
EnableSequence [
cdev "hw:rockchiprk809co"
cset "name='Capture MIC Path' Main Mic" # 主麦克风开启路由设定
]
DisableSequence [
cdev "hw:rockchiprk809co"
cset "name='Capture MIC Path' MIC OFF" # 主麦克风关闭路由设定
]
Value {
CapturePCM "hw:rockchiprk809co"
CaptureChannels "2" # 主麦克录音声道数
}
}
Debian11 用 pulseaudio-14.2版本的配置须使用 ucm2。与 ucm不同点是增加 etc/pulse/配置文件,其它路径基本一致。
etc/pulse/
|-- client.conf
|-- daemon.conf
`-- default.pa
接下来逐个解析配置文件
/etc/pulse/client.conf
# This file is part of PulseAudio.
#
# PulseAudio is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# PulseAudio is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
## Configuration file for PulseAudio clients. See pulse-client.conf(5) for
## more information. Default values are commented out. Use either ; or # for
## commenting.
; default-sink =
; default-source =
default-server = unix:/tmp/pulse-socket
; default-dbus-server =
; autospawn = yes
; daemon-binary = /usr/bin/pulseaudio
; extra-arguments = --log-target=syslog
; cookie-file =
; enable-shm = yes
; shm-size-bytes = 0 # setting this 0 will use the system-default, usually 64 MiB
; auto-connect-localhost = no
; auto-connect-display = no
/etc/pulse/daemon.conf
# This file is part of PulseAudio.
#
# PulseAudio is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# PulseAudio is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
## Configuration file for the PulseAudio daemon. See pulse-daemon.conf(5) for
## more information. Default values are commented out. Use either ; or # for
## commenting.
; daemonize = no
; fail = yes
; allow-module-loading = yes
allow-exit = no
; use-pid-file = yes
; system-instance = no
; local-server-type = user
; enable-shm = yes
; enable-memfd = yes
; shm-size-bytes = 0 # setting this 0 will use the system-default, usually 64 MiB
; lock-memory = no
; cpu-limit = no
; high-priority = yes
; nice-level = -11
; realtime-scheduling = yes
; realtime-priority = 5
exit-idle-time = -1
; scache-idle-time = 20
; dl-search-path = (depends on architecture)
; load-default-script-file = yes
; default-script-file = /etc/pulse/default.pa
; log-target = auto
; log-level = notice
; log-meta = no
; log-time = no
; log-backtrace = 0
; resample-method = speex-float-1
; avoid-resampling = false
; enable-remixing = yes
; remixing-use-all-sink-channels = yes
; remixing-produce-lfe = no
; remixing-consume-lfe = no
; lfe-crossover-freq = 0
; flat-volumes = no
; rescue-streams = yes
; rlimit-fsize = -1
; rlimit-data = -1
; rlimit-stack = -1
; rlimit-core = -1
; rlimit-as = -1
; rlimit-rss = -1
; rlimit-nproc = -1
; rlimit-nofile = 256
; rlimit-memlock = -1
; rlimit-locks = -1
; rlimit-sigpending = -1
; rlimit-msgqueue = -1
; rlimit-nice = 31
; rlimit-rtprio = 9
; rlimit-rttime = 200000
; default-sample-format = s16le
; default-sample-rate = 44100
; alternate-sample-rate = 48000
; default-sample-channels = 2
; default-channel-map = front-left,front-right
; default-fragments = 4
; default-fragment-size-msec = 25
; enable-deferred-volume = yes
; deferred-volume-safety-margin-usec = 8000
; deferred-volume-extra-delay-usec = 0
/etc/pulse/default.conf
#!/usr/bin/pulseaudio -nF
#
# This file is part of PulseAudio.
#
# PulseAudio is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# PulseAudio is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
# This startup script is used only if PulseAudio is started per-user
# (i.e. not in system mode)
.fail
### Automatically restore the volume of streams and devices
load-module module-device-restore
load-module module-stream-restore
load-module module-card-restore
### Automatically augment property information from .desktop files
### stored in /usr/share/application
load-module module-augment-properties
load-module module-switch-on-connect blacklist=""
### Should be after module-*-restore but before module-*-detect
load-module module-switch-on-port-available
### Load audio drivers statically
### (it's probably better to not load these drivers manually, but instead
### use module-udev-detect -- see below -- for doing this automatically)
#load-module module-alsa-sink
#load-module module-alsa-source device=hw:1,0
#load-module module-oss device="/dev/dsp" sink_name=output source_name=input
#load-module module-oss-mmap device="/dev/dsp" sink_name=output source_name=input
#load-module module-null-sink
#load-module module-pipe-sink
### Automatically load driver modules depending on the hardware available
.ifexists module-udev-detect.so
load-module module-udev-detect
.else
### Use the static hardware detection module (for systems that lack udev support)
load-module module-detect
.endif
### Automatically connect sink and source if JACK server is present
.ifexists module-jackdbus-detect.so
.nofail
load-module module-jackdbus-detect channels=2
.fail
.endif
### Automatically load driver modules for Bluetooth hardware
.ifexists module-bluetooth-policy.so
load-module module-bluetooth-policy
.endif
.ifexists module-bluetooth-discover.so
load-module module-bluetooth-discover
.endif
### Load several protocols
.ifexists module-esound-protocol-unix.so
load-module module-esound-protocol-unix
.endif
load-module module-native-protocol-unix auth-anonymous=1 socket=/tmp/pulse-socket
### Network access (may be configured with paprefs, so leave this commented
### here if you plan to use paprefs)
#load-module module-esound-protocol-tcp
#load-module module-native-protocol-tcp
#load-module module-zeroconf-publish
### Load the RTP receiver module (also configured via paprefs, see above)
#load-module module-rtp-recv
### Load the RTP sender module (also configured via paprefs, see above)
#load-module module-null-sink sink_name=rtp format=s16be channels=2 rate=44100 sink_properties="device.description='RTP Multicast Sink'"
#load-module module-rtp-send source=rtp.monitor
### Load additional modules from GSettings. This can be configured with the paprefs tool.
### Please keep in mind that the modules configured by paprefs might conflict with manually
### loaded modules.
.ifexists module-gsettings.so
.nofail
load-module module-gsettings
.fail
.endif
### Automatically restore the default sink/source when changed by the user
### during runtime
### NOTE: This should be loaded as early as possible so that subsequent modules
### that look up the default sink/source get the right value
load-module module-default-device-restore
### Automatically move streams to the default sink if the sink they are
### connected to dies, similar for sources
load-module module-rescue-streams
### Make sure we always have a sink around, even if it is a null sink.
load-module module-always-sink
### Honour intended role device property
load-module module-intended-roles
### Automatically suspend sinks/sources that become idle for too long
load-module module-suspend-on-idle
### If autoexit on idle is enabled we want to make sure we only quit
### when no local session needs us anymore.
.ifexists module-console-kit.so
load-module module-console-kit
.endif
.ifexists module-systemd-login.so
load-module module-systemd-login
.endif
### Enable positioned event sounds
load-module module-position-event-sounds
### Cork music/video streams when a phone stream is active
load-module module-role-cork
### Modules to allow autoloading of filters (such as echo cancellation)
### on demand. module-filter-heuristics tries to determine what filters
### make sense, and module-filter-apply does the heavy-lifting of
### loading modules and rerouting streams.
load-module module-filter-heuristics
load-module module-filter-apply
### Make some devices default
set-default-sink 1
set-default-source 1
与 ucm相同的目录结构,简单讲讲其中比较新的HDMI0和 HDMI1,。
.
└── ucm2
├── README.md
├── rockchip-es8388
│ ├── HiFi.conf
│ └── rockchip-es8388.conf
├── rockchip-hdmi0
│ ├── Hdmi.conf
│ └── rockchip-hdmi0.conf
├── rockchip-hdmi1
│ ├── Hdmi.conf
│ └── rockchip-hdmi1.conf
├── rockchip-rk809
│ ├── HiFi.conf
│ └── rockchip-rk809.conf
└── ucm.conf
usr/share/alsa/ucm2/rockchip-hdmi0/rockchip-hdmi0.conf
Syntax 2
Comment "Rockchip HDMI card"
SectionUseCase."HDMI" {
File "Hdmi.conf"
Comment "HDMI/Display Port 1 Stereo"
}
usr/share/alsa/ucm2/rockchip-hdmi0/Hdmi.conf
则是具体音频路由配置
# Usecase for device HDMI0/Display Port stereo playback on rockchip platforms
# For Audio in I2S mode
SectionDevice."HDMI0" {
Comment "HDMI/Display Port 1 Stereo"
Value {
PlaybackPriority 300
PlaybackPCM "hw:${CardId}"
If.1 {
Condition {
Type ControlExists
Control "iface=CARD,name='rockchip-hdmi0 Jack'"
}
True {
JackControl "rockchip-hdmi0 Jack"
}
False {
JackControl "rockchip-hdmi0 Jack"
}
}
}
}
JackControl “rockchip-hdmi0 Jack” 是 liunx 的标准的Jack检测机制。 与之对应的是 arch/arm64/boot/dts/rockchip/rk3588-evb.dtsi:
hdmi0_sound: hdmi0-sound {
status = "disabled";
compatible = "rockchip,hdmi";
rockchip,mclk-fs = <128>;
rockchip,card-name = "rockchip-hdmi0";
rockchip,cpu = <&i2s5_8ch>;
rockchip,codec = <&hdmi0>;
rockchip,jack-det; # 该属性来支持Jack,jack name 就是rockchip,card-name
};
声卡驱动则变为 sound/soc/rockchip/rockchip_hdmi.c,后面章节会详细讲解JACK检测机制。
除了音频路由配置文件路径和规则的,引用关系和目录结构基本没有变化。
usr/share/alsa/ucm2/rockchip-rk809/rockchip-rk809.conf
Syntax 2 # 与ucm相比增加属性
Comment "Rockchip RK809 Codec"
SectionUseCase."HiFi" {
File "HiFi.conf" #引用 HiFi.conf声明
Comment "Default" # 声卡描述变了
}
usr/share/alsa/ucm2/rockchip-rk809/HiFi.conf'
配置文件也是略有变化。
SectionVerb {
Value {
MinBufferLevel "512" # buffer 设定
}
EnableSequence [ #看似很任性的关闭路由
cset "name='Speaker Switch' off"
cset "name='Headphone Switch' off"
cset "name='Headset Mic Switch' off"
cset "name='Main Mic Switch' off"
cset "name='Playback Path' 'OFF'"
cset "name='Capture MIC Path' 'MIC OFF'"
]
}
SectionDevice."Speaker" {
Comment "Speaker"
ConflictingDevice [
"Headphones" #增加属性关联
]
Value {
PlaybackPriority 100
PlaybackPCM "hw:${CardId}" #由静态变动态变量引用
}
EnableSequence [
cset "name='Playback Path' 'SPK'"
]
DisableSequence [
cset "name='Playback Path' 'OFF'"
]
}
SectionDevice."Mic" {
Comment "Internal Microphone"
ConflictingDevice [
"Headset"
]
Value {
CapturePriority 100
CapturePCM "hw:${CardId}"
}
EnableSequence [
cset "name='Capture MIC Path' 'Main Mic'"
]
DisableSequence [
cset "name='Capture MIC Path' 'Mic OFF'"
]
}
SectionDevice."Headphones" {
Comment "Headphones"
ConflictingDevice [
"Speaker"
]
Value {
PlaybackPriority 200
PlaybackPCM "hw:${CardId}"
JackControl "Headphone Jack"
JackHWMute "Speaker"
}
EnableSequence [
cset "name='Playback Path' 'HP'"
]
DisableSequence [
cset "name='Playback Path' 'OFF'"
]
}
SectionDevice."Headset" {
Comment "Headset Microphone"
ConflictingDevice [
"Mic"
]
Value {
CapturePriority 200
CapturePCM "hw:${CardId}"
JackControl "Headset Mic Jack"
JackHWMute "Mic"
}
EnableSequence [
cset "name='Capture MIC Path' 'Hands Free Mic'"
]
DisableSequence [
cset "name='Capture MIC Path' 'Mic OFF'"
]
}
接下来以 es8388声卡音为例,讲解 JACK 内核和音频路由配置。引用关系直接不再赘述,直接跳到重要信息配置。
usr/share/alsa/ucm2/rockchip-es8388/rockchip-es8388.conf
Syntax 2
Comment "Rockchip ES8388 card"
SectionUseCase."HiFi" {
File "HiFi.conf"
Comment "Default"
}
重要信息当然是 HiFi.conf,路径usr/share/alsa/ucm2/rockchip-es8388/HiFi.conf
SectionVerb {
Value {
MinBufferLevel "512"
}
EnableSequence [ #非常银性化的配置
cset "name='Speaker Switch' off"
cset "name='Headphone Switch' off"
cset "name='Headset Mic Switch' off"
cset "name='Main Mic Switch' off"
cset "name='Speaker Switch' off"
cset "name='Headphone Switch' off"
cset "name='Headset Mic Switch' off"
cset "name='Main Mic Switch' off"
cset "name='PCM Volume' 192"
cset "name='Output 1 Playback Volume' 27"
cset "name='Output 2 Playback Volume' 27"
cset "name='Capture Digital Volume' 192"
cset "name='Left Channel Capture Volume' 3"
cset "name='Right Channel Capture Volume' 3"
cset "name='Left Mixer Left Playback Switch' on"
cset "name='Right Mixer Right Playback Switch' on"
cset "name='Capture Mute' off"
cset "name='Right PGA Mux' DifferentialR"
cset "name='Left PGA Mux' DifferentialL"
]
}
SectionDevice."Speaker" {
Comment "Speaker"
ConflictingDevice [
"Headphones"
]
Value {
PlaybackPriority 100
PlaybackPCM "hw:${CardId}"
}
EnableSequence [
cset "name='Speaker Switch' on"
]
DisableSequence [
cset "name='Speaker Switch' off"
]
}
SectionDevice."Mic" {
Comment "Internal Microphone"
ConflictingDevice [
"Headset"
]
Value {
CapturePriority 100
CapturePCM "hw:${CardId}"
}
EnableSequence [
cset "name='Differential Mux' Line 2"
cset "name='Main Mic Switch' on"
]
DisableSequence [
cset "name='Main Mic Switch' off"
]
}
SectionDevice."Headphones" {
Comment "Headphones"
ConflictingDevice [
"Speaker"
]
Value {
PlaybackPriority 200
PlaybackPCM "hw:${CardId}"
JackControl "Headphone Jack" #耳机检测
JackHWMute "Speaker"
}
EnableSequence [
cset "name='Headphone Switch' on"
]
DisableSequence [
cset "name='Headphone Switch' off"
]
}
SectionDevice."Headset" {
Comment "Headset Microphone"
ConflictingDevice [
"Mic"
]
Value {
CapturePriority 200
CapturePCM "hw:${CardId}"
JackControl "Headset Mic Jack" # 耳麦检测
JackHWMute "Mic"
}
EnableSequence [
cset "name='Differential Mux' Line 1"
cset "name='Headset Mic Switch' on"
]
DisableSequence [
cset "name='Headset Mic Switch' off"
]
}
配置中比较显眼jack是 Headphone Jack" 和 “Headset Mic Jack”。“Headphone Jack” 耳机检测jack,可由此确定是耳机或者喇叭播放音频,“Headset Mic Jack” 则是耳机麦克分检测 jack,与 rk809的相同,可由此切换主mic和耳机mic录音。
内核中驱动路径为 kernel/sound/soc/codecs/es8316.c
,与之匹配的声卡设备树如下:
es8316-sound {
compatible = "simple-audio-card";
simple-audio-card,format = "i2s";
simple-audio-card,name = "rockchip,es8316-codec";
simple-audio-card,mclk-fs = <256>;
simple-audio-card,widgets =
"Microphone", "Mic Jack",
"Headphone", "Headphone Jack";
simple-audio-card,routing =
"Mic Jack", "MICBIAS1",
"IN1P", "Mic Jack",
"Headphone Jack", "HPOL",
"Headphone Jack", "HPOR";
simple-audio-card,cpu {
sound-dai = <&i2s0>;
system-clock-frequency = <11289600>;
};
simple-audio-card,codec {
sound-dai = <&es8316>;
system-clock-frequency = <11289600>;
};
};
rk3588 内核设备树中找到参数比较全的 es8388 声卡配置,相比增加如下配置。具体请参照kernel/arch/arm64/boot/dts/rockchip/rk3588-evb7-lp4.dtsi
。
Property | Value | Description |
---|---|---|
hp-det-gpio phandle | phandle | 耳机检测pin,通过中断来检测耳机拔插状态 |
spk-con-gpio | phandle | 功放喇叭控制pin |
hp-con-gpio | phandle | 耳机控制pin |
io-channels | phandle | adc检测通道用来区分3/4段耳机, 以及耳机按键 |
poll-interval | int | adc 轮询时间间隔默认100ms |
keyup-threshold-microvolt | int | adc 按键电压 |
play-pause-key | phandle | 这里定义了播放暂停按键, 可以根据需求定义其他按键 |
rockchip,audio-routing | string | 声卡的 routing |
GPIO1_C4 低电平插入耳机检测
GPIO4_A7 使能耳机
GPIO4_A2 高电平使能喇叭。
ADC3 来区分3/4段耳机, 同时支持耳机线上的播放暂停按键。
Headphone 对应es8388 的 LROUT1,通过Headphone Power 控制 GPIO4_A7。
Speaker 对应es8388 的 LROUT2,通过Speaker Power 控制 GPIO4_A2。
LINPUT对应es8388 的 Main Mic, RINPUT对应Headset Mic。
# 根据es8388声卡索引名"rockchipes8388"查看声卡状态
$ sudo amixer -c rockchipes8388 contents
numid=26,iface=CARD,name='Headphone Jack'
; type=BOOLEAN,access=r-------,values=1
: values=on
numid=27,iface=CARD,name='Headset Mic Jack'
; type=BOOLEAN,access=r-------,values=1
: values=on
SectionDevice."Speaker" {
Comment "Speaker Playback"
EnableSequence [
cdev "hw:rockchiprk809co"
- cset "name='Playback Path' 'SPK'"
+ cset "name='Playback Path' SPK_HP"
]
站在巨人的肩膀上,可以看的更远。