2016年7月11日 星期一

dmix of ALSA library.

最近工作上需要讓 不同的兩個 AP 都能透過 ALSA 同時撥 .WAV file, 也就是需要兩個來源混音.

Google 了一下, 發現 ALSA Lib 就有內建 dmix (direct mixing) 的功能.

http://www.alsa-project.org/main/index.php/Asoundrc

These days we have a native plugin for ALSA called the dmix (direct mixing) plugin. It allows software mixing in an easy to use syntax and without the hassle of installing/understanding a new application first.


也有很多網頁說明要填寫 /etc/asound.conf , 我按照網頁的填寫方式後用 aplay 去驗證發現下列的問題:

ALSA lib pcm_direct.c:812:(snd_pcm_direct_initialize_slave) requested or auto-format is not available
ALSA lib pcm_dmix.c:831:(snd_pcm_dmix_open) unable to initialize slave 


追蹤 了一下 error code 的來源發生在下列片段內, 主要是沒有辦法設定格式,檢查了aousnd.conf 也看不出任何錯誤.

int snd_pcm_direct_initialize_slave(snd_pcm_direct_t *dmix, snd_pcm_t *spcm, struct slave_params *params)
{


........................................................
    ret = snd_pcm_hw_params_set_format(spcm, hw_params, params->format);
    if (ret < 0) {
        static const snd_pcm_format_t dmix_formats[] = {
            SND_PCM_FORMAT_S32,
            SND_PCM_FORMAT_S32 ^ SND_PCM_FORMAT_S32_LE ^ SND_PCM_FORMAT_S32_BE,
            SND_PCM_FORMAT_S16,
            SND_PCM_FORMAT_S16 ^ SND_PCM_FORMAT_S16_LE ^ SND_PCM_FORMAT_S16_BE,
            SND_PCM_FORMAT_S24_3LE,
        };
         snd_pcm_format_t format;
        unsigned int i;

        for (i = 0; i < sizeof dmix_formats / sizeof dmix_formats[0]; ++i) {
            format = dmix_formats[i];
            ret = snd_pcm_hw_params_set_format(spcm, hw_params, format);
            if (ret >= 0)
                break;
        }
        if (ret < 0 && dmix->type != SND_PCM_TYPE_DMIX) {
            /* TODO: try to choose a good format */
            ret = INTERNAL(snd_pcm_hw_params_set_format_first)(spcm, hw_params, &format);
        }
        if (ret < 0) {
            SNDERR("requested or auto-format is not available");
            return ret;
        }
        params->format = format;

 ................................



Google 上也沒有找到說明很清楚這樣的問題點出現在哪部份.

經過幾天的 trial and error 發現, 主要是我的 CODEC driver 並不支援dmix 所需要的 format.
dmix 只支援下列格式(由 code可以知道) .

SND_PCM_FORMAT_S32,
SND_PCM_FORMAT_S32 ^ SND_PCM_FORMAT_S32_LE ^ SND_PCM_FORMAT_S32_BE,
SND_PCM_FORMAT_S16,
SND_PCM_FORMAT_S16 ^ SND_PCM_FORMAT_S16_LE ^ SND_PCM_FORMAT_S16_BE,
SND_PCM_FORMAT_S24_3LE,



所以就先修改我的CODEC driver , 讓它支援 S16_LE (之前只有 支援 U16_LE ).
這個 bug 就解決了 , 並且 asound.conf 也可以讓 "default" 順利的轉向到 dmix 上.

所以 dmix 如果出現
ALSA lib pcm_direct.c:812:(snd_pcm_direct_initialize_slave) requested or auto-format is not available

錯誤 , 要先看看 codec driver 是否支援 dmix 需要的格式.


PS: 有關 asound.conf 可以參考下列網頁 , 我認為寫的比較清楚的.

       http://forums.gentoo.tw/viewtopic.php?f=18&t=44507







1 則留言:

  1. 我使用的是比較舊的 alsa library "alsa-lib-1.0.14" .

    回覆刪除