UpdateSize is not used for capture devices
[openal-soft.git] / Alc / oss.c
blob91b7a5176ff9315ca18c516d499c0206a0e30d75
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 char *oss_device;
51 static char *oss_device_capture;
53 typedef struct {
54 int fd;
55 int killNow;
56 ALvoid *thread;
58 ALubyte *mix_data;
59 int data_size;
61 RingBuffer *ring;
62 int doCapture;
63 } oss_data;
66 static int log2i(ALCuint x)
68 int y = 0;
69 while (x > 1)
71 x >>= 1;
72 y++;
74 return y;
78 static ALuint OSSProc(ALvoid *ptr)
80 ALCdevice *pDevice = (ALCdevice*)ptr;
81 oss_data *data = (oss_data*)pDevice->ExtraData;
82 int remaining = 0;
83 int wrote;
85 while(!data->killNow)
87 int len = data->data_size - remaining;
89 if(len > 0)
91 SuspendContext(NULL);
92 aluMixData(pDevice->Context, data->mix_data+remaining, len, pDevice->Format);
93 ProcessContext(NULL);
96 remaining += len;
97 wrote = write(data->fd, data->mix_data, remaining);
98 if(wrote < 0)
100 AL_PRINT("write failed: %s\n", strerror(errno));
101 remaining = 0;
103 else if(wrote > 0)
105 remaining -= wrote;
106 if(remaining > 0)
107 memmove(data->mix_data, data->mix_data+wrote, remaining);
109 else
110 Sleep(1);
113 return 0;
116 static ALuint OSSCaptureProc(ALvoid *ptr)
118 ALCdevice *pDevice = (ALCdevice*)ptr;
119 oss_data *data = (oss_data*)pDevice->ExtraData;
120 int frameSize;
121 int amt;
123 frameSize = aluBytesFromFormat(pDevice->Format);
124 frameSize *= aluChannelsFromFormat(pDevice->Format);
126 while(!data->killNow)
128 amt = read(data->fd, data->mix_data, data->data_size);
129 if(amt < 0)
131 AL_PRINT("read failed: %s\n", strerror(errno));
132 break;
134 if(amt == 0)
136 Sleep(1);
137 continue;
139 if(data->doCapture)
140 WriteRingBuffer(data->ring, data->mix_data, amt/frameSize);
143 return 0;
146 static ALCboolean oss_open_playback(ALCdevice *device, const ALCchar *deviceName)
148 int numFragmentsLogSize;
149 int log2FragmentSize;
150 unsigned int periods;
151 audio_buf_info info;
152 char driver[64];
153 int numChannels;
154 oss_data *data;
155 int ossFormat;
156 int ossSpeed;
157 char *err;
158 int i;
160 strncpy(driver, GetConfigValue("oss", "device", "/dev/dsp"), sizeof(driver)-1);
161 driver[sizeof(driver)-1] = 0;
162 if(deviceName)
164 if(strcmp(deviceName, oss_device))
165 return ALC_FALSE;
166 device->szDeviceName = oss_device;
168 else
169 device->szDeviceName = oss_device;
171 data = (oss_data*)calloc(1, sizeof(oss_data));
172 data->killNow = 0;
174 data->fd = open(driver, O_WRONLY);
175 if(data->fd == -1)
177 free(data);
178 AL_PRINT("Could not open %s: %s\n", driver, strerror(errno));
179 return ALC_FALSE;
182 switch(aluBytesFromFormat(device->Format))
184 case 1:
185 ossFormat = AFMT_U8;
186 break;
187 case 2:
188 ossFormat = AFMT_S16_NE;
189 break;
190 default:
191 ossFormat = -1;
192 AL_PRINT("Unknown format?! %x\n", device->Format);
195 periods = GetConfigValueInt("oss", "periods", 4);
196 if((int)periods <= 0)
197 periods = 4;
198 numChannels = aluChannelsFromFormat(device->Format);
199 ossSpeed = device->Frequency;
200 log2FragmentSize = log2i(device->UpdateSize * device->FrameSize / periods);
202 /* according to the OSS spec, 16 bytes are the minimum */
203 if (log2FragmentSize < 4)
204 log2FragmentSize = 4;
205 numFragmentsLogSize = (periods << 16) | log2FragmentSize;
207 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
208 if (!(ok(ioctl(data->fd, SNDCTL_DSP_SETFRAGMENT, &numFragmentsLogSize), "set fragment") &&
209 ok(ioctl(data->fd, SNDCTL_DSP_SETFMT, &ossFormat), "set format") &&
210 ok(ioctl(data->fd, SNDCTL_DSP_CHANNELS, &numChannels), "set channels") &&
211 ok(ioctl(data->fd, SNDCTL_DSP_SPEED, &ossSpeed), "set speed") &&
212 ok(ioctl(data->fd, SNDCTL_DSP_GETOSPACE, &info), "get space")))
214 AL_PRINT("%s failed: %s\n", err, strerror(errno));
215 close(data->fd);
216 free(data);
217 return ALC_FALSE;
219 #undef ok
221 device->Frequency = ossSpeed;
223 if((int)aluChannelsFromFormat(device->Format) != numChannels)
225 AL_PRINT("Could not set %d channels, got %d instead\n", aluChannelsFromFormat(device->Format), numChannels);
226 close(data->fd);
227 free(data);
228 return ALC_FALSE;
231 if(!((ossFormat == AFMT_U8 && aluBytesFromFormat(device->Format) == 1) ||
232 (ossFormat == AFMT_S16_NE && aluBytesFromFormat(device->Format) == 2)))
234 AL_PRINT("Could not set %d-bit output, got format %#x\n", aluBytesFromFormat(device->Format)*8, ossFormat);
235 close(data->fd);
236 free(data);
237 return ALC_FALSE;
240 device->UpdateSize = info.fragsize / device->FrameSize;
242 data->data_size = device->UpdateSize * device->FrameSize;
243 data->mix_data = calloc(1, data->data_size);
245 device->ExtraData = data;
246 data->thread = StartThread(OSSProc, device);
247 if(data->thread == NULL)
249 device->ExtraData = NULL;
250 free(data->mix_data);
251 free(data);
252 return ALC_FALSE;
255 return ALC_TRUE;
258 static void oss_close_playback(ALCdevice *device)
260 oss_data *data = (oss_data*)device->ExtraData;
261 data->killNow = 1;
262 StopThread(data->thread);
264 close(data->fd);
266 free(data->mix_data);
267 free(data);
268 device->ExtraData = NULL;
272 static ALCboolean oss_open_capture(ALCdevice *device, const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei SampleSize)
274 int numFragmentsLogSize;
275 int log2FragmentSize;
276 unsigned int periods;
277 audio_buf_info info;
278 int numChannels;
279 char driver[64];
280 oss_data *data;
281 int ossFormat;
282 int ossSpeed;
283 char *err;
284 int i;
286 strncpy(driver, GetConfigValue("oss", "capture", "/dev/dsp"), sizeof(driver)-1);
287 driver[sizeof(driver)-1] = 0;
288 if(deviceName)
290 if(strcmp(deviceName, oss_device_capture))
291 return ALC_FALSE;
292 device->szDeviceName = oss_device_capture;
294 else
295 device->szDeviceName = oss_device_capture;
297 data = (oss_data*)calloc(1, sizeof(oss_data));
298 data->killNow = 0;
300 data->fd = open(driver, O_RDONLY);
301 if(data->fd == -1)
303 free(data);
304 AL_PRINT("Could not open %s: %s\n", driver, strerror(errno));
305 return ALC_FALSE;
308 switch(aluBytesFromFormat(format))
310 case 1:
311 ossFormat = AFMT_U8;
312 break;
313 case 2:
314 ossFormat = AFMT_S16_NE;
315 break;
316 default:
317 ossFormat = -1;
318 AL_PRINT("Unknown format?! %x\n", device->Format);
321 periods = 4;
322 numChannels = aluChannelsFromFormat(device->Format);
323 ossSpeed = frequency;
324 log2FragmentSize = log2i(SampleSize * device->FrameSize / periods);
326 /* according to the OSS spec, 16 bytes are the minimum */
327 if (log2FragmentSize < 4)
328 log2FragmentSize = 4;
329 numFragmentsLogSize = (periods << 16) | log2FragmentSize;
331 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
332 if (!(ok(ioctl(data->fd, SNDCTL_DSP_SETFRAGMENT, &numFragmentsLogSize), "set fragment") &&
333 ok(ioctl(data->fd, SNDCTL_DSP_SETFMT, &ossFormat), "set format") &&
334 ok(ioctl(data->fd, SNDCTL_DSP_CHANNELS, &numChannels), "set channels") &&
335 ok(ioctl(data->fd, SNDCTL_DSP_SPEED, &ossSpeed), "set speed") &&
336 ok(ioctl(data->fd, SNDCTL_DSP_GETISPACE, &info), "get space")))
338 AL_PRINT("%s failed: %s\n", err, strerror(errno));
339 close(data->fd);
340 free(data);
341 return ALC_FALSE;
343 #undef ok
345 if((int)aluChannelsFromFormat(device->Format) != numChannels)
347 AL_PRINT("Could not set %d channels, got %d instead\n", aluChannelsFromFormat(device->Format), numChannels);
348 close(data->fd);
349 free(data);
350 return ALC_FALSE;
353 if(!((ossFormat == AFMT_U8 && aluBytesFromFormat(device->Format) == 1) ||
354 (ossFormat == AFMT_S16_NE && aluBytesFromFormat(device->Format) == 2)))
356 AL_PRINT("Could not set %d-bit input, got format %#x\n", aluBytesFromFormat(device->Format)*8, ossFormat);
357 close(data->fd);
358 free(data);
359 return ALC_FALSE;
362 data->ring = CreateRingBuffer(device->FrameSize, SampleSize);
363 if(!data->ring)
365 AL_PRINT("ring buffer create failed\n");
366 close(data->fd);
367 free(data);
368 return ALC_FALSE;
371 data->data_size = SampleSize * device->FrameSize * info.fragments;
372 data->mix_data = calloc(1, data->data_size);
374 device->ExtraData = data;
375 data->thread = StartThread(OSSCaptureProc, device);
376 if(data->thread == NULL)
378 device->ExtraData = NULL;
379 free(data->mix_data);
380 free(data);
381 return ALC_FALSE;
384 return ALC_TRUE;
387 static void oss_close_capture(ALCdevice *device)
389 oss_data *data = (oss_data*)device->ExtraData;
390 data->killNow = 1;
391 StopThread(data->thread);
393 close(data->fd);
395 DestroyRingBuffer(data->ring);
397 free(data->mix_data);
398 free(data);
399 device->ExtraData = NULL;
402 static void oss_start_capture(ALCdevice *pDevice)
404 oss_data *data = (oss_data*)pDevice->ExtraData;
405 data->doCapture = 1;
408 static void oss_stop_capture(ALCdevice *pDevice)
410 oss_data *data = (oss_data*)pDevice->ExtraData;
411 data->doCapture = 0;
414 static void oss_capture_samples(ALCdevice *pDevice, ALCvoid *pBuffer, ALCuint lSamples)
416 oss_data *data = (oss_data*)pDevice->ExtraData;
417 if(lSamples <= (ALCuint)RingBufferSize(data->ring))
418 ReadRingBuffer(data->ring, pBuffer, lSamples);
419 else
420 SetALCError(ALC_INVALID_VALUE);
423 static ALCuint oss_available_samples(ALCdevice *pDevice)
425 oss_data *data = (oss_data*)pDevice->ExtraData;
426 return RingBufferSize(data->ring);
430 BackendFuncs oss_funcs = {
431 oss_open_playback,
432 oss_close_playback,
433 oss_open_capture,
434 oss_close_capture,
435 oss_start_capture,
436 oss_stop_capture,
437 oss_capture_samples,
438 oss_available_samples
441 void alc_oss_init(BackendFuncs *func_list)
443 *func_list = oss_funcs;
445 oss_device = AppendDeviceList("OSS Software");
446 AppendAllDeviceList(oss_device);
448 oss_device_capture = AppendCaptureDeviceList("OSS Capture");