Don't define lib handles when dynamic loading is disabled
[openal-soft.git] / Alc / backends / portaudio.c
blob816224946978a1cf96f5d528aabc158162be4e59
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"
30 #include <portaudio.h>
33 static const ALCchar pa_device[] = "PortAudio Default";
36 #ifdef HAVE_DYNLOAD
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);
48 #undef MAKE_FUNC
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
59 #endif
61 static ALCboolean pa_load(void)
63 PaError err;
65 #ifdef HAVE_DYNLOAD
66 if(!pa_handle)
68 #ifdef _WIN32
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"
74 #else
75 # define PALIB "libportaudio.so.2"
76 #endif
78 pa_handle = LoadLib(PALIB);
79 if(!pa_handle)
80 return ALC_FALSE;
82 #define LOAD_FUNC(f) do { \
83 p##f = GetSymbol(pa_handle, #f); \
84 if(p##f == NULL) \
85 { \
86 CloseLib(pa_handle); \
87 pa_handle = NULL; \
88 return ALC_FALSE; \
89 } \
90 } while(0)
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);
100 #undef LOAD_FUNC
102 if((err=Pa_Initialize()) != paNoError)
104 ERR("Pa_Initialize() returned an error: %s\n", Pa_GetErrorText(err));
105 CloseLib(pa_handle);
106 pa_handle = NULL;
107 return ALC_FALSE;
110 #else
111 if((err=Pa_Initialize()) != paNoError)
113 ERR("Pa_Initialize() returned an error: %s\n", Pa_GetErrorText(err));
114 return ALC_FALSE;
116 #endif
117 return ALC_TRUE;
121 typedef struct {
122 PaStream *stream;
123 ALuint update_size;
125 RingBuffer *ring;
126 } pa_data;
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;
135 (void)inputBuffer;
136 (void)timeInfo;
137 (void)statusFlags;
139 aluMixData(device, outputBuffer, framesPerBuffer);
140 return 0;
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;
150 (void)outputBuffer;
151 (void)timeInfo;
152 (void)statusFlags;
154 WriteRingBuffer(data->ring, inputBuffer, framesPerBuffer);
155 return 0;
159 static ALCenum pa_open_playback(ALCdevice *device, const ALCchar *deviceName)
161 PaStreamParameters outParams;
162 pa_data *data;
163 PaError err;
165 if(!deviceName)
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);
184 retry_open:
185 switch(device->FmtType)
187 case DevFmtByte:
188 outParams.sampleFormat = paInt8;
189 break;
190 case DevFmtUByte:
191 outParams.sampleFormat = paUInt8;
192 break;
193 case DevFmtUShort:
194 device->FmtType = DevFmtShort;
195 /* fall-through */
196 case DevFmtShort:
197 outParams.sampleFormat = paInt16;
198 break;
199 case DevFmtUInt:
200 device->FmtType = DevFmtInt;
201 /* fall-through */
202 case DevFmtInt:
203 outParams.sampleFormat = paInt32;
204 break;
205 case DevFmtFloat:
206 outParams.sampleFormat = paFloat32;
207 break;
210 err = Pa_OpenStream(&data->stream, NULL, &outParams, device->Frequency,
211 device->UpdateSize, paNoFlag, pa_callback, device);
212 if(err != paNoError)
214 if(device->FmtType == DevFmtFloat)
216 device->FmtType = DevFmtShort;
217 goto retry_open;
219 ERR("Pa_OpenStream() returned an error: %s\n", Pa_GetErrorText(err));
220 device->ExtraData = NULL;
221 free(data);
222 return ALC_INVALID_VALUE;
225 device->szDeviceName = strdup(deviceName);
227 if((ALuint)outParams.channelCount != ChannelsFromDevFmt(device->FmtChans))
229 if(outParams.channelCount != 1 && outParams.channelCount != 2)
231 ERR("Unhandled channel count: %u\n", outParams.channelCount);
232 Pa_CloseStream(data->stream);
233 device->ExtraData = NULL;
234 free(data);
235 return ALC_INVALID_VALUE;
237 if((device->Flags&DEVICE_CHANNELS_REQUEST))
238 ERR("Failed to set %s, got %u channels instead\n", DevFmtChannelsString(device->FmtChans), outParams.channelCount);
239 device->Flags &= ~DEVICE_CHANNELS_REQUEST;
240 device->FmtChans = ((outParams.channelCount==1) ? DevFmtMono : DevFmtStereo);
242 SetDefaultChannelOrder(device);
244 return ALC_NO_ERROR;
247 static void pa_close_playback(ALCdevice *device)
249 pa_data *data = (pa_data*)device->ExtraData;
250 PaError err;
252 err = Pa_CloseStream(data->stream);
253 if(err != paNoError)
254 ERR("Error closing stream: %s\n", Pa_GetErrorText(err));
256 free(data);
257 device->ExtraData = NULL;
260 static ALCboolean pa_reset_playback(ALCdevice *device)
262 pa_data *data = (pa_data*)device->ExtraData;
263 const PaStreamInfo *streamInfo;
264 PaError err;
266 streamInfo = Pa_GetStreamInfo(data->stream);
267 device->Frequency = streamInfo->sampleRate;
268 device->UpdateSize = data->update_size;
270 err = Pa_StartStream(data->stream);
271 if(err != paNoError)
273 ERR("Pa_StartStream() returned an error: %s\n", Pa_GetErrorText(err));
274 return ALC_FALSE;
277 return ALC_TRUE;
280 static void pa_stop_playback(ALCdevice *device)
282 pa_data *data = (pa_data*)device->ExtraData;
283 PaError err;
285 err = Pa_StopStream(data->stream);
286 if(err != paNoError)
287 ERR("Error stopping stream: %s\n", Pa_GetErrorText(err));
291 static ALCenum pa_open_capture(ALCdevice *device, const ALCchar *deviceName)
293 PaStreamParameters inParams;
294 ALuint frame_size;
295 pa_data *data;
296 PaError err;
298 if(!deviceName)
299 deviceName = pa_device;
300 else if(strcmp(deviceName, pa_device) != 0)
301 return ALC_INVALID_VALUE;
303 data = (pa_data*)calloc(1, sizeof(pa_data));
304 if(data == NULL)
305 return ALC_OUT_OF_MEMORY;
307 frame_size = FrameSizeFromDevFmt(device->FmtChans, device->FmtType);
308 data->ring = CreateRingBuffer(frame_size, device->UpdateSize*device->NumUpdates);
309 if(data->ring == NULL)
310 goto error;
312 inParams.device = -1;
313 if(!ConfigValueInt("port", "capture", &inParams.device) || inParams.device < 0)
314 inParams.device = Pa_GetDefaultOutputDevice();
315 inParams.suggestedLatency = 0.0f;
316 inParams.hostApiSpecificStreamInfo = NULL;
318 switch(device->FmtType)
320 case DevFmtByte:
321 inParams.sampleFormat = paInt8;
322 break;
323 case DevFmtUByte:
324 inParams.sampleFormat = paUInt8;
325 break;
326 case DevFmtShort:
327 inParams.sampleFormat = paInt16;
328 break;
329 case DevFmtInt:
330 inParams.sampleFormat = paInt32;
331 break;
332 case DevFmtFloat:
333 inParams.sampleFormat = paFloat32;
334 break;
335 case DevFmtUInt:
336 case DevFmtUShort:
337 ERR("%s samples not supported\n", DevFmtTypeString(device->FmtType));
338 goto error;
340 inParams.channelCount = ChannelsFromDevFmt(device->FmtChans);
342 err = Pa_OpenStream(&data->stream, &inParams, NULL, device->Frequency,
343 paFramesPerBufferUnspecified, paNoFlag, pa_capture_cb, device);
344 if(err != paNoError)
346 ERR("Pa_OpenStream() returned an error: %s\n", Pa_GetErrorText(err));
347 goto error;
350 device->szDeviceName = strdup(deviceName);
352 device->ExtraData = data;
353 return ALC_NO_ERROR;
355 error:
356 DestroyRingBuffer(data->ring);
357 free(data);
358 return ALC_INVALID_VALUE;
361 static void pa_close_capture(ALCdevice *device)
363 pa_data *data = (pa_data*)device->ExtraData;
364 PaError err;
366 err = Pa_CloseStream(data->stream);
367 if(err != paNoError)
368 ERR("Error closing stream: %s\n", Pa_GetErrorText(err));
370 free(data);
371 device->ExtraData = NULL;
374 static void pa_start_capture(ALCdevice *device)
376 pa_data *data = device->ExtraData;
377 PaError err;
379 err = Pa_StartStream(data->stream);
380 if(err != paNoError)
381 ERR("Error starting stream: %s\n", Pa_GetErrorText(err));
384 static void pa_stop_capture(ALCdevice *device)
386 pa_data *data = (pa_data*)device->ExtraData;
387 PaError err;
389 err = Pa_StopStream(data->stream);
390 if(err != paNoError)
391 ERR("Error stopping stream: %s\n", Pa_GetErrorText(err));
394 static ALCenum pa_capture_samples(ALCdevice *device, ALCvoid *buffer, ALCuint samples)
396 pa_data *data = device->ExtraData;
397 ReadRingBuffer(data->ring, buffer, samples);
398 return ALC_NO_ERROR;
401 static ALCuint pa_available_samples(ALCdevice *device)
403 pa_data *data = device->ExtraData;
404 return RingBufferSize(data->ring);
408 static const BackendFuncs pa_funcs = {
409 pa_open_playback,
410 pa_close_playback,
411 pa_reset_playback,
412 pa_stop_playback,
413 pa_open_capture,
414 pa_close_capture,
415 pa_start_capture,
416 pa_stop_capture,
417 pa_capture_samples,
418 pa_available_samples
421 ALCboolean alc_pa_init(BackendFuncs *func_list)
423 if(!pa_load())
424 return ALC_FALSE;
425 *func_list = pa_funcs;
426 return ALC_TRUE;
429 void alc_pa_deinit(void)
431 #ifdef HAVE_DYNLOAD
432 if(pa_handle)
434 Pa_Terminate();
435 CloseLib(pa_handle);
436 pa_handle = NULL;
438 #else
439 Pa_Terminate();
440 #endif
443 void alc_pa_probe(enum DevProbe type)
445 switch(type)
447 case ALL_DEVICE_PROBE:
448 AppendAllDeviceList(pa_device);
449 break;
450 case CAPTURE_DEVICE_PROBE:
451 AppendCaptureDeviceList(pa_device);
452 break;