Better protect against negative attenuation
[openal-soft.git] / Alc / portaudio.c
blob062c403ac94a5e2e4bc1431afb8e5344f5c87a36
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 Default";
52 void *pa_load(void)
54 if(!pa_handle)
56 PaError err;
58 #ifdef _WIN32
59 pa_handle = LoadLibrary("portaudio.dll");
60 #define LOAD_FUNC(x) do { \
61 p##x = (typeof(p##x))GetProcAddress(pa_handle, #x); \
62 if(!(p##x)) { \
63 AL_PRINT("Could not load %s from portaudio.dll\n", #x); \
64 FreeLibrary(pa_handle); \
65 pa_handle = NULL; \
66 return NULL; \
67 } \
68 } while(0)
70 #elif defined(HAVE_DLFCN_H)
72 const char *str;
73 #if defined(__APPLE__) && defined(__MACH__)
74 # define PALIB "libportaudio.2.dylib"
75 #else
76 # define PALIB "libportaudio.so.2"
77 #endif
78 pa_handle = dlopen(PALIB, RTLD_NOW);
79 dlerror();
81 #define LOAD_FUNC(f) do { \
82 p##f = (typeof(f)*)dlsym(pa_handle, #f); \
83 if((str=dlerror()) != NULL) \
84 { \
85 dlclose(pa_handle); \
86 pa_handle = NULL; \
87 AL_PRINT("Could not load %s from "PALIB": %s\n", #f, str); \
88 return NULL; \
89 } \
90 } while(0)
92 #else
93 pa_handle = (void*)0xDEADBEEF;
94 #define LOAD_FUNC(f) p##f = f
95 #endif
97 if(!pa_handle)
98 return NULL;
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);
110 #undef LOAD_FUNC
112 if((err=pPa_Initialize()) != paNoError)
114 AL_PRINT("Pa_Initialize() returned an error: %s\n", pPa_GetErrorText(err));
115 #ifdef _WIN32
116 FreeLibrary(pa_handle);
117 #elif defined(HAVE_DLFCN_H)
118 dlclose(pa_handle);
119 #endif
120 pa_handle = NULL;
121 return NULL;
124 return pa_handle;
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 return ALC_FALSE;
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);
216 if(err != paNoError)
218 AL_PRINT("Pa_OpenStream() returned an error: %s\n", pPa_GetErrorText(err));
219 device->ExtraData = NULL;
220 free(data);
221 return ALC_FALSE;
223 streamInfo = pPa_GetStreamInfo(data->stream);
225 device->szDeviceName = strdup(deviceName);
226 device->Frequency = streamInfo->sampleRate;
228 return ALC_TRUE;
231 static void pa_close_playback(ALCdevice *device)
233 pa_data *data = (pa_data*)device->ExtraData;
234 PaError err;
236 err = pPa_CloseStream(data->stream);
237 if(err != paNoError)
238 AL_PRINT("Error closing stream: %s\n", pPa_GetErrorText(err));
240 free(data);
241 device->ExtraData = NULL;
244 static ALCboolean pa_reset_playback(ALCdevice *device)
246 pa_data *data = (pa_data*)device->ExtraData;
247 const PaStreamInfo *streamInfo;
248 PaError err;
250 streamInfo = pPa_GetStreamInfo(data->stream);
251 device->Frequency = streamInfo->sampleRate;
252 device->UpdateSize = data->update_size;
253 device->TimeRes = (ALuint64)device->UpdateSize * 1000000000 /
254 device->Frequency;
256 err = pPa_StartStream(data->stream);
257 if(err != paNoError)
259 AL_PRINT("Pa_StartStream() returned an error: %s\n", pPa_GetErrorText(err));
260 return ALC_FALSE;
263 return ALC_TRUE;
266 static void pa_stop_playback(ALCdevice *device)
268 pa_data *data = (pa_data*)device->ExtraData;
269 PaError err;
271 err = pPa_StopStream(data->stream);
272 if(err != paNoError)
273 AL_PRINT("Error stopping stream: %s\n", pPa_GetErrorText(err));
277 static ALCboolean pa_open_capture(ALCdevice *device, const ALCchar *deviceName)
279 PaStreamParameters inParams;
280 ALuint frame_size;
281 pa_data *data;
282 PaError err;
284 if(!deviceName)
285 deviceName = pa_device;
286 else if(strcmp(deviceName, pa_device) != 0)
287 return ALC_FALSE;
289 if(!pa_load())
290 return ALC_FALSE;
292 data = (pa_data*)calloc(1, sizeof(pa_data));
293 if(data == NULL)
295 alcSetError(device, ALC_OUT_OF_MEMORY);
296 return ALC_FALSE;
299 frame_size = aluFrameSizeFromFormat(device->Format);
300 data->ring = CreateRingBuffer(frame_size, device->UpdateSize*device->NumUpdates);
301 if(data->ring == NULL)
303 alcSetError(device, ALC_OUT_OF_MEMORY);
304 goto error;
307 inParams.device = GetConfigValueInt("port", "capture", -1);
308 if(inParams.device < 0)
309 inParams.device = pPa_GetDefaultOutputDevice();
310 inParams.suggestedLatency = 0.0f;
311 inParams.hostApiSpecificStreamInfo = NULL;
313 switch(aluBytesFromFormat(device->Format))
315 case 1:
316 inParams.sampleFormat = paUInt8;
317 break;
318 case 2:
319 inParams.sampleFormat = paInt16;
320 break;
321 case 4:
322 inParams.sampleFormat = paFloat32;
323 break;
324 default:
325 AL_PRINT("Unknown format: 0x%x\n", device->Format);
326 goto error;
328 inParams.channelCount = aluChannelsFromFormat(device->Format);
330 err = pPa_OpenStream(&data->stream, &inParams, NULL, device->Frequency,
331 paFramesPerBufferUnspecified, paNoFlag, pa_capture_cb, device);
332 if(err != paNoError)
334 AL_PRINT("Pa_OpenStream() returned an error: %s\n", pPa_GetErrorText(err));
335 goto error;
338 device->szDeviceName = strdup(deviceName);
340 device->ExtraData = data;
341 return ALC_TRUE;
343 error:
344 DestroyRingBuffer(data->ring);
345 free(data);
346 return ALC_FALSE;
349 static void pa_close_capture(ALCdevice *device)
351 pa_data *data = (pa_data*)device->ExtraData;
352 PaError err;
354 err = pPa_CloseStream(data->stream);
355 if(err != paNoError)
356 AL_PRINT("Error closing stream: %s\n", pPa_GetErrorText(err));
358 free(data);
359 device->ExtraData = NULL;
362 static void pa_start_capture(ALCdevice *device)
364 pa_data *data = device->ExtraData;
365 PaError err;
367 err = pPa_StartStream(data->stream);
368 if(err != paNoError)
369 AL_PRINT("Error starting stream: %s\n", pPa_GetErrorText(err));
372 static void pa_stop_capture(ALCdevice *device)
374 pa_data *data = (pa_data*)device->ExtraData;
375 PaError err;
377 err = pPa_StopStream(data->stream);
378 if(err != paNoError)
379 AL_PRINT("Error stopping stream: %s\n", pPa_GetErrorText(err));
382 static void pa_capture_samples(ALCdevice *device, ALCvoid *buffer, ALCuint samples)
384 pa_data *data = device->ExtraData;
385 if(samples <= (ALCuint)RingBufferSize(data->ring))
386 ReadRingBuffer(data->ring, buffer, samples);
387 else
388 alcSetError(device, ALC_INVALID_VALUE);
391 static ALCuint pa_available_samples(ALCdevice *device)
393 pa_data *data = device->ExtraData;
394 return RingBufferSize(data->ring);
397 static ALuint64 pa_get_time(ALCdevice *Device)
399 return Device->SamplesPlayed * 1000000000 / Device->Frequency;
403 static const BackendFuncs pa_funcs = {
404 pa_open_playback,
405 pa_close_playback,
406 pa_reset_playback,
407 pa_stop_playback,
408 pa_open_capture,
409 pa_close_capture,
410 pa_start_capture,
411 pa_stop_capture,
412 pa_capture_samples,
413 pa_available_samples,
414 pa_get_time
417 void alc_pa_init(BackendFuncs *func_list)
419 *func_list = pa_funcs;
422 void alc_pa_deinit(void)
424 if(pa_handle)
426 pPa_Terminate();
427 #ifdef _WIN32
428 FreeLibrary(pa_handle);
429 #elif defined(HAVE_DLFCN_H)
430 dlclose(pa_handle);
431 #endif
432 pa_handle = NULL;
436 void alc_pa_probe(int type)
438 if(!pa_load()) return;
440 if(type == DEVICE_PROBE)
441 AppendDeviceList(pa_device);
442 else if(type == ALL_DEVICE_PROBE)
443 AppendAllDeviceList(pa_device);
444 else if(type == CAPTURE_DEVICE_PROBE)
445 AppendCaptureDeviceList(pa_device);