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 584f4028c158e3d7f88fdcccd7b6b4431c7792e7
Hiltjo Posthuma <hiltjo@codemadness.org>
2014-12-28 22:56:07 +0100

alsa output: start, needs alot more work

 Makefile     |  3 ++-
 alsa.c       | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 config.def.h |  5 +++--
 sad.h        |  3 +++
 4 files changed, 78 insertions(+), 3 deletions(-)
diff --git a/Makefile b/Makefile
index c8a998a..bb16556 100644
--- a/Makefile
+++ b/Makefile
@@ -5,9 +5,10 @@ MANPREFIX = $(PREFIX)/man
 
 CFLAGS = -I/usr/local/include
 LDFLAGS = -L /usr/local/lib
-LDLIBS = -lsndfile -lmpg123 -lsndio -lvorbisfile
+LDLIBS = -lsndfile -lmpg123 -lsndio -lasound -lvorbisfile
 
 OBJ =\
+	alsa.o\
 	cmd.o\
 	decoder.o\
 	fifo.o\
diff --git a/alsa.c b/alsa.c
new file mode 100644
index 0000000..c1cc48d
--- /dev/null
+++ b/alsa.c
@@ -0,0 +1,70 @@
+/* reference: http://www.alsa-project.org/alsa-doc/alsa-lib/_2test_2pcm_min_8c-example.html */
+
+#include <sys/select.h>
+
+#include <err.h>
+#include <limits.h>
+#include <stdio.h>
+
+#include <alsa/asoundlib.h>
+
+#include "sad.h"
+
+static snd_pcm_t *hdl;
+static snd_pcm_format_t format = SND_PCM_FORMAT_S16_LE;
+static const int frame_size = 4; /* TODO: don't hardcode */
+
+static int
+alsavol(int vol)
+{
+	return 0;
+}
+
+static int
+alsaopen(int bits, int rate, int channels)
+{
+	const char *device = "default"; /* TODO: make configurable? */
+	int r;
+
+	if ((r = snd_pcm_open(&hdl, device, SND_PCM_STREAM_PLAYBACK, 0)) < 0) {
+		warnx("snd_pcm_open: %s\n", snd_strerror(r));
+		return -1;
+	}
+	if ((r = snd_pcm_set_params(hdl, format, SND_PCM_ACCESS_RW_INTERLEAVED,
+	     channels, rate, 1, 500000)) < 0) {
+		warnx("send_pcm_set_params: %s\n", snd_strerror(r));
+		return -1;
+	}
+	return 0;
+}
+
+static int
+alsaplay(void *buf, size_t nbytes)
+{
+	snd_pcm_sframes_t frames;
+
+	frames = snd_pcm_writei(hdl, buf, nbytes / frame_size);
+	if (frames < 0)
+		frames = snd_pcm_recover(hdl, frames, 0);
+	if (frames < 0) {
+		warnx("snd_pcm_writei failed: %s\n", snd_strerror(frames));
+		return -1;
+	}
+	return frames;
+}
+
+static int
+alsaclose(void)
+{
+	if (hdl)
+		snd_pcm_close(hdl);
+	hdl = NULL;
+	return 0;
+}
+
+Output alsaoutput = {
+	.vol = alsavol,
+	.open = alsaopen,
+	.play = alsaplay,
+	.close = alsaclose,
+};
diff --git a/config.def.h b/config.def.h
index cb990f1..268e70f 100644
--- a/config.def.h
+++ b/config.def.h
@@ -1,4 +1,5 @@
 Outputdesc Outputdescs[] = {
-       { "sndio", 16, 44100, 2, 1, 0, &sndiooutput },
-       { "fifo",  16, 44100, 2, 1, 0, &fifooutput  },
+	{ "sndio", 16, 44100, 2, 1, 0, &sndiooutput },
+	{ "alsa" , 16, 44100, 2, 1, 0, &alsaoutput  },
+	{ "fifo",  16, 44100, 2, 1, 0, &fifooutput  },
 };
diff --git a/sad.h b/sad.h
index e993f74..6833a46 100644
--- a/sad.h
+++ b/sad.h
@@ -93,6 +93,9 @@ extern Decoder vorbisdecoder;
 /* sndio.c */
 extern Output sndiooutput;
 
+/* alsa.c */
+extern Output alsaoutput;
+
 /* fifo.c */
 extern Output fifooutput;