sad

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

wav.c (1313B)


      1 #include <sys/select.h>
      2 
      3 #include <err.h>
      4 #include <limits.h>
      5 #include <stdio.h>
      6 #include <sndfile.h>
      7 
      8 #include "sad.h"
      9 
     10 static SNDFILE *sf;
     11 
     12 static int
     13 wavopen(Format *fmt, const char *name)
     14 {
     15 	SF_INFO sfinfo;
     16 	int     bits;
     17 
     18 	sf = sf_open(name, SFM_READ, &sfinfo);
     19 	if (!sf) {
     20 		warnx("sf_open_fd: failed");
     21 		return -1;
     22 	}
     23 
     24 	if (sfinfo.channels < 1 || sfinfo.channels > 2) {
     25 		warnx("sfinfo: unsupported number of channels %d",
     26 		      sfinfo.channels);
     27 		goto err0;
     28 	}
     29 
     30 	switch (sfinfo.format & 0xff) {
     31 	case SF_FORMAT_PCM_S8:
     32 		bits = 8;
     33 		break;
     34 	case SF_FORMAT_PCM_16:
     35 		bits = 16;
     36 		break;
     37 	case SF_FORMAT_PCM_24:
     38 		bits = 24;
     39 		break;
     40 	case SF_FORMAT_PCM_32:
     41 		bits = 32;
     42 		break;
     43 	default:
     44 		warnx("sfinfo: unsupported format");
     45 		goto err0;
     46 	}
     47 
     48 	fmt->bits = bits;
     49 	fmt->rate = sfinfo.samplerate;
     50 	fmt->channels = sfinfo.channels;
     51 
     52 	if (initresamplers(fmt) < 0)
     53 		goto err0;
     54 
     55 	return 0;
     56 err0:
     57 	sf_close(sf);
     58 	sf = NULL;
     59 	return -1;
     60 }
     61 
     62 static int
     63 wavdecode(void *buf, int nbytes)
     64 {
     65 	return sf_read_short(sf, buf, nbytes / sizeof(short)) * sizeof(short);
     66 }
     67 
     68 static int
     69 wavclose(void)
     70 {
     71 	int r = 0;
     72 
     73 	if (sf) {
     74 		r = sf_close(sf);
     75 		if (r != 0) {
     76 			warnx("sf_close: failed");
     77 			r = -1;
     78 		}
     79 	}
     80 	sf = NULL;
     81 	return r;
     82 }
     83 
     84 Decoder wavdecoder = {
     85 	.open = wavopen,
     86 	.decode = wavdecode,
     87 	.close = wavclose
     88 };