Update in properly-sized chunks for PulseAudio
[openal-soft.git] / Alc / portaudio.c
blobfb4b8ed538193468e13a612a608bfa8b78d08f44
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";
51 typedef struct {
52 PaStream *stream;
53 } pa_data;
56 static int pa_callback(const void *inputBuffer, void *outputBuffer,
57 unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *timeInfo,
58 const PaStreamCallbackFlags statusFlags, void *userData)
60 ALCdevice *device = (ALCdevice*)userData;
62 (void)inputBuffer;
63 (void)timeInfo;
64 (void)statusFlags;
66 aluMixData(device, outputBuffer, framesPerBuffer);
67 return 0;
71 static ALCboolean pa_open_playback(ALCdevice *device, const ALCchar *deviceName)
73 const PaStreamInfo *streamInfo;
74 PaStreamParameters outParams;
75 pa_data *data;
76 PaError err;
78 if(pa_handle == NULL)
79 return ALC_FALSE;
81 if(!deviceName)
82 deviceName = pa_device;
83 else if(strcmp(deviceName, pa_device) != 0)
84 return ALC_FALSE;
86 data = (pa_data*)calloc(1, sizeof(pa_data));
87 device->ExtraData = data;
89 outParams.device = GetConfigValueInt("port", "device", -1);
90 if(outParams.device < 0)
91 outParams.device = pPa_GetDefaultOutputDevice();
92 outParams.suggestedLatency = (device->UpdateSize*device->NumUpdates) /
93 (float)device->Frequency;
94 outParams.hostApiSpecificStreamInfo = NULL;
96 switch(aluBytesFromFormat(device->Format))
98 case 1:
99 outParams.sampleFormat = paUInt8;
100 break;
101 case 2:
102 outParams.sampleFormat = paInt16;
103 break;
104 case 4:
105 outParams.sampleFormat = paFloat32;
106 break;
107 default:
108 AL_PRINT("Unknown format: 0x%x\n", device->Format);
109 device->ExtraData = NULL;
110 free(data);
111 return ALC_FALSE;
113 outParams.channelCount = aluChannelsFromFormat(device->Format);
115 err = pPa_OpenStream(&data->stream, NULL, &outParams, device->Frequency,
116 device->UpdateSize, paNoFlag, pa_callback, device);
117 if(err != paNoError)
119 AL_PRINT("Pa_OpenStream() returned an error: %s\n", pPa_GetErrorText(err));
120 device->ExtraData = NULL;
121 free(data);
122 return ALC_FALSE;
124 streamInfo = pPa_GetStreamInfo(data->stream);
126 err = pPa_StartStream(data->stream);
127 if(err != paNoError)
129 AL_PRINT("Pa_StartStream() returned an error: %s\n", pPa_GetErrorText(err));
130 pPa_CloseStream(data->stream);
131 device->ExtraData = NULL;
132 free(data);
133 return ALC_FALSE;
136 device->szDeviceName = strdup(deviceName);
137 device->Frequency = streamInfo->sampleRate;
138 return ALC_TRUE;
141 static void pa_close_playback(ALCdevice *device)
143 pa_data *data = (pa_data*)device->ExtraData;
144 PaError err;
146 err = pPa_StopStream(data->stream);
147 if(err != paNoError)
148 fprintf(stderr, "Error stopping stream: %s\n", pPa_GetErrorText(err));
150 err = pPa_CloseStream(data->stream);
151 if(err != paNoError)
152 fprintf(stderr, "Error closing stream: %s\n", pPa_GetErrorText(err));
154 free(data);
155 device->ExtraData = NULL;
158 static ALCboolean pa_reset_playback(ALCdevice *device)
160 pa_data *data = (pa_data*)device->ExtraData;
161 const PaStreamInfo *streamInfo;
163 streamInfo = pPa_GetStreamInfo(data->stream);
164 device->Frequency = streamInfo->sampleRate;
166 return ALC_TRUE;
169 static void pa_stop_playback(ALCdevice *device)
171 (void)device;
175 static ALCboolean pa_open_capture(ALCdevice *device, const ALCchar *deviceName)
177 return ALC_FALSE;
178 (void)device;
179 (void)deviceName;
184 static const BackendFuncs pa_funcs = {
185 pa_open_playback,
186 pa_close_playback,
187 pa_reset_playback,
188 pa_stop_playback,
189 pa_open_capture,
190 NULL,
191 NULL,
192 NULL,
193 NULL,
194 NULL
197 void alc_pa_init(BackendFuncs *func_list)
199 const char *str;
200 PaError err;
202 *func_list = pa_funcs;
204 #ifdef HAVE_DLFCN_H
205 #if defined(__APPLE__) && defined(__MACH__)
206 # define PALIB "libportaudio.2.dylib"
207 #else
208 # define PALIB "libportaudio.so.2"
209 #endif
210 pa_handle = dlopen(PALIB, RTLD_NOW);
211 if(!pa_handle)
212 return;
213 dlerror();
215 #define LOAD_FUNC(f) do { \
216 p##f = (typeof(f)*)dlsym(pa_handle, #f); \
217 if((str=dlerror()) != NULL) \
219 dlclose(pa_handle); \
220 pa_handle = NULL; \
221 AL_PRINT("Could not load %s from "PALIB": %s\n", #f, str); \
222 return; \
224 } while(0)
225 #else
226 str = NULL;
227 pa_handle = (void*)0xDEADBEEF;
228 #define LOAD_FUNC(f) p##f = f
229 #endif
231 LOAD_FUNC(Pa_Initialize);
232 LOAD_FUNC(Pa_Terminate);
233 LOAD_FUNC(Pa_GetErrorText);
234 LOAD_FUNC(Pa_StartStream);
235 LOAD_FUNC(Pa_StopStream);
236 LOAD_FUNC(Pa_OpenStream);
237 LOAD_FUNC(Pa_CloseStream);
238 LOAD_FUNC(Pa_GetDefaultOutputDevice);
239 LOAD_FUNC(Pa_GetStreamInfo);
240 #undef LOAD_FUNC
242 if((err=pPa_Initialize()) != paNoError)
244 AL_PRINT("Pa_Initialize() returned an error: %s\n", pPa_GetErrorText(err));
245 alc_pa_deinit();
246 return;
250 void alc_pa_deinit(void)
252 if(pa_handle)
254 pPa_Terminate();
255 #ifdef HAVE_DLFCN_H
256 dlclose(pa_handle);
257 pa_handle = NULL;
258 #endif
262 void alc_pa_probe(int type)
264 if(!pa_handle)
265 return;
267 if(type == DEVICE_PROBE)
268 AppendDeviceList(pa_device);
269 else if(type == ALL_DEVICE_PROBE)
270 AppendAllDeviceList(pa_device);