commit 6673e4b30b2ec0720d7ebf7e0028f22c2f320362
parent efd09b125415e888997d6c5b6250500d6276b40b
Author: sin <sin@2f30.org>
Date: Thu, 25 Dec 2014 17:20:45 +0000
Handle multiple input formats (8, 16, 24 and 32 bit)
Diffstat:
M | wav.c | | | 40 | ++++++++++++++++++++++++++++++++++------ |
1 file changed, 34 insertions(+), 6 deletions(-)
diff --git a/wav.c b/wav.c
@@ -18,11 +18,34 @@ wavinit(void)
static int
wavopen(const char *name)
{
+ int bits;
+
sf = sf_open(name, SFM_READ, &sfinfo);
if (!sf) {
warnx("sf_open_fd: failed");
return -1;
}
+
+ switch (sfinfo.format & 0xff) {
+ case SF_FORMAT_PCM_S8:
+ bits = 8;
+ break;
+ case SF_FORMAT_PCM_16:
+ bits = 16;
+ break;
+ case SF_FORMAT_PCM_24:
+ bits = 24;
+ break;
+ case SF_FORMAT_PCM_32:
+ bits = 32;
+ break;
+ default:
+ warnx("sfinfo: unsupported format");
+ sf_close(sf);
+ sf = NULL;
+ return -1;
+ }
+
return output->open(16, sfinfo.samplerate, sfinfo.channels);
}
@@ -41,14 +64,19 @@ wavdecode(void)
static int
wavclose(void)
{
- int r;
+ int r = 0;
- r = sf_close(sf);
- if (r != 0) {
- warnx("sf_close: failed");
- return -1;
+ if (sf) {
+ r = sf_close(sf);
+ if (r != 0) {
+ warnx("sf_close: failed");
+ r = -1;
+ }
}
- return output->close();
+ sf = NULL;
+ if (output->close() < 0)
+ r = -1;
+ return r;
}
static void