Recently Written · git

sad

sad (2f30.org simple audio daemon) with a Cygwin/OSS port on branch cygwin

git clone https://github.com/equwal/sad

Log | Files | Refs


commit bbd9cf770210867313083db042728dbcda1a67d7
sin <sin@2f30.org>
2014-12-31 17:19:10 +0000

Introduce Format type and start using it

 config.def.h |  6 +++---
 mp3.c        |  8 ++++++--
 output.c     | 20 +++++++++-----------
 sad.c        |  4 ++--
 sad.h        | 13 ++++++++++---
 vorbis.c     |  8 ++++++--
 wav.c        |  8 ++++++--
 7 files changed, 42 insertions(+), 25 deletions(-)
diff --git a/config.def.h b/config.def.h
index 25be7ea..410b065 100644
--- a/config.def.h
+++ b/config.def.h
@@ -1,7 +1,7 @@
 #define RESAMPLEQUALITY SOXR_QQ
 
 Outputdesc outputdescs[] = {
-	{ "sndio", 16, 44100, 2, 0, 0, &sndiooutput, NULL, -1 },
-	{ "alsa" , 16, 44100, 2, 1, 0, &alsaoutput,  NULL, -1 },
-	{ "fifo",  16, 44100, 2, 0, 0, &fifooutput,  NULL, -1 },
+	{ "sndio", 16, 44100, 2, 0, 0, &sndiooutput, NULL },
+	{ "alsa" , 16, 44100, 2, 1, 0, &alsaoutput,  NULL },
+	{ "fifo",  16, 44100, 2, 0, 0, &fifooutput,  NULL },
 };
diff --git a/mp3.c b/mp3.c
index f99189e..e674356 100644
--- a/mp3.c
+++ b/mp3.c
@@ -26,7 +26,7 @@ mp3init(void)
 }
 
 static int
-mp3open(const char *name)
+mp3open(Format *fmt, const char *name)
 {
 	int  r;
 	long rate;
@@ -44,7 +44,11 @@ mp3open(const char *name)
 		goto err0;
 	}
 
-	if (initresamplers(rate) < 0)
+	fmt->bits = mpg123_encsize(encoding) * 8;
+	fmt->rate = rate;
+	fmt->channels = channels;
+
+	if (initresamplers(fmt) < 0)
 		goto err0;
 
 	return 0;
diff --git a/output.c b/output.c
index ed1a900..a5d091f 100644
--- a/output.c
+++ b/output.c
@@ -19,13 +19,12 @@ typedef struct {
 	int     active;
 	Output *output;
 	soxr_t  resampler;
-	int     inrate;
 } Outputdesc;
 
 #include "config.h"
 
 static int
-initresampler(Outputdesc *desc, int inrate)
+initresampler(Format *fmt, Outputdesc *desc)
 {
 	soxr_quality_spec_t quality;
 	soxr_io_spec_t      iospec;
@@ -36,7 +35,7 @@ initresampler(Outputdesc *desc, int inrate)
 
 	if (desc->resampler)
 		soxr_delete(desc->resampler);
-	desc->resampler = soxr_create(inrate, desc->rate,
+	desc->resampler = soxr_create(fmt->rate, desc->rate,
 	                              desc->channels,
 	                              NULL,
 	                              &iospec,
@@ -47,12 +46,11 @@ initresampler(Outputdesc *desc, int inrate)
 		return -1;
 	}
 
-	desc->inrate = inrate;
 	return 0;
 }
 
 int
-initresamplers(int inrate)
+initresamplers(Format *fmt)
 {
 	Outputdesc *desc;
 	int i, r = 0;
@@ -61,7 +59,7 @@ initresamplers(int inrate)
 		desc = &outputdescs[i];
 		if (!desc->enabled)
 			continue;
-		if (initresampler(desc, inrate) < 0)
+		if (initresampler(fmt, desc) < 0)
 			r = -1;
 	}
 	return r;
@@ -133,7 +131,7 @@ closeoutputs(void)
 }
 
 static int
