Advanced Linux Sound Architecture (简体中文)

From ArchWiki
Revision as of 02:45, 25 April 2013 by Cuihao (Talk | contribs) (内置麦克风无效)

Jump to: navigation, search

Template:Article summary start Template:Article summary text Template:Article summary heading Template:Article summary wiki Template:Article summary wiki Template:Article summary end

翻译状态: 本文是英文页面 Advanced_Linux_Sound_Architecture翻译,最后翻译时间:2013-04-25,点击这里可以查看翻译后英文页面的改动。

高级 Linux 声音体系(Advanced Linux Sound Architecture,ALSA)是Linux中提供声音设备驱动的内核组件,用来代替原来的开放声音系统(Open Sound System,OSSv3)。除了声音设备驱动,ALSA还包含一个用户空间的函数库,以方便开发者通过高级API使用驱动功能,而不必直接与内核驱动交互。

注意: 关于另一种声音体系,请阅读开放声音系统(OSS)的页面。

Contents

安装

Arch 默认的内核已经通过一套模块提供了 ALSA,不必特别安装。

udev会在系统启动时自动检测硬件,并加载相应的声音设备驱动模块。这时,你的声卡已经可以工作了,只是所有声道默认都被设置成静音了。

在本地登录(通过虚拟终端或登录管理器)的用户,都有权限播放音频并调整音量。要让远程登录的用户拥有这些权限,必须把用户加入 audio 用户组。该组的成员可以直接访问声音设备,会导致某些程序独占音频输出(破坏软件混音),还可能影响用户快速切换和拖机(multiseat)。因此,除非真的有某些特殊需求不建议把用户加入 audio 用户组。

用户空间工具

官方仓库

  • 安装 alsa-utils 软件包,其中的 alsamixer 工具允许用户在控制台或终端中配置声音设备。
  • 安装 alsa-oss 软件包,如果想让 OSS 应用程序与dmix(软件混音)协同工作。
注意: udev的171版本开始,默认情况下 OSS 仿真模块(snd_seq_osssnd_pcm_osssnd_mixer_oss)不再自动加载。

解除各声道的静音

目前版本的 ALSA 安装后,所有声道默认是静音的,必须手动解除。

使用 alsamixer 的 ncurses 界面,配置十分简单:

$ alsamixer

此外,还可以在命令行下使用 amixer

$ amixer sset Master unmute

alsamixer 中,下方标有 MM 的声道是静音的,而标有 00 的通道已经启用。

使用左右方向键,选中 MasterPCM 声道。按下 Template:Keypress 键解除静音。使用上方向键增加音量,直到增益值为0。该值显示在左上方 Item: 字段后。过高的增益值会导致声音失真。

要想得到完整的 5.1 或 7.1 环绕立体声,还得解除 Front、Surround、Center、LFE (subwoofer) 和 Side 这些声道的静音(上述名称是 Intel HD Audio 声卡使用的声道名,可能因设备不同而有所差异)。注意,仅有这些设置,系统不会自动将立体声源(多数音乐)提升(upmix)成环绕立体声。如果需要这些功能,请阅读#Upmixing/Downmixing

要启用麦克风,切换至 Capture 选项卡,按下 Template:Keypress,按下 Template:Keypress 启用其中一个声道即可。

按下 Template:Keypress 键退出 alsamixer。

注意:
  • 有一些声卡,需要关闭数字输出或将其调成静音,才能输出模拟音频信号。对于 Soundblaster Audigy LS 声卡,需要把 IEC958 通道设成静音。
  • 有些机器(如 Thinkpad T61),有一个 Speaker 通道,必须按照上述方法打开调整。
  • 有些机器(如 Dell E6400),可能还需要把 FrontHeadphone 声道打开调整。

接下来,测试声卡是否工作:

$ speaker-test -c 2

根据扬声器的实际情况,调整 -c 后的数值。对于 7.1 声道,这个数字是 8:

$ speaker-test -c 8

如果没有正常工作,请继续阅读 #配置 以及 #疑难解答 部分。

alsa-utils 软件包提供了两个配置 alsa 的服务:alsa-restore.servicealsa-store.service。它们分别在开机和关机时自动运行。

配置

VirtualBox 虚拟机无声音

如果使用 VirtualBox 时碰上了麻烦,请尝试以下命令:

$ alsactl init

Found hardware: "ICH" "SigmaTel STAC9700,83,84" "AC97a:83847600" "0x8086" "0x0000"
Hardware is initialized using a generic method

如果还是不行,请检查所使用的音频软件是否激活了 ALSA 输出。

设置默认声卡

如果发现开机时声卡次序会发生变化,可以在通过 /etc/modprobe.d 中的 .conf 文件(比如 /etc/modprobe.d/alsa-base.conf)手动设置次序。 比如,要让 mia 声卡成为 #0、Intel HDA 声卡成为 #1:

