portaudio.c 17 KB

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