portaudio.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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.
  8. *
  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.
  13. *
  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
  19. */
  20. #include "config.h"
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include "alMain.h"
  25. #include "alu.h"
  26. #include "compat.h"
  27. #include <portaudio.h>
  28. static const ALCchar pa_device[] = "PortAudio Default";
  29. #ifdef HAVE_DYNLOAD
  30. static void *pa_handle;
  31. #define MAKE_FUNC(x) static __typeof(x) * p##x
  32. MAKE_FUNC(Pa_Initialize);
  33. MAKE_FUNC(Pa_Terminate);
  34. MAKE_FUNC(Pa_GetErrorText);
  35. MAKE_FUNC(Pa_StartStream);
  36. MAKE_FUNC(Pa_StopStream);
  37. MAKE_FUNC(Pa_OpenStream);
  38. MAKE_FUNC(Pa_CloseStream);
  39. MAKE_FUNC(Pa_GetDefaultOutputDevice);
  40. MAKE_FUNC(Pa_GetStreamInfo);
  41. #undef MAKE_FUNC
  42. #define Pa_Initialize pPa_Initialize
  43. #define Pa_Terminate pPa_Terminate
  44. #define Pa_GetErrorText pPa_GetErrorText
  45. #define Pa_StartStream pPa_StartStream
  46. #define Pa_StopStream pPa_StopStream
  47. #define Pa_OpenStream pPa_OpenStream
  48. #define Pa_CloseStream pPa_CloseStream
  49. #define Pa_GetDefaultOutputDevice pPa_GetDefaultOutputDevice
  50. #define Pa_GetStreamInfo pPa_GetStreamInfo
  51. #endif
  52. static ALCboolean pa_load(void)
  53. {
  54. PaError err;
  55. #ifdef HAVE_DYNLOAD
  56. if(!pa_handle)
  57. {
  58. #ifdef _WIN32
  59. # define PALIB "portaudio.dll"
  60. #elif defined(__APPLE__) && defined(__MACH__)
  61. # define PALIB "libportaudio.2.dylib"
  62. #elif defined(__OpenBSD__)
  63. # define PALIB "libportaudio.so"
  64. #else
  65. # define PALIB "libportaudio.so.2"
  66. #endif
  67. pa_handle = LoadLib(PALIB);
  68. if(!pa_handle)
  69. return ALC_FALSE;
  70. #define LOAD_FUNC(f) do { \
  71. p##f = GetSymbol(pa_handle, #f); \
  72. if(p##f == NULL) \
  73. { \
  74. CloseLib(pa_handle); \
  75. pa_handle = NULL; \
  76. return ALC_FALSE; \
  77. } \
  78. } while(0)
  79. LOAD_FUNC(Pa_Initialize);
  80. LOAD_FUNC(Pa_Terminate);
  81. LOAD_FUNC(Pa_GetErrorText);
  82. LOAD_FUNC(Pa_StartStream);
  83. LOAD_FUNC(Pa_StopStream);
  84. LOAD_FUNC(Pa_OpenStream);
  85. LOAD_FUNC(Pa_CloseStream);
  86. LOAD_FUNC(Pa_GetDefaultOutputDevice);
  87. LOAD_FUNC(Pa_GetStreamInfo);
  88. #undef LOAD_FUNC
  89. if((err=Pa_Initialize()) != paNoError)
  90. {
  91. ERR("Pa_Initialize() returned an error: %s\n", Pa_GetErrorText(err));
  92. CloseLib(pa_handle);
  93. pa_handle = NULL;
  94. return ALC_FALSE;
  95. }
  96. }
  97. #else
  98. if((err=Pa_Initialize()) != paNoError)
  99. {
  100. ERR("Pa_Initialize() returned an error: %s\n", Pa_GetErrorText(err));
  101. return ALC_FALSE;
  102. }
  103. #endif
  104. return ALC_TRUE;
  105. }
  106. typedef struct {
  107. PaStream *stream;
  108. PaStreamParameters params;
  109. ALuint update_size;
  110. RingBuffer *ring;
  111. } pa_data;
  112. static int pa_callback(const void *UNUSED(inputBuffer), void *outputBuffer,
  113. unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *UNUSED(timeInfo),
  114. const PaStreamCallbackFlags UNUSED(statusFlags), void *userData)
  115. {
  116. ALCdevice *device = (ALCdevice*)userData;
  117. aluMixData(device, outputBuffer, framesPerBuffer);
  118. return 0;
  119. }
  120. static int pa_capture_cb(const void *inputBuffer, void *UNUSED(outputBuffer),
  121. unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *UNUSED(timeInfo),
  122. const PaStreamCallbackFlags UNUSED(statusFlags), void *userData)
  123. {
  124. ALCdevice *device = (ALCdevice*)userData;
  125. pa_data *data = (pa_data*)device->ExtraData;
  126. WriteRingBuffer(data->ring, inputBuffer, framesPerBuffer);
  127. return 0;
  128. }
  129. static ALCenum pa_open_playback(ALCdevice *device, const ALCchar *deviceName)
  130. {
  131. pa_data *data;
  132. PaError err;
  133. if(!deviceName)
  134. deviceName = pa_device;
  135. else if(strcmp(deviceName, pa_device) != 0)
  136. return ALC_INVALID_VALUE;
  137. data = (pa_data*)calloc(1, sizeof(pa_data));
  138. data->update_size = device->UpdateSize;
  139. data->params.device = -1;
  140. if(!ConfigValueInt("port", "device", &data->params.device) ||
  141. data->params.device < 0)
  142. data->params.device = Pa_GetDefaultOutputDevice();
  143. data->params.suggestedLatency = (device->UpdateSize*device->NumUpdates) /
  144. (float)device->Frequency;
  145. data->params.hostApiSpecificStreamInfo = NULL;
  146. data->params.channelCount = ((device->FmtChans == DevFmtMono) ? 1 : 2);
  147. switch(device->FmtType)
  148. {
  149. case DevFmtByte:
  150. data->params.sampleFormat = paInt8;
  151. break;
  152. case DevFmtUByte:
  153. data->params.sampleFormat = paUInt8;
  154. break;
  155. case DevFmtUShort:
  156. /* fall-through */
  157. case DevFmtShort:
  158. data->params.sampleFormat = paInt16;
  159. break;
  160. case DevFmtUInt:
  161. /* fall-through */
  162. case DevFmtInt:
  163. data->params.sampleFormat = paInt32;
  164. break;
  165. case DevFmtFloat:
  166. data->params.sampleFormat = paFloat32;
  167. break;
  168. }
  169. retry_open:
  170. err = Pa_OpenStream(&data->stream, NULL, &data->params, device->Frequency,
  171. device->UpdateSize, paNoFlag, pa_callback, device);
  172. if(err != paNoError)
  173. {
  174. if(data->params.sampleFormat == paFloat32)
  175. {
  176. data->params.sampleFormat = paInt16;
  177. goto retry_open;
  178. }
  179. ERR("Pa_OpenStream() returned an error: %s\n", Pa_GetErrorText(err));
  180. free(data);
  181. return ALC_INVALID_VALUE;
  182. }
  183. device->ExtraData = data;
  184. device->DeviceName = strdup(deviceName);
  185. return ALC_NO_ERROR;
  186. }
  187. static void pa_close_playback(ALCdevice *device)
  188. {
  189. pa_data *data = (pa_data*)device->ExtraData;
  190. PaError err;
  191. err = Pa_CloseStream(data->stream);
  192. if(err != paNoError)
  193. ERR("Error closing stream: %s\n", Pa_GetErrorText(err));
  194. free(data);
  195. device->ExtraData = NULL;
  196. }
  197. static ALCboolean pa_reset_playback(ALCdevice *device)
  198. {
  199. pa_data *data = (pa_data*)device->ExtraData;
  200. const PaStreamInfo *streamInfo;
  201. streamInfo = Pa_GetStreamInfo(data->stream);
  202. device->Frequency = streamInfo->sampleRate;
  203. device->UpdateSize = data->update_size;
  204. if(data->params.sampleFormat == paInt8)
  205. device->FmtType = DevFmtByte;
  206. else if(data->params.sampleFormat == paUInt8)
  207. device->FmtType = DevFmtUByte;
  208. else if(data->params.sampleFormat == paInt16)
  209. device->FmtType = DevFmtShort;
  210. else if(data->params.sampleFormat == paInt32)
  211. device->FmtType = DevFmtInt;
  212. else if(data->params.sampleFormat == paFloat32)
  213. device->FmtType = DevFmtFloat;
  214. else
  215. {
  216. ERR("Unexpected sample format: 0x%lx\n", data->params.sampleFormat);
  217. return ALC_FALSE;
  218. }
  219. if(data->params.channelCount == 2)
  220. device->FmtChans = DevFmtStereo;
  221. else if(data->params.channelCount == 1)
  222. device->FmtChans = DevFmtMono;
  223. else
  224. {
  225. ERR("Unexpected channel count: %u\n", data->params.channelCount);
  226. return ALC_FALSE;
  227. }
  228. SetDefaultChannelOrder(device);
  229. return ALC_TRUE;
  230. }
  231. static ALCboolean pa_start_playback(ALCdevice *device)
  232. {
  233. pa_data *data = (pa_data*)device->ExtraData;
  234. PaError err;
  235. err = Pa_StartStream(data->stream);
  236. if(err != paNoError)
  237. {
  238. ERR("Pa_StartStream() returned an error: %s\n", Pa_GetErrorText(err));
  239. return ALC_FALSE;
  240. }
  241. return ALC_TRUE;
  242. }
  243. static void pa_stop_playback(ALCdevice *device)
  244. {
  245. pa_data *data = (pa_data*)device->ExtraData;
  246. PaError err;
  247. err = Pa_StopStream(data->stream);
  248. if(err != paNoError)
  249. ERR("Error stopping stream: %s\n", Pa_GetErrorText(err));
  250. }
  251. static ALCenum pa_open_capture(ALCdevice *device, const ALCchar *deviceName)
  252. {
  253. ALuint frame_size;
  254. pa_data *data;
  255. PaError err;
  256. if(!deviceName)
  257. deviceName = pa_device;
  258. else if(strcmp(deviceName, pa_device) != 0)
  259. return ALC_INVALID_VALUE;
  260. data = (pa_data*)calloc(1, sizeof(pa_data));
  261. if(data == NULL)
  262. return ALC_OUT_OF_MEMORY;
  263. frame_size = FrameSizeFromDevFmt(device->FmtChans, device->FmtType);
  264. data->ring = CreateRingBuffer(frame_size, device->UpdateSize*device->NumUpdates);
  265. if(data->ring == NULL)
  266. goto error;
  267. data->params.device = -1;
  268. if(!ConfigValueInt("port", "capture", &data->params.device) ||
  269. data->params.device < 0)
  270. data->params.device = Pa_GetDefaultOutputDevice();
  271. data->params.suggestedLatency = 0.0f;
  272. data->params.hostApiSpecificStreamInfo = NULL;
  273. switch(device->FmtType)
  274. {
  275. case DevFmtByte:
  276. data->params.sampleFormat = paInt8;
  277. break;
  278. case DevFmtUByte:
  279. data->params.sampleFormat = paUInt8;
  280. break;
  281. case DevFmtShort:
  282. data->params.sampleFormat = paInt16;
  283. break;
  284. case DevFmtInt:
  285. data->params.sampleFormat = paInt32;
  286. break;
  287. case DevFmtFloat:
  288. data->params.sampleFormat = paFloat32;
  289. break;
  290. case DevFmtUInt:
  291. case DevFmtUShort:
  292. ERR("%s samples not supported\n", DevFmtTypeString(device->FmtType));
  293. goto error;
  294. }
  295. data->params.channelCount = ChannelsFromDevFmt(device->FmtChans);
  296. err = Pa_OpenStream(&data->stream, &data->params, NULL, device->Frequency,
  297. paFramesPerBufferUnspecified, paNoFlag, pa_capture_cb, device);
  298. if(err != paNoError)
  299. {
  300. ERR("Pa_OpenStream() returned an error: %s\n", Pa_GetErrorText(err));
  301. goto error;
  302. }
  303. device->DeviceName = strdup(deviceName);
  304. device->ExtraData = data;
  305. return ALC_NO_ERROR;
  306. error:
  307. DestroyRingBuffer(data->ring);
  308. free(data);
  309. return ALC_INVALID_VALUE;
  310. }
  311. static void pa_close_capture(ALCdevice *device)
  312. {
  313. pa_data *data = (pa_data*)device->ExtraData;
  314. PaError err;
  315. err = Pa_CloseStream(data->stream);
  316. if(err != paNoError)
  317. ERR("Error closing stream: %s\n", Pa_GetErrorText(err));
  318. DestroyRingBuffer(data->ring);
  319. data->ring = NULL;
  320. free(data);
  321. device->ExtraData = NULL;
  322. }
  323. static void pa_start_capture(ALCdevice *device)
  324. {
  325. pa_data *data = device->ExtraData;
  326. PaError err;
  327. err = Pa_StartStream(data->stream);
  328. if(err != paNoError)
  329. ERR("Error starting stream: %s\n", Pa_GetErrorText(err));
  330. }
  331. static void pa_stop_capture(ALCdevice *device)
  332. {
  333. pa_data *data = (pa_data*)device->ExtraData;
  334. PaError err;
  335. err = Pa_StopStream(data->stream);
  336. if(err != paNoError)
  337. ERR("Error stopping stream: %s\n", Pa_GetErrorText(err));
  338. }
  339. static ALCenum pa_capture_samples(ALCdevice *device, ALCvoid *buffer, ALCuint samples)
  340. {
  341. pa_data *data = device->ExtraData;
  342. ReadRingBuffer(data->ring, buffer, samples);
  343. return ALC_NO_ERROR;
  344. }
  345. static ALCuint pa_available_samples(ALCdevice *device)
  346. {
  347. pa_data *data = device->ExtraData;
  348. return RingBufferSize(data->ring);
  349. }
  350. static const BackendFuncs pa_funcs = {
  351. pa_open_playback,
  352. pa_close_playback,
  353. pa_reset_playback,
  354. pa_start_playback,
  355. pa_stop_playback,
  356. pa_open_capture,
  357. pa_close_capture,
  358. pa_start_capture,
  359. pa_stop_capture,
  360. pa_capture_samples,
  361. pa_available_samples,
  362. ALCdevice_GetLatencyDefault
  363. };
  364. ALCboolean alc_pa_init(BackendFuncs *func_list)
  365. {
  366. if(!pa_load())
  367. return ALC_FALSE;
  368. *func_list = pa_funcs;
  369. return ALC_TRUE;
  370. }
  371. void alc_pa_deinit(void)
  372. {
  373. #ifdef HAVE_DYNLOAD
  374. if(pa_handle)
  375. {
  376. Pa_Terminate();
  377. CloseLib(pa_handle);
  378. pa_handle = NULL;
  379. }
  380. #else
  381. Pa_Terminate();
  382. #endif
  383. }
  384. void alc_pa_probe(enum DevProbe type)
  385. {
  386. switch(type)
  387. {
  388. case ALL_DEVICE_PROBE:
  389. AppendAllDevicesList(pa_device);
  390. break;
  391. case CAPTURE_DEVICE_PROBE:
  392. AppendCaptureDeviceList(pa_device);
  393. break;
  394. }
  395. }