asoundrc配置文件简单介绍

Asoundrc

为什么关注asoundrc配置文件?

它有什么作用,为什么需要这个文件呢?

.asoundrc文件和asound.conf两个配置文件并不是ALSA正常工作所必须的。大多少的应用程序没有配置他们也能正常工作。 然而通过ALSA-lib库这两个文件能够为应用程序提供而外的功能,如通道路由,样本rate转换。 

ALSA配置文件框架概况

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

asoundrc文件在哪里呢?

asoundrc文件通常是安装在用户的主目录

$HOME/.asoundrc

由alsa.conf调用

/usr/share/alsa/alsa.conf

还可以写一个系统范围的配置文件:

/etc/asound.conf

当ALSA应用程序启动时,这两个配置文件就被读取。

The default plugin

用vi命令在你的home目录或root目录下创建一个asoundrc配置文件:

vi /home/xxx/.asoundrc

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

pcm.!default {
	type hw
	card 0
}

ctl.!default {
	type hw           
	card 0
}


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

现在可以用default设备测试:

aplay -D default test.wav

PCM设备的命名规则:

hw:后面的数字代表声卡号和设备号。  

hw:0,0表示第一块声卡的第一个设备,hw:1,0表示第二块声卡的第一个设备,hw:2,1表示第三块设备的第二个设备。

插件:

Q: 什么是插件?

A: 在 ALSA中, PCM插件能够扩展PCM设备的功能和特性、。插件会自动的做这些工作像:设备命名、样本采样率的转换、通道间样本的拷贝、, 写入文件、为实现多输入/输出而将多个设备join、, 使用多通道声卡、还有其他功能值得去探索。要使用这些插件需要创建一个虚拟的slave设备。 

以下是简单的slave的定义:

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

这个slave定义没有定义任何参数,紧紧是声卡的一个别名。个人理解slave是奴隶的意思,是干苦力的,因此slave的定义应该是用来干pcm里面的“累活”的。下面是利用slave定义一个rate_convert,下面的例子中,s12是rate_convert这个pcm设备的“奴隶”,听rate_convert使唤用来干活的。

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

pcm.rate_convert {
	type rate
	slave sl2
}

调用这个设备播放音频的例子:

aplay -D rate_convert test.wav

上面的例子可以用下面更简洁的嵌套方式来定义:

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

一个更复杂的用来转换的工具是pcm type plug。plug插件的语法定义如下:

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)
	}
}

plug插件使用例子:

pcm_slave.sl3 {
	pcm "hw:1,0"
	format S16_LE
	channels 1
	rate 16000
}

pcm.complex_convert {
	type plug
	slave sl3
}
用aplay打开这个pcm设备:aplay -vD complex_convert test.wav

软件混音

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

dmix插件

ALSA有个 dmix (direct mixing) 插件,语法简单易于使用。这个插件可以与default插件结合使用:

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

dsnoop插件

这个插件和dmix执行相反的操作,dmix是将多个播放的音频混合输出,而dsnoop插件是将多外部音频源进行合成存储到Pcm缓冲区,例子如下:

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
	}
}

实现虚拟多通道设备

如果你想将两个或更多的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;
}

Referenced applications

  • aRTsd - the aRTs sound server was the basis of desktop sound for KDE 3.
  • ESD - the Enlightened Sound Daemon mixes several audio streams for playback by a single audio device.
  • Ecasound - a commandline multitrack recorder and editor with various GUI apps.
  • JACK - Jack Audio Connection Kit. If you don't know this app you don't know JACK.

Further reading

  • A close look at ALSA explaining ALSA and the asoundrc in particular from A to Z. By Volker Schatz
  • .asoundrc at the Unofficial wiki

你可能感兴趣的:(asoundrc配置文件简单介绍)