Allow auxiliary effect slot 0 when (un)setting the source auxiliary send
[openal-soft.git] / Alc / oss.c
blobde8f6e43674ffa7701e15b518df946fd5c026de0
1 /**
2 * OpenAL cross platform audio library
3 * Copyright (C) 1999-2007 by authors.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 * Or go to http://www.gnu.org/copyleft/lgpl.html
21 #include <sys/ioctl.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <memory.h>
28 #include <unistd.h>
29 #include <errno.h>
30 #include <math.h>
31 #include "alMain.h"
32 #include "AL/al.h"
33 #include "AL/alc.h"
35 #include <sys/soundcard.h>
38 * The OSS documentation talks about SOUND_MIXER_READ, but the header
39 * only contains MIXER_READ. Play safe. Same for WRITE.
41 #ifndef SOUND_MIXER_READ
42 #define SOUND_MIXER_READ MIXER_READ
43 #endif
44 #ifndef SOUND_MIXER_WRITE
45 #define SOUND_MIXER_WRITE MIXER_WRITE
46 #endif
48 static char *oss_device;
49 static char *oss_device_capture;
51 typedef struct {
52 int fd;
53 int killNow;
54 ALvoid *thread;
56 ALubyte *mix_data;
57 int data_size;
58 int silence;
60 RingBuffer *ring;
61 int doCapture;
62 } oss_data;
65 static int log2i(ALCuint x)
67 int y = 0;
68 while (x > 1)
70 x >>= 1;
71 y++;
73 return y;
77 static ALuint OSSProc(ALvoid *ptr)
79 ALCdevice *pDevice = (ALCdevice*)ptr;
80 oss_data *data = (oss_data*)pDevice->ExtraData;
81 int remaining;
82 int wrote;
84 while(!data->killNow)
86 SuspendContext(NULL);
87 aluMixData(pDevice->Context,data->mix_data,data->data_size,pDevice->Format);
88 ProcessContext(NULL);
90 remaining = data->data_size;
91 while(remaining > 0)
93 wrote = write(data->fd, data->mix_data+data->data_size-remaining, remaining);
94 if(wrote < 0)
96 AL_PRINT("write failed: %s\n", strerror(errno));
97 break;
99 if(wrote == 0)
101 usleep(1000);
102 continue;
104 remaining -= wrote;
108 return 0;
111 static ALuint OSSCaptureProc(ALvoid *ptr)
113 ALCdevice *pDevice = (ALCdevice*)ptr;
114 oss_data *data = (oss_data*)pDevice->ExtraData;
115 int frameSize;
116 int amt;
118 frameSize = aluBytesFromFormat(pDevice->Format);
119 frameSize *= aluChannelsFromFormat(pDevice->Format);
121 while(!data->killNow)
123 amt = read(data->fd, data->mix_data, data->data_size);
124 if(amt < 0)
126 AL_PRINT("read failed: %s\n", strerror(errno));
127 break;
129 if(amt == 0)
131 usleep(1000);
132 continue;
134 if(data->doCapture)
135 WriteRingBuffer(data->ring, data->mix_data, amt/frameSize);
138 return 0;
141 static ALCboolean oss_open_playback(ALCdevice *device, const ALCchar *deviceName)
143 int numFragmentsLogSize;
144 int log2FragmentSize;
145 unsigned int periods;
146 audio_buf_info info;
147 char driver[64];
148 int numChannels;
149 oss_data *data;
150 int ossFormat;
151 int ossSpeed;
152 char *err;
153 int i;
155 strncpy(driver, GetConfigValue("oss", "device", "/dev/dsp"), sizeof(driver)-1);
156 driver[sizeof(driver)-1] = 0;
157 if(deviceName)
159 if(strcmp(deviceName, oss_device))
160 return ALC_FALSE;
161 device->szDeviceName = oss_device;
163 else
164 device->szDeviceName = oss_device;
166 data = (oss_data*)calloc(1, sizeof(oss_data));
167 data->killNow = 0;
169 data->fd = open(driver, O_WRONLY);
170 if(data->fd == -1)
172 free(data);
173 AL_PRINT("Could not open %s: %s\n", driver, strerror(errno));
174 return ALC_FALSE;
177 switch(device->Format)
179 case AL_FORMAT_MONO8:
180 case AL_FORMAT_STEREO8:
181 case AL_FORMAT_QUAD8:
182 data->silence = 0x80;
183 ossFormat = AFMT_U8;
184 break;
185 case AL_FORMAT_MONO16:
186 case AL_FORMAT_STEREO16:
187 case AL_FORMAT_QUAD16:
188 data->silence = 0;
189 ossFormat = AFMT_S16_NE;
190 break;
191 default:
192 ossFormat = -1;
193 AL_PRINT("Unknown format?! %x\n", device->Format);
196 periods = GetConfigValueInt("oss", "periods", 4);
197 if((int)periods < 0)
198 periods = 4;
199 numChannels = device->Channels;
200 ossSpeed = device->Frequency;
201 log2FragmentSize = log2i(device->UpdateFreq * device->FrameSize / periods);
203 /* according to the OSS spec, 16 bytes are the minimum */
204 if (log2FragmentSize < 4)
205 log2FragmentSize = 4;
206 numFragmentsLogSize = (periods << 16) | log2FragmentSize;
208 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
209 if (!(ok(ioctl(data->fd, SNDCTL_DSP_SETFRAGMENT, &numFragmentsLogSize), "set fragment") &&
210 ok(ioctl(data->fd, SNDCTL_DSP_SETFMT, &ossFormat), "set format") &&
211 ok(ioctl(data->fd, SNDCTL_DSP_CHANNELS, &numChannels), "set channels") &&
212 ok(ioctl(data->fd, SNDCTL_DSP_SPEED, &ossSpeed), "set speed") &&
213 ok(ioctl(data->fd, SNDCTL_DSP_GETOSPACE, &info), "get space")))
215 AL_PRINT("%s failed: %s\n", err, strerror(errno));
216 close(data->fd);
217 free(data);
218 return ALC_FALSE;
220 #undef ok
222 device->Channels = numChannels;
223 device->Frequency = ossSpeed;
224 device->Format = 0;
225 if(ossFormat == AFMT_U8)
227 if(device->Channels == 1)
229 device->Format = AL_FORMAT_MONO8;
230 device->FrameSize = 1;
232 else if(device->Channels == 2)
234 device->Format = AL_FORMAT_STEREO8;
235 device->FrameSize = 2;
237 else if(device->Channels == 4)
239 device->Format = AL_FORMAT_QUAD8;
240 device->FrameSize = 4;
243 else if(ossFormat == AFMT_S16_NE)
245 if(device->Channels == 1)
247 device->Format = AL_FORMAT_MONO16;
248 device->FrameSize = 2;
250 else if(device->Channels == 2)
252 device->Format = AL_FORMAT_STEREO16;
253 device->FrameSize = 4;
255 else if(device->Channels == 4)
257 device->Format = AL_FORMAT_QUAD16;
258 device->FrameSize = 8;
262 if(!device->Format)
264 AL_PRINT("returned unknown format: %#x %d!\n", ossFormat, numChannels);
265 close(data->fd);
266 free(data);
267 return ALC_FALSE;
270 device->UpdateFreq = info.fragsize / device->FrameSize;
272 data->data_size = device->UpdateFreq * device->FrameSize;
273 data->mix_data = calloc(1, data->data_size);
275 device->MaxNoOfSources = 256;
277 device->ExtraData = data;
278 data->thread = StartThread(OSSProc, device);
279 if(data->thread == NULL)
281 device->ExtraData = NULL;
282 free(data->mix_data);
283 free(data);
284 return ALC_FALSE;
287 return ALC_TRUE;
290 static void oss_close_playback(ALCdevice *device)
292 oss_data *data = (oss_data*)device->ExtraData;
293 data->killNow = 1;
294 StopThread(data->thread);
296 close(data->fd);
298 free(data->mix_data);
299 free(data);
300 device->ExtraData = NULL;
304 static ALCboolean oss_open_capture(ALCdevice *device, const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
306 int numFragmentsLogSize;
307 int log2FragmentSize;
308 unsigned int periods;
309 audio_buf_info info;
310 int numChannels;
311 char driver[64];
312 oss_data *data;
313 int ossFormat;
314 int ossSpeed;
315 char *err;
316 int i;
318 strncpy(driver, GetConfigValue("oss", "capture", "/dev/dsp"), sizeof(driver)-1);
319 driver[sizeof(driver)-1] = 0;
320 if(deviceName)
322 if(strcmp(deviceName, oss_device_capture))
323 return ALC_FALSE;
324 device->szDeviceName = oss_device_capture;
326 else
327 device->szDeviceName = oss_device_capture;
329 data = (oss_data*)calloc(1, sizeof(oss_data));
330 data->killNow = 0;
332 data->fd = open(driver, O_RDONLY);
333 if(data->fd == -1)
335 free(data);
336 AL_PRINT("Could not open %s: %s\n", driver, strerror(errno));
337 return ALC_FALSE;
340 switch(format)
342 case AL_FORMAT_MONO8:
343 case AL_FORMAT_STEREO8:
344 case AL_FORMAT_QUAD8:
345 data->silence = 0x80;
346 ossFormat = AFMT_U8;
347 break;
348 case AL_FORMAT_MONO16:
349 case AL_FORMAT_STEREO16:
350 case AL_FORMAT_QUAD16:
351 data->silence = 0;
352 ossFormat = AFMT_S16_NE;
353 break;
354 default:
355 ossFormat = -1;
356 AL_PRINT("Unknown format?! %x\n", device->Format);
359 periods = 4;
360 numChannels = device->Channels;
361 ossSpeed = frequency;
362 log2FragmentSize = log2i(device->UpdateFreq * device->FrameSize / periods);
364 /* according to the OSS spec, 16 bytes are the minimum */
365 if (log2FragmentSize < 4)
366 log2FragmentSize = 4;
367 numFragmentsLogSize = (periods << 16) | log2FragmentSize;
369 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
370 if (!(ok(ioctl(data->fd, SNDCTL_DSP_SETFRAGMENT, &numFragmentsLogSize), "set fragment") &&
371 ok(ioctl(data->fd, SNDCTL_DSP_SETFMT, &ossFormat), "set format") &&
372 ok(ioctl(data->fd, SNDCTL_DSP_CHANNELS, &numChannels), "set channels") &&
373 ok(ioctl(data->fd, SNDCTL_DSP_SPEED, &ossSpeed), "set speed") &&
374 ok(ioctl(data->fd, SNDCTL_DSP_GETISPACE, &info), "get space")))
376 AL_PRINT("%s failed: %s\n", err, strerror(errno));
377 close(data->fd);
378 free(data);
379 return ALC_FALSE;
381 #undef ok
383 device->Channels = numChannels;
384 device->Frequency = ossSpeed;
385 device->Format = 0;
386 if(ossFormat == AFMT_U8)
388 if(device->Channels == 1)
390 device->Format = AL_FORMAT_MONO8;
391 device->FrameSize = 1;
393 else if(device->Channels == 2)
395 device->Format = AL_FORMAT_STEREO8;
396 device->FrameSize = 2;
398 else if(device->Channels == 4)
400 device->Format = AL_FORMAT_QUAD8;
401 device->FrameSize = 4;
404 else if(ossFormat == AFMT_S16_NE)
406 if(device->Channels == 1)
408 device->Format = AL_FORMAT_MONO16;
409 device->FrameSize = 2;
411 else if(device->Channels == 2)
413 device->Format = AL_FORMAT_STEREO16;
414 device->FrameSize = 4;
416 else if(device->Channels == 4)
418 device->Format = AL_FORMAT_QUAD16;
419 device->FrameSize = 8;
423 if(!device->Format)
425 AL_PRINT("returned unknown format: %#x %d!\n", ossFormat, numChannels);
426 close(data->fd);
427 free(data);
428 return ALC_FALSE;
431 data->ring = CreateRingBuffer(device->FrameSize, SampleSize);
432 if(!data->ring)
434 AL_PRINT("ring buffer create failed\n");
435 close(data->fd);
436 free(data);
437 return ALC_FALSE;
440 device->UpdateFreq = info.fragsize / device->FrameSize;
442 data->data_size = device->UpdateFreq * device->FrameSize;
443 data->mix_data = calloc(1, data->data_size);
445 device->ExtraData = data;
446 data->thread = StartThread(OSSCaptureProc, device);
447 if(data->thread == NULL)
449 device->ExtraData = NULL;
450 free(data->mix_data);
451 free(data);
452 return ALC_FALSE;
455 return ALC_TRUE;
458 static void oss_close_capture(ALCdevice *device)
460 oss_data *data = (oss_data*)device->ExtraData;
461 data->killNow = 1;
462 StopThread(data->thread);
464 close(data->fd);
466 DestroyRingBuffer(data->ring);
468 free(data->mix_data);
469 free(data);
470 device->ExtraData = NULL;
473 static void oss_start_capture(ALCdevice *pDevice)
475 oss_data *data = (oss_data*)pDevice->ExtraData;
476 data->doCapture = 1;
479 static void oss_stop_capture(ALCdevice *pDevice)
481 oss_data *data = (oss_data*)pDevice->ExtraData;
482 data->doCapture = 0;
485 static void oss_capture_samples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCuint lSamples)
487 oss_data *data = (oss_data*)pDevice->ExtraData;
488 if(lSamples <= (ALCuint)RingBufferSize(data->ring))
489 ReadRingBuffer(data->ring, pBuffer, lSamples);
490 else
491 SetALCError(ALC_INVALID_VALUE);
494 static ALCuint oss_available_samples(ALCdevice *pDevice)
496 oss_data *data = (oss_data*)pDevice->ExtraData;
497 return RingBufferSize(data->ring);
501 BackendFuncs oss_funcs = {
502 oss_open_playback,
503 oss_close_playback,
504 oss_open_capture,
505 oss_close_capture,
506 oss_start_capture,
507 oss_stop_capture,
508 oss_capture_samples,
509 oss_available_samples
512 void alc_oss_init(BackendFuncs *func_list)
514 *func_list = oss_funcs;
516 oss_device = AppendDeviceList("OSS Software");
517 AppendAllDeviceList(oss_device);
519 oss_device_capture = AppendCaptureDeviceList("OSS Capture");