sad

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

sndio.c (1449B)


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