Store double formats as float
[openal-soft.git] / Alc / oss.c
blob898c10ef3951edad05d023fd777112108ebc5be6
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 "config.h"
23 #include <sys/ioctl.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <memory.h>
30 #include <unistd.h>
31 #include <errno.h>
32 #include <math.h>
33 #include "alMain.h"
34 #include "AL/al.h"
35 #include "AL/alc.h"
37 #include <sys/soundcard.h>
40 * The OSS documentation talks about SOUND_MIXER_READ, but the header
41 * only contains MIXER_READ. Play safe. Same for WRITE.
43 #ifndef SOUND_MIXER_READ
44 #define SOUND_MIXER_READ MIXER_READ
45 #endif
46 #ifndef SOUND_MIXER_WRITE
47 #define SOUND_MIXER_WRITE MIXER_WRITE
48 #endif
50 static const ALCchar oss_device[] = "OSS Default";
52 typedef struct {
53 int fd;
54 volatile int killNow;
55 ALvoid *thread;
57 ALubyte *mix_data;
58 int data_size;
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 ALint frameSize;
82 ssize_t wrote;
84 SetRTPriority();
86 frameSize = aluFrameSizeFromFormat(pDevice->Format);
88 while(!data->killNow && pDevice->Connected)
90 ALint len = data->data_size;
91 ALubyte *WritePtr = data->mix_data;
93 aluMixData(pDevice, WritePtr, len/frameSize);
94 while(len > 0 && !data->killNow)
96 wrote = write(data->fd, WritePtr, len);
97 if(wrote < 0)
99 if(errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR)
101 AL_PRINT("write failed: %s\n", strerror(errno));
102 aluHandleDisconnect(pDevice);
103 break;
106 Sleep(1);
107 continue;
110 len -= wrote;
111 WritePtr += wrote;
115 return 0;
118 static ALuint OSSCaptureProc(ALvoid *ptr)
120 ALCdevice *pDevice = (ALCdevice*)ptr;
121 oss_data *data = (oss_data*)pDevice->ExtraData;
122 int frameSize;
123 int amt;
125 SetRTPriority();
127 frameSize = aluFrameSizeFromFormat(pDevice->Format);
129 while(!data->killNow)
131 amt = read(data->fd, data->mix_data, data->data_size);
132 if(amt < 0)
134 AL_PRINT("read failed: %s\n", strerror(errno));
135 aluHandleDisconnect(pDevice);
136 break;
138 if(amt == 0)
140 Sleep(1);
141 continue;
143 if(data->doCapture)
144 WriteRingBuffer(data->ring, data->mix_data, amt/frameSize);
147 return 0;
150 static ALCboolean oss_open_playback(ALCdevice *device, const ALCchar *deviceName)
152 char driver[64];
153 oss_data *data;
155 strncpy(driver, GetConfigValue("oss", "device", "/dev/dsp"), sizeof(driver)-1);
156 driver[sizeof(driver)-1] = 0;
157 if(!deviceName)
158 deviceName = oss_device;
159 else if(strcmp(deviceName, oss_device) != 0)
160 return ALC_FALSE;
162 data = (oss_data*)calloc(1, sizeof(oss_data));
163 data->killNow = 0;
165 data->fd = open(driver, O_WRONLY);
166 if(data->fd == -1)
168 free(data);
169 AL_PRINT("Could not open %s: %s\n", driver, strerror(errno));
170 return ALC_FALSE;
173 device->szDeviceName = strdup(deviceName);
174 device->ExtraData = data;
175 return ALC_TRUE;
178 static void oss_close_playback(ALCdevice *device)
180 oss_data *data = (oss_data*)device->ExtraData;
182 close(data->fd);
183 free(data);
184 device->ExtraData = NULL;
187 static ALCboolean oss_reset_playback(ALCdevice *device)
189 oss_data *data = (oss_data*)device->ExtraData;
190 int numFragmentsLogSize;
191 int log2FragmentSize;
192 unsigned int periods;
193 audio_buf_info info;
194 ALuint frameSize;
195 int numChannels;
196 int ossFormat;
197 int ossSpeed;
198 char *err;
199 int i;
201 switch(aluBytesFromFormat(device->Format))
203 case 1:
204 ossFormat = AFMT_U8;
205 break;
206 case 4:
207 switch(aluChannelsFromFormat(device->Format))
209 case 1: device->Format = AL_FORMAT_MONO16; break;
210 case 2: device->Format = AL_FORMAT_STEREO16; break;
211 case 4: device->Format = AL_FORMAT_QUAD16; break;
212 case 6: device->Format = AL_FORMAT_51CHN16; break;
213 case 7: device->Format = AL_FORMAT_61CHN16; break;
214 case 8: device->Format = AL_FORMAT_71CHN16; break;
216 /* fall-through */
217 case 2:
218 ossFormat = AFMT_S16_NE;
219 break;
220 default:
221 AL_PRINT("Unknown format: 0x%x\n", device->Format);
222 return ALC_FALSE;
225 periods = device->NumUpdates;
226 numChannels = aluChannelsFromFormat(device->Format);
227 frameSize = numChannels * aluBytesFromFormat(device->Format);
229 ossSpeed = device->Frequency;
230 log2FragmentSize = log2i(device->UpdateSize * frameSize);
232 /* according to the OSS spec, 16 bytes are the minimum */
233 if (log2FragmentSize < 4)
234 log2FragmentSize = 4;
235 /* Subtract one period since the temp mixing buffer counts as one. Still
236 * need at least two on the card, though. */
237 if(periods > 2) periods--;
238 numFragmentsLogSize = (periods << 16) | log2FragmentSize;
240 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
241 /* Don't fail if SETFRAGMENT fails. We can handle just about anything
242 * that's reported back via GETOSPACE */
243 ioctl(data->fd, SNDCTL_DSP_SETFRAGMENT, &numFragmentsLogSize);
244 if (!(ok(ioctl(data->fd, SNDCTL_DSP_SETFMT, &ossFormat), "set format") &&
245 ok(ioctl(data->fd, SNDCTL_DSP_CHANNELS, &numChannels), "set channels") &&
246 ok(ioctl(data->fd, SNDCTL_DSP_SPEED, &ossSpeed), "set speed") &&
247 ok(ioctl(data->fd, SNDCTL_DSP_GETOSPACE, &info), "get space")))
249 AL_PRINT("%s failed: %s\n", err, strerror(errno));
250 return ALC_FALSE;
252 #undef ok
254 if((int)aluChannelsFromFormat(device->Format) != numChannels)
256 AL_PRINT("Could not set %d channels, got %d instead\n", aluChannelsFromFormat(device->Format), numChannels);
257 return ALC_FALSE;
260 if(!((ossFormat == AFMT_U8 && aluBytesFromFormat(device->Format) == 1) ||
261 (ossFormat == AFMT_S16_NE && aluBytesFromFormat(device->Format) == 2)))
263 AL_PRINT("Could not set %d-bit output, got format %#x\n", aluBytesFromFormat(device->Format)*8, ossFormat);
264 return ALC_FALSE;
267 device->Frequency = ossSpeed;
268 device->UpdateSize = info.fragsize / frameSize;
269 device->NumUpdates = info.fragments + 1;
271 data->data_size = device->UpdateSize * frameSize;
272 data->mix_data = calloc(1, data->data_size);
274 SetDefaultChannelOrder(device);
276 data->thread = StartThread(OSSProc, device);
277 if(data->thread == NULL)
279 free(data->mix_data);
280 data->mix_data = NULL;
281 return ALC_FALSE;
284 return ALC_TRUE;
287 static void oss_stop_playback(ALCdevice *device)
289 oss_data *data = (oss_data*)device->ExtraData;
291 if(!data->thread)
292 return;
294 data->killNow = 1;
295 StopThread(data->thread);
296 data->thread = NULL;
298 data->killNow = 0;
299 if(ioctl(data->fd, SNDCTL_DSP_RESET) != 0)
300 AL_PRINT("Error resetting device: %s\n", strerror(errno));
302 free(data->mix_data);
303 data->mix_data = NULL;
307 static ALCboolean oss_open_capture(ALCdevice *device, const ALCchar *deviceName)
309 int numFragmentsLogSize;
310 int log2FragmentSize;
311 unsigned int periods;
312 audio_buf_info info;
313 ALuint frameSize;
314 int numChannels;
315 char driver[64];
316 oss_data *data;
317 int ossFormat;
318 int ossSpeed;
319 char *err;
320 int i;
322 strncpy(driver, GetConfigValue("oss", "capture", "/dev/dsp"), sizeof(driver)-1);
323 driver[sizeof(driver)-1] = 0;
324 if(!deviceName)
325 deviceName = oss_device;
326 else if(strcmp(deviceName, oss_device) != 0)
327 return ALC_FALSE;
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(aluBytesFromFormat(device->Format))
342 case 1:
343 ossFormat = AFMT_U8;
344 break;
345 case 2:
346 ossFormat = AFMT_S16_NE;
347 break;
348 default:
349 AL_PRINT("Unknown format: 0x%x\n", device->Format);
350 close(data->fd);
351 free(data);
352 return ALC_FALSE;
355 periods = 4;
356 numChannels = aluChannelsFromFormat(device->Format);
357 frameSize = numChannels * aluBytesFromFormat(device->Format);
358 ossSpeed = device->Frequency;
359 log2FragmentSize = log2i(device->UpdateSize * device->NumUpdates *
360 frameSize / periods);
362 /* according to the OSS spec, 16 bytes are the minimum */
363 if (log2FragmentSize < 4)
364 log2FragmentSize = 4;
365 numFragmentsLogSize = (periods << 16) | log2FragmentSize;
367 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
368 if (!(ok(ioctl(data->fd, SNDCTL_DSP_SETFRAGMENT, &numFragmentsLogSize), "set fragment") &&
369 ok(ioctl(data->fd, SNDCTL_DSP_SETFMT, &ossFormat), "set format") &&
370 ok(ioctl(data->fd, SNDCTL_DSP_CHANNELS, &numChannels), "set channels") &&
371 ok(ioctl(data->fd, SNDCTL_DSP_SPEED, &ossSpeed), "set speed") &&
372 ok(ioctl(data->fd, SNDCTL_DSP_GETISPACE, &info), "get space")))
374 AL_PRINT("%s failed: %s\n", err, strerror(errno));
375 close(data->fd);
376 free(data);
377 return ALC_FALSE;
379 #undef ok
381 if((int)aluChannelsFromFormat(device->Format) != numChannels)
383 AL_PRINT("Could not set %d channels, got %d instead\n", aluChannelsFromFormat(device->Format), numChannels);
384 close(data->fd);
385 free(data);
386 return ALC_FALSE;
389 if(!((ossFormat == AFMT_U8 && aluBytesFromFormat(device->Format) == 1) ||
390 (ossFormat == AFMT_S16_NE && aluBytesFromFormat(device->Format) == 2)))
392 AL_PRINT("Could not set %d-bit input, got format %#x\n", aluBytesFromFormat(device->Format)*8, ossFormat);
393 close(data->fd);
394 free(data);
395 return ALC_FALSE;
398 data->ring = CreateRingBuffer(frameSize, device->UpdateSize * device->NumUpdates);
399 if(!data->ring)
401 AL_PRINT("ring buffer create failed\n");
402 close(data->fd);
403 free(data);
404 return ALC_FALSE;
407 data->data_size = info.fragsize;
408 data->mix_data = calloc(1, data->data_size);
410 device->ExtraData = data;
411 data->thread = StartThread(OSSCaptureProc, device);
412 if(data->thread == NULL)
414 device->ExtraData = NULL;
415 free(data->mix_data);
416 free(data);
417 return ALC_FALSE;
420 device->szDeviceName = strdup(deviceName);
421 return ALC_TRUE;
424 static void oss_close_capture(ALCdevice *device)
426 oss_data *data = (oss_data*)device->ExtraData;
427 data->killNow = 1;
428 StopThread(data->thread);
430 close(data->fd);
432 DestroyRingBuffer(data->ring);
434 free(data->mix_data);
435 free(data);
436 device->ExtraData = NULL;
439 static void oss_start_capture(ALCdevice *pDevice)
441 oss_data *data = (oss_data*)pDevice->ExtraData;
442 data->doCapture = 1;
445 static void oss_stop_capture(ALCdevice *pDevice)
447 oss_data *data = (oss_data*)pDevice->ExtraData;
448 data->doCapture = 0;
451 static void oss_capture_samples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCuint lSamples)
453 oss_data *data = (oss_data*)pDevice->ExtraData;
454 if(lSamples <= (ALCuint)RingBufferSize(data->ring))
455 ReadRingBuffer(data->ring, pBuffer, lSamples);
456 else
457 alcSetError(pDevice, ALC_INVALID_VALUE);
460 static ALCuint oss_available_samples(ALCdevice *pDevice)
462 oss_data *data = (oss_data*)pDevice->ExtraData;
463 return RingBufferSize(data->ring);
467 BackendFuncs oss_funcs = {
468 oss_open_playback,
469 oss_close_playback,
470 oss_reset_playback,
471 oss_stop_playback,
472 oss_open_capture,
473 oss_close_capture,
474 oss_start_capture,
475 oss_stop_capture,
476 oss_capture_samples,
477 oss_available_samples
480 void alc_oss_init(BackendFuncs *func_list)
482 *func_list = oss_funcs;
485 void alc_oss_deinit(void)
489 void alc_oss_probe(int type)
491 if(type == DEVICE_PROBE)
493 #ifdef HAVE_STAT
494 struct stat buf;
495 if(stat(GetConfigValue("oss", "device", "/dev/dsp"), &buf) == 0)
496 #endif
497 AppendDeviceList(oss_device);
499 else if(type == ALL_DEVICE_PROBE)
501 #ifdef HAVE_STAT
502 struct stat buf;
503 if(stat(GetConfigValue("oss", "device", "/dev/dsp"), &buf) == 0)
504 #endif
505 AppendAllDeviceList(oss_device);
507 else if(type == CAPTURE_DEVICE_PROBE)
509 #ifdef HAVE_STAT
510 struct stat buf;
511 if(stat(GetConfigValue("oss", "capture", "/dev/dsp"), &buf) == 0)
512 #endif
513 AppendCaptureDeviceList(oss_device);