sad

simple audio daemon
git clone git://git.2f30.org/sad
Log | Files | Refs | LICENSE

commit 805907160bf9588d6f52fcfbf067c2aaaa498a5c
parent 55e472d4ca2bc78f5d638a5574fe898050b7a0d4
Author: sin <sin@2f30.org>
Date:   Wed, 31 Dec 2014 15:27:14 +0000

Implement volume setting for the alsa backend

Diffstat:
Malsa.c | 43++++++++++++++++++++++++++++++++++++++++++-
Moutput.c | 6+++---
Msndio.c | 2+-
3 files changed, 46 insertions(+), 5 deletions(-)

diff --git a/alsa.c b/alsa.c @@ -1,4 +1,3 @@ -/* reference: http://www.alsa-project.org/alsa-doc/alsa-lib/_2test_2pcm_min_8c-example.html */ #include <sys/select.h> #include <err.h> @@ -16,7 +15,49 @@ static const char *device = "default"; /* TODO: make configurable? */ static int alsavol(int vol) { + long min, max; + snd_mixer_t *mixerhdl; + snd_mixer_elem_t *elem; + snd_mixer_selem_id_t *sid; + const char *selem_name = "Master"; + + if (snd_mixer_open(&mixerhdl, 0) < 0) { + warnx("snd_mixer_open: failed"); + return -1; + } + + if (snd_mixer_attach(mixerhdl, device) < 0) { + warnx("snd_mixer_attach: failed"); + goto err0; + } + + if (snd_mixer_selem_register(mixerhdl, NULL, NULL) < 0) { + warnx("snd_mixer_selem_register: failed"); + goto err0; + } + + if (snd_mixer_load(mixerhdl) < 0) { + warnx("snd_mixer_load: failed"); + goto err0; + } + + snd_mixer_selem_id_alloca(&sid); + snd_mixer_selem_id_set_index(sid, 0); + snd_mixer_selem_id_set_name(sid, selem_name); + elem = snd_mixer_find_selem(mixerhdl, sid); + if (!elem) { + warnx("snd_mixer_find_selem: failed"); + goto err0; + } + + snd_mixer_selem_get_playback_volume_range(elem, &min, &max); + snd_mixer_selem_set_playback_volume_all(elem, vol * max / 100); + + snd_mixer_close(mixerhdl); return 0; +err0: + snd_mixer_close(mixerhdl); + return -1; } static int diff --git a/output.c b/output.c @@ -13,7 +13,7 @@ typedef struct { char *name; int bits; - long rate; + int rate; int channels; int enabled; int active; @@ -184,7 +184,7 @@ playoutputs(void *inbuf, size_t nbytes) for (i = 0; i < LEN(outputdescs); i++) { desc = &outputdescs[i]; - if (!desc->active) + if (!desc->enabled) continue; if (playoutput(desc, inbuf, nbytes) < 0) r = -1; @@ -200,7 +200,7 @@ setvol(int vol) for (i = 0; i < LEN(outputdescs); i++) { desc = &outputdescs[i]; - if (!desc->active) + if (!desc->enabled) continue; if (desc->output->vol(vol) < 0) r = -1; diff --git a/sndio.c b/sndio.c @@ -12,7 +12,7 @@ static struct sio_hdl *hdl; static int sndiovol(int vol) { - if (hdl && !sio_setvol(hdl, (SIO_MAXVOL * vol) / 100)) { + if (!sio_setvol(hdl, (SIO_MAXVOL * vol) / 100)) { warnx("sio_setvol: failed"); return -1; }