Don't check explicitly against formats, but rather their byte/channel count
[openal-soft.git] / Alc / oss.c
bloba63acb9b74b3aa4c2c7ceb14335ea9c681d9f511
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(aluBytesFromFormat(device->Format))
179 case 1:
180 data->silence = 0x80;
181 ossFormat = AFMT_U8;
182 break;
183 case 2:
184 data->silence = 0;
185 ossFormat = AFMT_S16_NE;
186 break;
187 default:
188 ossFormat = -1;
189 AL_PRINT("Unknown format?! %x\n", device->Format);
192 periods = GetConfigValueInt("oss", "periods", 4);
193 if((int)periods < 0)
194 periods = 4;
195 numChannels = device->Channels;
196 ossSpeed = device->Frequency;
197 log2FragmentSize = log2i(device->UpdateFreq * device->FrameSize / periods);
199 /* according to the OSS spec, 16 bytes are the minimum */
200 if (log2FragmentSize < 4)
201 log2FragmentSize = 4;
202 numFragmentsLogSize = (periods << 16) | log2FragmentSize;
204 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
205 if (!(ok(ioctl(data->fd, SNDCTL_DSP_SETFRAGMENT, &numFragmentsLogSize), "set fragment") &&
206 ok(ioctl(data->fd, SNDCTL_DSP_SETFMT, &ossFormat), "set format") &&
207 ok(ioctl(data->fd, SNDCTL_DSP_CHANNELS, &numChannels), "set channels") &&
208 ok(ioctl(data->fd, SNDCTL_DSP_SPEED, &ossSpeed), "set speed") &&
209 ok(ioctl(data->fd, SNDCTL_DSP_GETOSPACE, &info), "get space")))
211 AL_PRINT("%s failed: %s\n", err, strerror(errno));
212 close(data->fd);
213 free(data);
214 return ALC_FALSE;
216 #undef ok
218 device->Channels = numChannels;
219 device->Frequency = ossSpeed;
220 device->Format = 0;
221 if(ossFormat == AFMT_U8)
223 if(device->Channels == 1)
225 device->Format = AL_FORMAT_MONO8;
226 device->FrameSize = 1;
228 else if(device->Channels == 2)
230 device->Format = AL_FORMAT_STEREO8;
231 device->FrameSize = 2;
233 else if(device->Channels == 4)
235 device->Format = AL_FORMAT_QUAD8;
236 device->FrameSize = 4;
239 else if(ossFormat == AFMT_S16_NE)
241 if(device->Channels == 1)
243 device->Format = AL_FORMAT_MONO16;
244 device->FrameSize = 2;
246 else if(device->Channels == 2)
248 device->Format = AL_FORMAT_STEREO16;
249 device->FrameSize = 4;
251 else if(device->Channels == 4)
253 device->Format = AL_FORMAT_QUAD16;
254 device->FrameSize = 8;
258 if(!device->Format)
260 AL_PRINT("returned unknown format: %#x %d!\n", ossFormat, numChannels);
261 close(data->fd);
262 free(data);
263 return ALC_FALSE;
266 device->UpdateFreq = info.fragsize / device->FrameSize;
268 data->data_size = device->UpdateFreq * device->FrameSize;
269 data->mix_data = calloc(1, data->data_size);
271 device->MaxNoOfSources = 256;
273 device->ExtraData = data;
274 data->thread = StartThread(OSSProc, device);
275 if(data->thread == NULL)
277 device->ExtraData = NULL;
278 free(data->mix_data);
279 free(data);
280 return ALC_FALSE;
283 return ALC_TRUE;
286 static void oss_close_playback(ALCdevice *device)
288 oss_data *data = (oss_data*)device->ExtraData;
289 data->killNow = 1;
290 StopThread(data->thread);
292 close(data->fd);
294 free(data->mix_data);
295 free(data);
296 device->ExtraData = NULL;
300 static ALCboolean oss_open_capture(ALCdevice *device, const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
302 int numFragmentsLogSize;
303 int log2FragmentSize;
304 unsigned int periods;
305 audio_buf_info info;
306 int numChannels;
307 char driver[64];
308 oss_data *data;
309 int ossFormat;
310 int ossSpeed;
311 char *err;
312 int i;
314 strncpy(driver, GetConfigValue("oss", "capture", "/dev/dsp"), sizeof(driver)-1);
315 driver[sizeof(driver)-1] = 0;
316 if(deviceName)
318 if(strcmp(deviceName, oss_device_capture))
319 return ALC_FALSE;
320 device->szDeviceName = oss_device_capture;
322 else
323 device->szDeviceName = oss_device_capture;
325 data = (oss_data*)calloc(1, sizeof(oss_data));
326 data->killNow = 0;
328 data->fd = open(driver, O_RDONLY);
329 if(data->fd == -1)
331 free(data);
332 AL_PRINT("Could not open %s: %s\n", driver, strerror(errno));
333 return ALC_FALSE;
336 switch(aluBytesFromFormat(format))
338 case 1:
339 data->silence = 0x80;
340 ossFormat = AFMT_U8;
341 break;
342 case 2:
343 data->silence = 0;
344 ossFormat = AFMT_S16_NE;
345 break;
346 default:
347 ossFormat = -1;
348 AL_PRINT("Unknown format?! %x\n", device->Format);
351 periods = 4;
352 numChannels = device->Channels;
353 ossSpeed = frequency;
354 log2FragmentSize = log2i(device->UpdateFreq * device->FrameSize / periods);
356 /* according to the OSS spec, 16 bytes are the minimum */
357 if (log2FragmentSize < 4)
358 log2FragmentSize = 4;
359 numFragmentsLogSize = (periods << 16) | log2FragmentSize;
361 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
362 if (!(ok(ioctl(data->fd, SNDCTL_DSP_SETFRAGMENT, &numFragmentsLogSize), "set fragment") &&
363 ok(ioctl(data->fd, SNDCTL_DSP_SETFMT, &ossFormat), "set format") &&
364 ok(ioctl(data->fd, SNDCTL_DSP_CHANNELS, &numChannels), "set channels") &&
365 ok(ioctl(data->fd, SNDCTL_DSP_SPEED, &ossSpeed), "set speed") &&
366 ok(ioctl(data->fd, SNDCTL_DSP_GETISPACE, &info), "get space")))
368 AL_PRINT("%s failed: %s\n", err, strerror(errno));
369 close(data->fd);
370 free(data);
371 return ALC_FALSE;
373 #undef ok
375 device->Channels = numChannels;
376 device->Frequency = ossSpeed;
377 device->Format = 0;
378 if(ossFormat == AFMT_U8)
380 if(device->Channels == 1)
382 device->Format = AL_FORMAT_MONO8;
383 device->FrameSize = 1;
385 else if(device->Channels == 2)
387 device->Format = AL_FORMAT_STEREO8;
388 device->FrameSize = 2;
390 else if(device->Channels == 4)
392 device->Format = AL_FORMAT_QUAD8;
393 device->FrameSize = 4;
396 else if(ossFormat == AFMT_S16_NE)
398 if(device->Channels == 1)
400 device->Format = AL_FORMAT_MONO16;
401 device->FrameSize = 2;
403 else if(device->Channels == 2)
405 device->Format = AL_FORMAT_STEREO16;
406 device->FrameSize = 4;
408 else if(device->Channels == 4)
410 device->Format = AL_FORMAT_QUAD16;
411 device->FrameSize = 8;
415 if(!device->Format)
417 AL_PRINT("returned unknown format: %#x %d!\n", ossFormat, numChannels);
418 close(data->fd);
419 free(data);
420 return ALC_FALSE;
423 data->ring = CreateRingBuffer(device->FrameSize, SampleSize);
424 if(!data->ring)
426 AL_PRINT("ring buffer create failed\n");
427 close(data->fd);
428 free(data);
429 return ALC_FALSE;
432 device->UpdateFreq = info.fragsize / device->FrameSize;
434 data->data_size = device->UpdateFreq * device->FrameSize;
435 data->mix_data = calloc(1, data->data_size);
437 device->ExtraData = data;
438 data->thread = StartThread(OSSCaptureProc, device);
439 if(data->thread == NULL)
441 device->ExtraData = NULL;
442 free(data->mix_data);
443 free(data);
444 return ALC_FALSE;
447 return ALC_TRUE;
450 static void oss_close_capture(ALCdevice *device)
452 oss_data *data = (oss_data*)device->ExtraData;
453 data->killNow = 1;
454 StopThread(data->thread);
456 close(data->fd);
458 DestroyRingBuffer(data->ring);
460 free(data->mix_data);
461 free(data);
462 device->ExtraData = NULL;
465 static void oss_start_capture(ALCdevice *pDevice)
467 oss_data *data = (oss_data*)pDevice->ExtraData;
468 data->doCapture = 1;
471 static void oss_stop_capture(ALCdevice *pDevice)
473 oss_data *data = (oss_data*)pDevice->ExtraData;
474 data->doCapture = 0;
477 static void oss_capture_samples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCuint lSamples)
479 oss_data *data = (oss_data*)pDevice->ExtraData;
480 if(lSamples <= (ALCuint)RingBufferSize(data->ring))
481 ReadRingBuffer(data->ring, pBuffer, lSamples);
482 else
483 SetALCError(ALC_INVALID_VALUE);
486 static ALCuint oss_available_samples(ALCdevice *pDevice)
488 oss_data *data = (oss_data*)pDevice->ExtraData;
489 return RingBufferSize(data->ring);
493 BackendFuncs oss_funcs = {
494 oss_open_playback,
495 oss_close_playback,
496 oss_open_capture,
497 oss_close_capture,
498 oss_start_capture,
499 oss_stop_capture,
500 oss_capture_samples,
501 oss_available_samples
504 void alc_oss_init(BackendFuncs *func_list)
506 *func_list = oss_funcs;
508 oss_device = AppendDeviceList("OSS Software");
509 AppendAllDeviceList(oss_device);
511 oss_device_capture = AppendCaptureDeviceList("OSS Capture");