commit 53145851013f976d963fc9eb820f1cc2afc9e5a4
parent 35f6493e774ec81f50df2d68cc5f40260d8d71c5
Author: sin <sin@2f30.org>
Date: Sat, 27 Dec 2014 11:30:15 +0000
Add output.c
Diffstat:
6 files changed, 101 insertions(+), 8 deletions(-)
diff --git a/Makefile b/Makefile
@@ -7,7 +7,7 @@ CFLAGS = -I/usr/local/include -g
LDFLAGS = -L /usr/local/lib
LDLIBS = -lsndfile -lmpg123 -lsndio -logg -lvorbis -lvorbisfile
-OBJ = sndio.o cmd.o mp3.o wav.o vorbis.o playlist.o sad.o decoder.o
+OBJ = sndio.o cmd.o mp3.o wav.o vorbis.o playlist.o sad.o decoder.o output.o
BIN = sad
# non-OpenBSD
@@ -27,6 +27,7 @@ vorbis.o: sad.h
playlist.o: sad.h
sad.o: sad.h
decoder.o: sad.h
+output.o: sad.h
install: all
mkdir -p $(DESTDIR)$(PREFIX)/bin
diff --git a/cmd.c b/cmd.c
@@ -32,7 +32,7 @@ cmdvolume(int fd, char *arg)
dprintf(fd, "ERR volume should be between [0, 100]\n");
return;
}
- if (output->vol(vol) < 0) {
+ if (voloutput(vol) < 0) {
dprintf(fd, "ERR failed to change volume\n");
return;
}
diff --git a/output.c b/output.c
@@ -0,0 +1,88 @@
+#include <sys/select.h>
+
+#include <err.h>
+#include <limits.h>
+#include <stdio.h>
+#include <string.h>
+#include <strings.h>
+
+#include "sad.h"
+
+static struct {
+ char *name;
+ int bits;
+ long rate;
+ int channels;
+ int enabled;
+ Output *output;
+} outputs[] = {
+ {
+ .name = "sndio",
+ .bits = 16,
+ .rate = 44100,
+ .channels = 2,
+ .enabled = 1,
+ .output = &sndiooutput
+ },
+};
+
+int
+openoutput(const char *name)
+{
+ int i;
+
+ for (i = 0; i < LEN(outputs); i++) {
+ if (outputs[i].enabled)
+ continue;
+ if (strcmp(outputs[i].name, name))
+ continue;
+ return outputs[i].output->open(outputs[i].bits,
+ outputs[i].rate,
+ outputs[i].channels);
+ }
+ return -1;
+}
+
+int
+openoutputs(void)
+{
+ int i, r = 0;
+
+ for (i = 0; i < LEN(outputs); i++) {
+ if (!outputs[i].enabled)
+ continue;
+ if (outputs[i].output->open(outputs[i].bits,
+ outputs[i].rate,
+ outputs[i].channels) < 0)
+ r = -1;
+ }
+ return r;
+}
+
+int
+playoutput(void *buf, size_t nbytes)
+{
+ int i, r = 0;
+
+ for (i = 0; i < LEN(outputs); i++) {
+ if (!outputs[i].enabled)
+ continue;
+ if (outputs[i].output->play(buf, nbytes) < 0)
+ r = -1;
+ }
+ return r;
+}
+
+int
+voloutput(int vol)
+{
+ int i, r = 0;
+
+ for (i = 0; i < LEN(outputs); i++) {
+ if (!outputs[i].enabled)
+ continue;
+ if (outputs[i].output->vol(vol) < 0)
+ r = -1;
+ }
+ return r;
+}
diff --git a/sad.c b/sad.c
@@ -16,7 +16,6 @@
fd_set master;
fd_set rfds;
int fdmax;
-Output *output = &sndiooutput;
static int
servlisten(const char *name)
@@ -84,7 +83,7 @@ playaudio(void)
if ((nbytes = s->decoder->decode(buf, sizeof(buf))) <= 0) {
playnextsong();
} else {
- output->play(buf, nbytes);
+ playoutput(buf, nbytes);
}
break;
}
@@ -104,7 +103,7 @@ main(void)
fdmax = listenfd;
initdecoders();
- output->open(16, 44100, 2);
+ openoutputs();
while (1) {
rfds = master;
diff --git a/sad.h b/sad.h
@@ -50,7 +50,6 @@ typedef struct {
extern fd_set master;
extern fd_set rfds;
extern int fdmax;
-extern Output *output;
/* cmd.c */
int docmd(int);
@@ -89,3 +88,9 @@ int tokenize(char *, char **, int);
/* decoder.c */
int initdecoders(void);
Decoder *matchdecoder(const char *);
+
+/* output.c */
+int openoutput(const char *);
+int openoutputs(void);
+int playoutput(void *, size_t);
+int voloutput(int);
diff --git a/sndio.c b/sndio.c
@@ -64,9 +64,9 @@ err0:
}
static int
-sndioplay(void *buf, size_t size)
+sndioplay(void *buf, size_t nbytes)
{
- return sio_write(hdl, buf, size);
+ return sio_write(hdl, buf, nbytes);
}
static int