]> code.delx.au - pulseaudio/blob - polyp/oss-util.c
rename src to polyp
[pulseaudio] / polyp / oss-util.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
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.
10
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.
15
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
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <assert.h>
27 #include <sys/soundcard.h>
28 #include <sys/ioctl.h>
29 #include <stdio.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <fcntl.h>
36
37 #include "oss-util.h"
38
39 int pa_oss_open(const char *device, int *mode, int* pcaps) {
40 int fd = -1;
41 assert(device && mode && (*mode == O_RDWR || *mode == O_RDONLY || *mode == O_WRONLY));
42
43 if (*mode == O_RDWR) {
44 if ((fd = open(device, O_RDWR|O_NDELAY)) >= 0) {
45 int dcaps, *tcaps;
46 ioctl(fd, SNDCTL_DSP_SETDUPLEX, 0);
47
48 tcaps = pcaps ? pcaps : &dcaps;
49
50 if (ioctl(fd, SNDCTL_DSP_GETCAPS, tcaps) < 0) {
51 fprintf(stderr, __FILE__": SNDCTL_DSP_GETCAPS: %s\n", strerror(errno));
52 goto fail;
53 }
54
55 if (*tcaps & DSP_CAP_DUPLEX)
56 return fd;
57
58 close(fd);
59 }
60
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));
64 goto fail;
65 }
66 }
67 } else {
68 if ((fd = open(device, *mode|O_NDELAY)) < 0) {
69 fprintf(stderr, __FILE__": open('%s'): %s\n", device, strerror(errno));
70 goto fail;
71 }
72 }
73
74 if (pcaps) {
75 if (ioctl(fd, SNDCTL_DSP_GETCAPS, pcaps) < 0) {
76 fprintf(stderr, "SNDCTL_DSP_GETCAPS: %s\n", strerror(errno));
77 goto fail;
78 }
79 }
80
81 return fd;
82
83 fail:
84 if (fd >= 0)
85 close(fd);
86 return fd;
87 }
88
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 */
99 };
100
101 assert(fd >= 0 && ss);
102
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;
108 format = f;
109 if (ioctl(fd, SNDCTL_DSP_SETFMT, &format) < 0 || format != f) {
110 format = AFMT_U8;
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));
113 return -1;
114 } else
115 ss->format = PA_SAMPLE_U8;
116 } else
117 ss->format = f == AFMT_S16_LE ? PA_SAMPLE_S16LE : PA_SAMPLE_S16BE;
118 } else
119 ss->format = PA_SAMPLE_S16NE;
120 }
121
122 channels = ss->channels;
123 if (ioctl(fd, SNDCTL_DSP_CHANNELS, &channels) < 0) {
124 fprintf(stderr, "SNDCTL_DSP_CHANNELS: %s\n", strerror(errno));
125 return -1;
126 }
127 assert(channels);
128 ss->channels = channels;
129
130 speed = ss->rate;
131 if (ioctl(fd, SNDCTL_DSP_SPEED, &speed) < 0) {
132 fprintf(stderr, "SNDCTL_DSP_SPEED: %s\n", strerror(errno));
133 return -1;
134 }
135 assert(speed);
136 ss->rate = speed;
137
138 return 0;
139 }
140
141 static int log2(int v) {
142 int k = 0;
143
144 for (;;) {
145 v >>= 1;
146 if (!v) break;
147 k++;
148 }
149
150 return k;
151 }
152
153 int pa_oss_set_fragments(int fd, int nfrags, int frag_size) {
154 int arg;
155 arg = ((int) nfrags << 16) | log2(frag_size);
156
157 if (ioctl(fd, SNDCTL_DSP_SETFRAGMENT, &arg) < 0) {
158 fprintf(stderr, "SNDCTL_DSP_SETFRAGMENT: %s\n", strerror(errno));
159 return -1;
160 }
161
162 return 0;
163 }