Set mixer as “default” ALSA API

2019-08-27 11:45发布

I wrote a code to open and control mixer volume:

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

My questions is way can open mixer with:

alsa_set_device( "hw:0" )

and with:

alsa_set_device( "default" )

got error:

mixer: Can't open mixer 'default'.

Also I want to write a plugin to capture audio stream from TV card and output them in default mainboard sound card. I want to can control stream volume from my application using ALSA API.

Maybe have porting to PulseAudio instead. In this case I need help with pulse mixer code equivalent with my above code.

Thanks

标签: c alsa mixer
0条回答
登录 后发表回答