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
33 static const ALCchar sndio_device
[] = "SndIO Default";
36 static ALCboolean
sndio_load(void)
43 struct sio_hdl
*sndHandle
;
53 static ALuint
sndio_proc(ALvoid
*ptr
)
55 ALCdevice
*device
= ptr
;
56 sndio_data
*data
= device
->ExtraData
;
62 frameSize
= FrameSizeFromDevFmt(device
->FmtChans
, device
->FmtType
);
64 while(!data
->killNow
&& device
->Connected
)
66 ALsizei len
= data
->data_size
;
67 ALubyte
*WritePtr
= data
->mix_data
;
69 aluMixData(device
, WritePtr
, len
/frameSize
);
70 while(len
> 0 && !data
->killNow
)
72 wrote
= sio_write(data
->sndHandle
, WritePtr
, len
);
75 ERR("sio_write failed\n");
76 aluHandleDisconnect(device
);
90 static ALCenum
sndio_open_playback(ALCdevice
*device
, const ALCchar
*deviceName
)
95 deviceName
= sndio_device
;
96 else if(strcmp(deviceName
, sndio_device
) != 0)
97 return ALC_INVALID_VALUE
;
99 data
= calloc(1, sizeof(*data
));
102 data
->sndHandle
= sio_open(NULL
, SIO_PLAY
, 0);
103 if(data
->sndHandle
== NULL
)
106 ERR("Could not open device\n");
107 return ALC_INVALID_VALUE
;
110 device
->DeviceName
= strdup(deviceName
);
111 device
->ExtraData
= data
;
116 static void sndio_close_playback(ALCdevice
*device
)
118 sndio_data
*data
= device
->ExtraData
;
120 sio_close(data
->sndHandle
);
122 device
->ExtraData
= NULL
;
125 static ALCboolean
sndio_reset_playback(ALCdevice
*device
)
127 sndio_data
*data
= device
->ExtraData
;
132 par
.rate
= device
->Frequency
;
133 par
.pchan
= ((device
->FmtChans
!= DevFmtMono
) ? 2 : 1);
135 switch(device
->FmtType
)
163 par
.le
= SIO_LE_NATIVE
;
165 par
.round
= device
->UpdateSize
;
166 par
.appbufsz
= device
->UpdateSize
* (device
->NumUpdates
-1);
167 if(!par
.appbufsz
) par
.appbufsz
= device
->UpdateSize
;
169 if(!sio_setpar(data
->sndHandle
, &par
) || !sio_getpar(data
->sndHandle
, &par
))
171 ERR("Failed to set device parameters\n");
175 if(par
.bits
!= par
.bps
*8)
177 ERR("Padded samples not supported (%u of %u bits)\n", par
.bits
, par
.bps
*8);
181 device
->Frequency
= par
.rate
;
182 device
->FmtChans
= ((par
.pchan
==1) ? DevFmtMono
: DevFmtStereo
);
184 if(par
.bits
== 8 && par
.sig
== 1)
185 device
->FmtType
= DevFmtByte
;
186 else if(par
.bits
== 8 && par
.sig
== 0)
187 device
->FmtType
= DevFmtUByte
;
188 else if(par
.bits
== 16 && par
.sig
== 1)
189 device
->FmtType
= DevFmtShort
;
190 else if(par
.bits
== 16 && par
.sig
== 0)
191 device
->FmtType
= DevFmtUShort
;
192 else if(par
.bits
== 32 && par
.sig
== 1)
193 device
->FmtType
= DevFmtInt
;
194 else if(par
.bits
== 32 && par
.sig
== 0)
195 device
->FmtType
= DevFmtUInt
;
198 ERR("Unhandled sample format: %s %u-bit\n", (par
.sig
?"signed":"unsigned"), par
.bits
);
202 device
->UpdateSize
= par
.round
;
203 device
->NumUpdates
= (par
.bufsz
/par
.round
) + 1;
205 SetDefaultChannelOrder(device
);
210 static ALCboolean
sndio_start_playback(ALCdevice
*device
)
212 sndio_data
*data
= device
->ExtraData
;
214 if(!sio_start(data
->sndHandle
))
216 ERR("Error starting playback\n");
220 data
->data_size
= device
->UpdateSize
* FrameSizeFromDevFmt(device
->FmtChans
, device
->FmtType
);
221 data
->mix_data
= calloc(1, data
->data_size
);
223 data
->thread
= StartThread(sndio_proc
, device
);
224 if(data
->thread
== NULL
)
226 sio_stop(data
->sndHandle
);
227 free(data
->mix_data
);
228 data
->mix_data
= NULL
;
235 static void sndio_stop_playback(ALCdevice
*device
)
237 sndio_data
*data
= device
->ExtraData
;
243 StopThread(data
->thread
);
247 if(!sio_stop(data
->sndHandle
))
248 ERR("Error stopping device\n");
250 free(data
->mix_data
);
251 data
->mix_data
= NULL
;
255 static const BackendFuncs sndio_funcs
= {
257 sndio_close_playback
,
258 sndio_reset_playback
,
259 sndio_start_playback
,
267 ALCdevice_LockDefault
,
268 ALCdevice_UnlockDefault
,
269 ALCdevice_GetLatencyDefault
272 ALCboolean
alc_sndio_init(BackendFuncs
*func_list
)
276 *func_list
= sndio_funcs
;
280 void alc_sndio_deinit(void)
284 void alc_sndio_probe(enum DevProbe type
)
288 case ALL_DEVICE_PROBE
:
289 AppendAllDevicesList(sndio_device
);
291 case CAPTURE_DEVICE_PROBE
: