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 #include <portaudio.h>
35 static void *pa_handle
;
36 #define MAKE_FUNC(x) static typeof(x) * p##x
37 MAKE_FUNC(Pa_Initialize
);
38 MAKE_FUNC(Pa_Terminate
);
39 MAKE_FUNC(Pa_GetErrorText
);
40 MAKE_FUNC(Pa_StartStream
);
41 MAKE_FUNC(Pa_StopStream
);
42 MAKE_FUNC(Pa_OpenStream
);
43 MAKE_FUNC(Pa_CloseStream
);
44 MAKE_FUNC(Pa_GetDefaultOutputDevice
);
45 MAKE_FUNC(Pa_GetStreamInfo
);
49 static const ALCchar pa_device
[] = "PortAudio Software";
50 static const ALCchar pa_capture
[] = "PortAudio Capture";
51 static volatile ALuint load_count
;
61 pa_handle
= LoadLibrary("portaudio.dll");
62 #define LOAD_FUNC(x) do { \
63 p##x = (typeof(p##x))GetProcAddress(pa_handle, #x); \
65 AL_PRINT("Could not load %s from portaudio.dll\n", #x); \
66 FreeLibrary(pa_handle); \
72 #elif defined(HAVE_DLFCN_H)
75 #if defined(__APPLE__) && defined(__MACH__)
76 # define PALIB "libportaudio.2.dylib"
78 # define PALIB "libportaudio.so.2"
80 pa_handle
= dlopen(PALIB
, RTLD_NOW
);
83 #define LOAD_FUNC(f) do { \
84 p##f = (typeof(f)*)dlsym(pa_handle, #f); \
85 if((str=dlerror()) != NULL) \
89 AL_PRINT("Could not load %s from "PALIB": %s\n", #f, str); \
95 pa_handle
= (void*)0xDEADBEEF;
96 #define LOAD_FUNC(f) p##f = f
102 LOAD_FUNC(Pa_Initialize
);
103 LOAD_FUNC(Pa_Terminate
);
104 LOAD_FUNC(Pa_GetErrorText
);
105 LOAD_FUNC(Pa_StartStream
);
106 LOAD_FUNC(Pa_StopStream
);
107 LOAD_FUNC(Pa_OpenStream
);
108 LOAD_FUNC(Pa_CloseStream
);
109 LOAD_FUNC(Pa_GetDefaultOutputDevice
);
110 LOAD_FUNC(Pa_GetStreamInfo
);
114 if((err
=pPa_Initialize()) != paNoError
)
116 AL_PRINT("Pa_Initialize() returned an error: %s\n", pPa_GetErrorText(err
));
118 FreeLibrary(pa_handle
);
119 #elif defined(HAVE_DLFCN_H)
133 if(load_count
== 0 || --load_count
> 0)
138 FreeLibrary(pa_handle
);
139 #elif defined(HAVE_DLFCN_H)
154 static int pa_callback(const void *inputBuffer
, void *outputBuffer
,
155 unsigned long framesPerBuffer
, const PaStreamCallbackTimeInfo
*timeInfo
,
156 const PaStreamCallbackFlags statusFlags
, void *userData
)
158 ALCdevice
*device
= (ALCdevice
*)userData
;
164 aluMixData(device
, outputBuffer
, framesPerBuffer
);
168 static int pa_capture_cb(const void *inputBuffer
, void *outputBuffer
,
169 unsigned long framesPerBuffer
, const PaStreamCallbackTimeInfo
*timeInfo
,
170 const PaStreamCallbackFlags statusFlags
, void *userData
)
172 ALCdevice
*device
= (ALCdevice
*)userData
;
173 pa_data
*data
= (pa_data
*)device
->ExtraData
;
179 WriteRingBuffer(data
->ring
, inputBuffer
, framesPerBuffer
);
184 static ALCboolean
pa_open_playback(ALCdevice
*device
, const ALCchar
*deviceName
)
186 const PaStreamInfo
*streamInfo
;
187 PaStreamParameters outParams
;
192 deviceName
= pa_device
;
193 else if(strcmp(deviceName
, pa_device
) != 0)
199 data
= (pa_data
*)calloc(1, sizeof(pa_data
));
200 data
->update_size
= device
->UpdateSize
;
202 device
->ExtraData
= data
;
204 outParams
.device
= GetConfigValueInt("port", "device", -1);
205 if(outParams
.device
< 0)
206 outParams
.device
= pPa_GetDefaultOutputDevice();
207 outParams
.suggestedLatency
= (device
->UpdateSize
*device
->NumUpdates
) /
208 (float)device
->Frequency
;
209 outParams
.hostApiSpecificStreamInfo
= NULL
;
211 switch(aluBytesFromFormat(device
->Format
))
214 outParams
.sampleFormat
= paUInt8
;
217 outParams
.sampleFormat
= paInt16
;
220 outParams
.sampleFormat
= paFloat32
;
223 AL_PRINT("Unknown format: 0x%x\n", device
->Format
);
224 device
->ExtraData
= NULL
;
229 outParams
.channelCount
= aluChannelsFromFormat(device
->Format
);
231 SetDefaultChannelOrder(device
);
233 err
= pPa_OpenStream(&data
->stream
, NULL
, &outParams
, device
->Frequency
,
234 device
->UpdateSize
, paNoFlag
, pa_callback
, device
);
237 AL_PRINT("Pa_OpenStream() returned an error: %s\n", pPa_GetErrorText(err
));
238 device
->ExtraData
= NULL
;
243 streamInfo
= pPa_GetStreamInfo(data
->stream
);
245 device
->szDeviceName
= strdup(deviceName
);
246 device
->Frequency
= streamInfo
->sampleRate
;
251 static void pa_close_playback(ALCdevice
*device
)
253 pa_data
*data
= (pa_data
*)device
->ExtraData
;
256 err
= pPa_CloseStream(data
->stream
);
258 AL_PRINT("Error closing stream: %s\n", pPa_GetErrorText(err
));
261 device
->ExtraData
= NULL
;
266 static ALCboolean
pa_reset_playback(ALCdevice
*device
)
268 pa_data
*data
= (pa_data
*)device
->ExtraData
;
269 const PaStreamInfo
*streamInfo
;
272 streamInfo
= pPa_GetStreamInfo(data
->stream
);
273 device
->Frequency
= streamInfo
->sampleRate
;
274 device
->UpdateSize
= data
->update_size
;
276 err
= pPa_StartStream(data
->stream
);
279 AL_PRINT("Pa_StartStream() returned an error: %s\n", pPa_GetErrorText(err
));
286 static void pa_stop_playback(ALCdevice
*device
)
288 pa_data
*data
= (pa_data
*)device
->ExtraData
;
291 err
= pPa_StopStream(data
->stream
);
293 AL_PRINT("Error stopping stream: %s\n", pPa_GetErrorText(err
));
297 static ALCboolean
pa_open_capture(ALCdevice
*device
, const ALCchar
*deviceName
)
299 PaStreamParameters inParams
;
305 deviceName
= pa_capture
;
306 else if(strcmp(deviceName
, pa_capture
) != 0)
312 data
= (pa_data
*)calloc(1, sizeof(pa_data
));
315 alcSetError(device
, ALC_OUT_OF_MEMORY
);
319 frame_size
= aluChannelsFromFormat(device
->Format
) *
320 aluBytesFromFormat(device
->Format
);
321 data
->ring
= CreateRingBuffer(frame_size
, device
->UpdateSize
*device
->NumUpdates
);
322 if(data
->ring
== NULL
)
324 alcSetError(device
, ALC_OUT_OF_MEMORY
);
328 inParams
.device
= GetConfigValueInt("port", "capture", -1);
329 if(inParams
.device
< 0)
330 inParams
.device
= pPa_GetDefaultOutputDevice();
331 inParams
.suggestedLatency
= 0.0f
;
332 inParams
.hostApiSpecificStreamInfo
= NULL
;
334 switch(aluBytesFromFormat(device
->Format
))
337 inParams
.sampleFormat
= paUInt8
;
340 inParams
.sampleFormat
= paInt16
;
343 inParams
.sampleFormat
= paFloat32
;
346 AL_PRINT("Unknown format: 0x%x\n", device
->Format
);
349 inParams
.channelCount
= aluChannelsFromFormat(device
->Format
);
351 err
= pPa_OpenStream(&data
->stream
, &inParams
, NULL
, device
->Frequency
,
352 paFramesPerBufferUnspecified
, paNoFlag
, pa_capture_cb
, device
);
355 AL_PRINT("Pa_OpenStream() returned an error: %s\n", pPa_GetErrorText(err
));
359 device
->szDeviceName
= strdup(deviceName
);
361 device
->ExtraData
= data
;
365 DestroyRingBuffer(data
->ring
);
371 static void pa_close_capture(ALCdevice
*device
)
373 pa_data
*data
= (pa_data
*)device
->ExtraData
;
376 err
= pPa_CloseStream(data
->stream
);
378 AL_PRINT("Error closing stream: %s\n", pPa_GetErrorText(err
));
381 device
->ExtraData
= NULL
;
386 static void pa_start_capture(ALCdevice
*device
)
388 pa_data
*data
= device
->ExtraData
;
391 err
= pPa_StartStream(data
->stream
);
393 AL_PRINT("Error starting stream: %s\n", pPa_GetErrorText(err
));
396 static void pa_stop_capture(ALCdevice
*device
)
398 pa_data
*data
= (pa_data
*)device
->ExtraData
;
401 err
= pPa_StopStream(data
->stream
);
403 AL_PRINT("Error stopping stream: %s\n", pPa_GetErrorText(err
));
406 static void pa_capture_samples(ALCdevice
*device
, ALCvoid
*buffer
, ALCuint samples
)
408 pa_data
*data
= device
->ExtraData
;
409 if(samples
<= (ALCuint
)RingBufferSize(data
->ring
))
410 ReadRingBuffer(data
->ring
, buffer
, samples
);
412 alcSetError(device
, ALC_INVALID_VALUE
);
415 static ALCuint
pa_available_samples(ALCdevice
*device
)
417 pa_data
*data
= device
->ExtraData
;
418 return RingBufferSize(data
->ring
);
422 static const BackendFuncs pa_funcs
= {
435 void alc_pa_init(BackendFuncs
*func_list
)
437 *func_list
= pa_funcs
;
440 void alc_pa_deinit(void)
444 void alc_pa_probe(int type
)
446 if(!pa_load()) return;
448 if(type
== DEVICE_PROBE
)
449 AppendDeviceList(pa_device
);
450 else if(type
== ALL_DEVICE_PROBE
)
451 AppendAllDeviceList(pa_device
);
452 else if(type
== CAPTURE_DEVICE_PROBE
)
453 AppendCaptureDeviceList(pa_capture
);