sad

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

commit c705b8624aaff3593851321d70852d300d52c437
parent 493723315574a974d39743e2d9d6ea11297e5db9
Author: sin <sin@2f30.org>
Date:   Thu, 25 Dec 2014 13:13:51 +0000

Add libsndfile

Diffstat:
MMakefile | 2+-
Mcmd.c | 21++-------------------
Mplaylist.c | 1-
Msad.c | 27+++++++++++++--------------
Msad.h | 5++---
Msndio.c | 10++++++++--
Mwav.c | 32+++++++++++++++++++++++++++-----
7 files changed, 53 insertions(+), 45 deletions(-)

diff --git a/Makefile b/Makefile @@ -5,7 +5,7 @@ MANPREFIX = $(PREFIX)/man CFLAGS = -I/usr/local/include -g LDFLAGS = -L /usr/local/lib -LDLIBS = -lsndio +LDLIBS = -lsndfile -lsndio OBJ = sndio.o cmd.o wav.o playlist.o sad.o tokenizer.o BIN = sad diff --git a/cmd.c b/cmd.c @@ -48,18 +48,12 @@ cmdpause(int fd, int argc, char **argv) switch (s->state) { case PLAYING: - if (pause == 1) { + if (pause == 1) s->state = PAUSED; - FD_CLR(s->fd, &master); - } break; case PAUSED: - if (pause == 0) { + if (pause == 0) s->state = PLAYING; - FD_SET(s->fd, &master); - if (s->fd > fdmax) - fdmax = s->fd; - } break; } printf("Song %s with id %d is %s\n", @@ -85,14 +79,6 @@ cmdplay(int fd, int argc, char **argv) return; } - s->fd = open(s->path, O_RDONLY); - if (s->fd < 0) { - dprintf(fd, "ERR \"file doesn't exist\"\n"); - return; - } - FD_SET(s->fd, &master); - if (s->fd > fdmax) - fdmax = s->fd; s->state = PREPARE; putcursong(s); @@ -121,9 +107,6 @@ cmdstop(int fd, int argc, char **argv) return; } decoder->close(); - output->close(); - close(s->fd); - s->fd = -1; s->state = NONE; dprintf(fd, "OK\n"); } diff --git a/playlist.c b/playlist.c @@ -23,7 +23,6 @@ addplaylist(const char *path) strncpy(s->path, path, sizeof(s->path)); s->path[sizeof(s->path) - 1] = '\0'; s->id = rollingid++; - s->fd = -1; s->state = 0; playlist.nsongs++; return s; diff --git a/sad.c b/sad.c @@ -62,24 +62,22 @@ servaccept(int listenfd) void doaudio(void) { - Song *s; + Song *s; s = getcursong(); if (!s) return; - if (!FD_ISSET(s->fd, &rfds)) - return; - switch (s->state) { case PREPARE: - if (decoder->open(s->fd) < 0) - err(1, "decoder: failed to prepare decoding"); + decoder->open(s->path); s->state = PLAYING; break; case PLAYING: - if (decoder->decode(s->fd) < 0) - err(1, "decoder: failde to decode buffer"); + if (decoder->decode() == 0) { + decoder->close(); + s->state = NONE; + } break; } } @@ -87,7 +85,8 @@ doaudio(void) int main(void) { - int listenfd, clifd, n, i; + int listenfd, clifd, n, i; + struct timeval tv; FD_ZERO(&master); FD_ZERO(&rfds); @@ -96,14 +95,14 @@ main(void) FD_SET(listenfd, &master); fdmax = listenfd; - if (output->init() < 0) - errx(1, "output: init failed"); - if (decoder->init() < 0) - errx(1, "decoder: init failed"); + output->init(); + decoder->init(); while (1) { rfds = master; - n = select(fdmax + 1, &rfds, NULL, NULL, NULL); + tv.tv_sec = 0; + tv.tv_usec = 1000; + n = select(fdmax + 1, &rfds, NULL, NULL, &tv); if (n < 0) err(1, "select"); diff --git a/sad.h b/sad.h @@ -16,7 +16,6 @@ enum { typedef struct { char path[PATH_MAX]; int id; - int fd; int state; } Song; @@ -28,8 +27,8 @@ typedef struct { typedef struct { int (*init)(void); - int (*open)(int); - int (*decode)(int); + int (*open)(const char *); + int (*decode)(void); int (*close)(void); void (*exit)(void); } Decoder; diff --git a/sndio.c b/sndio.c @@ -21,8 +21,10 @@ sndioopen(int bits, int rate, int channels) struct sio_par par; hdl = sio_open(SIO_DEVANY, SIO_PLAY, 0); - if (!hdl) + if (!hdl) { + warnx("sio_open: failed"); return -1; + } sio_initpar(&par); par.bits = bits; @@ -31,18 +33,21 @@ sndioopen(int bits, int rate, int channels) par.sig = 1; par.le = SIO_LE_NATIVE; - if (!sio_setpar(hdl, &par)) { + if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par)) { + warnx("sio_{set,get}par: failed"); sio_close(hdl); hdl = NULL; return -1; } if (!sio_start(hdl)) { + warnx("sio_start: failed"); sio_close(hdl); hdl = NULL; return -1; } + puts("Opened sndio output"); return 0; } @@ -58,6 +63,7 @@ sndioclose(void) if (hdl) sio_close(hdl); hdl = NULL; + puts("Closed sndio output"); } static void diff --git a/wav.c b/wav.c @@ -2,9 +2,13 @@ #include <limits.h> #include <stdio.h> +#include <sndfile.h> #include "sad.h" +static SNDFILE *sf; +static SF_INFO sfinfo; + static int wavinit(void) { @@ -12,21 +16,39 @@ wavinit(void) } static int -wavopen(int fd) +wavopen(const char *name) { - return 0; + sf = sf_open(name, SFM_READ, &sfinfo); + if (!sf) { + warnx("sf_open_fd: failed"); + return -1; + } + return output->open(16, sfinfo.samplerate, sfinfo.channels); } static int -wavdecode(int fd) +wavdecode(void) { - return 0; + sf_count_t n; + short buf[2048]; + + n = sf_read_short(sf, buf, 2048); + if (n > 0) + output->play(buf, n * sizeof(short)); + return n * sizeof(short); } static int wavclose(void) { - return 0; + int r; + + r = sf_close(sf); + if (r != 0) { + warnx("sf_close: failed"); + return -1; + } + return output->close(); } static void