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
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
);
47 static const ALCchar pa_device
[] = "PortAudio Software";
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
;
65 frameSize
= aluBytesFromFormat(device
->Format
);
66 frameSize
*= aluChannelsFromFormat(device
->Format
);
69 aluMixData(device
->Context
, outputBuffer
, framesPerBuffer
*frameSize
, device
->Format
);
76 static ALCboolean
pa_open_playback(ALCdevice
*device
, const ALCchar
*deviceName
)
78 PaStreamParameters outParams
;
87 deviceName
= pa_device
;
88 else if(strcmp(deviceName
, pa_device
) != 0)
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
))
104 outParams
.sampleFormat
= paUInt8
;
107 outParams
.sampleFormat
= paInt16
;
110 outParams
.sampleFormat
= paFloat32
;
113 outParams
.sampleFormat
= -1;
114 AL_PRINT("Unknown format?! %x\n", device
->Format
);
117 periods
= GetConfigValueInt("port", "periods", 4);
118 if((int)periods
<= 0)
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
);
127 AL_PRINT("Pa_OpenStream() returned an error: %s\n", pPa_GetErrorText(err
));
128 device
->ExtraData
= NULL
;
133 err
= pPa_StartStream(data
->stream
);
136 AL_PRINT("Pa_StartStream() returned an error: %s\n", pPa_GetErrorText(err
));
137 pPa_CloseStream(data
->stream
);
138 device
->ExtraData
= NULL
;
143 device
->szDeviceName
= strdup(deviceName
);
144 device
->UpdateSize
= device
->BufferSize
/periods
;
148 static void pa_close_playback(ALCdevice
*device
)
150 pa_data
*data
= (pa_data
*)device
->ExtraData
;
153 err
= pPa_StopStream(data
->stream
);
155 fprintf(stderr
, "Error stopping stream: %s\n", pPa_GetErrorText(err
));
157 err
= pPa_CloseStream(data
->stream
);
159 fprintf(stderr
, "Error closing stream: %s\n", pPa_GetErrorText(err
));
162 device
->ExtraData
= NULL
;
165 static ALCboolean
pa_start_context(ALCdevice
*device
, ALCcontext
*context
)
167 device
->Frequency
= context
->Frequency
;
171 static void pa_stop_context(ALCdevice
*device
, ALCcontext
*context
)
178 static ALCboolean
pa_open_capture(ALCdevice
*device
, const ALCchar
*deviceName
)
187 static const BackendFuncs pa_funcs
= {
200 void alc_pa_init(BackendFuncs
*func_list
)
205 *func_list
= pa_funcs
;
208 #if defined(__APPLE__) && defined(__MACH__)
209 # define PALIB "libportaudio.2.dylib"
211 # define PALIB "libportaudio.so.2"
213 pa_handle
= dlopen(PALIB
, RTLD_NOW
);
218 #define LOAD_FUNC(f) do { \
219 p##f = (typeof(f)*)dlsym(pa_handle, #f); \
220 if((str=dlerror()) != NULL) \
222 dlclose(pa_handle); \
224 AL_PRINT("Could not load %s from "PALIB": %s\n", #f, str); \
230 pa_handle
= (void*)0xDEADBEEF;
231 #define LOAD_FUNC(f) p##f = f
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
);
243 if((err
=pPa_Initialize()) != paNoError
)
245 AL_PRINT("Pa_Initialize() returned an error: %s\n", pPa_GetErrorText(err
));
251 void alc_pa_deinit(void)
260 void alc_pa_probe(int type
)
265 if(type
== DEVICE_PROBE
)
266 AppendDeviceList(pa_device
);
267 else if(type
== ALL_DEVICE_PROBE
)
268 AppendAllDeviceList(pa_device
);