Recently Written · git

sad

sad (2f30.org simple audio daemon) with a Cygwin/OSS port on branch cygwin

git clone https://github.com/equwal/sad

Log | Files | Refs


commit 805907160bf9588d6f52fcfbf067c2aaaa498a5c
sin <sin@2f30.org>
2014-12-31 15:27:14 +0000

Implement volume setting for the alsa backend

 alsa.c   | 43 ++++++++++++++++++++++++++++++++++++++++++-
 output.c |  6 +++---
 sndio.c  |  2 +-
 3 files changed, 46 insertions(+), 5 deletions(-)
diff --git a/alsa.c b/alsa.c
index 2d70b36..d9da80e 100644
--- 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
index 8ead95a..ed1a900 100644
--- 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
index 3c6dd51..221dd22 100644
--- 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;
 	}