commit aa530b3385c427a14e9fefd0244dee526f92819d
parent fde79b74090c98dc59dfcec05c32aea45677a078
Author: sin <sin@2f30.org>
Date: Tue, 30 Dec 2014 18:10:56 +0000
Factor out playoutput()
Diffstat:
M | output.c | | | 72 | ++++++++++++++++++++++++++++++++++++++++++++---------------------------- |
M | sad.c | | | 2 | +- |
M | sad.h | | | 2 | +- |
3 files changed, 46 insertions(+), 30 deletions(-)
diff --git a/output.c b/output.c
@@ -137,46 +137,62 @@ closeoutputs(void)
return r;
}
-int
-playoutput(void *inbuf, size_t nbytes)
+static int
+playoutput(Outputdesc *desc, void *inbuf, size_t nbytes)
{
- Outputdesc *desc;
soxr_error_t e;
size_t inframes, outframes;
size_t framesize;
size_t idone, odone;
void *outbuf;
float ratio;
- int i, r = 0;
+ int i;
+
+ if (!desc->active)
+ return 0;
+
+ if (desc->inrate == desc->rate) {
+ if (desc->output->play(inbuf, nbytes) < 0)
+ return -1;
+ return 0;
+ }
+
+ /* perform SRC */
+ framesize = (desc->bits + 7) / 8 * desc->channels;
+ inframes = nbytes / framesize;
+ ratio = (float)desc->rate / desc->inrate;
+ outframes = inframes * ratio + 1;
+ outbuf = malloc(outframes * framesize);
+ if (!outbuf)
+ err(1, "malloc");
+
+ e = soxr_process(desc->resampler, inbuf, inframes,
+ &idone, outbuf, outframes,
+ &odone);
+ if (!e) {
+ if (desc->output->play(outbuf, odone * framesize) < 0)
+ return -1;
+ free(outbuf);
+ return 0;
+ }
+
+ warnx("soxr_process: failed");
+ free(outbuf);
+ return -1;
+}
+
+int
+playoutputs(void *inbuf, size_t nbytes)
+{
+ Outputdesc *desc;
+ int i, r = 0;
for (i = 0; i < LEN(outputdescs); i++) {
desc = &outputdescs[i];
if (!desc->active)
continue;
- if (desc->inrate == desc->rate) {
- if (desc->output->play(inbuf, nbytes) <0)
- r = -1;
- } else {
- framesize = (desc->bits + 7) / 8 * desc->channels;
- inframes = nbytes / framesize;
- ratio = (float)desc->rate / desc->inrate;
- outframes = inframes * ratio + 1;
- outbuf = malloc(outframes * framesize);
- if (!outbuf)
- err(1, "malloc");
-
- e = soxr_process(desc->resampler, inbuf, inframes,
- &idone, outbuf, outframes,
- &odone);
- if (e) {
- warnx("soxr_process: failed");
- free(outbuf);
- continue;
- }
- if (desc->output->play(outbuf, odone * framesize) < 0)
- r = -1;
- free(outbuf);
- }
+ if (playoutput(desc, inbuf, nbytes) < 0)
+ r = -1;
}
return r;
}
diff --git a/sad.c b/sad.c
@@ -86,7 +86,7 @@ playaudio(void)
playsong(picknextsong());
notify(EVSONGFINISHED);
} else {
- playoutput(buf, nbytes);
+ playoutputs(buf, nbytes);
}
break;
}
diff --git a/sad.h b/sad.h
@@ -128,7 +128,7 @@ int openoutputs(void);
int closeoutputs(void);
int enableoutput(const char *);
int disableoutput(const char *);
-int playoutput(void *, size_t);
+int playoutputs(void *, size_t);
int setvol(int);
/* notify.c */