Simplify removing the context handle from the device's context array
[openal-soft.git] / Alc / portaudio.c
blobde58202eb4a597e7e1687cd9a9b509abd980478a
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 const char *str;
57 PaError err;
59 if(load_count == 0)
61 #ifdef HAVE_DLFCN_H
62 #if defined(__APPLE__) && defined(__MACH__)
63 # define PALIB "libportaudio.2.dylib"
64 #else
65 # define PALIB "libportaudio.so.2"
66 #endif
67 pa_handle = dlopen(PALIB, RTLD_NOW);
68 if(!pa_handle)
69 return NULL;
70 dlerror();
72 #define LOAD_FUNC(f) do { \
73 p##f = (typeof(f)*)dlsym(pa_handle, #f); \
74 if((str=dlerror()) != NULL) \
75 { \
76 dlclose(pa_handle); \
77 pa_handle = NULL; \
78 AL_PRINT("Could not load %s from "PALIB": %s\n", #f, str); \
79 return NULL; \
80 } \
81 } while(0)
82 #else
83 str = NULL;
84 pa_handle = (void*)0xDEADBEEF;
85 #define LOAD_FUNC(f) p##f = f
86 #endif
88 LOAD_FUNC(Pa_Initialize);
89 LOAD_FUNC(Pa_Terminate);
90 LOAD_FUNC(Pa_GetErrorText);
91 LOAD_FUNC(Pa_StartStream);
92 LOAD_FUNC(Pa_StopStream);
93 LOAD_FUNC(Pa_OpenStream);
94 LOAD_FUNC(Pa_CloseStream);
95 LOAD_FUNC(Pa_GetDefaultOutputDevice);
96 LOAD_FUNC(Pa_GetStreamInfo);
98 #undef LOAD_FUNC
100 if((err=pPa_Initialize()) != paNoError)
102 AL_PRINT("Pa_Initialize() returned an error: %s\n", pPa_GetErrorText(err));
103 #ifdef HAVE_DLFCN_H
104 dlclose(pa_handle);
105 #endif
106 pa_handle = NULL;
107 return NULL;
110 ++load_count;
112 return pa_handle;
115 void pa_unload(void)
117 if(load_count == 0 || --load_count > 0)
118 return;
120 pPa_Terminate();
121 #ifdef HAVE_DLFCN_H
122 dlclose(pa_handle);
123 #endif
124 pa_handle = NULL;
128 typedef struct {
129 PaStream *stream;
130 ALuint update_size;
132 RingBuffer *ring;
133 } pa_data;
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;
142 (void)inputBuffer;
143 (void)timeInfo;
144 (void)statusFlags;
146 aluMixData(device, outputBuffer, framesPerBuffer);
147 return 0;
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;
157 (void)outputBuffer;
158 (void)timeInfo;
159 (void)statusFlags;
161 WriteRingBuffer(data->ring, inputBuffer, framesPerBuffer);
162 return 0;
166 static ALCboolean pa_open_playback(ALCdevice *device, const ALCchar *deviceName)
168 const PaStreamInfo *streamInfo;
169 PaStreamParameters outParams;
170 pa_data *data;
171 PaError err;
173 if(!deviceName)
174 deviceName = pa_device;
175 else if(strcmp(deviceName, pa_device) != 0)
176 return ALC_FALSE;
178 if(!pa_load())
179 return ALC_FALSE;
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))
195 case 1:
196 outParams.sampleFormat = paUInt8;
197 break;
198 case 2:
199 outParams.sampleFormat = paInt16;
200 break;
201 case 4:
202 outParams.sampleFormat = paFloat32;
203 break;
204 default:
205 AL_PRINT("Unknown format: 0x%x\n", device->Format);
206 device->ExtraData = NULL;
207 free(data);
208 pa_unload();
209 return ALC_FALSE;
211 outParams.channelCount = aluChannelsFromFormat(device->Format);
213 SetDefaultChannelOrder(device);
215 err = pPa_OpenStream(&data->stream, NULL, &outParams, device->Frequency,
216 device->UpdateSize, paNoFlag, pa_callback, device);
217 if(err != paNoError)
219 AL_PRINT("Pa_OpenStream() returned an error: %s\n", pPa_GetErrorText(err));
220 device->ExtraData = NULL;
221 free(data);
222 pa_unload();
223 return ALC_FALSE;
225 streamInfo = pPa_GetStreamInfo(data->stream);
227 device->szDeviceName = strdup(deviceName);
228 device->Frequency = streamInfo->sampleRate;
230 return ALC_TRUE;
233 static void pa_close_playback(ALCdevice *device)
235 pa_data *data = (pa_data*)device->ExtraData;
236 PaError err;
238 err = pPa_CloseStream(data->stream);
239 if(err != paNoError)
240 AL_PRINT("Error closing stream: %s\n", pPa_GetErrorText(err));
242 free(data);
243 device->ExtraData = NULL;
245 pa_unload();
248 static ALCboolean pa_reset_playback(ALCdevice *device)
250 pa_data *data = (pa_data*)device->ExtraData;
251 const PaStreamInfo *streamInfo;
252 PaError err;
254 streamInfo = pPa_GetStreamInfo(data->stream);
255 device->Frequency = streamInfo->sampleRate;
256 device->UpdateSize = data->update_size;
258 err = pPa_StartStream(data->stream);
259 if(err != paNoError)
261 AL_PRINT("Pa_StartStream() returned an error: %s\n", pPa_GetErrorText(err));
262 return ALC_FALSE;
265 return ALC_TRUE;
268 static void pa_stop_playback(ALCdevice *device)
270 pa_data *data = (pa_data*)device->ExtraData;
271 PaError err;
273 err = pPa_StopStream(data->stream);
274 if(err != paNoError)
275 AL_PRINT("Error stopping stream: %s\n", pPa_GetErrorText(err));
279 static ALCboolean pa_open_capture(ALCdevice *device, const ALCchar *deviceName)
281 PaStreamParameters inParams;
282 ALuint frame_size;
283 pa_data *data;
284 PaError err;
286 if(!deviceName)
287 deviceName = pa_capture;
288 else if(strcmp(deviceName, pa_capture) != 0)
289 return ALC_FALSE;
291 if(!pa_load())
292 return ALC_FALSE;
294 data = (pa_data*)calloc(1, sizeof(pa_data));
295 if(data == NULL)
297 alcSetError(device, ALC_OUT_OF_MEMORY);
298 return ALC_FALSE;
301 frame_size = aluChannelsFromFormat(device->Format) *
302 aluBytesFromFormat(device->Format);
303 data->ring = CreateRingBuffer(frame_size, device->UpdateSize*device->NumUpdates);
304 if(data->ring == NULL)
306 alcSetError(device, ALC_OUT_OF_MEMORY);
307 goto error;
310 inParams.device = GetConfigValueInt("port", "capture", -1);
311 if(inParams.device < 0)
312 inParams.device = pPa_GetDefaultOutputDevice();
313 inParams.suggestedLatency = 0.0f;
314 inParams.hostApiSpecificStreamInfo = NULL;
316 switch(aluBytesFromFormat(device->Format))
318 case 1:
319 inParams.sampleFormat = paUInt8;
320 break;
321 case 2:
322 inParams.sampleFormat = paInt16;
323 break;
324 case 4:
325 inParams.sampleFormat = paFloat32;
326 break;
327 default:
328 AL_PRINT("Unknown format: 0x%x\n", device->Format);
329 goto error;
331 inParams.channelCount = aluChannelsFromFormat(device->Format);
333 err = pPa_OpenStream(&data->stream, &inParams, NULL, device->Frequency,
334 paFramesPerBufferUnspecified, paNoFlag, pa_capture_cb, device);
335 if(err != paNoError)
337 AL_PRINT("Pa_OpenStream() returned an error: %s\n", pPa_GetErrorText(err));
338 goto error;
341 device->szDeviceName = strdup(deviceName);
343 device->ExtraData = data;
344 return ALC_TRUE;
346 error:
347 DestroyRingBuffer(data->ring);
348 free(data);
349 pa_unload();
350 return ALC_FALSE;
353 static void pa_close_capture(ALCdevice *device)
355 pa_data *data = (pa_data*)device->ExtraData;
356 PaError err;
358 err = pPa_CloseStream(data->stream);
359 if(err != paNoError)
360 AL_PRINT("Error closing stream: %s\n", pPa_GetErrorText(err));
362 free(data);
363 device->ExtraData = NULL;
365 pa_unload();
368 static void pa_start_capture(ALCdevice *device)
370 pa_data *data = device->ExtraData;
371 PaError err;
373 err = pPa_StartStream(data->stream);
374 if(err != paNoError)
375 AL_PRINT("Error starting stream: %s\n", pPa_GetErrorText(err));
378 static void pa_stop_capture(ALCdevice *device)
380 pa_data *data = (pa_data*)device->ExtraData;
381 PaError err;
383 err = pPa_StopStream(data->stream);
384 if(err != paNoError)
385 AL_PRINT("Error stopping stream: %s\n", pPa_GetErrorText(err));
388 static void pa_capture_samples(ALCdevice *device, ALCvoid *buffer, ALCuint samples)
390 pa_data *data = device->ExtraData;
391 if(samples <= (ALCuint)RingBufferSize(data->ring))
392 ReadRingBuffer(data->ring, buffer, samples);
393 else
394 alcSetError(device, ALC_INVALID_VALUE);
397 static ALCuint pa_available_samples(ALCdevice *device)
399 pa_data *data = device->ExtraData;
400 return RingBufferSize(data->ring);
404 static const BackendFuncs pa_funcs = {
405 pa_open_playback,
406 pa_close_playback,
407 pa_reset_playback,
408 pa_stop_playback,
409 pa_open_capture,
410 pa_close_capture,
411 pa_start_capture,
412 pa_stop_capture,
413 pa_capture_samples,
414 pa_available_samples
417 void alc_pa_init(BackendFuncs *func_list)
419 *func_list = pa_funcs;
422 void alc_pa_deinit(void)
426 void alc_pa_probe(int type)
428 if(!pa_load()) return;
430 if(type == DEVICE_PROBE)
431 AppendDeviceList(pa_device);
432 else if(type == ALL_DEVICE_PROBE)
433 AppendAllDeviceList(pa_device);
434 else if(type == CAPTURE_DEVICE_PROBE)
435 AppendCaptureDeviceList(pa_capture);
437 pa_unload();