4 This file is part of polypaudio.
6 polypaudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
11 polypaudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
27 #include <sys/soundcard.h>
28 #include <sys/ioctl.h>
33 #include <sys/types.h>
39 int pa_oss_open(const char *device, int *mode, int* pcaps) {
41 assert(device && mode && (*mode == O_RDWR || *mode == O_RDONLY || *mode == O_WRONLY));
43 if (*mode == O_RDWR) {
44 if ((fd = open(device, O_RDWR|O_NDELAY)) >= 0) {
46 ioctl(fd, SNDCTL_DSP_SETDUPLEX, 0);
48 tcaps = pcaps ? pcaps : &dcaps;
50 if (ioctl(fd, SNDCTL_DSP_GETCAPS, tcaps) < 0) {
51 fprintf(stderr, __FILE__": SNDCTL_DSP_GETCAPS: %s\n", strerror(errno));
55 if (*tcaps & DSP_CAP_DUPLEX)
61 if ((fd = open(device, (*mode = O_WRONLY)|O_NDELAY)) < 0) {
62 if ((fd = open(device, (*mode = O_RDONLY)|O_NDELAY)) < 0) {
63 fprintf(stderr, __FILE__": open('%s'): %s\n", device, strerror(errno));
68 if ((fd = open(device, *mode|O_NDELAY)) < 0) {
69 fprintf(stderr, __FILE__": open('%s'): %s\n", device, strerror(errno));
75 if (ioctl(fd, SNDCTL_DSP_GETCAPS, pcaps) < 0) {
76 fprintf(stderr, "SNDCTL_DSP_GETCAPS: %s\n", strerror(errno));
89 int pa_oss_auto_format(int fd, struct pa_sample_spec *ss) {
90 int format, channels, speed, reqformat;
91 static const int format_trans[] = {
92 [PA_SAMPLE_U8] = AFMT_U8,
93 [PA_SAMPLE_ALAW] = AFMT_A_LAW,
94 [PA_SAMPLE_ULAW] = AFMT_MU_LAW,
95 [PA_SAMPLE_S16LE] = AFMT_S16_LE,
96 [PA_SAMPLE_S16BE] = AFMT_S16_BE,
97 [PA_SAMPLE_FLOAT32LE] = AFMT_QUERY, /* not supported */
98 [PA_SAMPLE_FLOAT32BE] = AFMT_QUERY, /* not supported */
101 assert(fd >= 0 && ss);
103 reqformat = format = format_trans[ss->format];
104 if (reqformat == AFMT_QUERY || ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != reqformat) {
105 format = AFMT_S16_NE;
106 if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != AFMT_S16_NE) {
107 int f = AFMT_S16_NE == AFMT_S16_LE ? AFMT_S16_BE : AFMT_S16_LE;
109 if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != f) {
111 if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != AFMT_U8) {
112 fprintf(stderr, "SNDCTL_DSP_SETFMT: %s\n", format != AFMT_U8 ? "No supported sample format" : strerror(errno));
115 ss->format = PA_SAMPLE_U8;
117 ss->format = f == AFMT_S16_LE ? PA_SAMPLE_S16LE : PA_SAMPLE_S16BE;
119 ss->format = PA_SAMPLE_S16NE;
122 channels = ss->channels;
123 if (ioctl(fd, SNDCTL_DSP_CHANNELS, &channels) < 0) {
124 fprintf(stderr, "SNDCTL_DSP_CHANNELS: %s\n", strerror(errno));
128 ss->channels = channels;
131 if (ioctl(fd, SNDCTL_DSP_SPEED, &speed) < 0) {
132 fprintf(stderr, "SNDCTL_DSP_SPEED: %s\n", strerror(errno));
141 static int log2(int v) {
153 int pa_oss_set_fragments(int fd, int nfrags, int frag_size) {
155 arg = ((int) nfrags << 16) | log2(frag_size);
157 if (ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &arg) < 0) {
158 fprintf(stderr, "SNDCTL_DSP_SETFRAGMENT: %s\n", strerror(errno));