Tizen 2.1 base
[framework/multimedia/openal-soft.git] / Alc / portaudio.c
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.
8  *
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.
13  *
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
19  */
20
21 #include "config.h"
22
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
32
33 #include <portaudio.h>
34
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
47
48
49 static const ALCchar pa_device[] = "PortAudio Default";
50
51
52 void *pa_load(void)
53 {
54     if(!pa_handle)
55     {
56         PaError err;
57
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)
69
70 #elif defined(HAVE_DLFCN_H)
71
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();
80
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)
91
92 #else
93         pa_handle = (void*)0xDEADBEEF;
94 #define LOAD_FUNC(f) p##f = f
95 #endif
96
97         if(!pa_handle)
98             return NULL;
99
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);
109
110 #undef LOAD_FUNC
111
112         if((err=pPa_Initialize()) != paNoError)
113         {
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;
122         }
123     }
124     return pa_handle;
125 }
126
127
128 typedef struct {
129     PaStream *stream;
130     ALuint update_size;
131
132     RingBuffer *ring;
133 } pa_data;
134
135
136 static int pa_callback(const void *inputBuffer, void *outputBuffer,
137                        unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *timeInfo,
138                        const PaStreamCallbackFlags statusFlags, void *userData)
139 {
140     ALCdevice *device = (ALCdevice*)userData;
141
142     (void)inputBuffer;
143     (void)timeInfo;
144     (void)statusFlags;
145
146     aluMixData(device, outputBuffer, framesPerBuffer);
147     return 0;
148 }
149
150 static int pa_capture_cb(const void *inputBuffer, void *outputBuffer,
151                          unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *timeInfo,
152                          const PaStreamCallbackFlags statusFlags, void *userData)
153 {
154     ALCdevice *device = (ALCdevice*)userData;
155     pa_data *data = (pa_data*)device->ExtraData;
156
157     (void)outputBuffer;
158     (void)timeInfo;
159     (void)statusFlags;
160
161     WriteRingBuffer(data->ring, inputBuffer, framesPerBuffer);
162     return 0;
163 }
164
165
166 static ALCboolean pa_open_playback(ALCdevice *device, const ALCchar *deviceName)
167 {
168     const PaStreamInfo *streamInfo;
169     PaStreamParameters outParams;
170     pa_data *data;
171     PaError err;
172
173     if(!deviceName)
174         deviceName = pa_device;
175     else if(strcmp(deviceName, pa_device) != 0)
176         return ALC_FALSE;
177
178     if(!pa_load())
179         return ALC_FALSE;
180
181     data = (pa_data*)calloc(1, sizeof(pa_data));
182     data->update_size = device->UpdateSize;
183
184     device->ExtraData = data;
185
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;
192
193     switch(device->FmtType)
194     {
195         case DevFmtByte:
196             outParams.sampleFormat = paInt8;
197             break;
198         case DevFmtUByte:
199             outParams.sampleFormat = paUInt8;
200             break;
201         case DevFmtUShort:
202             device->FmtType = DevFmtShort;
203             /* fall-through */
204         case DevFmtShort:
205             outParams.sampleFormat = paInt16;
206             break;
207         case DevFmtFloat:
208             outParams.sampleFormat = paFloat32;
209             break;
210     }
211     outParams.channelCount = ChannelsFromDevFmt(device->FmtChans);
212
213     SetDefaultChannelOrder(device);
214
215     err = pPa_OpenStream(&data->stream, NULL, &outParams, device->Frequency,
216                          device->UpdateSize, paNoFlag, pa_callback, device);
217     if(err != paNoError)
218     {
219         AL_PRINT("Pa_OpenStream() returned an error: %s\n", pPa_GetErrorText(err));
220         device->ExtraData = NULL;
221         free(data);
222         return ALC_FALSE;
223     }
224     streamInfo = pPa_GetStreamInfo(data->stream);
225
226     device->szDeviceName = strdup(deviceName);
227     device->Frequency = streamInfo->sampleRate;
228
229     return ALC_TRUE;
230 }
231
232 static void pa_close_playback(ALCdevice *device)
233 {
234     pa_data *data = (pa_data*)device->ExtraData;
235     PaError err;
236
237     err = pPa_CloseStream(data->stream);
238     if(err != paNoError)
239         AL_PRINT("Error closing stream: %s\n", pPa_GetErrorText(err));
240
241     free(data);
242     device->ExtraData = NULL;
243 }
244
245 static ALCboolean pa_reset_playback(ALCdevice *device)
246 {
247     pa_data *data = (pa_data*)device->ExtraData;
248     const PaStreamInfo *streamInfo;
249     PaError err;
250
251     streamInfo = pPa_GetStreamInfo(data->stream);
252     device->Frequency = streamInfo->sampleRate;
253     device->UpdateSize = data->update_size;
254
255     err = pPa_StartStream(data->stream);
256     if(err != paNoError)
257     {
258         AL_PRINT("Pa_StartStream() returned an error: %s\n", pPa_GetErrorText(err));
259         return ALC_FALSE;
260     }
261
262     return ALC_TRUE;
263 }
264
265 static void pa_stop_playback(ALCdevice *device)
266 {
267     pa_data *data = (pa_data*)device->ExtraData;
268     PaError err;
269
270     err = pPa_StopStream(data->stream);
271     if(err != paNoError)
272         AL_PRINT("Error stopping stream: %s\n", pPa_GetErrorText(err));
273 }
274
275
276 static ALCboolean pa_open_capture(ALCdevice *device, const ALCchar *deviceName)
277 {
278     PaStreamParameters inParams;
279     ALuint frame_size;
280     pa_data *data;
281     PaError err;
282
283     if(!deviceName)
284         deviceName = pa_device;
285     else if(strcmp(deviceName, pa_device) != 0)
286         return ALC_FALSE;
287
288     if(!pa_load())
289         return ALC_FALSE;
290
291     data = (pa_data*)calloc(1, sizeof(pa_data));
292     if(data == NULL)
293     {
294         alcSetError(device, ALC_OUT_OF_MEMORY);
295         return ALC_FALSE;
296     }
297
298     frame_size = FrameSizeFromDevFmt(device->FmtChans, device->FmtType);
299     data->ring = CreateRingBuffer(frame_size, device->UpdateSize*device->NumUpdates);
300     if(data->ring == NULL)
301     {
302         alcSetError(device, ALC_OUT_OF_MEMORY);
303         goto error;
304     }
305
306     inParams.device = GetConfigValueInt("port", "capture", -1);
307     if(inParams.device < 0)
308         inParams.device = pPa_GetDefaultOutputDevice();
309     inParams.suggestedLatency = 0.0f;
310     inParams.hostApiSpecificStreamInfo = NULL;
311
312     switch(device->FmtType)
313     {
314         case DevFmtByte:
315             inParams.sampleFormat = paInt8;
316             break;
317         case DevFmtUByte:
318             inParams.sampleFormat = paUInt8;
319             break;
320         case DevFmtShort:
321             inParams.sampleFormat = paInt16;
322             break;
323         case DevFmtFloat:
324             inParams.sampleFormat = paFloat32;
325             break;
326         case DevFmtUShort:
327             AL_PRINT("Unsigned short not supported\n");
328             goto error;
329     }
330     inParams.channelCount = ChannelsFromDevFmt(device->FmtChans);
331
332     err = pPa_OpenStream(&data->stream, &inParams, NULL, device->Frequency,
333                          paFramesPerBufferUnspecified, paNoFlag, pa_capture_cb, device);
334     if(err != paNoError)
335     {
336         AL_PRINT("Pa_OpenStream() returned an error: %s\n", pPa_GetErrorText(err));
337         goto error;
338     }
339
340     device->szDeviceName = strdup(deviceName);
341
342     device->ExtraData = data;
343     return ALC_TRUE;
344
345 error:
346     DestroyRingBuffer(data->ring);
347     free(data);
348     return ALC_FALSE;
349 }
350
351 static void pa_close_capture(ALCdevice *device)
352 {
353     pa_data *data = (pa_data*)device->ExtraData;
354     PaError err;
355
356     err = pPa_CloseStream(data->stream);
357     if(err != paNoError)
358         AL_PRINT("Error closing stream: %s\n", pPa_GetErrorText(err));
359
360     free(data);
361     device->ExtraData = NULL;
362 }
363
364 static void pa_start_capture(ALCdevice *device)
365 {
366     pa_data *data = device->ExtraData;
367     PaError err;
368
369     err = pPa_StartStream(data->stream);
370     if(err != paNoError)
371         AL_PRINT("Error starting stream: %s\n", pPa_GetErrorText(err));
372 }
373
374 static void pa_stop_capture(ALCdevice *device)
375 {
376     pa_data *data = (pa_data*)device->ExtraData;
377     PaError err;
378
379     err = pPa_StopStream(data->stream);
380     if(err != paNoError)
381         AL_PRINT("Error stopping stream: %s\n", pPa_GetErrorText(err));
382 }
383
384 static void pa_capture_samples(ALCdevice *device, ALCvoid *buffer, ALCuint samples)
385 {
386     pa_data *data = device->ExtraData;
387     if(samples <= (ALCuint)RingBufferSize(data->ring))
388         ReadRingBuffer(data->ring, buffer, samples);
389     else
390         alcSetError(device, ALC_INVALID_VALUE);
391 }
392
393 static ALCuint pa_available_samples(ALCdevice *device)
394 {
395     pa_data *data = device->ExtraData;
396     return RingBufferSize(data->ring);
397 }
398
399
400 static const BackendFuncs pa_funcs = {
401     pa_open_playback,
402     pa_close_playback,
403     pa_reset_playback,
404     pa_stop_playback,
405     pa_open_capture,
406     pa_close_capture,
407     pa_start_capture,
408     pa_stop_capture,
409     pa_capture_samples,
410     pa_available_samples
411 };
412
413 void alc_pa_init(BackendFuncs *func_list)
414 {
415     *func_list = pa_funcs;
416 }
417
418 void alc_pa_deinit(void)
419 {
420     if(pa_handle)
421     {
422         pPa_Terminate();
423 #ifdef _WIN32
424         FreeLibrary(pa_handle);
425 #elif defined(HAVE_DLFCN_H)
426         dlclose(pa_handle);
427 #endif
428         pa_handle = NULL;
429     }
430 }
431
432 void alc_pa_probe(int type)
433 {
434     if(!pa_load()) return;
435
436     if(type == DEVICE_PROBE)
437         AppendDeviceList(pa_device);
438     else if(type == ALL_DEVICE_PROBE)
439         AppendAllDeviceList(pa_device);
440     else if(type == CAPTURE_DEVICE_PROBE)
441         AppendCaptureDeviceList(pa_device);
442 }