sad

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

vorbis.c (1076B)


      1 #include <sys/select.h>
      2 
      3 #include <err.h>
      4 #include <limits.h>
      5 #include <stdio.h>
      6 #include <vorbis/vorbisfile.h>
      7 
      8 #include "sad.h"
      9 
     10 static OggVorbis_File vf;
     11 static int cursect;
     12 
     13 static int
     14 vorbisopen(Format *fmt, const char *name)
     15 {
     16 	int r;
     17 	vorbis_info *vi;
     18 
     19 	r = ov_fopen(name, &vf);
     20 	if (r < 0) {
     21 		warnx("ov_fopen: failed");
     22 		return -1;
     23 	}
     24 
     25 	vi = ov_info(&vf, -1);
     26 	if (!vi) {
     27 		warnx("ov_info: failed");
     28 		goto err0;
     29 	}
     30 
     31 	fmt->bits = 16;
     32 	fmt->rate = vi->rate;
     33 	fmt->channels = vi->channels;
     34 
     35 	if (initresamplers(fmt) < 0)
     36 		goto err0;
     37 
     38 	return 0;
     39 
     40 err0:
     41 	ov_clear(&vf);
     42 	return -1;
     43 }
     44 
     45 static int
     46 vorbisdecode(void *buf, int nbytes)
     47 {
     48 	int   r, decoded;
     49 	char *p = buf;
     50 
     51 	decoded = 0;
     52 	while (nbytes > 0) {
     53 		r = ov_read(&vf, &p[decoded], nbytes, 0, 2, 1, &cursect);
     54 		if (r < 0) {
     55 			warnx("ov_read: failed");
     56 			return -1;
     57 		} else if (r == 0) {
     58 			break;
     59 		}
     60 		decoded += r;
     61 		nbytes -= r;
     62 	}
     63 	return decoded;
     64 }
     65 
     66 static int
     67 vorbisclose(void)
     68 {
     69 	return ov_clear(&vf);
     70 }
     71 
     72 Decoder vorbisdecoder = {
     73 	.open = vorbisopen,
     74 	.decode = vorbisdecode,
     75 	.close = vorbisclose
     76 };