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 <sys/ioctl.h>
22 #include <sys/types.h>
35 #include <sys/soundcard.h>
38 * The OSS documentation talks about SOUND_MIXER_READ, but the header
39 * only contains MIXER_READ. Play safe. Same for WRITE.
41 #ifndef SOUND_MIXER_READ
42 #define SOUND_MIXER_READ MIXER_READ
44 #ifndef SOUND_MIXER_WRITE
45 #define SOUND_MIXER_WRITE MIXER_WRITE
48 static char *oss_device
;
49 static char *oss_device_capture
;
65 static int log2i(ALCuint x
)
77 static ALuint
OSSProc(ALvoid
*ptr
)
79 ALCdevice
*pDevice
= (ALCdevice
*)ptr
;
80 oss_data
*data
= (oss_data
*)pDevice
->ExtraData
;
87 aluMixData(pDevice
->Context
,data
->mix_data
,data
->data_size
,pDevice
->Format
);
90 remaining
= data
->data_size
;
93 wrote
= write(data
->fd
, data
->mix_data
+data
->data_size
-remaining
, remaining
);
96 AL_PRINT("write failed: %s\n", strerror(errno
));
111 static ALuint
OSSCaptureProc(ALvoid
*ptr
)
113 ALCdevice
*pDevice
= (ALCdevice
*)ptr
;
114 oss_data
*data
= (oss_data
*)pDevice
->ExtraData
;
118 frameSize
= aluBytesFromFormat(pDevice
->Format
);
119 frameSize
*= aluChannelsFromFormat(pDevice
->Format
);
121 while(!data
->killNow
)
123 amt
= read(data
->fd
, data
->mix_data
, data
->data_size
);
126 AL_PRINT("read failed: %s\n", strerror(errno
));
135 WriteRingBuffer(data
->ring
, data
->mix_data
, amt
/frameSize
);
141 static ALCboolean
oss_open_playback(ALCdevice
*device
, const ALCchar
*deviceName
)
143 int numFragmentsLogSize
;
144 int log2FragmentSize
;
145 unsigned int periods
;
155 strncpy(driver
, GetConfigValue("oss", "device", "/dev/dsp"), sizeof(driver
)-1);
156 driver
[sizeof(driver
)-1] = 0;
159 if(strcmp(deviceName
, oss_device
))
161 device
->szDeviceName
= oss_device
;
164 device
->szDeviceName
= oss_device
;
166 data
= (oss_data
*)calloc(1, sizeof(oss_data
));
169 data
->fd
= open(driver
, O_WRONLY
);
173 AL_PRINT("Could not open %s: %s\n", driver
, strerror(errno
));
177 switch(aluBytesFromFormat(device
->Format
))
180 data
->silence
= 0x80;
185 ossFormat
= AFMT_S16_NE
;
189 AL_PRINT("Unknown format?! %x\n", device
->Format
);
192 periods
= GetConfigValueInt("oss", "periods", 4);
195 numChannels
= device
->Channels
;
196 ossSpeed
= device
->Frequency
;
197 log2FragmentSize
= log2i(device
->UpdateFreq
* device
->FrameSize
/ periods
);
199 /* according to the OSS spec, 16 bytes are the minimum */
200 if (log2FragmentSize
< 4)
201 log2FragmentSize
= 4;
202 numFragmentsLogSize
= (periods
<< 16) | log2FragmentSize
;
204 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
205 if (!(ok(ioctl(data
->fd
, SNDCTL_DSP_SETFRAGMENT
, &numFragmentsLogSize
), "set fragment") &&
206 ok(ioctl(data
->fd
, SNDCTL_DSP_SETFMT
, &ossFormat
), "set format") &&
207 ok(ioctl(data
->fd
, SNDCTL_DSP_CHANNELS
, &numChannels
), "set channels") &&
208 ok(ioctl(data
->fd
, SNDCTL_DSP_SPEED
, &ossSpeed
), "set speed") &&
209 ok(ioctl(data
->fd
, SNDCTL_DSP_GETOSPACE
, &info
), "get space")))
211 AL_PRINT("%s failed: %s\n", err
, strerror(errno
));
218 device
->Channels
= numChannels
;
219 device
->Frequency
= ossSpeed
;
221 if(ossFormat
== AFMT_U8
)
223 if(device
->Channels
== 1)
225 device
->Format
= AL_FORMAT_MONO8
;
226 device
->FrameSize
= 1;
228 else if(device
->Channels
== 2)
230 device
->Format
= AL_FORMAT_STEREO8
;
231 device
->FrameSize
= 2;
233 else if(device
->Channels
== 4)
235 device
->Format
= AL_FORMAT_QUAD8
;
236 device
->FrameSize
= 4;
239 else if(ossFormat
== AFMT_S16_NE
)
241 if(device
->Channels
== 1)
243 device
->Format
= AL_FORMAT_MONO16
;
244 device
->FrameSize
= 2;
246 else if(device
->Channels
== 2)
248 device
->Format
= AL_FORMAT_STEREO16
;
249 device
->FrameSize
= 4;
251 else if(device
->Channels
== 4)
253 device
->Format
= AL_FORMAT_QUAD16
;
254 device
->FrameSize
= 8;
260 AL_PRINT("returned unknown format: %#x %d!\n", ossFormat
, numChannels
);
266 device
->UpdateFreq
= info
.fragsize
/ device
->FrameSize
;
268 data
->data_size
= device
->UpdateFreq
* device
->FrameSize
;
269 data
->mix_data
= calloc(1, data
->data_size
);
271 device
->MaxNoOfSources
= 256;
273 device
->ExtraData
= data
;
274 data
->thread
= StartThread(OSSProc
, device
);
275 if(data
->thread
== NULL
)
277 device
->ExtraData
= NULL
;
278 free(data
->mix_data
);
286 static void oss_close_playback(ALCdevice
*device
)
288 oss_data
*data
= (oss_data
*)device
->ExtraData
;
290 StopThread(data
->thread
);
294 free(data
->mix_data
);
296 device
->ExtraData
= NULL
;
300 static ALCboolean
oss_open_capture(ALCdevice
*device
, const ALCchar
*deviceName
, ALCuint frequency
, ALCenum format
, ALCsizei SampleSize
)
302 int numFragmentsLogSize
;
303 int log2FragmentSize
;
304 unsigned int periods
;
314 strncpy(driver
, GetConfigValue("oss", "capture", "/dev/dsp"), sizeof(driver
)-1);
315 driver
[sizeof(driver
)-1] = 0;
318 if(strcmp(deviceName
, oss_device_capture
))
320 device
->szDeviceName
= oss_device_capture
;
323 device
->szDeviceName
= oss_device_capture
;
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(format
))
339 data
->silence
= 0x80;
344 ossFormat
= AFMT_S16_NE
;
348 AL_PRINT("Unknown format?! %x\n", device
->Format
);
352 numChannels
= device
->Channels
;
353 ossSpeed
= frequency
;
354 log2FragmentSize
= log2i(device
->UpdateFreq
* device
->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 device
->Channels
= numChannels
;
376 device
->Frequency
= ossSpeed
;
378 if(ossFormat
== AFMT_U8
)
380 if(device
->Channels
== 1)
382 device
->Format
= AL_FORMAT_MONO8
;
383 device
->FrameSize
= 1;
385 else if(device
->Channels
== 2)
387 device
->Format
= AL_FORMAT_STEREO8
;
388 device
->FrameSize
= 2;
390 else if(device
->Channels
== 4)
392 device
->Format
= AL_FORMAT_QUAD8
;
393 device
->FrameSize
= 4;
396 else if(ossFormat
== AFMT_S16_NE
)
398 if(device
->Channels
== 1)
400 device
->Format
= AL_FORMAT_MONO16
;
401 device
->FrameSize
= 2;
403 else if(device
->Channels
== 2)
405 device
->Format
= AL_FORMAT_STEREO16
;
406 device
->FrameSize
= 4;
408 else if(device
->Channels
== 4)
410 device
->Format
= AL_FORMAT_QUAD16
;
411 device
->FrameSize
= 8;
417 AL_PRINT("returned unknown format: %#x %d!\n", ossFormat
, numChannels
);
423 data
->ring
= CreateRingBuffer(device
->FrameSize
, SampleSize
);
426 AL_PRINT("ring buffer create failed\n");
432 device
->UpdateFreq
= info
.fragsize
/ device
->FrameSize
;
434 data
->data_size
= device
->UpdateFreq
* device
->FrameSize
;
435 data
->mix_data
= calloc(1, data
->data_size
);
437 device
->ExtraData
= data
;
438 data
->thread
= StartThread(OSSCaptureProc
, device
);
439 if(data
->thread
== NULL
)
441 device
->ExtraData
= NULL
;
442 free(data
->mix_data
);
450 static void oss_close_capture(ALCdevice
*device
)
452 oss_data
*data
= (oss_data
*)device
->ExtraData
;
454 StopThread(data
->thread
);
458 DestroyRingBuffer(data
->ring
);
460 free(data
->mix_data
);
462 device
->ExtraData
= NULL
;
465 static void oss_start_capture(ALCdevice
*pDevice
)
467 oss_data
*data
= (oss_data
*)pDevice
->ExtraData
;
471 static void oss_stop_capture(ALCdevice
*pDevice
)
473 oss_data
*data
= (oss_data
*)pDevice
->ExtraData
;
477 static void oss_capture_samples(ALCdevice
*pDevice
, ALCvoid
*pBuffer
, ALCuint lSamples
)
479 oss_data
*data
= (oss_data
*)pDevice
->ExtraData
;
480 if(lSamples
<= (ALCuint
)RingBufferSize(data
->ring
))
481 ReadRingBuffer(data
->ring
, pBuffer
, lSamples
);
483 SetALCError(ALC_INVALID_VALUE
);
486 static ALCuint
oss_available_samples(ALCdevice
*pDevice
)
488 oss_data
*data
= (oss_data
*)pDevice
->ExtraData
;
489 return RingBufferSize(data
->ring
);
493 BackendFuncs oss_funcs
= {
501 oss_available_samples
504 void alc_oss_init(BackendFuncs
*func_list
)
506 *func_list
= oss_funcs
;
508 oss_device
= AppendDeviceList("OSS Software");
509 AppendAllDeviceList(oss_device
);
511 oss_device_capture
= AppendCaptureDeviceList("OSS Capture");