Fix IMA ADPCM offset calculation
[openal-soft.git] / Alc / portaudio.c
blob580cc4fbd41d98a6efa1c9fe694e47d5c28ea53c
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 volatile ALuint load_count;
53 void pa_load(void)
55 const char *str;
56 PaError err;
58 if(load_count == 0)
60 #ifdef HAVE_DLFCN_H
61 #if defined(__APPLE__) && defined(__MACH__)
62 # define PALIB "libportaudio.2.dylib"
63 #else
64 # define PALIB "libportaudio.so.2"
65 #endif
66 pa_handle = dlopen(PALIB, RTLD_NOW);
67 if(!pa_handle)
68 return;
69 dlerror();
71 #define LOAD_FUNC(f) do { \
72 p##f = (typeof(f)*)dlsym(pa_handle, #f); \
73 if((str=dlerror()) != NULL) \
74 { \
75 dlclose(pa_handle); \
76 pa_handle = NULL; \
77 AL_PRINT("Could not load %s from "PALIB": %s\n", #f, str); \
78 return; \
79 } \
80 } while(0)
81 #else
82 str = NULL;
83 pa_handle = (void*)0xDEADBEEF;
84 #define LOAD_FUNC(f) p##f = f
85 #endif
87 LOAD_FUNC(Pa_Initialize);
88 LOAD_FUNC(Pa_Terminate);
89 LOAD_FUNC(Pa_GetErrorText);
90 LOAD_FUNC(Pa_StartStream);
91 LOAD_FUNC(Pa_StopStream);
92 LOAD_FUNC(Pa_OpenStream);
93 LOAD_FUNC(Pa_CloseStream);
94 LOAD_FUNC(Pa_GetDefaultOutputDevice);
95 LOAD_FUNC(Pa_GetStreamInfo);
97 #undef LOAD_FUNC
99 if((err=pPa_Initialize()) != paNoError)
101 AL_PRINT("Pa_Initialize() returned an error: %s\n", pPa_GetErrorText(err));
102 #ifdef HAVE_DLFCN_H
103 dlclose(pa_handle);
104 #endif
105 pa_handle = NULL;
106 return;
110 ++load_count;
113 void pa_unload(void)
115 if(load_count == 0 || --load_count > 0)
116 return;
118 pPa_Terminate();
119 #ifdef HAVE_DLFCN_H
120 dlclose(pa_handle);
121 #endif
122 pa_handle = NULL;
126 typedef struct {
127 PaStream *stream;
128 } pa_data;
131 static int pa_callback(const void *inputBuffer, void *outputBuffer,
132 unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *timeInfo,
133 const PaStreamCallbackFlags statusFlags, void *userData)
135 ALCdevice *device = (ALCdevice*)userData;
137 (void)inputBuffer;
138 (void)timeInfo;
139 (void)statusFlags;
141 aluMixData(device, outputBuffer, framesPerBuffer);
142 return 0;
146 static ALCboolean pa_open_playback(ALCdevice *device, const ALCchar *deviceName)
148 const PaStreamInfo *streamInfo;
149 PaStreamParameters outParams;
150 pa_data *data;
151 PaError err;
153 if(!deviceName)
154 deviceName = pa_device;
155 else if(strcmp(deviceName, pa_device) != 0)
156 return ALC_FALSE;
158 pa_load();
159 if(pa_handle == NULL)
160 return ALC_FALSE;
162 data = (pa_data*)calloc(1, sizeof(pa_data));
163 device->ExtraData = data;
165 outParams.device = GetConfigValueInt("port", "device", -1);
166 if(outParams.device < 0)
167 outParams.device = pPa_GetDefaultOutputDevice();
168 outParams.suggestedLatency = (device->UpdateSize*device->NumUpdates) /
169 (float)device->Frequency;
170 outParams.hostApiSpecificStreamInfo = NULL;
172 switch(aluBytesFromFormat(device->Format))
174 case 1:
175 outParams.sampleFormat = paUInt8;
176 break;
177 case 2:
178 outParams.sampleFormat = paInt16;
179 break;
180 case 4:
181 outParams.sampleFormat = paFloat32;
182 break;
183 default:
184 AL_PRINT("Unknown format: 0x%x\n", device->Format);
185 device->ExtraData = NULL;
186 free(data);
187 pa_unload();
188 return ALC_FALSE;
190 outParams.channelCount = aluChannelsFromFormat(device->Format);
192 SetDefaultChannelOrder(device);
194 err = pPa_OpenStream(&data->stream, NULL, &outParams, device->Frequency,
195 device->UpdateSize, paNoFlag, pa_callback, device);
196 if(err != paNoError)
198 AL_PRINT("Pa_OpenStream() returned an error: %s\n", pPa_GetErrorText(err));
199 device->ExtraData = NULL;
200 free(data);
201 pa_unload();
202 return ALC_FALSE;
204 streamInfo = pPa_GetStreamInfo(data->stream);
206 err = pPa_StartStream(data->stream);
207 if(err != paNoError)
209 AL_PRINT("Pa_StartStream() returned an error: %s\n", pPa_GetErrorText(err));
210 pPa_CloseStream(data->stream);
211 device->ExtraData = NULL;
212 free(data);
213 pa_unload();
214 return ALC_FALSE;
217 device->szDeviceName = strdup(deviceName);
218 device->Frequency = streamInfo->sampleRate;
219 return ALC_TRUE;
222 static void pa_close_playback(ALCdevice *device)
224 pa_data *data = (pa_data*)device->ExtraData;
225 PaError err;
227 err = pPa_StopStream(data->stream);
228 if(err != paNoError)
229 fprintf(stderr, "Error stopping stream: %s\n", pPa_GetErrorText(err));
231 err = pPa_CloseStream(data->stream);
232 if(err != paNoError)
233 fprintf(stderr, "Error closing stream: %s\n", pPa_GetErrorText(err));
235 free(data);
236 device->ExtraData = NULL;
238 pa_unload();
241 static ALCboolean pa_reset_playback(ALCdevice *device)
243 pa_data *data = (pa_data*)device->ExtraData;
244 const PaStreamInfo *streamInfo;
246 streamInfo = pPa_GetStreamInfo(data->stream);
247 device->Frequency = streamInfo->sampleRate;
249 return ALC_TRUE;
252 static void pa_stop_playback(ALCdevice *device)
254 (void)device;
258 static ALCboolean pa_open_capture(ALCdevice *device, const ALCchar *deviceName)
260 return ALC_FALSE;
261 (void)device;
262 (void)deviceName;
267 static const BackendFuncs pa_funcs = {
268 pa_open_playback,
269 pa_close_playback,
270 pa_reset_playback,
271 pa_stop_playback,
272 pa_open_capture,
273 NULL,
274 NULL,
275 NULL,
276 NULL,
277 NULL
280 void alc_pa_init(BackendFuncs *func_list)
282 *func_list = pa_funcs;
285 void alc_pa_deinit(void)
289 void alc_pa_probe(int type)
291 pa_load();
292 if(!pa_handle) return;
294 if(type == DEVICE_PROBE)
295 AppendDeviceList(pa_device);
296 else if(type == ALL_DEVICE_PROBE)
297 AppendAllDeviceList(pa_device);
299 pa_unload();