Pass the uncompressed sample count to LoadData and ConvertData for IMA4
[openal-soft/android.git] / Alc / backends / portaudio.c
blobbb4f9d94cc49878a3483345df917bec8990d8636
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 DevFmtFloat:
196 outParams.sampleFormat = paFloat32;
197 break;
200 err = Pa_OpenStream(&data->stream, NULL, &outParams, device->Frequency,
201 device->UpdateSize, paNoFlag, pa_callback, device);
202 if(err != paNoError)
204 if(device->FmtType == DevFmtFloat)
206 device->FmtType = DevFmtShort;
207 goto retry_open;
209 ERR("Pa_OpenStream() returned an error: %s\n", Pa_GetErrorText(err));
210 device->ExtraData = NULL;
211 free(data);
212 return ALC_INVALID_VALUE;
215 device->szDeviceName = strdup(deviceName);
217 if((ALuint)outParams.channelCount != ChannelsFromDevFmt(device->FmtChans))
219 if(outParams.channelCount != 1 && outParams.channelCount != 2)
221 ERR("Unhandled channel count: %u\n", outParams.channelCount);
222 Pa_CloseStream(data->stream);
223 device->ExtraData = NULL;
224 free(data);
225 return ALC_INVALID_VALUE;
227 if((device->Flags&DEVICE_CHANNELS_REQUEST))
228 ERR("Failed to set %s, got %u channels instead\n", DevFmtChannelsString(device->FmtChans), outParams.channelCount);
229 device->Flags &= ~DEVICE_CHANNELS_REQUEST;
230 device->FmtChans = ((outParams.channelCount==1) ? DevFmtMono : DevFmtStereo);
232 SetDefaultChannelOrder(device);
234 return ALC_NO_ERROR;
237 static void pa_close_playback(ALCdevice *device)
239 pa_data *data = (pa_data*)device->ExtraData;
240 PaError err;
242 err = Pa_CloseStream(data->stream);
243 if(err != paNoError)
244 ERR("Error closing stream: %s\n", Pa_GetErrorText(err));
246 free(data);
247 device->ExtraData = NULL;
250 static ALCboolean pa_reset_playback(ALCdevice *device)
252 pa_data *data = (pa_data*)device->ExtraData;
253 const PaStreamInfo *streamInfo;
254 PaError err;
256 streamInfo = Pa_GetStreamInfo(data->stream);
257 device->Frequency = streamInfo->sampleRate;
258 device->UpdateSize = data->update_size;
260 err = Pa_StartStream(data->stream);
261 if(err != paNoError)
263 ERR("Pa_StartStream() returned an error: %s\n", Pa_GetErrorText(err));
264 return ALC_FALSE;
267 return ALC_TRUE;
270 static void pa_stop_playback(ALCdevice *device)
272 pa_data *data = (pa_data*)device->ExtraData;
273 PaError err;
275 err = Pa_StopStream(data->stream);
276 if(err != paNoError)
277 ERR("Error stopping stream: %s\n", Pa_GetErrorText(err));
281 static ALCenum pa_open_capture(ALCdevice *device, const ALCchar *deviceName)
283 PaStreamParameters inParams;
284 ALuint frame_size;
285 pa_data *data;
286 PaError err;
288 if(!deviceName)
289 deviceName = pa_device;
290 else if(strcmp(deviceName, pa_device) != 0)
291 return ALC_INVALID_VALUE;
293 data = (pa_data*)calloc(1, sizeof(pa_data));
294 if(data == NULL)
295 return ALC_OUT_OF_MEMORY;
297 frame_size = FrameSizeFromDevFmt(device->FmtChans, device->FmtType);
298 data->ring = CreateRingBuffer(frame_size, device->UpdateSize*device->NumUpdates);
299 if(data->ring == NULL)
300 goto error;
302 inParams.device = -1;
303 if(!ConfigValueInt("port", "capture", &inParams.device) || inParams.device < 0)
304 inParams.device = Pa_GetDefaultOutputDevice();
305 inParams.suggestedLatency = 0.0f;
306 inParams.hostApiSpecificStreamInfo = NULL;
308 switch(device->FmtType)
310 case DevFmtByte:
311 inParams.sampleFormat = paInt8;
312 break;
313 case DevFmtUByte:
314 inParams.sampleFormat = paUInt8;
315 break;
316 case DevFmtShort:
317 inParams.sampleFormat = paInt16;
318 break;
319 case DevFmtFloat:
320 inParams.sampleFormat = paFloat32;
321 break;
322 case DevFmtUShort:
323 ERR("Unsigned short samples not supported\n");
324 goto error;
326 inParams.channelCount = ChannelsFromDevFmt(device->FmtChans);
328 err = Pa_OpenStream(&data->stream, &inParams, NULL, device->Frequency,
329 paFramesPerBufferUnspecified, paNoFlag, pa_capture_cb, device);
330 if(err != paNoError)
332 ERR("Pa_OpenStream() returned an error: %s\n", Pa_GetErrorText(err));
333 goto error;
336 device->szDeviceName = strdup(deviceName);
338 device->ExtraData = data;
339 return ALC_NO_ERROR;
341 error:
342 DestroyRingBuffer(data->ring);
343 free(data);
344 return ALC_INVALID_VALUE;
347 static void pa_close_capture(ALCdevice *device)
349 pa_data *data = (pa_data*)device->ExtraData;
350 PaError err;
352 err = Pa_CloseStream(data->stream);
353 if(err != paNoError)
354 ERR("Error closing stream: %s\n", Pa_GetErrorText(err));
356 free(data);
357 device->ExtraData = NULL;
360 static void pa_start_capture(ALCdevice *device)
362 pa_data *data = device->ExtraData;
363 PaError err;
365 err = Pa_StartStream(data->stream);
366 if(err != paNoError)
367 ERR("Error starting stream: %s\n", Pa_GetErrorText(err));
370 static void pa_stop_capture(ALCdevice *device)
372 pa_data *data = (pa_data*)device->ExtraData;
373 PaError err;
375 err = Pa_StopStream(data->stream);
376 if(err != paNoError)
377 ERR("Error stopping stream: %s\n", Pa_GetErrorText(err));
380 static ALCenum pa_capture_samples(ALCdevice *device, ALCvoid *buffer, ALCuint samples)
382 pa_data *data = device->ExtraData;
383 ReadRingBuffer(data->ring, buffer, samples);
384 return ALC_NO_ERROR;
387 static ALCuint pa_available_samples(ALCdevice *device)
389 pa_data *data = device->ExtraData;
390 return RingBufferSize(data->ring);
394 static const BackendFuncs pa_funcs = {
395 pa_open_playback,
396 pa_close_playback,
397 pa_reset_playback,
398 pa_stop_playback,
399 pa_open_capture,
400 pa_close_capture,
401 pa_start_capture,
402 pa_stop_capture,
403 pa_capture_samples,
404 pa_available_samples
407 ALCboolean alc_pa_init(BackendFuncs *func_list)
409 if(!pa_load())
410 return ALC_FALSE;
411 *func_list = pa_funcs;
412 return ALC_TRUE;
415 void alc_pa_deinit(void)
417 if(pa_handle)
419 Pa_Terminate();
420 #ifdef HAVE_DYNLOAD
421 CloseLib(pa_handle);
422 #endif
423 pa_handle = NULL;
427 void alc_pa_probe(enum DevProbe type)
429 switch(type)
431 case DEVICE_PROBE:
432 AppendDeviceList(pa_device);
433 break;
434 case ALL_DEVICE_PROBE:
435 AppendAllDeviceList(pa_device);
436 break;
437 case CAPTURE_DEVICE_PROBE:
438 AppendCaptureDeviceList(pa_device);
439 break;