alsa 配置文件asound.conf

alsa 配置文件asound.conf

2018年09月04日 10:52:07 CNccion 阅读数:329更多

个人分类: tools

1.介绍

     笔者在博客《Alsa调试下篇:应用篇》中一文中提到过asound.conf这个配置文件,在本文中,笔者将为你详细介绍这个文件。在《Alsa调试上篇:编译篇》中通过交叉编译我们可以得到alsa-lib的一些文件,在usr/local/shar/alsa/中有一个文件叫---alsa.conf

alsa官网解释:https://www.alsa-project.org/main/index.php/Asoundrc

这个文件在调用snd_pcm_open这个api函数时,会被加载同时解析。接下来看看官网给的解释:

Why asoundrc?

What is it good for, why do I want one?

Neither the user-side .asoundrc nor the asound.conf configuration files are required for ALSA to work properly. Most applications will work without them. These files are used to allow extra functionality, such as routing and sample-rate conversion, through the alsa-lib layer. The actual reason that most applications will work without these user-side custom config files is that usually a default install of alsa-lib provides a sufficiently capable setup consisting of hierarchical config files (which always make use of the standard .asoundrc format syntax as well), which are specifically capable of supporting certain soundcard brands each. Thus, let's start with a quick overview of how ALSA config file framework evaluation is composed, globally:

Global view of ALSA config file framework, executive summary

The alsa-lib package (at least on Debian libasound2-data 1.0.27) provides the /usr/share/alsa/alsa.conf file as the main entry point. That file is responsible for including the full list of potential .asoundrc-format-type files on the system. It contains a reference to the ALSA "DATADIR" (Debian: /usr/share/alsa/). It continues by loading the DATADIR's cards/aliases.conf file: that one defines translation mappings from the kernel driver's sound card name (as listed at /proc/asound/cards, or aplay -Ll) to a "more detailed" description string. That "more detailed name" of a sound card then gets used to lookup a corresponding card-specific config file at DATADIR/cards/CARD.conf. And THAT card-specific file then attempts to provide a maximally elegant sound setup for its specific card brand, by compensating for various limitations of cards (e.g. use dmix to combat single-stream playback only, or stereo downmix to lessen a mono-output-only restriction). Finally (to support those cases where the standard setup of a soundcard is deficient/lacking, or custom plugin setup is desired), alsa.conf loads a system-global custom settings file /etc/asound.conf and a per-user custom settings file ~/.asoundrc.

So, the objective should be to achieve having the common alsa-lib configuration file framework enhanced by default in the best possible manner for each specific soundcard brand, to avoid the need of creating manually customized config files in all standard cases.

With this global overview done and cared for, let's have a look at the actual configuration format of alsa-lib files.

上面的主要说明点:

  1. .asoundrc和asound.conf的引入提供用户定制化
  2. /usr/share/alsa/alsa.conf为alsa-api的主要入口点

2.asound.conf的作用

   asound.conf允许对声卡或者设备进行更高级的控制,提供访问alsa-lib中的pcm插件方法,允许你做更多的复杂的控制,比如可以把声卡组合成一个或者多声卡访问多个I/O。

3.Default plugin

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

关键字default定义在alsa-lib中,将会访问hw:0,0声卡。
aplay -D default test.wav  (== aplay -Dhw:0,0 test.wav)

4.Plugin

     在alsa中,pcm插件扩展pcm设备的功能和特性。这些插件自动处理诸如命令设备,采样率转换,通道之间的采样复制,写入文件,为多个输入/输出(未采样同步)加入声卡/设备,使用多通道声卡/设备以及其他可能留给你的开发任务,详细的plugin和选项的完整列表:http://www.alsa-project.org/alsa-doc/alsa-lib/pcm_plugins.html

4.1rate_convert

 

pcm.rate_convert {
    type rate
    slave {
        pcm
        rate 48000
    }
}

调用这个接口,比如之前44.1k的采样频率将会转换为48khz。

 

更复杂的用法:

type plug                 # Format adjusted PCM
slave STR               # Slave name (see pcm_slave)
# or
slave {                 # Slave definition
    pcm STR         # Slave PCM name
    # or
    pcm { }         # 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 - sum
ttable {               # Transfer table (bidimensional compound of 
                       # cchannels * schannels numbers)
    CCHANNEL {
        SCHANNEL REAL     # route value (0.0 ... 1.0)
    }
}
pcm_slave.sl3 {
    pcm "hw:1,0"
    format S16_LE
    channels 1
    rate 16000
}
 
pcm.complex_convert {
    type plug
    slave sl3
}

4.2dmix

pcm.!default {
    type plug
    slave.pcm "dmixer"
}
 
pcm.dmixer  {
     type dmix
     ipc_key 1024
     slave {
        pcm "hw:1,0"
        period_time 0
        period_size 1024
        buffer_size 4096
        rate 44100
    }
    bindings {
        0 0
        1 1
    }
}
 
ctl.dmixer {
    type hw
    card 0
}

aplay -f cd -D default test.wav

4.3 dsnoop

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

4.4 jack plugin

pcm.jackplug {
    type plug
    slave { pcm "jack" }
}
 
pcm.jack {
    type jack
    playback_ports {
        0 alsa_pcm:playback_1
        1 alsa_pcm:playback_2
    }
    capture_ports {
        0 alsa_pcm:capture_1
        1 alsa_pcm:capture_2
    }
}

aplay -Djackplug somefile
arecord -Djackplug somefile

4.5Virtual multi channel devices

  如果你想链接两个或者多个alsa设备,以便你有一个虚拟的多通道设备。

# create a virtual four-channel device with two sound devices:
# This is in fact two interleaved stereo streams in
# different memory locations, so JACK will complain that it
# cannot get mmap-based access. see below.
 
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
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值