-playoutput(Outputdesc *desc, void *inbuf, size_t nbytes)
+playoutput(Format *fmt, Outputdesc *desc, void *inbuf, size_t nbytes)
 {
 	soxr_error_t e;
 	size_t inframes, outframes;
@@ -146,7 +144,7 @@ playoutput(Outputdesc *desc, void *inbuf, size_t nbytes)
 	if (!desc->active)
 		return 0;
 
-	if (desc->inrate == desc->rate) {
+	if (desc->rate == fmt->rate) {
 		if (desc->output->play(inbuf, nbytes) < 0)
 			return -1;
 		return 0;
@@ -155,7 +153,7 @@ playoutput(Outputdesc *desc, void *inbuf, size_t nbytes)
 	/* perform SRC */
 	framesize = (desc->bits + 7) / 8 * desc->channels;
 	inframes = nbytes / framesize;
-	ratio = (float)desc->rate / desc->inrate;
+	ratio = (float)desc->rate / fmt->rate;
 	outframes = inframes * ratio + 1;
 	outbuf = malloc(outframes * framesize);
 	if (!outbuf)
@@ -177,7 +175,7 @@ playoutput(Outputdesc *desc, void *inbuf, size_t nbytes)
 }
 
 int
-playoutputs(void *inbuf, size_t nbytes)
+playoutputs(Format *fmt, void *inbuf, size_t nbytes)
 {
 	Outputdesc *desc;
 	int i, r = 0;
@@ -186,7 +184,7 @@ playoutputs(void *inbuf, size_t nbytes)
 		desc = &outputdescs[i];
 		if (!desc->enabled)
 			continue;
-		if (playoutput(desc, inbuf, nbytes) < 0)
+		if (playoutput(fmt, desc, inbuf, nbytes) < 0)
 			r = -1;
 	}
 	return r;
diff --git a/sad.c b/sad.c
index a2be8ea..268a359 100644
--- a/sad.c
+++ b/sad.c
@@ -75,7 +75,7 @@ playaudio(void)
 
 	switch (s->state) {
 	case PREPARE:
-		if (s->decoder->open(s->path) < 0) {
+		if (s->decoder->open(&s->fmt, s->path) < 0) {
 			s->state = NONE;
 			return;
 		}
@@ -86,7 +86,7 @@ playaudio(void)
 			playsong(picknextsong());
 			notify(EVSONGFINISHED);
 		} else {
-			playoutputs(buf, nbytes);
+			playoutputs(&s->fmt, buf, nbytes);
 		}
 		break;
 	}
diff --git a/sad.h b/sad.h
index 30d6f3d..e2e3eb1 100644
--- a/sad.h
+++ b/sad.h
@@ -26,9 +26,15 @@ enum {
 	EVSONGFINISHED,
 };
 
+typedef struct {
+	int bits;
+	int rate;
+	int channels;
+} Format;
+
 typedef struct {
 	int (*init)(void);
-	int (*open)(const char *);
+	int (*open)(Format *, const char *);
 	int (*decode)(void *, int);
 	int (*close)(void);
 	void (*exit)(void);
@@ -46,6 +52,7 @@ typedef struct {
 	int      id;
 	int      state;
 	Decoder *decoder;
+	Format   fmt;
 } Song;
 
 typedef struct {
@@ -123,12 +130,12 @@ int initdecoders(void);
 Decoder *matchdecoder(const char *);
 
 /* output.c */
-int initresamplers(int);
+int initresamplers(Format *);
 int openoutputs(void);
 int closeoutputs(void);
 int enableoutput(const char *);
 int disableoutput(const char *);
-int playoutputs(void *, size_t);
+int playoutputs(Format *, void *, size_t);
 int setvol(int);
 
 /* notify.c */
diff --git a/vorbis.c b/vorbis.c
index 8aa36f2..caa1bbd 100644
--- a/vorbis.c
+++ b/vorbis.c
@@ -17,7 +17,7 @@ vorbisinit(void)
 }
 
 static int
-vorbisopen(const char *name)
+vorbisopen(Format *fmt, const char *name)
 {
 	int r;
 	vorbis_info *vi;
@@ -34,7 +34,11 @@ vorbisopen(const char *name)
 		goto err0;
 	}
 
-	if (initresamplers(vi->rate) < 0)
+	fmt->bits = 16;
+	fmt->rate = vi->rate;
+	fmt->channels = vi->channels;
+
+	if (initresamplers(fmt) < 0)
 		goto err0;
 
 	return 0;
diff --git a/wav.c b/wav.c
index 6985a9d..0436357 100644
--- a/wav.c
+++ b/wav.c
@@ -17,7 +17,7 @@ wavinit(void)
 }
 
 static int
-wavopen(const char *name)
+wavopen(Format *fmt, const char *name)
 {
 	int bits;
 
@@ -51,7 +51,11 @@ wavopen(const char *name)
 		goto err0;
 	}
 
-	if (initresamplers(sfinfo.samplerate) < 0)
+	fmt->bits = bits;
+	fmt->rate = sfinfo.samplerate;
+	fmt->channels = sfinfo.channels;
+
+	if (initresamplers(fmt) < 0)
 		goto err0;
 
 	return 0;