portaudio.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 "backends/base.h"
  28. #include <portaudio.h>
  29. static const ALCchar pa_device[] = "PortAudio Default";
  30. #ifdef HAVE_DYNLOAD
  31. static void *pa_handle;
  32. #define MAKE_FUNC(x) static __typeof(x) * p##x
  33. MAKE_FUNC(Pa_Initialize);
  34. MAKE_FUNC(Pa_Terminate);
  35. MAKE_FUNC(Pa_GetErrorText);
  36. MAKE_FUNC(Pa_StartStream);
  37. MAKE_FUNC(Pa_StopStream);
  38. MAKE_FUNC(Pa_OpenStream);
  39. MAKE_FUNC(Pa_CloseStream);
  40. MAKE_FUNC(Pa_GetDefaultOutputDevice);
  41. MAKE_FUNC(Pa_GetDefaultInputDevice);
  42. MAKE_FUNC(Pa_GetStreamInfo);
  43. #undef MAKE_FUNC
  44. #define Pa_Initialize pPa_Initialize
  45. #define Pa_Terminate pPa_Terminate
  46. #define Pa_GetErrorText pPa_GetErrorText
  47. #define Pa_StartStream pPa_StartStream
  48. #define Pa_StopStream pPa_StopStream
  49. #define Pa_OpenStream pPa_OpenStream
  50. #define Pa_CloseStream pPa_CloseStream
  51. #define Pa_GetDefaultOutputDevice pPa_GetDefaultOutputDevice
  52. #define Pa_GetDefaultInputDevice pPa_GetDefaultInputDevice
  53. #define Pa_GetStreamInfo pPa_GetStreamInfo
  54. #endif
  55. static ALCboolean pa_load(void)
  56. {
  57. PaError err;
  58. #ifdef HAVE_DYNLOAD
  59. if(!pa_handle)
  60. {
  61. #ifdef _WIN32
  62. # define PALIB "portaudio.dll"
  63. #elif defined(__APPLE__) && defined(__MACH__)
  64. # define PALIB "libportaudio.2.dylib"
  65. #elif defined(__OpenBSD__)
  66. # define PALIB "libportaudio.so"
  67. #else
  68. # define PALIB "libportaudio.so.2"
  69. #endif
  70. pa_handle = LoadLib(PALIB);
  71. if(!pa_handle)
  72. return ALC_FALSE;
  73. #define LOAD_FUNC(f) do { \
  74. p##f = GetSymbol(pa_handle, #f); \
  75. if(p##f == NULL) \
  76. { \
  77. CloseLib(pa_handle); \
  78. pa_handle = NULL; \
  79. return ALC_FALSE; \
  80. } \
  81. } while(0)
  82. LOAD_FUNC(Pa_Initialize);
  83. LOAD_FUNC(Pa_Terminate);
  84. LOAD_FUNC(Pa_GetErrorText);
  85. LOAD_FUNC(Pa_StartStream);
  86. LOAD_FUNC(Pa_StopStream);
  87. LOAD_FUNC(Pa_OpenStream);
  88. LOAD_FUNC(Pa_CloseStream);
  89. LOAD_FUNC(Pa_GetDefaultOutputDevice);
  90. LOAD_FUNC(Pa_GetDefaultInputDevice);
  91. LOAD_FUNC(Pa_GetStreamInfo);
  92. #undef LOAD_FUNC
  93. if((err=Pa_Initialize()) != paNoError)
  94. {
  95. ERR("Pa_Initialize() returned an error: %s\n", Pa_GetErrorText(err));
  96. CloseLib(pa_handle);
  97. pa_handle = NULL;
  98. return ALC_FALSE;
  99. }
  100. }
  101. #else
  102. if((err=Pa_Initialize()) != paNoError)
  103. {
  104. ERR("Pa_Initialize() returned an error: %s\n", Pa_GetErrorText(err));
  105. return ALC_FALSE;
  106. }
  107. #endif
  108. return ALC_TRUE;
  109. }
  110. typedef struct ALCportPlayback {
  111. DERIVE_FROM_TYPE(ALCbackend);
  112. PaStream *stream;
  113. PaStreamParameters params;
  114. ALuint update_size;
  115. } ALCportPlayback;
  116. static int ALCportPlayback_WriteCallback(const void *inputBuffer, void *outputBuffer,
  117. unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *timeInfo,
  118. const PaStreamCallbackFlags statusFlags, void *userData);
  119. static void ALCportPlayback_Construct(ALCportPlayback *self, ALCdevice *device);
  120. static void ALCportPlayback_Destruct(ALCportPlayback *self);
  121. static ALCenum ALCportPlayback_open(ALCportPlayback *self, const ALCchar *name);
  122. static void ALCportPlayback_close(ALCportPlayback *self);
  123. static ALCboolean ALCportPlayback_reset(ALCportPlayback *self);
  124. static ALCboolean ALCportPlayback_start(ALCportPlayback *self);
  125. static void ALCportPlayback_stop(ALCportPlayback *self);
  126. static DECLARE_FORWARD2(ALCportPlayback, ALCbackend, ALCenum, captureSamples, ALCvoid*, ALCuint)
  127. static DECLARE_FORWARD(ALCportPlayback, ALCbackend, ALCuint, availableSamples)
  128. static DECLARE_FORWARD(ALCportPlayback, ALCbackend, ClockLatency, getClockLatency)
  129. static DECLARE_FORWARD(ALCportPlayback, ALCbackend, void, lock)
  130. static DECLARE_FORWARD(ALCportPlayback, ALCbackend, void, unlock)
  131. DECLARE_DEFAULT_ALLOCATORS(ALCportPlayback)
  132. DEFINE_ALCBACKEND_VTABLE(ALCportPlayback);
  133. static void ALCportPlayback_Construct(ALCportPlayback *self, ALCdevice *device)
  134. {
  135. ALCbackend_Construct(STATIC_CAST(ALCbackend, self), device);
  136. SET_VTABLE2(ALCportPlayback, ALCbackend, self);
  137. self->stream = NULL;
  138. }
  139. static void ALCportPlayback_Destruct(ALCportPlayback *self)
  140. {
  141. if(self->stream)
  142. Pa_CloseStream(self->stream);
  143. self->stream = NULL;
  144. ALCbackend_Destruct(STATIC_CAST(ALCbackend, self));
  145. }
  146. static int ALCportPlayback_WriteCallback(const void *UNUSED(inputBuffer), void *outputBuffer,
  147. unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *UNUSED(timeInfo),
  148. const PaStreamCallbackFlags UNUSED(statusFlags), void *userData)
  149. {
  150. ALCportPlayback *self = userData;
  151. ALCportPlayback_lock(self);
  152. aluMixData(STATIC_CAST(ALCbackend, self)->mDevice, outputBuffer, framesPerBuffer);
  153. ALCportPlayback_unlock(self);
  154. return 0;
  155. }
  156. static ALCenum ALCportPlayback_open(ALCportPlayback *self, const ALCchar *name)
  157. {
  158. ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
  159. PaError err;
  160. if(!name)
  161. name = pa_device;
  162. else if(strcmp(name, pa_device) != 0)
  163. return ALC_INVALID_VALUE;
  164. self->update_size = device->UpdateSize;
  165. self->params.device = -1;
  166. if(!ConfigValueInt(NULL, "port", "device", &self->params.device) ||
  167. self->params.device < 0)
  168. self->params.device = Pa_GetDefaultOutputDevice();
  169. self->params.suggestedLatency = (device->UpdateSize*device->NumUpdates) /
  170. (float)device->Frequency;
  171. self->params.hostApiSpecificStreamInfo = NULL;
  172. self->params.channelCount = ((device->FmtChans == DevFmtMono) ? 1 : 2);
  173. switch(device->FmtType)
  174. {
  175. case DevFmtByte:
  176. self->params.sampleFormat = paInt8;
  177. break;
  178. case DevFmtUByte:
  179. self->params.sampleFormat = paUInt8;
  180. break;
  181. case DevFmtUShort:
  182. /* fall-through */
  183. case DevFmtShort:
  184. self->params.sampleFormat = paInt16;
  185. break;
  186. case DevFmtUInt:
  187. /* fall-through */
  188. case DevFmtInt:
  189. self->params.sampleFormat = paInt32;
  190. break;
  191. case DevFmtFloat:
  192. self->params.sampleFormat = paFloat32;
  193. break;
  194. }
  195. retry_open:
  196. err = Pa_OpenStream(&self->stream, NULL, &self->params,
  197. device->Frequency, device->UpdateSize, paNoFlag,
  198. ALCportPlayback_WriteCallback, self
  199. );
  200. if(err != paNoError)
  201. {
  202. if(self->params.sampleFormat == paFloat32)
  203. {
  204. self->params.sampleFormat = paInt16;
  205. goto retry_open;
  206. }
  207. ERR("Pa_OpenStream() returned an error: %s\n", Pa_GetErrorText(err));
  208. return ALC_INVALID_VALUE;
  209. }
  210. alstr_copy_cstr(&device->DeviceName, name);
  211. return ALC_NO_ERROR;
  212. }
  213. static void ALCportPlayback_close(ALCportPlayback *self)
  214. {
  215. PaError err = Pa_CloseStream(self->stream);
  216. if(err != paNoError)
  217. ERR("Error closing stream: %s\n", Pa_GetErrorText(err));
  218. self->stream = NULL;
  219. }
  220. static ALCboolean ALCportPlayback_reset(ALCportPlayback *self)
  221. {
  222. ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
  223. const PaStreamInfo *streamInfo;
  224. streamInfo = Pa_GetStreamInfo(self->stream);
  225. device->Frequency = streamInfo->sampleRate;
  226. device->UpdateSize = self->update_size;
  227. if(self->params.sampleFormat == paInt8)
  228. device->FmtType = DevFmtByte;
  229. else if(self->params.sampleFormat == paUInt8)
  230. device->FmtType = DevFmtUByte;
  231. else if(self->params.sampleFormat == paInt16)
  232. device->FmtType = DevFmtShort;
  233. else if(self->params.sampleFormat == paInt32)
  234. device->FmtType = DevFmtInt;
  235. else if(self->params.sampleFormat == paFloat32)
  236. device->FmtType = DevFmtFloat;
  237. else
  238. {
  239. ERR("Unexpected sample format: 0x%lx\n", self->params.sampleFormat);
  240. return ALC_FALSE;
  241. }
  242. if(self->params.channelCount == 2)
  243. device->FmtChans = DevFmtStereo;
  244. else if(self->params.channelCount == 1)
  245. device->FmtChans = DevFmtMono;
  246. else
  247. {
  248. ERR("Unexpected channel count: %u\n", self->params.channelCount);
  249. return ALC_FALSE;
  250. }
  251. SetDefaultChannelOrder(device);
  252. return ALC_TRUE;
  253. }
  254. static ALCboolean ALCportPlayback_start(ALCportPlayback *self)
  255. {
  256. PaError err;
  257. err = Pa_StartStream(self->stream);
  258. if(err != paNoError)
  259. {
  260. ERR("Pa_StartStream() returned an error: %s\n", Pa_GetErrorText(err));
  261. return ALC_FALSE;
  262. }
  263. return ALC_TRUE;
  264. }
  265. static void ALCportPlayback_stop(ALCportPlayback *self)
  266. {
  267. PaError err = Pa_StopStream(self->stream);
  268. if(err != paNoError)
  269. ERR("Error stopping stream: %s\n", Pa_GetErrorText(err));
  270. }
  271. typedef struct ALCportCapture {
  272. DERIVE_FROM_TYPE(ALCbackend);
  273. PaStream *stream;
  274. PaStreamParameters params;
  275. ll_ringbuffer_t *ring;
  276. } ALCportCapture;
  277. static int ALCportCapture_ReadCallback(const void *inputBuffer, void *outputBuffer,
  278. unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *timeInfo,
  279. const PaStreamCallbackFlags statusFlags, void *userData);
  280. static void ALCportCapture_Construct(ALCportCapture *self, ALCdevice *device);
  281. static void ALCportCapture_Destruct(ALCportCapture *self);
  282. static ALCenum ALCportCapture_open(ALCportCapture *self, const ALCchar *name);
  283. static void ALCportCapture_close(ALCportCapture *self);
  284. static DECLARE_FORWARD(ALCportCapture, ALCbackend, ALCboolean, reset)
  285. static ALCboolean ALCportCapture_start(ALCportCapture *self);
  286. static void ALCportCapture_stop(ALCportCapture *self);
  287. static ALCenum ALCportCapture_captureSamples(ALCportCapture *self, ALCvoid *buffer, ALCuint samples);
  288. static ALCuint ALCportCapture_availableSamples(ALCportCapture *self);
  289. static DECLARE_FORWARD(ALCportCapture, ALCbackend, ClockLatency, getClockLatency)
  290. static DECLARE_FORWARD(ALCportCapture, ALCbackend, void, lock)
  291. static DECLARE_FORWARD(ALCportCapture, ALCbackend, void, unlock)
  292. DECLARE_DEFAULT_ALLOCATORS(ALCportCapture)
  293. DEFINE_ALCBACKEND_VTABLE(ALCportCapture);
  294. static void ALCportCapture_Construct(ALCportCapture *self, ALCdevice *device)
  295. {
  296. ALCbackend_Construct(STATIC_CAST(ALCbackend, self), device);
  297. SET_VTABLE2(ALCportCapture, ALCbackend, self);
  298. self->stream = NULL;
  299. }
  300. static void ALCportCapture_Destruct(ALCportCapture *self)
  301. {
  302. if(self->stream)
  303. Pa_CloseStream(self->stream);
  304. self->stream = NULL;
  305. if(self->ring)
  306. ll_ringbuffer_free(self->ring);
  307. self->ring = NULL;
  308. ALCbackend_Destruct(STATIC_CAST(ALCbackend, self));
  309. }
  310. static int ALCportCapture_ReadCallback(const void *inputBuffer, void *UNUSED(outputBuffer),
  311. unsigned long framesPerBuffer, const PaStreamCallbackTimeInfo *UNUSED(timeInfo),
  312. const PaStreamCallbackFlags UNUSED(statusFlags), void *userData)
  313. {
  314. ALCportCapture *self = userData;
  315. size_t writable = ll_ringbuffer_write_space(self->ring);
  316. if(framesPerBuffer > writable)
  317. framesPerBuffer = writable;
  318. ll_ringbuffer_write(self->ring, inputBuffer, framesPerBuffer);
  319. return 0;
  320. }
  321. static ALCenum ALCportCapture_open(ALCportCapture *self, const ALCchar *name)
  322. {
  323. ALCdevice *device = STATIC_CAST(ALCbackend, self)->mDevice;
  324. ALuint samples, frame_size;
  325. PaError err;
  326. if(!name)
  327. name = pa_device;
  328. else if(strcmp(name, pa_device) != 0)
  329. return ALC_INVALID_VALUE;
  330. samples = device->UpdateSize * device->NumUpdates;
  331. samples = maxu(samples, 100 * device->Frequency / 1000);
  332. frame_size = FrameSizeFromDevFmt(device->FmtChans, device->FmtType, device->AmbiOrder);
  333. self->ring = ll_ringbuffer_create(samples, frame_size);
  334. if(self->ring == NULL) return ALC_INVALID_VALUE;
  335. self->params.device = -1;
  336. if(!ConfigValueInt(NULL, "port", "capture", &self->params.device) ||
  337. self->params.device < 0)
  338. self->params.device = Pa_GetDefaultInputDevice();
  339. self->params.suggestedLatency = 0.0f;
  340. self->params.hostApiSpecificStreamInfo = NULL;
  341. switch(device->FmtType)
  342. {
  343. case DevFmtByte:
  344. self->params.sampleFormat = paInt8;
  345. break;
  346. case DevFmtUByte:
  347. self->params.sampleFormat = paUInt8;
  348. break;
  349. case DevFmtShort:
  350. self->params.sampleFormat = paInt16;
  351. break;
  352. case DevFmtInt:
  353. self->params.sampleFormat = paInt32;
  354. break;
  355. case DevFmtFloat:
  356. self->params.sampleFormat = paFloat32;
  357. break;
  358. case DevFmtUInt:
  359. case DevFmtUShort:
  360. ERR("%s samples not supported\n", DevFmtTypeString(device->FmtType));
  361. return ALC_INVALID_VALUE;
  362. }
  363. self->params.channelCount = ChannelsFromDevFmt(device->FmtChans, device->AmbiOrder);
  364. err = Pa_OpenStream(&self->stream, &self->params, NULL,
  365. device->Frequency, paFramesPerBufferUnspecified, paNoFlag,
  366. ALCportCapture_ReadCallback, self
  367. );
  368. if(err != paNoError)
  369. {
  370. ERR("Pa_OpenStream() returned an error: %s\n", Pa_GetErrorText(err));
  371. return ALC_INVALID_VALUE;
  372. }
  373. alstr_copy_cstr(&device->DeviceName, name);
  374. return ALC_NO_ERROR;
  375. }
  376. static void ALCportCapture_close(ALCportCapture *self)
  377. {
  378. PaError err = Pa_CloseStream(self->stream);
  379. if(err != paNoError)
  380. ERR("Error closing stream: %s\n", Pa_GetErrorText(err));
  381. self->stream = NULL;
  382. ll_ringbuffer_free(self->ring);
  383. self->ring = NULL;
  384. }
  385. static ALCboolean ALCportCapture_start(ALCportCapture *self)
  386. {
  387. PaError err = Pa_StartStream(self->stream);
  388. if(err != paNoError)
  389. {
  390. ERR("Error starting stream: %s\n", Pa_GetErrorText(err));
  391. return ALC_FALSE;
  392. }
  393. return ALC_TRUE;
  394. }
  395. static void ALCportCapture_stop(ALCportCapture *self)
  396. {
  397. PaError err = Pa_StopStream(self->stream);
  398. if(err != paNoError)
  399. ERR("Error stopping stream: %s\n", Pa_GetErrorText(err));
  400. }
  401. static ALCuint ALCportCapture_availableSamples(ALCportCapture *self)
  402. {
  403. return ll_ringbuffer_read_space(self->ring);
  404. }
  405. static ALCenum ALCportCapture_captureSamples(ALCportCapture *self, ALCvoid *buffer, ALCuint samples)
  406. {
  407. ll_ringbuffer_read(self->ring, buffer, samples);
  408. return ALC_NO_ERROR;
  409. }
  410. typedef struct ALCportBackendFactory {
  411. DERIVE_FROM_TYPE(ALCbackendFactory);
  412. } ALCportBackendFactory;
  413. #define ALCPORTBACKENDFACTORY_INITIALIZER { { GET_VTABLE2(ALCportBackendFactory, ALCbackendFactory) } }
  414. static ALCboolean ALCportBackendFactory_init(ALCportBackendFactory *self);
  415. static void ALCportBackendFactory_deinit(ALCportBackendFactory *self);
  416. static ALCboolean ALCportBackendFactory_querySupport(ALCportBackendFactory *self, ALCbackend_Type type);
  417. static void ALCportBackendFactory_probe(ALCportBackendFactory *self, enum DevProbe type);
  418. static ALCbackend* ALCportBackendFactory_createBackend(ALCportBackendFactory *self, ALCdevice *device, ALCbackend_Type type);
  419. DEFINE_ALCBACKENDFACTORY_VTABLE(ALCportBackendFactory);
  420. static ALCboolean ALCportBackendFactory_init(ALCportBackendFactory* UNUSED(self))
  421. {
  422. if(!pa_load())
  423. return ALC_FALSE;
  424. return ALC_TRUE;
  425. }
  426. static void ALCportBackendFactory_deinit(ALCportBackendFactory* UNUSED(self))
  427. {
  428. #ifdef HAVE_DYNLOAD
  429. if(pa_handle)
  430. {
  431. Pa_Terminate();
  432. CloseLib(pa_handle);
  433. pa_handle = NULL;
  434. }
  435. #else
  436. Pa_Terminate();
  437. #endif
  438. }
  439. static ALCboolean ALCportBackendFactory_querySupport(ALCportBackendFactory* UNUSED(self), ALCbackend_Type type)
  440. {
  441. if(type == ALCbackend_Playback || type == ALCbackend_Capture)
  442. return ALC_TRUE;
  443. return ALC_FALSE;
  444. }
  445. static void ALCportBackendFactory_probe(ALCportBackendFactory* UNUSED(self), enum DevProbe type)
  446. {
  447. switch(type)
  448. {
  449. case ALL_DEVICE_PROBE:
  450. AppendAllDevicesList(pa_device);
  451. break;
  452. case CAPTURE_DEVICE_PROBE:
  453. AppendCaptureDeviceList(pa_device);
  454. break;
  455. }
  456. }
  457. static ALCbackend* ALCportBackendFactory_createBackend(ALCportBackendFactory* UNUSED(self), ALCdevice *device, ALCbackend_Type type)
  458. {
  459. if(type == ALCbackend_Playback)
  460. {
  461. ALCportPlayback *backend;
  462. NEW_OBJ(backend, ALCportPlayback)(device);
  463. if(!backend) return NULL;
  464. return STATIC_CAST(ALCbackend, backend);
  465. }
  466. if(type == ALCbackend_Capture)
  467. {
  468. ALCportCapture *backend;
  469. NEW_OBJ(backend, ALCportCapture)(device);
  470. if(!backend) return NULL;
  471. return STATIC_CAST(ALCbackend, backend);
  472. }
  473. return NULL;
  474. }
  475. ALCbackendFactory *ALCportBackendFactory_getFactory(void)
  476. {
  477. static ALCportBackendFactory factory = ALCPORTBACKENDFACTORY_INITIALIZER;
  478. return STATIC_CAST(ALCbackendFactory, &factory);
  479. }