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 EnableRTPrio(RTPrioLevel
);
87 frameSize
= aluChannelsFromFormat(pDevice
->Format
) *
88 aluBytesFromFormat(pDevice
->Format
);
90 while(!data
->killNow
&& pDevice
->Connected
)
92 ALint len
= data
->data_size
;
93 ALubyte
*WritePtr
= data
->mix_data
;
95 aluMixData(pDevice
, WritePtr
, len
/frameSize
);
96 while(len
> 0 && !data
->killNow
)
98 wrote
= write(data
->fd
, WritePtr
, len
);
101 if(errno
!= EAGAIN
&& errno
!= EWOULDBLOCK
&& errno
!= EINTR
)
103 AL_PRINT("write failed: %s\n", strerror(errno
));
104 aluHandleDisconnect(pDevice
);
120 static ALuint
OSSCaptureProc(ALvoid
*ptr
)
122 ALCdevice
*pDevice
= (ALCdevice
*)ptr
;
123 oss_data
*data
= (oss_data
*)pDevice
->ExtraData
;
127 EnableRTPrio(RTPrioLevel
);
129 frameSize
= aluBytesFromFormat(pDevice
->Format
);
130 frameSize
*= aluChannelsFromFormat(pDevice
->Format
);
132 while(!data
->killNow
)
134 amt
= read(data
->fd
, data
->mix_data
, data
->data_size
);
137 AL_PRINT("read failed: %s\n", strerror(errno
));
138 aluHandleDisconnect(pDevice
);
147 WriteRingBuffer(data
->ring
, data
->mix_data
, amt
/frameSize
);
153 static ALCboolean
oss_open_playback(ALCdevice
*device
, const ALCchar
*deviceName
)
158 strncpy(driver
, GetConfigValue("oss", "device", "/dev/dsp"), sizeof(driver
)-1);
159 driver
[sizeof(driver
)-1] = 0;
161 deviceName
= oss_device
;
162 else if(strcmp(deviceName
, oss_device
) != 0)
165 data
= (oss_data
*)calloc(1, sizeof(oss_data
));
168 data
->fd
= open(driver
, O_WRONLY
);
172 AL_PRINT("Could not open %s: %s\n", driver
, strerror(errno
));
176 device
->szDeviceName
= strdup(deviceName
);
177 device
->ExtraData
= data
;
181 static void oss_close_playback(ALCdevice
*device
)
183 oss_data
*data
= (oss_data
*)device
->ExtraData
;
187 device
->ExtraData
= NULL
;
190 static ALCboolean
oss_reset_playback(ALCdevice
*device
)
192 oss_data
*data
= (oss_data
*)device
->ExtraData
;
193 int numFragmentsLogSize
;
194 int log2FragmentSize
;
195 unsigned int periods
;
204 switch(aluBytesFromFormat(device
->Format
))
210 switch(aluChannelsFromFormat(device
->Format
))
212 case 1: device
->Format
= AL_FORMAT_MONO16
; break;
213 case 2: device
->Format
= AL_FORMAT_STEREO16
; break;
214 case 4: device
->Format
= AL_FORMAT_QUAD16
; break;
215 case 6: device
->Format
= AL_FORMAT_51CHN16
; break;
216 case 7: device
->Format
= AL_FORMAT_61CHN16
; break;
217 case 8: device
->Format
= AL_FORMAT_71CHN16
; break;
221 ossFormat
= AFMT_S16_NE
;
224 AL_PRINT("Unknown format: 0x%x\n", device
->Format
);
228 periods
= device
->NumUpdates
;
229 numChannels
= aluChannelsFromFormat(device
->Format
);
230 frameSize
= numChannels
* aluBytesFromFormat(device
->Format
);
232 ossSpeed
= device
->Frequency
;
233 log2FragmentSize
= log2i(device
->UpdateSize
* frameSize
);
235 /* according to the OSS spec, 16 bytes are the minimum */
236 if (log2FragmentSize
< 4)
237 log2FragmentSize
= 4;
238 /* Subtract one period since the temp mixing buffer counts as one. Still
239 * need at least two on the card, though. */
240 if(periods
> 2) periods
--;
241 numFragmentsLogSize
= (periods
<< 16) | log2FragmentSize
;
243 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
244 /* Don't fail if SETFRAGMENT fails. We can handle just about anything
245 * that's reported back via GETOSPACE */
246 ioctl(data
->fd
, SNDCTL_DSP_SETFRAGMENT
, &numFragmentsLogSize
);
247 if (!(ok(ioctl(data
->fd
, SNDCTL_DSP_SETFMT
, &ossFormat
), "set format") &&
248 ok(ioctl(data
->fd
, SNDCTL_DSP_CHANNELS
, &numChannels
), "set channels") &&
249 ok(ioctl(data
->fd
, SNDCTL_DSP_SPEED
, &ossSpeed
), "set speed") &&
250 ok(ioctl(data
->fd
, SNDCTL_DSP_GETOSPACE
, &info
), "get space")))
252 AL_PRINT("%s failed: %s\n", err
, strerror(errno
));
257 if((int)aluChannelsFromFormat(device
->Format
) != numChannels
)
259 AL_PRINT("Could not set %d channels, got %d instead\n", aluChannelsFromFormat(device
->Format
), numChannels
);
263 if(!((ossFormat
== AFMT_U8
&& aluBytesFromFormat(device
->Format
) == 1) ||
264 (ossFormat
== AFMT_S16_NE
&& aluBytesFromFormat(device
->Format
) == 2)))
266 AL_PRINT("Could not set %d-bit output, got format %#x\n", aluBytesFromFormat(device
->Format
)*8, ossFormat
);
270 device
->Frequency
= ossSpeed
;
271 device
->UpdateSize
= info
.fragsize
/ frameSize
;
272 device
->NumUpdates
= info
.fragments
+ 1;
274 data
->data_size
= device
->UpdateSize
* frameSize
;
275 data
->mix_data
= calloc(1, data
->data_size
);
277 SetDefaultChannelOrder(device
);
279 data
->thread
= StartThread(OSSProc
, device
);
280 if(data
->thread
== NULL
)
282 free(data
->mix_data
);
283 data
->mix_data
= NULL
;
290 static void oss_stop_playback(ALCdevice
*device
)
292 oss_data
*data
= (oss_data
*)device
->ExtraData
;
298 StopThread(data
->thread
);
302 if(ioctl(data
->fd
, SNDCTL_DSP_RESET
) != 0)
303 AL_PRINT("Error resetting device: %s\n", strerror(errno
));
305 free(data
->mix_data
);
306 data
->mix_data
= NULL
;
310 static ALCboolean
oss_open_capture(ALCdevice
*device
, const ALCchar
*deviceName
)
312 int numFragmentsLogSize
;
313 int log2FragmentSize
;
314 unsigned int periods
;
325 strncpy(driver
, GetConfigValue("oss", "capture", "/dev/dsp"), sizeof(driver
)-1);
326 driver
[sizeof(driver
)-1] = 0;
328 deviceName
= oss_device_capture
;
329 else if(strcmp(deviceName
, oss_device_capture
) != 0)
332 data
= (oss_data
*)calloc(1, sizeof(oss_data
));
335 data
->fd
= open(driver
, O_RDONLY
);
339 AL_PRINT("Could not open %s: %s\n", driver
, strerror(errno
));
343 switch(aluBytesFromFormat(device
->Format
))
349 ossFormat
= AFMT_S16_NE
;
352 AL_PRINT("Unknown format: 0x%x\n", device
->Format
);
359 numChannels
= aluChannelsFromFormat(device
->Format
);
360 frameSize
= numChannels
* aluBytesFromFormat(device
->Format
);
361 ossSpeed
= device
->Frequency
;
362 log2FragmentSize
= log2i(device
->UpdateSize
* device
->NumUpdates
*
363 frameSize
/ periods
);
365 /* according to the OSS spec, 16 bytes are the minimum */
366 if (log2FragmentSize
< 4)
367 log2FragmentSize
= 4;
368 numFragmentsLogSize
= (periods
<< 16) | log2FragmentSize
;
370 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
371 if (!(ok(ioctl(data
->fd
, SNDCTL_DSP_SETFRAGMENT
, &numFragmentsLogSize
), "set fragment") &&
372 ok(ioctl(data
->fd
, SNDCTL_DSP_SETFMT
, &ossFormat
), "set format") &&
373 ok(ioctl(data
->fd
, SNDCTL_DSP_CHANNELS
, &numChannels
), "set channels") &&
374 ok(ioctl(data
->fd
, SNDCTL_DSP_SPEED
, &ossSpeed
), "set speed") &&
375 ok(ioctl(data
->fd
, SNDCTL_DSP_GETISPACE
, &info
), "get space")))
377 AL_PRINT("%s failed: %s\n", err
, strerror(errno
));
384 if((int)aluChannelsFromFormat(device
->Format
) != numChannels
)
386 AL_PRINT("Could not set %d channels, got %d instead\n", aluChannelsFromFormat(device
->Format
), numChannels
);
392 if(!((ossFormat
== AFMT_U8
&& aluBytesFromFormat(device
->Format
) == 1) ||
393 (ossFormat
== AFMT_S16_NE
&& aluBytesFromFormat(device
->Format
) == 2)))
395 AL_PRINT("Could not set %d-bit input, got format %#x\n", aluBytesFromFormat(device
->Format
)*8, ossFormat
);
401 data
->ring
= CreateRingBuffer(frameSize
, device
->UpdateSize
* device
->NumUpdates
);
404 AL_PRINT("ring buffer create failed\n");
410 data
->data_size
= info
.fragsize
;
411 data
->mix_data
= calloc(1, data
->data_size
);
413 device
->ExtraData
= data
;
414 data
->thread
= StartThread(OSSCaptureProc
, device
);
415 if(data
->thread
== NULL
)
417 device
->ExtraData
= NULL
;
418 free(data
->mix_data
);
423 device
->szDeviceName
= strdup(deviceName
);
427 static void oss_close_capture(ALCdevice
*device
)
429 oss_data
*data
= (oss_data
*)device
->ExtraData
;
431 StopThread(data
->thread
);
435 DestroyRingBuffer(data
->ring
);
437 free(data
->mix_data
);
439 device
->ExtraData
= NULL
;
442 static void oss_start_capture(ALCdevice
*pDevice
)
444 oss_data
*data
= (oss_data
*)pDevice
->ExtraData
;
448 static void oss_stop_capture(ALCdevice
*pDevice
)
450 oss_data
*data
= (oss_data
*)pDevice
->ExtraData
;
454 static void oss_capture_samples(ALCdevice
*pDevice
, ALCvoid
*pBuffer
, ALCuint lSamples
)
456 oss_data
*data
= (oss_data
*)pDevice
->ExtraData
;
457 if(lSamples
<= (ALCuint
)RingBufferSize(data
->ring
))
458 ReadRingBuffer(data
->ring
, pBuffer
, lSamples
);
460 alcSetError(ALC_INVALID_VALUE
);
463 static ALCuint
oss_available_samples(ALCdevice
*pDevice
)
465 oss_data
*data
= (oss_data
*)pDevice
->ExtraData
;
466 return RingBufferSize(data
->ring
);
470 BackendFuncs oss_funcs
= {
480 oss_available_samples
483 void alc_oss_init(BackendFuncs
*func_list
)
485 *func_list
= oss_funcs
;
488 void alc_oss_deinit(void)
492 void alc_oss_probe(int type
)
494 if(type
== DEVICE_PROBE
)
495 AppendDeviceList(oss_device
);
496 else if(type
== ALL_DEVICE_PROBE
)
497 AppendAllDeviceList(oss_device
);
498 else if(type
== CAPTURE_DEVICE_PROBE
)
499 AppendCaptureDeviceList(oss_device_capture
);