asound.conf的作用

发布时间 2023-08-09 09:09:09作者: ahuo

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

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

  

  • Plugin

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

  

dmix

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

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

  

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

  

Virtual 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