sad

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

sndio.c (1301B)


      1 #include <sys/select.h>
      2 
      3 #include <err.h>
      4 #include <limits.h>
      5 #include <stdio.h>
      6 #include <sndio.h>
      7 
      8 #include "sad.h"
      9 
     10 static struct sio_hdl *hdl;
     11 
     12 static int
     13 sndiovol(int vol)
     14 {
     15 	if (!sio_setvol(hdl, (SIO_MAXVOL * vol) / 100)) {
     16 		warnx("sio_setvol: failed");
     17 		return -1;
     18 	}
     19 	return 0;
     20 }
     21 
     22 static int
     23 sndioopen(Format *fmt)
     24 {
     25 	struct sio_par par;
     26 
     27 	hdl = sio_open(SIO_DEVANY, SIO_PLAY, 0);
     28 	if (!hdl) {
     29 		warnx("sio_open: failed");
     30 		return -1;
     31 	}
     32 
     33 	sio_initpar(&par);
     34 	par.bits = fmt->bits;
     35 	par.rate = fmt->rate;
     36 	par.pchan = fmt->channels;
     37 	par.sig = 1;
     38 	par.le = SIO_LE_NATIVE;
     39 
     40 	if (!sio_setpar(hdl, &par) || !sio_getpar(hdl, &par)) {
     41 		warnx("sio_{set,get}par: failed");
     42 		goto err0;
     43 	}
     44 
     45 	if (par.bits != fmt->bits || par.rate != fmt->rate ||
     46 	    par.pchan != fmt->channels || par.le != SIO_LE_NATIVE ||
     47 	    par.sig != 1) {
     48 		warnx("unsupported audio params");
     49 		goto err0;
     50 	}
     51 
     52 	if (!sio_start(hdl)) {
     53 		warnx("sio_start: failed");
     54 		goto err0;
     55 	}
     56 
     57 	return 0;
     58 
     59 err0:
     60 	sio_close(hdl);
     61 	hdl = NULL;
     62 	return -1;
     63 }
     64 
     65 static int
     66 sndioplay(void *buf, size_t nbytes)
     67 {
     68 	return sio_write(hdl, buf, nbytes);
     69 }
     70 
     71 static int
     72 sndioclose(void)
     73 {
     74 	if (hdl)
     75 		sio_close(hdl);
     76 	hdl = NULL;
     77 	return 0;
     78 }
     79 
     80 Output sndiooutput = {
     81 	.vol = sndiovol,
     82 	.open = sndioopen,
     83 	.play = sndioplay,
     84 	.close = sndioclose,
     85 };