Original address http://www.alsa-project.org/main/index.php/Asoundrc
Why asoundrc?
. Asoundrc and asound. conf are not necessary for ALSA to work properly. Most applications can run normally without them. These two files are used to provide specific additional functions, such as routing and sampling rate conversion on the Alsa-lig layer.
The. asoundrc File
This file provides you with more advanced control over card/device .. The asoundrc file contains various card definitions so that you can access PCM plugins at the Alsa-lib layer. You can do a lot of amazing things, such as combining multiple cards into one, or accessing multiple I/OS of multi-channel cards.
Where does asoundrc live?
Asoundrc files are generally stored in the user's home directory.
~/.asoundrc
Include the following file
/usr/share/alsa/alsa.conf
Of course, you can also use the configuration files that take effect for all users.
/etc/asound.conf
When an ALSA application is started, both configuration files are loaded.
The following is the most basic configuration definition.
The default plugin
Create a. asoundrc file in the home directory of the current user.
vi ~/.asoundrc
Write the following content and save the file
pcm.!default { type hw card 0}ctl.!default { type hw card 0}
Keyword default is defined as HW: In ALSA lib API (default device of the default sound card ). ID "! "Default" will overwrite the default defined in the Alsa lib API
Now you can test as follows:
aplay -D default test.wav
The naming of PCM Devices
A typical asoundrc starts with 'PCM HW type. This allows the Alsa application to access a virtual sound card device (plugin or slave) by a name ). Without it, we must access Soundcard (s)/device (s) in the format of HW: or default, as shown below:
aplay -D hw:0,0 test.wav
The two digits following the keyword HW indicate the sound card number and the device number.
The first item usually starts from 0. For example, the representation of PCM devices (physical I/O channels) in ALSA starts with pcm0c (capture) and pcm0p (playback ).
The control device
The control device of one card allows us to program and modify various controls of the card. For most cards, mixer is included (but some cards do not have mixer ). However, they still have other controls that may be accessed by programs like Jack. For example, the digital I/O sync indicator and the sample clock source switch.
Aliases
You can also use "pcm hw type" to define the device alias. Syntax:
pcm.NAME { type hw card INT/STR [device] INT [subdevice] INT mmap_emulation BOOL}
For example, the following shows the alias of the first sound card.
pcm.primary { type hw card 0 device 0}
Now you can access the first sound card through the alias primary.
aplay -D primary test.wav
Plugins
Q: What is plugins?
A: In ALSA, PCM plugins extends the functions and features of PCM devices. Plugin can automatically perform incredible tasks, such as naming devices, sampling rate conversion, sampling and replication between channels, writing files, and combining multiple sound cards/devices with multiple inputs/outputs. You can use multi-channel sound cards, devices, and other functions. To use these plugins, you need to create a virtual slave device.
For a complete list of plugins and options, see ALSA-lib documentation. The following is a brief introduction.
A simple slave definition is as follows:
pcm_slave.sltest { pcm "hw:1,0"}
A slave is defined here. This only defines an alias for your sound device. It is hard to understand that the 'PCM types' parameter must be defined in the slave-definition-block. Let's set a rate-converter first
pcm_slave.sl2 { pcm "hw:0,0" rate 48000}pcm.rate_convert { type rate slave sl2}
During play, the automatic conversion sampling rate is 44.1 kHz. Although this function is not very practical, most players will convert to the sampling conversion rate supported by the sound card, but sometimes you may want to convert the sampling rate to a low static sampling rate.
For convenience, we can rewrite the nested definition not described below.
pcm.rate_convert { type rate slave { pcm "hw:0,0" rate 48000 }}
PCM type plug can be used to generate a more complex conversion tool. The syntax is as follows:
type plug #format adjusted PCMslave STR #slave name# orslave { pcm STR #dslave definition #or pcm {} #slave pcm definition [format STR] #slave format [channel INT] [rate INT]}route policy STRttable { CCHANNEL { SCHANNEL REAL }}
We can define a complex_convert as follows:
pcm_slave.sl3 { pcm "hw:0,0" format S16_LE channels 1 rate 16000}pcm.complex_convert { type plug slave sl3}
The caller is as follows:
aplay -vD complex_convert test.wav
During playback, you will convert the sample format to s16_le, 1 channel, and 16 KHz sampling. Use the-V option to call aplay to display the original settings of the file.
Software Mixing
Software sound mixing can play multiple sound files simultaneously on the same device. There are many ways to execute software mixing in Linux. A server, such as artsd, ESD, Jack..., is usually required. This list will be very long, so applications are often confused about which server to use.
Dmix
Now ALSA has a local plug-in called dmix (direct mixing. It makes software sound mixing very simple and does not need to worry about installing or uninstalling new applications.
This plug-in has a very interesting and potentially useful function. It is combined with the default plug-in name. Theoretically, all local applications share sound devices. Although not many applications actually use this function.
pcm.!default { type plug slave.pcm "dmixer"}pcm.dmixer { type dmix ipc_key 1024 slave { pcm "hw:0,0" period_time 0 period_size 1024 buffer_size 4096 rate 44100 } bindings { 0 0 1 1 }}ctl.dmixer { type hw card 0}
Try to play the video as follows:
aplay -f cd -D default test.
Note that the dmix PCM name has been defined in the global configuration file/usr/share/ALSA. conf.
The default sampling rate of this device is 48000Hz, which can be changed as follows:
aplay -D "plug:'dmix:RATE=44100'" test.wav
Dsnoop
Dmix plugin is used to mix multiple output streams. If you want to use multiple inputs, use dsnoop plugin
Jack plugin
Virtual Multi Channel devices