Initialize OpenAL with a constructor call instead of first-use
[openal-soft.git] / Alc / portaudio.c
blob0142cac3a2f0f14e1031ffa9f10fca92119c0acf
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_GetErrorText);
39 MAKE_FUNC(Pa_StartStream);
40 MAKE_FUNC(Pa_StopStream);
41 MAKE_FUNC(Pa_OpenStream);
42 MAKE_FUNC(Pa_CloseStream);
43 MAKE_FUNC(Pa_GetDefaultOutputDevice);
44 #undef MAKE_FUNC
47 static const ALCchar pa_device[] = "PortAudio Software";
49 typedef struct {
50 PaStream *stream;
51 } pa_data;
54 static int pa_callback(const void *inputBuffer, void *outputBuffer,
55 unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *timeInfo,
56 const PaStreamCallbackFlags statusFlags, void *userData)
58 ALCdevice *device = (ALCdevice*)userData;
59 int frameSize;
61 (void)inputBuffer;
62 (void)timeInfo;
63 (void)statusFlags;
65 frameSize = aluBytesFromFormat(device->Format);
66 frameSize *= aluChannelsFromFormat(device->Format);
68 SuspendContext(NULL);
69 aluMixData(device->Context, outputBuffer, framesPerBuffer*frameSize, device->Format);
70 ProcessContext(NULL);
72 return 0;
76 static ALCboolean pa_open_playback(ALCdevice *device, const ALCchar *deviceName)
78 PaStreamParameters outParams;
79 pa_data *data;
80 int periods;
81 PaError err;
83 if(pa_handle == NULL)
84 return ALC_FALSE;
86 if(!deviceName)
87 deviceName = pa_device;
88 else if(strcmp(deviceName, pa_device) != 0)
89 return ALC_FALSE;
91 data = (pa_data*)calloc(1, sizeof(pa_data));
92 device->ExtraData = data;
94 outParams.device = GetConfigValueInt("port", "device", -1);
95 if(outParams.device < 0)
96 outParams.device = pPa_GetDefaultOutputDevice();
97 outParams.suggestedLatency = (float)device->BufferSize /
98 (float)device->Frequency;
99 outParams.hostApiSpecificStreamInfo = NULL;
101 switch(aluBytesFromFormat(device->Format))
103 case 1:
104 outParams.sampleFormat = paUInt8;
105 break;
106 case 2:
107 outParams.sampleFormat = paInt16;
108 break;
109 case 4:
110 outParams.sampleFormat = paFloat32;
111 break;
112 default:
113 outParams.sampleFormat = -1;
114 AL_PRINT("Unknown format?! %x\n", device->Format);
117 periods = GetConfigValueInt("port", "periods", 4);
118 if((int)periods <= 0)
119 periods = 4;
120 outParams.channelCount = aluChannelsFromFormat(device->Format);
122 err = pPa_OpenStream(&data->stream, NULL, &outParams, device->Frequency,
123 device->BufferSize/periods, paNoFlag,
124 pa_callback, device);
125 if(err != paNoError)
127 AL_PRINT("Pa_OpenStream() returned an error: %s\n", pPa_GetErrorText(err));
128 device->ExtraData = NULL;
129 free(data);
130 return ALC_FALSE;
133 err = pPa_StartStream(data->stream);
134 if(err != paNoError)
136 AL_PRINT("Pa_StartStream() returned an error: %s\n", pPa_GetErrorText(err));
137 pPa_CloseStream(data->stream);
138 device->ExtraData = NULL;
139 free(data);
140 return ALC_FALSE;
143 device->szDeviceName = strdup(deviceName);
144 device->UpdateSize = device->BufferSize/periods;
145 return ALC_TRUE;
148 static void pa_close_playback(ALCdevice *device)
150 pa_data *data = (pa_data*)device->ExtraData;
151 PaError err;
153 err = pPa_StopStream(data->stream);
154 if(err != paNoError)
155 fprintf(stderr, "Error stopping stream: %s\n", pPa_GetErrorText(err));
157 err = pPa_CloseStream(data->stream);
158 if(err != paNoError)
159 fprintf(stderr, "Error closing stream: %s\n", pPa_GetErrorText(err));
161 free(data);
162 device->ExtraData = NULL;
165 static ALCboolean pa_start_context(ALCdevice *device, ALCcontext *context)
167 device->Frequency = context->Frequency;
168 return ALC_TRUE;
171 static void pa_stop_context(ALCdevice *device, ALCcontext *context)
173 (void)device;
174 (void)context;
178 static ALCboolean pa_open_capture(ALCdevice *device, const ALCchar *deviceName)
180 return ALC_FALSE;
181 (void)device;
182 (void)deviceName;
187 static const BackendFuncs pa_funcs = {
188 pa_open_playback,
189 pa_close_playback,
190 pa_start_context,
191 pa_stop_context,
192 pa_open_capture,
193 NULL,
194 NULL,
195 NULL,
196 NULL,
197 NULL
200 void alc_pa_init(BackendFuncs *func_list)
202 const char *str;
203 PaError err;
205 *func_list = pa_funcs;
207 #ifdef HAVE_DLFCN_H
208 #if defined(__APPLE__) && defined(__MACH__)
209 # define PALIB "libportaudio.2.dylib"
210 #else
211 # define PALIB "libportaudio.so.2"
212 #endif
213 pa_handle = dlopen(PALIB, RTLD_NOW);
214 if(!pa_handle)
215 return;
216 dlerror();
218 #define LOAD_FUNC(f) do { \
219 p##f = (typeof(f)*)dlsym(pa_handle, #f); \
220 if((str=dlerror()) != NULL) \
222 dlclose(pa_handle); \
223 pa_handle = NULL; \
224 AL_PRINT("Could not load %s from "PALIB": %s\n", #f, str); \
225 return; \
227 } while(0)
228 #else
229 str = NULL;
230 pa_handle = (void*)0xDEADBEEF;
231 #define LOAD_FUNC(f) p##f = f
232 #endif
234 LOAD_FUNC(Pa_Initialize);
235 LOAD_FUNC(Pa_GetErrorText);
236 LOAD_FUNC(Pa_StartStream);
237 LOAD_FUNC(Pa_StopStream);
238 LOAD_FUNC(Pa_OpenStream);
239 LOAD_FUNC(Pa_CloseStream);
240 LOAD_FUNC(Pa_GetDefaultOutputDevice);
241 #undef LOAD_FUNC
243 if((err=pPa_Initialize()) != paNoError)
245 AL_PRINT("Pa_Initialize() returned an error: %s\n", pPa_GetErrorText(err));
246 alc_pa_deinit();
247 return;
251 void alc_pa_deinit(void)
253 #ifdef HAVE_DLFCN_H
254 if(pa_handle)
255 dlclose(pa_handle);
256 pa_handle = NULL;
257 #endif
260 void alc_pa_probe(int type)
262 if(!pa_handle)
263 return;
265 if(type == DEVICE_PROBE)
266 AppendDeviceList(pa_device);
267 else if(type == ALL_DEVICE_PROBE)
268 AppendAllDeviceList(pa_device);