/etc/modprobe.d/alsa-base.conf
options snd slots=snd_mia,snd_hda_intel
options snd_mia index=0
options snd_hda_intel index=1

使用 lsmod | grep snd 获取设备列表。

snd_mia 和 snd_hda_intel 分别是两个声卡的内核驱动模块。该配置仅适用于同时装有 mia 声卡和使用 snd_hda_intel 驱动的声卡(通常是板载声卡)的情况。

如果使用 -2 的 index 值,ALSA 就不会将对应的设备作为主声卡使用。Linux Mint 和 Ubuntu 等发行版使用了以下配置,避免 USB 声卡和其他“非主流”设备变成 index 为 0 的主声卡:

/etc/modprobe.d/alsa-base.conf
options bt87x index=-2
options cx88_alsa index=-2
options saa7134-alsa index=-2
options snd-atiixp-modem index=-2
options snd-intel8x0m index=-2
options snd-via82xx-modem index=-2
options snd-usb-audio index=-2
options snd-usb-caiaq index=-2
options snd-usb-ua101 index=-2
options snd-usb-us122l index=-2
options snd-usb-usx2y index=-2
# Keep snd-pcsp from being loaded as first soundcard
options snd-pcsp index=-2
# Keep snd-usb-audio from beeing loaded as first soundcard
options snd-usb-audio index=-2

以上配置会在系统重启后生效。

Tango-view-refresh-red.pngThis article or section is out of date.Tango-view-refresh-red.png

