sad

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

commit 536ea984e5ae4208479a038eb6d9c61296b16d02
parent e187d5d35e8714d2bb438b0b055b46a6b37a1f2d
Author: sin <sin@2f30.org>
Date:   Thu, 25 Dec 2014 11:33:26 +0000

Back to the drawing board

Diffstat:
MMakefile | 8++++----
Dao.c | 65-----------------------------------------------------------------
Mcmd.c | 4++--
Dmp3.c | 80-------------------------------------------------------------------------------
Msad.c | 32+++++++++++++++++---------------
Msad.h | 26+++++++++++++-------------
Asndio.c | 74++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7 files changed, 110 insertions(+), 179 deletions(-)

diff --git a/Makefile b/Makefile @@ -5,9 +5,9 @@ MANPREFIX = $(PREFIX)/man CFLAGS = -I/usr/local/include -g LDFLAGS = -L /usr/local/lib -LDLIBS = -lmpg123 -lao +LDLIBS = -lsndio -OBJ = ao.o cmd.o mp3.o playlist.o sad.o tokenizer.o +OBJ = sndio.o cmd.o wav.o playlist.o sad.o tokenizer.o BIN = sad all: $(BIN) @@ -15,9 +15,9 @@ all: $(BIN) $(BIN): $(OBJ) $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJ) $(LDLIBS) -ao.o: sad.h +sndio.o: sad.h cmd.o: sad.h -mp3.o: sad.h +wav.o: sad.h playlist.o: sad.h sad.o: sad.h tokenizer.o: sad.h diff --git a/ao.c b/ao.c @@ -1,65 +0,0 @@ -#include <sys/select.h> - -#include <err.h> -#include <limits.h> -#include <stdio.h> - -#include <ao/ao.h> - -#include "sad.h" - -static ao_sample_format format; -static ao_device *dev; -static int driver; - -static int -aoinit(void) -{ - ao_initialize(); - driver = ao_default_driver_id(); - return driver < 0 ? -1 : 0; -} - -static int -aoopen(long rate, int channels, int bits) -{ - format.rate = rate; - format.channels = channels; - format.bits = bits; - format.byte_format = AO_FMT_NATIVE; - format.matrix = 0; - - dev = ao_open_live(driver, &format, NULL); - return !dev ? -1 : 0; -} - -static int -aoplay(void *buf, size_t size) -{ - int r; - - r = ao_play(dev, buf, size); - if (!r) - return -1; - return size; -} - -static int -aoclose(void) -{ - return !ao_close(dev) ? -1 : 0; -} - -static void -aoexit(void) -{ - ao_shutdown(); -} - -Output aooutput = { - .init = aoinit, - .open = aoopen, - .play = aoplay, - .close = aoclose, - .exit = aoexit -}; diff --git a/cmd.c b/cmd.c @@ -120,8 +120,8 @@ cmdstop(int fd, int argc, char **argv) dprintf(fd, "ERR \"no song is active\"\n"); return; } - curdecoder->close(); - curoutput->close(); + decoder->close(); + output->close(); close(s->fd); s->fd = -1; s->state = NONE; diff --git a/mp3.c b/mp3.c @@ -1,80 +0,0 @@ -#include <sys/select.h> - -#include <limits.h> -#include <stdio.h> - -#include <mpg123.h> - -#include "sad.h" - -static mpg123_handle *handle; - -static int -mp3init(void) -{ - mpg123_init(); - - handle = mpg123_new(NULL, NULL); - if (!handle) - return -1; - return 0; -} - -static int -mp3open(int fd) -{ - return mpg123_open_feed(handle) != MPG123_OK ? -1 : 0; -} - -static int -mp3decode(int fd) -{ - unsigned char inbuf[8192]; - unsigned char outbuf[32768]; - ssize_t n; - size_t sz; - int r; - long rate; - int channels, encoding, bits; - - n = read(fd, inbuf, sizeof(inbuf)); - if (n < 0) - return -1; - if (n == 0) - return 0; - - mpg123_feed(handle, inbuf, n); - r = mpg123_read(handle, outbuf, sizeof(outbuf), &sz); - if (r == MPG123_NEW_FORMAT) { - r = mpg123_getformat(handle, &rate, &channels, - &encoding); - if (r != MPG123_OK) - return -1; - bits = mpg123_encsize(encoding) * 8; - mpg123_format_none(handle); - mpg123_format(handle, rate, channels, encoding); - curoutput->open(rate, channels, bits); - } - curoutput->play(outbuf, sz); -} - -static int -mp3close(void) -{ - return mpg123_close(handle) != MPG123_OK ? -1 : 0; -} - -static void -mp3exit(void) -{ - mpg123_delete(handle); - mpg123_exit(); -} - -Decoder mp3decoder = { - .init = mp3init, - .open = mp3open, - .decode = mp3decode, - .close = mp3close, - .exit = mp3exit -}; diff --git a/sad.c b/sad.c @@ -15,13 +15,13 @@ fd_set master; fd_set rfds; int fdmax; -Output *curoutput = &aooutput; -Decoder *curdecoder = &mp3decoder; +Output *output = &sndiooutput; +Decoder *decoder = &wavdecoder; int servlisten(const char *name) { - struct sockaddr_un sa; + struct sockaddr_un sun; int listenfd, r, len; listenfd = socket(AF_UNIX, SOCK_STREAM, 0); @@ -30,12 +30,12 @@ servlisten(const char *name) unlink(name); - memset(&sa, 0, sizeof(sa)); - sa.sun_family = AF_UNIX; - strcpy(sa.sun_path, name); + memset(&sun, 0, sizeof(sun)); + sun.sun_family = AF_UNIX; + strlcpy(sun.sun_path, name, sizeof(sun.sun_path)); - len = strlen(sa.sun_path) + sizeof(sa.sun_family); - r = bind(listenfd, (struct sockaddr *)&sa, len); + len = sizeof(sun); + r = bind(listenfd, (struct sockaddr *)&sun, len); if (r < 0) err(1, "bind"); @@ -49,11 +49,11 @@ servlisten(const char *name) int servaccept(int listenfd) { - struct sockaddr_un sa; + struct sockaddr_un sun; int clifd, len; - len = sizeof(sa); - clifd = accept(listenfd, (struct sockaddr *)&sa, &len); + len = sizeof(sun); + clifd = accept(listenfd, (struct sockaddr *)&sun, &len); if (clifd < 0) err(1, "accept"); return clifd; @@ -73,11 +73,13 @@ doaudio(void) switch (s->state) { case PREPARE: - curdecoder->open(s->fd); + if (decoder->open(s->fd) < 0) + err(1, "decoder: failed to prepare decoding"); s->state = PLAYING; break; case PLAYING: - curdecoder->decode(s->fd); + if (decoder->decode(s->fd) < 0) + err(1, "decoder: failde to decode buffer"); break; } } @@ -94,9 +96,9 @@ main(void) FD_SET(listenfd, &master); fdmax = listenfd; - if (curoutput->init() < 0) + if (output->init() < 0) errx(1, "output: init failed"); - if (curdecoder->init() < 0) + if (decoder->init() < 0) errx(1, "decoder: init failed"); while (1) { diff --git a/sad.h b/sad.h @@ -36,17 +36,18 @@ typedef struct { typedef struct { int (*init)(void); - int (*open)(long, int, int); - int (*play)(void *, size_t); + int (*open)(int, int, int); + void (*play)(void *, size_t); int (*close)(void); void (*exit)(void); } Output; -/* ao.c */ -extern Output aooutput; - -/* mp3.c */ -extern Decoder mp3decoder; +/* sad.c */ +extern fd_set master; +extern fd_set rfds; +extern int fdmax; +extern Output *output; +extern Decoder *decoder; /* playlist.c */ Song *addplaylist(const char *); @@ -55,12 +56,11 @@ Song *findsongid(int); Song *getcursong(void); void putcursong(Song *); -/* sad.c */ -extern fd_set master; -extern fd_set rfds; -extern int fdmax; -extern Output *curoutput; -extern Decoder *curdecoder; +/* wav.c */ +extern Decoder wavdecoder; + +/* sndio.c */ +extern Output sndiooutput; /* tokenizer.c */ int gettokens(char *, char **, int, char *); diff --git a/sndio.c b/sndio.c @@ -0,0 +1,74 @@ +#include <sys/select.h> + +#include <err.h> +#include <limits.h> +#include <stdio.h> +#include <sndio.h> + +#include "sad.h" + +static struct sio_hdl *hdl; + +static int +sndioinit(void) +{ + return 0; +} + +static int +sndioopen(int bits, int rate, int channels) +{ + struct sio_par par; + + hdl = sio_open(SIO_DEVANY, SIO_PLAY, 0); + if (!hdl) + return -1; + + sio_initpar(&par); + par.bits = bits; + par.rate = rate; + par.pchan = channels; + par.sig = 1; + par.le = SIO_LE_NATIVE; + + if (!sio_setpar(hdl, &par)) { + sio_close(hdl); + hdl = NULL; + return -1; + } + + if (!sio_start(hdl)) { + sio_close(hdl); + hdl = NULL; + return -1; + } + + return 0; +} + +static void +sndioplay(void *buf, size_t size) +{ + sio_write(hdl, buf, size); +} + +static int +sndioclose(void) +{ + if (hdl) + sio_close(hdl); + hdl = NULL; +} + +static void +sndioexit(void) +{ +} + +Output sndiooutput = { + .init = sndioinit, + .open = sndioopen, + .play = sndioplay, + .close = sndioclose, + .exit = sndioexit +};