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_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
);
49 static const ALCchar pa_device
[] = "PortAudio Software";
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
;
66 aluMixData(device
, outputBuffer
, framesPerBuffer
);
71 static ALCboolean
pa_open_playback(ALCdevice
*device
, const ALCchar
*deviceName
)
73 const PaStreamInfo
*streamInfo
;
74 PaStreamParameters outParams
;
82 deviceName
= pa_device
;
83 else if(strcmp(deviceName
, pa_device
) != 0)
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
))
99 outParams
.sampleFormat
= paUInt8
;
102 outParams
.sampleFormat
= paInt16
;
105 outParams
.sampleFormat
= paFloat32
;
108 AL_PRINT("Unknown format: 0x%x\n", device
->Format
);
109 device
->ExtraData
= NULL
;
113 outParams
.channelCount
= aluChannelsFromFormat(device
->Format
);
115 err
= pPa_OpenStream(&data
->stream
, NULL
, &outParams
, device
->Frequency
,
116 device
->UpdateSize
, paNoFlag
, pa_callback
, device
);
119 AL_PRINT("Pa_OpenStream() returned an error: %s\n", pPa_GetErrorText(err
));
120 device
->ExtraData
= NULL
;
124 streamInfo
= pPa_GetStreamInfo(data
->stream
);
126 err
= pPa_StartStream(data
->stream
);
129 AL_PRINT("Pa_StartStream() returned an error: %s\n", pPa_GetErrorText(err
));
130 pPa_CloseStream(data
->stream
);
131 device
->ExtraData
= NULL
;
136 device
->szDeviceName
= strdup(deviceName
);
137 device
->Frequency
= streamInfo
->sampleRate
;
141 static void pa_close_playback(ALCdevice
*device
)
143 pa_data
*data
= (pa_data
*)device
->ExtraData
;
146 err
= pPa_StopStream(data
->stream
);
148 fprintf(stderr
, "Error stopping stream: %s\n", pPa_GetErrorText(err
));
150 err
= pPa_CloseStream(data
->stream
);
152 fprintf(stderr
, "Error closing stream: %s\n", pPa_GetErrorText(err
));
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
;
169 static void pa_stop_playback(ALCdevice
*device
)
175 static ALCboolean
pa_open_capture(ALCdevice
*device
, const ALCchar
*deviceName
)
184 static const BackendFuncs pa_funcs
= {
197 void alc_pa_init(BackendFuncs
*func_list
)
202 *func_list
= pa_funcs
;
205 #if defined(__APPLE__) && defined(__MACH__)
206 # define PALIB "libportaudio.2.dylib"
208 # define PALIB "libportaudio.so.2"
210 pa_handle
= dlopen(PALIB
, RTLD_NOW
);
215 #define LOAD_FUNC(f) do { \
216 p##f = (typeof(f)*)dlsym(pa_handle, #f); \
217 if((str=dlerror()) != NULL) \
219 dlclose(pa_handle); \
221 AL_PRINT("Could not load %s from "PALIB": %s\n", #f, str); \
227 pa_handle
= (void*)0xDEADBEEF;
228 #define LOAD_FUNC(f) p##f = f
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
);
242 if((err
=pPa_Initialize()) != paNoError
)
244 AL_PRINT("Pa_Initialize() returned an error: %s\n", pPa_GetErrorText(err
));
250 void alc_pa_deinit(void)
262 void alc_pa_probe(int type
)
267 if(type
== DEVICE_PROBE
)
268 AppendDeviceList(pa_device
);
269 else if(type
== ALL_DEVICE_PROBE
)
270 AppendAllDeviceList(pa_device
);