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 const ALCchar oss_device
[] = "OSS Software";
51 static const ALCchar oss_device_capture
[] = "OSS 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
;
85 frameSize
= aluChannelsFromFormat(pDevice
->Format
) *
86 aluBytesFromFormat(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
);
99 if(errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
&& errno
!= EINTR
)
101 AL_PRINT("write failed: %s\n", strerror(errno
));
102 aluHandleDisconnect(pDevice
);
118 static ALuint
OSSCaptureProc(ALvoid
*ptr
)
120 ALCdevice
*pDevice
= (ALCdevice
*)ptr
;
121 oss_data
*data
= (oss_data
*)pDevice
->ExtraData
;
125 frameSize
= aluBytesFromFormat(pDevice
->Format
);
126 frameSize
*= aluChannelsFromFormat(pDevice
->Format
);
128 while(!data
->killNow
)
130 amt
= read(data
->fd
, data
->mix_data
, data
->data_size
);
133 AL_PRINT("read failed: %s\n", strerror(errno
));
134 aluHandleDisconnect(pDevice
);
143 WriteRingBuffer(data
->ring
, data
->mix_data
, amt
/frameSize
);
149 static ALCboolean
oss_open_playback(ALCdevice
*device
, const ALCchar
*deviceName
)
154 strncpy(driver
, GetConfigValue("oss", "device", "/dev/dsp"), sizeof(driver
)-1);
155 driver
[sizeof(driver
)-1] = 0;
157 deviceName
= oss_device
;
158 else if(strcmp(deviceName
, oss_device
) != 0)
161 data
= (oss_data
*)calloc(1, sizeof(oss_data
));
164 data
->fd
= open(driver
, O_WRONLY
);
168 AL_PRINT("Could not open %s: %s\n", driver
, strerror(errno
));
172 device
->szDeviceName
= strdup(deviceName
);
173 device
->ExtraData
= data
;
177 static void oss_close_playback(ALCdevice
*device
)
179 oss_data
*data
= (oss_data
*)device
->ExtraData
;
183 device
->ExtraData
= NULL
;
186 static ALCboolean
oss_reset_playback(ALCdevice
*device
)
188 oss_data
*data
= (oss_data
*)device
->ExtraData
;
189 int numFragmentsLogSize
;
190 int log2FragmentSize
;
191 unsigned int periods
;
200 switch(aluBytesFromFormat(device
->Format
))
206 switch(aluChannelsFromFormat(device
->Format
))
208 case 1: device
->Format
= AL_FORMAT_MONO16
; break;
209 case 2: device
->Format
= AL_FORMAT_STEREO16
; break;
210 case 4: device
->Format
= AL_FORMAT_QUAD16
; break;
211 case 6: device
->Format
= AL_FORMAT_51CHN16
; break;
212 case 7: device
->Format
= AL_FORMAT_61CHN16
; break;
213 case 8: device
->Format
= AL_FORMAT_71CHN16
; break;
217 ossFormat
= AFMT_S16_NE
;
220 AL_PRINT("Unknown format: 0x%x\n", device
->Format
);
224 periods
= device
->NumUpdates
;
225 numChannels
= aluChannelsFromFormat(device
->Format
);
226 frameSize
= numChannels
* aluBytesFromFormat(device
->Format
);
228 ossSpeed
= device
->Frequency
;
229 log2FragmentSize
= log2i(device
->UpdateSize
* frameSize
);
231 /* according to the OSS spec, 16 bytes are the minimum */
232 if (log2FragmentSize
< 4)
233 log2FragmentSize
= 4;
234 /* Subtract one period since the temp mixing buffer counts as one. Still
235 * need at least two on the card, though. */
236 if(periods
> 2) periods
--;
237 numFragmentsLogSize
= (periods
<< 16) | log2FragmentSize
;
239 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
240 /* Don't fail if SETFRAGMENT fails. We can handle just about anything
241 * that's reported back via GETOSPACE */
242 ioctl(data
->fd
, SNDCTL_DSP_SETFRAGMENT
, &numFragmentsLogSize
);
243 if (!(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
;
268 device
->NumUpdates
= info
.fragments
+ 1;
270 data
->data_size
= device
->UpdateSize
* frameSize
;
271 data
->mix_data
= calloc(1, data
->data_size
);
273 data
->thread
= StartThread(OSSProc
, device
);
274 if(data
->thread
== NULL
)
276 free(data
->mix_data
);
277 data
->mix_data
= NULL
;
284 static void oss_stop_playback(ALCdevice
*device
)
286 oss_data
*data
= (oss_data
*)device
->ExtraData
;
292 StopThread(data
->thread
);
295 if(ioctl(data
->fd
, SNDCTL_DSP_RESET
) != 0)
296 AL_PRINT("Error resetting device: %s\n", strerror(errno
));
298 free(data
->mix_data
);
299 data
->mix_data
= NULL
;
303 static ALCboolean
oss_open_capture(ALCdevice
*device
, const ALCchar
*deviceName
)
305 int numFragmentsLogSize
;
306 int log2FragmentSize
;
307 unsigned int periods
;
318 strncpy(driver
, GetConfigValue("oss", "capture", "/dev/dsp"), sizeof(driver
)-1);
319 driver
[sizeof(driver
)-1] = 0;
321 deviceName
= oss_device_capture
;
322 else if(strcmp(deviceName
, oss_device_capture
) != 0)
325 data
= (oss_data
*)calloc(1, sizeof(oss_data
));
328 data
->fd
= open(driver
, O_RDONLY
);
332 AL_PRINT("Could not open %s: %s\n", driver
, strerror(errno
));
336 switch(aluBytesFromFormat(device
->Format
))
342 ossFormat
= AFMT_S16_NE
;
345 AL_PRINT("Unknown format: 0x%x\n", device
->Format
);
352 numChannels
= aluChannelsFromFormat(device
->Format
);
353 frameSize
= numChannels
* aluBytesFromFormat(device
->Format
);
354 ossSpeed
= device
->Frequency
;
355 log2FragmentSize
= log2i(device
->UpdateSize
* device
->NumUpdates
*
356 frameSize
/ periods
);
358 /* according to the OSS spec, 16 bytes are the minimum */
359 if (log2FragmentSize
< 4)
360 log2FragmentSize
= 4;
361 numFragmentsLogSize
= (periods
<< 16) | log2FragmentSize
;
363 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
364 if (!(ok(ioctl(data
->fd
, SNDCTL_DSP_SETFRAGMENT
, &numFragmentsLogSize
), "set fragment") &&
365 ok(ioctl(data
->fd
, SNDCTL_DSP_SETFMT
, &ossFormat
), "set format") &&
366 ok(ioctl(data
->fd
, SNDCTL_DSP_CHANNELS
, &numChannels
), "set channels") &&
367 ok(ioctl(data
->fd
, SNDCTL_DSP_SPEED
, &ossSpeed
), "set speed") &&
368 ok(ioctl(data
->fd
, SNDCTL_DSP_GETISPACE
, &info
), "get space")))
370 AL_PRINT("%s failed: %s\n", err
, strerror(errno
));
377 if((int)aluChannelsFromFormat(device
->Format
) != numChannels
)
379 AL_PRINT("Could not set %d channels, got %d instead\n", aluChannelsFromFormat(device
->Format
), numChannels
);
385 if(!((ossFormat
== AFMT_U8
&& aluBytesFromFormat(device
->Format
) == 1) ||
386 (ossFormat
== AFMT_S16_NE
&& aluBytesFromFormat(device
->Format
) == 2)))
388 AL_PRINT("Could not set %d-bit input, got format %#x\n", aluBytesFromFormat(device
->Format
)*8, ossFormat
);
394 data
->ring
= CreateRingBuffer(frameSize
, device
->UpdateSize
* device
->NumUpdates
);
397 AL_PRINT("ring buffer create failed\n");
403 data
->data_size
= info
.fragsize
;
404 data
->mix_data
= calloc(1, data
->data_size
);
406 device
->ExtraData
= data
;
407 data
->thread
= StartThread(OSSCaptureProc
, device
);
408 if(data
->thread
== NULL
)
410 device
->ExtraData
= NULL
;
411 free(data
->mix_data
);
416 device
->szDeviceName
= strdup(deviceName
);
420 static void oss_close_capture(ALCdevice
*device
)
422 oss_data
*data
= (oss_data
*)device
->ExtraData
;
424 StopThread(data
->thread
);
428 DestroyRingBuffer(data
->ring
);
430 free(data
->mix_data
);
432 device
->ExtraData
= NULL
;
435 static void oss_start_capture(ALCdevice
*pDevice
)
437 oss_data
*data
= (oss_data
*)pDevice
->ExtraData
;
441 static void oss_stop_capture(ALCdevice
*pDevice
)
443 oss_data
*data
= (oss_data
*)pDevice
->ExtraData
;
447 static void oss_capture_samples(ALCdevice
*pDevice
, ALCvoid
*pBuffer
, ALCuint lSamples
)
449 oss_data
*data
= (oss_data
*)pDevice
->ExtraData
;
450 if(lSamples
<= (ALCuint
)RingBufferSize(data
->ring
))
451 ReadRingBuffer(data
->ring
, pBuffer
, lSamples
);
453 alcSetError(ALC_INVALID_VALUE
);
456 static ALCuint
oss_available_samples(ALCdevice
*pDevice
)
458 oss_data
*data
= (oss_data
*)pDevice
->ExtraData
;
459 return RingBufferSize(data
->ring
);
463 BackendFuncs oss_funcs
= {
473 oss_available_samples
476 void alc_oss_init(BackendFuncs
*func_list
)
478 *func_list
= oss_funcs
;
481 void alc_oss_deinit(void)
485 void alc_oss_probe(int type
)
487 if(type
== DEVICE_PROBE
)
488 AppendDeviceList(oss_device
);
489 else if(type
== ALL_DEVICE_PROBE
)
490 AppendAllDeviceList(oss_device
);
491 else if(type
== CAPTURE_DEVICE_PROBE
)
492 AppendCaptureDeviceList(oss_device_capture
);