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
30 #include <portaudio.h>
33 static const ALCchar pa_device
[] = "PortAudio Default";
36 static void *pa_handle
;
38 #define MAKE_FUNC(x) static typeof(x) * p##x
39 MAKE_FUNC(Pa_Initialize
);
40 MAKE_FUNC(Pa_Terminate
);
41 MAKE_FUNC(Pa_GetErrorText
);
42 MAKE_FUNC(Pa_StartStream
);
43 MAKE_FUNC(Pa_StopStream
);
44 MAKE_FUNC(Pa_OpenStream
);
45 MAKE_FUNC(Pa_CloseStream
);
46 MAKE_FUNC(Pa_GetDefaultOutputDevice
);
47 MAKE_FUNC(Pa_GetStreamInfo
);
50 #define Pa_Initialize pPa_Initialize
51 #define Pa_Terminate pPa_Terminate
52 #define Pa_GetErrorText pPa_GetErrorText
53 #define Pa_StartStream pPa_StartStream
54 #define Pa_StopStream pPa_StopStream
55 #define Pa_OpenStream pPa_OpenStream
56 #define Pa_CloseStream pPa_CloseStream
57 #define Pa_GetDefaultOutputDevice pPa_GetDefaultOutputDevice
58 #define Pa_GetStreamInfo pPa_GetStreamInfo
61 static ALCboolean
pa_load(void)
69 # define PALIB "portaudio.dll"
70 #elif defined(__APPLE__) && defined(__MACH__)
71 # define PALIB "libportaudio.2.dylib"
72 #elif defined(__OpenBSD__)
73 # define PALIB "libportaudio.so"
75 # define PALIB "libportaudio.so.2"
78 pa_handle
= LoadLib(PALIB
);
82 #define LOAD_FUNC(f) do { \
83 p##f = GetSymbol(pa_handle, #f); \
86 CloseLib(pa_handle); \
91 LOAD_FUNC(Pa_Initialize
);
92 LOAD_FUNC(Pa_Terminate
);
93 LOAD_FUNC(Pa_GetErrorText
);
94 LOAD_FUNC(Pa_StartStream
);
95 LOAD_FUNC(Pa_StopStream
);
96 LOAD_FUNC(Pa_OpenStream
);
97 LOAD_FUNC(Pa_CloseStream
);
98 LOAD_FUNC(Pa_GetDefaultOutputDevice
);
99 LOAD_FUNC(Pa_GetStreamInfo
);
102 pa_handle
= (void*)0xDEADBEEF;
105 if((err
=Pa_Initialize()) != paNoError
)
107 ERR("Pa_Initialize() returned an error: %s\n", Pa_GetErrorText(err
));
125 static int pa_callback(const void *inputBuffer
, void *outputBuffer
,
126 unsigned long framesPerBuffer
, const PaStreamCallbackTimeInfo
*timeInfo
,
127 const PaStreamCallbackFlags statusFlags
, void *userData
)
129 ALCdevice
*device
= (ALCdevice
*)userData
;
135 aluMixData(device
, outputBuffer
, framesPerBuffer
);
139 static int pa_capture_cb(const void *inputBuffer
, void *outputBuffer
,
140 unsigned long framesPerBuffer
, const PaStreamCallbackTimeInfo
*timeInfo
,
141 const PaStreamCallbackFlags statusFlags
, void *userData
)
143 ALCdevice
*device
= (ALCdevice
*)userData
;
144 pa_data
*data
= (pa_data
*)device
->ExtraData
;
150 WriteRingBuffer(data
->ring
, inputBuffer
, framesPerBuffer
);
155 static ALCenum
pa_open_playback(ALCdevice
*device
, const ALCchar
*deviceName
)
157 PaStreamParameters outParams
;
162 deviceName
= pa_device
;
163 else if(strcmp(deviceName
, pa_device
) != 0)
164 return ALC_INVALID_VALUE
;
166 data
= (pa_data
*)calloc(1, sizeof(pa_data
));
167 data
->update_size
= device
->UpdateSize
;
169 device
->ExtraData
= data
;
171 outParams
.device
= -1;
172 if(!ConfigValueInt("port", "device", &outParams
.device
) || outParams
.device
< 0)
173 outParams
.device
= Pa_GetDefaultOutputDevice();
174 outParams
.suggestedLatency
= (device
->UpdateSize
*device
->NumUpdates
) /
175 (float)device
->Frequency
;
176 outParams
.hostApiSpecificStreamInfo
= NULL
;
178 outParams
.channelCount
= ((device
->FmtChans
== DevFmtMono
) ? 1 : 2);
181 switch(device
->FmtType
)
184 outParams
.sampleFormat
= paInt8
;
187 outParams
.sampleFormat
= paUInt8
;
190 device
->FmtType
= DevFmtShort
;
193 outParams
.sampleFormat
= paInt16
;
196 device
->FmtType
= DevFmtInt
;
199 outParams
.sampleFormat
= paInt32
;
202 outParams
.sampleFormat
= paFloat32
;
206 err
= Pa_OpenStream(&data
->stream
, NULL
, &outParams
, device
->Frequency
,
207 device
->UpdateSize
, paNoFlag
, pa_callback
, device
);
210 if(device
->FmtType
== DevFmtFloat
)
212 device
->FmtType
= DevFmtShort
;
215 ERR("Pa_OpenStream() returned an error: %s\n", Pa_GetErrorText(err
));
216 device
->ExtraData
= NULL
;
218 return ALC_INVALID_VALUE
;
221 device
->szDeviceName
= strdup(deviceName
);
223 if((ALuint
)outParams
.channelCount
!= ChannelsFromDevFmt(device
->FmtChans
))
225 if(outParams
.channelCount
!= 1 && outParams
.channelCount
!= 2)
227 ERR("Unhandled channel count: %u\n", outParams
.channelCount
);
228 Pa_CloseStream(data
->stream
);
229 device
->ExtraData
= NULL
;
231 return ALC_INVALID_VALUE
;
233 if((device
->Flags
&DEVICE_CHANNELS_REQUEST
))
234 ERR("Failed to set %s, got %u channels instead\n", DevFmtChannelsString(device
->FmtChans
), outParams
.channelCount
);
235 device
->Flags
&= ~DEVICE_CHANNELS_REQUEST
;
236 device
->FmtChans
= ((outParams
.channelCount
==1) ? DevFmtMono
: DevFmtStereo
);
238 SetDefaultChannelOrder(device
);
243 static void pa_close_playback(ALCdevice
*device
)
245 pa_data
*data
= (pa_data
*)device
->ExtraData
;
248 err
= Pa_CloseStream(data
->stream
);
250 ERR("Error closing stream: %s\n", Pa_GetErrorText(err
));
253 device
->ExtraData
= NULL
;
256 static ALCboolean
pa_reset_playback(ALCdevice
*device
)
258 pa_data
*data
= (pa_data
*)device
->ExtraData
;
259 const PaStreamInfo
*streamInfo
;
262 streamInfo
= Pa_GetStreamInfo(data
->stream
);
263 device
->Frequency
= streamInfo
->sampleRate
;
264 device
->UpdateSize
= data
->update_size
;
266 err
= Pa_StartStream(data
->stream
);
269 ERR("Pa_StartStream() returned an error: %s\n", Pa_GetErrorText(err
));
276 static void pa_stop_playback(ALCdevice
*device
)
278 pa_data
*data
= (pa_data
*)device
->ExtraData
;
281 err
= Pa_StopStream(data
->stream
);
283 ERR("Error stopping stream: %s\n", Pa_GetErrorText(err
));
287 static ALCenum
pa_open_capture(ALCdevice
*device
, const ALCchar
*deviceName
)
289 PaStreamParameters inParams
;
295 deviceName
= pa_device
;
296 else if(strcmp(deviceName
, pa_device
) != 0)
297 return ALC_INVALID_VALUE
;
299 data
= (pa_data
*)calloc(1, sizeof(pa_data
));
301 return ALC_OUT_OF_MEMORY
;
303 frame_size
= FrameSizeFromDevFmt(device
->FmtChans
, device
->FmtType
);
304 data
->ring
= CreateRingBuffer(frame_size
, device
->UpdateSize
*device
->NumUpdates
);
305 if(data
->ring
== NULL
)
308 inParams
.device
= -1;
309 if(!ConfigValueInt("port", "capture", &inParams
.device
) || inParams
.device
< 0)
310 inParams
.device
= Pa_GetDefaultOutputDevice();
311 inParams
.suggestedLatency
= 0.0f
;
312 inParams
.hostApiSpecificStreamInfo
= NULL
;
314 switch(device
->FmtType
)
317 inParams
.sampleFormat
= paInt8
;
320 inParams
.sampleFormat
= paUInt8
;
323 inParams
.sampleFormat
= paInt16
;
326 inParams
.sampleFormat
= paInt32
;
329 inParams
.sampleFormat
= paFloat32
;
333 ERR("%s samples not supported\n", DevFmtTypeString(device
->FmtType
));
336 inParams
.channelCount
= ChannelsFromDevFmt(device
->FmtChans
);
338 err
= Pa_OpenStream(&data
->stream
, &inParams
, NULL
, device
->Frequency
,
339 paFramesPerBufferUnspecified
, paNoFlag
, pa_capture_cb
, device
);
342 ERR("Pa_OpenStream() returned an error: %s\n", Pa_GetErrorText(err
));
346 device
->szDeviceName
= strdup(deviceName
);
348 device
->ExtraData
= data
;
352 DestroyRingBuffer(data
->ring
);
354 return ALC_INVALID_VALUE
;
357 static void pa_close_capture(ALCdevice
*device
)
359 pa_data
*data
= (pa_data
*)device
->ExtraData
;
362 err
= Pa_CloseStream(data
->stream
);
364 ERR("Error closing stream: %s\n", Pa_GetErrorText(err
));
367 device
->ExtraData
= NULL
;
370 static void pa_start_capture(ALCdevice
*device
)
372 pa_data
*data
= device
->ExtraData
;
375 err
= Pa_StartStream(data
->stream
);
377 ERR("Error starting stream: %s\n", Pa_GetErrorText(err
));
380 static void pa_stop_capture(ALCdevice
*device
)
382 pa_data
*data
= (pa_data
*)device
->ExtraData
;
385 err
= Pa_StopStream(data
->stream
);
387 ERR("Error stopping stream: %s\n", Pa_GetErrorText(err
));
390 static ALCenum
pa_capture_samples(ALCdevice
*device
, ALCvoid
*buffer
, ALCuint samples
)
392 pa_data
*data
= device
->ExtraData
;
393 ReadRingBuffer(data
->ring
, buffer
, samples
);
397 static ALCuint
pa_available_samples(ALCdevice
*device
)
399 pa_data
*data
= device
->ExtraData
;
400 return RingBufferSize(data
->ring
);
404 static const BackendFuncs pa_funcs
= {
417 ALCboolean
alc_pa_init(BackendFuncs
*func_list
)
421 *func_list
= pa_funcs
;
425 void alc_pa_deinit(void)
437 void alc_pa_probe(enum DevProbe type
)
441 case ALL_DEVICE_PROBE
:
442 AppendAllDeviceList(pa_device
);
444 case CAPTURE_DEVICE_PROBE
:
445 AppendCaptureDeviceList(pa_device
);