sad

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

commit 0cb5189947908a042856c4223302e1fd49a4aa43
parent 20bee892a31cfebba550fc012a4db8a742d37529
Author: sin <sin@2f30.org>
Date:   Fri,  2 Jan 2015 18:05:30 +0000

No need for a separate init callback for decoders

Just do any required initialization in open().

Diffstat:
Mdecoder.c | 11-----------
Mmp3.c | 50+++++++++++++++++++++++---------------------------
Msad.c | 1-
Msad.h | 3---
Mvorbis.c | 15+--------------
Mwav.c | 19+++----------------
6 files changed, 27 insertions(+), 72 deletions(-)

diff --git a/decoder.c b/decoder.c @@ -18,17 +18,6 @@ static struct { { ".ogg", &vorbisdecoder }, }; -int -initdecoders(void) -{ - int i, r = 0; - - for (i = 0; i < LEN(decodermap); i++) - if (decodermap[i].decoder->init() < 0) - r = -1; - return r; -} - Decoder * matchdecoder(const char *name) { diff --git a/mp3.c b/mp3.c @@ -10,8 +10,12 @@ static mpg123_handle *hdl; static int -mp3init(void) +mp3open(Format *fmt, const char *name) { + int r; + long rate; + int channels, encoding; + if (mpg123_init() != MPG123_OK) { warnx("mpg123_init: failed"); return -1; @@ -20,28 +24,19 @@ mp3init(void) hdl = mpg123_new(NULL, NULL); if (!hdl) { warnx("mpg123_new: failed"); - return -1; + goto err0; } - return 0; -} - -static int -mp3open(Format *fmt, const char *name) -{ - int r; - long rate; - int channels, encoding; r = mpg123_open(hdl, name); if (r != MPG123_OK) { warnx("mpg123_open: failed"); - return -1; + goto err1; } r = mpg123_getformat(hdl, &rate, &channels, &encoding); if (r != MPG123_OK) { warnx("mpg123_getformat: failed"); - goto err0; + goto err2; } fmt->bits = mpg123_encsize(encoding) * 8; @@ -49,11 +44,17 @@ mp3open(Format *fmt, const char *name) fmt->channels = channels; if (initresamplers(fmt) < 0) - goto err0; + goto err2; return 0; -err0: + +err2: mpg123_close(hdl); +err1: + mpg123_delete(hdl); +err0: + mpg123_exit(); + hdl = NULL; return -1; } @@ -76,27 +77,22 @@ mp3decode(void *buf, int nbytes) static int mp3close(void) { - if (mpg123_close(hdl) != MPG123_OK) { - warnx("mpg123_close: failed"); - return -1; - } - return 0; -} + int r = 0; -static void -mp3exit(void) -{ if (hdl) { + if (mpg123_close(hdl) != MPG123_OK) { + warnx("mpg123_close: failed"); + r = -1; + } mpg123_delete(hdl); mpg123_exit(); } hdl = NULL; + return 0; } Decoder mp3decoder = { - .init = mp3init, .open = mp3open, .decode = mp3decode, - .close = mp3close, - .exit = mp3exit + .close = mp3close }; diff --git a/sad.c b/sad.c @@ -121,7 +121,6 @@ main(int argc, char *argv[]) FD_SET(listenfd, &master); fdmax = listenfd; - initdecoders(); initoutputs(); openoutputs(); initnotifier(); diff --git a/sad.h b/sad.h @@ -33,11 +33,9 @@ typedef struct { } Format; typedef struct { - int (*init)(void); int (*open)(Format *, const char *); int (*decode)(void *, int); int (*close)(void); - void (*exit)(void); } Decoder; typedef struct { @@ -116,7 +114,6 @@ int gettokens(char *, char **, int, char *); int tokenize(char *, char **, int); /* decoder.c */ -int initdecoders(void); Decoder *matchdecoder(const char *); /* output.c */ diff --git a/vorbis.c b/vorbis.c @@ -11,12 +11,6 @@ static OggVorbis_File vf; static int cursect; static int -vorbisinit(void) -{ - return 0; -} - -static int vorbisopen(Format *fmt, const char *name) { int r; @@ -75,15 +69,8 @@ vorbisclose(void) return ov_clear(&vf); } -static void -vorbisexit(void) -{ -} - Decoder vorbisdecoder = { - .init = vorbisinit, .open = vorbisopen, .decode = vorbisdecode, - .close = vorbisclose, - .exit = vorbisexit + .close = vorbisclose }; diff --git a/wav.c b/wav.c @@ -8,18 +8,12 @@ #include "sad.h" static SNDFILE *sf; -static SF_INFO sfinfo; - -static int -wavinit(void) -{ - return 0; -} static int wavopen(Format *fmt, const char *name) { - int bits; + SF_INFO sfinfo; + int bits; sf = sf_open(name, SFM_READ, &sfinfo); if (!sf) { @@ -87,15 +81,8 @@ wavclose(void) return r; } -static void -wavexit(void) -{ -} - Decoder wavdecoder = { - .init = wavinit, .open = wavopen, .decode = wavdecode, - .close = wavclose, - .exit = wavexit + .close = wavclose };