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
;
61 static int log2i(ALCuint x
)
73 static ALuint
OSSProc(ALvoid
*ptr
)
75 ALCdevice
*pDevice
= (ALCdevice
*)ptr
;
76 oss_data
*data
= (oss_data
*)pDevice
->ExtraData
;
83 aluMixData(pDevice
->Context
,data
->mix_data
,data
->data_size
,pDevice
->Format
);
86 remaining
= data
->data_size
;
89 wrote
= write(data
->fd
, data
->mix_data
+data
->data_size
-remaining
, remaining
);
92 AL_PRINT("write failed: %s\n", strerror(errno
));
107 static ALCboolean
oss_open_playback(ALCdevice
*device
, const ALCchar
*deviceName
)
109 int numFragmentsLogSize
;
110 int log2FragmentSize
;
111 unsigned int periods
;
121 strncpy(driver
, GetConfigValue("oss", "device", "/dev/dsp"), sizeof(driver
)-1);
122 driver
[sizeof(driver
)-1] = 0;
125 if(strcmp(deviceName
, oss_device
))
130 strcpy(device
->szDeviceName
, deviceName
);
132 strcpy(device
->szDeviceName
, oss_device
);
134 data
= (oss_data
*)calloc(1, sizeof(oss_data
));
137 data
->fd
= open(driver
, O_WRONLY
);
141 AL_PRINT("Could not open %s: %s\n", driver
, strerror(errno
));
145 switch(device
->Format
)
147 case AL_FORMAT_MONO8
:
148 case AL_FORMAT_STEREO8
:
149 case AL_FORMAT_QUAD8
:
150 data
->silence
= 0x80;
153 case AL_FORMAT_MONO16
:
154 case AL_FORMAT_STEREO16
:
155 case AL_FORMAT_QUAD16
:
157 ossFormat
= AFMT_S16_NE
;
161 AL_PRINT("Unknown format?! %x\n", device
->Format
);
164 periods
= GetConfigValueInt("oss", "periods", 4);
167 numChannels
= device
->Channels
;
168 ossSpeed
= device
->Frequency
;
169 log2FragmentSize
= log2i(device
->UpdateFreq
* device
->FrameSize
/ periods
);
171 /* according to the OSS spec, 16 bytes are the minimum */
172 if (log2FragmentSize
< 4)
173 log2FragmentSize
= 4;
174 numFragmentsLogSize
= (periods
<< 16) | log2FragmentSize
;
176 #define ok(func, str) (i=(func),((i<0)?(err=(str)),0:1))
177 if (!(ok(ioctl(data
->fd
, SNDCTL_DSP_SETFRAGMENT
, &numFragmentsLogSize
), "set fragment") &&
178 ok(ioctl(data
->fd
, SNDCTL_DSP_SETFMT
, &ossFormat
), "set format") &&
179 ok(ioctl(data
->fd
, SNDCTL_DSP_CHANNELS
, &numChannels
), "set channels") &&
180 ok(ioctl(data
->fd
, SNDCTL_DSP_SPEED
, &ossSpeed
), "set speed") &&
181 ok(ioctl(data
->fd
, SNDCTL_DSP_GETOSPACE
, &info
), "get space")))
183 AL_PRINT("%s failed: %s\n", err
, strerror(i
));
190 device
->Channels
= numChannels
;
191 device
->Frequency
= ossSpeed
;
193 if(ossFormat
== AFMT_U8
)
195 if(device
->Channels
== 1)
197 device
->Format
= AL_FORMAT_MONO8
;
198 device
->FrameSize
= 1;
200 else if(device
->Channels
== 2)
202 device
->Format
= AL_FORMAT_STEREO8
;
203 device
->FrameSize
= 2;
205 else if(device
->Channels
== 4)
207 device
->Format
= AL_FORMAT_QUAD8
;
208 device
->FrameSize
= 4;
211 else if(ossFormat
== AFMT_S16_NE
)
213 if(device
->Channels
== 1)
215 device
->Format
= AL_FORMAT_MONO16
;
216 device
->FrameSize
= 2;
218 else if(device
->Channels
== 2)
220 device
->Format
= AL_FORMAT_STEREO16
;
221 device
->FrameSize
= 4;
223 else if(device
->Channels
== 4)
225 device
->Format
= AL_FORMAT_QUAD16
;
226 device
->FrameSize
= 8;
232 AL_PRINT("returned unknown format: %#x %d!\n", ossFormat
, numChannels
);
238 device
->UpdateFreq
= info
.fragsize
/ device
->FrameSize
;
240 data
->data_size
= device
->UpdateFreq
* device
->FrameSize
;
241 data
->mix_data
= calloc(1, data
->data_size
);
243 device
->MaxNoOfSources
= 256;
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
*pDevice
, const ALCchar
*deviceName
, ALCuint frequency
, ALCenum format
, ALCsizei SampleSize
)
282 static void oss_close_capture(ALCdevice
*pDevice
)
287 static void oss_start_capture(ALCdevice
*pDevice
)
292 static void oss_stop_capture(ALCdevice
*pDevice
)
297 static void oss_capture_samples(ALCdevice
*pDevice
, ALCvoid
*pBuffer
, ALCuint lSamples
)
304 static ALCuint
oss_available_samples(ALCdevice
*pDevice
)
311 BackendFuncs oss_funcs
= {
319 oss_available_samples
322 void alc_oss_init(BackendFuncs
*func_list
)
324 *func_list
= oss_funcs
;
326 oss_device
= AppendDeviceList("OSS Software");
327 AppendAllDeviceList(oss_device
);