Close some libs when deinitializing
[openal-soft.git] / Alc / portaudio.c
blob20bb2d7bb0e66ee659ccb1ffa69e8d4e39fc7002
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)
88 if(strcmp(deviceName, pa_device) != 0)
89 return ALC_FALSE;
92 data = (pa_data*)calloc(1, sizeof(pa_data));
93 device->ExtraData = data;
95 outParams.device = GetConfigValueInt("port", "device", -1);
96 if(outParams.device < 0)
97 outParams.device = pPa_GetDefaultOutputDevice();
98 outParams.suggestedLatency = (float)device->BufferSize /
99 (float)device->Frequency;
100 outParams.hostApiSpecificStreamInfo = NULL;
102 switch(aluBytesFromFormat(device->Format))
104 case 1:
105 outParams.sampleFormat = paUInt8;
106 break;
107 case 2:
108 outParams.sampleFormat = paInt16;
109 break;
110 case 4:
111 outParams.sampleFormat = paFloat32;
112 break;
113 default:
114 outParams.sampleFormat = -1;
115 AL_PRINT("Unknown format?! %x\n", device->Format);
118 periods = GetConfigValueInt("port", "periods", 4);
119 if((int)periods <= 0)
120 periods = 4;
121 outParams.channelCount = aluChannelsFromFormat(device->Format);
123 err = pPa_OpenStream(&data->stream, NULL, &outParams, device->Frequency,
124 device->BufferSize/periods, paNoFlag,
125 pa_callback, device);
126 if(err != paNoError)
128 AL_PRINT("Pa_OpenStream() returned an error: %s\n", pPa_GetErrorText(err));
129 device->ExtraData = NULL;
130 free(data);
131 return ALC_FALSE;
134 err = pPa_StartStream(data->stream);
135 if(err != paNoError)
137 AL_PRINT("Pa_StartStream() returned an error: %s\n", pPa_GetErrorText(err));
138 pPa_CloseStream(data->stream);
139 device->ExtraData = NULL;
140 free(data);
141 return ALC_FALSE;
144 device->szDeviceName = strdup(pa_device);
145 device->UpdateSize = device->BufferSize/periods;
146 return ALC_TRUE;
149 static void pa_close_playback(ALCdevice *device)
151 pa_data *data = (pa_data*)device->ExtraData;
152 PaError err;
154 err = pPa_StopStream(data->stream);
155 if(err != paNoError)
156 fprintf(stderr, "Error stopping stream: %s\n", pPa_GetErrorText(err));
158 err = pPa_CloseStream(data->stream);
159 if(err != paNoError)
160 fprintf(stderr, "Error closing stream: %s\n", pPa_GetErrorText(err));
162 free(data);
163 device->ExtraData = NULL;
166 static ALCboolean pa_start_context(ALCdevice *device, ALCcontext *context)
168 device->Frequency = context->Frequency;
169 return ALC_TRUE;
172 static void pa_stop_context(ALCdevice *device, ALCcontext *context)
174 (void)device;
175 (void)context;
179 static ALCboolean pa_open_capture(ALCdevice *device, const ALCchar *deviceName)
181 return ALC_FALSE;
182 (void)device;
183 (void)deviceName;
188 static const BackendFuncs pa_funcs = {
189 pa_open_playback,
190 pa_close_playback,
191 pa_start_context,
192 pa_stop_context,
193 pa_open_capture,
194 NULL,
195 NULL,
196 NULL,
197 NULL,
198 NULL
201 void alc_pa_init(BackendFuncs *func_list)
203 const char *str;
204 PaError err;
206 *func_list = pa_funcs;
208 #ifdef HAVE_DLFCN_H
209 #if defined(__APPLE__) && defined(__MACH__)
210 # define PALIB "libportaudio.2.dylib"
211 #else
212 # define PALIB "libportaudio.so.2"
213 #endif
214 pa_handle = dlopen(PALIB, RTLD_NOW);
215 if(!pa_handle)
216 return;
217 dlerror();
219 #define LOAD_FUNC(f) do { \
220 p##f = (typeof(f)*)dlsym(pa_handle, #f); \
221 if((str=dlerror()) != NULL) \
223 dlclose(pa_handle); \
224 pa_handle = NULL; \
225 AL_PRINT("Could not load %s from "PALIB": %s\n", #f, str); \
226 return; \
228 } while(0)
229 #else
230 str = NULL;
231 pa_handle = (void*)0xDEADBEEF;
232 #define LOAD_FUNC(f) p##f = f
233 #endif
235 LOAD_FUNC(Pa_Initialize);
236 LOAD_FUNC(Pa_GetErrorText);
237 LOAD_FUNC(Pa_StartStream);
238 LOAD_FUNC(Pa_StopStream);
239 LOAD_FUNC(Pa_OpenStream);
240 LOAD_FUNC(Pa_CloseStream);
241 LOAD_FUNC(Pa_GetDefaultOutputDevice);
242 #undef LOAD_FUNC
244 if((err=pPa_Initialize()) != paNoError)
246 AL_PRINT("Pa_Initialize() returned an error: %s\n", pPa_GetErrorText(err));
247 return;
250 AppendDeviceList(pa_device);
251 AppendAllDeviceList(pa_device);
254 void alc_pa_deinit(void)
256 #ifdef HAVE_DLFCN_H
257 dlclose(pa_handle);
258 pa_handle = NULL;
259 #endif