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
;
86 int len
= data
->data_size
- remaining
;
91 aluMixData(pDevice
->Context
, data
->mix_data
+remaining
, len
, pDevice
->Format
);
96 wrote
= write(data
->fd
, data
->mix_data
, remaining
);
99 AL_PRINT("write failed: %s\n", strerror(errno
));
106 memmove(data
->mix_data
, data
->mix_data
+wrote
, remaining
);
115 static ALuint
OSSCaptureProc(ALvoid
*ptr
)
117 ALCdevice
*pDevice
= (ALCdevice
*)ptr
;
118 oss_data
*data
= (oss_data
*)pDevice
->ExtraData
;
122 frameSize
= aluBytesFromFormat(pDevice
->Format
);
123 frameSize
*= aluChannelsFromFormat(pDevice
->Format
);
125 while(!data
->killNow
)
127 amt
= read(data
->fd
, data
->mix_data
, data
->data_size
);
130 AL_PRINT("read failed: %s\n", strerror(errno
));
139 WriteRingBuffer(data
->ring
, data
->mix_data
, amt
/frameSize
);
145 static ALCboolean
oss_open_playback(ALCdevice
*device
, const ALCchar
*deviceName
)
147 int numFragmentsLogSize
;
148 int log2FragmentSize
;
149 unsigned int periods
;
159 strncpy(driver
, GetConfigValue("oss", "device", "/dev/dsp"), sizeof(driver
)-1);
160 driver
[sizeof(driver
)-1] = 0;
163 if(strcmp(deviceName
, oss_device
))
165 device
->szDeviceName
= oss_device
;
168 device
->szDeviceName
= oss_device
;
170 data
= (oss_data
*)calloc(1, sizeof(oss_data
));
173 data
->fd
= open(driver
, O_WRONLY
);
177 AL_PRINT("Could not open %s: %s\n", driver
, strerror(errno
));
181 switch(aluBytesFromFormat(device
->Format
))
184 data
->silence
= 0x80;
189 ossFormat
= AFMT_S16_NE
;
193 AL_PRINT("Unknown format?! %x\n", device
->Format
);
196 periods
= GetConfigValueInt("oss", "periods", 4);
197 if((int)periods
<= 0)
199 numChannels
= device
->Channels
;
200 ossSpeed
= device
->Frequency
;
201 log2FragmentSize
= log2i(device
->UpdateFreq
* device
->FrameSize
/ periods
);
203 /* according to the OSS spec, 16 bytes are the minimum */
204 if (log2FragmentSize
< 4)
205 log2FragmentSize
= 4;
206 numFragmentsLogSize
= (periods
<< 16) | log2FragmentSize
;
208 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
209 if (!(ok(ioctl(data
->fd
, SNDCTL_DSP_SETFRAGMENT
, &numFragmentsLogSize
), "set fragment") &&
210 ok(ioctl(data
->fd
, SNDCTL_DSP_SETFMT
, &ossFormat
), "set format") &&
211 ok(ioctl(data
->fd
, SNDCTL_DSP_CHANNELS
, &numChannels
), "set channels") &&
212 ok(ioctl(data
->fd
, SNDCTL_DSP_SPEED
, &ossSpeed
), "set speed") &&
213 ok(ioctl(data
->fd
, SNDCTL_DSP_GETOSPACE
, &info
), "get space")))
215 AL_PRINT("%s failed: %s\n", err
, strerror(errno
));
222 device
->Frequency
= ossSpeed
;
224 if((int)device
->Channels
!= numChannels
)
226 AL_PRINT("Could not set %d channels, got %d instead\n", device
->Channels
, numChannels
);
232 if(!((ossFormat
== AFMT_U8
&& aluBytesFromFormat(device
->Format
) == 1) ||
233 (ossFormat
== AFMT_S16_NE
&& aluBytesFromFormat(device
->Format
) == 2)))
235 AL_PRINT("Could not set %d-bit output, got format %#x\n", aluBytesFromFormat(device
->Format
)*8, ossFormat
);
241 device
->UpdateFreq
= info
.fragsize
/ device
->FrameSize
;
243 data
->data_size
= device
->UpdateFreq
* device
->FrameSize
;
244 data
->mix_data
= calloc(1, data
->data_size
);
246 device
->MaxNoOfSources
= 256;
248 device
->ExtraData
= data
;
249 data
->thread
= StartThread(OSSProc
, device
);
250 if(data
->thread
== NULL
)
252 device
->ExtraData
= NULL
;
253 free(data
->mix_data
);
261 static void oss_close_playback(ALCdevice
*device
)
263 oss_data
*data
= (oss_data
*)device
->ExtraData
;
265 StopThread(data
->thread
);
269 free(data
->mix_data
);
271 device
->ExtraData
= NULL
;
275 static ALCboolean
oss_open_capture(ALCdevice
*device
, const ALCchar
*deviceName
, ALCuint frequency
, ALCenum format
, ALCsizei SampleSize
)
277 int numFragmentsLogSize
;
278 int log2FragmentSize
;
279 unsigned int periods
;
289 strncpy(driver
, GetConfigValue("oss", "capture", "/dev/dsp"), sizeof(driver
)-1);
290 driver
[sizeof(driver
)-1] = 0;
293 if(strcmp(deviceName
, oss_device_capture
))
295 device
->szDeviceName
= oss_device_capture
;
298 device
->szDeviceName
= oss_device_capture
;
300 data
= (oss_data
*)calloc(1, sizeof(oss_data
));
303 data
->fd
= open(driver
, O_RDONLY
);
307 AL_PRINT("Could not open %s: %s\n", driver
, strerror(errno
));
311 switch(aluBytesFromFormat(format
))
314 data
->silence
= 0x80;
319 ossFormat
= AFMT_S16_NE
;
323 AL_PRINT("Unknown format?! %x\n", device
->Format
);
327 numChannels
= device
->Channels
;
328 ossSpeed
= frequency
;
329 log2FragmentSize
= log2i(device
->UpdateFreq
* device
->FrameSize
/ periods
);
331 /* according to the OSS spec, 16 bytes are the minimum */
332 if (log2FragmentSize
< 4)
333 log2FragmentSize
= 4;
334 numFragmentsLogSize
= (periods
<< 16) | log2FragmentSize
;
336 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
337 if (!(ok(ioctl(data
->fd
, SNDCTL_DSP_SETFRAGMENT
, &numFragmentsLogSize
), "set fragment") &&
338 ok(ioctl(data
->fd
, SNDCTL_DSP_SETFMT
, &ossFormat
), "set format") &&
339 ok(ioctl(data
->fd
, SNDCTL_DSP_CHANNELS
, &numChannels
), "set channels") &&
340 ok(ioctl(data
->fd
, SNDCTL_DSP_SPEED
, &ossSpeed
), "set speed") &&
341 ok(ioctl(data
->fd
, SNDCTL_DSP_GETISPACE
, &info
), "get space")))
343 AL_PRINT("%s failed: %s\n", err
, strerror(errno
));
350 device
->Channels
= numChannels
;
351 device
->Frequency
= ossSpeed
;
353 if(ossFormat
== AFMT_U8
)
355 if(device
->Channels
== 1)
357 device
->Format
= AL_FORMAT_MONO8
;
358 device
->FrameSize
= 1;
360 else if(device
->Channels
== 2)
362 device
->Format
= AL_FORMAT_STEREO8
;
363 device
->FrameSize
= 2;
365 else if(device
->Channels
== 4)
367 device
->Format
= AL_FORMAT_QUAD8
;
368 device
->FrameSize
= 4;
371 else if(ossFormat
== AFMT_S16_NE
)
373 if(device
->Channels
== 1)
375 device
->Format
= AL_FORMAT_MONO16
;
376 device
->FrameSize
= 2;
378 else if(device
->Channels
== 2)
380 device
->Format
= AL_FORMAT_STEREO16
;
381 device
->FrameSize
= 4;
383 else if(device
->Channels
== 4)
385 device
->Format
= AL_FORMAT_QUAD16
;
386 device
->FrameSize
= 8;
392 AL_PRINT("returned unknown format: %#x %d!\n", ossFormat
, numChannels
);
398 data
->ring
= CreateRingBuffer(device
->FrameSize
, SampleSize
);
401 AL_PRINT("ring buffer create failed\n");
407 device
->UpdateFreq
= info
.fragsize
/ device
->FrameSize
;
409 data
->data_size
= device
->UpdateFreq
* device
->FrameSize
* info
.fragments
;
410 data
->mix_data
= calloc(1, data
->data_size
);
412 device
->ExtraData
= data
;
413 data
->thread
= StartThread(OSSCaptureProc
, device
);
414 if(data
->thread
== NULL
)
416 device
->ExtraData
= NULL
;
417 free(data
->mix_data
);
425 static void oss_close_capture(ALCdevice
*device
)
427 oss_data
*data
= (oss_data
*)device
->ExtraData
;
429 StopThread(data
->thread
);
433 DestroyRingBuffer(data
->ring
);
435 free(data
->mix_data
);
437 device
->ExtraData
= NULL
;
440 static void oss_start_capture(ALCdevice
*pDevice
)
442 oss_data
*data
= (oss_data
*)pDevice
->ExtraData
;
446 static void oss_stop_capture(ALCdevice
*pDevice
)
448 oss_data
*data
= (oss_data
*)pDevice
->ExtraData
;
452 static void oss_capture_samples(ALCdevice
*pDevice
, ALCvoid
*pBuffer
, ALCuint lSamples
)
454 oss_data
*data
= (oss_data
*)pDevice
->ExtraData
;
455 if(lSamples
<= (ALCuint
)RingBufferSize(data
->ring
))
456 ReadRingBuffer(data
->ring
, pBuffer
, lSamples
);
458 SetALCError(ALC_INVALID_VALUE
);
461 static ALCuint
oss_available_samples(ALCdevice
*pDevice
)
463 oss_data
*data
= (oss_data
*)pDevice
->ExtraData
;
464 return RingBufferSize(data
->ring
);
468 BackendFuncs oss_funcs
= {
476 oss_available_samples
479 void alc_oss_init(BackendFuncs
*func_list
)
481 *func_list
= oss_funcs
;
483 oss_device
= AppendDeviceList("OSS Software");
484 AppendAllDeviceList(oss_device
);
486 oss_device_capture
= AppendCaptureDeviceList("OSS Capture");