Fix non-dynamic PortAudio
[openal-soft/android/lowlatency.git] / Alc / portaudio.c
bloba06aa4dcd33a9a35787a6bbff627abd81aad12a9
1 /**
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 "config.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include "alMain.h"
27 #include "AL/al.h"
28 #include "AL/alc.h"
29 #ifdef HAVE_DLFCN_H
30 #include <dlfcn.h>
31 #endif
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);
46 #undef MAKE_FUNC
49 static const ALCchar pa_device[] = "PortAudio Software";
50 static const ALCchar pa_capture[] = "PortAudio Capture";
51 static volatile ALuint load_count;
54 void *pa_load(void)
56 if(load_count == 0)
58 PaError err;
60 #ifdef _WIN32
61 pa_handle = LoadLibrary("portaudio.dll");
62 #define LOAD_FUNC(x) do { \
63 p##x = (typeof(p##x))GetProcAddress(pa_handle, #x); \
64 if(!(p##x)) { \
65 AL_PRINT("Could not load %s from portaudio.dll\n", #x); \
66 FreeLibrary(pa_handle); \
67 pa_handle = NULL; \
68 return NULL; \
69 } \
70 } while(0)
72 #elif defined(HAVE_DLFCN_H)
74 const char *str;
75 #if defined(__APPLE__) && defined(__MACH__)
76 # define PALIB "libportaudio.2.dylib"
77 #else
78 # define PALIB "libportaudio.so.2"
79 #endif
80 pa_handle = dlopen(PALIB, RTLD_NOW);
81 dlerror();
83 #define LOAD_FUNC(f) do { \
84 p##f = (typeof(f)*)dlsym(pa_handle, #f); \
85 if((str=dlerror()) != NULL) \
86 { \
87 dlclose(pa_handle); \
88 pa_handle = NULL; \
89 AL_PRINT("Could not load %s from "PALIB": %s\n", #f, str); \
90 return NULL; \
91 } \
92 } while(0)
94 #else
95 pa_handle = (void*)0xDEADBEEF;
96 #define LOAD_FUNC(f) p##f = f
97 #endif
99 if(!pa_handle)
100 return NULL;
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);
112 #undef LOAD_FUNC
114 if((err=pPa_Initialize()) != paNoError)
116 AL_PRINT("Pa_Initialize() returned an error: %s\n", pPa_GetErrorText(err));
117 #ifdef _WIN32
118 FreeLibrary(pa_handle);
119 #elif defined(HAVE_DLFCN_H)
120 dlclose(pa_handle);
121 #endif
122 pa_handle = NULL;
123 return NULL;
126 ++load_count;
128 return pa_handle;
131 void pa_unload(void)
133 if(load_count == 0 || --load_count > 0)
134 return;
136 pPa_Terminate();
137 #ifdef _WIN32
138 FreeLibrary(pa_handle);
139 #elif defined(HAVE_DLFCN_H)
140 dlclose(pa_handle);
141 #endif
142 pa_handle = NULL;
146 typedef struct {
147 PaStream *stream;
148 ALuint update_size;
150 RingBuffer *ring;
151 } pa_data;
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;
160 (void)inputBuffer;
161 (void)timeInfo;
162 (void)statusFlags;
164 aluMixData(device, outputBuffer, framesPerBuffer);
165 return 0;
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;
175 (void)outputBuffer;
176 (void)timeInfo;
177 (void)statusFlags;
179 WriteRingBuffer(data->ring, inputBuffer, framesPerBuffer);
180 return 0;
184 static ALCboolean pa_open_playback(ALCdevice *device, const ALCchar *deviceName)
186 const PaStreamInfo *streamInfo;
187 PaStreamParameters outParams;
188 pa_data *data;
189 PaError err;
191 if(!deviceName)
192 deviceName = pa_device;
193 else if(strcmp(deviceName, pa_device) != 0)
194 return ALC_FALSE;
196 if(!pa_load())
197 return ALC_FALSE;
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))
213 case 1:
214 outParams.sampleFormat = paUInt8;
215 break;
216 case 2:
217 outParams.sampleFormat = paInt16;
218 break;
219 case 4:
220 outParams.sampleFormat = paFloat32;
221 break;
222 default:
223 AL_PRINT("Unknown format: 0x%x\n", device->Format);
224 device->ExtraData = NULL;
225 free(data);
226 pa_unload();
227 return ALC_FALSE;
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);
235 if(err != paNoError)
237 AL_PRINT("Pa_OpenStream() returned an error: %s\n", pPa_GetErrorText(err));
238 device->ExtraData = NULL;
239 free(data);
240 pa_unload();
241 return ALC_FALSE;
243 streamInfo = pPa_GetStreamInfo(data->stream);
245 device->szDeviceName = strdup(deviceName);
246 device->Frequency = streamInfo->sampleRate;
248 return ALC_TRUE;
251 static void pa_close_playback(ALCdevice *device)
253 pa_data *data = (pa_data*)device->ExtraData;
254 PaError err;
256 err = pPa_CloseStream(data->stream);
257 if(err != paNoError)
258 AL_PRINT("Error closing stream: %s\n", pPa_GetErrorText(err));
260 free(data);
261 device->ExtraData = NULL;
263 pa_unload();
266 static ALCboolean pa_reset_playback(ALCdevice *device)
268 pa_data *data = (pa_data*)device->ExtraData;
269 const PaStreamInfo *streamInfo;
270 PaError err;
272 streamInfo = pPa_GetStreamInfo(data->stream);
273 device->Frequency = streamInfo->sampleRate;
274 device->UpdateSize = data->update_size;
276 err = pPa_StartStream(data->stream);
277 if(err != paNoError)
279 AL_PRINT("Pa_StartStream() returned an error: %s\n", pPa_GetErrorText(err));
280 return ALC_FALSE;
283 return ALC_TRUE;
286 static void pa_stop_playback(ALCdevice *device)
288 pa_data *data = (pa_data*)device->ExtraData;
289 PaError err;
291 err = pPa_StopStream(data->stream);
292 if(err != paNoError)
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;
300 ALuint frame_size;
301 pa_data *data;
302 PaError err;
304 if(!deviceName)
305 deviceName = pa_capture;
306 else if(strcmp(deviceName, pa_capture) != 0)
307 return ALC_FALSE;
309 if(!pa_load())
310 return ALC_FALSE;
312 data = (pa_data*)calloc(1, sizeof(pa_data));
313 if(data == NULL)
315 alcSetError(device, ALC_OUT_OF_MEMORY);
316 return ALC_FALSE;
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);
325 goto error;
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))
336 case 1:
337 inParams.sampleFormat = paUInt8;
338 break;
339 case 2:
340 inParams.sampleFormat = paInt16;
341 break;
342 case 4:
343 inParams.sampleFormat = paFloat32;
344 break;
345 default:
346 AL_PRINT("Unknown format: 0x%x\n", device->Format);
347 goto error;
349 inParams.channelCount = aluChannelsFromFormat(device->Format);
351 err = pPa_OpenStream(&data->stream, &inParams, NULL, device->Frequency,
352 paFramesPerBufferUnspecified, paNoFlag, pa_capture_cb, device);
353 if(err != paNoError)
355 AL_PRINT("Pa_OpenStream() returned an error: %s\n", pPa_GetErrorText(err));
356 goto error;
359 device->szDeviceName = strdup(deviceName);
361 device->ExtraData = data;
362 return ALC_TRUE;
364 error:
365 DestroyRingBuffer(data->ring);
366 free(data);
367 pa_unload();
368 return ALC_FALSE;
371 static void pa_close_capture(ALCdevice *device)
373 pa_data *data = (pa_data*)device->ExtraData;
374 PaError err;
376 err = pPa_CloseStream(data->stream);
377 if(err != paNoError)
378 AL_PRINT("Error closing stream: %s\n", pPa_GetErrorText(err));
380 free(data);
381 device->ExtraData = NULL;
383 pa_unload();
386 static void pa_start_capture(ALCdevice *device)
388 pa_data *data = device->ExtraData;
389 PaError err;
391 err = pPa_StartStream(data->stream);
392 if(err != paNoError)
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;
399 PaError err;
401 err = pPa_StopStream(data->stream);
402 if(err != paNoError)
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);
411 else
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 = {
423 pa_open_playback,
424 pa_close_playback,
425 pa_reset_playback,
426 pa_stop_playback,
427 pa_open_capture,
428 pa_close_capture,
429 pa_start_capture,
430 pa_stop_capture,
431 pa_capture_samples,
432 pa_available_samples
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);
455 pa_unload();