Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include <stdio.h>
#include <alsa/asoundlib.h>
static char card[64] = "default";
static void error(const char *fmt,...)
{
va_list va;
va_start(va, fmt);
fprintf(stderr, "amixer: ");
vfprintf(stderr, fmt, va);
fprintf(stderr, "\n");
va_end(va);
exit(-1);
}
int main() {
int err;
snd_mixer_t *handle = NULL;
snd_mixer_elem_t *elem;
snd_mixer_selem_id_t *sid;
long pvol, pmin, pmax;
snd_mixer_selem_id_alloca(&sid);
fprintf(stderr, "welcome\n");
if ( (err = snd_mixer_open(&handle, 1)) < 0)
error("Control %s open error: %s\n", card, snd_strerror(err));
fprintf(stderr, "attach\n");
if ((err = snd_mixer_attach(handle, card)) < 0) {
snd_mixer_close(handle);
error("Mixer attach %s error: %s", card, snd_strerror(err));
}
fprintf(stderr, "register\n");
if ((err = snd_mixer_selem_register(handle, NULL, NULL)) < 0) {
snd_mixer_close(handle);
error("Mixer register error: %s", snd_strerror(err));
}
fprintf(stderr, "load\n");
err = snd_mixer_load(handle);
if (err < 0) {
snd_mixer_close(handle);
error("Mixer %s load error: %s", card, snd_strerror(err));
}
fprintf(stderr, "set_index\n");
snd_mixer_selem_id_set_index(sid, 0); // spotify does this
snd_mixer_selem_id_set_name(sid, "Master");
fprintf(stderr, "find_selem\n");
elem = snd_mixer_find_selem(handle, sid);
if (!elem) {
snd_mixer_close(handle);
error("Unable to find simple control '%s',%i\n",
snd_mixer_selem_id_get_name(sid),
snd_mixer_selem_id_get_index(sid));
}
fprintf(stderr, "get_index\n");
printf("Simple mixer control '%s',%i\n",
snd_mixer_selem_id_get_name(sid),
snd_mixer_selem_id_get_index(sid));
//show_selem(handle, sid, " ", 1);
// err = snd_mixer_selem_set_playback_volume(elem, SND_MIXER_SCHN_MONO, 33);
//if(err != 0) fprintf(stderr, "error: could not get playback range\n");
// keep printing mixer values for a while. they don't seem to update
// when updated externally!
while(1) {
err = snd_mixer_selem_get_playback_volume_range(elem, &pmin, &pmax);
if(err != 0) fprintf(stderr, "error: could not get playback range\n");
err = snd_mixer_selem_get_playback_volume(elem, SND_MIXER_SCHN_MONO, &pvol);
if(err != 0) fprintf(stderr, "error: could not get playback range\n");
fprintf(stderr, "vol: %ld [%ld, %ld]\n", pvol, pmin, pmax);
pvol = 0;
usleep(1000 * 1000);
}
err = snd_mixer_close(handle);
if(err != 0)
fprintf(stderr, "could not close alsa handle!\n");
return 0;
}
@kristianlm

This comment has been minimized.

Copy link
Owner Author

kristianlm commented Oct 1, 2014

this program is missing handle_events. have a look here, if you're interested

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.