Reason: 据英文 Wiki 用户反应,/etc/asound.conf 这个配置文件默认是不存在的,下列操作可能有误。 (Discuss in Talk:Advanced Linux Sound Architecture (简体中文)#)

首先,运行 aplay -l,获取声卡的声卡ID和设备ID:

$ aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: Intel [HDA Intel], device 0: CONEXANT Analog [CONEXANT Analog]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 0: Intel [HDA Intel], device 1: Conexant Digital [Conexant Digital]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: JamLab [JamLab], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 2: Audio [Altec Lansing XT1 - USB Audio], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

拿输出中的最后一项来说,这个声卡的声卡ID为2,设备ID为0。若要把这块声卡作为默认声卡,可以把下列配置添加到系统级别的 /etc/asound.conf 或用户级别的 ~/.asoundrc 文件。如果文件不存在,可以手动创建。其中的各个ID,请根据实际情况调整:

defaults.pcm.card 2
defaults.pcm.device 0
defaults.ctl.card 2

“pcm”选项决定用来播放音频的设备,而“ctl”选项决定那个声卡能够由控制工具(如 alsamixer)使用。

上述配置在重启音频程序(如 mplayer)后立即生效。

确认所有声音模块都已经加载

一般 udev 都会自动识别出声卡。使用以下命令确认:

$ lsmod | grep '^snd' | column -t
snd_hda_codec_hdmi     22378   4
snd_hda_codec_realtek  294191  1
snd_hda_intel          21738   1
snd_hda_codec          73739   3  snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_hda_intel
snd_hwdep              6134    1  snd_hda_codec
snd_pcm                71032   3  snd_hda_codec_hdmi,snd_hda_intel,snd_hda_codec
snd_timer              18992   1  snd_pcm
snd                    55132   9  snd_hda_codec_hdmi,snd_hda_codec_realtek,snd_hda_intel,snd_hda_codec,snd_hwdep,snd_pcm,snd_timer
snd_page_alloc         7017    2  snd_hda_intel,snd_pcm

如果你的输出和上面类似,那就说明声卡已经被正确识别。

注意: udev的171版本开始,默认情况下 OSS 仿真模块(snd_seq_osssnd_pcm_osssnd_mixer_oss)不再自动加载。如有需要,请手动加载

还可以检查一下 /dev/snd/ 目录,看看是否有这些设备文件:

$ ls -l /dev/snd
total 0
crw-rw----  1 root audio 116,  0 Apr  8 14:17 controlC0
crw-rw----  1 root audio 116, 32 Apr  8 14:17 controlC1
crw-rw----  1 root audio 116, 24 Apr  8 14:17 pcmC0D0c
crw-rw----  1 root audio 116, 16 Apr  8 14:17 pcmC0D0p
crw-rw----  1 root audio 116, 25 Apr  8 14:17 pcmC0D1c
crw-rw----  1 root audio 116, 56 Apr  8 14:17 pcmC1D0c
crw-rw----  1 root audio 116, 48 Apr  8 14:17 pcmC1D0p
crw-rw----  1 root audio 116,  1 Apr  8 14:17 seq
crw-rw----  1 root audio 116, 33 Apr  8 14:17 timer
注意: 在 IRC 或论坛寻求这方面帮助时,别忘了贴出上面几个命令的输出。

如果你的输出跟上面类似,或至少有 controlC0pcmC0D0p,那么声卡就已经正常加载了。

如果出现问题,声卡模块没有正确加载,那么请尝试手动加载模块:

  • 确定声卡对应的驱动模块:ALSA Soundcard Matrix。这些模块都会有一个”snd-“前缀(例如:snd-via82xx)。
  • 加载模块
  • 检查 /dev/snd 目录中的设备文件(参见上文);或者,检查 alsamixeramixer 的输出是否正确。
  • 修改配置,使 snd-<模块名>snd-pcm-oss 模块开机自动加载

启用 SPDIF 输出

(由 Gentoo 论坛的 gralves 提供)

  • 在 GNOME 音量控制的选项(Options)标签中,把 IEC958 改成 PCM。该选项可以在首选项中启用。
  • 不使用 GNOME 音量控制工具的话:
    • 编辑 /var/lib/alsa/asound.state。该文件是 alsasound 保存混音器设置的地方。
    • 找到这一行:“IEC958 Playback Switch”。该行上下应该有一行内容为 value:false,将其改为 value:true
    • 再找到这一行:”IEC958 Playback AC97-SPSA“。把对应的值改为 0。
    • 重启 ALSA。

Tango-view-refresh-red.pngThis article or section is out of date.Tango-view-refresh-red.png

Reason: rc.local 在 systemd 中已经弃用,以下方法可能不再适用。 (Discuss in Talk:Advanced Linux Sound Architecture (简体中文)#)

还有另外一个开机自动启用 SPDIF 输出的方法(在 SoundBlaster Audigy 声卡上测试过):

  • /etc/rc.local 中添加如下内容:
 # Use COAX-digital output
 amixer set 'IEC958 Optical' 100 unmute
 amixer set 'Audigy Analog/Digital Output Jack' on

使用以下命令,获取你的声卡的数字输出的名称:

 $ amixer scontrols

系统级均衡器

使用 AlsaEqual(包含界面)

AUR 安装 alsaequalAUR

注意: 如果使用的是64位系统,而又安装了32位的 Flash 插件,这里的设置会导致 Flash 无声。如果发生了这种情况,请禁用 alsaequal 或编译32位版本的 alsaequal。

安装后,把下列内容添加到 ALSA 配置文件(~/.asoundrc/etc/asound.conf):

ctl.equal {
 type equal;
}

pcm.plugequal {
  type equal;
  # Modify the line below if you do not
  #slave.pcm "plughw:0,0";
  #by default we want to play from more sources at time:
  slave.pcm "plug:dmix";
}
#pcm.equal {
  # If you do not want the equalizer to be your
  # default soundcard comment the following
  # line and uncomment the above line. (You can
  # choose it as the output device by addressing
  # it with specific apps,eg mpg123 -a equal 06.Back_In_Black.mp3)
pcm.!default {
  type plug;
  slave.pcm plugequal;
}

设置好后,切换均衡器:

$ alsamixer -D equal

需要注意,每个用户的 alsaequal 配置文件都是独立的,位于 ~/.alsaequal.bin。因此,如果想在 mpd 之类的以独立用户身份运行的程序中使用 AlsaEqual,就得像下面这样:

# su mpd -c 'alsamixer -D equal'

或者,在其他用户的主目录下创建符号链接,指向你的 .alsaequal.bin……

管理 AlsaEqual 配置

Xyne 的软件仓库AUR 安装 alsaequal-mgr

同上,配置均衡器:

$alsamixer -D equal

如果对效果感到满意,将其命名(如“foo”)并保存该配置:

$alsaequal-mgr save foo

重新加载“foo”中的配置:

$alsaequal-mgr load foo

这样,就可以为游戏、电影、不同音乐流派、网络电话等等创建不同的均衡器配置,以便随时切换。

参见项目主页和程序的帮助信息,了解更多设置。

使用 mbeq

注意: 该软件用到了一个 ladspa 插件,可能导致播放音频时 CPU 占用率偏高。In addition, this was made with stereophonic sound (e.g. headphones) in mind.

安装 alsa-pluginsladspaswh-plugins 软件包。

  • ~/.asoundrc/etc/asound.conf 中添加如下内容:
/etc/asound.conf
pcm.eq {
  type ladspa

  # The output from the EQ can either go direct to a hardware device
  # (if you have a hardware mixer, e.g. SBLive/Audigy) or it can go
  # to the software mixer shown here.
  #slave.pcm "plughw:0,0"
  slave.pcm "plug:dmix"

  # Sometimes you may need to specify the path to the plugins,
  # especially if you have just installed them.  Once you have logged
  # out/restarted this should not be necessary, but if you get errors
  # about being unable to find plugins, try uncommenting this.
  #path "/usr/lib/ladspa"

  plugins [
    {
      label mbeq
      id 1197
      input {
        #this setting is here by example, edit to your own taste
        #bands: 50hz, 100hz, 156hz, 220hz, 311hz, 440hz, 622hz, 880hz, 1250hz, 1750hz, 25000hz,
        #50000hz, 10000hz, 20000hz
        controls [ -5 -5 -5 -5 -5 -10 -20 -15 -10 -10 -10 -10 -10 -3 -2 ]
      }
    }
  ]
 }

 # Redirect the default device to go via the EQ - you may want to do
 # this last, once you are sure everything is working.  Otherwise all
 # your audio programs will break/crash if something has gone wrong.

 pcm.!default {
  type plug
  slave.pcm "eq"
 }

 # Redirect the OSS emulation through the EQ too (when programs are running through "aoss")

 pcm.dsp0 {
  type plug
  slave.pcm "eq"
 }
  • 完成。若有问题,欢迎前往论坛咨询。

高质量重采样

启用软件混音时,ALSA 会强制把所有音频重采样到相同的频率(如果系统支持,默认是 48000)。dmix 的重采样算法很糟糕,会导致显著的质量损失。

安装 alsa-pluginslibsamplerate

将默认的采样率转换器更改为 libsamplerate:

/etc/asound.conf
defaults.pcm.rate_converter "samplerate_best"

或:

~/.asoundrc
defaults.pcm.rate_converter "samplerate_best"

samplerate_best 输出的声音质量最佳,但需要耗费大量CPU资源进行实时重采样。还有其他的算法可供选择(如 samplerate 等),但比起默认的算法,这些算法的改进并不明显。

警告: 某些系统中,启用 samplerate_best 可能导致 Flash 播放器无声音。

Upmixing/Downmixing

Upmixing

In order for stereo sources like music to be able to saturate a 5.1 or 7.1 sound system, you need to use upmixing. In darker days this used to be tricky and error prone but nowadays plugins exist to easily take care of this task. Thus, install alsa-plugins.

Then add the following to your ALSA configuration file of choice (either /etc/asound.conf or ~/.asoundrc):

pcm.upmix71 {
    type upmix
    slave.pcm "surround71"
    delay 15
    channels 8
}

You can easily change this example for 7.1 upmixing to 5.1 or 4.0.

This adds a new pcm that you can use for upmixing. If you want all sound sources to go through this pcm, add it as a default below the previous definition like so:

pcm.!default "plug:upmix71"

The plugin automatically allows multiple sources to play through it without problems so setting is as a default is actually a safe choice. If this is not working, you have to setup your own dmixer for the upmixing PCM like this:

pcm.dmix6 {
    type asym
    playback.pcm {
        type dmix
        ipc_key 567829
        slave {
            pcm "hw:0,0"
            channels 6
        }
    }
}

and use "dmix6" instead of "surround71". If you experience skipping or distorted sound, consider increasing the buffer_size (to 32768, for example) or use a high quality resampler.

Downmixing

If you want to downmix sources to stereo because you, for instance, want to watch a movie with 5.1 sound on a stereo system, you need to use the vdownmix plugin that alsa provides in the same package.

Again, in your configuration file, add this:

pcm.!surround51 {
    type vdownmix
    slave.pcm "default"
}
pcm.!surround40 {
    type vdownmix
    slave.pcm "default"
}

混音

软件混音(dmix)

注意: 在 ALSA 1.0.9rc2 及以上版本中,如果声卡不支持硬件混音,模拟输出将默认使用 dmix 混音,无需额外设置。

如果软件混音未正常启用,那么可以手动创建 ~/.asoundrc,加入以下内容:

pcm.dsp {
    type plug
    slave.pcm "dmix"
}

这样就启用了软件混音,多个程序能够同时使用声卡。

至于 S/PDIF 之类的数字音频输出,目前的 ALSA 还不会默认启用 dmix。因此,必须像上面这样手动启用。

常见问题的解答,参见#疑难解答部分。

硬件混音

支持

如果声卡支持,一般无需任何配置即可启用硬件混音。绝大多数板载声卡都不支持硬件混音,必须得使用软件混音(见上文)。很多独立声卡都支持硬件混音,Linux 下支持较好的有:

  • Creative SoundBlaster Live! (5.1 型号)
  • Creative SoundBlaster Audigy (部分型号)
  • Creative SoundBlaster Audidy 2 (ZS 系列型号)
  • Creative SoundBlaster Audigy 4 (Pro 系列型号)
注意: 上述声卡的低端版本(Audigy SE、Audigy 2 NX、SoundBlaster Live! 24bit 以及 SoundBlaster Live! 7.1)使用了不同的芯片,不支持硬件混音。
注意: 板载 VIA8237 芯片支持4路硬件混音,但在某些主板上只能使用3路(第4路无声),甚至根本无法使用。即使能正常工作,质量也比不上其他混音方法。

问题修复

如果你的声卡是 Intel Corporation 82801I (ICH9 Family) HD Audio Controller (rev 02),且使用了64位系统,需要执行以下命令才能在 Enemy Territory 中听到声音:

echo "et.x86 0 0 direct" > /proc/asound/card0/pcm0p/oss
echo "et.x86 0 0 disable" > /proc/asound/card0/pcm0c/oss

疑难解答

使用CPU频率动态调整时,音频发生跳跃

使用 ondemandconservative 频率策略、启用CPU频率动态调整时,某些声卡和某些 ALSA 驱动会出现声音跳跃的情况。目前,唯一的解决方案是使用固定的频率,如 performance 性能策略。

详情参见 CPU Frequency Scaling (简体中文)

同时只能有一个用户播放音频

有时会出现一个用户独占软件混音器的情况。多数情况下这没什么影响,但在使用 mpd 这样以独立用户身份运行的程序时,就会出现问题。当 mpd 运行时,用户自己就无法播放其他音频了。一种解决方法是,以正在使用的用户身份运行 mpd。另一种方法是,在 ALSA 配置文件的 pcm.dmixer 部分添加 ipc_key_add_uid 0 禁止独占。以下是 asound.conf 示例的片段,省略的部分无需修改:

...
pcm.dmixer {
 type dmix
 ipc_key 1024
 ipc_key_add_uid 0
 ipc_perm 0660
slave {
...

无法同时播放多个音频

如果安装了 PulseAudio(可能作为 GNOME 的依赖被安装),可能出现无法同步播放的问题,因为 PulseAudio 的默认配置是“独占”声卡。ALSA 的用户可能对自己的配置已经很满意、不希望使用 PulseAudio。解决方案是编辑 /etc/asound.conf 并注释掉以下内容:

# Use PulseAudio by default
#pcm.!default {
#  type pulse
#  fallback "sysdefault"
#  hint {
#    show on
#    description "Default ALSA Output (currently PulseAudio Sound Server)"
#  }
#}

可能还需要注释掉下面的内容:

#ctl.!default {
#  type pulse
#  fallback "sysdefault"
#}

比起完全卸载 PulseAudio,这大概是个更简单的方法。

以下是 /etc/asound.conf 配置文件的一个示例:

pcm.dmixer {
        type dmix
        ipc_key 1024
        ipc_key_add_uid 0
        ipc_perm 0660
}
pcm.dsp {
        type plug
        slave.pcm "dmix"
}
注意: /etc/asound.conf 本来是配合 MPD 全局配置使用的。参见该节关于多用户的内容。

有时候开机无声

如果会随机地出现开机无声的问题,原因可能是多声卡的顺序发生了变化。如果是这种情况,请参照#设置默认声卡进行设置。

对于 mpd 用户,上述配置无效,请参考该文设置。

特定程序的问题

对于不使用系统声音配置的程序,如 XMMS 和 Mplayer,必须特别设置以应用 ALSA 配置。

对于 mplayer,将以下内容添加到 ~/.mplayer/config (或系统全局配置文件 /etc/mplayer/mplayer.conf):

ao=alsa

对于 XMMS 和 Beep Media Player,打开选项窗口,要把声音驱动设置为 ALSA 而不是 OSS。

XMMS 的设置方法:

  • 启动 XMMS
    • 选项 -> 首选项
    • 选择 Alsa 输出插件

对于未提供 ALSA 输出的程序,可以使用 alsa-oss 软件包中的 aoss 工具启动。使用方法是:在要运行的程序的命令行前加上 aoss。例如:

aoss realplay

pcm.!default{ ... } 貌似已经不管用了。但下面的设置有效:

 pcm.default pcm.dmixer

特定型号的配置

ALSA 会通过 BIOS 检测声卡型号,少数情况下也可能无法正确识别。声卡芯片类型可以在 alsamixer 界面上找到(如 ALC662),而型号则要设置在 /etc/modprobe.d/modprobe.conf/etc/modprobe.d/sound.conf 文件中:

options snd-hda-intel model=<型号>

具体设置因声卡型号而异。多数情况下不需要特地这样做。如果想知道具体声卡型号的设置,请查阅 Alsa Soundcard List,找到你的声卡型号,点击“Details”,然后查看“Setting up modprobe...”小节,将其中的内容插入 /etc/modprobe.d/modprobe.conf 文件即可。例如,Intel AC97 的配置如下:

# ALSA portion
alias char-major-116 snd
alias snd-card-0 snd-intel8x0
# module options should go here

# OSS/Free portion
alias char-major-14 soundcore
alias sound-slot-0 snd-card-0

# card #1
alias sound-service-0-0 snd-mixer-oss
alias sound-service-0-1 snd-seq-oss
alias sound-service-0-3 snd-pcm-oss
alias sound-service-0-8 snd-seq-oss
alias sound-service-0-12 snd-pcm-oss

与蜂鸣器冲突

如果你确认所有声道都已解除静音,声卡驱动配置正确,且音量大小也合适,但还是听不到声音,那么请试试将下面的内容添加到 /etc/modprobe.d/modprobe.conf

(如果是 via82xx 驱动)

options snd-<模块名请自行补充> ac97_quirk=0

(如果是 snd_intel8x0 驱动)

options snd-NAME-OF-MODULE ac97_quirk=1

麦克风输入无声音

In alsamixer, make sure that all the volume levels are up under recording, and that CAPTURE is toggled active on the microphone (e.g. Mic, Internal Mic) and/or on Capture (in alsamixer, select these items and press space). Try making positive Mic Boost and raising Capture and Digital levels higher; this make make static or distortion, but then you can adjust them back down once you are hearing something when you record

As the pulseaudio wrapper is shown as "default" in alsamixer, you may have to press F6 to select your actual soundcard first. You may also need to enable and increase the volume of Line-in in the Playback section.

To test the microphone, run these commands (see arecord's man page for further information):

 arecord -d 5 test-mic.wav
 aplay test-mic.wav

If all fails, you may want to eliminate hardware failure by testing the microphone with a different device.

For at least some computers, muting a microphone (MM) simply means its input does not go immediately to the speakers. It still receives input.

Many Dell laptops need "-dmic" to be appended to the model name in /etc/modprobe.d/modprobe.conf:

 options snd-hda-intel model=dell-m6-dmic

Some programs use try to use OSS as the main input software. Add the following lines to /etc/modprobe.d/modprobe.conf to prevent OSS modules from being loaded:

Note: The OSS modules are no longer autoloaded anyway.
blacklist snd_pcm_oss
blacklist snd_mixer_oss
blacklist snd_seq_oss

See also:

设置默认的麦克风/录音设备

某些程序(Pidgin、Adobe Flash)提供了设置录音设备的选项。如果录音设备不是内置的声卡,而是外接的(如USB摄像头、麦克风),就会有些问题。如果只想更改默认的录音设备、不更改回放设备,需要像下面这样设置 ~/.asoundrc

pcm.usb
{
    type hw
    card U0x46d0x81d
}

pcm.!default
{
    type asym
    playback.pcm
    {
        type plug
        slave.pcm "dmix"
    }
    capture.pcm
    {
        type plug
        slave.pcm "usb"
    }
}

把“U0x46d0x81d”替换为你的录音设备的名称。可以通过 arecord -L 查看所有 ALSA 识别到的录音设备。

内置麦克风无效

首先,请检查 alsamixer,确保所有的录音声道的音量都达到可以录制的程度。如果还是不行,试试修改 /etc/sound.conf 并重载 snd-* 模块。这样就会多出来一个名叫 Capture 的音频控制,可以用它调节内置麦克风的音量。例如,对于 snd-hda-intel 模块,配置如下:

 options snd-hda-intel enable_msi=1

然后,输入下面的命令重载模块,调整音量并测试。

# rmmod snd-hda-intel && modprobe snd-hda-intel

Intel 板载声卡无声

可能由两个相互冲突的驱动模块引起,分别是 snd_intel8x0snd_intel8x0m。如果是的话,屏蔽 snd_intel8x0m 即可:

/etc/modprobe.d/modprobe.conf
blacklist snd_intel8x0m

以下方法可能也有效:在 alsamixeramixer 把 “External Amplifier” 设置为静音。参见:ALSA wiki 相关条目

也可以试试解除“Mix”的静音。

No Headphone Sound with Onboard Intel Sound Card

With Intel Corporation 82801 I (ICH9 Family) HD Audio Controller on laptop, you may need to add this line to modprobe or sound.conf:

options snd-hda-intel model=$model

Where $model is any one of the following (in order of possibility to work, but not merit):

  • dell-vostro
  • olpc-xo-1_5
  • laptop
  • dell-m6
  • laptop-hpsense

Note: It may be necessary to put this "options" line below (after) any "alias" lines about your card.

You can see all the available models in the kernel documentation. For example here, but check that it is the correct version of that document for your kernel version.

A list of available models is also available here. To know your chip name type the following command (with * being corrected to match your files). Note that some chips could have been renamed and do not directly match the available ones in the file.

cat /proc/asound/card*/codec* | grep Codec

Note that there is a high chance none of the input devices (all internal and external mics) will work if you choose to do this, so it is either your headphones or your mic. Please report to ALSA if you are affected by this bug.

And also, if you have problems getting beeps to work (pcspkr):

options snd-hda-intel model=$model enable=1 index=0

No sound when S/PDIF video card is installed

Discover available modules and their order:

$ cat /proc/asound/modules
0 snd_hda_intel
1 snd_ca0106

Disable the undesired video card audio codec in /etc/modprobe.d/modprobe.conf:

# /etc/modprobe.d/modprobe.conf
#
install snd_hda_intel /bin/false

If both devices use the same module, it might be possible to disable one of them in the BIOS.

Poor sound quality or clipping

If you experience poor sound quality, try setting the PCM volume (in alsamixer) to a level such that gain is 0.

If snd-usb-audio driver has been loaded, you could try to enable softvol in /etc/asound.conf file. Example configuration for the first audio device:

 pcm.!default {
   type plug
   slave.pcm "softvol"
 }
 pcm.dmixer {
      type dmix
      ipc_key 1024
      slave {
          pcm "hw:0"
          period_time 0
          period_size 4096
          buffer_size 131072
          rate 50000
      }
      bindings {
          0 0
          1 1
      }
 }
 pcm.dsnooper {
      type dsnoop
      ipc_key 1024
      slave {
          pcm "hw:0"
          channels 2
          period_time 0
          period_size 4096
          buffer_size 131072
          rate 50000
      }
      bindings {
          0 0
          1 1
      }
 }
 pcm.softvol {
      type softvol
      slave { pcm "dmixer" }
      control {
          name "Master"
          card 0
      }
 }
 ctl.!default {
   type hw
   card 0
 }
 ctl.softvol {
   type hw
   card 0
 }
 ctl.dmixer {
   type hw
   card 0
 }

Pops When Starting and Stopping Playback

Some modules (e.g. snd_ac97_codec and snd_hda_intel) can power off your sound card when not in use. This can make an audible noise (like a crack/pop/scratch) when turning on/off your sound card. Sometimes even when move the slider volume, or open and close windows (KDE4). If you find this annoying try modinfo snd_MY_MODULE, and look for a module option that adjusts or disables this feature.

Example: to disable the power saving mode and solve cracking sound trough speakers issue, using snd_hda_intel add in /etc/modprobe.d/modprobe.conf

options snd_hda_intel power_save=0

or

options snd_hda_intel power_save=0 power_save_controller=N

You can also try it with modprobe snd_hda_intel power_save=0 before.

You may also have to unmute the 'Line' ALSA channel for this to work. Any value will do (other than '0' or something too high).

Example: on an onboard VIA VT1708S (using the snd_hda_intel module) these cracks occured even though 'power_save' was set to 0. Unmuting the 'Line' channel and setting a value of '1' solved the issue.

Source: https://www.kernel.org/doc/Documentation/sound/alsa/powersave.txt

If you use a laptop, pm-utils will change power_save back to 1 when you go onto battery power even if you disable power saving in /etc/modprobe.d. Disable this for pm-utils by disabling the script that makes the change (see Disabling a hook for more information):

# touch /etc/pm/power.d/intel-audio-powersave

S/PDIF Output Does Not Work

If the optical/coaxial digital output of your motherboard/sound card is not working or stopped working, and have already enabled and unmuted it in alsamixer, try running

# iecset audio on

as root.

You can also put this command in rc.local as it sometimes it may stop working after a reboot.

HDMI Output Does Not Work

If the HDMI output of your motherboard/sound card is not working or stopped working, and have already enabled and unmuted it in alsamixer, try the following.

Query for Playback Devices:

 $ aplay -l
 **** List of PLAYBACK Hardware Devices ****
 card 0: NVidia [HDA NVidia], device 0: ALC1200 Analog [ALC1200 Analog]
   Subdevices: 1/1
   Subdevice #0: subdevice #0
 card 0: NVidia [HDA NVidia], device 1: ALC1200 Digital [ALC1200 Digital]
   Subdevices: 1/1
   Subdevice #0: subdevice #0
 card 0: NVidia [HDA NVidia], device 3: NVIDIA HDMI [NVIDIA HDMI]
   Subdevices: 0/1
   Subdevice #0: subdevice #0

Now that we have the info for the HDMI device, try a test, In the example below, 0 is the card number and 3 is the device number.

 $ aplay -D plughw:0,3 /usr/share/sounds/alsa/Front_Center.wav

If aplay does not output any errors, but still no sound is heared, "reboot" the receiver, monitor or tv set. Since the HDMI interface executes a handshake on connection, it might have noticed before that there was no audio stream embedded, and disabled audio decoding. In particular, if you are using a standalone window manager (don’t know about Gnome or KDE), you may need to have some sound playing while plugging in the HDMI cable.

Note: If you are using an ATI card and linux 3.0, check your kernel/grub settings, you most likely need to use the 'radeon.audio=1' boot parameter, or alternatively set up the option in your modprobe configuration files. It's important to note that before trying, is to check the Radeon Feature Matrix to see if it's actually supported. At the moment this is written (march 29th, 2013), some chipsets of the S. Islands (namely HD7750 through HD7970) doesn't (still) support HDMI Audio.

If the test is successful, create or edit your ~/.asoundrc file to set HDMI as the default audio device.

~/.asoundrc
pcm.!default {
  type hw
  card 0
  device 3
}

Or you above config does not work try:

~/.asoundrc
defaults.pcm.card 0
defaults.pcm.device 3
defaults.ctl.card 0

HDMI Multi-channel PCM output does not work (Intel)

As of Linux 3.1 multi-channel PCM output through HDMI with a Intel card (Intel Eaglelake, IbexPeak/Ironlake,SandyBridge/CougarPoint and IvyBridge/PantherPoint) is not yet supported. Support for it has been recently added and expected to be available in Linux 3.2. To make it work in Linux 3.1 you need to apply the following patches:

HP TX2500

Add these 2 lines into /etc/modprobe.d/modprobe.conf:

options snd-cmipci mpu_port=0x330 fm_port=0x388
options snd-hda-intel index=0 model=toshiba position_fix=1
options snd-hda-intel model=hp (works for tx2000cto)

Skipping Sound When Playing MP3

If you have sound skipping when playing MP3 files and you have more then 2 speakers attached to your computer (i.e. > 2 speaker system), run alsamixer and disable the channels for the speakers that you DO NOT have (i.e. do not enable the sound for the center speaker if you do not have a center speaker.

Using a USB Headset and External USB Sound Cards

If you are using a USB headset with ALSA you can try using asoundconfAUR (currently only available from the AUR) to set the headset as the primary sound output. Before running make sure you have usb audio module enabled (modprobe snd-usb-audio).

# asoundconf is-active
# asoundconf list
# asoundconf set-default-card <chosen soundcard>

Crackling sound with USB devices

If you experience crackling sound on USB devices, you can try tuning the snd-usb-audio for minimal latency.

Add this to your /etc/modprobe.d/modprobe.conf:

options snd-usb-audio nrpacks=1

source: http://alsa.opensrc.org/Usb-audio#Tuning_USB_devices_for_minimal_latencies

Hot-plugging a USB Sound Card

In order to automatically make a USB Sound Card the primary output device, when the card is plugged in, you can use the following udev rules (e.g. add the following two lines to /etc/udev/rules.d/00-local.rules and reboot).

KERNEL=="pcmC[D0-9cp]*", ACTION=="add", PROGRAM="/bin/sh -c 'K=%k; K=$${K#pcmC}; K=$${K%%D*}; echo defaults.ctl.card $$K > /etc/asound.conf; echo defaults.pcm.card $$K >>/etc/asound.conf'"
KERNEL=="pcmC[D0-9cp]*", ACTION=="remove", PROGRAM="/bin/sh -c 'echo defaults.ctl.card 0 > /etc/asound.conf; echo defaults.pcm.card 0 >>/etc/asound.conf'"

Error 'Unknown hardware' Appears After a Kernel Update

The following messages may be displayed during the start-up ALSA after the kernel update:

Unknown hardware "foo" "bar" ...
Hardware is initialized using a guess method
/usr/sbin/alsactl: set_control:nnnn:failed to obtain info for control #mm (No such file or directory)

or

Found hardware: "HDA-Intel" "VIA VT1705" "HDA:11064397,18490397,00100000" "0x1849" "0x0397"
Hardware is initialized using a generic method
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #1 (No such file or directory)
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #2 (No such file or directory)
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #25 (No such file or directory)
/usr/sbin/alsactl: set_control:1328: failed to obtain info for control #26 (No such file or directory)

Simply store ALSA mixer settings again (as root):

# alsactl -f /var/lib/alsa/asound.state store

It may be necessary configure ALSA again with alsamixer

HDA Analyzer

If the mappings to your audio pins(plugs) do not correspond but ALSA works fine, you could try HDA Analyzer -- a pyGTK2 GUI for HD-audio control can be found at the ALSA wiki. Try tweaking the Widget Control section of the PIN nodes, to make microphones IN and headphone jacks OUT. Referring to the Config Defaults heading is a good idea.

NOTE: the script is done by such way that it is incompatible with python3 (which is now shipped with ArchLinux) but tries to use it. The workaround is: open "run.py", find all occurences of "python" (2 occurences - one on the first line, and the second on the last line) and replace them all by "python2".

NOTE2: the script requires root acces, but running it via su/sudo is bogus. Run it via kdesu or gksu.

ALSA with SDL

If you get no sound via SDL and ALSA cannot be chosen from the application. Try setting the environmental variable SDL_AUDIODRIVER to alsa.

# export SDL_AUDIODRIVER=alsa

Low Sound Workaround

If you are facing low sound even after maxing out your speakers/headphones, you can give the softvol plugin a try. Add the following to /etc/asound.conf.

pcm.!default {
      type plug
      slave.pcm "softvol"
  }

pcm.softvol {
    type softvol
    slave {
        pcm "dmix"
    }
    control {
        name "Pre-Amp"
        card 0
    }
    min_dB -5.0
    max_dB 20.0
    resolution 6
}
Note: You will probably have to restart the computer, as restarting the alsa daemon did not load the new configuration for me. Also, if the configuration does not work even after restarting, try changing plug with hw in the above configuration.

After the changes are loaded successfully, you will see a Pre-Amp section in alsamixer. You can adjust the levels there.

Note: Setting a high value for Pre-Amp can cause sound distortion, so adjust it according to the level that suits you.

Popping sound after resuming from suspension

You might hear a popping sound after resuming the computer from suspension. This can be fixed by editing /etc/pm/sleep.d/90alsa and removing the line that says aplay -d 1 /dev/zero

设置文件范例

参见:Advanced Linux Sound Architecture/Example Configurations

相关阅读