参考自:asoundrc文件
    asoundrc配置文件简单介绍
    asound.conf 插件讲解

文章目录

  • 1、Asoundrc
    • 1.1、什么是asoundrc文件?
    • 1.2、为什么需要asoundrc文件?
    • 1.3、asoundrc文件什么时候被加载的?
  • 2、Plugin(插件)
    • 2.1、Plugin: hw
    • 2.2、Slave 定义
    • 2.3、Plugin: Rate
    • 2.4、Plugin: Route & Volume
    • 2.5、Automatic conversion plugin
    • 2.6、Plugin: dmix
    • 2.7、Plugin: dsnoop
    • 2.8、实现虚拟多通道设备
  • 3、附录

做alsa的基本都能体会到alsa-lib的复杂与强大,而alsa-lib的强大正是从asound.conf与.asoundrc等配置文件体现出来的。alsa驱动开发只是一个方面,而真正想随心所欲的配置音频设备,asound.conf与.asoundrc的掌握是必不可少的。所幸,这方面的资料还是比较丰富,所需了解的知识点基本都能从官网上找到文档甚至example。

Alsa-lib层,为不同的驱动提供统一的接口alsa API,简化了开发人员对于驱动层的调用开发。接口定义地址:

https://www.alsa-project.org/alsa-doc/alsa-lib/

关于asound.conf的配置,可以参考官网解释:

https://www.alsa-project.org/main/index.php/Asoundrc

详细的插件讲解,官网网址:

https://www.alsa-project.org/alsa-doc/alsa-lib/pcm_plugins.html

1、Asoundrc


1.1、什么是asoundrc文件?

这里asoundrc文件实际上包含两个文件: “.asoundrc”“asound.conf”

.asoundrc 是一个隐藏文件,是针对个人用户的配置文件,存在于用户主目录下。asound.conf 则是全局的配置文件。

1.2、为什么需要asoundrc文件?

事实上,asoundrc(.asoundrcasound.conf)文件对ALSA来说,不是必须的配置文件,也就是说,无论它存不存在,都不影响ALSA的正常工作。那么既然这样,它存在的意义是什么呢?使用asoundrc文件可以实现更多对card/device的高级控制。例如在alsa-lib层进行通道路由和采样率转换,把多块声卡合为一块或者访问多通道卡的多个I/O。然而通过ALSA-lib库这两个文件能够为应用程序提供额外的功能,如通道路由,样本rate转换。

1.3、asoundrc文件什么时候被加载的?

alsa-lib 库将 /usr/share/alsa/alsa.conf 配置文件作为主要的入口点。其他文件在哪是否需要都由这个文件决定。 alsa.conf中自动加载系统全局自定义设置文件 /etc/asound.conf 和用户自定义配置文件 ~/.asoundrc


这个文件在调用 snd_pcm_open 这个api函数时,会被加载同时解析。

2、Plugin(插件)


在ALSA中,PCM插件扩展了PCM设备的功能和特性。插件可以自动处理诸如:命名设备、采样率转换、通道间的采样复制、写入文件、为多个输入/输出连接声卡/设备(不同步采样)、使用多通道声卡/设备以及其他可能留给你的开发任务。

2.1、Plugin: hw

此插件直接与ALSA内核驱动程序通信,它是一种没有任何转换的原始通信。

pcm.name {type hw         # Kernel PCMcard INT/STR        # Card name (string) or number (integer)[device INT]        # Device number (default 0)[subdevice INT]     # Subdevice number (default -1: first available)[sync_ptr_ioctl BOOL]   # Use SYNC_PTR ioctl rather than the direct mmap access for control structures[nonblock BOOL]     # Force non-blocking open mode[format STR]        # Restrict only to the given format[channels INT]      # Restrict only to the given channels[rate INT]      # Restrict only to the given rate[chmap MAP]     # Override channel maps; MAP is a string array
}

