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
30 #include <portaudio.h>
33 static const ALCchar pa_device
[] = "PortAudio Default";
37 static void *pa_handle
;
38 #define MAKE_FUNC(x) static typeof(x) * p##x
39 MAKE_FUNC(Pa_Initialize
);
40 MAKE_FUNC(Pa_Terminate
);
41 MAKE_FUNC(Pa_GetErrorText
);
42 MAKE_FUNC(Pa_StartStream
);
43 MAKE_FUNC(Pa_StopStream
);
44 MAKE_FUNC(Pa_OpenStream
);
45 MAKE_FUNC(Pa_CloseStream
);
46 MAKE_FUNC(Pa_GetDefaultOutputDevice
);
47 MAKE_FUNC(Pa_GetStreamInfo
);
50 #define Pa_Initialize pPa_Initialize
51 #define Pa_Terminate pPa_Terminate
52 #define Pa_GetErrorText pPa_GetErrorText
53 #define Pa_StartStream pPa_StartStream
54 #define Pa_StopStream pPa_StopStream
55 #define Pa_OpenStream pPa_OpenStream
56 #define Pa_CloseStream pPa_CloseStream
57 #define Pa_GetDefaultOutputDevice pPa_GetDefaultOutputDevice
58 #define Pa_GetStreamInfo pPa_GetStreamInfo
61 static ALCboolean
pa_load(void)
69 # define PALIB "portaudio.dll"
70 #elif defined(__APPLE__) && defined(__MACH__)
71 # define PALIB "libportaudio.2.dylib"
72 #elif defined(__OpenBSD__)
73 # define PALIB "libportaudio.so"
75 # define PALIB "libportaudio.so.2"
78 pa_handle
= LoadLib(PALIB
);
82 #define LOAD_FUNC(f) do { \
83 p##f = GetSymbol(pa_handle, #f); \
86 CloseLib(pa_handle); \
91 LOAD_FUNC(Pa_Initialize
);
92 LOAD_FUNC(Pa_Terminate
);
93 LOAD_FUNC(Pa_GetErrorText
);
94 LOAD_FUNC(Pa_StartStream
);
95 LOAD_FUNC(Pa_StopStream
);
96 LOAD_FUNC(Pa_OpenStream
);
97 LOAD_FUNC(Pa_CloseStream
);
98 LOAD_FUNC(Pa_GetDefaultOutputDevice
);
99 LOAD_FUNC(Pa_GetStreamInfo
);
102 if((err
=Pa_Initialize()) != paNoError
)
104 ERR("Pa_Initialize() returned an error: %s\n", Pa_GetErrorText(err
));
111 if((err
=Pa_Initialize()) != paNoError
)
113 ERR("Pa_Initialize() returned an error: %s\n", Pa_GetErrorText(err
));
129 static int pa_callback(const void *inputBuffer
, void *outputBuffer
,
130 unsigned long framesPerBuffer
, const PaStreamCallbackTimeInfo
*timeInfo
,
131 const PaStreamCallbackFlags statusFlags
, void *userData
)
133 ALCdevice
*device
= (ALCdevice
*)userData
;
139 aluMixData(device
, outputBuffer
, framesPerBuffer
);
143 static int pa_capture_cb(const void *inputBuffer
, void *outputBuffer
,
144 unsigned long framesPerBuffer
, const PaStreamCallbackTimeInfo
*timeInfo
,
145 const PaStreamCallbackFlags statusFlags
, void *userData
)
147 ALCdevice
*device
= (ALCdevice
*)userData
;
148 pa_data
*data
= (pa_data
*)device
->ExtraData
;
154 WriteRingBuffer(data
->ring
, inputBuffer
, framesPerBuffer
);
159 static ALCenum
pa_open_playback(ALCdevice
*device
, const ALCchar
*deviceName
)
161 PaStreamParameters outParams
;
166 deviceName
= pa_device
;
167 else if(strcmp(deviceName
, pa_device
) != 0)
168 return ALC_INVALID_VALUE
;
170 data
= (pa_data
*)calloc(1, sizeof(pa_data
));
171 data
->update_size
= device
->UpdateSize
;
173 device
->ExtraData
= data
;
175 outParams
.device
= -1;
176 if(!ConfigValueInt("port", "device", &outParams
.device
) || outParams
.device
< 0)
177 outParams
.device
= Pa_GetDefaultOutputDevice();
178 outParams
.suggestedLatency
= (device
->UpdateSize
*device
->NumUpdates
) /
179 (float)device
->Frequency
;
180 outParams
.hostApiSpecificStreamInfo
= NULL
;
182 outParams
.channelCount
= ((device
->FmtChans
== DevFmtMono
) ? 1 : 2);
185 switch(device
->FmtType
)
188 outParams
.sampleFormat
= paInt8
;
191 outParams
.sampleFormat
= paUInt8
;
194 device
->FmtType
= DevFmtShort
;
197 outParams
.sampleFormat
= paInt16
;
200 device
->FmtType
= DevFmtInt
;
203 outParams
.sampleFormat
= paInt32
;
206 outParams
.sampleFormat
= paFloat32
;
210 err
= Pa_OpenStream(&data
->stream
, NULL
, &outParams
, device
->Frequency
,
211 device
->UpdateSize
, paNoFlag
, pa_callback
, device
);
214 if(device
->FmtType
== DevFmtFloat
)
216 device
->FmtType
= DevFmtShort
;
219 ERR("Pa_OpenStream() returned an error: %s\n", Pa_GetErrorText(err
));
220 device
->ExtraData
= NULL
;
222 return ALC_INVALID_VALUE
;
225 if((ALuint
)outParams
.channelCount
!= ChannelsFromDevFmt(device
->FmtChans
))
227 if(outParams
.channelCount
!= 1 && outParams
.channelCount
!= 2)
229 ERR("Unhandled channel count: %u\n", outParams
.channelCount
);
230 Pa_CloseStream(data
->stream
);
231 device
->ExtraData
= NULL
;
233 return ALC_INVALID_VALUE
;
235 if((device
->Flags
&DEVICE_CHANNELS_REQUEST
))
236 ERR("Failed to set %s, got %u channels instead\n", DevFmtChannelsString(device
->FmtChans
), outParams
.channelCount
);
237 device
->Flags
&= ~DEVICE_CHANNELS_REQUEST
;
238 device
->FmtChans
= ((outParams
.channelCount
==1) ? DevFmtMono
: DevFmtStereo
);
240 SetDefaultChannelOrder(device
);
242 device
->szDeviceName
= strdup(deviceName
);
247 static void pa_close_playback(ALCdevice
*device
)
249 pa_data
*data
= (pa_data
*)device
->ExtraData
;
252 err
= Pa_CloseStream(data
->stream
);
254 ERR("Error closing stream: %s\n", Pa_GetErrorText(err
));
257 device
->ExtraData
= NULL
;
260 static ALCboolean
pa_reset_playback(ALCdevice
*device
)
262 pa_data
*data
= (pa_data
*)device
->ExtraData
;
263 const PaStreamInfo
*streamInfo
;
265 streamInfo
= Pa_GetStreamInfo(data
->stream
);
266 device
->Frequency
= streamInfo
->sampleRate
;
267 device
->UpdateSize
= data
->update_size
;
272 static ALCboolean
pa_start_playback(ALCdevice
*device
)
274 pa_data
*data
= (pa_data
*)device
->ExtraData
;
277 err
= Pa_StartStream(data
->stream
);
280 ERR("Pa_StartStream() returned an error: %s\n", Pa_GetErrorText(err
));
287 static void pa_stop_playback(ALCdevice
*device
)
289 pa_data
*data
= (pa_data
*)device
->ExtraData
;
292 err
= Pa_StopStream(data
->stream
);
294 ERR("Error stopping stream: %s\n", Pa_GetErrorText(err
));
298 static ALCenum
pa_open_capture(ALCdevice
*device
, const ALCchar
*deviceName
)
300 PaStreamParameters inParams
;
306 deviceName
= pa_device
;
307 else if(strcmp(deviceName
, pa_device
) != 0)
308 return ALC_INVALID_VALUE
;
310 data
= (pa_data
*)calloc(1, sizeof(pa_data
));
312 return ALC_OUT_OF_MEMORY
;
314 frame_size
= FrameSizeFromDevFmt(device
->FmtChans
, device
->FmtType
);
315 data
->ring
= CreateRingBuffer(frame_size
, device
->UpdateSize
*device
->NumUpdates
);
316 if(data
->ring
== NULL
)
319 inParams
.device
= -1;
320 if(!ConfigValueInt("port", "capture", &inParams
.device
) || inParams
.device
< 0)
321 inParams
.device
= Pa_GetDefaultOutputDevice();
322 inParams
.suggestedLatency
= 0.0f
;
323 inParams
.hostApiSpecificStreamInfo
= NULL
;
325 switch(device
->FmtType
)
328 inParams
.sampleFormat
= paInt8
;
331 inParams
.sampleFormat
= paUInt8
;
334 inParams
.sampleFormat
= paInt16
;
337 inParams
.sampleFormat
= paInt32
;
340 inParams
.sampleFormat
= paFloat32
;
344 ERR("%s samples not supported\n", DevFmtTypeString(device
->FmtType
));
347 inParams
.channelCount
= ChannelsFromDevFmt(device
->FmtChans
);
349 err
= Pa_OpenStream(&data
->stream
, &inParams
, NULL
, device
->Frequency
,
350 paFramesPerBufferUnspecified
, paNoFlag
, pa_capture_cb
, device
);
353 ERR("Pa_OpenStream() returned an error: %s\n", Pa_GetErrorText(err
));
357 device
->szDeviceName
= strdup(deviceName
);
359 device
->ExtraData
= data
;
363 DestroyRingBuffer(data
->ring
);
365 return ALC_INVALID_VALUE
;
368 static void pa_close_capture(ALCdevice
*device
)
370 pa_data
*data
= (pa_data
*)device
->ExtraData
;
373 err
= Pa_CloseStream(data
->stream
);
375 ERR("Error closing stream: %s\n", Pa_GetErrorText(err
));
378 device
->ExtraData
= NULL
;
381 static void pa_start_capture(ALCdevice
*device
)
383 pa_data
*data
= device
->ExtraData
;
386 err
= Pa_StartStream(data
->stream
);
388 ERR("Error starting stream: %s\n", Pa_GetErrorText(err
));
391 static void pa_stop_capture(ALCdevice
*device
)
393 pa_data
*data
= (pa_data
*)device
->ExtraData
;
396 err
= Pa_StopStream(data
->stream
);
398 ERR("Error stopping stream: %s\n", Pa_GetErrorText(err
));
401 static ALCenum
pa_capture_samples(ALCdevice
*device
, ALCvoid
*buffer
, ALCuint samples
)
403 pa_data
*data
= device
->ExtraData
;
404 ReadRingBuffer(data
->ring
, buffer
, samples
);
408 static ALCuint
pa_available_samples(ALCdevice
*device
)
410 pa_data
*data
= device
->ExtraData
;
411 return RingBufferSize(data
->ring
);
415 static const BackendFuncs pa_funcs
= {
429 ALCboolean
alc_pa_init(BackendFuncs
*func_list
)
433 *func_list
= pa_funcs
;
437 void alc_pa_deinit(void)
451 void alc_pa_probe(enum DevProbe type
)
455 case ALL_DEVICE_PROBE
:
456 AppendAllDeviceList(pa_device
);
458 case CAPTURE_DEVICE_PROBE
:
459 AppendCaptureDeviceList(pa_device
);