sad

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

commit e187d5d35e8714d2bb438b0b055b46a6b37a1f2d
parent 7f2c334cdf0b4c672772ad7275a36e6b9d64333c
Author: sin <sin@2f30.org>
Date:   Wed Dec 24 23:19:18 +0000

Various updates - completely broken atm

Diffstat:
Makefile | 2+-
ao.c | 14++++----------
cmd.c | 4++--
mp3.c | 61+++++++++++++++++++++++++++++--------------------------------
sad.c | 30++++--------------------------
sad.h | 13+++++--------
6 files changed, 45 insertions(+), 79 deletions(-)
diff --git a/Makefile b/Makefile @@ -3,7 +3,7 @@ VERSION = 0.0 PREFIX = /usr/local MANPREFIX = $(PREFIX)/man -CFLAGS = -I/usr/local/include +CFLAGS = -I/usr/local/include -g LDFLAGS = -L /usr/local/lib LDLIBS = -lmpg123 -lao diff --git a/ao.c b/ao.c @@ -21,32 +21,27 @@ aoinit(void) } static int -aoputfmt(long rate, int channels, int bits) +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; - return 0; -} -static int -aoopen(void) -{ dev = ao_open_live(driver, &format, NULL); return !dev ? -1 : 0; } static int -aowrite(void *buf, size_t size) +aoplay(void *buf, size_t size) { int r; r = ao_play(dev, buf, size); if (!r) return -1; - return r; + return size; } static int @@ -64,8 +59,7 @@ aoexit(void) Output aooutput = { .init = aoinit, .open = aoopen, - .putfmt = aoputfmt, - .write = aowrite, + .play = aoplay, .close = aoclose, .exit = aoexit }; diff --git a/cmd.c b/cmd.c @@ -93,10 +93,10 @@ cmdplay(int fd, int argc, char **argv) FD_SET(s->fd, &master); if (s->fd > fdmax) fdmax = s->fd; - s->state = READYTOPLAY; + s->state = PREPARE; putcursong(s); - printf("Song %s with %d ready to play\n", + printf("Song %s with %d playing\n", s->path, s->id); } diff --git a/mp3.c b/mp3.c @@ -13,6 +13,7 @@ static int mp3init(void) { mpg123_init(); + handle = mpg123_new(NULL, NULL); if (!handle) return -1; @@ -20,43 +21,41 @@ mp3init(void) } static int -mp3open(const char *path) +mp3open(int fd) { - return mpg123_open(handle, path) != MPG123_OK ? -1 : 0; -} - -static size_t -mp3bufsz(void) -{ - return mpg123_outblock(handle); + return mpg123_open_feed(handle) != MPG123_OK ? -1 : 0; } static int -mp3getfmt(long *rate, int *channels, int *bits) +mp3decode(int fd) { - long r; - int c, e; - int ret; + unsigned char inbuf[8192]; + unsigned char outbuf[32768]; + ssize_t n; + size_t sz; + int r; + long rate; + int channels, encoding, bits; - ret = mpg123_getformat(handle, &r, &c, &e); - if (ret != MPG123_OK) + n = read(fd, inbuf, sizeof(inbuf)); + if (n < 0) return -1; - *rate = r; - *channels = c; - *bits = mpg123_encsize(e) * 8; - return 0; -} + if (n == 0) + return 0; -static int -mp3read(void *buf, size_t size) -{ - size_t done; - int r; - - r = mpg123_read(handle, buf, size, &done); - if (r != MPG123_OK) - return -1; - return done; + 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 @@ -75,9 +74,7 @@ mp3exit(void) Decoder mp3decoder = { .init = mp3init, .open = mp3open, - .bufsz = mp3bufsz, - .getfmt = mp3getfmt, - .read = mp3read, + .decode = mp3decode, .close = mp3close, .exit = mp3exit }; diff --git a/sad.c b/sad.c @@ -63,11 +63,6 @@ void doaudio(void) { Song *s; - unsigned char *buf; - size_t bufsz; - long rate; - int channels, bits; - int n; s = getcursong(); if (!s) @@ -77,30 +72,13 @@ doaudio(void) return; switch (s->state) { - case READYTOPLAY: - if (curdecoder->open(s->path) < 0) - errx(1, "decoder: failed to open %s", s->path); - if (curdecoder->getfmt(&rate, &channels, &bits) < 0) - errx(1, "decoder: failed to get format"); - if (curoutput->putfmt(rate, channels, bits) < 0) - errx(1, "output: failed to put format"); - if (curoutput->open() < 0) - errx(1, "output: failed to open output device"); + case PREPARE: + curdecoder->open(s->fd); s->state = PLAYING; break; case PLAYING: - bufsz = curdecoder->bufsz(); - buf = malloc(bufsz); - if (!buf) - err(1, "malloc"); - n = curdecoder->read(buf, bufsz); - if (n < 0) - warnx("decoder: failed to decode buffer at %p of length %zu", - buf, bufsz); - else if (n > 0) - if (curoutput->write(buf, n) < 0) - warnx("output: failed to write buffer to output device"); - free(buf); + curdecoder->decode(s->fd); + break; } } diff --git a/sad.h b/sad.h @@ -8,7 +8,7 @@ typedef struct { enum { NONE, - READYTOPLAY, + PREPARE, PLAYING, PAUSED }; @@ -28,19 +28,16 @@ typedef struct { typedef struct { int (*init)(void); - int (*open)(const char *); - size_t (*bufsz)(void); - int (*getfmt)(long *, int *, int *); - int (*read)(void *, size_t); + int (*open)(int); + int (*decode)(int); int (*close)(void); void (*exit)(void); } Decoder; typedef struct { int (*init)(void); - int (*putfmt)(long, int, int); - int (*open)(void); - int (*write)(void *, size_t); + int (*open)(long, int, int); + int (*play)(void *, size_t); int (*close)(void); void (*exit)(void); } Output;