nonblock选项指定设备是否以非阻塞方式打开。注意此选项并不会更改读/写访问的阻塞行为。只影响打开设备时的阻塞行为。如果想保持与旧ALSA版本的兼容性,请关闭此选项。

下面是一个例子:

用vi命令在你的home目录或root目录下创建一个asoundrc配置文件:vi /home/xxx/.asoundrc (或者在打开 /etc/asound.conf 文件)

将下面的代码拷贝到该文件中并保存:

pcm.!default {type hwcard 0
}ctl.!default {type hw           card 0
}

名字为default的声卡,指向card0,也就是hw:0,0,测试命令:aplay -D default test.wav

注意:ALSA库定义的default 设备是 hw:0,0。 由 !default 指定的内容将替换ALSA lib API定义的。

hw后面跟的数字是声卡号和设备号,可以用如下命令查看硬件支持声卡数:

cat /proc/asound/cards 或者 ls /dev/snd (注:pcm0c (capture), pcm0p (playback))

2.2、Slave 定义

在ALSA中,PCM插件扩展了PCM设备的功能和特性。插件可以自动处理诸如:命名设备、采样率转换、通道间的采样复制、写入文件、为多个输入/输出连接声卡/设备(不同步采样)、使用多通道声卡/设备等工作。要使用它们,开发者需要创建一个虚拟从属设备(slave device)。

pcm_slave.NAME {pcm STR     # PCM name# orpcm { }     # PCM definitionformat STR  # Format or "unchanged"channels INT    # Count of channels or "unchanged" stringrate INT    # Rate in Hz or "unchanged" stringperiod_time INT # Period time in us or "unchanged" stringbuffer_time INT # Buffer time in us or "unchanged" string
}

一个最简单的slave用例:

pcm_slave.sltest {pcm "hw:1,0"
}

上面的实例定义了一个没有任何参数的slave,实际上也就仅仅是给声卡设备定义了一别名而已。对于需要参数的情况,需要注意一点 “PCM types”的参数必须定义在 slave-definition-block ,例如下面这两种方式定义是一致的:

pcm_slave.sl2 {pcm "hw:1,0"rate 48000
}pcm.rate_convert {type rateslave sl2
}

或者

pcm.rate_convert {type rateslave {pcm "hw:1,0"rate 48000}
}

在slave设备配置中加入采样率转换。注意,这两种方式中参数定义的位置。

调用这个设备播放音频的例子: aplay -D rate_convert test.wav ,此时是以48kHz的采样率对test.wav进行播放。

上面的用例用到了 Plugin: Rate

2.3、Plugin: Rate

这个插件可以转换采样率,但是输入输出格式必须为线型。

pcm.name {type rate               # Rate PCMslave STR               # Slave name# orslave {                 # Slave definitionpcm STR         # Slave PCM name# orpcm { }         # Slave PCM definitionrate INT        # Slave rate[format STR]    # Slave format}converter STR           # optional# orconverter [ STR1 STR2 ... ] # optional# Converter type, default is taken from# defaults.pcm.rate_converter# orconverter {     # optionalname STR    # Convertor typexxx yyy     # optional convertor-specific configuration}
}

2.4、Plugin: Route & Volume

此插件可以转换声道数和应用声音。转换声音这个我没有很明白,官网也没有给出具体的用例。

pcm.name {type route              # Route & Volume conversion PCMslave STR               # Slave name# orslave {                 # Slave definitionpcm STR         # Slave PCM name# orpcm { }         # Slave PCM definition[format STR]    # Slave format[channels INT]  # Slave channels}ttable {                # Transfer table (bi-dimensional compound of cchannels * schannels numbers)CCHANNEL {SCHANNEL REAL   # route value (0.0 - 1.0)}}
}

2.5、Automatic conversion plugin

这个插件可以转换channels、rate和format,可以理解为是rate和Route & Volume的集合体。

