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
23 #include <sys/ioctl.h>
24 #include <sys/types.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
46 #ifndef SOUND_MIXER_WRITE
47 #define SOUND_MIXER_WRITE MIXER_WRITE
50 static char *oss_device
;
51 static char *oss_device_capture
;
66 static int log2i(ALCuint x
)
78 static ALuint
OSSProc(ALvoid
*ptr
)
80 ALCdevice
*pDevice
= (ALCdevice
*)ptr
;
81 oss_data
*data
= (oss_data
*)pDevice
->ExtraData
;
84 while(!data
->killNow
&& !pDevice
->Connected
)
86 ALint len
= data
->data_size
;
87 ALubyte
*WritePtr
= data
->mix_data
;
90 aluMixData(pDevice
->Context
, WritePtr
, len
, pDevice
->Format
);
93 while(len
> 0 && !data
->killNow
)
95 wrote
= write(data
->fd
, WritePtr
, len
);
98 if(errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
)
100 AL_PRINT("write failed: %s\n", strerror(errno
));
101 aluHandleDisconnect(pDevice
);
117 static ALuint
OSSCaptureProc(ALvoid
*ptr
)
119 ALCdevice
*pDevice
= (ALCdevice
*)ptr
;
120 oss_data
*data
= (oss_data
*)pDevice
->ExtraData
;
124 frameSize
= aluBytesFromFormat(pDevice
->Format
);
125 frameSize
*= aluChannelsFromFormat(pDevice
->Format
);
127 while(!data
->killNow
)
129 amt
= read(data
->fd
, data
->mix_data
, data
->data_size
);
132 AL_PRINT("read failed: %s\n", strerror(errno
));
133 aluHandleDisconnect(pDevice
);
142 WriteRingBuffer(data
->ring
, data
->mix_data
, amt
/frameSize
);
148 static ALCboolean
oss_open_playback(ALCdevice
*device
, const ALCchar
*deviceName
)
153 strncpy(driver
, GetConfigValue("oss", "device", "/dev/dsp"), sizeof(driver
)-1);
154 driver
[sizeof(driver
)-1] = 0;
157 if(strcmp(deviceName
, oss_device
))
159 device
->szDeviceName
= oss_device
;
162 device
->szDeviceName
= oss_device
;
164 data
= (oss_data
*)calloc(1, sizeof(oss_data
));
167 data
->fd
= open(driver
, O_WRONLY
);
171 AL_PRINT("Could not open %s: %s\n", driver
, strerror(errno
));
175 device
->ExtraData
= data
;
179 static void oss_close_playback(ALCdevice
*device
)
181 oss_data
*data
= (oss_data
*)device
->ExtraData
;
185 device
->ExtraData
= NULL
;
188 static ALCboolean
oss_start_context(ALCdevice
*device
, ALCcontext
*context
)
190 oss_data
*data
= (oss_data
*)device
->ExtraData
;
191 int numFragmentsLogSize
;
192 int log2FragmentSize
;
193 unsigned int periods
;
203 switch(aluBytesFromFormat(device
->Format
))
209 switch(aluChannelsFromFormat(device
->Format
))
211 case 1: device
->Format
= AL_FORMAT_MONO16
; break;
212 case 2: device
->Format
= AL_FORMAT_STEREO16
; break;
213 case 4: device
->Format
= AL_FORMAT_QUAD16
; break;
214 case 6: device
->Format
= AL_FORMAT_51CHN16
; break;
215 case 7: device
->Format
= AL_FORMAT_61CHN16
; break;
216 case 8: device
->Format
= AL_FORMAT_71CHN16
; break;
220 ossFormat
= AFMT_S16_NE
;
224 AL_PRINT("Unknown format?! %x\n", device
->Format
);
227 periods
= GetConfigValueInt("oss", "periods", 4);
228 if((int)periods
<= 0)
230 numChannels
= aluChannelsFromFormat(device
->Format
);
231 frameSize
= numChannels
* aluBytesFromFormat(device
->Format
);
233 ossSpeed
= device
->Frequency
;
234 log2FragmentSize
= log2i(device
->BufferSize
* frameSize
/ periods
);
236 /* according to the OSS spec, 16 bytes are the minimum */
237 if (log2FragmentSize
< 4)
238 log2FragmentSize
= 4;
239 numFragmentsLogSize
= (periods
<< 16) | log2FragmentSize
;
241 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
242 if (!(ok(ioctl(data
->fd
, SNDCTL_DSP_SETFRAGMENT
, &numFragmentsLogSize
), "set fragment") &&
243 ok(ioctl(data
->fd
, SNDCTL_DSP_SETFMT
, &ossFormat
), "set format") &&
244 ok(ioctl(data
->fd
, SNDCTL_DSP_CHANNELS
, &numChannels
), "set channels") &&
245 ok(ioctl(data
->fd
, SNDCTL_DSP_SPEED
, &ossSpeed
), "set speed") &&
246 ok(ioctl(data
->fd
, SNDCTL_DSP_GETOSPACE
, &info
), "get space")))
248 AL_PRINT("%s failed: %s\n", err
, strerror(errno
));
253 if((int)aluChannelsFromFormat(device
->Format
) != numChannels
)
255 AL_PRINT("Could not set %d channels, got %d instead\n", aluChannelsFromFormat(device
->Format
), numChannels
);
259 if(!((ossFormat
== AFMT_U8
&& aluBytesFromFormat(device
->Format
) == 1) ||
260 (ossFormat
== AFMT_S16_NE
&& aluBytesFromFormat(device
->Format
) == 2)))
262 AL_PRINT("Could not set %d-bit output, got format %#x\n", aluBytesFromFormat(device
->Format
)*8, ossFormat
);
266 device
->Frequency
= ossSpeed
;
267 device
->UpdateSize
= info
.fragsize
/ frameSize
;
269 data
->data_size
= device
->UpdateSize
* frameSize
;
270 data
->mix_data
= calloc(1, data
->data_size
);
272 data
->thread
= StartThread(OSSProc
, device
);
273 if(data
->thread
== NULL
)
275 free(data
->mix_data
);
276 data
->mix_data
= NULL
;
283 static void oss_stop_context(ALCdevice
*device
, ALCcontext
*context
)
285 oss_data
*data
= (oss_data
*)device
->ExtraData
;
292 StopThread(data
->thread
);
295 free(data
->mix_data
);
296 data
->mix_data
= NULL
;
300 static ALCboolean
oss_open_capture(ALCdevice
*device
, const ALCchar
*deviceName
)
302 int numFragmentsLogSize
;
303 int log2FragmentSize
;
304 unsigned int periods
;
315 strncpy(driver
, GetConfigValue("oss", "capture", "/dev/dsp"), sizeof(driver
)-1);
316 driver
[sizeof(driver
)-1] = 0;
319 if(strcmp(deviceName
, oss_device_capture
))
321 device
->szDeviceName
= oss_device_capture
;
324 device
->szDeviceName
= oss_device_capture
;
326 data
= (oss_data
*)calloc(1, sizeof(oss_data
));
329 data
->fd
= open(driver
, O_RDONLY
);
333 AL_PRINT("Could not open %s: %s\n", driver
, strerror(errno
));
337 switch(aluBytesFromFormat(device
->Format
))
343 ossFormat
= AFMT_S16_NE
;
347 AL_PRINT("Unknown format?! %x\n", device
->Format
);
351 numChannels
= aluChannelsFromFormat(device
->Format
);
352 frameSize
= numChannels
* aluBytesFromFormat(device
->Format
);
353 ossSpeed
= device
->Frequency
;
354 log2FragmentSize
= log2i(device
->BufferSize
* 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
));
375 if((int)aluChannelsFromFormat(device
->Format
) != numChannels
)
377 AL_PRINT("Could not set %d channels, got %d instead\n", aluChannelsFromFormat(device
->Format
), numChannels
);
383 if(!((ossFormat
== AFMT_U8
&& aluBytesFromFormat(device
->Format
) == 1) ||
384 (ossFormat
== AFMT_S16_NE
&& aluBytesFromFormat(device
->Format
) == 2)))
386 AL_PRINT("Could not set %d-bit input, got format %#x\n", aluBytesFromFormat(device
->Format
)*8, ossFormat
);
392 data
->ring
= CreateRingBuffer(frameSize
, device
->BufferSize
);
395 AL_PRINT("ring buffer create failed\n");
401 data
->data_size
= info
.fragsize
;
402 data
->mix_data
= calloc(1, data
->data_size
);
404 device
->ExtraData
= data
;
405 data
->thread
= StartThread(OSSCaptureProc
, device
);
406 if(data
->thread
== NULL
)
408 device
->ExtraData
= NULL
;
409 free(data
->mix_data
);
417 static void oss_close_capture(ALCdevice
*device
)
419 oss_data
*data
= (oss_data
*)device
->ExtraData
;
421 StopThread(data
->thread
);
425 DestroyRingBuffer(data
->ring
);
427 free(data
->mix_data
);
429 device
->ExtraData
= NULL
;
432 static void oss_start_capture(ALCdevice
*pDevice
)
434 oss_data
*data
= (oss_data
*)pDevice
->ExtraData
;
438 static void oss_stop_capture(ALCdevice
*pDevice
)
440 oss_data
*data
= (oss_data
*)pDevice
->ExtraData
;
444 static void oss_capture_samples(ALCdevice
*pDevice
, ALCvoid
*pBuffer
, ALCuint lSamples
)
446 oss_data
*data
= (oss_data
*)pDevice
->ExtraData
;
447 if(lSamples
<= (ALCuint
)RingBufferSize(data
->ring
))
448 ReadRingBuffer(data
->ring
, pBuffer
, lSamples
);
450 SetALCError(ALC_INVALID_VALUE
);
453 static ALCuint
oss_available_samples(ALCdevice
*pDevice
)
455 oss_data
*data
= (oss_data
*)pDevice
->ExtraData
;
456 return RingBufferSize(data
->ring
);
460 BackendFuncs oss_funcs
= {
470 oss_available_samples
473 void alc_oss_init(BackendFuncs
*func_list
)
475 *func_list
= oss_funcs
;
477 oss_device
= AppendDeviceList("OSS Software");
478 AppendAllDeviceList(oss_device
);
480 oss_device_capture
= AppendCaptureDeviceList("OSS Capture");
483 void alc_oss_deinit(void)