分享

在嵌入式平台中利用alsa dmix实现混音

 dwlinux_gs 2015-11-04
首先了解下alsa的基本架构:
                                      Application 
                  ---------------
                   Alsa-lib                   User Space
                  -------------------------------------
                   Alsa                      Kernel Space
                  -------
                   sound driver 
                  ----------------------------------
                   Hardware 
Application : 比如aplay ,它不是直接调用Kernel所提供的接口,而是调用ALSA-lib 的接口。所以应用程序只要#include "asound.h"
 并链 接libasound .
 对于上面的架构,在某一时刻只能有一个程序打开声卡并占有它,此时其它程序打开的话,会返回busy.
如要支持同时可 以多个应用程序打开声卡,需要支持混音功能,有些声卡支持硬件混音,但大部分声卡不支持硬件混音,需要软件混音。这时需要 ESD,pulseAudio等,架构变为:
                                      App1    App2 
                  ---------------
                   ESD , pulseaudio
                  --------------------  
                   Alsa-lib                   User Space
                  -------------------------------------
                   Alsa                      Kernel Space
                  -------
                   sound driver 
                  ----------------------------------
                   Hardware 
  此时,应用程序将调用ESD,pulseaudio等混音器提供的接口。对于ESD,很多程序支持,比如mplayer . 对于pulseaudio ,有相应的patch。

Alsa本身也提供混音的plugin,dmix .
                   App1    App2 
                 ---------------
                  Alsa-lib (dmix)           User Space
                 -------------------------------------
                   Alsa                      Kernel Space
                 -------
                   sound driver 
                 ----------------------------------
                   Hardware 
  此架构和架构1,应用程序不需要做任何修改,只需要修改
/usr/share/alsa/asound.conf 

  架构1的/usr/share/alsa/asound.conf的例子:
  pcm.!default {   
   type hw    
    card 0  
    } 
ctl.!default {  
    type hw     
    card 0 
     }
   架构3的
/usr/share/alsa/asound.conf的例子:   

   pcm.card0 {
    type hw
    card 0
}

pcm.!default {
    type plug
    slave.pcm "dmixer"
}
pcm.dmixer  {
    type dmix
    ipc_key 1025
    slave {
        pcm "hw:0,0"
        period_time 0
        period_size 4096
        buffer_size 16384 
        periods 128
        rate 44100
    }
    bindings {
        0 0
        1 1
    }
}
 关于配置,可以参考这个网站:
  http://www./alsa-doc/alsa-lib/pcm_plugins.html 

对于period_size和buffer_size,要注意,这里将它们修改为1024 ,8192.

用mplayer进行测试:

board1:$mplayer -slave -quiet test1.mp3

board2:$mplayer -slave -quiet test2.mp3

就可以听到2个音乐混在一起输出,且可以各自控制各自的播放。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多