pcm.name {type plug              # Format adjusted PCMslave STR               # Slave name (see pcm_slave)# orslave {                 # Slave definitionpcm STR         # Slave PCM name# orpcm { }         # Slave PCM definition[format STR]    # Slave format (default nearest) or "unchanged"[channels INT]  # Slave channels (default nearest) or "unchanged"[rate INT]      # Slave rate (default nearest) or "unchanged"}route_policy STR       # route policy for automatic ttable generation# STR can be 'default', 'average', 'copy', 'duplicate'# average: result is average of input channels# copy: only first channels are copied to destination# duplicate: duplicate first set of channels# default: copy policy, except for mono capture - sumttable {                # Transfer table (bidimensional compound of cchannels * schannels numbers)CCHANNEL {SCHANNEL REAL     # route value (0.0 ... 1.0)}}rate_converter STR     # type of rate converter# orrate_converter [ STR1 STR2 ... ]# type of rate converter# default value is taken from defaults.pcm.rate_converter

plug插件使用例子:

pcm_slave.sl3 {pcm "hw:1,0"format S16_LEchannels 1rate 16000
}pcm.complex_convert {type plugslave sl3
}

用aplay打开这个pcm设备:aplay -vD complex_convert test.wav

当播放test.wav的时候,就会转换为S16_LE,一个通道和16kHz的采样率进行播放。如果只是使用 -v 选项,你就能看到测试文件的原始设置。 aplay -v test.wav

2.6、Plugin: dmix

软件混音功能是使用一个声卡设备来同时播放多个音频源,或者多个应用程序同时使用这个设备来播放音频数据。

ALSA有个叫做dmix(direct mixing直接混合)的本地插件,通过使用指定的语法,它可以让实现软件混音变得非常简单,而用户也不用再额外安装其他的软件。但是需要注意的一点:The resolution for 32-bit mixing is only 24-bit. The low significant byte is filled with zeros. The extra 8 bits are used for the saturation.

注意,dmix插件不是基于客户机/服务器架构,而是直接写入声卡的dma缓冲区。一次可以运行的实例数量没有限制。

pcm.name {type dmix       # Direct mixipc_key INT     # unique IPC keyipc_key_add_uid BOOL    # add current uid to unique IPC keyipc_perm INT        # IPC permissions (octal, default 0600)hw_ptr_alignment STR    # Slave application and hw pointer alignment type# STR can be one of the below strings :# no# roundup# rounddown# auto (default)slave STR# orslave {         # Slave definitionpcm STR     # slave PCM name# orpcm { }     # slave PCM definitionformat STR  # format definitionrate INT    # rate definitionchannels INTperiod_time INT # in usec# orperiod_size INT # in bytesbuffer_time INT # in usec# orbuffer_size INT # in bytesperiods INT # when buffer_size or buffer_time is not specified}bindings {      # note: this is client independent!!!N INT       # maps slave channel to client channel N}slowptr BOOL        # slow but more precise pointer updates
}
  • ipc_key 必须是整数形式的唯一ipc key。对于每个不同的dmix定义,这个数字必须是唯一的,因为共享内存是用这个key创建的。当ipc_key_add_uid设置为true时,uid值将添加到ipc_key设置中。这样可以避免同一个IPC密钥与不同用户同时发生冲突。

  • hw_ptr_alignment 这个配置默认是auto,其他配置可以去上面贴的第三个网址中了解。

注意,dmix插件本身只支持单个配置。也就是说,它只支持固定rate(默认48000)、format(s16)、channels(2)和period_time (125000)。对于使用其它配置,开发者必须在从PCM定义中明确设置该值。

(1)这个插件可以与default插件结合使用:

pcm.!default {type plugslave.pcm "dmixer"
}pcm.dmixer  {type dmixipc_key 1024  # any unique valueipc_key_add_uid trueslave {pcm "hw:1,0"period_time 0period_size 1024buffer_size 4096format S32_LErate 44100}bindings {0 01 1}
}ctl.dmixer {type hwcard 0
}

测试例子:aplay -f cd -D default test.wav (可以在多个终端上面同时使用该指令测试)。

(2)该插件也可以单独使用:

pcm.dmix_44 {type dmixipc_key 321456  # any unique valueipc_key_add_uid trueslave {pcm "hw:0"format S32_LErate 44100}
}

可以利用这个配置的声卡播放一个48k 的音频:aplay -Dplug:dmix_44 foo_48k.wav

对于使用OSS仿真设备的dmix插件,必须将period和buffer sizes设置为2的幂次方。例如:

pcm.dmixoss {type dmixipc_key 321456  # any unique valueipc_key_add_uid trueslave {pcm "hw:0"period_time 0period_size 1024  # must be power of 2buffer_size 8192  # ditto}
}

period_time必须为0,对于带有多通道IO的声卡,添加绑定也非常有用。绑定的用例:

pcm.dmixoss {...bindings {0 0   # map from 0 to 01 1   # map from 1 to 1}
}

2.7、Plugin: dsnoop

这个插件和dmix执行相反的操作,dmix是将多个播放的音频混合输出(即混音多个输出音频流),而dsnoop插件是将多外部音频源进行合成存储到Pcm缓冲区(即混合多个输入音频流)。以下参数的含义与dmix插件几乎相同。

pcm.name {type dsnoop     # Direct snoopipc_key INT     # unique IPC keyipc_key_add_uid BOOL    # add current uid to unique IPC keyipc_perm INT        # IPC permissions (octal, default 0600)slave STR# orslave {         # Slave definitionpcm STR     # slave PCM name# orpcm { }     # slave PCM definitionformat STR  # format definitionrate INT    # rate definitionchannels INTperiod_time INT # in usec# orperiod_size INT # in bytesbuffer_time INT # in usec# orbuffer_size INT # in bytesperiods INT # when buffer_size or buffer_time is not specified}bindings {      # note: this is client independent!!!N INT       # maps slave channel to client channel N}slowptr BOOL        # slow but more precise pointer updates
}

例子如下:

pcm.mixin {type dsnoopipc_key 5978293    # must be unique for all dmix plugins!!!!ipc_key_add_uid yesslave {pcm "hw:0,0"channels 2period_size 1024buffer_size 4096rate 44100periods 0 period_time 0}bindings {0 00 1}
}

2.8、实现虚拟多通道设备

如果你想将两个或更多的alsa设备连接在一起,来模拟一个虚拟的多通道设备使用。

# 联合两个两通道的声卡设备来创建一个虚拟的4个声道的设备:pcm.multi {type multi;slaves.a.pcm "hw:0,0";  #第一块声卡设备slaves.a.channels 2;    #两个通道slaves.b.pcm "hw:1,0";  #第二块声卡设备slaves.b.channels 2;bindings.0.slave a;bindings.0.channel 0;bindings.1.slave a;bindings.1.channel 1;bindings.2.slave b;bindings.2.channel 0;bindings.3.slave b;bindings.3.channel 1;
}# JACK will be unhappy if there is no mixer to talk to, so we set
# this to card 0. This could be any device but 0 is easy. ctl.multi {type hw;card 0;
}# This creates a 4 channel interleaved pcm stream based on
# the multi device. JACK will work with this one.pcm.ttable {type route;slave.pcm "multi";slave.channels 4;ttable.0.0 1;ttable.1.1 1;ttable.2.2 1;ttable.3.3 1;
}
# see above.
ctl.ttable {type hw;card 0;
}
arecord -f S16_LE -r 44100 -c 4 -D multi | aplay -f S16_LE -r 44100 -c 4 -D multi

