Remove unnecessary NULL check
[openal-soft.git] / Alc / portaudio.c
blob8281da174c34dc6c843891bbe9828db09d310fed
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 str = NULL;
96 pa_handle = (void*)0xDEADBEEF;
97 #define LOAD_FUNC(f) p##f = f
98 #endif
100 if(!pa_handle)
101 return NULL;
103 LOAD_FUNC(Pa_Initialize);
104 LOAD_FUNC(Pa_Terminate);
105 LOAD_FUNC(Pa_GetErrorText);
106 LOAD_FUNC(Pa_StartStream);
107 LOAD_FUNC(Pa_StopStream);
108 LOAD_FUNC(Pa_OpenStream);
109 LOAD_FUNC(Pa_CloseStream);
110 LOAD_FUNC(Pa_GetDefaultOutputDevice);
111 LOAD_FUNC(Pa_GetStreamInfo);
113 #undef LOAD_FUNC
115 if((err=pPa_Initialize()) != paNoError)
117 AL_PRINT("Pa_Initialize() returned an error: %s\n", pPa_GetErrorText(err));
118 #ifdef _WIN32
119 FreeLibrary(pa_handle);
120 #elif defined(HAVE_DLFCN_H)
121 dlclose(pa_handle);
122 #endif
123 pa_handle = NULL;
124 return NULL;
127 ++load_count;
129 return pa_handle;
132 void pa_unload(void)
134 if(load_count == 0 || --load_count > 0)
135 return;
137 pPa_Terminate();
138 #ifdef _WIN32
139 FreeLibrary(pa_handle);
140 #elif defined(HAVE_DLFCN_H)
141 dlclose(pa_handle);
142 #endif
143 pa_handle = NULL;
147 typedef struct {
148 PaStream *stream;
149 ALuint update_size;
151 RingBuffer *ring;
152 } pa_data;
155 static int pa_callback(const void *inputBuffer, void *outputBuffer,
156 unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *timeInfo,
157 const PaStreamCallbackFlags statusFlags, void *userData)
159 ALCdevice *device = (ALCdevice*)userData;
161 (void)inputBuffer;
162 (void)timeInfo;
163 (void)statusFlags;
165 aluMixData(device, outputBuffer, framesPerBuffer);
166 return 0;
169 static int pa_capture_cb(const void *inputBuffer, void *outputBuffer,
170 unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *timeInfo,
171 const PaStreamCallbackFlags statusFlags, void *userData)
173 ALCdevice *device = (ALCdevice*)userData;
174 pa_data *data = (pa_data*)device->ExtraData;
176 (void)outputBuffer;
177 (void)timeInfo;
178 (void)statusFlags;
180 WriteRingBuffer(data->ring, inputBuffer, framesPerBuffer);
181 return 0;
185 static ALCboolean pa_open_playback(ALCdevice *device, const ALCchar *deviceName)
187 const PaStreamInfo *streamInfo;
188 PaStreamParameters outParams;
189 pa_data *data;
190 PaError err;
192 if(!deviceName)
193 deviceName = pa_device;
194 else if(strcmp(deviceName, pa_device) != 0)
195 return ALC_FALSE;
197 if(!pa_load())
198 return ALC_FALSE;
200 data = (pa_data*)calloc(1, sizeof(pa_data));
201 data->update_size = device->UpdateSize;
203 device->ExtraData = data;
205 outParams.device = GetConfigValueInt("port", "device", -1);
206 if(outParams.device < 0)
207 outParams.device = pPa_GetDefaultOutputDevice();
208 outParams.suggestedLatency = (device->UpdateSize*device->NumUpdates) /
209 (float)device->Frequency;
210 outParams.hostApiSpecificStreamInfo = NULL;
212 switch(aluBytesFromFormat(device->Format))
214 case 1:
215 outParams.sampleFormat = paUInt8;
216 break;
217 case 2:
218 outParams.sampleFormat = paInt16;
219 break;
220 case 4:
221 outParams.sampleFormat = paFloat32;
222 break;
223 default:
224 AL_PRINT("Unknown format: 0x%x\n", device->Format);
225 device->ExtraData = NULL;
226 free(data);
227 pa_unload();
228 return ALC_FALSE;
230 outParams.channelCount = aluChannelsFromFormat(device->Format);
232 SetDefaultChannelOrder(device);
234 err = pPa_OpenStream(&data->stream, NULL, &outParams, device->Frequency,
235 device->UpdateSize, paNoFlag, pa_callback, device);
236 if(err != paNoError)
238 AL_PRINT("Pa_OpenStream() returned an error: %s\n", pPa_GetErrorText(err));
239 device->ExtraData = NULL;
240 free(data);
241 pa_unload();
242 return ALC_FALSE;
244 streamInfo = pPa_GetStreamInfo(data->stream);
246 device->szDeviceName = strdup(deviceName);
247 device->Frequency = streamInfo->sampleRate;
249 return ALC_TRUE;
252 static void pa_close_playback(ALCdevice *device)
254 pa_data *data = (pa_data*)device->ExtraData;
255 PaError err;
257 err = pPa_CloseStream(data->stream);
258 if(err != paNoError)
259 AL_PRINT("Error closing stream: %s\n", pPa_GetErrorText(err));
261 free(data);
262 device->ExtraData = NULL;
264 pa_unload();
267 static ALCboolean pa_reset_playback(ALCdevice *device)
269 pa_data *data = (pa_data*)device->ExtraData;
270 const PaStreamInfo *streamInfo;
271 PaError err;
273 streamInfo = pPa_GetStreamInfo(data->stream);
274 device->Frequency = streamInfo->sampleRate;
275 device->UpdateSize = data->update_size;
277 err = pPa_StartStream(data->stream);
278 if(err != paNoError)
280 AL_PRINT("Pa_StartStream() returned an error: %s\n", pPa_GetErrorText(err));
281 return ALC_FALSE;
284 return ALC_TRUE;
287 static void pa_stop_playback(ALCdevice *device)
289 pa_data *data = (pa_data*)device->ExtraData;
290 PaError err;
292 err = pPa_StopStream(data->stream);
293 if(err != paNoError)
294 AL_PRINT("Error stopping stream: %s\n", pPa_GetErrorText(err));
298 static ALCboolean pa_open_capture(ALCdevice *device, const ALCchar *deviceName)
300 PaStreamParameters inParams;
301 ALuint frame_size;
302 pa_data *data;
303 PaError err;
305 if(!deviceName)
306 deviceName = pa_capture;
307 else if(strcmp(deviceName, pa_capture) != 0)
308 return ALC_FALSE;
310 if(!pa_load())
311 return ALC_FALSE;
313 data = (pa_data*)calloc(1, sizeof(pa_data));
314 if(data == NULL)
316 alcSetError(device, ALC_OUT_OF_MEMORY);
317 return ALC_FALSE;
320 frame_size = aluChannelsFromFormat(device->Format) *
321 aluBytesFromFormat(device->Format);
322 data->ring = CreateRingBuffer(frame_size, device->UpdateSize*device->NumUpdates);
323 if(data->ring == NULL)
325 alcSetError(device, ALC_OUT_OF_MEMORY);
326 goto error;
329 inParams.device = GetConfigValueInt("port", "capture", -1);
330 if(inParams.device < 0)
331 inParams.device = pPa_GetDefaultOutputDevice();
332 inParams.suggestedLatency = 0.0f;
333 inParams.hostApiSpecificStreamInfo = NULL;
335 switch(aluBytesFromFormat(device->Format))
337 case 1:
338 inParams.sampleFormat = paUInt8;
339 break;
340 case 2:
341 inParams.sampleFormat = paInt16;
342 break;
343 case 4:
344 inParams.sampleFormat = paFloat32;
345 break;
346 default:
347 AL_PRINT("Unknown format: 0x%x\n", device->Format);
348 goto error;
350 inParams.channelCount = aluChannelsFromFormat(device->Format);
352 err = pPa_OpenStream(&data->stream, &inParams, NULL, device->Frequency,
353 paFramesPerBufferUnspecified, paNoFlag, pa_capture_cb, device);
354 if(err != paNoError)
356 AL_PRINT("Pa_OpenStream() returned an error: %s\n", pPa_GetErrorText(err));
357 goto error;
360 device->szDeviceName = strdup(deviceName);
362 device->ExtraData = data;
363 return ALC_TRUE;
365 error:
366 DestroyRingBuffer(data->ring);
367 free(data);
368 pa_unload();
369 return ALC_FALSE;
372 static void pa_close_capture(ALCdevice *device)
374 pa_data *data = (pa_data*)device->ExtraData;
375 PaError err;
377 err = pPa_CloseStream(data->stream);
378 if(err != paNoError)
379 AL_PRINT("Error closing stream: %s\n", pPa_GetErrorText(err));
381 free(data);
382 device->ExtraData = NULL;
384 pa_unload();
387 static void pa_start_capture(ALCdevice *device)
389 pa_data *data = device->ExtraData;
390 PaError err;
392 err = pPa_StartStream(data->stream);
393 if(err != paNoError)
394 AL_PRINT("Error starting stream: %s\n", pPa_GetErrorText(err));
397 static void pa_stop_capture(ALCdevice *device)
399 pa_data *data = (pa_data*)device->ExtraData;
400 PaError err;
402 err = pPa_StopStream(data->stream);
403 if(err != paNoError)
404 AL_PRINT("Error stopping stream: %s\n", pPa_GetErrorText(err));
407 static void pa_capture_samples(ALCdevice *device, ALCvoid *buffer, ALCuint samples)
409 pa_data *data = device->ExtraData;
410 if(samples <= (ALCuint)RingBufferSize(data->ring))
411 ReadRingBuffer(data->ring, buffer, samples);
412 else
413 alcSetError(device, ALC_INVALID_VALUE);
416 static ALCuint pa_available_samples(ALCdevice *device)
418 pa_data *data = device->ExtraData;
419 return RingBufferSize(data->ring);
423 static const BackendFuncs pa_funcs = {
424 pa_open_playback,
425 pa_close_playback,
426 pa_reset_playback,
427 pa_stop_playback,
428 pa_open_capture,
429 pa_close_capture,
430 pa_start_capture,
431 pa_stop_capture,
432 pa_capture_samples,
433 pa_available_samples
436 void alc_pa_init(BackendFuncs *func_list)
438 *func_list = pa_funcs;
441 void alc_pa_deinit(void)
445 void alc_pa_probe(int type)
447 if(!pa_load()) return;
449 if(type == DEVICE_PROBE)
450 AppendDeviceList(pa_device);
451 else if(type == ALL_DEVICE_PROBE)
452 AppendAllDeviceList(pa_device);
453 else if(type == CAPTURE_DEVICE_PROBE)
454 AppendCaptureDeviceList(pa_capture);
456 pa_unload();