# 9.2.设置声卡

默认情况下，FreeBSD 会自动检测系统使用的声卡。FreeBSD 支持多种声卡，支持的声卡列表可以在 [sound(4)](https://man.freebsd.org/cgi/man.cgi?query=sound\&sektion=4\&format=html) 中查阅。

> **注意**
>
> 仅当 FreeBSD 未能正确检测到声卡时，才需要加载声卡模块。

如果不知道系统使用的是哪个声卡，或者使用哪个模块，可以通过执行以下命令加载 `snd_driver` 元驱动：

```sh
# kldload snd_driver
```

或者，为了在启动时将驱动作为模块加载，可以在 /boot/loader.conf 中添加以下行：

```sh
snd_driver_load="YES"
```

## 9.2.1. 测试声音

为了确认声卡是否已被检测到，可以执行以下命令：

```sh
% dmesg | grep pcm
```

输出应类似于以下内容：

```sh
pcm0: <Conexant CX20561 (Hermosa) (Analog 2.0+HP/2.0)> at nid 26,22 and 24 on hdaa0
pcm1: <Conexant CX20561 (Hermosa) (Internal Analog Mic)> at nid 29 on hdaa0
```

还可以使用以下命令检查声卡的状态：

```sh
# cat /dev/sndstat
```

输出应类似于以下内容：

```sh
Installed devices:
pcm0: <Conexant CX20561 (Hermosa) (Analog 2.0+HP/2.0)> (play/rec) default
pcm1: <Conexant CX20561 (Hermosa) (Internal Analog Mic)> (rec)
```

如果没有列出 `pcm` 设备，请仔细检查是否加载了正确的设备驱动。如果一切顺利，声卡应该可以在 FreeBSD 上正常工作。

可以使用 [beep(1)](https://man.freebsd.org/cgi/man.cgi?query=beep\&sektion=1\&format=html) 发出一些声音，以确认声卡是否正常工作：

```sh
% beep
```

## 9.2.2. 混音器

FreeBSD 提供了不同的实用程序来设置和显示声卡混音器值，这些程序基于 FreeBSD 声音系统：

**表 1. 支持的混音器**

| 名称                                                                                  | 许可证     | 包                                                                                       | 工具包 |
| ----------------------------------------------------------------------------------- | ------- | --------------------------------------------------------------------------------------- | --- |
| [mixer(8)](https://man.freebsd.org/cgi/man.cgi?query=mixer\&sektion=8\&format=html) | BSD-2   | 系统自带                                                                                    | CLI |
| dsbmixer                                                                            | BSD-2   | [audio/dsbmixer](https://cgit.freebsd.org/ports/tree/audio/dsbmixer/)                   | Qt  |
| KDE Plasma 音频小工具                                                                    | GPL 2.0 | [audio/plasma6-plasma-pa](https://cgit.freebsd.org/ports/tree/audio/plasma6-plasma-pa/) | Qt  |
| mixertui                                                                            | BSD-2   | [audio/mixertui](https://cgit.freebsd.org/ports/tree/audio/mixertui/)                   | TUI |

## 9.2.3. 显卡声音

显卡通常带有自己的集成声音设备，可能不清楚哪个设备被用作默认设备。可以通过运行 `dmesg` 并查看 pcm 条目来确认系统如何枚举输出。执行以下命令：

```sh
% dmesg | grep pcm
```

输出可能类似于以下内容：

```sh
pcm0: <HDA NVIDIA (Unknown) PCM #0 DisplayPort> at cad 0 nid 1 on hdac0
pcm1: <HDA NVIDIA (Unknown) PCM #0 DisplayPort> at cad 1 nid 1 on hdac0
pcm2: <HDA NVIDIA (Unknown) PCM #0 DisplayPort> at cad 2 nid 1 on hdac0
pcm3: <HDA NVIDIA (Unknown) PCM #0 DisplayPort> at cad 3 nid 1 on hdac0
hdac1: HDA Codec #2: Realtek ALC889
pcm4: <HDA Realtek ALC889 PCM #0 Analog> at cad 2 nid 1 on hdac1
pcm5: <HDA Realtek ALC889 PCM #1 Analog> at cad 2 nid 1 on hdac1
pcm6: <HDA Realtek ALC889 PCM #2 Digital> at cad 2 nid 1 on hdac1
pcm7: <HDA Realtek ALC889 PCM #3 Digital> at cad 2 nid 1 on hdac1
```

显卡（NVIDIA®）被列在声卡（Realtek®）之前，声卡作为 `pcm4` 显示。可以通过执行以下命令将系统配置为使用声卡作为默认设备：

```sh
# sysctl hw.snd.default_unit=4
```

为了使此更改永久生效，可以将以下行添加到 /etc/sysctl.conf 文件中：

```sh
hw.snd.default_unit=4
```

## 9.2.4. 自动切换到耳机

某些系统可能会在音频输出之间切换时遇到困难，但幸运的是，FreeBSD 允许在 `device.hints` 中配置自动切换。

通过执行以下命令，识别系统如何枚举音频输出：

```sh
% dmesg | grep pcm
```

输出可能类似于以下内容：

```sh
pcm0: <Realtek ALC892 Analog> at nid 23 and 26 on hdaa0
pcm1: <Realtek ALC892 Right Analog Headphones> at nid 22 on hdaa0
```

将以下行添加到 /boot/device.hints：

```ini
hint.hdac.0.cad0.nid22.config="as=1 seq=15 device=Headphones"
hint.hdac.0.cad0.nid26.config="as=2 seq=0 device=speakers"
```

> **注意**
>
> 切记，这些值适用于上面示例中的情况。它们可能会根据系统的不同而有所变化。

## 9.2.5. 故障排除

一些常见的错误信息及其解决方案：

| 错误信息                        | 解决方案                                                                         |
| --------------------------- | ---------------------------------------------------------------------------- |
| `xxx: can’t open /dev/dsp!` | 输入 `fstat \| grep dsp` 检查是否有其他应用程序正在占用该设备。需要注意的“麻烦制造者”包括 esound 和 KDE 的声音支持。 |

使用 [audio/pulseaudio](https://cgit.freebsd.org/ports/tree/audio/pulseaudio/) 的程序可能需要重新启动 [audio/pulseaudio](https://cgit.freebsd.org/ports/tree/audio/pulseaudio/) 守护进程，以使 `hw.snd.default_unit` 的更改生效。或者，也可以即时更改 [audio/pulseaudio](https://cgit.freebsd.org/ports/tree/audio/pulseaudio/) 的设置。使用 [pacmd(1)](https://man.freebsd.org/cgi/man.cgi?query=pacmd\&sektion=1\&format=html) 可以连接到 [audio/pulseaudio](https://cgit.freebsd.org/ports/tree/audio/pulseaudio/) 守护进程的命令行界面：

```sh
# pacmd
Welcome to PulseAudio 14.2! Use "help" for usage information.
>>>
```

以下命令将默认输出设备更改为前面示例中的第 4 号卡：

```sh
set-default-sink 4
```

> **警告**
>
> 不要使用 `exit` 命令退出命令行界面。那样会终止 [audio/pulseaudio](https://cgit.freebsd.org/ports/tree/audio/pulseaudio/) 守护进程。请改用 <kbd>Ctrl</kbd>+<kbd>D</kbd>。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://handbook.bsdcn.org/di-9-zhang-duo-mei-ti/9.2.-she-zhi-sheng-ka.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
