oss.c (1255 bytes)
1 #include <sys/ioctl.h> 2 #include <sys/select.h> 3 #include <sys/soundcard.h> 4 5 #include <err.h> 6 #include <fcntl.h> 7 #include <limits.h> 8 #include <stdio.h> 9 #include <unistd.h> 10 11 #include "sad.h" 12 13 static int ossfd = -1; 14 int ossvolstatus = DEFAULTVOL; 15 16 static int ossvol(int vol) { 17 ossvolstatus = vol; 18 return 0; 19 } 20 21 static int ossopen(Format *fmt) { 22 int afmt = AFMT_S16_LE, chans = fmt->channels, rate = fmt->rate; 23 24 ossfd = open("/dev/dsp", O_WRONLY); 25 if (ossfd < 0) { 26 warn("open /dev/dsp"); 27 return -1; 28 } 29 if (ioctl(ossfd, SNDCTL_DSP_SETFMT, &afmt) < 0 || 30 ioctl(ossfd, SNDCTL_DSP_CHANNELS, &chans) < 0 || 31 ioctl(ossfd, SNDCTL_DSP_SPEED, &rate) < 0) { 32 warn("ioctl /dev/dsp"); 33 goto err0; 34 } 35 if (afmt != AFMT_S16_LE || chans != (int)fmt->channels || 36 rate != (int)fmt->rate) { 37 warnx("unsupported audio params"); 38 goto err0; 39 } 40 return 0; 41 42 err0: 43 close(ossfd); 44 ossfd = -1; 45 return -1; 46 } 47 48 static int ossplay(void *buf, size_t nbytes) { 49 return write(ossfd, buf, nbytes); 50 } 51 52 static int ossclose(void) { 53 if (ossfd != -1) 54 close(ossfd); 55 ossfd = -1; 56 return 0; 57 } 58 59 Output ossoutput = { 60 .volstatus = &ossvolstatus, 61 .vol = ossvol, 62 .open = ossopen, 63 .play = ossplay, 64 .close = ossclose, 65 };