0

我写了一个代码来打开和控制混音器音量:

char *card, *channel;
snd_mixer_t *handle = NULL;
snd_mixer_elem_t *elem = NULL;

static long alsa_min, alsa_max;

void alsa_open_mixer( void )
{
    int err;
    static snd_mixer_selem_id_t *sid = NULL;
    if ((err = snd_mixer_open (&handle, 0)) < 0)
        return;
    if ((err = snd_mixer_attach (handle, card)) < 0)
        goto error;
    if ((err = snd_mixer_selem_register (handle, NULL, NULL)) < 0)
        goto error;
    if ((err = snd_mixer_load (handle)) < 0)
        goto error;
    snd_mixer_selem_id_malloc(&sid);
    if (sid == NULL)
        goto error;
    snd_mixer_selem_id_set_name(sid, channel);
    if (!(elem = snd_mixer_find_selem(handle, sid)))
        goto error;
    if (!snd_mixer_selem_has_playback_volume(elem))
        goto error;
    snd_mixer_selem_get_playback_volume_range(elem, &alsa_min, &alsa_max);
    if ((alsa_max - alsa_min) <= 0)
        goto error;
    snd_mixer_selem_id_free(sid);
    return;

error:
    if (sid != NULL) {
        snd_mixer_selem_id_free(sid);
        sid = NULL;
    }

    if (handle) {
        snd_mixer_close(handle);
        handle = NULL;
    }
    return;
}

int alsa_set_device( const char *devname )
{
    int i;

    if (card) free(card);
    card = strdup( devname );
    if( !card )
        return 0;

    i = strcspn( card, "/" );
    if( i == strlen( card ) ) {
        channel = "Line";
    } else {
        card[i] = 0;
        channel = card + i + 1;
    }
    alsa_open_mixer();
    if (!handle) {
        fprintf( stderr, "mixer: Can't open mixer '%s'.\n", card );
        return 0;
    }
    return 1;
}

我的问题是可以通过以下方式打开混音器:

alsa_set_device( "hw:0" )

与:

alsa_set_device( "default" )

得到错误:

mixer: Can't open mixer 'default'.

另外我想编写一个插件来从电视卡捕获音频流并将它们输出到默认的主板声卡中。我想使用 ALSA API 控制我的应用程序的流音量。

也许有移植到 PulseAudio 代替。在这种情况下,我需要与上述代码等效的脉冲混频器代码的帮助。

谢谢

4

0 回答 0