3、附录

# R329
# audiocodec
# snddaudio0
# snddaudio1
# snddaudio2
# snddmic
# sndspdifctl.!default {type hwcard audiocodec
}pcm.!default {type asymplayback.pcm "Playback"capture.pcm "Capture6Mic2Aec"
}pcm.Playback {type plugslave.pcm {type softvolslave.pcm PlaybackDmixcontrol {name "Soft Volume Master"card audiocodec}min_dB -51.0max_dB 0.0resolution 256}slave.rate 48000slave.format S16_LE
}pcm.PlaybackDmix {type plugslave.pcm {type dmixipc_key 1111ipc_perm 0666slave {pcm "hw:audiocodec"format S16_LErate 48000channels 2period_size 1024periods 4}}
}pcm.PlaybackSpeaker {type plugslave.pcm "Playback"
}pcm.Capture {type hwcard audiocodec
}pcm.CaptureDsnoop {type plugslave.pcm {type dsnoopipc_key 1111ipc_perm 0666slave {pcm "hw:audiocodec,0"rate 16000channels 6period_size 1024periods 4}}
}pcm.CaptureMic {type plugslave {pcm "CaptureDsnoop"channels 4}
}pcm.Capture6Mic2Aec {type plugslave.pcm {type multislaves {a { pcm "Capture6MicDsnoop" channels 6 }b { pcm "Capture2AecDsnoop" channels 2 }}bindings {0 { slave a channel 0 }1 { slave a channel 1 }2 { slave a channel 2 }3 { slave a channel 3 }4 { slave a channel 4 }5 { slave a channel 5 }6 { slave b channel 0 }7 { slave b channel 1 }}}ttable.0.0 1ttable.1.1 1ttable.2.2 1ttable.3.3 1ttable.4.4 1ttable.5.5 1ttable.6.6 1ttable.7.7 1slave.rate 16000slave.format S16_LE
}pcm.Capture6MicDsnoop {type plugslave.pcm {type dsnoopipc_key 2222ipc_perm 0666slave {pcm "hw:audiocodec,0"rate 16000channels 6period_size 1024periods 4}}
}pcm.Capture2AecDsnoop {type plugslave.pcm {type dsnoopipc_key 2223ipc_perm 0666slave {pcm "hw:sndac10720036"rate 16000channels 2period_size 1024periods 4}}
}pcm.CaptureDmic {type hwcard "snddmic"
}pcm.PlaybackSpdif {type hwcard "sndspdif"
}pcm.CaptureSpdif {type hwcard "sndspdif"
}pcm.PlaybackDaudioLoopback1 {type hooksslave.pcm "hw:snddaudio1"hooks.0 {type ctl_elemshook_args [{name "sunxi daudio loopback debug"preserve truelock trueoptional truevalue 1}]}
}pcm.LoopbackDaudio1 {type asymplayback.pcm "PlaybackDaudioLoopback1"capture.pcm "hw:snddaudio1"
}pcm.Daudio2 {type asymplayback.pcm "hw:snddaudio2"capture.pcm "hw:snddaudio2"
}pcm.PlaybackSpdifLoopback {type hooksslave.pcm "hw:sndspdif"hooks.0 {type ctl_elemshook_args [{name "sunxi spdif loopback debug"preserve truelock trueoptional truevalue 1}]}
}pcm.LoopbackSpdif {type asymplayback.pcm "PlaybackSpdifLoopback"capture.pcm "CaptureSpdif"
}

