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 Default";
59 pa_handle
= LoadLibrary("portaudio.dll");
60 #define LOAD_FUNC(x) do { \
61 p##x = (typeof(p##x))GetProcAddress(pa_handle, #x); \
63 AL_PRINT("Could not load %s from portaudio.dll\n", #x); \
64 FreeLibrary(pa_handle); \
70 #elif defined(HAVE_DLFCN_H)
73 #if defined(__APPLE__) && defined(__MACH__)
74 # define PALIB "libportaudio.2.dylib"
76 # define PALIB "libportaudio.so.2"
78 pa_handle
= dlopen(PALIB
, RTLD_NOW
);
81 #define LOAD_FUNC(f) do { \
82 p##f = (typeof(f)*)dlsym(pa_handle, #f); \
83 if((str=dlerror()) != NULL) \
87 AL_PRINT("Could not load %s from "PALIB": %s\n", #f, str); \
93 pa_handle
= (void*)0xDEADBEEF;
94 #define LOAD_FUNC(f) p##f = f
100 LOAD_FUNC(Pa_Initialize
);
101 LOAD_FUNC(Pa_Terminate
);
102 LOAD_FUNC(Pa_GetErrorText
);
103 LOAD_FUNC(Pa_StartStream
);
104 LOAD_FUNC(Pa_StopStream
);
105 LOAD_FUNC(Pa_OpenStream
);
106 LOAD_FUNC(Pa_CloseStream
);
107 LOAD_FUNC(Pa_GetDefaultOutputDevice
);
108 LOAD_FUNC(Pa_GetStreamInfo
);
112 if((err
=pPa_Initialize()) != paNoError
)
114 AL_PRINT("Pa_Initialize() returned an error: %s\n", pPa_GetErrorText(err
));
116 FreeLibrary(pa_handle
);
117 #elif defined(HAVE_DLFCN_H)
136 static int pa_callback(const void *inputBuffer
, void *outputBuffer
,
137 unsigned long framesPerBuffer
, const PaStreamCallbackTimeInfo
*timeInfo
,
138 const PaStreamCallbackFlags statusFlags
, void *userData
)
140 ALCdevice
*device
= (ALCdevice
*)userData
;
146 aluMixData(device
, outputBuffer
, framesPerBuffer
);
150 static int pa_capture_cb(const void *inputBuffer
, void *outputBuffer
,
151 unsigned long framesPerBuffer
, const PaStreamCallbackTimeInfo
*timeInfo
,
152 const PaStreamCallbackFlags statusFlags
, void *userData
)
154 ALCdevice
*device
= (ALCdevice
*)userData
;
155 pa_data
*data
= (pa_data
*)device
->ExtraData
;
161 WriteRingBuffer(data
->ring
, inputBuffer
, framesPerBuffer
);
166 static ALCboolean
pa_open_playback(ALCdevice
*device
, const ALCchar
*deviceName
)
168 const PaStreamInfo
*streamInfo
;
169 PaStreamParameters outParams
;
174 deviceName
= pa_device
;
175 else if(strcmp(deviceName
, pa_device
) != 0)
181 data
= (pa_data
*)calloc(1, sizeof(pa_data
));
182 data
->update_size
= device
->UpdateSize
;
184 device
->ExtraData
= data
;
186 outParams
.device
= GetConfigValueInt("port", "device", -1);
187 if(outParams
.device
< 0)
188 outParams
.device
= pPa_GetDefaultOutputDevice();
189 outParams
.suggestedLatency
= (device
->UpdateSize
*device
->NumUpdates
) /
190 (float)device
->Frequency
;
191 outParams
.hostApiSpecificStreamInfo
= NULL
;
193 switch(aluBytesFromFormat(device
->Format
))
196 outParams
.sampleFormat
= paUInt8
;
199 outParams
.sampleFormat
= paInt16
;
202 outParams
.sampleFormat
= paFloat32
;
205 AL_PRINT("Unknown format: 0x%x\n", device
->Format
);
206 device
->ExtraData
= NULL
;
210 outParams
.channelCount
= aluChannelsFromFormat(device
->Format
);
212 SetDefaultChannelOrder(device
);
214 err
= pPa_OpenStream(&data
->stream
, NULL
, &outParams
, device
->Frequency
,
215 device
->UpdateSize
, paNoFlag
, pa_callback
, device
);
218 AL_PRINT("Pa_OpenStream() returned an error: %s\n", pPa_GetErrorText(err
));
219 device
->ExtraData
= NULL
;
223 streamInfo
= pPa_GetStreamInfo(data
->stream
);
225 device
->szDeviceName
= strdup(deviceName
);
226 device
->Frequency
= streamInfo
->sampleRate
;
231 static void pa_close_playback(ALCdevice
*device
)
233 pa_data
*data
= (pa_data
*)device
->ExtraData
;
236 err
= pPa_CloseStream(data
->stream
);
238 AL_PRINT("Error closing stream: %s\n", pPa_GetErrorText(err
));
241 device
->ExtraData
= NULL
;
244 static ALCboolean
pa_reset_playback(ALCdevice
*device
)
246 pa_data
*data
= (pa_data
*)device
->ExtraData
;
247 const PaStreamInfo
*streamInfo
;
250 streamInfo
= pPa_GetStreamInfo(data
->stream
);
251 device
->Frequency
= streamInfo
->sampleRate
;
252 device
->UpdateSize
= data
->update_size
;
254 err
= pPa_StartStream(data
->stream
);
257 AL_PRINT("Pa_StartStream() returned an error: %s\n", pPa_GetErrorText(err
));
264 static void pa_stop_playback(ALCdevice
*device
)
266 pa_data
*data
= (pa_data
*)device
->ExtraData
;
269 err
= pPa_StopStream(data
->stream
);
271 AL_PRINT("Error stopping stream: %s\n", pPa_GetErrorText(err
));
275 static ALCboolean
pa_open_capture(ALCdevice
*device
, const ALCchar
*deviceName
)
277 PaStreamParameters inParams
;
283 deviceName
= pa_device
;
284 else if(strcmp(deviceName
, pa_device
) != 0)
290 data
= (pa_data
*)calloc(1, sizeof(pa_data
));
293 alcSetError(device
, ALC_OUT_OF_MEMORY
);
297 frame_size
= aluFrameSizeFromFormat(device
->Format
);
298 data
->ring
= CreateRingBuffer(frame_size
, device
->UpdateSize
*device
->NumUpdates
);
299 if(data
->ring
== NULL
)
301 alcSetError(device
, ALC_OUT_OF_MEMORY
);
305 inParams
.device
= GetConfigValueInt("port", "capture", -1);
306 if(inParams
.device
< 0)
307 inParams
.device
= pPa_GetDefaultOutputDevice();
308 inParams
.suggestedLatency
= 0.0f
;
309 inParams
.hostApiSpecificStreamInfo
= NULL
;
311 switch(aluBytesFromFormat(device
->Format
))
314 inParams
.sampleFormat
= paUInt8
;
317 inParams
.sampleFormat
= paInt16
;
320 inParams
.sampleFormat
= paFloat32
;
323 AL_PRINT("Unknown format: 0x%x\n", device
->Format
);
326 inParams
.channelCount
= aluChannelsFromFormat(device
->Format
);
328 err
= pPa_OpenStream(&data
->stream
, &inParams
, NULL
, device
->Frequency
,
329 paFramesPerBufferUnspecified
, paNoFlag
, pa_capture_cb
, device
);
332 AL_PRINT("Pa_OpenStream() returned an error: %s\n", pPa_GetErrorText(err
));
336 device
->szDeviceName
= strdup(deviceName
);
338 device
->ExtraData
= data
;
342 DestroyRingBuffer(data
->ring
);
347 static void pa_close_capture(ALCdevice
*device
)
349 pa_data
*data
= (pa_data
*)device
->ExtraData
;
352 err
= pPa_CloseStream(data
->stream
);
354 AL_PRINT("Error closing stream: %s\n", pPa_GetErrorText(err
));
357 device
->ExtraData
= NULL
;
360 static void pa_start_capture(ALCdevice
*device
)
362 pa_data
*data
= device
->ExtraData
;
365 err
= pPa_StartStream(data
->stream
);
367 AL_PRINT("Error starting stream: %s\n", pPa_GetErrorText(err
));
370 static void pa_stop_capture(ALCdevice
*device
)
372 pa_data
*data
= (pa_data
*)device
->ExtraData
;
375 err
= pPa_StopStream(data
->stream
);
377 AL_PRINT("Error stopping stream: %s\n", pPa_GetErrorText(err
));
380 static void pa_capture_samples(ALCdevice
*device
, ALCvoid
*buffer
, ALCuint samples
)
382 pa_data
*data
= device
->ExtraData
;
383 if(samples
<= (ALCuint
)RingBufferSize(data
->ring
))
384 ReadRingBuffer(data
->ring
, buffer
, samples
);
386 alcSetError(device
, ALC_INVALID_VALUE
);
389 static ALCuint
pa_available_samples(ALCdevice
*device
)
391 pa_data
*data
= device
->ExtraData
;
392 return RingBufferSize(data
->ring
);
396 static const BackendFuncs pa_funcs
= {
409 void alc_pa_init(BackendFuncs
*func_list
)
411 *func_list
= pa_funcs
;
414 void alc_pa_deinit(void)
420 FreeLibrary(pa_handle
);
421 #elif defined(HAVE_DLFCN_H)
428 void alc_pa_probe(int type
)
430 if(!pa_load()) return;
432 if(type
== DEVICE_PROBE
)
433 AppendDeviceList(pa_device
);
434 else if(type
== ALL_DEVICE_PROBE
)
435 AppendAllDeviceList(pa_device
);
436 else if(type
== CAPTURE_DEVICE_PROBE
)
437 AppendCaptureDeviceList(pa_device
);