Always use "OpenAL Soft" for the short device enumeration list
[openal-soft.git] / Alc / backends / portaudio.c
blob2ac668b815e531c735cc459bfc93b799a059babf
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 static void *pa_handle;
37 #ifdef HAVE_DYNLOAD
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 if(!pa_handle)
65 PaError err;
67 #ifdef HAVE_DYNLOAD
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
101 #else
102 pa_handle = (void*)0xDEADBEEF;
103 #endif
105 if((err=Pa_Initialize()) != paNoError)
107 ERR("Pa_Initialize() returned an error: %s\n", Pa_GetErrorText(err));
108 CloseLib(pa_handle);
109 pa_handle = NULL;
110 return ALC_FALSE;
113 return ALC_TRUE;
117 typedef struct {
118 PaStream *stream;
119 ALuint update_size;
121 RingBuffer *ring;
122 } pa_data;
125 static int pa_callback(const void *inputBuffer, void *outputBuffer,
126 unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *timeInfo,
127 const PaStreamCallbackFlags statusFlags, void *userData)
129 ALCdevice *device = (ALCdevice*)userData;
131 (void)inputBuffer;
132 (void)timeInfo;
133 (void)statusFlags;
135 aluMixData(device, outputBuffer, framesPerBuffer);
136 return 0;
139 static int pa_capture_cb(const void *inputBuffer, void *outputBuffer,
140 unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *timeInfo,
141 const PaStreamCallbackFlags statusFlags, void *userData)
143 ALCdevice *device = (ALCdevice*)userData;
144 pa_data *data = (pa_data*)device->ExtraData;
146 (void)outputBuffer;
147 (void)timeInfo;
148 (void)statusFlags;
150 WriteRingBuffer(data->ring, inputBuffer, framesPerBuffer);
151 return 0;
155 static ALCenum pa_open_playback(ALCdevice *device, const ALCchar *deviceName)
157 PaStreamParameters outParams;
158 pa_data *data;
159 PaError err;
161 if(!deviceName)
162 deviceName = pa_device;
163 else if(strcmp(deviceName, pa_device) != 0)
164 return ALC_INVALID_VALUE;
166 data = (pa_data*)calloc(1, sizeof(pa_data));
167 data->update_size = device->UpdateSize;
169 device->ExtraData = data;
171 outParams.device = -1;
172 if(!ConfigValueInt("port", "device", &outParams.device) || outParams.device < 0)
173 outParams.device = Pa_GetDefaultOutputDevice();
174 outParams.suggestedLatency = (device->UpdateSize*device->NumUpdates) /
175 (float)device->Frequency;
176 outParams.hostApiSpecificStreamInfo = NULL;
178 outParams.channelCount = ((device->FmtChans == DevFmtMono) ? 1 : 2);
180 retry_open:
181 switch(device->FmtType)
183 case DevFmtByte:
184 outParams.sampleFormat = paInt8;
185 break;
186 case DevFmtUByte:
187 outParams.sampleFormat = paUInt8;
188 break;
189 case DevFmtUShort:
190 device->FmtType = DevFmtShort;
191 /* fall-through */
192 case DevFmtShort:
193 outParams.sampleFormat = paInt16;
194 break;
195 case DevFmtUInt:
196 device->FmtType = DevFmtInt;
197 /* fall-through */
198 case DevFmtInt:
199 outParams.sampleFormat = paInt32;
200 break;
201 case DevFmtFloat:
202 outParams.sampleFormat = paFloat32;
203 break;
206 err = Pa_OpenStream(&data->stream, NULL, &outParams, device->Frequency,
207 device->UpdateSize, paNoFlag, pa_callback, device);
208 if(err != paNoError)
210 if(device->FmtType == DevFmtFloat)
212 device->FmtType = DevFmtShort;
213 goto retry_open;
215 ERR("Pa_OpenStream() returned an error: %s\n", Pa_GetErrorText(err));
216 device->ExtraData = NULL;
217 free(data);
218 return ALC_INVALID_VALUE;
221 device->szDeviceName = strdup(deviceName);
223 if((ALuint)outParams.channelCount != ChannelsFromDevFmt(device->FmtChans))
225 if(outParams.channelCount != 1 && outParams.channelCount != 2)
227 ERR("Unhandled channel count: %u\n", outParams.channelCount);
228 Pa_CloseStream(data->stream);
229 device->ExtraData = NULL;
230 free(data);
231 return ALC_INVALID_VALUE;
233 if((device->Flags&DEVICE_CHANNELS_REQUEST))
234 ERR("Failed to set %s, got %u channels instead\n", DevFmtChannelsString(device->FmtChans), outParams.channelCount);
235 device->Flags &= ~DEVICE_CHANNELS_REQUEST;
236 device->FmtChans = ((outParams.channelCount==1) ? DevFmtMono : DevFmtStereo);
238 SetDefaultChannelOrder(device);
240 return ALC_NO_ERROR;
243 static void pa_close_playback(ALCdevice *device)
245 pa_data *data = (pa_data*)device->ExtraData;
246 PaError err;
248 err = Pa_CloseStream(data->stream);
249 if(err != paNoError)
250 ERR("Error closing stream: %s\n", Pa_GetErrorText(err));
252 free(data);
253 device->ExtraData = NULL;
256 static ALCboolean pa_reset_playback(ALCdevice *device)
258 pa_data *data = (pa_data*)device->ExtraData;
259 const PaStreamInfo *streamInfo;
260 PaError err;
262 streamInfo = Pa_GetStreamInfo(data->stream);
263 device->Frequency = streamInfo->sampleRate;
264 device->UpdateSize = data->update_size;
266 err = Pa_StartStream(data->stream);
267 if(err != paNoError)
269 ERR("Pa_StartStream() returned an error: %s\n", Pa_GetErrorText(err));
270 return ALC_FALSE;
273 return ALC_TRUE;
276 static void pa_stop_playback(ALCdevice *device)
278 pa_data *data = (pa_data*)device->ExtraData;
279 PaError err;
281 err = Pa_StopStream(data->stream);
282 if(err != paNoError)
283 ERR("Error stopping stream: %s\n", Pa_GetErrorText(err));
287 static ALCenum pa_open_capture(ALCdevice *device, const ALCchar *deviceName)
289 PaStreamParameters inParams;
290 ALuint frame_size;
291 pa_data *data;
292 PaError err;
294 if(!deviceName)
295 deviceName = pa_device;
296 else if(strcmp(deviceName, pa_device) != 0)
297 return ALC_INVALID_VALUE;
299 data = (pa_data*)calloc(1, sizeof(pa_data));
300 if(data == NULL)
301 return ALC_OUT_OF_MEMORY;
303 frame_size = FrameSizeFromDevFmt(device->FmtChans, device->FmtType);
304 data->ring = CreateRingBuffer(frame_size, device->UpdateSize*device->NumUpdates);
305 if(data->ring == NULL)
306 goto error;
308 inParams.device = -1;
309 if(!ConfigValueInt("port", "capture", &inParams.device) || inParams.device < 0)
310 inParams.device = Pa_GetDefaultOutputDevice();
311 inParams.suggestedLatency = 0.0f;
312 inParams.hostApiSpecificStreamInfo = NULL;
314 switch(device->FmtType)
316 case DevFmtByte:
317 inParams.sampleFormat = paInt8;
318 break;
319 case DevFmtUByte:
320 inParams.sampleFormat = paUInt8;
321 break;
322 case DevFmtShort:
323 inParams.sampleFormat = paInt16;
324 break;
325 case DevFmtInt:
326 inParams.sampleFormat = paInt32;
327 break;
328 case DevFmtFloat:
329 inParams.sampleFormat = paFloat32;
330 break;
331 case DevFmtUInt:
332 case DevFmtUShort:
333 ERR("%s samples not supported\n", DevFmtTypeString(device->FmtType));
334 goto error;
336 inParams.channelCount = ChannelsFromDevFmt(device->FmtChans);
338 err = Pa_OpenStream(&data->stream, &inParams, NULL, device->Frequency,
339 paFramesPerBufferUnspecified, paNoFlag, pa_capture_cb, device);
340 if(err != paNoError)
342 ERR("Pa_OpenStream() returned an error: %s\n", Pa_GetErrorText(err));
343 goto error;
346 device->szDeviceName = strdup(deviceName);
348 device->ExtraData = data;
349 return ALC_NO_ERROR;
351 error:
352 DestroyRingBuffer(data->ring);
353 free(data);
354 return ALC_INVALID_VALUE;
357 static void pa_close_capture(ALCdevice *device)
359 pa_data *data = (pa_data*)device->ExtraData;
360 PaError err;
362 err = Pa_CloseStream(data->stream);
363 if(err != paNoError)
364 ERR("Error closing stream: %s\n", Pa_GetErrorText(err));
366 free(data);
367 device->ExtraData = NULL;
370 static void pa_start_capture(ALCdevice *device)
372 pa_data *data = device->ExtraData;
373 PaError err;
375 err = Pa_StartStream(data->stream);
376 if(err != paNoError)
377 ERR("Error starting stream: %s\n", Pa_GetErrorText(err));
380 static void pa_stop_capture(ALCdevice *device)
382 pa_data *data = (pa_data*)device->ExtraData;
383 PaError err;
385 err = Pa_StopStream(data->stream);
386 if(err != paNoError)
387 ERR("Error stopping stream: %s\n", Pa_GetErrorText(err));
390 static ALCenum pa_capture_samples(ALCdevice *device, ALCvoid *buffer, ALCuint samples)
392 pa_data *data = device->ExtraData;
393 ReadRingBuffer(data->ring, buffer, samples);
394 return ALC_NO_ERROR;
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 ALCboolean alc_pa_init(BackendFuncs *func_list)
419 if(!pa_load())
420 return ALC_FALSE;
421 *func_list = pa_funcs;
422 return ALC_TRUE;
425 void alc_pa_deinit(void)
427 if(pa_handle)
429 Pa_Terminate();
430 #ifdef HAVE_DYNLOAD
431 CloseLib(pa_handle);
432 #endif
433 pa_handle = NULL;
437 void alc_pa_probe(enum DevProbe type)
439 switch(type)
441 case ALL_DEVICE_PROBE:
442 AppendAllDeviceList(pa_device);
443 break;
444 case CAPTURE_DEVICE_PROBE:
445 AppendCaptureDeviceList(pa_device);
446 break;