ALSA 配置文件简介相关推荐

  1. Linux ALSA 之一:ALSA 架构简介

    一.概述 ALSA是 Advanced Linux Sound Architecture 的缩写,目前已经成为了linux的主流音频体系结构. 在 Linux 内核设备驱动层,ALSA 提供了 als ...

  2. Linux ALSA声卡驱动之一:ALSA架构简介和ASOC架构简介

    ALSA声卡驱动: 1.Linux ALSA声卡驱动之一:ALSA架构简介和ASOC架构简介 2.Linux ALSA声卡驱动之二:Platform 3. Linux ALSA声卡驱动之三:Platf ...

  3. Linux学习笔记——Apache服务器配置文件简介

    Apache服务器配置文件简介 Apache的配置文件是包含若干指令的纯文本文件,其文件名为httpd.conf.在Apache启动时,会自动读取配置文件中的内容,配置Apache服务器的运行.配置文 ...

  4. Linux ALSA驱动框架(一)--ALSA架构简介--声卡的创建

    (1)ALSA简介 (1) Native ALSA Application:tinyplay/tinycap/tinymix,这些用户程序直接调用 alsa 用户库接口来实现放音.录音.控制 ALSA ...

  5. Flutter入门二——项目结构及配置文件简介

    前言 环境搭建完成之后,我们来看看Flutter:New Project后生成的项目结构. 具体环境搭建可以参考:w7上使用VSCode配置Flutter开发环境 项目结构 pubspec.yaml配 ...

  6. Java内存溢出[转]和mysql配置文件简介[原创]

    1. PermGen space的全称是Permanent Generation space,是指内存的永久保存区域OutOfMemoryError: PermGen space从表面上看就是内存益出 ...

  7. Spring配置文件简介

    这么多的人,那么多的思想. 当我们为不同的客户实施软件时,有时我们需要处理同一项目的各种需求. 例如,客户A需要SAML身份验证,客户B需要LDAP身份验证. 借助Spring Profiles(可从 ...

  8. linux alsa声卡命令,Linux ALSA声卡驱动之一:ALSA架构简介

    一.  概述 ALSA是Advanced Linux Sound Architecture 的缩写,目前已经成为了linux的主流音频体系结构,想了解更多的关于ALSA的这一开源项目的信息和知识,请查 ...

  9. Linux ALSA声卡驱动之一:ALSA架构简介

    一.  概述 ALSA是Advanced Linux Sound Architecture 的缩写,目前已经成为了linux的主流音频体系结构,想了解更多的关于ALSA的这一开源项目的信息和知识,请查 ...

最新文章

  1. 关于java设计模式笔记
  2. opencv摄像头用法
  3. [Contest20171005]Maze
  4. 用户空间访问I2C设备驱动
  5. Flex +WebService
  6. nlp中的经典模型(三)
  7. Visual Studio 2017 正式版各版本比较:企业版最强大
  8. 迪斯尼首次取代苹果公司 成“美国人最亲密品牌”
  9. B站、字节跳动等上榜2021福布斯中国最佳雇主榜;中兴发布“5G 智能T恤”:可监测呼吸;Linux 5.13 发布|极客头条...
  10. 由一段JS代码引发的思考
  11. 190314每日一句
  12. Python画哆啦A梦
  13. 近红外光谱定性定量分析模型 Matlab
  14. web前端期末大作业:旅游网页主题网站设计——桂林旅游网站的设计 (7页)HTML+CSS+JavaScript web网页设计与开发 静态网页的制作 web期末作业设计网页 web结课作业的源
  15. 研究表明:手机移动数据可用于预测犯罪行为
  16. Win11右键菜单反应慢有延迟解决方法分享
  17. 万维网、因特网、互联网
  18. [FreeBSD] pf+altq实战[转]
  19. 经典差动放大器应用电路详解
  20. 宝藏又小众的东方行走rpg制作大师素材网站分享

热门文章

  1. ssm基于Html+css的音乐网站的设计与实现毕业设计源码181627
  2. 专家详解“手机窃听”:夸大宣传并不神奇
  3. 强强联合 加速科技“牵手”清华大学达成深度战略合作
  4. 正确的使用HTTP代理
  5. 视觉3D感知(二):单目3D物体检测
  6. 那些年我们用过的final和static
  7. command patten 读书笔记
  8. 抽屉原理与Simhash
  9. facebook有哪些信息_如何让人们不知道您在Facebook上阅读了他们的信息
  10. Vista删除EISA配置分区