state.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  1. /**
  2. * OpenAL cross platform audio library
  3. * Copyright (C) 1999-2000 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 "version.h"
  22. #include <atomic>
  23. #include <cmath>
  24. #include <cstdlib>
  25. #include <cstring>
  26. #include <mutex>
  27. #include "AL/al.h"
  28. #include "AL/alc.h"
  29. #include "AL/alext.h"
  30. #include "alcontext.h"
  31. #include "almalloc.h"
  32. #include "alnumeric.h"
  33. #include "alspan.h"
  34. #include "alu.h"
  35. #include "atomic.h"
  36. #include "core/except.h"
  37. #include "event.h"
  38. #include "inprogext.h"
  39. #include "opthelpers.h"
  40. #include "strutils.h"
  41. #include "voice.h"
  42. namespace {
  43. constexpr ALchar alVendor[] = "OpenAL Community";
  44. constexpr ALchar alVersion[] = "1.1 ALSOFT " ALSOFT_VERSION;
  45. constexpr ALchar alRenderer[] = "OpenAL Soft";
  46. // Error Messages
  47. constexpr ALchar alNoError[] = "No Error";
  48. constexpr ALchar alErrInvalidName[] = "Invalid Name";
  49. constexpr ALchar alErrInvalidEnum[] = "Invalid Enum";
  50. constexpr ALchar alErrInvalidValue[] = "Invalid Value";
  51. constexpr ALchar alErrInvalidOp[] = "Invalid Operation";
  52. constexpr ALchar alErrOutOfMemory[] = "Out of Memory";
  53. /* Resampler strings */
  54. template<Resampler rtype> struct ResamplerName { };
  55. template<> struct ResamplerName<Resampler::Point>
  56. { static constexpr const ALchar *Get() noexcept { return "Nearest"; } };
  57. template<> struct ResamplerName<Resampler::Linear>
  58. { static constexpr const ALchar *Get() noexcept { return "Linear"; } };
  59. template<> struct ResamplerName<Resampler::Cubic>
  60. { static constexpr const ALchar *Get() noexcept { return "Cubic"; } };
  61. template<> struct ResamplerName<Resampler::FastBSinc12>
  62. { static constexpr const ALchar *Get() noexcept { return "11th order Sinc (fast)"; } };
  63. template<> struct ResamplerName<Resampler::BSinc12>
  64. { static constexpr const ALchar *Get() noexcept { return "11th order Sinc"; } };
  65. template<> struct ResamplerName<Resampler::FastBSinc24>
  66. { static constexpr const ALchar *Get() noexcept { return "23rd order Sinc (fast)"; } };
  67. template<> struct ResamplerName<Resampler::BSinc24>
  68. { static constexpr const ALchar *Get() noexcept { return "23rd order Sinc"; } };
  69. const ALchar *GetResamplerName(const Resampler rtype)
  70. {
  71. #define HANDLE_RESAMPLER(r) case r: return ResamplerName<r>::Get()
  72. switch(rtype)
  73. {
  74. HANDLE_RESAMPLER(Resampler::Point);
  75. HANDLE_RESAMPLER(Resampler::Linear);
  76. HANDLE_RESAMPLER(Resampler::Cubic);
  77. HANDLE_RESAMPLER(Resampler::FastBSinc12);
  78. HANDLE_RESAMPLER(Resampler::BSinc12);
  79. HANDLE_RESAMPLER(Resampler::FastBSinc24);
  80. HANDLE_RESAMPLER(Resampler::BSinc24);
  81. }
  82. #undef HANDLE_RESAMPLER
  83. /* Should never get here. */
  84. throw std::runtime_error{"Unexpected resampler index"};
  85. }
  86. al::optional<DistanceModel> DistanceModelFromALenum(ALenum model)
  87. {
  88. switch(model)
  89. {
  90. case AL_NONE: return al::make_optional(DistanceModel::Disable);
  91. case AL_INVERSE_DISTANCE: return al::make_optional(DistanceModel::Inverse);
  92. case AL_INVERSE_DISTANCE_CLAMPED: return al::make_optional(DistanceModel::InverseClamped);
  93. case AL_LINEAR_DISTANCE: return al::make_optional(DistanceModel::Linear);
  94. case AL_LINEAR_DISTANCE_CLAMPED: return al::make_optional(DistanceModel::LinearClamped);
  95. case AL_EXPONENT_DISTANCE: return al::make_optional(DistanceModel::Exponent);
  96. case AL_EXPONENT_DISTANCE_CLAMPED: return al::make_optional(DistanceModel::ExponentClamped);
  97. }
  98. return al::nullopt;
  99. }
  100. ALenum ALenumFromDistanceModel(DistanceModel model)
  101. {
  102. switch(model)
  103. {
  104. case DistanceModel::Disable: return AL_NONE;
  105. case DistanceModel::Inverse: return AL_INVERSE_DISTANCE;
  106. case DistanceModel::InverseClamped: return AL_INVERSE_DISTANCE_CLAMPED;
  107. case DistanceModel::Linear: return AL_LINEAR_DISTANCE;
  108. case DistanceModel::LinearClamped: return AL_LINEAR_DISTANCE_CLAMPED;
  109. case DistanceModel::Exponent: return AL_EXPONENT_DISTANCE;
  110. case DistanceModel::ExponentClamped: return AL_EXPONENT_DISTANCE_CLAMPED;
  111. }
  112. throw std::runtime_error{"Unexpected distance model "+std::to_string(static_cast<int>(model))};
  113. }
  114. } // namespace
  115. /* WARNING: Non-standard export! Not part of any extension, or exposed in the
  116. * alcFunctions list.
  117. */
  118. extern "C" AL_API const ALchar* AL_APIENTRY alsoft_get_version(void)
  119. START_API_FUNC
  120. {
  121. static const auto spoof = al::getenv("ALSOFT_SPOOF_VERSION");
  122. if(spoof) return spoof->c_str();
  123. return ALSOFT_VERSION;
  124. }
  125. END_API_FUNC
  126. #define DO_UPDATEPROPS() do { \
  127. if(!context->mDeferUpdates.load(std::memory_order_acquire)) \
  128. UpdateContextProps(context.get()); \
  129. else \
  130. context->mPropsClean.clear(std::memory_order_release); \
  131. } while(0)
  132. AL_API void AL_APIENTRY alEnable(ALenum capability)
  133. START_API_FUNC
  134. {
  135. ContextRef context{GetContextRef()};
  136. if UNLIKELY(!context) return;
  137. std::lock_guard<std::mutex> _{context->mPropLock};
  138. switch(capability)
  139. {
  140. case AL_SOURCE_DISTANCE_MODEL:
  141. context->mSourceDistanceModel = true;
  142. DO_UPDATEPROPS();
  143. break;
  144. default:
  145. context->setError(AL_INVALID_VALUE, "Invalid enable property 0x%04x", capability);
  146. }
  147. }
  148. END_API_FUNC
  149. AL_API void AL_APIENTRY alDisable(ALenum capability)
  150. START_API_FUNC
  151. {
  152. ContextRef context{GetContextRef()};
  153. if UNLIKELY(!context) return;
  154. std::lock_guard<std::mutex> _{context->mPropLock};
  155. switch(capability)
  156. {
  157. case AL_SOURCE_DISTANCE_MODEL:
  158. context->mSourceDistanceModel = false;
  159. DO_UPDATEPROPS();
  160. break;
  161. default:
  162. context->setError(AL_INVALID_VALUE, "Invalid disable property 0x%04x", capability);
  163. }
  164. }
  165. END_API_FUNC
  166. AL_API ALboolean AL_APIENTRY alIsEnabled(ALenum capability)
  167. START_API_FUNC
  168. {
  169. ContextRef context{GetContextRef()};
  170. if UNLIKELY(!context) return AL_FALSE;
  171. std::lock_guard<std::mutex> _{context->mPropLock};
  172. ALboolean value{AL_FALSE};
  173. switch(capability)
  174. {
  175. case AL_SOURCE_DISTANCE_MODEL:
  176. value = context->mSourceDistanceModel ? AL_TRUE : AL_FALSE;
  177. break;
  178. default:
  179. context->setError(AL_INVALID_VALUE, "Invalid is enabled property 0x%04x", capability);
  180. }
  181. return value;
  182. }
  183. END_API_FUNC
  184. AL_API ALboolean AL_APIENTRY alGetBoolean(ALenum pname)
  185. START_API_FUNC
  186. {
  187. ContextRef context{GetContextRef()};
  188. if UNLIKELY(!context) return AL_FALSE;
  189. std::lock_guard<std::mutex> _{context->mPropLock};
  190. ALboolean value{AL_FALSE};
  191. switch(pname)
  192. {
  193. case AL_DOPPLER_FACTOR:
  194. if(context->mDopplerFactor != 0.0f)
  195. value = AL_TRUE;
  196. break;
  197. case AL_DOPPLER_VELOCITY:
  198. if(context->mDopplerVelocity != 0.0f)
  199. value = AL_TRUE;
  200. break;
  201. case AL_DISTANCE_MODEL:
  202. if(context->mDistanceModel == DistanceModel::Default)
  203. value = AL_TRUE;
  204. break;
  205. case AL_SPEED_OF_SOUND:
  206. if(context->mSpeedOfSound != 0.0f)
  207. value = AL_TRUE;
  208. break;
  209. case AL_DEFERRED_UPDATES_SOFT:
  210. if(context->mDeferUpdates.load(std::memory_order_acquire))
  211. value = AL_TRUE;
  212. break;
  213. case AL_GAIN_LIMIT_SOFT:
  214. if(GainMixMax/context->mGainBoost != 0.0f)
  215. value = AL_TRUE;
  216. break;
  217. case AL_NUM_RESAMPLERS_SOFT:
  218. /* Always non-0. */
  219. value = AL_TRUE;
  220. break;
  221. case AL_DEFAULT_RESAMPLER_SOFT:
  222. value = static_cast<int>(ResamplerDefault) ? AL_TRUE : AL_FALSE;
  223. break;
  224. default:
  225. context->setError(AL_INVALID_VALUE, "Invalid boolean property 0x%04x", pname);
  226. }
  227. return value;
  228. }
  229. END_API_FUNC
  230. AL_API ALdouble AL_APIENTRY alGetDouble(ALenum pname)
  231. START_API_FUNC
  232. {
  233. ContextRef context{GetContextRef()};
  234. if UNLIKELY(!context) return 0.0;
  235. std::lock_guard<std::mutex> _{context->mPropLock};
  236. ALdouble value{0.0};
  237. switch(pname)
  238. {
  239. case AL_DOPPLER_FACTOR:
  240. value = context->mDopplerFactor;
  241. break;
  242. case AL_DOPPLER_VELOCITY:
  243. value = context->mDopplerVelocity;
  244. break;
  245. case AL_DISTANCE_MODEL:
  246. value = static_cast<ALdouble>(ALenumFromDistanceModel(context->mDistanceModel));
  247. break;
  248. case AL_SPEED_OF_SOUND:
  249. value = context->mSpeedOfSound;
  250. break;
  251. case AL_DEFERRED_UPDATES_SOFT:
  252. if(context->mDeferUpdates.load(std::memory_order_acquire))
  253. value = static_cast<ALdouble>(AL_TRUE);
  254. break;
  255. case AL_GAIN_LIMIT_SOFT:
  256. value = ALdouble{GainMixMax}/context->mGainBoost;
  257. break;
  258. case AL_NUM_RESAMPLERS_SOFT:
  259. value = static_cast<ALdouble>(Resampler::Max) + 1.0;
  260. break;
  261. case AL_DEFAULT_RESAMPLER_SOFT:
  262. value = static_cast<ALdouble>(ResamplerDefault);
  263. break;
  264. default:
  265. context->setError(AL_INVALID_VALUE, "Invalid double property 0x%04x", pname);
  266. }
  267. return value;
  268. }
  269. END_API_FUNC
  270. AL_API ALfloat AL_APIENTRY alGetFloat(ALenum pname)
  271. START_API_FUNC
  272. {
  273. ContextRef context{GetContextRef()};
  274. if UNLIKELY(!context) return 0.0f;
  275. std::lock_guard<std::mutex> _{context->mPropLock};
  276. ALfloat value{0.0f};
  277. switch(pname)
  278. {
  279. case AL_DOPPLER_FACTOR:
  280. value = context->mDopplerFactor;
  281. break;
  282. case AL_DOPPLER_VELOCITY:
  283. value = context->mDopplerVelocity;
  284. break;
  285. case AL_DISTANCE_MODEL:
  286. value = static_cast<ALfloat>(ALenumFromDistanceModel(context->mDistanceModel));
  287. break;
  288. case AL_SPEED_OF_SOUND:
  289. value = context->mSpeedOfSound;
  290. break;
  291. case AL_DEFERRED_UPDATES_SOFT:
  292. if(context->mDeferUpdates.load(std::memory_order_acquire))
  293. value = static_cast<ALfloat>(AL_TRUE);
  294. break;
  295. case AL_GAIN_LIMIT_SOFT:
  296. value = GainMixMax/context->mGainBoost;
  297. break;
  298. case AL_NUM_RESAMPLERS_SOFT:
  299. value = static_cast<ALfloat>(Resampler::Max) + 1.0f;
  300. break;
  301. case AL_DEFAULT_RESAMPLER_SOFT:
  302. value = static_cast<ALfloat>(ResamplerDefault);
  303. break;
  304. default:
  305. context->setError(AL_INVALID_VALUE, "Invalid float property 0x%04x", pname);
  306. }
  307. return value;
  308. }
  309. END_API_FUNC
  310. AL_API ALint AL_APIENTRY alGetInteger(ALenum pname)
  311. START_API_FUNC
  312. {
  313. ContextRef context{GetContextRef()};
  314. if UNLIKELY(!context) return 0;
  315. std::lock_guard<std::mutex> _{context->mPropLock};
  316. ALint value{0};
  317. switch(pname)
  318. {
  319. case AL_DOPPLER_FACTOR:
  320. value = static_cast<ALint>(context->mDopplerFactor);
  321. break;
  322. case AL_DOPPLER_VELOCITY:
  323. value = static_cast<ALint>(context->mDopplerVelocity);
  324. break;
  325. case AL_DISTANCE_MODEL:
  326. value = ALenumFromDistanceModel(context->mDistanceModel);
  327. break;
  328. case AL_SPEED_OF_SOUND:
  329. value = static_cast<ALint>(context->mSpeedOfSound);
  330. break;
  331. case AL_DEFERRED_UPDATES_SOFT:
  332. if(context->mDeferUpdates.load(std::memory_order_acquire))
  333. value = AL_TRUE;
  334. break;
  335. case AL_GAIN_LIMIT_SOFT:
  336. value = static_cast<ALint>(GainMixMax/context->mGainBoost);
  337. break;
  338. case AL_NUM_RESAMPLERS_SOFT:
  339. value = static_cast<int>(Resampler::Max) + 1;
  340. break;
  341. case AL_DEFAULT_RESAMPLER_SOFT:
  342. value = static_cast<int>(ResamplerDefault);
  343. break;
  344. default:
  345. context->setError(AL_INVALID_VALUE, "Invalid integer property 0x%04x", pname);
  346. }
  347. return value;
  348. }
  349. END_API_FUNC
  350. extern "C" AL_API ALint64SOFT AL_APIENTRY alGetInteger64SOFT(ALenum pname)
  351. START_API_FUNC
  352. {
  353. ContextRef context{GetContextRef()};
  354. if UNLIKELY(!context) return 0_i64;
  355. std::lock_guard<std::mutex> _{context->mPropLock};
  356. ALint64SOFT value{0};
  357. switch(pname)
  358. {
  359. case AL_DOPPLER_FACTOR:
  360. value = static_cast<ALint64SOFT>(context->mDopplerFactor);
  361. break;
  362. case AL_DOPPLER_VELOCITY:
  363. value = static_cast<ALint64SOFT>(context->mDopplerVelocity);
  364. break;
  365. case AL_DISTANCE_MODEL:
  366. value = ALenumFromDistanceModel(context->mDistanceModel);
  367. break;
  368. case AL_SPEED_OF_SOUND:
  369. value = static_cast<ALint64SOFT>(context->mSpeedOfSound);
  370. break;
  371. case AL_DEFERRED_UPDATES_SOFT:
  372. if(context->mDeferUpdates.load(std::memory_order_acquire))
  373. value = AL_TRUE;
  374. break;
  375. case AL_GAIN_LIMIT_SOFT:
  376. value = static_cast<ALint64SOFT>(GainMixMax/context->mGainBoost);
  377. break;
  378. case AL_NUM_RESAMPLERS_SOFT:
  379. value = static_cast<ALint64SOFT>(Resampler::Max) + 1;
  380. break;
  381. case AL_DEFAULT_RESAMPLER_SOFT:
  382. value = static_cast<ALint64SOFT>(ResamplerDefault);
  383. break;
  384. default:
  385. context->setError(AL_INVALID_VALUE, "Invalid integer64 property 0x%04x", pname);
  386. }
  387. return value;
  388. }
  389. END_API_FUNC
  390. AL_API ALvoid* AL_APIENTRY alGetPointerSOFT(ALenum pname)
  391. START_API_FUNC
  392. {
  393. ContextRef context{GetContextRef()};
  394. if UNLIKELY(!context) return nullptr;
  395. std::lock_guard<std::mutex> _{context->mPropLock};
  396. void *value{nullptr};
  397. switch(pname)
  398. {
  399. case AL_EVENT_CALLBACK_FUNCTION_SOFT:
  400. value = reinterpret_cast<void*>(context->mEventCb);
  401. break;
  402. case AL_EVENT_CALLBACK_USER_PARAM_SOFT:
  403. value = context->mEventParam;
  404. break;
  405. default:
  406. context->setError(AL_INVALID_VALUE, "Invalid pointer property 0x%04x", pname);
  407. }
  408. return value;
  409. }
  410. END_API_FUNC
  411. AL_API void AL_APIENTRY alGetBooleanv(ALenum pname, ALboolean *values)
  412. START_API_FUNC
  413. {
  414. if(values)
  415. {
  416. switch(pname)
  417. {
  418. case AL_DOPPLER_FACTOR:
  419. case AL_DOPPLER_VELOCITY:
  420. case AL_DISTANCE_MODEL:
  421. case AL_SPEED_OF_SOUND:
  422. case AL_DEFERRED_UPDATES_SOFT:
  423. case AL_GAIN_LIMIT_SOFT:
  424. case AL_NUM_RESAMPLERS_SOFT:
  425. case AL_DEFAULT_RESAMPLER_SOFT:
  426. values[0] = alGetBoolean(pname);
  427. return;
  428. }
  429. }
  430. ContextRef context{GetContextRef()};
  431. if UNLIKELY(!context) return;
  432. if(!values)
  433. context->setError(AL_INVALID_VALUE, "NULL pointer");
  434. else switch(pname)
  435. {
  436. default:
  437. context->setError(AL_INVALID_VALUE, "Invalid boolean-vector property 0x%04x", pname);
  438. }
  439. }
  440. END_API_FUNC
  441. AL_API void AL_APIENTRY alGetDoublev(ALenum pname, ALdouble *values)
  442. START_API_FUNC
  443. {
  444. if(values)
  445. {
  446. switch(pname)
  447. {
  448. case AL_DOPPLER_FACTOR:
  449. case AL_DOPPLER_VELOCITY:
  450. case AL_DISTANCE_MODEL:
  451. case AL_SPEED_OF_SOUND:
  452. case AL_DEFERRED_UPDATES_SOFT:
  453. case AL_GAIN_LIMIT_SOFT:
  454. case AL_NUM_RESAMPLERS_SOFT:
  455. case AL_DEFAULT_RESAMPLER_SOFT:
  456. values[0] = alGetDouble(pname);
  457. return;
  458. }
  459. }
  460. ContextRef context{GetContextRef()};
  461. if UNLIKELY(!context) return;
  462. if(!values)
  463. context->setError(AL_INVALID_VALUE, "NULL pointer");
  464. else switch(pname)
  465. {
  466. default:
  467. context->setError(AL_INVALID_VALUE, "Invalid double-vector property 0x%04x", pname);
  468. }
  469. }
  470. END_API_FUNC
  471. AL_API void AL_APIENTRY alGetFloatv(ALenum pname, ALfloat *values)
  472. START_API_FUNC
  473. {
  474. if(values)
  475. {
  476. switch(pname)
  477. {
  478. case AL_DOPPLER_FACTOR:
  479. case AL_DOPPLER_VELOCITY:
  480. case AL_DISTANCE_MODEL:
  481. case AL_SPEED_OF_SOUND:
  482. case AL_DEFERRED_UPDATES_SOFT:
  483. case AL_GAIN_LIMIT_SOFT:
  484. case AL_NUM_RESAMPLERS_SOFT:
  485. case AL_DEFAULT_RESAMPLER_SOFT:
  486. values[0] = alGetFloat(pname);
  487. return;
  488. }
  489. }
  490. ContextRef context{GetContextRef()};
  491. if UNLIKELY(!context) return;
  492. if(!values)
  493. context->setError(AL_INVALID_VALUE, "NULL pointer");
  494. else switch(pname)
  495. {
  496. default:
  497. context->setError(AL_INVALID_VALUE, "Invalid float-vector property 0x%04x", pname);
  498. }
  499. }
  500. END_API_FUNC
  501. AL_API void AL_APIENTRY alGetIntegerv(ALenum pname, ALint *values)
  502. START_API_FUNC
  503. {
  504. if(values)
  505. {
  506. switch(pname)
  507. {
  508. case AL_DOPPLER_FACTOR:
  509. case AL_DOPPLER_VELOCITY:
  510. case AL_DISTANCE_MODEL:
  511. case AL_SPEED_OF_SOUND:
  512. case AL_DEFERRED_UPDATES_SOFT:
  513. case AL_GAIN_LIMIT_SOFT:
  514. case AL_NUM_RESAMPLERS_SOFT:
  515. case AL_DEFAULT_RESAMPLER_SOFT:
  516. values[0] = alGetInteger(pname);
  517. return;
  518. }
  519. }
  520. ContextRef context{GetContextRef()};
  521. if UNLIKELY(!context) return;
  522. if(!values)
  523. context->setError(AL_INVALID_VALUE, "NULL pointer");
  524. else switch(pname)
  525. {
  526. default:
  527. context->setError(AL_INVALID_VALUE, "Invalid integer-vector property 0x%04x", pname);
  528. }
  529. }
  530. END_API_FUNC
  531. extern "C" AL_API void AL_APIENTRY alGetInteger64vSOFT(ALenum pname, ALint64SOFT *values)
  532. START_API_FUNC
  533. {
  534. if(values)
  535. {
  536. switch(pname)
  537. {
  538. case AL_DOPPLER_FACTOR:
  539. case AL_DOPPLER_VELOCITY:
  540. case AL_DISTANCE_MODEL:
  541. case AL_SPEED_OF_SOUND:
  542. case AL_DEFERRED_UPDATES_SOFT:
  543. case AL_GAIN_LIMIT_SOFT:
  544. case AL_NUM_RESAMPLERS_SOFT:
  545. case AL_DEFAULT_RESAMPLER_SOFT:
  546. values[0] = alGetInteger64SOFT(pname);
  547. return;
  548. }
  549. }
  550. ContextRef context{GetContextRef()};
  551. if UNLIKELY(!context) return;
  552. if(!values)
  553. context->setError(AL_INVALID_VALUE, "NULL pointer");
  554. else switch(pname)
  555. {
  556. default:
  557. context->setError(AL_INVALID_VALUE, "Invalid integer64-vector property 0x%04x", pname);
  558. }
  559. }
  560. END_API_FUNC
  561. AL_API void AL_APIENTRY alGetPointervSOFT(ALenum pname, ALvoid **values)
  562. START_API_FUNC
  563. {
  564. if(values)
  565. {
  566. switch(pname)
  567. {
  568. case AL_EVENT_CALLBACK_FUNCTION_SOFT:
  569. case AL_EVENT_CALLBACK_USER_PARAM_SOFT:
  570. values[0] = alGetPointerSOFT(pname);
  571. return;
  572. }
  573. }
  574. ContextRef context{GetContextRef()};
  575. if UNLIKELY(!context) return;
  576. if(!values)
  577. context->setError(AL_INVALID_VALUE, "NULL pointer");
  578. else switch(pname)
  579. {
  580. default:
  581. context->setError(AL_INVALID_VALUE, "Invalid pointer-vector property 0x%04x", pname);
  582. }
  583. }
  584. END_API_FUNC
  585. AL_API const ALchar* AL_APIENTRY alGetString(ALenum pname)
  586. START_API_FUNC
  587. {
  588. ContextRef context{GetContextRef()};
  589. if UNLIKELY(!context) return nullptr;
  590. const ALchar *value{nullptr};
  591. switch(pname)
  592. {
  593. case AL_VENDOR:
  594. value = alVendor;
  595. break;
  596. case AL_VERSION:
  597. value = alVersion;
  598. break;
  599. case AL_RENDERER:
  600. value = alRenderer;
  601. break;
  602. case AL_EXTENSIONS:
  603. value = context->mExtensionList;
  604. break;
  605. case AL_NO_ERROR:
  606. value = alNoError;
  607. break;
  608. case AL_INVALID_NAME:
  609. value = alErrInvalidName;
  610. break;
  611. case AL_INVALID_ENUM:
  612. value = alErrInvalidEnum;
  613. break;
  614. case AL_INVALID_VALUE:
  615. value = alErrInvalidValue;
  616. break;
  617. case AL_INVALID_OPERATION:
  618. value = alErrInvalidOp;
  619. break;
  620. case AL_OUT_OF_MEMORY:
  621. value = alErrOutOfMemory;
  622. break;
  623. default:
  624. context->setError(AL_INVALID_VALUE, "Invalid string property 0x%04x", pname);
  625. }
  626. return value;
  627. }
  628. END_API_FUNC
  629. AL_API void AL_APIENTRY alDopplerFactor(ALfloat value)
  630. START_API_FUNC
  631. {
  632. ContextRef context{GetContextRef()};
  633. if UNLIKELY(!context) return;
  634. if(!(value >= 0.0f && std::isfinite(value)))
  635. context->setError(AL_INVALID_VALUE, "Doppler factor %f out of range", value);
  636. else
  637. {
  638. std::lock_guard<std::mutex> _{context->mPropLock};
  639. context->mDopplerFactor = value;
  640. DO_UPDATEPROPS();
  641. }
  642. }
  643. END_API_FUNC
  644. AL_API void AL_APIENTRY alDopplerVelocity(ALfloat value)
  645. START_API_FUNC
  646. {
  647. ContextRef context{GetContextRef()};
  648. if UNLIKELY(!context) return;
  649. if(!(value >= 0.0f && std::isfinite(value)))
  650. context->setError(AL_INVALID_VALUE, "Doppler velocity %f out of range", value);
  651. else
  652. {
  653. std::lock_guard<std::mutex> _{context->mPropLock};
  654. context->mDopplerVelocity = value;
  655. DO_UPDATEPROPS();
  656. }
  657. }
  658. END_API_FUNC
  659. AL_API void AL_APIENTRY alSpeedOfSound(ALfloat value)
  660. START_API_FUNC
  661. {
  662. ContextRef context{GetContextRef()};
  663. if UNLIKELY(!context) return;
  664. if(!(value > 0.0f && std::isfinite(value)))
  665. context->setError(AL_INVALID_VALUE, "Speed of sound %f out of range", value);
  666. else
  667. {
  668. std::lock_guard<std::mutex> _{context->mPropLock};
  669. context->mSpeedOfSound = value;
  670. DO_UPDATEPROPS();
  671. }
  672. }
  673. END_API_FUNC
  674. AL_API void AL_APIENTRY alDistanceModel(ALenum value)
  675. START_API_FUNC
  676. {
  677. ContextRef context{GetContextRef()};
  678. if UNLIKELY(!context) return;
  679. if(auto model = DistanceModelFromALenum(value))
  680. {
  681. std::lock_guard<std::mutex> _{context->mPropLock};
  682. context->mDistanceModel = *model;
  683. if(!context->mSourceDistanceModel)
  684. DO_UPDATEPROPS();
  685. }
  686. else
  687. context->setError(AL_INVALID_VALUE, "Distance model 0x%04x out of range", value);
  688. }
  689. END_API_FUNC
  690. AL_API void AL_APIENTRY alDeferUpdatesSOFT(void)
  691. START_API_FUNC
  692. {
  693. ContextRef context{GetContextRef()};
  694. if UNLIKELY(!context) return;
  695. context->deferUpdates();
  696. }
  697. END_API_FUNC
  698. AL_API void AL_APIENTRY alProcessUpdatesSOFT(void)
  699. START_API_FUNC
  700. {
  701. ContextRef context{GetContextRef()};
  702. if UNLIKELY(!context) return;
  703. context->processUpdates();
  704. }
  705. END_API_FUNC
  706. AL_API const ALchar* AL_APIENTRY alGetStringiSOFT(ALenum pname, ALsizei index)
  707. START_API_FUNC
  708. {
  709. ContextRef context{GetContextRef()};
  710. if UNLIKELY(!context) return nullptr;
  711. const ALchar *value{nullptr};
  712. switch(pname)
  713. {
  714. case AL_RESAMPLER_NAME_SOFT:
  715. if(index < 0 || index > static_cast<ALint>(Resampler::Max))
  716. context->setError(AL_INVALID_VALUE, "Resampler name index %d out of range", index);
  717. else
  718. value = GetResamplerName(static_cast<Resampler>(index));
  719. break;
  720. default:
  721. context->setError(AL_INVALID_VALUE, "Invalid string indexed property");
  722. }
  723. return value;
  724. }
  725. END_API_FUNC
  726. void UpdateContextProps(ALCcontext *context)
  727. {
  728. /* Get an unused proprty container, or allocate a new one as needed. */
  729. ContextProps *props{context->mFreeContextProps.load(std::memory_order_acquire)};
  730. if(!props)
  731. props = new ContextProps{};
  732. else
  733. {
  734. ContextProps *next;
  735. do {
  736. next = props->next.load(std::memory_order_relaxed);
  737. } while(context->mFreeContextProps.compare_exchange_weak(props, next,
  738. std::memory_order_seq_cst, std::memory_order_acquire) == 0);
  739. }
  740. /* Copy in current property values. */
  741. props->DopplerFactor = context->mDopplerFactor;
  742. props->DopplerVelocity = context->mDopplerVelocity;
  743. props->SpeedOfSound = context->mSpeedOfSound;
  744. props->SourceDistanceModel = context->mSourceDistanceModel;
  745. props->mDistanceModel = context->mDistanceModel;
  746. /* Set the new container for updating internal parameters. */
  747. props = context->mParams.ContextUpdate.exchange(props, std::memory_order_acq_rel);
  748. if(props)
  749. {
  750. /* If there was an unused update container, put it back in the
  751. * freelist.
  752. */
  753. AtomicReplaceHead(context->mFreeContextProps, props);
  754. }
  755. }