PageRenderTime 64ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Telegram/_openal_patch/Alc/ALc.c

https://gitlab.com/mba811/tdesktop
C | 3771 lines | 3003 code | 525 blank | 243 comment | 514 complexity | cf4dede07cefe854417a5ff6574ec021 MD5 | raw file
Possible License(s): GPL-3.0
  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 <math.h>
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <memory.h>
  25. #include <ctype.h>
  26. #include <signal.h>
  27. #include "alMain.h"
  28. #include "alSource.h"
  29. #include "alListener.h"
  30. #include "alThunk.h"
  31. #include "alSource.h"
  32. #include "alBuffer.h"
  33. #include "alAuxEffectSlot.h"
  34. #include "alError.h"
  35. #include "alMidi.h"
  36. #include "bs2b.h"
  37. #include "alu.h"
  38. #include "compat.h"
  39. #include "threads.h"
  40. #include "alstring.h"
  41. #include "backends/base.h"
  42. #include "midi/base.h"
  43. /************************************************
  44. * Backends
  45. ************************************************/
  46. struct BackendInfo {
  47. const char *name;
  48. ALCbackendFactory* (*getFactory)(void);
  49. ALCboolean (*Init)(BackendFuncs*);
  50. void (*Deinit)(void);
  51. void (*Probe)(enum DevProbe);
  52. BackendFuncs Funcs;
  53. };
  54. #define EmptyFuncs { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
  55. static struct BackendInfo BackendList[] = {
  56. #ifdef HAVE_PULSEAUDIO
  57. { "pulse", ALCpulseBackendFactory_getFactory, NULL, NULL, NULL, EmptyFuncs },
  58. #endif
  59. #ifdef HAVE_ALSA
  60. { "alsa", ALCalsaBackendFactory_getFactory, NULL, NULL, NULL, EmptyFuncs },
  61. #endif
  62. #ifdef HAVE_COREAUDIO
  63. { "core", NULL, alc_ca_init, alc_ca_deinit, alc_ca_probe, EmptyFuncs },
  64. #endif
  65. #ifdef HAVE_OSS
  66. { "oss", ALCossBackendFactory_getFactory, NULL, NULL, NULL, EmptyFuncs },
  67. #endif
  68. #ifdef HAVE_SOLARIS
  69. { "solaris", NULL, alc_solaris_init, alc_solaris_deinit, alc_solaris_probe, EmptyFuncs },
  70. #endif
  71. #ifdef HAVE_SNDIO
  72. { "sndio", NULL, alc_sndio_init, alc_sndio_deinit, alc_sndio_probe, EmptyFuncs },
  73. #endif
  74. #ifdef HAVE_QSA
  75. { "qsa", NULL, alc_qsa_init, alc_qsa_deinit, alc_qsa_probe, EmptyFuncs },
  76. #endif
  77. #ifdef HAVE_MMDEVAPI
  78. { "mmdevapi", ALCmmdevBackendFactory_getFactory, NULL, NULL, NULL, EmptyFuncs },
  79. #endif
  80. #ifdef HAVE_DSOUND
  81. { "dsound", ALCdsoundBackendFactory_getFactory, NULL, NULL, NULL, EmptyFuncs },
  82. #endif
  83. #ifdef HAVE_WINMM
  84. { "winmm", NULL, alcWinMMInit, alcWinMMDeinit, alcWinMMProbe, EmptyFuncs },
  85. #endif
  86. #ifdef HAVE_PORTAUDIO
  87. { "port", NULL, alc_pa_init, alc_pa_deinit, alc_pa_probe, EmptyFuncs },
  88. #endif
  89. #ifdef HAVE_OPENSL
  90. { "opensl", NULL, alc_opensl_init, alc_opensl_deinit, alc_opensl_probe, EmptyFuncs },
  91. #endif
  92. { "null", ALCnullBackendFactory_getFactory, NULL, NULL, NULL, EmptyFuncs },
  93. #ifdef HAVE_WAVE
  94. { "wave", ALCwaveBackendFactory_getFactory, NULL, NULL, NULL, EmptyFuncs },
  95. #endif
  96. { NULL, NULL, NULL, NULL, NULL, EmptyFuncs }
  97. };
  98. #undef EmptyFuncs
  99. static struct BackendInfo PlaybackBackend;
  100. static struct BackendInfo CaptureBackend;
  101. static bool SuspendAndProcessSupported = false;
  102. /************************************************
  103. * Functions, enums, and errors
  104. ************************************************/
  105. typedef struct ALCfunction {
  106. const ALCchar *funcName;
  107. ALCvoid *address;
  108. } ALCfunction;
  109. typedef struct ALCenums {
  110. const ALCchar *enumName;
  111. ALCenum value;
  112. } ALCenums;
  113. #define DECL(x) { #x, (ALCvoid*)(x) }
  114. static const ALCfunction alcFunctions[] = {
  115. DECL(alcCreateContext),
  116. DECL(alcMakeContextCurrent),
  117. DECL(alcProcessContext),
  118. DECL(alcSuspendContext),
  119. DECL(alcDestroyContext),
  120. DECL(alcGetCurrentContext),
  121. DECL(alcGetContextsDevice),
  122. DECL(alcOpenDevice),
  123. DECL(alcCloseDevice),
  124. DECL(alcGetError),
  125. DECL(alcIsExtensionPresent),
  126. DECL(alcGetProcAddress),
  127. DECL(alcGetEnumValue),
  128. DECL(alcGetString),
  129. DECL(alcGetIntegerv),
  130. DECL(alcCaptureOpenDevice),
  131. DECL(alcCaptureCloseDevice),
  132. DECL(alcCaptureStart),
  133. DECL(alcCaptureStop),
  134. DECL(alcCaptureSamples),
  135. DECL(alcSetThreadContext),
  136. DECL(alcGetThreadContext),
  137. DECL(alcLoopbackOpenDeviceSOFT),
  138. DECL(alcIsRenderFormatSupportedSOFT),
  139. DECL(alcRenderSamplesSOFT),
  140. DECL(alcDevicePauseSOFT),
  141. DECL(alcDeviceResumeSOFT),
  142. DECL(alcGetInteger64vSOFT),
  143. DECL(alEnable),
  144. DECL(alDisable),
  145. DECL(alIsEnabled),
  146. DECL(alGetString),
  147. DECL(alGetBooleanv),
  148. DECL(alGetIntegerv),
  149. DECL(alGetFloatv),
  150. DECL(alGetDoublev),
  151. DECL(alGetBoolean),
  152. DECL(alGetInteger),
  153. DECL(alGetFloat),
  154. DECL(alGetDouble),
  155. DECL(alGetError),
  156. DECL(alIsExtensionPresent),
  157. DECL(alGetProcAddress),
  158. DECL(alGetEnumValue),
  159. DECL(alListenerf),
  160. DECL(alListener3f),
  161. DECL(alListenerfv),
  162. DECL(alListeneri),
  163. DECL(alListener3i),
  164. DECL(alListeneriv),
  165. DECL(alGetListenerf),
  166. DECL(alGetListener3f),
  167. DECL(alGetListenerfv),
  168. DECL(alGetListeneri),
  169. DECL(alGetListener3i),
  170. DECL(alGetListeneriv),
  171. DECL(alGenSources),
  172. DECL(alDeleteSources),
  173. DECL(alIsSource),
  174. DECL(alSourcef),
  175. DECL(alSource3f),
  176. DECL(alSourcefv),
  177. DECL(alSourcei),
  178. DECL(alSource3i),
  179. DECL(alSourceiv),
  180. DECL(alGetSourcef),
  181. DECL(alGetSource3f),
  182. DECL(alGetSourcefv),
  183. DECL(alGetSourcei),
  184. DECL(alGetSource3i),
  185. DECL(alGetSourceiv),
  186. DECL(alSourcePlayv),
  187. DECL(alSourceStopv),
  188. DECL(alSourceRewindv),
  189. DECL(alSourcePausev),
  190. DECL(alSourcePlay),
  191. DECL(alSourceStop),
  192. DECL(alSourceRewind),
  193. DECL(alSourcePause),
  194. DECL(alSourceQueueBuffers),
  195. DECL(alSourceUnqueueBuffers),
  196. DECL(alGenBuffers),
  197. DECL(alDeleteBuffers),
  198. DECL(alIsBuffer),
  199. DECL(alBufferData),
  200. DECL(alBufferf),
  201. DECL(alBuffer3f),
  202. DECL(alBufferfv),
  203. DECL(alBufferi),
  204. DECL(alBuffer3i),
  205. DECL(alBufferiv),
  206. DECL(alGetBufferf),
  207. DECL(alGetBuffer3f),
  208. DECL(alGetBufferfv),
  209. DECL(alGetBufferi),
  210. DECL(alGetBuffer3i),
  211. DECL(alGetBufferiv),
  212. DECL(alDopplerFactor),
  213. DECL(alDopplerVelocity),
  214. DECL(alSpeedOfSound),
  215. DECL(alDistanceModel),
  216. DECL(alGenFilters),
  217. DECL(alDeleteFilters),
  218. DECL(alIsFilter),
  219. DECL(alFilteri),
  220. DECL(alFilteriv),
  221. DECL(alFilterf),
  222. DECL(alFilterfv),
  223. DECL(alGetFilteri),
  224. DECL(alGetFilteriv),
  225. DECL(alGetFilterf),
  226. DECL(alGetFilterfv),
  227. DECL(alGenEffects),
  228. DECL(alDeleteEffects),
  229. DECL(alIsEffect),
  230. DECL(alEffecti),
  231. DECL(alEffectiv),
  232. DECL(alEffectf),
  233. DECL(alEffectfv),
  234. DECL(alGetEffecti),
  235. DECL(alGetEffectiv),
  236. DECL(alGetEffectf),
  237. DECL(alGetEffectfv),
  238. DECL(alGenAuxiliaryEffectSlots),
  239. DECL(alDeleteAuxiliaryEffectSlots),
  240. DECL(alIsAuxiliaryEffectSlot),
  241. DECL(alAuxiliaryEffectSloti),
  242. DECL(alAuxiliaryEffectSlotiv),
  243. DECL(alAuxiliaryEffectSlotf),
  244. DECL(alAuxiliaryEffectSlotfv),
  245. DECL(alGetAuxiliaryEffectSloti),
  246. DECL(alGetAuxiliaryEffectSlotiv),
  247. DECL(alGetAuxiliaryEffectSlotf),
  248. DECL(alGetAuxiliaryEffectSlotfv),
  249. DECL(alBufferSubDataSOFT),
  250. DECL(alBufferSamplesSOFT),
  251. DECL(alBufferSubSamplesSOFT),
  252. DECL(alGetBufferSamplesSOFT),
  253. DECL(alIsBufferFormatSupportedSOFT),
  254. DECL(alDeferUpdatesSOFT),
  255. DECL(alProcessUpdatesSOFT),
  256. DECL(alSourcedSOFT),
  257. DECL(alSource3dSOFT),
  258. DECL(alSourcedvSOFT),
  259. DECL(alGetSourcedSOFT),
  260. DECL(alGetSource3dSOFT),
  261. DECL(alGetSourcedvSOFT),
  262. DECL(alSourcei64SOFT),
  263. DECL(alSource3i64SOFT),
  264. DECL(alSourcei64vSOFT),
  265. DECL(alGetSourcei64SOFT),
  266. DECL(alGetSource3i64SOFT),
  267. DECL(alGetSourcei64vSOFT),
  268. DECL(alGenSoundfontsSOFT),
  269. DECL(alDeleteSoundfontsSOFT),
  270. DECL(alIsSoundfontSOFT),
  271. DECL(alGetSoundfontivSOFT),
  272. DECL(alSoundfontPresetsSOFT),
  273. DECL(alGenPresetsSOFT),
  274. DECL(alDeletePresetsSOFT),
  275. DECL(alIsPresetSOFT),
  276. DECL(alPresetiSOFT),
  277. DECL(alPresetivSOFT),
  278. DECL(alGetPresetivSOFT),
  279. DECL(alPresetFontsoundsSOFT),
  280. DECL(alGenFontsoundsSOFT),
  281. DECL(alDeleteFontsoundsSOFT),
  282. DECL(alIsFontsoundSOFT),
  283. DECL(alFontsoundiSOFT),
  284. DECL(alFontsound2iSOFT),
  285. DECL(alFontsoundivSOFT),
  286. DECL(alGetFontsoundivSOFT),
  287. DECL(alFontsoundModulatoriSOFT),
  288. DECL(alGetFontsoundModulatorivSOFT),
  289. DECL(alMidiSoundfontSOFT),
  290. DECL(alMidiSoundfontvSOFT),
  291. DECL(alMidiEventSOFT),
  292. DECL(alMidiSysExSOFT),
  293. DECL(alMidiPlaySOFT),
  294. DECL(alMidiPauseSOFT),
  295. DECL(alMidiStopSOFT),
  296. DECL(alMidiResetSOFT),
  297. DECL(alMidiGainSOFT),
  298. DECL(alGetInteger64SOFT),
  299. DECL(alGetInteger64vSOFT),
  300. DECL(alLoadSoundfontSOFT),
  301. { NULL, NULL }
  302. };
  303. #undef DECL
  304. #define DECL(x) { #x, (x) }
  305. static const ALCenums enumeration[] = {
  306. DECL(ALC_INVALID),
  307. DECL(ALC_FALSE),
  308. DECL(ALC_TRUE),
  309. DECL(ALC_MAJOR_VERSION),
  310. DECL(ALC_MINOR_VERSION),
  311. DECL(ALC_ATTRIBUTES_SIZE),
  312. DECL(ALC_ALL_ATTRIBUTES),
  313. DECL(ALC_DEFAULT_DEVICE_SPECIFIER),
  314. DECL(ALC_DEVICE_SPECIFIER),
  315. DECL(ALC_ALL_DEVICES_SPECIFIER),
  316. DECL(ALC_DEFAULT_ALL_DEVICES_SPECIFIER),
  317. DECL(ALC_EXTENSIONS),
  318. DECL(ALC_FREQUENCY),
  319. DECL(ALC_REFRESH),
  320. DECL(ALC_SYNC),
  321. DECL(ALC_MONO_SOURCES),
  322. DECL(ALC_STEREO_SOURCES),
  323. DECL(ALC_CAPTURE_DEVICE_SPECIFIER),
  324. DECL(ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER),
  325. DECL(ALC_CAPTURE_SAMPLES),
  326. DECL(ALC_CONNECTED),
  327. DECL(ALC_EFX_MAJOR_VERSION),
  328. DECL(ALC_EFX_MINOR_VERSION),
  329. DECL(ALC_MAX_AUXILIARY_SENDS),
  330. DECL(ALC_FORMAT_CHANNELS_SOFT),
  331. DECL(ALC_FORMAT_TYPE_SOFT),
  332. DECL(ALC_MONO_SOFT),
  333. DECL(ALC_STEREO_SOFT),
  334. DECL(ALC_QUAD_SOFT),
  335. DECL(ALC_5POINT1_SOFT),
  336. DECL(ALC_6POINT1_SOFT),
  337. DECL(ALC_7POINT1_SOFT),
  338. DECL(ALC_BYTE_SOFT),
  339. DECL(ALC_UNSIGNED_BYTE_SOFT),
  340. DECL(ALC_SHORT_SOFT),
  341. DECL(ALC_UNSIGNED_SHORT_SOFT),
  342. DECL(ALC_INT_SOFT),
  343. DECL(ALC_UNSIGNED_INT_SOFT),
  344. DECL(ALC_FLOAT_SOFT),
  345. DECL(ALC_NO_ERROR),
  346. DECL(ALC_INVALID_DEVICE),
  347. DECL(ALC_INVALID_CONTEXT),
  348. DECL(ALC_INVALID_ENUM),
  349. DECL(ALC_INVALID_VALUE),
  350. DECL(ALC_OUT_OF_MEMORY),
  351. DECL(AL_INVALID),
  352. DECL(AL_NONE),
  353. DECL(AL_FALSE),
  354. DECL(AL_TRUE),
  355. DECL(AL_SOURCE_RELATIVE),
  356. DECL(AL_CONE_INNER_ANGLE),
  357. DECL(AL_CONE_OUTER_ANGLE),
  358. DECL(AL_PITCH),
  359. DECL(AL_POSITION),
  360. DECL(AL_DIRECTION),
  361. DECL(AL_VELOCITY),
  362. DECL(AL_LOOPING),
  363. DECL(AL_BUFFER),
  364. DECL(AL_GAIN),
  365. DECL(AL_MIN_GAIN),
  366. DECL(AL_MAX_GAIN),
  367. DECL(AL_ORIENTATION),
  368. DECL(AL_REFERENCE_DISTANCE),
  369. DECL(AL_ROLLOFF_FACTOR),
  370. DECL(AL_CONE_OUTER_GAIN),
  371. DECL(AL_MAX_DISTANCE),
  372. DECL(AL_SEC_OFFSET),
  373. DECL(AL_SAMPLE_OFFSET),
  374. DECL(AL_SAMPLE_RW_OFFSETS_SOFT),
  375. DECL(AL_BYTE_OFFSET),
  376. DECL(AL_BYTE_RW_OFFSETS_SOFT),
  377. DECL(AL_SOURCE_TYPE),
  378. DECL(AL_STATIC),
  379. DECL(AL_STREAMING),
  380. DECL(AL_UNDETERMINED),
  381. DECL(AL_METERS_PER_UNIT),
  382. DECL(AL_DIRECT_CHANNELS_SOFT),
  383. DECL(AL_DIRECT_FILTER),
  384. DECL(AL_AUXILIARY_SEND_FILTER),
  385. DECL(AL_AIR_ABSORPTION_FACTOR),
  386. DECL(AL_ROOM_ROLLOFF_FACTOR),
  387. DECL(AL_CONE_OUTER_GAINHF),
  388. DECL(AL_DIRECT_FILTER_GAINHF_AUTO),
  389. DECL(AL_AUXILIARY_SEND_FILTER_GAIN_AUTO),
  390. DECL(AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO),
  391. DECL(AL_SOURCE_STATE),
  392. DECL(AL_INITIAL),
  393. DECL(AL_PLAYING),
  394. DECL(AL_PAUSED),
  395. DECL(AL_STOPPED),
  396. DECL(AL_BUFFERS_QUEUED),
  397. DECL(AL_BUFFERS_PROCESSED),
  398. DECL(AL_FORMAT_MONO8),
  399. DECL(AL_FORMAT_MONO16),
  400. DECL(AL_FORMAT_MONO_FLOAT32),
  401. DECL(AL_FORMAT_MONO_DOUBLE_EXT),
  402. DECL(AL_FORMAT_STEREO8),
  403. DECL(AL_FORMAT_STEREO16),
  404. DECL(AL_FORMAT_STEREO_FLOAT32),
  405. DECL(AL_FORMAT_STEREO_DOUBLE_EXT),
  406. DECL(AL_FORMAT_MONO_IMA4),
  407. DECL(AL_FORMAT_STEREO_IMA4),
  408. DECL(AL_FORMAT_MONO_MSADPCM_SOFT),
  409. DECL(AL_FORMAT_STEREO_MSADPCM_SOFT),
  410. DECL(AL_FORMAT_QUAD8_LOKI),
  411. DECL(AL_FORMAT_QUAD16_LOKI),
  412. DECL(AL_FORMAT_QUAD8),
  413. DECL(AL_FORMAT_QUAD16),
  414. DECL(AL_FORMAT_QUAD32),
  415. DECL(AL_FORMAT_51CHN8),
  416. DECL(AL_FORMAT_51CHN16),
  417. DECL(AL_FORMAT_51CHN32),
  418. DECL(AL_FORMAT_61CHN8),
  419. DECL(AL_FORMAT_61CHN16),
  420. DECL(AL_FORMAT_61CHN32),
  421. DECL(AL_FORMAT_71CHN8),
  422. DECL(AL_FORMAT_71CHN16),
  423. DECL(AL_FORMAT_71CHN32),
  424. DECL(AL_FORMAT_REAR8),
  425. DECL(AL_FORMAT_REAR16),
  426. DECL(AL_FORMAT_REAR32),
  427. DECL(AL_FORMAT_MONO_MULAW),
  428. DECL(AL_FORMAT_MONO_MULAW_EXT),
  429. DECL(AL_FORMAT_STEREO_MULAW),
  430. DECL(AL_FORMAT_STEREO_MULAW_EXT),
  431. DECL(AL_FORMAT_QUAD_MULAW),
  432. DECL(AL_FORMAT_51CHN_MULAW),
  433. DECL(AL_FORMAT_61CHN_MULAW),
  434. DECL(AL_FORMAT_71CHN_MULAW),
  435. DECL(AL_FORMAT_REAR_MULAW),
  436. DECL(AL_FORMAT_MONO_ALAW_EXT),
  437. DECL(AL_FORMAT_STEREO_ALAW_EXT),
  438. DECL(AL_MONO8_SOFT),
  439. DECL(AL_MONO16_SOFT),
  440. DECL(AL_MONO32F_SOFT),
  441. DECL(AL_STEREO8_SOFT),
  442. DECL(AL_STEREO16_SOFT),
  443. DECL(AL_STEREO32F_SOFT),
  444. DECL(AL_QUAD8_SOFT),
  445. DECL(AL_QUAD16_SOFT),
  446. DECL(AL_QUAD32F_SOFT),
  447. DECL(AL_REAR8_SOFT),
  448. DECL(AL_REAR16_SOFT),
  449. DECL(AL_REAR32F_SOFT),
  450. DECL(AL_5POINT1_8_SOFT),
  451. DECL(AL_5POINT1_16_SOFT),
  452. DECL(AL_5POINT1_32F_SOFT),
  453. DECL(AL_6POINT1_8_SOFT),
  454. DECL(AL_6POINT1_16_SOFT),
  455. DECL(AL_6POINT1_32F_SOFT),
  456. DECL(AL_7POINT1_8_SOFT),
  457. DECL(AL_7POINT1_16_SOFT),
  458. DECL(AL_7POINT1_32F_SOFT),
  459. DECL(AL_MONO_SOFT),
  460. DECL(AL_STEREO_SOFT),
  461. DECL(AL_QUAD_SOFT),
  462. DECL(AL_REAR_SOFT),
  463. DECL(AL_5POINT1_SOFT),
  464. DECL(AL_6POINT1_SOFT),
  465. DECL(AL_7POINT1_SOFT),
  466. DECL(AL_BYTE_SOFT),
  467. DECL(AL_UNSIGNED_BYTE_SOFT),
  468. DECL(AL_SHORT_SOFT),
  469. DECL(AL_UNSIGNED_SHORT_SOFT),
  470. DECL(AL_INT_SOFT),
  471. DECL(AL_UNSIGNED_INT_SOFT),
  472. DECL(AL_FLOAT_SOFT),
  473. DECL(AL_DOUBLE_SOFT),
  474. DECL(AL_BYTE3_SOFT),
  475. DECL(AL_UNSIGNED_BYTE3_SOFT),
  476. DECL(AL_FREQUENCY),
  477. DECL(AL_BITS),
  478. DECL(AL_CHANNELS),
  479. DECL(AL_SIZE),
  480. DECL(AL_INTERNAL_FORMAT_SOFT),
  481. DECL(AL_BYTE_LENGTH_SOFT),
  482. DECL(AL_SAMPLE_LENGTH_SOFT),
  483. DECL(AL_SEC_LENGTH_SOFT),
  484. DECL(AL_UNPACK_BLOCK_ALIGNMENT_SOFT),
  485. DECL(AL_PACK_BLOCK_ALIGNMENT_SOFT),
  486. DECL(AL_UNUSED),
  487. DECL(AL_PENDING),
  488. DECL(AL_PROCESSED),
  489. DECL(AL_NO_ERROR),
  490. DECL(AL_INVALID_NAME),
  491. DECL(AL_INVALID_ENUM),
  492. DECL(AL_INVALID_VALUE),
  493. DECL(AL_INVALID_OPERATION),
  494. DECL(AL_OUT_OF_MEMORY),
  495. DECL(AL_VENDOR),
  496. DECL(AL_VERSION),
  497. DECL(AL_RENDERER),
  498. DECL(AL_EXTENSIONS),
  499. DECL(AL_DOPPLER_FACTOR),
  500. DECL(AL_DOPPLER_VELOCITY),
  501. DECL(AL_DISTANCE_MODEL),
  502. DECL(AL_SPEED_OF_SOUND),
  503. DECL(AL_SOURCE_DISTANCE_MODEL),
  504. DECL(AL_DEFERRED_UPDATES_SOFT),
  505. DECL(AL_INVERSE_DISTANCE),
  506. DECL(AL_INVERSE_DISTANCE_CLAMPED),
  507. DECL(AL_LINEAR_DISTANCE),
  508. DECL(AL_LINEAR_DISTANCE_CLAMPED),
  509. DECL(AL_EXPONENT_DISTANCE),
  510. DECL(AL_EXPONENT_DISTANCE_CLAMPED),
  511. DECL(AL_FILTER_TYPE),
  512. DECL(AL_FILTER_NULL),
  513. DECL(AL_FILTER_LOWPASS),
  514. DECL(AL_FILTER_HIGHPASS),
  515. DECL(AL_FILTER_BANDPASS),
  516. DECL(AL_LOWPASS_GAIN),
  517. DECL(AL_LOWPASS_GAINHF),
  518. DECL(AL_HIGHPASS_GAIN),
  519. DECL(AL_HIGHPASS_GAINLF),
  520. DECL(AL_BANDPASS_GAIN),
  521. DECL(AL_BANDPASS_GAINHF),
  522. DECL(AL_BANDPASS_GAINLF),
  523. DECL(AL_EFFECT_TYPE),
  524. DECL(AL_EFFECT_NULL),
  525. DECL(AL_EFFECT_REVERB),
  526. DECL(AL_EFFECT_EAXREVERB),
  527. DECL(AL_EFFECT_CHORUS),
  528. DECL(AL_EFFECT_DISTORTION),
  529. DECL(AL_EFFECT_ECHO),
  530. DECL(AL_EFFECT_FLANGER),
  531. #if 0
  532. DECL(AL_EFFECT_FREQUENCY_SHIFTER),
  533. DECL(AL_EFFECT_VOCAL_MORPHER),
  534. DECL(AL_EFFECT_PITCH_SHIFTER),
  535. #endif
  536. DECL(AL_EFFECT_RING_MODULATOR),
  537. #if 0
  538. DECL(AL_EFFECT_AUTOWAH),
  539. #endif
  540. DECL(AL_EFFECT_COMPRESSOR),
  541. DECL(AL_EFFECT_EQUALIZER),
  542. DECL(AL_EFFECT_DEDICATED_LOW_FREQUENCY_EFFECT),
  543. DECL(AL_EFFECT_DEDICATED_DIALOGUE),
  544. DECL(AL_EAXREVERB_DENSITY),
  545. DECL(AL_EAXREVERB_DIFFUSION),
  546. DECL(AL_EAXREVERB_GAIN),
  547. DECL(AL_EAXREVERB_GAINHF),
  548. DECL(AL_EAXREVERB_GAINLF),
  549. DECL(AL_EAXREVERB_DECAY_TIME),
  550. DECL(AL_EAXREVERB_DECAY_HFRATIO),
  551. DECL(AL_EAXREVERB_DECAY_LFRATIO),
  552. DECL(AL_EAXREVERB_REFLECTIONS_GAIN),
  553. DECL(AL_EAXREVERB_REFLECTIONS_DELAY),
  554. DECL(AL_EAXREVERB_REFLECTIONS_PAN),
  555. DECL(AL_EAXREVERB_LATE_REVERB_GAIN),
  556. DECL(AL_EAXREVERB_LATE_REVERB_DELAY),
  557. DECL(AL_EAXREVERB_LATE_REVERB_PAN),
  558. DECL(AL_EAXREVERB_ECHO_TIME),
  559. DECL(AL_EAXREVERB_ECHO_DEPTH),
  560. DECL(AL_EAXREVERB_MODULATION_TIME),
  561. DECL(AL_EAXREVERB_MODULATION_DEPTH),
  562. DECL(AL_EAXREVERB_AIR_ABSORPTION_GAINHF),
  563. DECL(AL_EAXREVERB_HFREFERENCE),
  564. DECL(AL_EAXREVERB_LFREFERENCE),
  565. DECL(AL_EAXREVERB_ROOM_ROLLOFF_FACTOR),
  566. DECL(AL_EAXREVERB_DECAY_HFLIMIT),
  567. DECL(AL_REVERB_DENSITY),
  568. DECL(AL_REVERB_DIFFUSION),
  569. DECL(AL_REVERB_GAIN),
  570. DECL(AL_REVERB_GAINHF),
  571. DECL(AL_REVERB_DECAY_TIME),
  572. DECL(AL_REVERB_DECAY_HFRATIO),
  573. DECL(AL_REVERB_REFLECTIONS_GAIN),
  574. DECL(AL_REVERB_REFLECTIONS_DELAY),
  575. DECL(AL_REVERB_LATE_REVERB_GAIN),
  576. DECL(AL_REVERB_LATE_REVERB_DELAY),
  577. DECL(AL_REVERB_AIR_ABSORPTION_GAINHF),
  578. DECL(AL_REVERB_ROOM_ROLLOFF_FACTOR),
  579. DECL(AL_REVERB_DECAY_HFLIMIT),
  580. DECL(AL_CHORUS_WAVEFORM),
  581. DECL(AL_CHORUS_PHASE),
  582. DECL(AL_CHORUS_RATE),
  583. DECL(AL_CHORUS_DEPTH),
  584. DECL(AL_CHORUS_FEEDBACK),
  585. DECL(AL_CHORUS_DELAY),
  586. DECL(AL_DISTORTION_EDGE),
  587. DECL(AL_DISTORTION_GAIN),
  588. DECL(AL_DISTORTION_LOWPASS_CUTOFF),
  589. DECL(AL_DISTORTION_EQCENTER),
  590. DECL(AL_DISTORTION_EQBANDWIDTH),
  591. DECL(AL_ECHO_DELAY),
  592. DECL(AL_ECHO_LRDELAY),
  593. DECL(AL_ECHO_DAMPING),
  594. DECL(AL_ECHO_FEEDBACK),
  595. DECL(AL_ECHO_SPREAD),
  596. DECL(AL_FLANGER_WAVEFORM),
  597. DECL(AL_FLANGER_PHASE),
  598. DECL(AL_FLANGER_RATE),
  599. DECL(AL_FLANGER_DEPTH),
  600. DECL(AL_FLANGER_FEEDBACK),
  601. DECL(AL_FLANGER_DELAY),
  602. DECL(AL_RING_MODULATOR_FREQUENCY),
  603. DECL(AL_RING_MODULATOR_HIGHPASS_CUTOFF),
  604. DECL(AL_RING_MODULATOR_WAVEFORM),
  605. #if 0
  606. DECL(AL_AUTOWAH_ATTACK_TIME),
  607. DECL(AL_AUTOWAH_PEAK_GAIN),
  608. DECL(AL_AUTOWAH_RELEASE_TIME),
  609. DECL(AL_AUTOWAH_RESONANCE),
  610. #endif
  611. DECL(AL_COMPRESSOR_ONOFF),
  612. DECL(AL_EQUALIZER_LOW_GAIN),
  613. DECL(AL_EQUALIZER_LOW_CUTOFF),
  614. DECL(AL_EQUALIZER_MID1_GAIN),
  615. DECL(AL_EQUALIZER_MID1_CENTER),
  616. DECL(AL_EQUALIZER_MID1_WIDTH),
  617. DECL(AL_EQUALIZER_MID2_GAIN),
  618. DECL(AL_EQUALIZER_MID2_CENTER),
  619. DECL(AL_EQUALIZER_MID2_WIDTH),
  620. DECL(AL_EQUALIZER_HIGH_GAIN),
  621. DECL(AL_EQUALIZER_HIGH_CUTOFF),
  622. DECL(AL_DEDICATED_GAIN),
  623. { NULL, (ALCenum)0 }
  624. };
  625. #undef DECL
  626. static const ALCchar alcNoError[] = "No Error";
  627. static const ALCchar alcErrInvalidDevice[] = "Invalid Device";
  628. static const ALCchar alcErrInvalidContext[] = "Invalid Context";
  629. static const ALCchar alcErrInvalidEnum[] = "Invalid Enum";
  630. static const ALCchar alcErrInvalidValue[] = "Invalid Value";
  631. static const ALCchar alcErrOutOfMemory[] = "Out of Memory";
  632. /************************************************
  633. * Global variables
  634. ************************************************/
  635. /* Enumerated device names */
  636. static const ALCchar alcDefaultName[] = "OpenAL Soft\0";
  637. static al_string alcAllDevicesList;
  638. static al_string alcCaptureDeviceList;
  639. /* Default is always the first in the list */
  640. static ALCchar *alcDefaultAllDevicesSpecifier;
  641. static ALCchar *alcCaptureDefaultDeviceSpecifier;
  642. /* Default context extensions */
  643. static const ALchar alExtList[] =
  644. "AL_EXT_ALAW AL_EXT_DOUBLE AL_EXT_EXPONENT_DISTANCE AL_EXT_FLOAT32 "
  645. "AL_EXT_IMA4 AL_EXT_LINEAR_DISTANCE AL_EXT_MCFORMATS AL_EXT_MULAW "
  646. "AL_EXT_MULAW_MCFORMATS AL_EXT_OFFSET AL_EXT_source_distance_model "
  647. "AL_LOKI_quadriphonic AL_SOFT_block_alignment AL_SOFT_buffer_samples "
  648. "AL_SOFT_buffer_sub_data AL_SOFT_deferred_updates AL_SOFT_direct_channels "
  649. "AL_SOFT_loop_points AL_SOFT_MSADPCM AL_SOFT_source_latency "
  650. "AL_SOFT_source_length";
  651. static ATOMIC(ALCenum) LastNullDeviceError = ATOMIC_INIT_STATIC(ALC_NO_ERROR);
  652. /* Thread-local current context */
  653. static altss_t LocalContext;
  654. /* Process-wide current context */
  655. static ATOMIC(ALCcontext*) GlobalContext = ATOMIC_INIT_STATIC(NULL);
  656. /* Mixing thread piority level */
  657. ALint RTPrioLevel;
  658. FILE *LogFile;
  659. #ifdef _DEBUG
  660. enum LogLevel LogLevel = LogWarning;
  661. #else
  662. enum LogLevel LogLevel = LogError;
  663. #endif
  664. /* Flag to trap ALC device errors */
  665. static ALCboolean TrapALCError = ALC_FALSE;
  666. /* One-time configuration init control */
  667. static alonce_flag alc_config_once = AL_ONCE_FLAG_INIT;
  668. /* Default effect that applies to sources that don't have an effect on send 0 */
  669. static ALeffect DefaultEffect;
  670. /************************************************
  671. * ALC information
  672. ************************************************/
  673. static const ALCchar alcNoDeviceExtList[] =
  674. "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
  675. "ALC_EXT_thread_local_context ALC_SOFT_loopback";
  676. static const ALCchar alcExtensionList[] =
  677. "ALC_ENUMERATE_ALL_EXT ALC_ENUMERATION_EXT ALC_EXT_CAPTURE "
  678. "ALC_EXT_DEDICATED ALC_EXT_disconnect ALC_EXT_EFX "
  679. "ALC_EXT_thread_local_context ALC_SOFTX_device_clock ALC_SOFTX_HRTF "
  680. "ALC_SOFT_loopback ALC_SOFTX_midi_interface ALC_SOFT_pause_device";
  681. static const ALCint alcMajorVersion = 1;
  682. static const ALCint alcMinorVersion = 1;
  683. static const ALCint alcEFXMajorVersion = 1;
  684. static const ALCint alcEFXMinorVersion = 0;
  685. /************************************************
  686. * Device lists
  687. ************************************************/
  688. static ATOMIC(ALCdevice*) DeviceList = ATOMIC_INIT_STATIC(NULL);
  689. static almtx_t ListLock;
  690. static inline void LockLists(void)
  691. {
  692. int lockret = almtx_lock(&ListLock);
  693. assert(lockret == althrd_success);
  694. }
  695. static inline void UnlockLists(void)
  696. {
  697. int unlockret = almtx_unlock(&ListLock);
  698. assert(unlockret == althrd_success);
  699. }
  700. /************************************************
  701. * Library initialization
  702. ************************************************/
  703. #if defined(_WIN32)
  704. static void alc_init(void);
  705. static void alc_deinit(void);
  706. static void alc_deinit_safe(void);
  707. #ifndef AL_LIBTYPE_STATIC
  708. BOOL APIENTRY DllMain(HINSTANCE hModule, DWORD reason, LPVOID lpReserved)
  709. {
  710. switch(reason)
  711. {
  712. case DLL_PROCESS_ATTACH:
  713. /* Pin the DLL so we won't get unloaded until the process terminates */
  714. GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_PIN | GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
  715. (WCHAR*)hModule, &hModule);
  716. alc_init();
  717. break;
  718. case DLL_THREAD_DETACH:
  719. break;
  720. case DLL_PROCESS_DETACH:
  721. if(!lpReserved)
  722. alc_deinit();
  723. else
  724. alc_deinit_safe();
  725. break;
  726. }
  727. return TRUE;
  728. }
  729. #elif defined(_MSC_VER)
  730. #pragma section(".CRT$XCU",read)
  731. static void alc_constructor(void);
  732. static void alc_destructor(void);
  733. __declspec(allocate(".CRT$XCU")) void (__cdecl* alc_constructor_)(void) = alc_constructor;
  734. static void alc_constructor(void)
  735. {
  736. atexit(alc_destructor);
  737. alc_init();
  738. }
  739. static void alc_destructor(void)
  740. {
  741. alc_deinit();
  742. }
  743. #elif defined(HAVE_GCC_DESTRUCTOR)
  744. static void alc_init(void) __attribute__((constructor));
  745. static void alc_deinit(void) __attribute__((destructor));
  746. #else
  747. #error "No static initialization available on this platform!"
  748. #endif
  749. #elif defined(HAVE_GCC_DESTRUCTOR)
  750. static void alc_init(void) __attribute__((constructor));
  751. static void alc_deinit(void) __attribute__((destructor));
  752. #else
  753. #error "No global initialization available on this platform!"
  754. #endif
  755. static void ReleaseThreadCtx(void *ptr);
  756. static void alc_init(void)
  757. {
  758. const char *str;
  759. int ret;
  760. LogFile = stderr;
  761. AL_STRING_INIT(alcAllDevicesList);
  762. AL_STRING_INIT(alcCaptureDeviceList);
  763. str = getenv("__ALSOFT_HALF_ANGLE_CONES");
  764. if(str && (strcasecmp(str, "true") == 0 || strtol(str, NULL, 0) == 1))
  765. ConeScale *= 0.5f;
  766. str = getenv("__ALSOFT_REVERSE_Z");
  767. if(str && (strcasecmp(str, "true") == 0 || strtol(str, NULL, 0) == 1))
  768. ZScale *= -1.0f;
  769. ret = altss_create(&LocalContext, ReleaseThreadCtx);
  770. assert(ret == althrd_success);
  771. ret = almtx_init(&ListLock, almtx_recursive);
  772. assert(ret == althrd_success);
  773. ThunkInit();
  774. }
  775. static void alc_initconfig(void)
  776. {
  777. const char *devs, *str;
  778. ALuint capfilter;
  779. float valf;
  780. int i, n;
  781. str = getenv("ALSOFT_LOGLEVEL");
  782. if(str)
  783. {
  784. long lvl = strtol(str, NULL, 0);
  785. if(lvl >= NoLog && lvl <= LogRef)
  786. LogLevel = lvl;
  787. }
  788. str = getenv("ALSOFT_LOGFILE");
  789. if(str && str[0])
  790. {
  791. FILE *logfile = al_fopen(str, "wt");
  792. if(logfile) LogFile = logfile;
  793. else ERR("Failed to open log file '%s'\n", str);
  794. }
  795. {
  796. char buf[1024] = "";
  797. int len = snprintf(buf, sizeof(buf), "%s", BackendList[0].name);
  798. for(i = 1;BackendList[i].name;i++)
  799. len += snprintf(buf+len, sizeof(buf)-len, ", %s", BackendList[i].name);
  800. TRACE("Supported backends: %s\n", buf);
  801. }
  802. ReadALConfig();
  803. capfilter = 0;
  804. #if defined(HAVE_SSE4_1)
  805. capfilter |= CPU_CAP_SSE | CPU_CAP_SSE2 | CPU_CAP_SSE4_1;
  806. #elif defined(HAVE_SSE2)
  807. capfilter |= CPU_CAP_SSE | CPU_CAP_SSE2;
  808. #elif defined(HAVE_SSE)
  809. capfilter |= CPU_CAP_SSE;
  810. #endif
  811. #ifdef HAVE_NEON
  812. capfilter |= CPU_CAP_NEON;
  813. #endif
  814. if(ConfigValueStr(NULL, "disable-cpu-exts", &str))
  815. {
  816. if(strcasecmp(str, "all") == 0)
  817. capfilter = 0;
  818. else
  819. {
  820. size_t len;
  821. const char *next = str;
  822. do {
  823. str = next;
  824. while(isspace(str[0]))
  825. str++;
  826. next = strchr(str, ',');
  827. if(!str[0] || str[0] == ',')
  828. continue;
  829. len = (next ? ((size_t)(next-str)) : strlen(str));
  830. while(len > 0 && isspace(str[len-1]))
  831. len--;
  832. if(len == 3 && strncasecmp(str, "sse", len) == 0)
  833. capfilter &= ~CPU_CAP_SSE;
  834. else if(len == 4 && strncasecmp(str, "sse2", len) == 0)
  835. capfilter &= ~CPU_CAP_SSE2;
  836. else if(len == 6 && strncasecmp(str, "sse4.1", len) == 0)
  837. capfilter &= ~CPU_CAP_SSE4_1;
  838. else if(len == 4 && strncasecmp(str, "neon", len) == 0)
  839. capfilter &= ~CPU_CAP_NEON;
  840. else
  841. WARN("Invalid CPU extension \"%s\"\n", str);
  842. } while(next++);
  843. }
  844. }
  845. FillCPUCaps(capfilter);
  846. #ifdef _WIN32
  847. RTPrioLevel = 1;
  848. #else
  849. RTPrioLevel = 0;
  850. #endif
  851. ConfigValueInt(NULL, "rt-prio", &RTPrioLevel);
  852. if(ConfigValueStr(NULL, "resampler", &str))
  853. {
  854. if(strcasecmp(str, "point") == 0 || strcasecmp(str, "none") == 0)
  855. DefaultResampler = PointResampler;
  856. else if(strcasecmp(str, "linear") == 0)
  857. DefaultResampler = LinearResampler;
  858. else if(strcasecmp(str, "cubic") == 0)
  859. DefaultResampler = CubicResampler;
  860. else
  861. {
  862. char *end;
  863. n = strtol(str, &end, 0);
  864. if(*end == '\0' && (n == PointResampler || n == LinearResampler || n == CubicResampler))
  865. DefaultResampler = n;
  866. else
  867. WARN("Invalid resampler: %s\n", str);
  868. }
  869. }
  870. str = getenv("ALSOFT_TRAP_ERROR");
  871. if(str && (strcasecmp(str, "true") == 0 || strtol(str, NULL, 0) == 1))
  872. {
  873. TrapALError = AL_TRUE;
  874. TrapALCError = AL_TRUE;
  875. }
  876. else
  877. {
  878. str = getenv("ALSOFT_TRAP_AL_ERROR");
  879. if(str && (strcasecmp(str, "true") == 0 || strtol(str, NULL, 0) == 1))
  880. TrapALError = AL_TRUE;
  881. TrapALError = GetConfigValueBool(NULL, "trap-al-error", TrapALError);
  882. str = getenv("ALSOFT_TRAP_ALC_ERROR");
  883. if(str && (strcasecmp(str, "true") == 0 || strtol(str, NULL, 0) == 1))
  884. TrapALCError = ALC_TRUE;
  885. TrapALCError = GetConfigValueBool(NULL, "trap-alc-error", TrapALCError);
  886. }
  887. if(ConfigValueFloat("reverb", "boost", &valf))
  888. ReverbBoost *= powf(10.0f, valf / 20.0f);
  889. EmulateEAXReverb = GetConfigValueBool("reverb", "emulate-eax", AL_FALSE);
  890. if(((devs=getenv("ALSOFT_DRIVERS")) && devs[0]) ||
  891. ConfigValueStr(NULL, "drivers", &devs))
  892. {
  893. int n;
  894. size_t len;
  895. const char *next = devs;
  896. int endlist, delitem;
  897. i = 0;
  898. do {
  899. devs = next;
  900. while(isspace(devs[0]))
  901. devs++;
  902. next = strchr(devs, ',');
  903. delitem = (devs[0] == '-');
  904. if(devs[0] == '-') devs++;
  905. if(!devs[0] || devs[0] == ',')
  906. {
  907. endlist = 0;
  908. continue;
  909. }
  910. endlist = 1;
  911. len = (next ? ((size_t)(next-devs)) : strlen(devs));
  912. while(len > 0 && isspace(devs[len-1]))
  913. len--;
  914. for(n = i;BackendList[n].name;n++)
  915. {
  916. if(len == strlen(BackendList[n].name) &&
  917. strncmp(BackendList[n].name, devs, len) == 0)
  918. {
  919. if(delitem)
  920. {
  921. do {
  922. BackendList[n] = BackendList[n+1];
  923. ++n;
  924. } while(BackendList[n].name);
  925. }
  926. else
  927. {
  928. struct BackendInfo Bkp = BackendList[n];
  929. while(n > i)
  930. {
  931. BackendList[n] = BackendList[n-1];
  932. --n;
  933. }
  934. BackendList[n] = Bkp;
  935. i++;
  936. }
  937. break;
  938. }
  939. }
  940. } while(next++);
  941. if(endlist)
  942. {
  943. BackendList[i].name = NULL;
  944. BackendList[i].getFactory = NULL;
  945. BackendList[i].Init = NULL;
  946. BackendList[i].Deinit = NULL;
  947. BackendList[i].Probe = NULL;
  948. }
  949. }
  950. for(i = 0;(BackendList[i].Init || BackendList[i].getFactory) && (!PlaybackBackend.name || !CaptureBackend.name);i++)
  951. {
  952. if(BackendList[i].getFactory)
  953. {
  954. ALCbackendFactory *factory = BackendList[i].getFactory();
  955. if(!V0(factory,init)())
  956. {
  957. WARN("Failed to initialize backend \"%s\"\n", BackendList[i].name);
  958. continue;
  959. }
  960. TRACE("Initialized backend \"%s\"\n", BackendList[i].name);
  961. if(!PlaybackBackend.name && V(factory,querySupport)(ALCbackend_Playback))
  962. {
  963. PlaybackBackend = BackendList[i];
  964. TRACE("Added \"%s\" for playback\n", PlaybackBackend.name);
  965. }
  966. if(!CaptureBackend.name && V(factory,querySupport)(ALCbackend_Capture))
  967. {
  968. CaptureBackend = BackendList[i];
  969. TRACE("Added \"%s\" for capture\n", CaptureBackend.name);
  970. }
  971. continue;
  972. }
  973. if(!BackendList[i].Init(&BackendList[i].Funcs))
  974. {
  975. WARN("Failed to initialize backend \"%s\"\n", BackendList[i].name);
  976. continue;
  977. }
  978. TRACE("Initialized backend \"%s\"\n", BackendList[i].name);
  979. if(BackendList[i].Funcs.OpenPlayback && !PlaybackBackend.name)
  980. {
  981. PlaybackBackend = BackendList[i];
  982. TRACE("Added \"%s\" for playback\n", PlaybackBackend.name);
  983. }
  984. if(BackendList[i].Funcs.OpenCapture && !CaptureBackend.name)
  985. {
  986. CaptureBackend = BackendList[i];
  987. TRACE("Added \"%s\" for capture\n", CaptureBackend.name);
  988. }
  989. }
  990. {
  991. ALCbackendFactory *factory = ALCloopbackFactory_getFactory();
  992. V0(factory,init)();
  993. }
  994. if(ConfigValueStr(NULL, "excludefx", &str))
  995. {
  996. size_t len;
  997. const char *next = str;
  998. do {
  999. str = next;
  1000. next = strchr(str, ',');
  1001. if(!str[0] || next == str)
  1002. continue;
  1003. len = (next ? ((size_t)(next-str)) : strlen(str));
  1004. for(n = 0;EffectList[n].name;n++)
  1005. {
  1006. if(len == strlen(EffectList[n].name) &&
  1007. strncmp(EffectList[n].name, str, len) == 0)
  1008. DisabledEffects[EffectList[n].type] = AL_TRUE;
  1009. }
  1010. } while(next++);
  1011. }
  1012. InitEffectFactoryMap();
  1013. InitEffect(&DefaultEffect);
  1014. str = getenv("ALSOFT_DEFAULT_REVERB");
  1015. if((str && str[0]) || ConfigValueStr(NULL, "default-reverb", &str))
  1016. LoadReverbPreset(str, &DefaultEffect);
  1017. }
  1018. #define DO_INITCONFIG() alcall_once(&alc_config_once, alc_initconfig)
  1019. /************************************************
  1020. * Library deinitialization
  1021. ************************************************/
  1022. static void alc_cleanup(void)
  1023. {
  1024. ALCdevice *dev;
  1025. AL_STRING_DEINIT(alcAllDevicesList);
  1026. AL_STRING_DEINIT(alcCaptureDeviceList);
  1027. free(alcDefaultAllDevicesSpecifier);
  1028. alcDefaultAllDevicesSpecifier = NULL;
  1029. free(alcCaptureDefaultDeviceSpecifier);
  1030. alcCaptureDefaultDeviceSpecifier = NULL;
  1031. if((dev=ATOMIC_EXCHANGE(ALCdevice*, &DeviceList, NULL)) != NULL)
  1032. {
  1033. ALCuint num = 0;
  1034. do {
  1035. num++;
  1036. } while((dev=dev->next) != NULL);
  1037. ERR("%u device%s not closed\n", num, (num>1)?"s":"");
  1038. }
  1039. DeinitEffectFactoryMap();
  1040. }
  1041. static void alc_deinit_safe(void)
  1042. {
  1043. alc_cleanup();
  1044. FreeHrtfs();
  1045. FreeALConfig();
  1046. ThunkExit();
  1047. almtx_destroy(&ListLock);
  1048. altss_delete(LocalContext);
  1049. if(LogFile != stderr)
  1050. fclose(LogFile);
  1051. LogFile = NULL;
  1052. }
  1053. static void alc_deinit(void)
  1054. {
  1055. int i;
  1056. alc_cleanup();
  1057. memset(&PlaybackBackend, 0, sizeof(PlaybackBackend));
  1058. memset(&CaptureBackend, 0, sizeof(CaptureBackend));
  1059. for(i = 0;BackendList[i].Deinit || BackendList[i].getFactory;i++)
  1060. {
  1061. if(!BackendList[i].getFactory)
  1062. BackendList[i].Deinit();
  1063. else
  1064. {
  1065. ALCbackendFactory *factory = BackendList[i].getFactory();
  1066. V0(factory,deinit)();
  1067. }
  1068. }
  1069. {
  1070. ALCbackendFactory *factory = ALCloopbackFactory_getFactory();
  1071. V0(factory,deinit)();
  1072. }
  1073. alc_deinit_safe();
  1074. }
  1075. /************************************************
  1076. * Device enumeration
  1077. ************************************************/
  1078. static void ProbeDevices(al_string *list, enum DevProbe type)
  1079. {
  1080. DO_INITCONFIG();
  1081. LockLists();
  1082. al_string_clear(list);
  1083. if(type == ALL_DEVICE_PROBE && (PlaybackBackend.Probe || PlaybackBackend.getFactory))
  1084. {
  1085. if(!PlaybackBackend.getFactory)
  1086. PlaybackBackend.Probe(type);
  1087. else
  1088. {
  1089. ALCbackendFactory *factory = PlaybackBackend.getFactory();
  1090. V(factory,probe)(type);
  1091. }
  1092. }
  1093. else if(type == CAPTURE_DEVICE_PROBE && (CaptureBackend.Probe || CaptureBackend.getFactory))
  1094. {
  1095. if(!CaptureBackend.getFactory)
  1096. CaptureBackend.Probe(type);
  1097. else
  1098. {
  1099. ALCbackendFactory *factory = CaptureBackend.getFactory();
  1100. V(factory,probe)(type);
  1101. }
  1102. }
  1103. UnlockLists();
  1104. }
  1105. static void ProbeAllDevicesList(void)
  1106. { ProbeDevices(&alcAllDevicesList, ALL_DEVICE_PROBE); }
  1107. static void ProbeCaptureDeviceList(void)
  1108. { ProbeDevices(&alcCaptureDeviceList, CAPTURE_DEVICE_PROBE); }
  1109. static void AppendDevice(const ALCchar *name, al_string *devnames)
  1110. {
  1111. size_t len = strlen(name);
  1112. if(len > 0)
  1113. {
  1114. al_string_append_range(devnames, name, name+len);
  1115. al_string_append_char(devnames, '\0');
  1116. }
  1117. }
  1118. void AppendAllDevicesList(const ALCchar *name)
  1119. { AppendDevice(name, &alcAllDevicesList); }
  1120. void AppendCaptureDeviceList(const ALCchar *name)
  1121. { AppendDevice(name, &alcCaptureDeviceList); }
  1122. /************************************************
  1123. * Device format information
  1124. ************************************************/
  1125. const ALCchar *DevFmtTypeString(enum DevFmtType type)
  1126. {
  1127. switch(type)
  1128. {
  1129. case DevFmtByte: return "Signed Byte";
  1130. case DevFmtUByte: return "Unsigned Byte";
  1131. case DevFmtShort: return "Signed Short";
  1132. case DevFmtUShort: return "Unsigned Short";
  1133. case DevFmtInt: return "Signed Int";
  1134. case DevFmtUInt: return "Unsigned Int";
  1135. case DevFmtFloat: return "Float";
  1136. }
  1137. return "(unknown type)";
  1138. }
  1139. const ALCchar *DevFmtChannelsString(enum DevFmtChannels chans)
  1140. {
  1141. switch(chans)
  1142. {
  1143. case DevFmtMono: return "Mono";
  1144. case DevFmtStereo: return "Stereo";
  1145. case DevFmtQuad: return "Quadraphonic";
  1146. case DevFmtX51: return "5.1 Surround";
  1147. case DevFmtX51Side: return "5.1 Side";
  1148. case DevFmtX61: return "6.1 Surround";
  1149. case DevFmtX71: return "7.1 Surround";
  1150. }
  1151. return "(unknown channels)";
  1152. }
  1153. extern inline ALuint FrameSizeFromDevFmt(enum DevFmtChannels chans, enum DevFmtType type);
  1154. ALuint BytesFromDevFmt(enum DevFmtType type)
  1155. {
  1156. switch(type)
  1157. {
  1158. case DevFmtByte: return sizeof(ALbyte);
  1159. case DevFmtUByte: return sizeof(ALubyte);
  1160. case DevFmtShort: return sizeof(ALshort);
  1161. case DevFmtUShort: return sizeof(ALushort);
  1162. case DevFmtInt: return sizeof(ALint);
  1163. case DevFmtUInt: return sizeof(ALuint);
  1164. case DevFmtFloat: return sizeof(ALfloat);
  1165. }
  1166. return 0;
  1167. }
  1168. ALuint ChannelsFromDevFmt(enum DevFmtChannels chans)
  1169. {
  1170. switch(chans)
  1171. {
  1172. case DevFmtMono: return 1;
  1173. case DevFmtStereo: return 2;
  1174. case DevFmtQuad: return 4;
  1175. case DevFmtX51: return 6;
  1176. case DevFmtX51Side: return 6;
  1177. case DevFmtX61: return 7;
  1178. case DevFmtX71: return 8;
  1179. }
  1180. return 0;
  1181. }
  1182. DECL_CONST static ALboolean DecomposeDevFormat(ALenum format,
  1183. enum DevFmtChannels *chans, enum DevFmtType *type)
  1184. {
  1185. static const struct {
  1186. ALenum format;
  1187. enum DevFmtChannels channels;
  1188. enum DevFmtType type;
  1189. } list[] = {
  1190. { AL_FORMAT_MONO8, DevFmtMono, DevFmtUByte },
  1191. { AL_FORMAT_MONO16, DevFmtMono, DevFmtShort },
  1192. { AL_FORMAT_MONO_FLOAT32, DevFmtMono, DevFmtFloat },
  1193. { AL_FORMAT_STEREO8, DevFmtStereo, DevFmtUByte },
  1194. { AL_FORMAT_STEREO16, DevFmtStereo, DevFmtShort },
  1195. { AL_FORMAT_STEREO_FLOAT32, DevFmtStereo, DevFmtFloat },
  1196. { AL_FORMAT_QUAD8, DevFmtQuad, DevFmtUByte },
  1197. { AL_FORMAT_QUAD16, DevFmtQuad, DevFmtShort },
  1198. { AL_FORMAT_QUAD32, DevFmtQuad, DevFmtFloat },
  1199. { AL_FORMAT_51CHN8, DevFmtX51, DevFmtUByte },
  1200. { AL_FORMAT_51CHN16, DevFmtX51, DevFmtShort },
  1201. { AL_FORMAT_51CHN32, DevFmtX51, DevFmtFloat },
  1202. { AL_FORMAT_61CHN8, DevFmtX61, DevFmtUByte },
  1203. { AL_FORMAT_61CHN16, DevFmtX61, DevFmtShort },
  1204. { AL_FORMAT_61CHN32, DevFmtX61, DevFmtFloat },
  1205. { AL_FORMAT_71CHN8, DevFmtX71, DevFmtUByte },
  1206. { AL_FORMAT_71CHN16, DevFmtX71, DevFmtShort },
  1207. { AL_FORMAT_71CHN32, DevFmtX71, DevFmtFloat },
  1208. };
  1209. ALuint i;
  1210. for(i = 0;i < COUNTOF(list);i++)
  1211. {
  1212. if(list[i].format == format)
  1213. {
  1214. *chans = list[i].channels;
  1215. *type = list[i].type;
  1216. return AL_TRUE;
  1217. }
  1218. }
  1219. return AL_FALSE;
  1220. }
  1221. DECL_CONST static ALCboolean IsValidALCType(ALCenum type)
  1222. {
  1223. switch(type)
  1224. {
  1225. case ALC_BYTE_SOFT:
  1226. case ALC_UNSIGNED_BYTE_SOFT:
  1227. case ALC_SHORT_SOFT:
  1228. case ALC_UNSIGNED_SHORT_SOFT:
  1229. case ALC_INT_SOFT:
  1230. case ALC_UNSIGNED_INT_SOFT:
  1231. case ALC_FLOAT_SOFT:
  1232. return ALC_TRUE;
  1233. }
  1234. return ALC_FALSE;
  1235. }
  1236. DECL_CONST static ALCboolean IsValidALCChannels(ALCenum channels)
  1237. {
  1238. switch(channels)
  1239. {
  1240. case ALC_MONO_SOFT:
  1241. case ALC_STEREO_SOFT:
  1242. case ALC_QUAD_SOFT:
  1243. case ALC_5POINT1_SOFT:
  1244. case ALC_6POINT1_SOFT:
  1245. case ALC_7POINT1_SOFT:
  1246. return ALC_TRUE;
  1247. }
  1248. return ALC_FALSE;
  1249. }
  1250. /************************************************
  1251. * Miscellaneous ALC helpers
  1252. ************************************************/
  1253. extern inline void LockContext(ALCcontext *context);
  1254. extern inline void UnlockContext(ALCcontext *context);
  1255. ALint64 ALCdevice_GetLatencyDefault(ALCdevice *UNUSED(device))
  1256. {
  1257. return 0;
  1258. }
  1259. ALint64 ALCdevice_GetLatency(ALCdevice *device)
  1260. {
  1261. return V0(device->Backend,getLatency)();
  1262. }
  1263. void ALCdevice_Lock(ALCdevice *device)
  1264. {
  1265. V0(device->Backend,lock)();
  1266. }
  1267. void ALCdevice_Unlock(ALCdevice *device)
  1268. {
  1269. V0(device->Backend,unlock)();
  1270. }
  1271. /* SetDefaultWFXChannelOrder
  1272. *
  1273. * Sets the default channel order used by WaveFormatEx.
  1274. */
  1275. void SetDefaultWFXChannelOrder(ALCdevice *device)
  1276. {
  1277. ALuint i;
  1278. for(i = 0;i < MaxChannels;i++)
  1279. device->ChannelOffsets[i] = INVALID_OFFSET;
  1280. switch(device->FmtChans)
  1281. {
  1282. case DevFmtMono: device->ChannelOffsets[FrontCenter] = 0;
  1283. break;
  1284. case DevFmtStereo: device->ChannelOffsets[FrontLeft] = 0;
  1285. device->ChannelOffsets[FrontRight] = 1;
  1286. break;
  1287. case DevFmtQuad: device->ChannelOffsets[FrontLeft] = 0;
  1288. device->ChannelOffsets[FrontRight] = 1;
  1289. device->ChannelOffsets[BackLeft] = 2;
  1290. device->ChannelOffsets[BackRight] = 3;
  1291. break;
  1292. case DevFmtX51: device->ChannelOffsets[FrontLeft] = 0;
  1293. device->ChannelOffsets[FrontRight] = 1;
  1294. device->ChannelOffsets[FrontCenter] = 2;
  1295. device->ChannelOffsets[LFE] = 3;
  1296. device->ChannelOffsets[BackLeft] = 4;
  1297. device->ChannelOffsets[BackRight] = 5;
  1298. break;
  1299. case DevFmtX51Side: device->ChannelOffsets[FrontLeft] = 0;
  1300. device->ChannelOffsets[FrontRight] = 1;
  1301. device->ChannelOffsets[FrontCenter] = 2;
  1302. device->ChannelOffsets[LFE] = 3;
  1303. device->ChannelOffsets[SideLeft] = 4;
  1304. device->ChannelOffsets[SideRight] = 5;
  1305. break;
  1306. case DevFmtX61: device->ChannelOffsets[FrontLeft] = 0;
  1307. device->ChannelOffsets[FrontRight] = 1;
  1308. device->ChannelOffsets[FrontCenter] = 2;
  1309. device->ChannelOffsets[LFE] = 3;
  1310. device->ChannelOffsets[BackCenter] = 4;
  1311. device->ChannelOffsets[SideLeft] = 5;
  1312. device->ChannelOffsets[SideRight] = 6;
  1313. break;
  1314. case DevFmtX71: device->ChannelOffsets[FrontLeft] = 0;
  1315. device->ChannelOffsets[FrontRight] = 1;
  1316. device->ChannelOffsets[FrontCenter] = 2;
  1317. device->ChannelOffsets[LFE] = 3;
  1318. device->ChannelOffsets[BackLeft] = 4;
  1319. device->ChannelOffsets[BackRight] = 5;
  1320. device->ChannelOffsets[SideLeft] = 6;
  1321. device->ChannelOffsets[SideRight] = 7;
  1322. break;
  1323. }
  1324. }
  1325. /* SetDefaultChannelOrder
  1326. *
  1327. * Sets the default channel order used by most non-WaveFormatEx-based APIs.
  1328. */
  1329. void SetDefaultChannelOrder(ALCdevice *device)
  1330. {
  1331. ALuint i;
  1332. for(i = 0;i < MaxChannels;i++)
  1333. device->ChannelOffsets[i] = INVALID_OFFSET;
  1334. switch(device->FmtChans)
  1335. {
  1336. case DevFmtX51: device->ChannelOffsets[FrontLeft] = 0;
  1337. device->ChannelOffsets[FrontRight] = 1;
  1338. device->ChannelOffsets[BackLeft] = 2;
  1339. device->ChannelOffsets[BackRight] = 3;
  1340. device->ChannelOffsets[FrontCenter] = 4;
  1341. device->ChannelOffsets[LFE] = 5;
  1342. return;
  1343. case DevFmtX71: device->ChannelOffsets[FrontLeft] = 0;
  1344. device->ChannelOffsets[FrontRight] = 1;
  1345. device->ChannelOffsets[BackLeft] = 2;
  1346. device->ChannelOffsets[BackRight] = 3;
  1347. device->ChannelOffsets[FrontCenter] = 4;
  1348. device->ChannelOffsets[LFE] = 5;
  1349. device->ChannelOffsets[SideLeft] = 6;
  1350. device->ChannelOffsets[SideRight] = 7;
  1351. return;
  1352. /* Same as WFX order */
  1353. case DevFmtMono:
  1354. case DevFmtStereo:
  1355. case DevFmtQuad:
  1356. case DevFmtX51Side:
  1357. case DevFmtX61:
  1358. break;
  1359. }
  1360. SetDefaultWFXChannelOrder(device);
  1361. }
  1362. /* alcSetError
  1363. *
  1364. * Stores the latest ALC device error
  1365. */
  1366. static void alcSetError(ALCdevice *device, ALCenum errorCode)
  1367. {
  1368. if(TrapALCError)
  1369. {
  1370. #ifdef _WIN32
  1371. /* DebugBreak() will cause an exception if there is no debugger */
  1372. if(IsDebuggerPresent())
  1373. DebugBreak();
  1374. #elif defined(SIGTRAP)
  1375. raise(SIGTRAP);
  1376. #endif
  1377. }
  1378. if(device)
  1379. ATOMIC_STORE(&device->LastError, errorCode);
  1380. else
  1381. ATOMIC_STORE(&LastNullDeviceError, errorCode);
  1382. }
  1383. /* UpdateClockBase
  1384. *
  1385. * Updates the device's base clock time with however many samples have been
  1386. * done. This is used so frequency changes on the device don't cause the time
  1387. * to jump forward or back.
  1388. */
  1389. static inline void UpdateClockBase(ALCdevice *device)
  1390. {
  1391. device->ClockBase += device->SamplesDone * DEVICE_CLOCK_RES / device->Frequency;
  1392. device->SamplesDone = 0;
  1393. }
  1394. /* UpdateDeviceParams
  1395. *
  1396. * Updates device parameters according to the attribute list (caller is
  1397. * responsible for holding the list lock).
  1398. */
  1399. static ALCenum UpdateDeviceParams(ALCdevice *device, const ALCint *attrList)
  1400. {
  1401. ALCcontext *context;
  1402. enum DevFmtChannels oldChans;
  1403. enum DevFmtType oldType;
  1404. ALCuint oldFreq;
  1405. FPUCtl oldMode;
  1406. // Check for attributes
  1407. if(device->Type == Loopback)
  1408. {
  1409. enum {
  1410. GotFreq = 1<<0,
  1411. GotChans = 1<<1,
  1412. GotType = 1<<2,
  1413. GotAll = GotFreq|GotChans|GotType
  1414. };
  1415. ALCuint freq, numMono, numStereo, numSends, flags;
  1416. enum DevFmtChannels schans;
  1417. enum DevFmtType stype;
  1418. ALCuint attrIdx = 0;
  1419. ALCint gotFmt = 0;
  1420. if(!attrList)
  1421. {
  1422. WARN("Missing attributes for loopback device\n");
  1423. return ALC_INVALID_VALUE;
  1424. }
  1425. numMono = device->NumMonoSources;
  1426. numStereo = device->NumStereoSources;
  1427. numSends = device->NumAuxSends;
  1428. schans = device->FmtChans;
  1429. stype = device->FmtType;
  1430. freq = device->Frequency;
  1431. flags = device->Flags;
  1432. while(attrList[attrIdx])
  1433. {
  1434. if(attrList[attrIdx] == ALC_FORMAT_CHANNELS_SOFT)
  1435. {
  1436. ALCint val = attrList[attrIdx + 1];
  1437. if(!IsValidALCChannels(val) || !ChannelsFromDevFmt(val))
  1438. return ALC_INVALID_VALUE;
  1439. schans = val;
  1440. gotFmt |= GotChans;
  1441. }
  1442. if(attrList[attrIdx] == ALC_FORMAT_TYPE_SOFT)
  1443. {
  1444. ALCint val = attrList[attrIdx + 1];
  1445. if(!IsValidALCType(val) || !BytesFromDevFmt(val))
  1446. return ALC_INVALID_VALUE;
  1447. stype = val;
  1448. gotFmt |= GotType;
  1449. }
  1450. if(attrList[attrIdx] == ALC_FREQUENCY)
  1451. {
  1452. freq = attrList[attrIdx + 1];
  1453. if(freq < MIN_OUTPUT_RATE)
  1454. return ALC_INVALID_VALUE;
  1455. gotFmt |= GotFreq;
  1456. }
  1457. if(attrList[attrIdx] == ALC_STEREO_SOURCES)
  1458. {
  1459. numStereo = attrList[attrIdx + 1];
  1460. if(numStereo > device->MaxNoOfSources)
  1461. numStereo = device->MaxNoOfSources;
  1462. numMono = device->MaxNoOfSources - numStereo;
  1463. }
  1464. if(attrList[attrIdx] == ALC_MAX_AUXILIARY_SENDS)
  1465. numSends = attrList[attrIdx + 1];
  1466. if(attrList[attrIdx] == ALC_HRTF_SOFT)
  1467. {
  1468. if(attrList[attrIdx + 1] != ALC_FALSE)
  1469. flags |= DEVICE_HRTF_REQUEST;
  1470. else
  1471. flags &= ~DEVICE_HRTF_REQUEST;
  1472. }
  1473. attrIdx += 2;
  1474. }
  1475. if(gotFmt != GotAll)
  1476. {
  1477. WARN("Missing format for loopback device\n");
  1478. return ALC_INVALID_VALUE;
  1479. }
  1480. ConfigValueUInt(NULL, "sends", &numSends);
  1481. numSends = minu(MAX_SENDS, numSends);
  1482. if((device->Flags&DEVICE_RUNNING))
  1483. V0(device->Backend,stop)();
  1484. device->Flags = (flags & ~DEVICE_RUNNING);
  1485. UpdateClockBase(device);
  1486. device->Frequency = freq;
  1487. device->FmtChans = schans;
  1488. device->FmtType = stype;
  1489. device->NumMonoSources = numMono;
  1490. device->NumStereoSources = numStereo;
  1491. device->NumAuxSends = numSends;
  1492. }
  1493. else if(attrList && attrList[0])
  1494. {
  1495. ALCuint freq, numMono, numStereo, numSends;
  1496. ALCuint attrIdx = 0;
  1497. /* If a context is already running on the device, stop playback so the
  1498. * device attributes can be updated. */
  1499. if((device->Flags&DEVICE_RUNNING))
  1500. V0(device->Backend,stop)();
  1501. device->Flags &= ~DEVICE_RUNNING;
  1502. freq = device->Frequency;
  1503. numMono = device->NumMonoSources;
  1504. numStereo = device->NumStereoSources;
  1505. numSends = device->NumAuxSends;
  1506. while(attrList[attrIdx])
  1507. {
  1508. if(attrList[attrIdx] == ALC_FREQUENCY)
  1509. {
  1510. freq = attrList[attrIdx + 1];
  1511. device->Flags |= DEVICE_FREQUENCY_REQUEST;
  1512. }
  1513. if(attrList[attrIdx] == ALC_STEREO_SOURCES)
  1514. {
  1515. numStereo = attrList[attrIdx + 1];
  1516. if(numStereo > device->MaxNoOfSources)
  1517. numStereo = device->MaxNoOfSources;
  1518. numMono = device->MaxNoOfSources - numStereo;
  1519. }
  1520. if(attrList[attrIdx] == ALC_MAX_AUXILIARY_SENDS)
  1521. numSends = attrList[attrIdx + 1];
  1522. if(attrList[attrIdx] == ALC_HRTF_SOFT)
  1523. {
  1524. if(attrList[attrIdx + 1] != ALC_FALSE)
  1525. device->Flags |= DEVICE_HRTF_REQUEST;
  1526. else
  1527. device->Flags &= ~DEVICE_HRTF_REQUEST;
  1528. }
  1529. attrIdx += 2;
  1530. }
  1531. ConfigValueUInt(NULL, "frequency", &freq);
  1532. freq = maxu(freq, MIN_OUTPUT_RATE);
  1533. ConfigValueUInt(NULL, "sends", &numSends);
  1534. numSends = minu(MAX_SENDS, numSends);
  1535. UpdateClockBase(device);
  1536. device->UpdateSize = (ALuint64)device->UpdateSize * freq /
  1537. device->Frequency;
  1538. /* SSE and Neon do best with the update size being a multiple of 4 */
  1539. if((CPUCapFlags&(CPU_CAP_SSE|CPU_CAP_NEON)) != 0)
  1540. device->UpdateSize = (device->UpdateSize+3)&~3;
  1541. device->Frequency = freq;
  1542. device->NumMonoSources = numMono;
  1543. device->NumStereoSources = numStereo;
  1544. device->NumAuxSends = numSends;
  1545. }
  1546. if((device->Flags&DEVICE_RUNNING))
  1547. return ALC_NO_ERROR;
  1548. UpdateClockBase(device);
  1549. if(device->Type != Loopback)
  1550. {
  1551. bool usehrtf = !!(device->Flags&DEVICE_HRTF_REQUEST);
  1552. if(GetConfigValueBool(NULL, "hrtf", usehrtf))
  1553. device->Flags |= DEVICE_HRTF_REQUEST;
  1554. else
  1555. device->Flags &= ~DEVICE_HRTF_REQUEST;
  1556. }
  1557. if((device->Flags&DEVICE_HRTF_REQUEST))
  1558. {
  1559. enum DevFmtChannels chans = device->FmtChans;
  1560. ALCuint freq = device->Frequency;
  1561. if(FindHrtfFormat(&chans, &freq))
  1562. {
  1563. if(device->Type != Loopback)
  1564. {
  1565. device->Frequency = freq;
  1566. device->FmtChans = chans;
  1567. device->Flags |= DEVICE_CHANNELS_REQUEST |
  1568. DEVICE_FREQUENCY_REQUEST;
  1569. }
  1570. else if(device->Frequency != freq || device->FmtChans != chans)
  1571. {
  1572. ERR("Requested format not HRTF compatible: %s, %uhz\n",
  1573. DevFmtChannelsString(device->FmtChans), device->Frequency);
  1574. device->Flags &= ~DEVICE_HRTF_REQUEST;
  1575. }
  1576. }
  1577. }
  1578. oldFreq = device->Frequency;
  1579. oldChans = device->FmtChans;
  1580. oldType = device->FmtType;
  1581. TRACE("Pre-reset: %s%s, %s%s, %s%uhz, %u update size x%d\n",
  1582. (device->Flags&DEVICE_CHANNELS_REQUEST)?"*":"",
  1583. DevFmtChannelsString(device->FmtChans),
  1584. (device->Flags&DEVICE_SAMPLE_TYPE_REQUEST)?"*":"",
  1585. DevFmtTypeString(device->FmtType),
  1586. (device->Flags&DEVICE_FREQUENCY_REQUEST)?"*":"",
  1587. device->Frequency,
  1588. device->UpdateSize, device->NumUpdates);
  1589. if(V0(device->Backend,reset)() == ALC_FALSE)
  1590. return ALC_INVALID_DEVICE;
  1591. if(device->FmtChans != oldChans && (device->Flags&DEVICE_CHANNELS_REQUEST))
  1592. {
  1593. ERR("Failed to set %s, got %s instead\n", DevFmtChannelsString(oldChans),
  1594. DevFmtChannelsString(device->FmtChans));
  1595. device->Flags &= ~DEVICE_CHANNELS_REQUEST;
  1596. }
  1597. if(device->FmtType != oldType && (device->Flags&DEVICE_SAMPLE_TYPE_REQUEST))
  1598. {
  1599. ERR("Failed to set %s, got %s instead\n", DevFmtTypeString(oldType),
  1600. DevFmtTypeString(device->FmtType));
  1601. device->Flags &= ~DEVICE_SAMPLE_TYPE_REQUEST;
  1602. }
  1603. if(device->Frequency != oldFreq && (device->Flags&DEVICE_FREQUENCY_REQUEST))
  1604. {
  1605. ERR("Failed to set %uhz, got %uhz instead\n", oldFreq, device->Frequency);
  1606. device->Flags &= ~DEVICE_FREQUENCY_REQUEST;
  1607. }
  1608. TRACE("Post-reset: %s, %s, %uhz, %u update size x%d\n",
  1609. DevFmtChannelsString(device->FmtChans),
  1610. DevFmtTypeString(device->FmtType), device->Frequency,
  1611. device->UpdateSize, device->NumUpdates);
  1612. aluInitPanning(device);
  1613. V(device->Synth,update)(device);
  1614. device->Hrtf = NULL;
  1615. if((device->Flags&DEVICE_HRTF_REQUEST))
  1616. {
  1617. device->Hrtf = GetHrtf(device->FmtChans, device->Frequency);
  1618. if(!device->Hrtf)
  1619. device->Flags &= ~DEVICE_HRTF_REQUEST;
  1620. }
  1621. TRACE("HRTF %s\n", device->Hrtf?"enabled":"disabled");
  1622. if(!device->Hrtf && device->Bs2bLevel > 0 && device->Bs2bLevel <= 6)
  1623. {
  1624. if(!device->Bs2b)
  1625. {
  1626. device->Bs2b = calloc(1, sizeof(*device->Bs2b));
  1627. bs2b_clear(device->Bs2b);
  1628. }
  1629. bs2b_set_srate(device->Bs2b, device->Frequency);
  1630. bs2b_set_level(device->Bs2b, device->Bs2bLevel);
  1631. TRACE("BS2B level %d\n", device->Bs2bLevel);
  1632. }
  1633. else
  1634. {
  1635. free(device->Bs2b);
  1636. device->Bs2b = NULL;
  1637. TRACE("BS2B disabled\n");
  1638. }
  1639. device->Flags &= ~DEVICE_WIDE_STEREO;
  1640. if(device->Type != Loopback && !device->Hrtf && GetConfigValueBool(NULL, "wide-stereo", AL_FALSE))
  1641. device->Flags |= DEVICE_WIDE_STEREO;
  1642. if(!device->Hrtf && (device->UpdateSize&3))
  1643. {
  1644. if((CPUCapFlags&CPU_CAP_SSE))
  1645. WARN("SSE performs best with multiple of 4 update sizes (%u)\n", device->UpdateSize);
  1646. if((CPUCapFlags&CPU_CAP_NEON))
  1647. WARN("NEON performs best with multiple of 4 update sizes (%u)\n", device->UpdateSize);
  1648. }
  1649. SetMixerFPUMode(&oldMode);
  1650. ALCdevice_Lock(device);
  1651. context = ATOMIC_LOAD(&device->ContextList);
  1652. while(context)
  1653. {
  1654. ALsizei pos;
  1655. ATOMIC_STORE(&context->UpdateSources, AL_FALSE);
  1656. LockUIntMapRead(&context->EffectSlotMap);
  1657. for(pos = 0;pos < context->EffectSlotMap.size;pos++)
  1658. {
  1659. ALeffectslot *slot = context->EffectSlotMap.array[pos].value;
  1660. if(V(slot->EffectState,deviceUpdate)(device) == AL_FALSE)
  1661. {
  1662. UnlockUIntMapRead(&context->EffectSlotMap);
  1663. ALCdevice_Unlock(device);
  1664. RestoreFPUMode(&oldMode);
  1665. return ALC_INVALID_DEVICE;
  1666. }
  1667. ATOMIC_STORE(&slot->NeedsUpdate, AL_FALSE);
  1668. V(slot->EffectState,update)(device, slot);
  1669. }
  1670. UnlockUIntMapRead(&context->EffectSlotMap);
  1671. LockUIntMapRead(&context->SourceMap);
  1672. for(pos = 0;pos < context->SourceMap.size;pos++)
  1673. {
  1674. ALsource *source = context->SourceMap.array[pos].value;
  1675. ALuint s = device->NumAuxSends;
  1676. while(s < MAX_SENDS)
  1677. {
  1678. if(source->Send[s].Slot)
  1679. DecrementRef(&source->Send[s].Slot->ref);
  1680. source->Send[s].Slot = NULL;
  1681. source->Send[s].Gain = 1.0f;
  1682. source->Send[s].GainHF = 1.0f;
  1683. s++;
  1684. }
  1685. ATOMIC_STORE(&source->NeedsUpdate, AL_TRUE);
  1686. }
  1687. UnlockUIntMapRead(&context->SourceMap);
  1688. for(pos = 0;pos < context->VoiceCount;pos++)
  1689. {
  1690. ALvoice *voice = &context->Voices[pos];
  1691. ALsource *source = voice->Source;
  1692. ALuint s = device->NumAuxSends;
  1693. while(s < MAX_SENDS)
  1694. {
  1695. voice->Send[s].Moving = AL_FALSE;
  1696. voice->Send[s].Counter = 0;
  1697. s++;
  1698. }
  1699. if(source)
  1700. {
  1701. ATOMIC_STORE(&source->NeedsUpdate, AL_FALSE);
  1702. voice->Update(voice, source, context);
  1703. }
  1704. }
  1705. context = context->next;
  1706. }
  1707. if(device->DefaultSlot)
  1708. {
  1709. ALeffectslot *slot = device->DefaultSlot;
  1710. if(V(slot->EffectState,deviceUpdate)(device) == AL_FALSE)
  1711. {
  1712. ALCdevice_Unlock(device);
  1713. RestoreFPUMode(&oldMode);
  1714. return ALC_INVALID_DEVICE;
  1715. }
  1716. ATOMIC_STORE(&slot->NeedsUpdate, AL_FALSE);
  1717. V(slot->EffectState,update)(device, slot);
  1718. }
  1719. ALCdevice_Unlock(device);
  1720. RestoreFPUMode(&oldMode);
  1721. if(!(device->Flags&DEVICE_PAUSED))
  1722. {
  1723. if(V0(device->Backend,start)() == ALC_FALSE)
  1724. return ALC_INVALID_DEVICE;
  1725. device->Flags |= DEVICE_RUNNING;
  1726. }
  1727. return ALC_NO_ERROR;
  1728. }
  1729. /* FreeDevice
  1730. *
  1731. * Frees the device structure, and destroys any objects the app failed to
  1732. * delete. Called once there's no more references on the device.
  1733. */
  1734. static ALCvoid FreeDevice(ALCdevice *device)
  1735. {
  1736. TRACE("%p\n", device);
  1737. V0(device->Backend,close)();
  1738. DELETE_OBJ(device->Backend);
  1739. device->Backend = NULL;
  1740. DELETE_OBJ(device->Synth);
  1741. device->Synth = NULL;
  1742. if(device->DefaultSlot)
  1743. {
  1744. ALeffectState *state = device->DefaultSlot->EffectState;
  1745. device->DefaultSlot = NULL;
  1746. DELETE_OBJ(state);
  1747. }
  1748. if(device->DefaultSfont)
  1749. ALsoundfont_deleteSoundfont(device->DefaultSfont, device);
  1750. device->DefaultSfont = NULL;
  1751. if(device->BufferMap.size > 0)
  1752. {
  1753. WARN("(%p) Deleting %d Buffer(s)\n", device, device->BufferMap.size);
  1754. ReleaseALBuffers(device);
  1755. }
  1756. ResetUIntMap(&device->BufferMap);
  1757. if(device->EffectMap.size > 0)
  1758. {
  1759. WARN("(%p) Deleting %d Effect(s)\n", device, device->EffectMap.size);
  1760. ReleaseALEffects(device);
  1761. }
  1762. ResetUIntMap(&device->EffectMap);
  1763. if(device->FilterMap.size > 0)
  1764. {
  1765. WARN("(%p) Deleting %d Filter(s)\n", device, device->FilterMap.size);
  1766. ReleaseALFilters(device);
  1767. }
  1768. ResetUIntMap(&device->FilterMap);
  1769. if(device->SfontMap.size > 0)
  1770. {
  1771. WARN("(%p) Deleting %d Soundfont(s)\n", device, device->SfontMap.size);
  1772. ReleaseALSoundfonts(device);
  1773. }
  1774. ResetUIntMap(&device->SfontMap);
  1775. if(device->PresetMap.size > 0)
  1776. {
  1777. WARN("(%p) Deleting %d Preset(s)\n", device, device->PresetMap.size);
  1778. ReleaseALPresets(device);
  1779. }
  1780. ResetUIntMap(&device->PresetMap);
  1781. if(device->FontsoundMap.size > 0)
  1782. {
  1783. WARN("(%p) Deleting %d Fontsound(s)\n", device, device->FontsoundMap.size);
  1784. ReleaseALFontsounds(device);
  1785. }
  1786. ResetUIntMap(&device->FontsoundMap);
  1787. free(device->Bs2b);
  1788. device->Bs2b = NULL;
  1789. AL_STRING_DEINIT(device->DeviceName);
  1790. al_free(device);
  1791. }
  1792. void ALCdevice_IncRef(ALCdevice *device)
  1793. {
  1794. uint ref;
  1795. ref = IncrementRef(&device->ref);
  1796. TRACEREF("%p increasing refcount to %u\n", device, ref);
  1797. }
  1798. void ALCdevice_DecRef(ALCdevice *device)
  1799. {
  1800. uint ref;
  1801. ref = DecrementRef(&device->ref);
  1802. TRACEREF("%p decreasing refcount to %u\n", device, ref);
  1803. if(ref == 0) FreeDevice(device);
  1804. }
  1805. /* VerifyDevice
  1806. *
  1807. * Checks if the device handle is valid, and increments its ref count if so.
  1808. */
  1809. static ALCdevice *VerifyDevice(ALCdevice *device)
  1810. {
  1811. ALCdevice *tmpDevice;
  1812. if(!device)
  1813. return NULL;
  1814. LockLists();
  1815. tmpDevice = ATOMIC_LOAD(&DeviceList);
  1816. while(tmpDevice && tmpDevice != device)
  1817. tmpDevice = tmpDevice->next;
  1818. if(tmpDevice)
  1819. ALCdevice_IncRef(tmpDevice);
  1820. UnlockLists();
  1821. return tmpDevice;
  1822. }
  1823. /* InitContext
  1824. *
  1825. * Initializes context fields
  1826. */
  1827. static ALvoid InitContext(ALCcontext *Context)
  1828. {
  1829. ALint i, j;
  1830. //Initialise listener
  1831. Context->Listener->Gain = 1.0f;
  1832. Context->Listener->MetersPerUnit = 1.0f;
  1833. Context->Listener->Position[0] = 0.0f;
  1834. Context->Listener->Position[1] = 0.0f;
  1835. Context->Listener->Position[2] = 0.0f;
  1836. Context->Listener->Velocity[0] = 0.0f;
  1837. Context->Listener->Velocity[1] = 0.0f;
  1838. Context->Listener->Velocity[2] = 0.0f;
  1839. Context->Listener->Forward[0] = 0.0f;
  1840. Context->Listener->Forward[1] = 0.0f;
  1841. Context->Listener->Forward[2] = -1.0f;
  1842. Context->Listener->Up[0] = 0.0f;
  1843. Context->Listener->Up[1] = 1.0f;
  1844. Context->Listener->Up[2] = 0.0f;
  1845. for(i = 0;i < 4;i++)
  1846. {
  1847. for(j = 0;j < 4;j++)
  1848. Context->Listener->Params.Matrix[i][j] = ((i==j) ? 1.0f : 0.0f);
  1849. }
  1850. for(i = 0;i < 3;i++)
  1851. Context->Listener->Params.Velocity[i] = 0.0f;
  1852. //Validate Context
  1853. ATOMIC_INIT(&Context->LastError, AL_NO_ERROR);
  1854. ATOMIC_INIT(&Context->UpdateSources, AL_FALSE);
  1855. InitUIntMap(&Context->SourceMap, Context->Device->MaxNoOfSources);
  1856. InitUIntMap(&Context->EffectSlotMap, Context->Device->AuxiliaryEffectSlotMax);
  1857. //Set globals
  1858. Context->DistanceModel = DefaultDistanceModel;
  1859. Context->SourceDistanceModel = AL_FALSE;
  1860. Context->DopplerFactor = 1.0f;
  1861. Context->DopplerVelocity = 1.0f;
  1862. Context->SpeedOfSound = SPEEDOFSOUNDMETRESPERSEC;
  1863. Context->DeferUpdates = AL_FALSE;
  1864. Context->ExtensionList = alExtList;
  1865. }
  1866. /* FreeContext
  1867. *
  1868. * Cleans up the context, and destroys any remaining objects the app failed to
  1869. * delete. Called once there's no more references on the context.
  1870. */
  1871. static void FreeContext(ALCcontext *context)
  1872. {
  1873. TRACE("%p\n", context);
  1874. if(context->SourceMap.size > 0)
  1875. {
  1876. WARN("(%p) Deleting %d Source(s)\n", context, context->SourceMap.size);
  1877. ReleaseALSources(context);
  1878. }
  1879. ResetUIntMap(&context->SourceMap);
  1880. if(context->EffectSlotMap.size > 0)
  1881. {
  1882. WARN("(%p) Deleting %d AuxiliaryEffectSlot(s)\n", context, context->EffectSlotMap.size);
  1883. ReleaseALAuxiliaryEffectSlots(context);
  1884. }
  1885. ResetUIntMap(&context->EffectSlotMap);
  1886. al_free(context->Voices);
  1887. context->Voices = NULL;
  1888. context->VoiceCount = 0;
  1889. context->MaxVoices = 0;
  1890. VECTOR_DEINIT(context->ActiveAuxSlots);
  1891. ALCdevice_DecRef(context->Device);
  1892. context->Device = NULL;
  1893. //Invalidate context
  1894. memset(context, 0, sizeof(ALCcontext));
  1895. al_free(context);
  1896. }
  1897. /* ReleaseContext
  1898. *
  1899. * Removes the context reference from the given device and removes it from
  1900. * being current on the running thread or globally.
  1901. */
  1902. static void ReleaseContext(ALCcontext *context, ALCdevice *device)
  1903. {
  1904. ALCcontext *nextctx;
  1905. ALCcontext *origctx;
  1906. if(altss_get(LocalContext) == context)
  1907. {
  1908. WARN("%p released while current on thread\n", context);
  1909. altss_set(LocalContext, NULL);
  1910. ALCcontext_DecRef(context);
  1911. }
  1912. origctx = context;
  1913. if(ATOMIC_COMPARE_EXCHANGE_STRONG(ALCcontext*, &GlobalContext, &origctx, NULL))
  1914. ALCcontext_DecRef(context);
  1915. ALCdevice_Lock(device);
  1916. origctx = context;
  1917. nextctx = context->next;
  1918. if(!ATOMIC_COMPARE_EXCHANGE_STRONG(ALCcontext*, &device->ContextList, &origctx, nextctx))
  1919. {
  1920. ALCcontext *list;
  1921. do {
  1922. list = origctx;
  1923. origctx = context;
  1924. } while(!COMPARE_EXCHANGE(&list->next, &origctx, nextctx));
  1925. }
  1926. ALCdevice_Unlock(device);
  1927. ALCcontext_DecRef(context);
  1928. }
  1929. void ALCcontext_IncRef(ALCcontext *context)
  1930. {
  1931. uint ref;
  1932. ref = IncrementRef(&context->ref);
  1933. TRACEREF("%p increasing refcount to %u\n", context, ref);
  1934. }
  1935. void ALCcontext_DecRef(ALCcontext *context)
  1936. {
  1937. uint ref;
  1938. ref = DecrementRef(&context->ref);
  1939. TRACEREF("%p decreasing refcount to %u\n", context, ref);
  1940. if(ref == 0) FreeContext(context);
  1941. }
  1942. static void ReleaseThreadCtx(void *ptr)
  1943. {
  1944. WARN("%p current for thread being destroyed\n", ptr);
  1945. ALCcontext_DecRef(ptr);
  1946. }
  1947. /* VerifyContext
  1948. *
  1949. * Checks that the given context is valid, and increments its reference count.
  1950. */
  1951. static ALCcontext *VerifyContext(ALCcontext *context)
  1952. {
  1953. ALCdevice *dev;
  1954. LockLists();
  1955. dev = ATOMIC_LOAD(&DeviceList);
  1956. while(dev)
  1957. {
  1958. ALCcontext *ctx = ATOMIC_LOAD(&dev->ContextList);
  1959. while(ctx)
  1960. {
  1961. if(ctx == context)
  1962. {
  1963. ALCcontext_IncRef(ctx);
  1964. UnlockLists();
  1965. return ctx;
  1966. }
  1967. ctx = ctx->next;
  1968. }
  1969. dev = dev->next;
  1970. }
  1971. UnlockLists();
  1972. return NULL;
  1973. }
  1974. /* GetContextRef
  1975. *
  1976. * Returns the currently active context for this thread, and adds a reference
  1977. * without locking it.
  1978. */
  1979. ALCcontext *GetContextRef(void)
  1980. {
  1981. ALCcontext *context;
  1982. context = altss_get(LocalContext);
  1983. if(context)
  1984. ALCcontext_IncRef(context);
  1985. else
  1986. {
  1987. LockLists();
  1988. context = ATOMIC_LOAD(&GlobalContext);
  1989. if(context)
  1990. ALCcontext_IncRef(context);
  1991. UnlockLists();
  1992. }
  1993. return context;
  1994. }
  1995. /************************************************
  1996. * Standard ALC functions
  1997. ************************************************/
  1998. /* alcGetError
  1999. *
  2000. * Return last ALC generated error code for the given device
  2001. */
  2002. ALC_API ALCenum ALC_APIENTRY alcGetError(ALCdevice *device)
  2003. {
  2004. ALCenum errorCode;
  2005. if(VerifyDevice(device))
  2006. {
  2007. errorCode = ATOMIC_EXCHANGE(ALCenum, &device->LastError, ALC_NO_ERROR);
  2008. ALCdevice_DecRef(device);
  2009. }
  2010. else
  2011. errorCode = ATOMIC_EXCHANGE(ALCenum, &LastNullDeviceError, ALC_NO_ERROR);
  2012. return errorCode;
  2013. }
  2014. /* alcSuspendContext
  2015. *
  2016. * Not functional
  2017. */
  2018. ALC_API ALCvoid ALC_APIENTRY alcSuspendContext(ALCcontext *context)
  2019. {
  2020. if (!SuspendAndProcessSupported) return;
  2021. ALCdevice *Device;
  2022. LockLists();
  2023. /* alcGetContextsDevice sets an error for invalid contexts */
  2024. Device = alcGetContextsDevice(context);
  2025. if (Device && (Device->Flags & DEVICE_RUNNING))
  2026. {
  2027. V0(Device->Backend, stop)();
  2028. Device->Flags &= ~DEVICE_RUNNING;
  2029. }
  2030. UnlockLists();
  2031. }
  2032. /* alcProcessContext
  2033. *
  2034. * Not functional
  2035. */
  2036. ALC_API ALCvoid ALC_APIENTRY alcProcessContext(ALCcontext *context)
  2037. {
  2038. if (!SuspendAndProcessSupported) return;
  2039. ALCdevice *Device;
  2040. LockLists();
  2041. /* alcGetContextsDevice sets an error for invalid contexts */
  2042. Device = alcGetContextsDevice(context);
  2043. if (Device && !(Device->Flags & DEVICE_RUNNING))
  2044. {
  2045. V0(Device->Backend, start)();
  2046. Device->Flags |= DEVICE_RUNNING;
  2047. }
  2048. UnlockLists();
  2049. }
  2050. /* alcGetString
  2051. *
  2052. * Returns information about the device, and error strings
  2053. */
  2054. ALC_API const ALCchar* ALC_APIENTRY alcGetString(ALCdevice *Device, ALCenum param)
  2055. {
  2056. const ALCchar *value = NULL;
  2057. switch(param)
  2058. {
  2059. case ALC_NO_ERROR:
  2060. value = alcNoError;
  2061. break;
  2062. case ALC_INVALID_ENUM:
  2063. value = alcErrInvalidEnum;
  2064. break;
  2065. case ALC_INVALID_VALUE:
  2066. value = alcErrInvalidValue;
  2067. break;
  2068. case ALC_INVALID_DEVICE:
  2069. value = alcErrInvalidDevice;
  2070. break;
  2071. case ALC_INVALID_CONTEXT:
  2072. value = alcErrInvalidContext;
  2073. break;
  2074. case ALC_OUT_OF_MEMORY:
  2075. value = alcErrOutOfMemory;
  2076. break;
  2077. case ALC_DEVICE_SPECIFIER:
  2078. value = alcDefaultName;
  2079. break;
  2080. case ALC_ALL_DEVICES_SPECIFIER:
  2081. if(VerifyDevice(Device))
  2082. {
  2083. value = al_string_get_cstr(Device->DeviceName);
  2084. ALCdevice_DecRef(Device);
  2085. }
  2086. else
  2087. {
  2088. ProbeAllDevicesList();
  2089. value = al_string_get_cstr(alcAllDevicesList);
  2090. }
  2091. break;
  2092. case ALC_CAPTURE_DEVICE_SPECIFIER:
  2093. if(VerifyDevice(Device))
  2094. {
  2095. value = al_string_get_cstr(Device->DeviceName);
  2096. ALCdevice_DecRef(Device);
  2097. }
  2098. else
  2099. {
  2100. ProbeCaptureDeviceList();
  2101. value = al_string_get_cstr(alcCaptureDeviceList);
  2102. }
  2103. break;
  2104. /* Default devices are always first in the list */
  2105. case ALC_DEFAULT_DEVICE_SPECIFIER:
  2106. value = alcDefaultName;
  2107. break;
  2108. case ALC_DEFAULT_ALL_DEVICES_SPECIFIER:
  2109. if(al_string_empty(alcAllDevicesList))
  2110. ProbeAllDevicesList();
  2111. Device = VerifyDevice(Device);
  2112. free(alcDefaultAllDevicesSpecifier);
  2113. alcDefaultAllDevicesSpecifier = strdup(al_string_get_cstr(alcAllDevicesList));
  2114. if(!alcDefaultAllDevicesSpecifier)
  2115. alcSetError(Device, ALC_OUT_OF_MEMORY);
  2116. value = alcDefaultAllDevicesSpecifier;
  2117. if(Device) ALCdevice_DecRef(Device);
  2118. break;
  2119. case ALC_CAPTURE_DEFAULT_DEVICE_SPECIFIER:
  2120. if(al_string_empty(alcCaptureDeviceList))
  2121. ProbeCaptureDeviceList();
  2122. Device = VerifyDevice(Device);
  2123. free(alcCaptureDefaultDeviceSpecifier);
  2124. alcCaptureDefaultDeviceSpecifier = strdup(al_string_get_cstr(alcCaptureDeviceList));
  2125. if(!alcCaptureDefaultDeviceSpecifier)
  2126. alcSetError(Device, ALC_OUT_OF_MEMORY);
  2127. value = alcCaptureDefaultDeviceSpecifier;
  2128. if(Device) ALCdevice_DecRef(Device);
  2129. break;
  2130. case ALC_EXTENSIONS:
  2131. if(!VerifyDevice(Device))
  2132. value = alcNoDeviceExtList;
  2133. else
  2134. {
  2135. value = alcExtensionList;
  2136. ALCdevice_DecRef(Device);
  2137. }
  2138. break;
  2139. default:
  2140. Device = VerifyDevice(Device);
  2141. alcSetError(Device, ALC_INVALID_ENUM);
  2142. if(Device) ALCdevice_DecRef(Device);
  2143. break;
  2144. }
  2145. return value;
  2146. }
  2147. static ALCsizei GetIntegerv(ALCdevice *device, ALCenum param, ALCsizei size, ALCint *values)
  2148. {
  2149. ALCsizei i;
  2150. if(size <= 0 || values == NULL)
  2151. {
  2152. alcSetError(device, ALC_INVALID_VALUE);
  2153. return 0;
  2154. }
  2155. if(!device)
  2156. {
  2157. switch(param)
  2158. {
  2159. case ALC_MAJOR_VERSION:
  2160. values[0] = alcMajorVersion;
  2161. return 1;
  2162. case ALC_MINOR_VERSION:
  2163. values[0] = alcMinorVersion;
  2164. return 1;
  2165. case ALC_ATTRIBUTES_SIZE:
  2166. case ALC_ALL_ATTRIBUTES:
  2167. case ALC_FREQUENCY:
  2168. case ALC_REFRESH:
  2169. case ALC_SYNC:
  2170. case ALC_MONO_SOURCES:
  2171. case ALC_STEREO_SOURCES:
  2172. case ALC_CAPTURE_SAMPLES:
  2173. case ALC_FORMAT_CHANNELS_SOFT:
  2174. case ALC_FORMAT_TYPE_SOFT:
  2175. alcSetError(NULL, ALC_INVALID_DEVICE);
  2176. return 0;
  2177. default:
  2178. alcSetError(NULL, ALC_INVALID_ENUM);
  2179. return 0;
  2180. }
  2181. return 0;
  2182. }
  2183. if(device->Type == Capture)
  2184. {
  2185. switch(param)
  2186. {
  2187. case ALC_CAPTURE_SAMPLES:
  2188. ALCdevice_Lock(device);
  2189. values[0] = V0(device->Backend,availableSamples)();
  2190. ALCdevice_Unlock(device);
  2191. return 1;
  2192. case ALC_CONNECTED:
  2193. values[0] = device->Connected;
  2194. return 1;
  2195. default:
  2196. alcSetError(device, ALC_INVALID_ENUM);
  2197. return 0;
  2198. }
  2199. return 0;
  2200. }
  2201. /* render device */
  2202. switch(param)
  2203. {
  2204. case ALC_MAJOR_VERSION:
  2205. values[0] = alcMajorVersion;
  2206. return 1;
  2207. case ALC_MINOR_VERSION:
  2208. values[0] = alcMinorVersion;
  2209. return 1;
  2210. case ALC_EFX_MAJOR_VERSION:
  2211. values[0] = alcEFXMajorVersion;
  2212. return 1;
  2213. case ALC_EFX_MINOR_VERSION:
  2214. values[0] = alcEFXMinorVersion;
  2215. return 1;
  2216. case ALC_ATTRIBUTES_SIZE:
  2217. values[0] = 15;
  2218. return 1;
  2219. case ALC_ALL_ATTRIBUTES:
  2220. if(size < 15)
  2221. {
  2222. alcSetError(device, ALC_INVALID_VALUE);
  2223. return 0;
  2224. }
  2225. i = 0;
  2226. values[i++] = ALC_FREQUENCY;
  2227. values[i++] = device->Frequency;
  2228. if(device->Type != Loopback)
  2229. {
  2230. values[i++] = ALC_REFRESH;
  2231. values[i++] = device->Frequency / device->UpdateSize;
  2232. values[i++] = ALC_SYNC;
  2233. values[i++] = ALC_FALSE;
  2234. }
  2235. else
  2236. {
  2237. values[i++] = ALC_FORMAT_CHANNELS_SOFT;
  2238. values[i++] = device->FmtChans;
  2239. values[i++] = ALC_FORMAT_TYPE_SOFT;
  2240. values[i++] = device->FmtType;
  2241. }
  2242. values[i++] = ALC_MONO_SOURCES;
  2243. values[i++] = device->NumMonoSources;
  2244. values[i++] = ALC_STEREO_SOURCES;
  2245. values[i++] = device->NumStereoSources;
  2246. values[i++] = ALC_MAX_AUXILIARY_SENDS;
  2247. values[i++] = device->NumAuxSends;
  2248. values[i++] = ALC_HRTF_SOFT;
  2249. values[i++] = (device->Hrtf ? ALC_TRUE : ALC_FALSE);
  2250. values[i++] = 0;
  2251. return i;
  2252. case ALC_FREQUENCY:
  2253. values[0] = device->Frequency;
  2254. return 1;
  2255. case ALC_REFRESH:
  2256. if(device->Type == Loopback)
  2257. {
  2258. alcSetError(device, ALC_INVALID_DEVICE);
  2259. return 0;
  2260. }
  2261. values[0] = device->Frequency / device->UpdateSize;
  2262. return 1;
  2263. case ALC_SYNC:
  2264. if(device->Type == Loopback)
  2265. {
  2266. alcSetError(device, ALC_INVALID_DEVICE);
  2267. return 0;
  2268. }
  2269. values[0] = ALC_FALSE;
  2270. return 1;
  2271. case ALC_FORMAT_CHANNELS_SOFT:
  2272. if(device->Type != Loopback)
  2273. {
  2274. alcSetError(device, ALC_INVALID_DEVICE);
  2275. return 0;
  2276. }
  2277. values[0] = device->FmtChans;
  2278. return 1;
  2279. case ALC_FORMAT_TYPE_SOFT:
  2280. if(device->Type != Loopback)
  2281. {
  2282. alcSetError(device, ALC_INVALID_DEVICE);
  2283. return 0;
  2284. }
  2285. values[0] = device->FmtType;
  2286. return 1;
  2287. case ALC_MONO_SOURCES:
  2288. values[0] = device->NumMonoSources;
  2289. return 1;
  2290. case ALC_STEREO_SOURCES:
  2291. values[0] = device->NumStereoSources;
  2292. return 1;
  2293. case ALC_MAX_AUXILIARY_SENDS:
  2294. values[0] = device->NumAuxSends;
  2295. return 1;
  2296. case ALC_CONNECTED:
  2297. values[0] = device->Connected;
  2298. return 1;
  2299. case ALC_HRTF_SOFT:
  2300. values[0] = (device->Hrtf ? ALC_TRUE : ALC_FALSE);
  2301. return 1;
  2302. default:
  2303. alcSetError(device, ALC_INVALID_ENUM);
  2304. return 0;
  2305. }
  2306. return 0;
  2307. }
  2308. /* alcGetIntegerv
  2309. *
  2310. * Returns information about the device and the version of OpenAL
  2311. */
  2312. ALC_API void ALC_APIENTRY alcGetIntegerv(ALCdevice *device, ALCenum param, ALCsizei size, ALCint *values)
  2313. {
  2314. device = VerifyDevice(device);
  2315. if(size <= 0 || values == NULL)
  2316. alcSetError(device, ALC_INVALID_VALUE);
  2317. else
  2318. GetIntegerv(device, param, size, values);
  2319. if(device) ALCdevice_DecRef(device);
  2320. }
  2321. ALC_API void ALC_APIENTRY alcGetInteger64vSOFT(ALCdevice *device, ALCenum pname, ALCsizei size, ALCint64SOFT *values)
  2322. {
  2323. ALCint *ivals;
  2324. ALsizei i;
  2325. device = VerifyDevice(device);
  2326. if(size <= 0 || values == NULL)
  2327. alcSetError(device, ALC_INVALID_VALUE);
  2328. else if(!device || device->Type == Capture)
  2329. {
  2330. ivals = malloc(size * sizeof(ALCint));
  2331. size = GetIntegerv(device, pname, size, ivals);
  2332. for(i = 0;i < size;i++)
  2333. values[i] = ivals[i];
  2334. free(ivals);
  2335. }
  2336. else /* render device */
  2337. {
  2338. switch(pname)
  2339. {
  2340. case ALC_ATTRIBUTES_SIZE:
  2341. *values = 17;
  2342. break;
  2343. case ALC_ALL_ATTRIBUTES:
  2344. if(size < 17)
  2345. alcSetError(device, ALC_INVALID_VALUE);
  2346. else
  2347. {
  2348. int i = 0;
  2349. V0(device->Backend,lock)();
  2350. values[i++] = ALC_FREQUENCY;
  2351. values[i++] = device->Frequency;
  2352. if(device->Type != Loopback)
  2353. {
  2354. values[i++] = ALC_REFRESH;
  2355. values[i++] = device->Frequency / device->UpdateSize;
  2356. values[i++] = ALC_SYNC;
  2357. values[i++] = ALC_FALSE;
  2358. }
  2359. else
  2360. {
  2361. values[i++] = ALC_FORMAT_CHANNELS_SOFT;
  2362. values[i++] = device->FmtChans;
  2363. values[i++] = ALC_FORMAT_TYPE_SOFT;
  2364. values[i++] = device->FmtType;
  2365. }
  2366. values[i++] = ALC_MONO_SOURCES;
  2367. values[i++] = device->NumMonoSources;
  2368. values[i++] = ALC_STEREO_SOURCES;
  2369. values[i++] = device->NumStereoSources;
  2370. values[i++] = ALC_MAX_AUXILIARY_SENDS;
  2371. values[i++] = device->NumAuxSends;
  2372. values[i++] = ALC_HRTF_SOFT;
  2373. values[i++] = (device->Hrtf ? ALC_TRUE : ALC_FALSE);
  2374. values[i++] = ALC_DEVICE_CLOCK_SOFT;
  2375. values[i++] = device->ClockBase +
  2376. (device->SamplesDone * DEVICE_CLOCK_RES / device->Frequency);
  2377. values[i++] = 0;
  2378. V0(device->Backend,unlock)();
  2379. }
  2380. break;
  2381. case ALC_DEVICE_CLOCK_SOFT:
  2382. V0(device->Backend,lock)();
  2383. *values = device->ClockBase +
  2384. (device->SamplesDone * DEVICE_CLOCK_RES / device->Frequency);
  2385. V0(device->Backend,unlock)();
  2386. break;
  2387. default:
  2388. ivals = malloc(size * sizeof(ALCint));
  2389. size = GetIntegerv(device, pname, size, ivals);
  2390. for(i = 0;i < size;i++)
  2391. values[i] = ivals[i];
  2392. free(ivals);
  2393. break;
  2394. }
  2395. }
  2396. if(device)
  2397. ALCdevice_DecRef(device);
  2398. }
  2399. /* alcIsExtensionPresent
  2400. *
  2401. * Determines if there is support for a particular extension
  2402. */
  2403. ALC_API ALCboolean ALC_APIENTRY alcIsExtensionPresent(ALCdevice *device, const ALCchar *extName)
  2404. {
  2405. ALCboolean bResult = ALC_FALSE;
  2406. device = VerifyDevice(device);
  2407. if(!extName)
  2408. alcSetError(device, ALC_INVALID_VALUE);
  2409. else
  2410. {
  2411. size_t len = strlen(extName);
  2412. const char *ptr = (device ? alcExtensionList : alcNoDeviceExtList);
  2413. while(ptr && *ptr)
  2414. {
  2415. if(strncasecmp(ptr, extName, len) == 0 &&
  2416. (ptr[len] == '\0' || isspace(ptr[len])))
  2417. {
  2418. bResult = ALC_TRUE;
  2419. break;
  2420. }
  2421. if((ptr=strchr(ptr, ' ')) != NULL)
  2422. {
  2423. do {
  2424. ++ptr;
  2425. } while(isspace(*ptr));
  2426. }
  2427. }
  2428. }
  2429. if(device)
  2430. ALCdevice_DecRef(device);
  2431. return bResult;
  2432. }
  2433. /* alcGetProcAddress
  2434. *
  2435. * Retrieves the function address for a particular extension function
  2436. */
  2437. ALC_API ALCvoid* ALC_APIENTRY alcGetProcAddress(ALCdevice *device, const ALCchar *funcName)
  2438. {
  2439. ALCvoid *ptr = NULL;
  2440. if(!funcName)
  2441. {
  2442. device = VerifyDevice(device);
  2443. alcSetError(device, ALC_INVALID_VALUE);
  2444. if(device) ALCdevice_DecRef(device);
  2445. }
  2446. else
  2447. {
  2448. ALsizei i = 0;
  2449. while(alcFunctions[i].funcName && strcmp(alcFunctions[i].funcName, funcName) != 0)
  2450. i++;
  2451. ptr = alcFunctions[i].address;
  2452. }
  2453. return ptr;
  2454. }
  2455. /* alcGetEnumValue
  2456. *
  2457. * Get the value for a particular ALC enumeration name
  2458. */
  2459. ALC_API ALCenum ALC_APIENTRY alcGetEnumValue(ALCdevice *device, const ALCchar *enumName)
  2460. {
  2461. ALCenum val = 0;
  2462. if(!enumName)
  2463. {
  2464. device = VerifyDevice(device);
  2465. alcSetError(device, ALC_INVALID_VALUE);
  2466. if(device) ALCdevice_DecRef(device);
  2467. }
  2468. else
  2469. {
  2470. ALsizei i = 0;
  2471. while(enumeration[i].enumName && strcmp(enumeration[i].enumName, enumName) != 0)
  2472. i++;
  2473. val = enumeration[i].value;
  2474. }
  2475. return val;
  2476. }
  2477. /* alcCreateContext
  2478. *
  2479. * Create and attach a context to the given device.
  2480. */
  2481. ALC_API ALCcontext* ALC_APIENTRY alcCreateContext(ALCdevice *device, const ALCint *attrList)
  2482. {
  2483. ALCcontext *ALContext;
  2484. ALCenum err;
  2485. LockLists();
  2486. if(!(device=VerifyDevice(device)) || device->Type == Capture || !device->Connected)
  2487. {
  2488. UnlockLists();
  2489. alcSetError(device, ALC_INVALID_DEVICE);
  2490. if(device) ALCdevice_DecRef(device);
  2491. return NULL;
  2492. }
  2493. ATOMIC_STORE(&device->LastError, ALC_NO_ERROR);
  2494. if((err=UpdateDeviceParams(device, attrList)) != ALC_NO_ERROR)
  2495. {
  2496. UnlockLists();
  2497. alcSetError(device, err);
  2498. if(err == ALC_INVALID_DEVICE)
  2499. {
  2500. ALCdevice_Lock(device);
  2501. aluHandleDisconnect(device);
  2502. ALCdevice_Unlock(device);
  2503. }
  2504. ALCdevice_DecRef(device);
  2505. return NULL;
  2506. }
  2507. ALContext = al_calloc(16, sizeof(ALCcontext)+sizeof(ALlistener));
  2508. if(ALContext)
  2509. {
  2510. InitRef(&ALContext->ref, 1);
  2511. ALContext->Listener = (ALlistener*)ALContext->_listener_mem;
  2512. VECTOR_INIT(ALContext->ActiveAuxSlots);
  2513. ALContext->VoiceCount = 0;
  2514. ALContext->MaxVoices = 256;
  2515. ALContext->Voices = al_calloc(16, ALContext->MaxVoices * sizeof(ALContext->Voices[0]));
  2516. }
  2517. if(!ALContext || !ALContext->Voices)
  2518. {
  2519. if(!ATOMIC_LOAD(&device->ContextList))
  2520. {
  2521. V0(device->Backend,stop)();
  2522. device->Flags &= ~DEVICE_RUNNING;
  2523. }
  2524. UnlockLists();
  2525. if(ALContext)
  2526. {
  2527. al_free(ALContext->Voices);
  2528. ALContext->Voices = NULL;
  2529. VECTOR_DEINIT(ALContext->ActiveAuxSlots);
  2530. al_free(ALContext);
  2531. ALContext = NULL;
  2532. }
  2533. alcSetError(device, ALC_OUT_OF_MEMORY);
  2534. ALCdevice_DecRef(device);
  2535. return NULL;
  2536. }
  2537. ALContext->Device = device;
  2538. ALCdevice_IncRef(device);
  2539. InitContext(ALContext);
  2540. {
  2541. ALCcontext *head = ATOMIC_LOAD(&device->ContextList);
  2542. do {
  2543. ALContext->next = head;
  2544. } while(!ATOMIC_COMPARE_EXCHANGE_WEAK(ALCcontext*, &device->ContextList, &head, ALContext));
  2545. }
  2546. UnlockLists();
  2547. ALCdevice_DecRef(device);
  2548. TRACE("Created context %p\n", ALContext);
  2549. return ALContext;
  2550. }
  2551. /* alcDestroyContext
  2552. *
  2553. * Remove a context from its device
  2554. */
  2555. ALC_API ALCvoid ALC_APIENTRY alcDestroyContext(ALCcontext *context)
  2556. {
  2557. ALCdevice *Device;
  2558. LockLists();
  2559. /* alcGetContextsDevice sets an error for invalid contexts */
  2560. Device = alcGetContextsDevice(context);
  2561. if(Device)
  2562. {
  2563. ReleaseContext(context, Device);
  2564. if(!ATOMIC_LOAD(&Device->ContextList))
  2565. {
  2566. V0(Device->Backend,stop)();
  2567. Device->Flags &= ~DEVICE_RUNNING;
  2568. }
  2569. }
  2570. UnlockLists();
  2571. }
  2572. /* alcGetCurrentContext
  2573. *
  2574. * Returns the currently active context on the calling thread
  2575. */
  2576. ALC_API ALCcontext* ALC_APIENTRY alcGetCurrentContext(void)
  2577. {
  2578. ALCcontext *Context = altss_get(LocalContext);
  2579. if(!Context) Context = ATOMIC_LOAD(&GlobalContext);
  2580. return Context;
  2581. }
  2582. /* alcGetThreadContext
  2583. *
  2584. * Returns the currently active thread-local context
  2585. */
  2586. ALC_API ALCcontext* ALC_APIENTRY alcGetThreadContext(void)
  2587. {
  2588. return altss_get(LocalContext);
  2589. }
  2590. /* alcMakeContextCurrent
  2591. *
  2592. * Makes the given context the active process-wide context, and removes the
  2593. * thread-local context for the calling thread.
  2594. */
  2595. ALC_API ALCboolean ALC_APIENTRY alcMakeContextCurrent(ALCcontext *context)
  2596. {
  2597. /* context must be valid or NULL */
  2598. if(context && !(context=VerifyContext(context)))
  2599. {
  2600. alcSetError(NULL, ALC_INVALID_CONTEXT);
  2601. return ALC_FALSE;
  2602. }
  2603. /* context's reference count is already incremented */
  2604. context = ATOMIC_EXCHANGE(ALCcontext*, &GlobalContext, context);
  2605. if(context) ALCcontext_DecRef(context);
  2606. if((context=altss_get(LocalContext)) != NULL)
  2607. {
  2608. altss_set(LocalContext, NULL);
  2609. ALCcontext_DecRef(context);
  2610. }
  2611. return ALC_TRUE;
  2612. }
  2613. /* alcSetThreadContext
  2614. *
  2615. * Makes the given context the active context for the current thread
  2616. */
  2617. ALC_API ALCboolean ALC_APIENTRY alcSetThreadContext(ALCcontext *context)
  2618. {
  2619. ALCcontext *old;
  2620. /* context must be valid or NULL */
  2621. if(context && !(context=VerifyContext(context)))
  2622. {
  2623. alcSetError(NULL, ALC_INVALID_CONTEXT);
  2624. return ALC_FALSE;
  2625. }
  2626. /* context's reference count is already incremented */
  2627. old = altss_get(LocalContext);
  2628. altss_set(LocalContext, context);
  2629. if(old) ALCcontext_DecRef(old);
  2630. return ALC_TRUE;
  2631. }
  2632. /* alcGetContextsDevice
  2633. *
  2634. * Returns the device that a particular context is attached to
  2635. */
  2636. ALC_API ALCdevice* ALC_APIENTRY alcGetContextsDevice(ALCcontext *Context)
  2637. {
  2638. ALCdevice *Device;
  2639. if(!(Context=VerifyContext(Context)))
  2640. {
  2641. alcSetError(NULL, ALC_INVALID_CONTEXT);
  2642. return NULL;
  2643. }
  2644. Device = Context->Device;
  2645. ALCcontext_DecRef(Context);
  2646. return Device;
  2647. }
  2648. /* alcOpenDevice
  2649. *
  2650. * Opens the named device.
  2651. */
  2652. ALC_API ALCdevice* ALC_APIENTRY alcOpenDevice(const ALCchar *deviceName)
  2653. {
  2654. const ALCchar *fmt;
  2655. ALCdevice *device;
  2656. ALCenum err;
  2657. DO_INITCONFIG();
  2658. if(!PlaybackBackend.name)
  2659. {
  2660. alcSetError(NULL, ALC_INVALID_VALUE);
  2661. return NULL;
  2662. }
  2663. SuspendAndProcessSupported = strcasecmp(PlaybackBackend.name, "mmdevapi") == 0;
  2664. if(deviceName && (!deviceName[0] || strcasecmp(deviceName, alcDefaultName) == 0 || strcasecmp(deviceName, "openal-soft") == 0))
  2665. deviceName = NULL;
  2666. device = al_calloc(16, sizeof(ALCdevice)+sizeof(ALeffectslot));
  2667. if(!device)
  2668. {
  2669. alcSetError(NULL, ALC_OUT_OF_MEMORY);
  2670. return NULL;
  2671. }
  2672. //Validate device
  2673. InitRef(&device->ref, 1);
  2674. device->Connected = ALC_TRUE;
  2675. device->Type = Playback;
  2676. ATOMIC_INIT(&device->LastError, ALC_NO_ERROR);
  2677. device->Flags = 0;
  2678. device->Bs2b = NULL;
  2679. device->Bs2bLevel = 0;
  2680. AL_STRING_INIT(device->DeviceName);
  2681. ATOMIC_INIT(&device->ContextList, NULL);
  2682. device->ClockBase = 0;
  2683. device->SamplesDone = 0;
  2684. device->MaxNoOfSources = 256;
  2685. device->AuxiliaryEffectSlotMax = 4;
  2686. device->NumAuxSends = MAX_SENDS;
  2687. InitUIntMap(&device->BufferMap, ~0);
  2688. InitUIntMap(&device->EffectMap, ~0);
  2689. InitUIntMap(&device->FilterMap, ~0);
  2690. InitUIntMap(&device->SfontMap, ~0);
  2691. InitUIntMap(&device->PresetMap, ~0);
  2692. InitUIntMap(&device->FontsoundMap, ~0);
  2693. //Set output format
  2694. device->FmtChans = DevFmtChannelsDefault;
  2695. device->FmtType = DevFmtTypeDefault;
  2696. device->Frequency = DEFAULT_OUTPUT_RATE;
  2697. device->NumUpdates = 4;
  2698. device->UpdateSize = 1024;
  2699. if(!PlaybackBackend.getFactory)
  2700. device->Backend = create_backend_wrapper(device, &PlaybackBackend.Funcs,
  2701. ALCbackend_Playback);
  2702. else
  2703. {
  2704. ALCbackendFactory *factory = PlaybackBackend.getFactory();
  2705. device->Backend = V(factory,createBackend)(device, ALCbackend_Playback);
  2706. }
  2707. if(!device->Backend)
  2708. {
  2709. al_free(device);
  2710. alcSetError(NULL, ALC_OUT_OF_MEMORY);
  2711. return NULL;
  2712. }
  2713. if(ConfigValueStr(NULL, "channels", &fmt))
  2714. {
  2715. static const struct {
  2716. const char name[16];
  2717. enum DevFmtChannels chans;
  2718. } chanlist[] = {
  2719. { "mono", DevFmtMono },
  2720. { "stereo", DevFmtStereo },
  2721. { "quad", DevFmtQuad },
  2722. { "surround51", DevFmtX51 },
  2723. { "surround61", DevFmtX61 },
  2724. { "surround71", DevFmtX71 },
  2725. };
  2726. size_t i;
  2727. for(i = 0;i < COUNTOF(chanlist);i++)
  2728. {
  2729. if(strcasecmp(chanlist[i].name, fmt) == 0)
  2730. {
  2731. device->FmtChans = chanlist[i].chans;
  2732. device->Flags |= DEVICE_CHANNELS_REQUEST;
  2733. break;
  2734. }
  2735. }
  2736. if(i == COUNTOF(chanlist))
  2737. ERR("Unsupported channels: %s\n", fmt);
  2738. }
  2739. if(ConfigValueStr(NULL, "sample-type", &fmt))
  2740. {
  2741. static const struct {
  2742. const char name[16];
  2743. enum DevFmtType type;
  2744. } typelist[] = {
  2745. { "int8", DevFmtByte },
  2746. { "uint8", DevFmtUByte },
  2747. { "int16", DevFmtShort },
  2748. { "uint16", DevFmtUShort },
  2749. { "int32", DevFmtInt },
  2750. { "uint32", DevFmtUInt },
  2751. { "float32", DevFmtFloat },
  2752. };
  2753. size_t i;
  2754. for(i = 0;i < COUNTOF(typelist);i++)
  2755. {
  2756. if(strcasecmp(typelist[i].name, fmt) == 0)
  2757. {
  2758. device->FmtType = typelist[i].type;
  2759. device->Flags |= DEVICE_SAMPLE_TYPE_REQUEST;
  2760. break;
  2761. }
  2762. }
  2763. if(i == COUNTOF(typelist))
  2764. ERR("Unsupported sample-type: %s\n", fmt);
  2765. }
  2766. #define DEVICE_FORMAT_REQUEST (DEVICE_CHANNELS_REQUEST|DEVICE_SAMPLE_TYPE_REQUEST)
  2767. if((device->Flags&DEVICE_FORMAT_REQUEST) != DEVICE_FORMAT_REQUEST &&
  2768. ConfigValueStr(NULL, "format", &fmt))
  2769. {
  2770. static const struct {
  2771. const char name[32];
  2772. enum DevFmtChannels channels;
  2773. enum DevFmtType type;
  2774. } formats[] = {
  2775. { "AL_FORMAT_MONO32", DevFmtMono, DevFmtFloat },
  2776. { "AL_FORMAT_STEREO32", DevFmtStereo, DevFmtFloat },
  2777. { "AL_FORMAT_QUAD32", DevFmtQuad, DevFmtFloat },
  2778. { "AL_FORMAT_51CHN32", DevFmtX51, DevFmtFloat },
  2779. { "AL_FORMAT_61CHN32", DevFmtX61, DevFmtFloat },
  2780. { "AL_FORMAT_71CHN32", DevFmtX71, DevFmtFloat },
  2781. { "AL_FORMAT_MONO16", DevFmtMono, DevFmtShort },
  2782. { "AL_FORMAT_STEREO16", DevFmtStereo, DevFmtShort },
  2783. { "AL_FORMAT_QUAD16", DevFmtQuad, DevFmtShort },
  2784. { "AL_FORMAT_51CHN16", DevFmtX51, DevFmtShort },
  2785. { "AL_FORMAT_61CHN16", DevFmtX61, DevFmtShort },
  2786. { "AL_FORMAT_71CHN16", DevFmtX71, DevFmtShort },
  2787. { "AL_FORMAT_MONO8", DevFmtMono, DevFmtByte },
  2788. { "AL_FORMAT_STEREO8", DevFmtStereo, DevFmtByte },
  2789. { "AL_FORMAT_QUAD8", DevFmtQuad, DevFmtByte },
  2790. { "AL_FORMAT_51CHN8", DevFmtX51, DevFmtByte },
  2791. { "AL_FORMAT_61CHN8", DevFmtX61, DevFmtByte },
  2792. { "AL_FORMAT_71CHN8", DevFmtX71, DevFmtByte }
  2793. };
  2794. size_t i;
  2795. ERR("Option 'format' is deprecated, please use 'channels' and 'sample-type'\n");
  2796. for(i = 0;i < COUNTOF(formats);i++)
  2797. {
  2798. if(strcasecmp(fmt, formats[i].name) == 0)
  2799. {
  2800. if(!(device->Flags&DEVICE_CHANNELS_REQUEST))
  2801. device->FmtChans = formats[i].channels;
  2802. if(!(device->Flags&DEVICE_SAMPLE_TYPE_REQUEST))
  2803. device->FmtType = formats[i].type;
  2804. device->Flags |= DEVICE_FORMAT_REQUEST;
  2805. break;
  2806. }
  2807. }
  2808. if(i == COUNTOF(formats))
  2809. ERR("Unsupported format: %s\n", fmt);
  2810. }
  2811. #undef DEVICE_FORMAT_REQUEST
  2812. if(ConfigValueUInt(NULL, "frequency", &device->Frequency))
  2813. {
  2814. device->Flags |= DEVICE_FREQUENCY_REQUEST;
  2815. if(device->Frequency < MIN_OUTPUT_RATE)
  2816. ERR("%uhz request clamped to %uhz minimum\n", device->Frequency, MIN_OUTPUT_RATE);
  2817. device->Frequency = maxu(device->Frequency, MIN_OUTPUT_RATE);
  2818. }
  2819. ConfigValueUInt(NULL, "periods", &device->NumUpdates);
  2820. device->NumUpdates = clampu(device->NumUpdates, 2, 16);
  2821. ConfigValueUInt(NULL, "period_size", &device->UpdateSize);
  2822. device->UpdateSize = clampu(device->UpdateSize, 64, 8192);
  2823. if((CPUCapFlags&CPU_CAP_SSE))
  2824. device->UpdateSize = (device->UpdateSize+3)&~3;
  2825. ConfigValueUInt(NULL, "sources", &device->MaxNoOfSources);
  2826. if(device->MaxNoOfSources == 0) device->MaxNoOfSources = 256;
  2827. ConfigValueUInt(NULL, "slots", &device->AuxiliaryEffectSlotMax);
  2828. if(device->AuxiliaryEffectSlotMax == 0) device->AuxiliaryEffectSlotMax = 4;
  2829. ConfigValueUInt(NULL, "sends", &device->NumAuxSends);
  2830. if(device->NumAuxSends > MAX_SENDS) device->NumAuxSends = MAX_SENDS;
  2831. ConfigValueInt(NULL, "cf_level", &device->Bs2bLevel);
  2832. device->NumStereoSources = 1;
  2833. device->NumMonoSources = device->MaxNoOfSources - device->NumStereoSources;
  2834. device->Synth = SynthCreate(device);
  2835. if(!device->Synth)
  2836. {
  2837. DELETE_OBJ(device->Backend);
  2838. al_free(device);
  2839. alcSetError(NULL, ALC_OUT_OF_MEMORY);
  2840. return NULL;
  2841. }
  2842. // Find a playback device to open
  2843. if((err=V(device->Backend,open)(deviceName)) != ALC_NO_ERROR)
  2844. {
  2845. DELETE_OBJ(device->Synth);
  2846. DELETE_OBJ(device->Backend);
  2847. al_free(device);
  2848. alcSetError(NULL, err);
  2849. return NULL;
  2850. }
  2851. if(DefaultEffect.type != AL_EFFECT_NULL)
  2852. {
  2853. device->DefaultSlot = (ALeffectslot*)device->_slot_mem;
  2854. if(InitEffectSlot(device->DefaultSlot) != AL_NO_ERROR)
  2855. {
  2856. device->DefaultSlot = NULL;
  2857. ERR("Failed to initialize the default effect slot\n");
  2858. }
  2859. else if(InitializeEffect(device, device->DefaultSlot, &DefaultEffect) != AL_NO_ERROR)
  2860. {
  2861. ALeffectState *state = device->DefaultSlot->EffectState;
  2862. device->DefaultSlot = NULL;
  2863. DELETE_OBJ(state);
  2864. ERR("Failed to initialize the default effect\n");
  2865. }
  2866. }
  2867. {
  2868. ALCdevice *head = ATOMIC_LOAD(&DeviceList);
  2869. do {
  2870. device->next = head;
  2871. } while(!ATOMIC_COMPARE_EXCHANGE_WEAK(ALCdevice*, &DeviceList, &head, device));
  2872. }
  2873. TRACE("Created device %p, \"%s\"\n", device, al_string_get_cstr(device->DeviceName));
  2874. return device;
  2875. }
  2876. /* alcCloseDevice
  2877. *
  2878. * Closes the given device.
  2879. */
  2880. ALC_API ALCboolean ALC_APIENTRY alcCloseDevice(ALCdevice *device)
  2881. {
  2882. ALCdevice *list, *origdev, *nextdev;
  2883. ALCcontext *ctx;
  2884. LockLists();
  2885. list = ATOMIC_LOAD(&DeviceList);
  2886. do {
  2887. if(list == device)
  2888. break;
  2889. } while((list=list->next) != NULL);
  2890. if(!list || list->Type == Capture)
  2891. {
  2892. alcSetError(list, ALC_INVALID_DEVICE);
  2893. UnlockLists();
  2894. return ALC_FALSE;
  2895. }
  2896. origdev = device;
  2897. nextdev = device->next;
  2898. if(!ATOMIC_COMPARE_EXCHANGE_STRONG(ALCdevice*, &DeviceList, &origdev, nextdev))
  2899. {
  2900. do {
  2901. list = origdev;
  2902. origdev = device;
  2903. } while(!COMPARE_EXCHANGE(&list->next, &origdev, nextdev));
  2904. }
  2905. UnlockLists();
  2906. ctx = ATOMIC_LOAD(&device->ContextList);
  2907. while(ctx != NULL)
  2908. {
  2909. ALCcontext *next = ctx->next;
  2910. WARN("Releasing context %p\n", ctx);
  2911. ReleaseContext(ctx, device);
  2912. ctx = next;
  2913. }
  2914. if((device->Flags&DEVICE_RUNNING))
  2915. V0(device->Backend,stop)();
  2916. device->Flags &= ~DEVICE_RUNNING;
  2917. ALCdevice_DecRef(device);
  2918. return ALC_TRUE;
  2919. }
  2920. /************************************************
  2921. * ALC capture functions
  2922. ************************************************/
  2923. ALC_API ALCdevice* ALC_APIENTRY alcCaptureOpenDevice(const ALCchar *deviceName, ALCuint frequency, ALCenum format, ALCsizei samples)
  2924. {
  2925. ALCdevice *device = NULL;
  2926. ALCenum err;
  2927. DO_INITCONFIG();
  2928. if(!CaptureBackend.name)
  2929. {
  2930. alcSetError(NULL, ALC_INVALID_VALUE);
  2931. return NULL;
  2932. }
  2933. if(samples <= 0)
  2934. {
  2935. alcSetError(NULL, ALC_INVALID_VALUE);
  2936. return NULL;
  2937. }
  2938. if(deviceName && (!deviceName[0] || strcasecmp(deviceName, alcDefaultName) == 0 || strcasecmp(deviceName, "openal-soft") == 0))
  2939. deviceName = NULL;
  2940. device = al_calloc(16, sizeof(ALCdevice));
  2941. if(!device)
  2942. {
  2943. alcSetError(NULL, ALC_OUT_OF_MEMORY);
  2944. return NULL;
  2945. }
  2946. //Validate device
  2947. InitRef(&device->ref, 1);
  2948. device->Connected = ALC_TRUE;
  2949. device->Type = Capture;
  2950. AL_STRING_INIT(device->DeviceName);
  2951. InitUIntMap(&device->BufferMap, ~0);
  2952. InitUIntMap(&device->EffectMap, ~0);
  2953. InitUIntMap(&device->FilterMap, ~0);
  2954. InitUIntMap(&device->SfontMap, ~0);
  2955. InitUIntMap(&device->PresetMap, ~0);
  2956. InitUIntMap(&device->FontsoundMap, ~0);
  2957. if(!CaptureBackend.getFactory)
  2958. device->Backend = create_backend_wrapper(device, &CaptureBackend.Funcs,
  2959. ALCbackend_Capture);
  2960. else
  2961. {
  2962. ALCbackendFactory *factory = CaptureBackend.getFactory();
  2963. device->Backend = V(factory,createBackend)(device, ALCbackend_Capture);
  2964. }
  2965. if(!device->Backend)
  2966. {
  2967. al_free(device);
  2968. alcSetError(NULL, ALC_OUT_OF_MEMORY);
  2969. return NULL;
  2970. }
  2971. device->Flags |= DEVICE_FREQUENCY_REQUEST;
  2972. device->Frequency = frequency;
  2973. device->Flags |= DEVICE_CHANNELS_REQUEST | DEVICE_SAMPLE_TYPE_REQUEST;
  2974. if(DecomposeDevFormat(format, &device->FmtChans, &device->FmtType) == AL_FALSE)
  2975. {
  2976. al_free(device);
  2977. alcSetError(NULL, ALC_INVALID_ENUM);
  2978. return NULL;
  2979. }
  2980. device->UpdateSize = samples;
  2981. device->NumUpdates = 1;
  2982. if((err=V(device->Backend,open)(deviceName)) != ALC_NO_ERROR)
  2983. {
  2984. al_free(device);
  2985. alcSetError(NULL, err);
  2986. return NULL;
  2987. }
  2988. {
  2989. ALCdevice *head = ATOMIC_LOAD(&DeviceList);
  2990. do {
  2991. device->next = head;
  2992. } while(!ATOMIC_COMPARE_EXCHANGE_WEAK(ALCdevice*, &DeviceList, &head, device));
  2993. }
  2994. TRACE("Created device %p, \"%s\"\n", device, al_string_get_cstr(device->DeviceName));
  2995. return device;
  2996. }
  2997. ALC_API ALCboolean ALC_APIENTRY alcCaptureCloseDevice(ALCdevice *device)
  2998. {
  2999. ALCdevice *list, *next, *nextdev;
  3000. LockLists();
  3001. list = ATOMIC_LOAD(&DeviceList);
  3002. do {
  3003. if(list == device)
  3004. break;
  3005. } while((list=list->next) != NULL);
  3006. if(!list || list->Type != Capture)
  3007. {
  3008. alcSetError(list, ALC_INVALID_DEVICE);
  3009. UnlockLists();
  3010. return ALC_FALSE;
  3011. }
  3012. next = device;
  3013. nextdev = device->next;
  3014. if(!ATOMIC_COMPARE_EXCHANGE_STRONG(ALCdevice*, &DeviceList, &next, nextdev))
  3015. {
  3016. do {
  3017. list = next;
  3018. next = device;
  3019. } while(!COMPARE_EXCHANGE(&list->next, &next, nextdev));
  3020. }
  3021. UnlockLists();
  3022. ALCdevice_DecRef(device);
  3023. return ALC_TRUE;
  3024. }
  3025. ALC_API void ALC_APIENTRY alcCaptureStart(ALCdevice *device)
  3026. {
  3027. if(!(device=VerifyDevice(device)) || device->Type != Capture)
  3028. alcSetError(device, ALC_INVALID_DEVICE);
  3029. else
  3030. {
  3031. ALCdevice_Lock(device);
  3032. if(device->Connected)
  3033. {
  3034. if(!(device->Flags&DEVICE_RUNNING))
  3035. V0(device->Backend,start)();
  3036. device->Flags |= DEVICE_RUNNING;
  3037. }
  3038. ALCdevice_Unlock(device);
  3039. }
  3040. if(device) ALCdevice_DecRef(device);
  3041. }
  3042. ALC_API void ALC_APIENTRY alcCaptureStop(ALCdevice *device)
  3043. {
  3044. if(!(device=VerifyDevice(device)) || device->Type != Capture)
  3045. alcSetError(device, ALC_INVALID_DEVICE);
  3046. else
  3047. {
  3048. ALCdevice_Lock(device);
  3049. if((device->Flags&DEVICE_RUNNING))
  3050. V0(device->Backend,stop)();
  3051. device->Flags &= ~DEVICE_RUNNING;
  3052. ALCdevice_Unlock(device);
  3053. }
  3054. if(device) ALCdevice_DecRef(device);
  3055. }
  3056. ALC_API void ALC_APIENTRY alcCaptureSamples(ALCdevice *device, ALCvoid *buffer, ALCsizei samples)
  3057. {
  3058. if(!(device=VerifyDevice(device)) || device->Type != Capture)
  3059. alcSetError(device, ALC_INVALID_DEVICE);
  3060. else
  3061. {
  3062. ALCenum err = ALC_INVALID_VALUE;
  3063. ALCdevice_Lock(device);
  3064. if(samples >= 0 && V0(device->Backend,availableSamples)() >= (ALCuint)samples)
  3065. err = V(device->Backend,captureSamples)(buffer, samples);
  3066. ALCdevice_Unlock(device);
  3067. if(err != ALC_NO_ERROR)
  3068. alcSetError(device, err);
  3069. }
  3070. if(device) ALCdevice_DecRef(device);
  3071. }
  3072. /************************************************
  3073. * ALC loopback functions
  3074. ************************************************/
  3075. /* alcLoopbackOpenDeviceSOFT
  3076. *
  3077. * Open a loopback device, for manual rendering.
  3078. */
  3079. ALC_API ALCdevice* ALC_APIENTRY alcLoopbackOpenDeviceSOFT(const ALCchar *deviceName)
  3080. {
  3081. ALCbackendFactory *factory;
  3082. ALCdevice *device;
  3083. DO_INITCONFIG();
  3084. /* Make sure the device name, if specified, is us. */
  3085. if(deviceName && strcmp(deviceName, alcDefaultName) != 0)
  3086. {
  3087. alcSetError(NULL, ALC_INVALID_VALUE);
  3088. return NULL;
  3089. }
  3090. device = al_calloc(16, sizeof(ALCdevice));
  3091. if(!device)
  3092. {
  3093. alcSetError(NULL, ALC_OUT_OF_MEMORY);
  3094. return NULL;
  3095. }
  3096. //Validate device
  3097. InitRef(&device->ref, 1);
  3098. device->Connected = ALC_TRUE;
  3099. device->Type = Loopback;
  3100. ATOMIC_INIT(&device->LastError, ALC_NO_ERROR);
  3101. device->Flags = 0;
  3102. device->Bs2b = NULL;
  3103. device->Bs2bLevel = 0;
  3104. AL_STRING_INIT(device->DeviceName);
  3105. ATOMIC_INIT(&device->ContextList, NULL);
  3106. device->ClockBase = 0;
  3107. device->SamplesDone = 0;
  3108. device->MaxNoOfSources = 256;
  3109. device->AuxiliaryEffectSlotMax = 4;
  3110. device->NumAuxSends = MAX_SENDS;
  3111. InitUIntMap(&device->BufferMap, ~0);
  3112. InitUIntMap(&device->EffectMap, ~0);
  3113. InitUIntMap(&device->FilterMap, ~0);
  3114. InitUIntMap(&device->SfontMap, ~0);
  3115. InitUIntMap(&device->PresetMap, ~0);
  3116. InitUIntMap(&device->FontsoundMap, ~0);
  3117. factory = ALCloopbackFactory_getFactory();
  3118. device->Backend = V(factory,createBackend)(device, ALCbackend_Loopback);
  3119. if(!device->Backend)
  3120. {
  3121. al_free(device);
  3122. alcSetError(NULL, ALC_OUT_OF_MEMORY);
  3123. return NULL;
  3124. }
  3125. //Set output format
  3126. device->NumUpdates = 0;
  3127. device->UpdateSize = 0;
  3128. device->Frequency = DEFAULT_OUTPUT_RATE;
  3129. device->FmtChans = DevFmtChannelsDefault;
  3130. device->FmtType = DevFmtTypeDefault;
  3131. ConfigValueUInt(NULL, "sources", &device->MaxNoOfSources);
  3132. if(device->MaxNoOfSources == 0) device->MaxNoOfSources = 256;
  3133. ConfigValueUInt(NULL, "slots", &device->AuxiliaryEffectSlotMax);
  3134. if(device->AuxiliaryEffectSlotMax == 0) device->AuxiliaryEffectSlotMax = 4;
  3135. ConfigValueUInt(NULL, "sends", &device->NumAuxSends);
  3136. if(device->NumAuxSends > MAX_SENDS) device->NumAuxSends = MAX_SENDS;
  3137. device->NumStereoSources = 1;
  3138. device->NumMonoSources = device->MaxNoOfSources - device->NumStereoSources;
  3139. device->Synth = SynthCreate(device);
  3140. if(!device->Synth)
  3141. {
  3142. DELETE_OBJ(device->Backend);
  3143. al_free(device);
  3144. alcSetError(NULL, ALC_OUT_OF_MEMORY);
  3145. return NULL;
  3146. }
  3147. // Open the "backend"
  3148. V(device->Backend,open)("Loopback");
  3149. {
  3150. ALCdevice *head = ATOMIC_LOAD(&DeviceList);
  3151. do {
  3152. device->next = head;
  3153. } while(!ATOMIC_COMPARE_EXCHANGE_WEAK(ALCdevice*, &DeviceList, &head, device));
  3154. }
  3155. TRACE("Created device %p\n", device);
  3156. return device;
  3157. }
  3158. /* alcIsRenderFormatSupportedSOFT
  3159. *
  3160. * Determines if the loopback device supports the given format for rendering.
  3161. */
  3162. ALC_API ALCboolean ALC_APIENTRY alcIsRenderFormatSupportedSOFT(ALCdevice *device, ALCsizei freq, ALCenum channels, ALCenum type)
  3163. {
  3164. ALCboolean ret = ALC_FALSE;
  3165. if(!(device=VerifyDevice(device)) || device->Type != Loopback)
  3166. alcSetError(device, ALC_INVALID_DEVICE);
  3167. else if(freq <= 0)
  3168. alcSetError(device, ALC_INVALID_VALUE);
  3169. else
  3170. {
  3171. if(IsValidALCType(type) && BytesFromDevFmt(type) > 0 &&
  3172. IsValidALCChannels(channels) && ChannelsFromDevFmt(channels) > 0 &&
  3173. freq >= MIN_OUTPUT_RATE)
  3174. ret = ALC_TRUE;
  3175. }
  3176. if(device) ALCdevice_DecRef(device);
  3177. return ret;
  3178. }
  3179. /* alcRenderSamplesSOFT
  3180. *
  3181. * Renders some samples into a buffer, using the format last set by the
  3182. * attributes given to alcCreateContext.
  3183. */
  3184. FORCE_ALIGN ALC_API void ALC_APIENTRY alcRenderSamplesSOFT(ALCdevice *device, ALCvoid *buffer, ALCsizei samples)
  3185. {
  3186. if(!(device=VerifyDevice(device)) || device->Type != Loopback)
  3187. alcSetError(device, ALC_INVALID_DEVICE);
  3188. else if(samples < 0 || (samples > 0 && buffer == NULL))
  3189. alcSetError(device, ALC_INVALID_VALUE);
  3190. else
  3191. aluMixData(device, buffer, samples);
  3192. if(device) ALCdevice_DecRef(device);
  3193. }
  3194. /************************************************
  3195. * ALC DSP pause/resume functions
  3196. ************************************************/
  3197. /* alcDevicePauseSOFT
  3198. *
  3199. * Pause the DSP to stop audio processing.
  3200. */
  3201. ALC_API void ALC_APIENTRY alcDevicePauseSOFT(ALCdevice *device)
  3202. {
  3203. if(!(device=VerifyDevice(device)) || device->Type != Playback)
  3204. alcSetError(device, ALC_INVALID_DEVICE);
  3205. else
  3206. {
  3207. LockLists();
  3208. if((device->Flags&DEVICE_RUNNING))
  3209. V0(device->Backend,stop)();
  3210. device->Flags &= ~DEVICE_RUNNING;
  3211. device->Flags |= DEVICE_PAUSED;
  3212. UnlockLists();
  3213. }
  3214. if(device) ALCdevice_DecRef(device);
  3215. }
  3216. /* alcDeviceResumeSOFT
  3217. *
  3218. * Resume the DSP to restart audio processing.
  3219. */
  3220. ALC_API void ALC_APIENTRY alcDeviceResumeSOFT(ALCdevice *device)
  3221. {
  3222. if(!(device=VerifyDevice(device)) || device->Type != Playback)
  3223. alcSetError(device, ALC_INVALID_DEVICE);
  3224. else
  3225. {
  3226. LockLists();
  3227. if((device->Flags&DEVICE_PAUSED))
  3228. {
  3229. device->Flags &= ~DEVICE_PAUSED;
  3230. if(ATOMIC_LOAD(&device->ContextList) != NULL)
  3231. {
  3232. if(V0(device->Backend,start)() != ALC_FALSE)
  3233. device->Flags |= DEVICE_RUNNING;
  3234. else
  3235. {
  3236. alcSetError(device, ALC_INVALID_DEVICE);
  3237. ALCdevice_Lock(device);
  3238. aluHandleDisconnect(device);
  3239. ALCdevice_Unlock(device);
  3240. }
  3241. }
  3242. }
  3243. UnlockLists();
  3244. }
  3245. if(device) ALCdevice_DecRef(device);
  3246. }