Debian Codegraph @

3rdparty/iaxclient/lib/portmixer/px_unix_oss/px_unix_oss.c

raw symbols SCIP
1 /*
2 * PortMixer
3 * Unix OSS Implementation
4 *
5 * Copyright (c) 2002
6 *
7 * Written by Dominic Mazzoni
8 *
9 * PortMixer is intended to work side-by-side with PortAudio,
10 * the Portable Real-Time Audio Library by Ross Bencina and
11 * Phil Burk.
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining
14 * a copy of this software and associated documentation files
15 * (the "Software"), to deal in the Software without restriction,
16 * including without limitation the rights to use, copy, modify, merge,
17 * publish, distribute, sublicense, and/or sell copies of the Software,
18 * and to permit persons to whom the Software is furnished to do so,
19 * subject to the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be
22 * included in all copies or substantial portions of the Software.
23 *
24 * Any person wishing to distribute modifications to the Software is
25 * requested to send the modifications to the original developer so that
26 * they can be incorporated into the canonical version.
27 *
28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
31 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
32 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
33 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 *
36 */
37
38 #if defined(__linux__)
39 #include <linux/soundcard.h>
40 #elif defined(__FreeBSD__)
41 #include <sys/soundcard.h>
42 #else
43 #include <machine/soundcard.h> /* JH20010905 */
44 #endif
45
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <fcntl.h>
49 #include <unistd.h>
50 #include <sys/types.h>
51 #include <sys/stat.h>
52 #include <sys/ioctl.h>
53
54 #include "portaudio.h"
55 #include "portmixer.h"
56
57 typedef struct PxInfo
58 {
59 int index;
60 int fd;
61
62 int num_out;
63 int outs[SOUND_MIXER_NRDEVICES];
64 int num_rec;
65 int recs[SOUND_MIXER_NRDEVICES];
66 } PxInfo;
67
68 char PxDevice[20] = "/dev/mixerX";
69
70 int PxNumDevices = 0;
71 int PxDevices[10];
72
73 int Px_GetNumMixers( void *pa_stream )
74 {
75 int i;
76 int fd;
77
78 PxNumDevices = 0;
79
80 for(i=0; i<11; i++) {
81 if (i==0)
82 PxDevice[10] = 0;
83 else
84 PxDevice[10] = '0'+(i-1);
85 fd = open(PxDevice, O_RDWR);
86 if (fd >= 0) {
87 PxDevices[PxNumDevices] = i;
88 PxNumDevices++;
89 close(fd);
90 }
91 }
92
93 return PxNumDevices;
94 }
95
96 const char *Px_GetMixerName( void *pa_stream, int index )
97 {
98 if (PxNumDevices <= 0)
99 Px_GetNumMixers(pa_stream);
100
101 if (index < 0 || index >= PxNumDevices)
102 return NULL;
103
104 if (PxDevices[index]==0)
105 PxDevice[10] = 0;
106 else
107 PxDevice[10] = '0'+(PxDevices[index]-1);
108 return PxDevice;
109 }
110
111 PxMixer *Px_OpenMixer( void *pa_stream, int index )
112 {
113 PxInfo *info;
114 int devmask, recmask, outmask;
115 int i;
116
117 if (PxNumDevices <= 0)
118 Px_GetNumMixers(pa_stream);
119
120 if (index < 0 || index >= PxNumDevices)
121 return NULL;
122
123 info = (PxInfo *)malloc(sizeof(PxInfo));
124 if (info == NULL)
125 return NULL;
126
127 info->index = PxDevice[index];
128
129 if (PxDevices[index]==0)
130 PxDevice[10] = 0;
131 else
132 PxDevice[10] = '0'+(PxDevices[index]-1);
133 info->fd = open(PxDevice, O_RDWR);
134 if (info->fd < 0)
135 goto bad;
136
137 if (ioctl(info->fd, MIXER_READ(SOUND_MIXER_READ_DEVMASK),
138 &devmask) == -1)
139 goto bad;
140 if (ioctl(info->fd, MIXER_READ(SOUND_MIXER_READ_RECMASK),
141 &recmask) == -1)
142 goto bad;
143 outmask = devmask ^ recmask;
144
145 info->num_out = 0;
146 info->num_rec = 0;
147
148 for(i=0; i<SOUND_MIXER_NRDEVICES; i++)
149 if (recmask & (1<<i))
150 info->recs[info->num_rec++] = i;
151 else if (devmask & (1<<i))
152 info->outs[info->num_out++] = i;
153
154 return (PxMixer *)info;
155
156 bad:
157 free(info);
158 return NULL;
159 }
160
161 /*
162 Px_CloseMixer() closes a mixer opened using Px_OpenMixer and frees any
163 memory associated with it.
164 */
165
166 void Px_CloseMixer(PxMixer *mixer)
167 {
168 PxInfo *info = (PxInfo *)mixer;
169
170 close(info->fd);
171
172 free(info);
173 }
174
175 PxVolume GetVolume(int fd, int channel)
176 {
177 int vol;
178 int stereo;
179
180 if (ioctl(fd, SOUND_MIXER_READ_STEREODEVS, &stereo) == 0)
181 stereo = ((stereo & (1 << channel)) != 0);
182 else
183 stereo = 0;
184
185 if (ioctl(fd, MIXER_READ(channel), &vol) == -1)
186 return 0.0;
187
188 if (stereo)
189 return ((vol & 0xFF)/200.0) + (((vol>>8) & 0xFF)/200.0);
190 else
191 return (vol & 0xFF)/100.0;
192 }
193
194 /*
195 Master (output) volume
196 */
197
198 PxVolume Px_GetMasterVolume( PxMixer *mixer )
199 {
200 PxInfo *info = (PxInfo *)mixer;
201
202 return GetVolume(info->fd, SOUND_MIXER_VOLUME);
203 }
204
205 void Px_SetMasterVolume( PxMixer *mixer, PxVolume volume )
206 {
207 PxInfo *info = (PxInfo *)mixer;
208
209 int vol = (int)((volume * 100.0) + 0.5);
210 vol = (vol | (vol<<8));
211 ioctl(info->fd, MIXER_WRITE(SOUND_MIXER_VOLUME), &vol);
212 }
213
214 /*
215 PCM output volume
216 */
217
218 int Px_SupportsPCMOutputVolume( PxMixer* mixer )
219 {
220 return 1 ;
221 }
222
223 PxVolume Px_GetPCMOutputVolume( PxMixer *mixer )
224 {
225 PxInfo *info = (PxInfo *)mixer;
226
227 return GetVolume(info->fd, SOUND_MIXER_PCM);
228 }
229
230 void Px_SetPCMOutputVolume( PxMixer *mixer, PxVolume volume )
231 {
232 PxInfo *info = (PxInfo *)mixer;
233
234 int vol = (int)((volume * 100.0) + 0.5);
235 vol = (vol | (vol<<8));
236 ioctl(info->fd, MIXER_WRITE(SOUND_MIXER_PCM), &vol);
237 }
238
239 /*
240 All output volumes
241 */
242
243 int Px_GetNumOutputVolumes( PxMixer *mixer )
244 {
245 PxInfo *info = (PxInfo *)mixer;
246
247 return info->num_out;
248 }
249
250 const char *Px_GetOutputVolumeName( PxMixer *mixer, int i )
251 {
252 PxInfo *info = (PxInfo *)mixer;
253 const char *labels[] = SOUND_DEVICE_LABELS;
254
255 return labels[info->outs[i]];
256 }
257
258 PxVolume Px_GetOutputVolume( PxMixer *mixer, int i )
259 {
260 PxInfo *info = (PxInfo *)mixer;
261
262 return GetVolume(info->fd, info->outs[i]);
263 }
264
265 void Px_SetOutputVolume( PxMixer *mixer, int i, PxVolume volume )
266 {
267 PxInfo *info = (PxInfo *)mixer;
268
269 int vol = (int)((volume * 100.0) + 0.5);
270 vol = (vol | (vol<<8));
271 ioctl(info->fd, MIXER_WRITE(info->outs[i]), &vol);
272 }
273
274 /*
275 Input sources
276 */
277
278 int Px_GetNumInputSources( PxMixer *mixer )
279 {
280 PxInfo *info = (PxInfo *)mixer;
281
282 return info->num_rec;
283 }
284
285 const char *Px_GetInputSourceName( PxMixer *mixer, int i)
286 {
287 PxInfo *info = (PxInfo *)mixer;
288
289 const char *labels[] = SOUND_DEVICE_LABELS;
290 return labels[info->recs[i]];
291 }
292
293 int Px_GetCurrentInputSource( PxMixer *mixer )
294 {
295 PxInfo *info = (PxInfo *)mixer;
296 int recmask;
297 int i;
298
299 /* Note that there may be more than one in OSS; we pick
300 the first one */
301
302 if (ioctl(info->fd, MIXER_READ(SOUND_MIXER_READ_RECSRC),
303 &recmask) == -1)
304 return -1; /* none / error */
305
306 for(i=0; i<info->num_rec; i++)
307 if (recmask & (1 << (info->recs[i])))
308 return i;
309
310 return -1; /* none */
311 }
312
313 void Px_SetCurrentInputSource( PxMixer *mixer, int i )
314 {
315 PxInfo *info = (PxInfo *)mixer;
316 int newrecsrcmask = (1 << (info->recs[i]));
317
318 ioctl(info->fd, MIXER_WRITE(SOUND_MIXER_READ_RECSRC),
319 &newrecsrcmask);
320 }
321
322 /*
323 Input volume
324 */
325
326 PxVolume Px_GetInputVolume( PxMixer *mixer )
327 {
328 PxInfo *info = (PxInfo *)mixer;
329 int i;
330
331 i = Px_GetCurrentInputSource(mixer);
332 if (i < 0)
333 return 0.0;
334
335 return GetVolume(info->fd, info->recs[i]);
336 }
337
338 void Px_SetInputVolume( PxMixer *mixer, PxVolume volume )
339 {
340 PxInfo *info = (PxInfo *)mixer;
341 int vol;
342 int i;
343
344 i = Px_GetCurrentInputSource(mixer);
345 if (i < 0)
346 return;
347
348 vol = (int)((volume * 100.0) + 0.5);
349 vol = (vol | (vol<<8));
350 ioctl(info->fd, MIXER_WRITE(info->recs[i]), &vol);
351 }
352
353 /*
354 Balance
355 */
356
357 int Px_SupportsOutputBalance( PxMixer *mixer )
358 {
359 return 0;
360 }
361
362 PxBalance Px_GetOutputBalance( PxMixer *mixer )
363 {
364 return 0.0;
365 }
366
367 void Px_SetOutputBalance( PxMixer *mixer, PxBalance balance )
368 {
369 }
370
371 /*
372 Playthrough
373 */
374
375 int Px_SupportsPlaythrough( PxMixer *mixer )
376 {
377 return 0;
378 }
379
380 PxVolume Px_GetPlaythrough( PxMixer *mixer )
381 {
382 return 0.0;
383 }
384
385 void Px_SetPlaythrough( PxMixer *mixer, PxVolume volume )
386 {
387 }
388
389
390 /*
391 unimplemented stubs
392 */
393
394 int Px_SetMicrophoneBoost( PxMixer* mixer, int enable )
395 {
396 return 1 ;
397 }
398
399 int Px_GetMicrophoneBoost( PxMixer* mixer )
400 {
401 return -1 ;
402 }
403
404 int Px_SetCurrentInputSourceByName( PxMixer* mixer, const char* line_name )
405 {
406 return 1 ;
407 }