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
;
87 int len
= data
->data_size
- remaining
;
92 aluMixData(pDevice
->Context
, data
->mix_data
+remaining
, len
, pDevice
->Format
);
97 wrote
= write(data
->fd
, data
->mix_data
, remaining
);
100 AL_PRINT("write failed: %s\n", strerror(errno
));
107 memmove(data
->mix_data
, data
->mix_data
+wrote
, remaining
);
116 static ALuint
OSSCaptureProc(ALvoid
*ptr
)
118 ALCdevice
*pDevice
= (ALCdevice
*)ptr
;
119 oss_data
*data
= (oss_data
*)pDevice
->ExtraData
;
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
);
131 AL_PRINT("read failed: %s\n", strerror(errno
));
140 WriteRingBuffer(data
->ring
, data
->mix_data
, amt
/frameSize
);
146 static ALCboolean
oss_open_playback(ALCdevice
*device
, const ALCchar
*deviceName
)
148 int numFragmentsLogSize
;
149 int log2FragmentSize
;
150 unsigned int periods
;
160 strncpy(driver
, GetConfigValue("oss", "device", "/dev/dsp"), sizeof(driver
)-1);
161 driver
[sizeof(driver
)-1] = 0;
164 if(strcmp(deviceName
, oss_device
))
166 device
->szDeviceName
= oss_device
;
169 device
->szDeviceName
= oss_device
;
171 data
= (oss_data
*)calloc(1, sizeof(oss_data
));
174 data
->fd
= open(driver
, O_WRONLY
);
178 AL_PRINT("Could not open %s: %s\n", driver
, strerror(errno
));
182 switch(aluBytesFromFormat(device
->Format
))
188 ossFormat
= AFMT_S16_NE
;
192 AL_PRINT("Unknown format?! %x\n", device
->Format
);
195 periods
= GetConfigValueInt("oss", "periods", 4);
196 if((int)periods
<= 0)
198 numChannels
= aluChannelsFromFormat(device
->Format
);
199 ossSpeed
= device
->Frequency
;
200 log2FragmentSize
= log2i(device
->UpdateFreq
* 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
));
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
);
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
);
240 device
->UpdateFreq
= info
.fragsize
/ device
->FrameSize
;
242 data
->data_size
= device
->UpdateFreq
* 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
);
258 static void oss_close_playback(ALCdevice
*device
)
260 oss_data
*data
= (oss_data
*)device
->ExtraData
;
262 StopThread(data
->thread
);
266 free(data
->mix_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
;
286 strncpy(driver
, GetConfigValue("oss", "capture", "/dev/dsp"), sizeof(driver
)-1);
287 driver
[sizeof(driver
)-1] = 0;
290 if(strcmp(deviceName
, oss_device_capture
))
292 device
->szDeviceName
= oss_device_capture
;
295 device
->szDeviceName
= oss_device_capture
;
297 data
= (oss_data
*)calloc(1, sizeof(oss_data
));
300 data
->fd
= open(driver
, O_RDONLY
);
304 AL_PRINT("Could not open %s: %s\n", driver
, strerror(errno
));
308 switch(aluBytesFromFormat(format
))
314 ossFormat
= AFMT_S16_NE
;
318 AL_PRINT("Unknown format?! %x\n", device
->Format
);
322 numChannels
= aluChannelsFromFormat(device
->Format
);
323 ossSpeed
= frequency
;
324 log2FragmentSize
= log2i(device
->UpdateFreq
* 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
));
345 if((int)aluChannelsFromFormat(device
->Format
) != numChannels
)
347 AL_PRINT("Could not set %d channels, got %d instead\n", aluChannelsFromFormat(device
->Format
), numChannels
);
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
);
362 data
->ring
= CreateRingBuffer(device
->FrameSize
, SampleSize
);
365 AL_PRINT("ring buffer create failed\n");
371 device
->UpdateFreq
= info
.fragsize
/ device
->FrameSize
;
373 data
->data_size
= device
->UpdateFreq
* device
->FrameSize
* info
.fragments
;
374 data
->mix_data
= calloc(1, data
->data_size
);
376 device
->ExtraData
= data
;
377 data
->thread
= StartThread(OSSCaptureProc
, device
);
378 if(data
->thread
== NULL
)
380 device
->ExtraData
= NULL
;
381 free(data
->mix_data
);
389 static void oss_close_capture(ALCdevice
*device
)
391 oss_data
*data
= (oss_data
*)device
->ExtraData
;
393 StopThread(data
->thread
);
397 DestroyRingBuffer(data
->ring
);
399 free(data
->mix_data
);
401 device
->ExtraData
= NULL
;
404 static void oss_start_capture(ALCdevice
*pDevice
)
406 oss_data
*data
= (oss_data
*)pDevice
->ExtraData
;
410 static void oss_stop_capture(ALCdevice
*pDevice
)
412 oss_data
*data
= (oss_data
*)pDevice
->ExtraData
;
416 static void oss_capture_samples(ALCdevice
*pDevice
, ALCvoid
*pBuffer
, ALCuint lSamples
)
418 oss_data
*data
= (oss_data
*)pDevice
->ExtraData
;
419 if(lSamples
<= (ALCuint
)RingBufferSize(data
->ring
))
420 ReadRingBuffer(data
->ring
, pBuffer
, lSamples
);
422 SetALCError(ALC_INVALID_VALUE
);
425 static ALCuint
oss_available_samples(ALCdevice
*pDevice
)
427 oss_data
*data
= (oss_data
*)pDevice
->ExtraData
;
428 return RingBufferSize(data
->ring
);
432 BackendFuncs oss_funcs
= {
440 oss_available_samples
443 void alc_oss_init(BackendFuncs
*func_list
)
445 *func_list
= oss_funcs
;
447 oss_device
= AppendDeviceList("OSS Software");
448 AppendAllDeviceList(oss_device
);
450 oss_device_capture
= AppendCaptureDeviceList("OSS Capture");