🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

6068 lines
188 KiB

  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 "source.h"
  22. #include <algorithm>
  23. #include <array>
  24. #include <atomic>
  25. #include <cassert>
  26. #include <chrono>
  27. #include <climits>
  28. #include <cmath>
  29. #include <cstdint>
  30. #include <functional>
  31. #include <iterator>
  32. #include <limits>
  33. #include <memory>
  34. #include <mutex>
  35. #include <new>
  36. #include <numeric>
  37. #include <stdexcept>
  38. #include <thread>
  39. #include <utility>
  40. #include "AL/al.h"
  41. #include "AL/alc.h"
  42. #include "AL/alext.h"
  43. #include "AL/efx.h"
  44. #include "albit.h"
  45. #include "alc/alu.h"
  46. #include "alc/backends/base.h"
  47. #include "alc/context.h"
  48. #include "alc/device.h"
  49. #include "alc/inprogext.h"
  50. #include "almalloc.h"
  51. #include "alnumeric.h"
  52. #include "aloptional.h"
  53. #include "alspan.h"
  54. #include "atomic.h"
  55. #include "auxeffectslot.h"
  56. #include "buffer.h"
  57. #include "core/ambidefs.h"
  58. #include "core/bformatdec.h"
  59. #include "core/except.h"
  60. #include "core/filters/nfc.h"
  61. #include "core/filters/splitter.h"
  62. #include "core/logging.h"
  63. #include "core/voice_change.h"
  64. #include "event.h"
  65. #include "filter.h"
  66. #include "opthelpers.h"
  67. #include "ringbuffer.h"
  68. #include "threads.h"
  69. #ifdef ALSOFT_EAX
  70. #include "eax_exception.h"
  71. #endif // ALSOFT_EAX
  72. namespace {
  73. using namespace std::placeholders;
  74. using std::chrono::nanoseconds;
  75. Voice *GetSourceVoice(ALsource *source, ALCcontext *context)
  76. {
  77. auto voicelist = context->getVoicesSpan();
  78. ALuint idx{source->VoiceIdx};
  79. if(idx < voicelist.size())
  80. {
  81. ALuint sid{source->id};
  82. Voice *voice = voicelist[idx];
  83. if(voice->mSourceID.load(std::memory_order_acquire) == sid)
  84. return voice;
  85. }
  86. source->VoiceIdx = INVALID_VOICE_IDX;
  87. return nullptr;
  88. }
  89. void UpdateSourceProps(const ALsource *source, Voice *voice, ALCcontext *context)
  90. {
  91. /* Get an unused property container, or allocate a new one as needed. */
  92. VoicePropsItem *props{context->mFreeVoiceProps.load(std::memory_order_acquire)};
  93. if(!props)
  94. {
  95. context->allocVoiceProps();
  96. props = context->mFreeVoiceProps.load(std::memory_order_acquire);
  97. }
  98. VoicePropsItem *next;
  99. do {
  100. next = props->next.load(std::memory_order_relaxed);
  101. } while(unlikely(context->mFreeVoiceProps.compare_exchange_weak(props, next,
  102. std::memory_order_acq_rel, std::memory_order_acquire) == false));
  103. props->Pitch = source->Pitch;
  104. props->Gain = source->Gain;
  105. props->OuterGain = source->OuterGain;
  106. props->MinGain = source->MinGain;
  107. props->MaxGain = source->MaxGain;
  108. props->InnerAngle = source->InnerAngle;
  109. props->OuterAngle = source->OuterAngle;
  110. props->RefDistance = source->RefDistance;
  111. props->MaxDistance = source->MaxDistance;
  112. props->RolloffFactor = source->RolloffFactor
  113. #ifdef ALSOFT_EAX
  114. + source->RolloffFactor2
  115. #endif
  116. ;
  117. props->Position = source->Position;
  118. props->Velocity = source->Velocity;
  119. props->Direction = source->Direction;
  120. props->OrientAt = source->OrientAt;
  121. props->OrientUp = source->OrientUp;
  122. props->HeadRelative = source->HeadRelative;
  123. props->mDistanceModel = source->mDistanceModel;
  124. props->mResampler = source->mResampler;
  125. props->DirectChannels = source->DirectChannels;
  126. props->mSpatializeMode = source->mSpatialize;
  127. props->DryGainHFAuto = source->DryGainHFAuto;
  128. props->WetGainAuto = source->WetGainAuto;
  129. props->WetGainHFAuto = source->WetGainHFAuto;
  130. props->OuterGainHF = source->OuterGainHF;
  131. props->AirAbsorptionFactor = source->AirAbsorptionFactor;
  132. props->RoomRolloffFactor = source->RoomRolloffFactor;
  133. props->DopplerFactor = source->DopplerFactor;
  134. props->StereoPan = source->StereoPan;
  135. props->Radius = source->Radius;
  136. props->EnhWidth = source->EnhWidth;
  137. props->Direct.Gain = source->Direct.Gain;
  138. props->Direct.GainHF = source->Direct.GainHF;
  139. props->Direct.HFReference = source->Direct.HFReference;
  140. props->Direct.GainLF = source->Direct.GainLF;
  141. props->Direct.LFReference = source->Direct.LFReference;
  142. auto copy_send = [](const ALsource::SendData &srcsend) noexcept -> VoiceProps::SendData
  143. {
  144. VoiceProps::SendData ret{};
  145. ret.Slot = srcsend.Slot ? &srcsend.Slot->mSlot : nullptr;
  146. ret.Gain = srcsend.Gain;
  147. ret.GainHF = srcsend.GainHF;
  148. ret.HFReference = srcsend.HFReference;
  149. ret.GainLF = srcsend.GainLF;
  150. ret.LFReference = srcsend.LFReference;
  151. return ret;
  152. };
  153. std::transform(source->Send.cbegin(), source->Send.cend(), props->Send, copy_send);
  154. if(!props->Send[0].Slot && context->mDefaultSlot)
  155. props->Send[0].Slot = &context->mDefaultSlot->mSlot;
  156. /* Set the new container for updating internal parameters. */
  157. props = voice->mUpdate.exchange(props, std::memory_order_acq_rel);
  158. if(props)
  159. {
  160. /* If there was an unused update container, put it back in the
  161. * freelist.
  162. */
  163. AtomicReplaceHead(context->mFreeVoiceProps, props);
  164. }
  165. }
  166. /* GetSourceSampleOffset
  167. *
  168. * Gets the current read offset for the given Source, in 32.32 fixed-point
  169. * samples. The offset is relative to the start of the queue (not the start of
  170. * the current buffer).
  171. */
  172. int64_t GetSourceSampleOffset(ALsource *Source, ALCcontext *context, nanoseconds *clocktime)
  173. {
  174. ALCdevice *device{context->mALDevice.get()};
  175. const VoiceBufferItem *Current{};
  176. uint64_t readPos{};
  177. ALuint refcount;
  178. Voice *voice;
  179. do {
  180. refcount = device->waitForMix();
  181. *clocktime = GetDeviceClockTime(device);
  182. voice = GetSourceVoice(Source, context);
  183. if(voice)
  184. {
  185. Current = voice->mCurrentBuffer.load(std::memory_order_relaxed);
  186. readPos = uint64_t{voice->mPosition.load(std::memory_order_relaxed)} << 32;
  187. readPos |= uint64_t{voice->mPositionFrac.load(std::memory_order_relaxed)} <<
  188. (32-MixerFracBits);
  189. }
  190. std::atomic_thread_fence(std::memory_order_acquire);
  191. } while(refcount != device->MixCount.load(std::memory_order_relaxed));
  192. if(!voice)
  193. return 0;
  194. for(auto &item : Source->mQueue)
  195. {
  196. if(&item == Current) break;
  197. readPos += uint64_t{item.mSampleLen} << 32;
  198. }
  199. return static_cast<int64_t>(minu64(readPos, 0x7fffffffffffffff_u64));
  200. }
  201. /* GetSourceSecOffset
  202. *
  203. * Gets the current read offset for the given Source, in seconds. The offset is
  204. * relative to the start of the queue (not the start of the current buffer).
  205. */
  206. double GetSourceSecOffset(ALsource *Source, ALCcontext *context, nanoseconds *clocktime)
  207. {
  208. ALCdevice *device{context->mALDevice.get()};
  209. const VoiceBufferItem *Current{};
  210. uint64_t readPos{};
  211. ALuint refcount;
  212. Voice *voice;
  213. do {
  214. refcount = device->waitForMix();
  215. *clocktime = GetDeviceClockTime(device);
  216. voice = GetSourceVoice(Source, context);
  217. if(voice)
  218. {
  219. Current = voice->mCurrentBuffer.load(std::memory_order_relaxed);
  220. readPos = uint64_t{voice->mPosition.load(std::memory_order_relaxed)} << MixerFracBits;
  221. readPos |= voice->mPositionFrac.load(std::memory_order_relaxed);
  222. }
  223. std::atomic_thread_fence(std::memory_order_acquire);
  224. } while(refcount != device->MixCount.load(std::memory_order_relaxed));
  225. if(!voice)
  226. return 0.0f;
  227. const ALbuffer *BufferFmt{nullptr};
  228. auto BufferList = Source->mQueue.cbegin();
  229. while(BufferList != Source->mQueue.cend() && std::addressof(*BufferList) != Current)
  230. {
  231. if(!BufferFmt) BufferFmt = BufferList->mBuffer;
  232. readPos += uint64_t{BufferList->mSampleLen} << MixerFracBits;
  233. ++BufferList;
  234. }
  235. while(BufferList != Source->mQueue.cend() && !BufferFmt)
  236. {
  237. BufferFmt = BufferList->mBuffer;
  238. ++BufferList;
  239. }
  240. ASSUME(BufferFmt != nullptr);
  241. return static_cast<double>(readPos) / double{MixerFracOne} / BufferFmt->mSampleRate;
  242. }
  243. /* GetSourceOffset
  244. *
  245. * Gets the current read offset for the given Source, in the appropriate format
  246. * (Bytes, Samples or Seconds). The offset is relative to the start of the
  247. * queue (not the start of the current buffer).
  248. */
  249. double GetSourceOffset(ALsource *Source, ALenum name, ALCcontext *context)
  250. {
  251. ALCdevice *device{context->mALDevice.get()};
  252. const VoiceBufferItem *Current{};
  253. ALuint readPos{};
  254. ALuint readPosFrac{};
  255. ALuint refcount;
  256. Voice *voice;
  257. do {
  258. refcount = device->waitForMix();
  259. voice = GetSourceVoice(Source, context);
  260. if(voice)
  261. {
  262. Current = voice->mCurrentBuffer.load(std::memory_order_relaxed);
  263. readPos = voice->mPosition.load(std::memory_order_relaxed);
  264. readPosFrac = voice->mPositionFrac.load(std::memory_order_relaxed);
  265. }
  266. std::atomic_thread_fence(std::memory_order_acquire);
  267. } while(refcount != device->MixCount.load(std::memory_order_relaxed));
  268. if(!voice)
  269. return 0.0;
  270. const ALbuffer *BufferFmt{nullptr};
  271. auto BufferList = Source->mQueue.cbegin();
  272. while(BufferList != Source->mQueue.cend() && std::addressof(*BufferList) != Current)
  273. {
  274. if(!BufferFmt) BufferFmt = BufferList->mBuffer;
  275. readPos += BufferList->mSampleLen;
  276. ++BufferList;
  277. }
  278. while(BufferList != Source->mQueue.cend() && !BufferFmt)
  279. {
  280. BufferFmt = BufferList->mBuffer;
  281. ++BufferList;
  282. }
  283. ASSUME(BufferFmt != nullptr);
  284. double offset{};
  285. switch(name)
  286. {
  287. case AL_SEC_OFFSET:
  288. offset = (readPos + readPosFrac/double{MixerFracOne}) / BufferFmt->mSampleRate;
  289. break;
  290. case AL_SAMPLE_OFFSET:
  291. offset = readPos + readPosFrac/double{MixerFracOne};
  292. break;
  293. case AL_BYTE_OFFSET:
  294. if(BufferFmt->OriginalType == UserFmtIMA4)
  295. {
  296. ALuint FrameBlockSize{BufferFmt->OriginalAlign};
  297. ALuint align{(BufferFmt->OriginalAlign-1)/2 + 4};
  298. ALuint BlockSize{align * BufferFmt->channelsFromFmt()};
  299. /* Round down to nearest ADPCM block */
  300. offset = static_cast<double>(readPos / FrameBlockSize * BlockSize);
  301. }
  302. else if(BufferFmt->OriginalType == UserFmtMSADPCM)
  303. {
  304. ALuint FrameBlockSize{BufferFmt->OriginalAlign};
  305. ALuint align{(FrameBlockSize-2)/2 + 7};
  306. ALuint BlockSize{align * BufferFmt->channelsFromFmt()};
  307. /* Round down to nearest ADPCM block */
  308. offset = static_cast<double>(readPos / FrameBlockSize * BlockSize);
  309. }
  310. else
  311. {
  312. const ALuint FrameSize{BufferFmt->frameSizeFromFmt()};
  313. offset = static_cast<double>(readPos * FrameSize);
  314. }
  315. break;
  316. }
  317. return offset;
  318. }
  319. /* GetSourceLength
  320. *
  321. * Gets the length of the given Source's buffer queue, in the appropriate
  322. * format (Bytes, Samples or Seconds).
  323. */
  324. double GetSourceLength(const ALsource *source, ALenum name)
  325. {
  326. uint64_t length{0};
  327. const ALbuffer *BufferFmt{nullptr};
  328. for(auto &listitem : source->mQueue)
  329. {
  330. if(!BufferFmt)
  331. BufferFmt = listitem.mBuffer;
  332. length += listitem.mSampleLen;
  333. }
  334. if(length == 0)
  335. return 0.0;
  336. ASSUME(BufferFmt != nullptr);
  337. switch(name)
  338. {
  339. case AL_SEC_LENGTH_SOFT:
  340. return static_cast<double>(length) / BufferFmt->mSampleRate;
  341. case AL_SAMPLE_LENGTH_SOFT:
  342. return static_cast<double>(length);
  343. case AL_BYTE_LENGTH_SOFT:
  344. if(BufferFmt->OriginalType == UserFmtIMA4)
  345. {
  346. ALuint FrameBlockSize{BufferFmt->OriginalAlign};
  347. ALuint align{(BufferFmt->OriginalAlign-1)/2 + 4};
  348. ALuint BlockSize{align * BufferFmt->channelsFromFmt()};
  349. /* Round down to nearest ADPCM block */
  350. return static_cast<double>(length / FrameBlockSize) * BlockSize;
  351. }
  352. else if(BufferFmt->OriginalType == UserFmtMSADPCM)
  353. {
  354. ALuint FrameBlockSize{BufferFmt->OriginalAlign};
  355. ALuint align{(FrameBlockSize-2)/2 + 7};
  356. ALuint BlockSize{align * BufferFmt->channelsFromFmt()};
  357. /* Round down to nearest ADPCM block */
  358. return static_cast<double>(length / FrameBlockSize) * BlockSize;
  359. }
  360. return static_cast<double>(length) * BufferFmt->frameSizeFromFmt();
  361. }
  362. return 0.0;
  363. }
  364. struct VoicePos {
  365. ALuint pos, frac;
  366. ALbufferQueueItem *bufferitem;
  367. };
  368. /**
  369. * GetSampleOffset
  370. *
  371. * Retrieves the voice position, fixed-point fraction, and bufferlist item
  372. * using the givem offset type and offset. If the offset is out of range,
  373. * returns an empty optional.
  374. */
  375. al::optional<VoicePos> GetSampleOffset(al::deque<ALbufferQueueItem> &BufferList, ALenum OffsetType,
  376. double Offset)
  377. {
  378. /* Find the first valid Buffer in the Queue */
  379. const ALbuffer *BufferFmt{nullptr};
  380. for(auto &item : BufferList)
  381. {
  382. BufferFmt = item.mBuffer;
  383. if(BufferFmt) break;
  384. }
  385. if(!BufferFmt || BufferFmt->mCallback)
  386. return al::nullopt;
  387. /* Get sample frame offset */
  388. ALuint offset{0u}, frac{0u};
  389. double dbloff, dblfrac;
  390. switch(OffsetType)
  391. {
  392. case AL_SEC_OFFSET:
  393. dblfrac = std::modf(Offset*BufferFmt->mSampleRate, &dbloff);
  394. offset = static_cast<ALuint>(mind(dbloff, std::numeric_limits<ALuint>::max()));
  395. frac = static_cast<ALuint>(mind(dblfrac*MixerFracOne, MixerFracOne-1.0));
  396. break;
  397. case AL_SAMPLE_OFFSET:
  398. dblfrac = std::modf(Offset, &dbloff);
  399. offset = static_cast<ALuint>(mind(dbloff, std::numeric_limits<ALuint>::max()));
  400. frac = static_cast<ALuint>(mind(dblfrac*MixerFracOne, MixerFracOne-1.0));
  401. break;
  402. case AL_BYTE_OFFSET:
  403. /* Determine the ByteOffset (and ensure it is block aligned) */
  404. offset = static_cast<ALuint>(Offset);
  405. if(BufferFmt->OriginalType == UserFmtIMA4)
  406. {
  407. const ALuint align{(BufferFmt->OriginalAlign-1)/2 + 4};
  408. offset /= align * BufferFmt->channelsFromFmt();
  409. offset *= BufferFmt->OriginalAlign;
  410. }
  411. else if(BufferFmt->OriginalType == UserFmtMSADPCM)
  412. {
  413. const ALuint align{(BufferFmt->OriginalAlign-2)/2 + 7};
  414. offset /= align * BufferFmt->channelsFromFmt();
  415. offset *= BufferFmt->OriginalAlign;
  416. }
  417. else
  418. offset /= BufferFmt->frameSizeFromFmt();
  419. frac = 0;
  420. break;
  421. }
  422. /* Find the bufferlist item this offset belongs to. */
  423. ALuint totalBufferLen{0u};
  424. for(auto &item : BufferList)
  425. {
  426. if(totalBufferLen > offset)
  427. break;
  428. if(item.mSampleLen > offset-totalBufferLen)
  429. {
  430. /* Offset is in this buffer */
  431. return VoicePos{offset-totalBufferLen, frac, &item};
  432. }
  433. totalBufferLen += item.mSampleLen;
  434. }
  435. /* Offset is out of range of the queue */
  436. return al::nullopt;
  437. }
  438. void InitVoice(Voice *voice, ALsource *source, ALbufferQueueItem *BufferList, ALCcontext *context,
  439. ALCdevice *device)
  440. {
  441. voice->mLoopBuffer.store(source->Looping ? &source->mQueue.front() : nullptr,
  442. std::memory_order_relaxed);
  443. ALbuffer *buffer{BufferList->mBuffer};
  444. voice->mFrequency = buffer->mSampleRate;
  445. voice->mFmtChannels =
  446. (buffer->mChannels == FmtStereo && source->mStereoMode == SourceStereo::Enhanced) ?
  447. FmtSuperStereo : buffer->mChannels;
  448. voice->mFmtType = buffer->mType;
  449. voice->mFrameStep = buffer->channelsFromFmt();
  450. voice->mFrameSize = buffer->frameSizeFromFmt();
  451. voice->mAmbiLayout = IsUHJ(voice->mFmtChannels) ? AmbiLayout::FuMa : buffer->mAmbiLayout;
  452. voice->mAmbiScaling = IsUHJ(voice->mFmtChannels) ? AmbiScaling::UHJ : buffer->mAmbiScaling;
  453. voice->mAmbiOrder = (voice->mFmtChannels == FmtSuperStereo) ? 1 : buffer->mAmbiOrder;
  454. if(buffer->mCallback) voice->mFlags.set(VoiceIsCallback);
  455. else if(source->SourceType == AL_STATIC) voice->mFlags.set(VoiceIsStatic);
  456. voice->mNumCallbackSamples = 0;
  457. voice->prepare(device);
  458. source->mPropsDirty = false;
  459. UpdateSourceProps(source, voice, context);
  460. voice->mSourceID.store(source->id, std::memory_order_release);
  461. }
  462. VoiceChange *GetVoiceChanger(ALCcontext *ctx)
  463. {
  464. VoiceChange *vchg{ctx->mVoiceChangeTail};
  465. if UNLIKELY(vchg == ctx->mCurrentVoiceChange.load(std::memory_order_acquire))
  466. {
  467. ctx->allocVoiceChanges();
  468. vchg = ctx->mVoiceChangeTail;
  469. }
  470. ctx->mVoiceChangeTail = vchg->mNext.exchange(nullptr, std::memory_order_relaxed);
  471. return vchg;
  472. }
  473. void SendVoiceChanges(ALCcontext *ctx, VoiceChange *tail)
  474. {
  475. ALCdevice *device{ctx->mALDevice.get()};
  476. VoiceChange *oldhead{ctx->mCurrentVoiceChange.load(std::memory_order_acquire)};
  477. while(VoiceChange *next{oldhead->mNext.load(std::memory_order_relaxed)})
  478. oldhead = next;
  479. oldhead->mNext.store(tail, std::memory_order_release);
  480. const bool connected{device->Connected.load(std::memory_order_acquire)};
  481. device->waitForMix();
  482. if UNLIKELY(!connected)
  483. {
  484. if(ctx->mStopVoicesOnDisconnect.load(std::memory_order_acquire))
  485. {
  486. /* If the device is disconnected and voices are stopped, just
  487. * ignore all pending changes.
  488. */
  489. VoiceChange *cur{ctx->mCurrentVoiceChange.load(std::memory_order_acquire)};
  490. while(VoiceChange *next{cur->mNext.load(std::memory_order_acquire)})
  491. {
  492. cur = next;
  493. if(Voice *voice{cur->mVoice})
  494. voice->mSourceID.store(0, std::memory_order_relaxed);
  495. }
  496. ctx->mCurrentVoiceChange.store(cur, std::memory_order_release);
  497. }
  498. }
  499. }
  500. bool SetVoiceOffset(Voice *oldvoice, const VoicePos &vpos, ALsource *source, ALCcontext *context,
  501. ALCdevice *device)
  502. {
  503. /* First, get a free voice to start at the new offset. */
  504. auto voicelist = context->getVoicesSpan();
  505. Voice *newvoice{};
  506. ALuint vidx{0};
  507. for(Voice *voice : voicelist)
  508. {
  509. if(voice->mPlayState.load(std::memory_order_acquire) == Voice::Stopped
  510. && voice->mSourceID.load(std::memory_order_relaxed) == 0u
  511. && voice->mPendingChange.load(std::memory_order_relaxed) == false)
  512. {
  513. newvoice = voice;
  514. break;
  515. }
  516. ++vidx;
  517. }
  518. if(unlikely(!newvoice))
  519. {
  520. auto &allvoices = *context->mVoices.load(std::memory_order_relaxed);
  521. if(allvoices.size() == voicelist.size())
  522. context->allocVoices(1);
  523. context->mActiveVoiceCount.fetch_add(1, std::memory_order_release);
  524. voicelist = context->getVoicesSpan();
  525. vidx = 0;
  526. for(Voice *voice : voicelist)
  527. {
  528. if(voice->mPlayState.load(std::memory_order_acquire) == Voice::Stopped
  529. && voice->mSourceID.load(std::memory_order_relaxed) == 0u
  530. && voice->mPendingChange.load(std::memory_order_relaxed) == false)
  531. {
  532. newvoice = voice;
  533. break;
  534. }
  535. ++vidx;
  536. }
  537. ASSUME(newvoice != nullptr);
  538. }
  539. /* Initialize the new voice and set its starting offset.
  540. * TODO: It might be better to have the VoiceChange processing copy the old
  541. * voice's mixing parameters (and pending update) insead of initializing it
  542. * all here. This would just need to set the minimum properties to link the
  543. * voice to the source and its position-dependent properties (including the
  544. * fading flag).
  545. */
  546. newvoice->mPlayState.store(Voice::Pending, std::memory_order_relaxed);
  547. newvoice->mPosition.store(vpos.pos, std::memory_order_relaxed);
  548. newvoice->mPositionFrac.store(vpos.frac, std::memory_order_relaxed);
  549. newvoice->mCurrentBuffer.store(vpos.bufferitem, std::memory_order_relaxed);
  550. newvoice->mFlags.reset();
  551. if(vpos.pos > 0 || vpos.frac > 0 || vpos.bufferitem != &source->mQueue.front())
  552. newvoice->mFlags.set(VoiceIsFading);
  553. InitVoice(newvoice, source, vpos.bufferitem, context, device);
  554. source->VoiceIdx = vidx;
  555. /* Set the old voice as having a pending change, and send it off with the
  556. * new one with a new offset voice change.
  557. */
  558. oldvoice->mPendingChange.store(true, std::memory_order_relaxed);
  559. VoiceChange *vchg{GetVoiceChanger(context)};
  560. vchg->mOldVoice = oldvoice;
  561. vchg->mVoice = newvoice;
  562. vchg->mSourceID = source->id;
  563. vchg->mState = VChangeState::Restart;
  564. SendVoiceChanges(context, vchg);
  565. /* If the old voice still has a sourceID, it's still active and the change-
  566. * over will work on the next update.
  567. */
  568. if LIKELY(oldvoice->mSourceID.load(std::memory_order_acquire) != 0u)
  569. return true;
  570. /* Otherwise, if the new voice's state is not pending, the change-over
  571. * already happened.
  572. */
  573. if(newvoice->mPlayState.load(std::memory_order_acquire) != Voice::Pending)
  574. return true;
  575. /* Otherwise, wait for any current mix to finish and check one last time. */
  576. device->waitForMix();
  577. if(newvoice->mPlayState.load(std::memory_order_acquire) != Voice::Pending)
  578. return true;
  579. /* The change-over failed because the old voice stopped before the new
  580. * voice could start at the new offset. Let go of the new voice and have
  581. * the caller store the source offset since it's stopped.
  582. */
  583. newvoice->mCurrentBuffer.store(nullptr, std::memory_order_relaxed);
  584. newvoice->mLoopBuffer.store(nullptr, std::memory_order_relaxed);
  585. newvoice->mSourceID.store(0u, std::memory_order_relaxed);
  586. newvoice->mPlayState.store(Voice::Stopped, std::memory_order_relaxed);
  587. return false;
  588. }
  589. /**
  590. * Returns if the last known state for the source was playing or paused. Does
  591. * not sync with the mixer voice.
  592. */
  593. inline bool IsPlayingOrPaused(ALsource *source)
  594. { return source->state == AL_PLAYING || source->state == AL_PAUSED; }
  595. /**
  596. * Returns an updated source state using the matching voice's status (or lack
  597. * thereof).
  598. */
  599. inline ALenum GetSourceState(ALsource *source, Voice *voice)
  600. {
  601. if(!voice && source->state == AL_PLAYING)
  602. source->state = AL_STOPPED;
  603. return source->state;
  604. }
  605. bool EnsureSources(ALCcontext *context, size_t needed)
  606. {
  607. size_t count{std::accumulate(context->mSourceList.cbegin(), context->mSourceList.cend(),
  608. size_t{0},
  609. [](size_t cur, const SourceSubList &sublist) noexcept -> size_t
  610. { return cur + static_cast<ALuint>(al::popcount(sublist.FreeMask)); })};
  611. while(needed > count)
  612. {
  613. if UNLIKELY(context->mSourceList.size() >= 1<<25)
  614. return false;
  615. context->mSourceList.emplace_back();
  616. auto sublist = context->mSourceList.end() - 1;
  617. sublist->FreeMask = ~0_u64;
  618. sublist->Sources = static_cast<ALsource*>(al_calloc(alignof(ALsource), sizeof(ALsource)*64));
  619. if UNLIKELY(!sublist->Sources)
  620. {
  621. context->mSourceList.pop_back();
  622. return false;
  623. }
  624. count += 64;
  625. }
  626. return true;
  627. }
  628. ALsource *AllocSource(ALCcontext *context)
  629. {
  630. auto sublist = std::find_if(context->mSourceList.begin(), context->mSourceList.end(),
  631. [](const SourceSubList &entry) noexcept -> bool
  632. { return entry.FreeMask != 0; });
  633. auto lidx = static_cast<ALuint>(std::distance(context->mSourceList.begin(), sublist));
  634. auto slidx = static_cast<ALuint>(al::countr_zero(sublist->FreeMask));
  635. ASSUME(slidx < 64);
  636. ALsource *source{al::construct_at(sublist->Sources + slidx)};
  637. /* Add 1 to avoid source ID 0. */
  638. source->id = ((lidx<<6) | slidx) + 1;
  639. context->mNumSources += 1;
  640. sublist->FreeMask &= ~(1_u64 << slidx);
  641. return source;
  642. }
  643. void FreeSource(ALCcontext *context, ALsource *source)
  644. {
  645. const ALuint id{source->id - 1};
  646. const size_t lidx{id >> 6};
  647. const ALuint slidx{id & 0x3f};
  648. if(Voice *voice{GetSourceVoice(source, context)})
  649. {
  650. VoiceChange *vchg{GetVoiceChanger(context)};
  651. voice->mPendingChange.store(true, std::memory_order_relaxed);
  652. vchg->mVoice = voice;
  653. vchg->mSourceID = source->id;
  654. vchg->mState = VChangeState::Stop;
  655. SendVoiceChanges(context, vchg);
  656. }
  657. al::destroy_at(source);
  658. context->mSourceList[lidx].FreeMask |= 1_u64 << slidx;
  659. context->mNumSources--;
  660. }
  661. inline ALsource *LookupSource(ALCcontext *context, ALuint id) noexcept
  662. {
  663. const size_t lidx{(id-1) >> 6};
  664. const ALuint slidx{(id-1) & 0x3f};
  665. if UNLIKELY(lidx >= context->mSourceList.size())
  666. return nullptr;
  667. SourceSubList &sublist{context->mSourceList[lidx]};
  668. if UNLIKELY(sublist.FreeMask & (1_u64 << slidx))
  669. return nullptr;
  670. return sublist.Sources + slidx;
  671. }
  672. inline ALbuffer *LookupBuffer(ALCdevice *device, ALuint id) noexcept
  673. {
  674. const size_t lidx{(id-1) >> 6};
  675. const ALuint slidx{(id-1) & 0x3f};
  676. if UNLIKELY(lidx >= device->BufferList.size())
  677. return nullptr;
  678. BufferSubList &sublist = device->BufferList[lidx];
  679. if UNLIKELY(sublist.FreeMask & (1_u64 << slidx))
  680. return nullptr;
  681. return sublist.Buffers + slidx;
  682. }
  683. inline ALfilter *LookupFilter(ALCdevice *device, ALuint id) noexcept
  684. {
  685. const size_t lidx{(id-1) >> 6};
  686. const ALuint slidx{(id-1) & 0x3f};
  687. if UNLIKELY(lidx >= device->FilterList.size())
  688. return nullptr;
  689. FilterSubList &sublist = device->FilterList[lidx];
  690. if UNLIKELY(sublist.FreeMask & (1_u64 << slidx))
  691. return nullptr;
  692. return sublist.Filters + slidx;
  693. }
  694. inline ALeffectslot *LookupEffectSlot(ALCcontext *context, ALuint id) noexcept
  695. {
  696. const size_t lidx{(id-1) >> 6};
  697. const ALuint slidx{(id-1) & 0x3f};
  698. if UNLIKELY(lidx >= context->mEffectSlotList.size())
  699. return nullptr;
  700. EffectSlotSubList &sublist{context->mEffectSlotList[lidx]};
  701. if UNLIKELY(sublist.FreeMask & (1_u64 << slidx))
  702. return nullptr;
  703. return sublist.EffectSlots + slidx;
  704. }
  705. al::optional<SourceStereo> StereoModeFromEnum(ALenum mode)
  706. {
  707. switch(mode)
  708. {
  709. case AL_NORMAL_SOFT: return al::make_optional(SourceStereo::Normal);
  710. case AL_SUPER_STEREO_SOFT: return al::make_optional(SourceStereo::Enhanced);
  711. }
  712. WARN("Unsupported stereo mode: 0x%04x\n", mode);
  713. return al::nullopt;
  714. }
  715. ALenum EnumFromStereoMode(SourceStereo mode)
  716. {
  717. switch(mode)
  718. {
  719. case SourceStereo::Normal: return AL_NORMAL_SOFT;
  720. case SourceStereo::Enhanced: return AL_SUPER_STEREO_SOFT;
  721. }
  722. throw std::runtime_error{"Invalid SourceStereo: "+std::to_string(int(mode))};
  723. }
  724. al::optional<SpatializeMode> SpatializeModeFromEnum(ALenum mode)
  725. {
  726. switch(mode)
  727. {
  728. case AL_FALSE: return al::make_optional(SpatializeMode::Off);
  729. case AL_TRUE: return al::make_optional(SpatializeMode::On);
  730. case AL_AUTO_SOFT: return al::make_optional(SpatializeMode::Auto);
  731. }
  732. WARN("Unsupported spatialize mode: 0x%04x\n", mode);
  733. return al::nullopt;
  734. }
  735. ALenum EnumFromSpatializeMode(SpatializeMode mode)
  736. {
  737. switch(mode)
  738. {
  739. case SpatializeMode::Off: return AL_FALSE;
  740. case SpatializeMode::On: return AL_TRUE;
  741. case SpatializeMode::Auto: return AL_AUTO_SOFT;
  742. }
  743. throw std::runtime_error{"Invalid SpatializeMode: "+std::to_string(int(mode))};
  744. }
  745. al::optional<DirectMode> DirectModeFromEnum(ALenum mode)
  746. {
  747. switch(mode)
  748. {
  749. case AL_FALSE: return al::make_optional(DirectMode::Off);
  750. case AL_DROP_UNMATCHED_SOFT: return al::make_optional(DirectMode::DropMismatch);
  751. case AL_REMIX_UNMATCHED_SOFT: return al::make_optional(DirectMode::RemixMismatch);
  752. }
  753. WARN("Unsupported direct mode: 0x%04x\n", mode);
  754. return al::nullopt;
  755. }
  756. ALenum EnumFromDirectMode(DirectMode mode)
  757. {
  758. switch(mode)
  759. {
  760. case DirectMode::Off: return AL_FALSE;
  761. case DirectMode::DropMismatch: return AL_DROP_UNMATCHED_SOFT;
  762. case DirectMode::RemixMismatch: return AL_REMIX_UNMATCHED_SOFT;
  763. }
  764. throw std::runtime_error{"Invalid DirectMode: "+std::to_string(int(mode))};
  765. }
  766. al::optional<DistanceModel> DistanceModelFromALenum(ALenum model)
  767. {
  768. switch(model)
  769. {
  770. case AL_NONE: return al::make_optional(DistanceModel::Disable);
  771. case AL_INVERSE_DISTANCE: return al::make_optional(DistanceModel::Inverse);
  772. case AL_INVERSE_DISTANCE_CLAMPED: return al::make_optional(DistanceModel::InverseClamped);
  773. case AL_LINEAR_DISTANCE: return al::make_optional(DistanceModel::Linear);
  774. case AL_LINEAR_DISTANCE_CLAMPED: return al::make_optional(DistanceModel::LinearClamped);
  775. case AL_EXPONENT_DISTANCE: return al::make_optional(DistanceModel::Exponent);
  776. case AL_EXPONENT_DISTANCE_CLAMPED: return al::make_optional(DistanceModel::ExponentClamped);
  777. }
  778. return al::nullopt;
  779. }
  780. ALenum ALenumFromDistanceModel(DistanceModel model)
  781. {
  782. switch(model)
  783. {
  784. case DistanceModel::Disable: return AL_NONE;
  785. case DistanceModel::Inverse: return AL_INVERSE_DISTANCE;
  786. case DistanceModel::InverseClamped: return AL_INVERSE_DISTANCE_CLAMPED;
  787. case DistanceModel::Linear: return AL_LINEAR_DISTANCE;
  788. case DistanceModel::LinearClamped: return AL_LINEAR_DISTANCE_CLAMPED;
  789. case DistanceModel::Exponent: return AL_EXPONENT_DISTANCE;
  790. case DistanceModel::ExponentClamped: return AL_EXPONENT_DISTANCE_CLAMPED;
  791. }
  792. throw std::runtime_error{"Unexpected distance model "+std::to_string(static_cast<int>(model))};
  793. }
  794. enum SourceProp : ALenum {
  795. srcPitch = AL_PITCH,
  796. srcGain = AL_GAIN,
  797. srcMinGain = AL_MIN_GAIN,
  798. srcMaxGain = AL_MAX_GAIN,
  799. srcMaxDistance = AL_MAX_DISTANCE,
  800. srcRolloffFactor = AL_ROLLOFF_FACTOR,
  801. srcDopplerFactor = AL_DOPPLER_FACTOR,
  802. srcConeOuterGain = AL_CONE_OUTER_GAIN,
  803. srcSecOffset = AL_SEC_OFFSET,
  804. srcSampleOffset = AL_SAMPLE_OFFSET,
  805. srcByteOffset = AL_BYTE_OFFSET,
  806. srcConeInnerAngle = AL_CONE_INNER_ANGLE,
  807. srcConeOuterAngle = AL_CONE_OUTER_ANGLE,
  808. srcRefDistance = AL_REFERENCE_DISTANCE,
  809. srcPosition = AL_POSITION,
  810. srcVelocity = AL_VELOCITY,
  811. srcDirection = AL_DIRECTION,
  812. srcSourceRelative = AL_SOURCE_RELATIVE,
  813. srcLooping = AL_LOOPING,
  814. srcBuffer = AL_BUFFER,
  815. srcSourceState = AL_SOURCE_STATE,
  816. srcBuffersQueued = AL_BUFFERS_QUEUED,
  817. srcBuffersProcessed = AL_BUFFERS_PROCESSED,
  818. srcSourceType = AL_SOURCE_TYPE,
  819. /* ALC_EXT_EFX */
  820. srcConeOuterGainHF = AL_CONE_OUTER_GAINHF,
  821. srcAirAbsorptionFactor = AL_AIR_ABSORPTION_FACTOR,
  822. srcRoomRolloffFactor = AL_ROOM_ROLLOFF_FACTOR,
  823. srcDirectFilterGainHFAuto = AL_DIRECT_FILTER_GAINHF_AUTO,
  824. srcAuxSendFilterGainAuto = AL_AUXILIARY_SEND_FILTER_GAIN_AUTO,
  825. srcAuxSendFilterGainHFAuto = AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO,
  826. srcDirectFilter = AL_DIRECT_FILTER,
  827. srcAuxSendFilter = AL_AUXILIARY_SEND_FILTER,
  828. /* AL_SOFT_direct_channels */
  829. srcDirectChannelsSOFT = AL_DIRECT_CHANNELS_SOFT,
  830. /* AL_EXT_source_distance_model */
  831. srcDistanceModel = AL_DISTANCE_MODEL,
  832. /* AL_SOFT_source_latency */
  833. srcSampleOffsetLatencySOFT = AL_SAMPLE_OFFSET_LATENCY_SOFT,
  834. srcSecOffsetLatencySOFT = AL_SEC_OFFSET_LATENCY_SOFT,
  835. /* AL_EXT_STEREO_ANGLES */
  836. srcAngles = AL_STEREO_ANGLES,
  837. /* AL_EXT_SOURCE_RADIUS */
  838. srcRadius = AL_SOURCE_RADIUS,
  839. /* AL_EXT_BFORMAT */
  840. srcOrientation = AL_ORIENTATION,
  841. /* AL_SOFT_source_length */
  842. srcByteLength = AL_BYTE_LENGTH_SOFT,
  843. srcSampleLength = AL_SAMPLE_LENGTH_SOFT,
  844. srcSecLength = AL_SEC_LENGTH_SOFT,
  845. /* AL_SOFT_source_resampler */
  846. srcResampler = AL_SOURCE_RESAMPLER_SOFT,
  847. /* AL_SOFT_source_spatialize */
  848. srcSpatialize = AL_SOURCE_SPATIALIZE_SOFT,
  849. /* ALC_SOFT_device_clock */
  850. srcSampleOffsetClockSOFT = AL_SAMPLE_OFFSET_CLOCK_SOFT,
  851. srcSecOffsetClockSOFT = AL_SEC_OFFSET_CLOCK_SOFT,
  852. /* AL_SOFT_UHJ */
  853. srcStereoMode = AL_STEREO_MODE_SOFT,
  854. srcSuperStereoWidth = AL_SUPER_STEREO_WIDTH_SOFT,
  855. };
  856. constexpr size_t MaxValues{6u};
  857. ALuint FloatValsByProp(ALenum prop)
  858. {
  859. switch(static_cast<SourceProp>(prop))
  860. {
  861. case AL_PITCH:
  862. case AL_GAIN:
  863. case AL_MIN_GAIN:
  864. case AL_MAX_GAIN:
  865. case AL_MAX_DISTANCE:
  866. case AL_ROLLOFF_FACTOR:
  867. case AL_DOPPLER_FACTOR:
  868. case AL_CONE_OUTER_GAIN:
  869. case AL_SEC_OFFSET:
  870. case AL_SAMPLE_OFFSET:
  871. case AL_BYTE_OFFSET:
  872. case AL_CONE_INNER_ANGLE:
  873. case AL_CONE_OUTER_ANGLE:
  874. case AL_REFERENCE_DISTANCE:
  875. case AL_CONE_OUTER_GAINHF:
  876. case AL_AIR_ABSORPTION_FACTOR:
  877. case AL_ROOM_ROLLOFF_FACTOR:
  878. case AL_DIRECT_FILTER_GAINHF_AUTO:
  879. case AL_AUXILIARY_SEND_FILTER_GAIN_AUTO:
  880. case AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO:
  881. case AL_DIRECT_CHANNELS_SOFT:
  882. case AL_DISTANCE_MODEL:
  883. case AL_SOURCE_RELATIVE:
  884. case AL_LOOPING:
  885. case AL_SOURCE_STATE:
  886. case AL_BUFFERS_QUEUED:
  887. case AL_BUFFERS_PROCESSED:
  888. case AL_SOURCE_TYPE:
  889. case AL_SOURCE_RADIUS:
  890. case AL_SOURCE_RESAMPLER_SOFT:
  891. case AL_SOURCE_SPATIALIZE_SOFT:
  892. case AL_BYTE_LENGTH_SOFT:
  893. case AL_SAMPLE_LENGTH_SOFT:
  894. case AL_SEC_LENGTH_SOFT:
  895. case AL_STEREO_MODE_SOFT:
  896. case AL_SUPER_STEREO_WIDTH_SOFT:
  897. return 1;
  898. case AL_STEREO_ANGLES:
  899. return 2;
  900. case AL_POSITION:
  901. case AL_VELOCITY:
  902. case AL_DIRECTION:
  903. return 3;
  904. case AL_ORIENTATION:
  905. return 6;
  906. case AL_SEC_OFFSET_LATENCY_SOFT:
  907. case AL_SEC_OFFSET_CLOCK_SOFT:
  908. break; /* Double only */
  909. case AL_BUFFER:
  910. case AL_DIRECT_FILTER:
  911. case AL_AUXILIARY_SEND_FILTER:
  912. break; /* i/i64 only */
  913. case AL_SAMPLE_OFFSET_LATENCY_SOFT:
  914. case AL_SAMPLE_OFFSET_CLOCK_SOFT:
  915. break; /* i64 only */
  916. }
  917. return 0;
  918. }
  919. ALuint DoubleValsByProp(ALenum prop)
  920. {
  921. switch(static_cast<SourceProp>(prop))
  922. {
  923. case AL_PITCH:
  924. case AL_GAIN:
  925. case AL_MIN_GAIN:
  926. case AL_MAX_GAIN:
  927. case AL_MAX_DISTANCE:
  928. case AL_ROLLOFF_FACTOR:
  929. case AL_DOPPLER_FACTOR:
  930. case AL_CONE_OUTER_GAIN:
  931. case AL_SEC_OFFSET:
  932. case AL_SAMPLE_OFFSET:
  933. case AL_BYTE_OFFSET:
  934. case AL_CONE_INNER_ANGLE:
  935. case AL_CONE_OUTER_ANGLE:
  936. case AL_REFERENCE_DISTANCE:
  937. case AL_CONE_OUTER_GAINHF:
  938. case AL_AIR_ABSORPTION_FACTOR:
  939. case AL_ROOM_ROLLOFF_FACTOR:
  940. case AL_DIRECT_FILTER_GAINHF_AUTO:
  941. case AL_AUXILIARY_SEND_FILTER_GAIN_AUTO:
  942. case AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO:
  943. case AL_DIRECT_CHANNELS_SOFT:
  944. case AL_DISTANCE_MODEL:
  945. case AL_SOURCE_RELATIVE:
  946. case AL_LOOPING:
  947. case AL_SOURCE_STATE:
  948. case AL_BUFFERS_QUEUED:
  949. case AL_BUFFERS_PROCESSED:
  950. case AL_SOURCE_TYPE:
  951. case AL_SOURCE_RADIUS:
  952. case AL_SOURCE_RESAMPLER_SOFT:
  953. case AL_SOURCE_SPATIALIZE_SOFT:
  954. case AL_BYTE_LENGTH_SOFT:
  955. case AL_SAMPLE_LENGTH_SOFT:
  956. case AL_SEC_LENGTH_SOFT:
  957. case AL_STEREO_MODE_SOFT:
  958. case AL_SUPER_STEREO_WIDTH_SOFT:
  959. return 1;
  960. case AL_SEC_OFFSET_LATENCY_SOFT:
  961. case AL_SEC_OFFSET_CLOCK_SOFT:
  962. case AL_STEREO_ANGLES:
  963. return 2;
  964. case AL_POSITION:
  965. case AL_VELOCITY:
  966. case AL_DIRECTION:
  967. return 3;
  968. case AL_ORIENTATION:
  969. return 6;
  970. case AL_BUFFER:
  971. case AL_DIRECT_FILTER:
  972. case AL_AUXILIARY_SEND_FILTER:
  973. break; /* i/i64 only */
  974. case AL_SAMPLE_OFFSET_LATENCY_SOFT:
  975. case AL_SAMPLE_OFFSET_CLOCK_SOFT:
  976. break; /* i64 only */
  977. }
  978. return 0;
  979. }
  980. void SetSourcefv(ALsource *Source, ALCcontext *Context, SourceProp prop, const al::span<const float> values);
  981. void SetSourceiv(ALsource *Source, ALCcontext *Context, SourceProp prop, const al::span<const int> values);
  982. void SetSourcei64v(ALsource *Source, ALCcontext *Context, SourceProp prop, const al::span<const int64_t> values);
  983. #define CHECKSIZE(v, s) do { \
  984. if LIKELY((v).size() == (s) || (v).size() == MaxValues) break; \
  985. Context->setError(AL_INVALID_ENUM, \
  986. "Property 0x%04x expects %d value(s), got %zu", prop, (s), \
  987. (v).size()); \
  988. return; \
  989. } while(0)
  990. #define CHECKVAL(x) do { \
  991. if LIKELY(x) break; \
  992. Context->setError(AL_INVALID_VALUE, "Value out of range"); \
  993. return; \
  994. } while(0)
  995. void UpdateSourceProps(ALsource *source, ALCcontext *context)
  996. {
  997. if(!context->mDeferUpdates)
  998. {
  999. if(Voice *voice{GetSourceVoice(source, context)})
  1000. {
  1001. UpdateSourceProps(source, voice, context);
  1002. return;
  1003. }
  1004. }
  1005. source->mPropsDirty = true;
  1006. }
  1007. #ifdef ALSOFT_EAX
  1008. void CommitAndUpdateSourceProps(ALsource *source, ALCcontext *context)
  1009. {
  1010. if(!context->mDeferUpdates)
  1011. {
  1012. if(source->eax_is_initialized())
  1013. source->eax_commit();
  1014. if(Voice *voice{GetSourceVoice(source, context)})
  1015. {
  1016. UpdateSourceProps(source, voice, context);
  1017. return;
  1018. }
  1019. }
  1020. source->mPropsDirty = true;
  1021. }
  1022. #else
  1023. inline void CommitAndUpdateSourceProps(ALsource *source, ALCcontext *context)
  1024. { UpdateSourceProps(source, context); }
  1025. #endif
  1026. void SetSourcefv(ALsource *Source, ALCcontext *Context, SourceProp prop,
  1027. const al::span<const float> values)
  1028. {
  1029. int ival;
  1030. switch(prop)
  1031. {
  1032. case AL_SEC_LENGTH_SOFT:
  1033. case AL_SEC_OFFSET_LATENCY_SOFT:
  1034. case AL_SEC_OFFSET_CLOCK_SOFT:
  1035. /* Query only */
  1036. SETERR_RETURN(Context, AL_INVALID_OPERATION,,
  1037. "Setting read-only source property 0x%04x", prop);
  1038. case AL_PITCH:
  1039. CHECKSIZE(values, 1);
  1040. CHECKVAL(values[0] >= 0.0f);
  1041. Source->Pitch = values[0];
  1042. return UpdateSourceProps(Source, Context);
  1043. case AL_CONE_INNER_ANGLE:
  1044. CHECKSIZE(values, 1);
  1045. CHECKVAL(values[0] >= 0.0f && values[0] <= 360.0f);
  1046. Source->InnerAngle = values[0];
  1047. return CommitAndUpdateSourceProps(Source, Context);
  1048. case AL_CONE_OUTER_ANGLE:
  1049. CHECKSIZE(values, 1);
  1050. CHECKVAL(values[0] >= 0.0f && values[0] <= 360.0f);
  1051. Source->OuterAngle = values[0];
  1052. return CommitAndUpdateSourceProps(Source, Context);
  1053. case AL_GAIN:
  1054. CHECKSIZE(values, 1);
  1055. CHECKVAL(values[0] >= 0.0f);
  1056. Source->Gain = values[0];
  1057. return UpdateSourceProps(Source, Context);
  1058. case AL_MAX_DISTANCE:
  1059. CHECKSIZE(values, 1);
  1060. CHECKVAL(values[0] >= 0.0f);
  1061. Source->MaxDistance = values[0];
  1062. return CommitAndUpdateSourceProps(Source, Context);
  1063. case AL_ROLLOFF_FACTOR:
  1064. CHECKSIZE(values, 1);
  1065. CHECKVAL(values[0] >= 0.0f);
  1066. Source->RolloffFactor = values[0];
  1067. return CommitAndUpdateSourceProps(Source, Context);
  1068. case AL_REFERENCE_DISTANCE:
  1069. CHECKSIZE(values, 1);
  1070. CHECKVAL(values[0] >= 0.0f);
  1071. Source->RefDistance = values[0];
  1072. return CommitAndUpdateSourceProps(Source, Context);
  1073. case AL_MIN_GAIN:
  1074. CHECKSIZE(values, 1);
  1075. CHECKVAL(values[0] >= 0.0f);
  1076. Source->MinGain = values[0];
  1077. return UpdateSourceProps(Source, Context);
  1078. case AL_MAX_GAIN:
  1079. CHECKSIZE(values, 1);
  1080. CHECKVAL(values[0] >= 0.0f);
  1081. Source->MaxGain = values[0];
  1082. return UpdateSourceProps(Source, Context);
  1083. case AL_CONE_OUTER_GAIN:
  1084. CHECKSIZE(values, 1);
  1085. CHECKVAL(values[0] >= 0.0f && values[0] <= 1.0f);
  1086. Source->OuterGain = values[0];
  1087. return UpdateSourceProps(Source, Context);
  1088. case AL_CONE_OUTER_GAINHF:
  1089. CHECKSIZE(values, 1);
  1090. CHECKVAL(values[0] >= 0.0f && values[0] <= 1.0f);
  1091. Source->OuterGainHF = values[0];
  1092. return UpdateSourceProps(Source, Context);
  1093. case AL_AIR_ABSORPTION_FACTOR:
  1094. CHECKSIZE(values, 1);
  1095. CHECKVAL(values[0] >= 0.0f && values[0] <= 10.0f);
  1096. Source->AirAbsorptionFactor = values[0];
  1097. return UpdateSourceProps(Source, Context);
  1098. case AL_ROOM_ROLLOFF_FACTOR:
  1099. CHECKSIZE(values, 1);
  1100. CHECKVAL(values[0] >= 0.0f && values[0] <= 10.0f);
  1101. Source->RoomRolloffFactor = values[0];
  1102. return UpdateSourceProps(Source, Context);
  1103. case AL_DOPPLER_FACTOR:
  1104. CHECKSIZE(values, 1);
  1105. CHECKVAL(values[0] >= 0.0f && values[0] <= 1.0f);
  1106. Source->DopplerFactor = values[0];
  1107. return UpdateSourceProps(Source, Context);
  1108. case AL_SEC_OFFSET:
  1109. case AL_SAMPLE_OFFSET:
  1110. case AL_BYTE_OFFSET:
  1111. CHECKSIZE(values, 1);
  1112. CHECKVAL(values[0] >= 0.0f);
  1113. if(Voice *voice{GetSourceVoice(Source, Context)})
  1114. {
  1115. auto vpos = GetSampleOffset(Source->mQueue, prop, values[0]);
  1116. if(!vpos) SETERR_RETURN(Context, AL_INVALID_VALUE,, "Invalid offset");
  1117. if(SetVoiceOffset(voice, *vpos, Source, Context, Context->mALDevice.get()))
  1118. return;
  1119. }
  1120. Source->OffsetType = prop;
  1121. Source->Offset = values[0];
  1122. return;
  1123. case AL_SOURCE_RADIUS:
  1124. CHECKSIZE(values, 1);
  1125. CHECKVAL(values[0] >= 0.0f && std::isfinite(values[0]));
  1126. Source->Radius = values[0];
  1127. return UpdateSourceProps(Source, Context);
  1128. case AL_SUPER_STEREO_WIDTH_SOFT:
  1129. CHECKSIZE(values, 1);
  1130. CHECKVAL(values[0] >= 0.0f && values[0] <= 1.0f);
  1131. Source->EnhWidth = values[0];
  1132. return UpdateSourceProps(Source, Context);
  1133. case AL_STEREO_ANGLES:
  1134. CHECKSIZE(values, 2);
  1135. CHECKVAL(std::isfinite(values[0]) && std::isfinite(values[1]));
  1136. Source->StereoPan[0] = values[0];
  1137. Source->StereoPan[1] = values[1];
  1138. return UpdateSourceProps(Source, Context);
  1139. case AL_POSITION:
  1140. CHECKSIZE(values, 3);
  1141. CHECKVAL(std::isfinite(values[0]) && std::isfinite(values[1]) && std::isfinite(values[2]));
  1142. Source->Position[0] = values[0];
  1143. Source->Position[1] = values[1];
  1144. Source->Position[2] = values[2];
  1145. return CommitAndUpdateSourceProps(Source, Context);
  1146. case AL_VELOCITY:
  1147. CHECKSIZE(values, 3);
  1148. CHECKVAL(std::isfinite(values[0]) && std::isfinite(values[1]) && std::isfinite(values[2]));
  1149. Source->Velocity[0] = values[0];
  1150. Source->Velocity[1] = values[1];
  1151. Source->Velocity[2] = values[2];
  1152. return CommitAndUpdateSourceProps(Source, Context);
  1153. case AL_DIRECTION:
  1154. CHECKSIZE(values, 3);
  1155. CHECKVAL(std::isfinite(values[0]) && std::isfinite(values[1]) && std::isfinite(values[2]));
  1156. Source->Direction[0] = values[0];
  1157. Source->Direction[1] = values[1];
  1158. Source->Direction[2] = values[2];
  1159. return CommitAndUpdateSourceProps(Source, Context);
  1160. case AL_ORIENTATION:
  1161. CHECKSIZE(values, 6);
  1162. CHECKVAL(std::isfinite(values[0]) && std::isfinite(values[1]) && std::isfinite(values[2])
  1163. && std::isfinite(values[3]) && std::isfinite(values[4]) && std::isfinite(values[5]));
  1164. Source->OrientAt[0] = values[0];
  1165. Source->OrientAt[1] = values[1];
  1166. Source->OrientAt[2] = values[2];
  1167. Source->OrientUp[0] = values[3];
  1168. Source->OrientUp[1] = values[4];
  1169. Source->OrientUp[2] = values[5];
  1170. return UpdateSourceProps(Source, Context);
  1171. case AL_SOURCE_RELATIVE:
  1172. case AL_LOOPING:
  1173. case AL_SOURCE_STATE:
  1174. case AL_SOURCE_TYPE:
  1175. case AL_DISTANCE_MODEL:
  1176. case AL_DIRECT_FILTER_GAINHF_AUTO:
  1177. case AL_AUXILIARY_SEND_FILTER_GAIN_AUTO:
  1178. case AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO:
  1179. case AL_DIRECT_CHANNELS_SOFT:
  1180. case AL_SOURCE_RESAMPLER_SOFT:
  1181. case AL_SOURCE_SPATIALIZE_SOFT:
  1182. case AL_BYTE_LENGTH_SOFT:
  1183. case AL_SAMPLE_LENGTH_SOFT:
  1184. case AL_STEREO_MODE_SOFT:
  1185. CHECKSIZE(values, 1);
  1186. ival = static_cast<int>(values[0]);
  1187. return SetSourceiv(Source, Context, prop, {&ival, 1u});
  1188. case AL_BUFFERS_QUEUED:
  1189. case AL_BUFFERS_PROCESSED:
  1190. CHECKSIZE(values, 1);
  1191. ival = static_cast<int>(static_cast<ALuint>(values[0]));
  1192. return SetSourceiv(Source, Context, prop, {&ival, 1u});
  1193. case AL_BUFFER:
  1194. case AL_DIRECT_FILTER:
  1195. case AL_AUXILIARY_SEND_FILTER:
  1196. case AL_SAMPLE_OFFSET_LATENCY_SOFT:
  1197. case AL_SAMPLE_OFFSET_CLOCK_SOFT:
  1198. break;
  1199. }
  1200. ERR("Unexpected property: 0x%04x\n", prop);
  1201. Context->setError(AL_INVALID_ENUM, "Invalid source float property 0x%04x", prop);
  1202. return;
  1203. }
  1204. void SetSourceiv(ALsource *Source, ALCcontext *Context, SourceProp prop,
  1205. const al::span<const int> values)
  1206. {
  1207. ALCdevice *device{Context->mALDevice.get()};
  1208. ALeffectslot *slot{nullptr};
  1209. al::deque<ALbufferQueueItem> oldlist;
  1210. std::unique_lock<std::mutex> slotlock;
  1211. float fvals[6];
  1212. switch(prop)
  1213. {
  1214. case AL_SOURCE_STATE:
  1215. case AL_SOURCE_TYPE:
  1216. case AL_BUFFERS_QUEUED:
  1217. case AL_BUFFERS_PROCESSED:
  1218. case AL_BYTE_LENGTH_SOFT:
  1219. case AL_SAMPLE_LENGTH_SOFT:
  1220. /* Query only */
  1221. SETERR_RETURN(Context, AL_INVALID_OPERATION,,
  1222. "Setting read-only source property 0x%04x", prop);
  1223. case AL_SOURCE_RELATIVE:
  1224. CHECKSIZE(values, 1);
  1225. CHECKVAL(values[0] == AL_FALSE || values[0] == AL_TRUE);
  1226. Source->HeadRelative = values[0] != AL_FALSE;
  1227. return CommitAndUpdateSourceProps(Source, Context);
  1228. case AL_LOOPING:
  1229. CHECKSIZE(values, 1);
  1230. CHECKVAL(values[0] == AL_FALSE || values[0] == AL_TRUE);
  1231. Source->Looping = values[0] != AL_FALSE;
  1232. if(Voice *voice{GetSourceVoice(Source, Context)})
  1233. {
  1234. if(Source->Looping)
  1235. voice->mLoopBuffer.store(&Source->mQueue.front(), std::memory_order_release);
  1236. else
  1237. voice->mLoopBuffer.store(nullptr, std::memory_order_release);
  1238. /* If the source is playing, wait for the current mix to finish to
  1239. * ensure it isn't currently looping back or reaching the end.
  1240. */
  1241. device->waitForMix();
  1242. }
  1243. return;
  1244. case AL_BUFFER:
  1245. CHECKSIZE(values, 1);
  1246. {
  1247. const ALenum state{GetSourceState(Source, GetSourceVoice(Source, Context))};
  1248. if(state == AL_PLAYING || state == AL_PAUSED)
  1249. SETERR_RETURN(Context, AL_INVALID_OPERATION,,
  1250. "Setting buffer on playing or paused source %u", Source->id);
  1251. }
  1252. if(values[0])
  1253. {
  1254. std::lock_guard<std::mutex> _{device->BufferLock};
  1255. ALbuffer *buffer{LookupBuffer(device, static_cast<ALuint>(values[0]))};
  1256. if(!buffer)
  1257. SETERR_RETURN(Context, AL_INVALID_VALUE,, "Invalid buffer ID %u",
  1258. static_cast<ALuint>(values[0]));
  1259. if(buffer->MappedAccess && !(buffer->MappedAccess&AL_MAP_PERSISTENT_BIT_SOFT))
  1260. SETERR_RETURN(Context, AL_INVALID_OPERATION,,
  1261. "Setting non-persistently mapped buffer %u", buffer->id);
  1262. if(buffer->mCallback && ReadRef(buffer->ref) != 0)
  1263. SETERR_RETURN(Context, AL_INVALID_OPERATION,,
  1264. "Setting already-set callback buffer %u", buffer->id);
  1265. /* Add the selected buffer to a one-item queue */
  1266. al::deque<ALbufferQueueItem> newlist;
  1267. newlist.emplace_back();
  1268. newlist.back().mCallback = buffer->mCallback;
  1269. newlist.back().mUserData = buffer->mUserData;
  1270. newlist.back().mSampleLen = buffer->mSampleLen;
  1271. newlist.back().mLoopStart = buffer->mLoopStart;
  1272. newlist.back().mLoopEnd = buffer->mLoopEnd;
  1273. newlist.back().mSamples = buffer->mData.data();
  1274. newlist.back().mBuffer = buffer;
  1275. IncrementRef(buffer->ref);
  1276. /* Source is now Static */
  1277. Source->SourceType = AL_STATIC;
  1278. Source->mQueue.swap(oldlist);
  1279. Source->mQueue.swap(newlist);
  1280. }
  1281. else
  1282. {
  1283. /* Source is now Undetermined */
  1284. Source->SourceType = AL_UNDETERMINED;
  1285. Source->mQueue.swap(oldlist);
  1286. }
  1287. /* Delete all elements in the previous queue */
  1288. for(auto &item : oldlist)
  1289. {
  1290. if(ALbuffer *buffer{item.mBuffer})
  1291. DecrementRef(buffer->ref);
  1292. }
  1293. return;
  1294. case AL_SEC_OFFSET:
  1295. case AL_SAMPLE_OFFSET:
  1296. case AL_BYTE_OFFSET:
  1297. CHECKSIZE(values, 1);
  1298. CHECKVAL(values[0] >= 0);
  1299. if(Voice *voice{GetSourceVoice(Source, Context)})
  1300. {
  1301. auto vpos = GetSampleOffset(Source->mQueue, prop, values[0]);
  1302. if(!vpos) SETERR_RETURN(Context, AL_INVALID_VALUE,, "Invalid source offset");
  1303. if(SetVoiceOffset(voice, *vpos, Source, Context, device))
  1304. return;
  1305. }
  1306. Source->OffsetType = prop;
  1307. Source->Offset = values[0];
  1308. return;
  1309. case AL_DIRECT_FILTER:
  1310. CHECKSIZE(values, 1);
  1311. if(values[0])
  1312. {
  1313. std::lock_guard<std::mutex> _{device->FilterLock};
  1314. ALfilter *filter{LookupFilter(device, static_cast<ALuint>(values[0]))};
  1315. if(!filter)
  1316. SETERR_RETURN(Context, AL_INVALID_VALUE,, "Invalid filter ID %u",
  1317. static_cast<ALuint>(values[0]));
  1318. Source->Direct.Gain = filter->Gain;
  1319. Source->Direct.GainHF = filter->GainHF;
  1320. Source->Direct.HFReference = filter->HFReference;
  1321. Source->Direct.GainLF = filter->GainLF;
  1322. Source->Direct.LFReference = filter->LFReference;
  1323. }
  1324. else
  1325. {
  1326. Source->Direct.Gain = 1.0f;
  1327. Source->Direct.GainHF = 1.0f;
  1328. Source->Direct.HFReference = LOWPASSFREQREF;
  1329. Source->Direct.GainLF = 1.0f;
  1330. Source->Direct.LFReference = HIGHPASSFREQREF;
  1331. }
  1332. return UpdateSourceProps(Source, Context);
  1333. case AL_DIRECT_FILTER_GAINHF_AUTO:
  1334. CHECKSIZE(values, 1);
  1335. CHECKVAL(values[0] == AL_FALSE || values[0] == AL_TRUE);
  1336. Source->DryGainHFAuto = values[0] != AL_FALSE;
  1337. return UpdateSourceProps(Source, Context);
  1338. case AL_AUXILIARY_SEND_FILTER_GAIN_AUTO:
  1339. CHECKSIZE(values, 1);
  1340. CHECKVAL(values[0] == AL_FALSE || values[0] == AL_TRUE);
  1341. Source->WetGainAuto = values[0] != AL_FALSE;
  1342. return UpdateSourceProps(Source, Context);
  1343. case AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO:
  1344. CHECKSIZE(values, 1);
  1345. CHECKVAL(values[0] == AL_FALSE || values[0] == AL_TRUE);
  1346. Source->WetGainHFAuto = values[0] != AL_FALSE;
  1347. return UpdateSourceProps(Source, Context);
  1348. case AL_DIRECT_CHANNELS_SOFT:
  1349. CHECKSIZE(values, 1);
  1350. if(auto mode = DirectModeFromEnum(values[0]))
  1351. {
  1352. Source->DirectChannels = *mode;
  1353. return UpdateSourceProps(Source, Context);
  1354. }
  1355. Context->setError(AL_INVALID_VALUE, "Unsupported AL_DIRECT_CHANNELS_SOFT: 0x%04x\n",
  1356. values[0]);
  1357. return;
  1358. case AL_DISTANCE_MODEL:
  1359. CHECKSIZE(values, 1);
  1360. if(auto model = DistanceModelFromALenum(values[0]))
  1361. {
  1362. Source->mDistanceModel = *model;
  1363. if(Context->mSourceDistanceModel)
  1364. UpdateSourceProps(Source, Context);
  1365. return;
  1366. }
  1367. Context->setError(AL_INVALID_VALUE, "Distance model out of range: 0x%04x", values[0]);
  1368. return;
  1369. case AL_SOURCE_RESAMPLER_SOFT:
  1370. CHECKSIZE(values, 1);
  1371. CHECKVAL(values[0] >= 0 && values[0] <= static_cast<int>(Resampler::Max));
  1372. Source->mResampler = static_cast<Resampler>(values[0]);
  1373. return UpdateSourceProps(Source, Context);
  1374. case AL_SOURCE_SPATIALIZE_SOFT:
  1375. CHECKSIZE(values, 1);
  1376. if(auto mode = SpatializeModeFromEnum(values[0]))
  1377. {
  1378. Source->mSpatialize = *mode;
  1379. return UpdateSourceProps(Source, Context);
  1380. }
  1381. Context->setError(AL_INVALID_VALUE, "Unsupported AL_SOURCE_SPATIALIZE_SOFT: 0x%04x\n",
  1382. values[0]);
  1383. return;
  1384. case AL_STEREO_MODE_SOFT:
  1385. CHECKSIZE(values, 1);
  1386. {
  1387. const ALenum state{GetSourceState(Source, GetSourceVoice(Source, Context))};
  1388. if(state == AL_PLAYING || state == AL_PAUSED)
  1389. SETERR_RETURN(Context, AL_INVALID_OPERATION,,
  1390. "Modifying stereo mode on playing or paused source %u", Source->id);
  1391. }
  1392. if(auto mode = StereoModeFromEnum(values[0]))
  1393. {
  1394. Source->mStereoMode = *mode;
  1395. return;
  1396. }
  1397. Context->setError(AL_INVALID_VALUE, "Unsupported AL_STEREO_MODE_SOFT: 0x%04x\n",
  1398. values[0]);
  1399. return;
  1400. case AL_AUXILIARY_SEND_FILTER:
  1401. CHECKSIZE(values, 3);
  1402. slotlock = std::unique_lock<std::mutex>{Context->mEffectSlotLock};
  1403. if(values[0] && (slot=LookupEffectSlot(Context, static_cast<ALuint>(values[0]))) == nullptr)
  1404. SETERR_RETURN(Context, AL_INVALID_VALUE,, "Invalid effect ID %u", values[0]);
  1405. if(static_cast<ALuint>(values[1]) >= device->NumAuxSends)
  1406. SETERR_RETURN(Context, AL_INVALID_VALUE,, "Invalid send %u", values[1]);
  1407. if(values[2])
  1408. {
  1409. std::lock_guard<std::mutex> _{device->FilterLock};
  1410. ALfilter *filter{LookupFilter(device, static_cast<ALuint>(values[2]))};
  1411. if(!filter)
  1412. SETERR_RETURN(Context, AL_INVALID_VALUE,, "Invalid filter ID %u", values[2]);
  1413. auto &send = Source->Send[static_cast<ALuint>(values[1])];
  1414. send.Gain = filter->Gain;
  1415. send.GainHF = filter->GainHF;
  1416. send.HFReference = filter->HFReference;
  1417. send.GainLF = filter->GainLF;
  1418. send.LFReference = filter->LFReference;
  1419. }
  1420. else
  1421. {
  1422. /* Disable filter */
  1423. auto &send = Source->Send[static_cast<ALuint>(values[1])];
  1424. send.Gain = 1.0f;
  1425. send.GainHF = 1.0f;
  1426. send.HFReference = LOWPASSFREQREF;
  1427. send.GainLF = 1.0f;
  1428. send.LFReference = HIGHPASSFREQREF;
  1429. }
  1430. if(slot != Source->Send[static_cast<ALuint>(values[1])].Slot && IsPlayingOrPaused(Source))
  1431. {
  1432. /* Add refcount on the new slot, and release the previous slot */
  1433. if(slot) IncrementRef(slot->ref);
  1434. if(auto *oldslot = Source->Send[static_cast<ALuint>(values[1])].Slot)
  1435. DecrementRef(oldslot->ref);
  1436. Source->Send[static_cast<ALuint>(values[1])].Slot = slot;
  1437. /* We must force an update if the auxiliary slot changed on an
  1438. * active source, in case the slot is about to be deleted.
  1439. */
  1440. Voice *voice{GetSourceVoice(Source, Context)};
  1441. if(voice) UpdateSourceProps(Source, voice, Context);
  1442. else Source->mPropsDirty = true;
  1443. }
  1444. else
  1445. {
  1446. if(slot) IncrementRef(slot->ref);
  1447. if(auto *oldslot = Source->Send[static_cast<ALuint>(values[1])].Slot)
  1448. DecrementRef(oldslot->ref);
  1449. Source->Send[static_cast<ALuint>(values[1])].Slot = slot;
  1450. UpdateSourceProps(Source, Context);
  1451. }
  1452. return;
  1453. /* 1x float */
  1454. case AL_CONE_INNER_ANGLE:
  1455. case AL_CONE_OUTER_ANGLE:
  1456. case AL_PITCH:
  1457. case AL_GAIN:
  1458. case AL_MIN_GAIN:
  1459. case AL_MAX_GAIN:
  1460. case AL_REFERENCE_DISTANCE:
  1461. case AL_ROLLOFF_FACTOR:
  1462. case AL_CONE_OUTER_GAIN:
  1463. case AL_MAX_DISTANCE:
  1464. case AL_DOPPLER_FACTOR:
  1465. case AL_CONE_OUTER_GAINHF:
  1466. case AL_AIR_ABSORPTION_FACTOR:
  1467. case AL_ROOM_ROLLOFF_FACTOR:
  1468. case AL_SOURCE_RADIUS:
  1469. case AL_SEC_LENGTH_SOFT:
  1470. case AL_SUPER_STEREO_WIDTH_SOFT:
  1471. CHECKSIZE(values, 1);
  1472. fvals[0] = static_cast<float>(values[0]);
  1473. return SetSourcefv(Source, Context, prop, {fvals, 1u});
  1474. /* 3x float */
  1475. case AL_POSITION:
  1476. case AL_VELOCITY:
  1477. case AL_DIRECTION:
  1478. CHECKSIZE(values, 3);
  1479. fvals[0] = static_cast<float>(values[0]);
  1480. fvals[1] = static_cast<float>(values[1]);
  1481. fvals[2] = static_cast<float>(values[2]);
  1482. return SetSourcefv(Source, Context, prop, {fvals, 3u});
  1483. /* 6x float */
  1484. case AL_ORIENTATION:
  1485. CHECKSIZE(values, 6);
  1486. fvals[0] = static_cast<float>(values[0]);
  1487. fvals[1] = static_cast<float>(values[1]);
  1488. fvals[2] = static_cast<float>(values[2]);
  1489. fvals[3] = static_cast<float>(values[3]);
  1490. fvals[4] = static_cast<float>(values[4]);
  1491. fvals[5] = static_cast<float>(values[5]);
  1492. return SetSourcefv(Source, Context, prop, {fvals, 6u});
  1493. case AL_SAMPLE_OFFSET_LATENCY_SOFT:
  1494. case AL_SEC_OFFSET_LATENCY_SOFT:
  1495. case AL_SEC_OFFSET_CLOCK_SOFT:
  1496. case AL_SAMPLE_OFFSET_CLOCK_SOFT:
  1497. case AL_STEREO_ANGLES:
  1498. break;
  1499. }
  1500. ERR("Unexpected property: 0x%04x\n", prop);
  1501. Context->setError(AL_INVALID_ENUM, "Invalid source integer property 0x%04x", prop);
  1502. return;
  1503. }
  1504. void SetSourcei64v(ALsource *Source, ALCcontext *Context, SourceProp prop,
  1505. const al::span<const int64_t> values)
  1506. {
  1507. float fvals[MaxValues];
  1508. int ivals[MaxValues];
  1509. switch(prop)
  1510. {
  1511. case AL_SOURCE_TYPE:
  1512. case AL_BUFFERS_QUEUED:
  1513. case AL_BUFFERS_PROCESSED:
  1514. case AL_SOURCE_STATE:
  1515. case AL_BYTE_LENGTH_SOFT:
  1516. case AL_SAMPLE_LENGTH_SOFT:
  1517. case AL_SAMPLE_OFFSET_LATENCY_SOFT:
  1518. case AL_SAMPLE_OFFSET_CLOCK_SOFT:
  1519. /* Query only */
  1520. SETERR_RETURN(Context, AL_INVALID_OPERATION,,
  1521. "Setting read-only source property 0x%04x", prop);
  1522. /* 1x int */
  1523. case AL_SOURCE_RELATIVE:
  1524. case AL_LOOPING:
  1525. case AL_SEC_OFFSET:
  1526. case AL_SAMPLE_OFFSET:
  1527. case AL_BYTE_OFFSET:
  1528. case AL_DIRECT_FILTER_GAINHF_AUTO:
  1529. case AL_AUXILIARY_SEND_FILTER_GAIN_AUTO:
  1530. case AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO:
  1531. case AL_DIRECT_CHANNELS_SOFT:
  1532. case AL_DISTANCE_MODEL:
  1533. case AL_SOURCE_RESAMPLER_SOFT:
  1534. case AL_SOURCE_SPATIALIZE_SOFT:
  1535. case AL_STEREO_MODE_SOFT:
  1536. CHECKSIZE(values, 1);
  1537. CHECKVAL(values[0] <= INT_MAX && values[0] >= INT_MIN);
  1538. ivals[0] = static_cast<int>(values[0]);
  1539. return SetSourceiv(Source, Context, prop, {ivals, 1u});
  1540. /* 1x uint */
  1541. case AL_BUFFER:
  1542. case AL_DIRECT_FILTER:
  1543. CHECKSIZE(values, 1);
  1544. CHECKVAL(values[0] <= UINT_MAX && values[0] >= 0);
  1545. ivals[0] = static_cast<int>(values[0]);
  1546. return SetSourceiv(Source, Context, prop, {ivals, 1u});
  1547. /* 3x uint */
  1548. case AL_AUXILIARY_SEND_FILTER:
  1549. CHECKSIZE(values, 3);
  1550. CHECKVAL(values[0] <= UINT_MAX && values[0] >= 0 && values[1] <= UINT_MAX && values[1] >= 0
  1551. && values[2] <= UINT_MAX && values[2] >= 0);
  1552. ivals[0] = static_cast<int>(values[0]);
  1553. ivals[1] = static_cast<int>(values[1]);
  1554. ivals[2] = static_cast<int>(values[2]);
  1555. return SetSourceiv(Source, Context, prop, {ivals, 3u});
  1556. /* 1x float */
  1557. case AL_CONE_INNER_ANGLE:
  1558. case AL_CONE_OUTER_ANGLE:
  1559. case AL_PITCH:
  1560. case AL_GAIN:
  1561. case AL_MIN_GAIN:
  1562. case AL_MAX_GAIN:
  1563. case AL_REFERENCE_DISTANCE:
  1564. case AL_ROLLOFF_FACTOR:
  1565. case AL_CONE_OUTER_GAIN:
  1566. case AL_MAX_DISTANCE:
  1567. case AL_DOPPLER_FACTOR:
  1568. case AL_CONE_OUTER_GAINHF:
  1569. case AL_AIR_ABSORPTION_FACTOR:
  1570. case AL_ROOM_ROLLOFF_FACTOR:
  1571. case AL_SOURCE_RADIUS:
  1572. case AL_SEC_LENGTH_SOFT:
  1573. case AL_SUPER_STEREO_WIDTH_SOFT:
  1574. CHECKSIZE(values, 1);
  1575. fvals[0] = static_cast<float>(values[0]);
  1576. return SetSourcefv(Source, Context, prop, {fvals, 1u});
  1577. /* 3x float */
  1578. case AL_POSITION:
  1579. case AL_VELOCITY:
  1580. case AL_DIRECTION:
  1581. CHECKSIZE(values, 3);
  1582. fvals[0] = static_cast<float>(values[0]);
  1583. fvals[1] = static_cast<float>(values[1]);
  1584. fvals[2] = static_cast<float>(values[2]);
  1585. return SetSourcefv(Source, Context, prop, {fvals, 3u});
  1586. /* 6x float */
  1587. case AL_ORIENTATION:
  1588. CHECKSIZE(values, 6);
  1589. fvals[0] = static_cast<float>(values[0]);
  1590. fvals[1] = static_cast<float>(values[1]);
  1591. fvals[2] = static_cast<float>(values[2]);
  1592. fvals[3] = static_cast<float>(values[3]);
  1593. fvals[4] = static_cast<float>(values[4]);
  1594. fvals[5] = static_cast<float>(values[5]);
  1595. return SetSourcefv(Source, Context, prop, {fvals, 6u});
  1596. case AL_SEC_OFFSET_LATENCY_SOFT:
  1597. case AL_SEC_OFFSET_CLOCK_SOFT:
  1598. case AL_STEREO_ANGLES:
  1599. break;
  1600. }
  1601. ERR("Unexpected property: 0x%04x\n", prop);
  1602. Context->setError(AL_INVALID_ENUM, "Invalid source integer64 property 0x%04x", prop);
  1603. return;
  1604. }
  1605. #undef CHECKVAL
  1606. #undef CHECKSIZE
  1607. #define CHECKSIZE(v, s) do { \
  1608. if LIKELY((v).size() == (s) || (v).size() == MaxValues) break; \
  1609. Context->setError(AL_INVALID_ENUM, \
  1610. "Property 0x%04x expects %d value(s), got %zu", prop, (s), \
  1611. (v).size()); \
  1612. return false; \
  1613. } while(0)
  1614. bool GetSourcedv(ALsource *Source, ALCcontext *Context, SourceProp prop, const al::span<double> values);
  1615. bool GetSourceiv(ALsource *Source, ALCcontext *Context, SourceProp prop, const al::span<int> values);
  1616. bool GetSourcei64v(ALsource *Source, ALCcontext *Context, SourceProp prop, const al::span<int64_t> values);
  1617. bool GetSourcedv(ALsource *Source, ALCcontext *Context, SourceProp prop, const al::span<double> values)
  1618. {
  1619. ALCdevice *device{Context->mALDevice.get()};
  1620. ClockLatency clocktime;
  1621. nanoseconds srcclock;
  1622. int ivals[MaxValues];
  1623. bool err;
  1624. switch(prop)
  1625. {
  1626. case AL_GAIN:
  1627. CHECKSIZE(values, 1);
  1628. values[0] = Source->Gain;
  1629. return true;
  1630. case AL_PITCH:
  1631. CHECKSIZE(values, 1);
  1632. values[0] = Source->Pitch;
  1633. return true;
  1634. case AL_MAX_DISTANCE:
  1635. CHECKSIZE(values, 1);
  1636. values[0] = Source->MaxDistance;
  1637. return true;
  1638. case AL_ROLLOFF_FACTOR:
  1639. CHECKSIZE(values, 1);
  1640. values[0] = Source->RolloffFactor;
  1641. return true;
  1642. case AL_REFERENCE_DISTANCE:
  1643. CHECKSIZE(values, 1);
  1644. values[0] = Source->RefDistance;
  1645. return true;
  1646. case AL_CONE_INNER_ANGLE:
  1647. CHECKSIZE(values, 1);
  1648. values[0] = Source->InnerAngle;
  1649. return true;
  1650. case AL_CONE_OUTER_ANGLE:
  1651. CHECKSIZE(values, 1);
  1652. values[0] = Source->OuterAngle;
  1653. return true;
  1654. case AL_MIN_GAIN:
  1655. CHECKSIZE(values, 1);
  1656. values[0] = Source->MinGain;
  1657. return true;
  1658. case AL_MAX_GAIN:
  1659. CHECKSIZE(values, 1);
  1660. values[0] = Source->MaxGain;
  1661. return true;
  1662. case AL_CONE_OUTER_GAIN:
  1663. CHECKSIZE(values, 1);
  1664. values[0] = Source->OuterGain;
  1665. return true;
  1666. case AL_SEC_OFFSET:
  1667. case AL_SAMPLE_OFFSET:
  1668. case AL_BYTE_OFFSET:
  1669. CHECKSIZE(values, 1);
  1670. values[0] = GetSourceOffset(Source, prop, Context);
  1671. return true;
  1672. case AL_CONE_OUTER_GAINHF:
  1673. CHECKSIZE(values, 1);
  1674. values[0] = Source->OuterGainHF;
  1675. return true;
  1676. case AL_AIR_ABSORPTION_FACTOR:
  1677. CHECKSIZE(values, 1);
  1678. values[0] = Source->AirAbsorptionFactor;
  1679. return true;
  1680. case AL_ROOM_ROLLOFF_FACTOR:
  1681. CHECKSIZE(values, 1);
  1682. values[0] = Source->RoomRolloffFactor;
  1683. return true;
  1684. case AL_DOPPLER_FACTOR:
  1685. CHECKSIZE(values, 1);
  1686. values[0] = Source->DopplerFactor;
  1687. return true;
  1688. case AL_SOURCE_RADIUS:
  1689. CHECKSIZE(values, 1);
  1690. values[0] = Source->Radius;
  1691. return true;
  1692. case AL_SUPER_STEREO_WIDTH_SOFT:
  1693. CHECKSIZE(values, 1);
  1694. values[0] = Source->EnhWidth;
  1695. return true;
  1696. case AL_BYTE_LENGTH_SOFT:
  1697. case AL_SAMPLE_LENGTH_SOFT:
  1698. case AL_SEC_LENGTH_SOFT:
  1699. CHECKSIZE(values, 1);
  1700. values[0] = GetSourceLength(Source, prop);
  1701. return true;
  1702. case AL_STEREO_ANGLES:
  1703. CHECKSIZE(values, 2);
  1704. values[0] = Source->StereoPan[0];
  1705. values[1] = Source->StereoPan[1];
  1706. return true;
  1707. case AL_SEC_OFFSET_LATENCY_SOFT:
  1708. CHECKSIZE(values, 2);
  1709. /* Get the source offset with the clock time first. Then get the clock
  1710. * time with the device latency. Order is important.
  1711. */
  1712. values[0] = GetSourceSecOffset(Source, Context, &srcclock);
  1713. {
  1714. std::lock_guard<std::mutex> _{device->StateLock};
  1715. clocktime = GetClockLatency(device, device->Backend.get());
  1716. }
  1717. if(srcclock == clocktime.ClockTime)
  1718. values[1] = static_cast<double>(clocktime.Latency.count()) / 1000000000.0;
  1719. else
  1720. {
  1721. /* If the clock time incremented, reduce the latency by that much
  1722. * since it's that much closer to the source offset it got earlier.
  1723. */
  1724. const nanoseconds diff{clocktime.ClockTime - srcclock};
  1725. const nanoseconds latency{clocktime.Latency - std::min(clocktime.Latency, diff)};
  1726. values[1] = static_cast<double>(latency.count()) / 1000000000.0;
  1727. }
  1728. return true;
  1729. case AL_SEC_OFFSET_CLOCK_SOFT:
  1730. CHECKSIZE(values, 2);
  1731. values[0] = GetSourceSecOffset(Source, Context, &srcclock);
  1732. values[1] = static_cast<double>(srcclock.count()) / 1000000000.0;
  1733. return true;
  1734. case AL_POSITION:
  1735. CHECKSIZE(values, 3);
  1736. values[0] = Source->Position[0];
  1737. values[1] = Source->Position[1];
  1738. values[2] = Source->Position[2];
  1739. return true;
  1740. case AL_VELOCITY:
  1741. CHECKSIZE(values, 3);
  1742. values[0] = Source->Velocity[0];
  1743. values[1] = Source->Velocity[1];
  1744. values[2] = Source->Velocity[2];
  1745. return true;
  1746. case AL_DIRECTION:
  1747. CHECKSIZE(values, 3);
  1748. values[0] = Source->Direction[0];
  1749. values[1] = Source->Direction[1];
  1750. values[2] = Source->Direction[2];
  1751. return true;
  1752. case AL_ORIENTATION:
  1753. CHECKSIZE(values, 6);
  1754. values[0] = Source->OrientAt[0];
  1755. values[1] = Source->OrientAt[1];
  1756. values[2] = Source->OrientAt[2];
  1757. values[3] = Source->OrientUp[0];
  1758. values[4] = Source->OrientUp[1];
  1759. values[5] = Source->OrientUp[2];
  1760. return true;
  1761. /* 1x int */
  1762. case AL_SOURCE_RELATIVE:
  1763. case AL_LOOPING:
  1764. case AL_SOURCE_STATE:
  1765. case AL_BUFFERS_QUEUED:
  1766. case AL_BUFFERS_PROCESSED:
  1767. case AL_SOURCE_TYPE:
  1768. case AL_DIRECT_FILTER_GAINHF_AUTO:
  1769. case AL_AUXILIARY_SEND_FILTER_GAIN_AUTO:
  1770. case AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO:
  1771. case AL_DIRECT_CHANNELS_SOFT:
  1772. case AL_DISTANCE_MODEL:
  1773. case AL_SOURCE_RESAMPLER_SOFT:
  1774. case AL_SOURCE_SPATIALIZE_SOFT:
  1775. case AL_STEREO_MODE_SOFT:
  1776. CHECKSIZE(values, 1);
  1777. if((err=GetSourceiv(Source, Context, prop, {ivals, 1u})) != false)
  1778. values[0] = static_cast<double>(ivals[0]);
  1779. return err;
  1780. case AL_BUFFER:
  1781. case AL_DIRECT_FILTER:
  1782. case AL_AUXILIARY_SEND_FILTER:
  1783. case AL_SAMPLE_OFFSET_LATENCY_SOFT:
  1784. case AL_SAMPLE_OFFSET_CLOCK_SOFT:
  1785. break;
  1786. }
  1787. ERR("Unexpected property: 0x%04x\n", prop);
  1788. Context->setError(AL_INVALID_ENUM, "Invalid source double property 0x%04x", prop);
  1789. return false;
  1790. }
  1791. bool GetSourceiv(ALsource *Source, ALCcontext *Context, SourceProp prop, const al::span<int> values)
  1792. {
  1793. double dvals[MaxValues];
  1794. bool err;
  1795. switch(prop)
  1796. {
  1797. case AL_SOURCE_RELATIVE:
  1798. CHECKSIZE(values, 1);
  1799. values[0] = Source->HeadRelative;
  1800. return true;
  1801. case AL_LOOPING:
  1802. CHECKSIZE(values, 1);
  1803. values[0] = Source->Looping;
  1804. return true;
  1805. case AL_BUFFER:
  1806. CHECKSIZE(values, 1);
  1807. {
  1808. ALbufferQueueItem *BufferList{(Source->SourceType == AL_STATIC)
  1809. ? &Source->mQueue.front() : nullptr};
  1810. ALbuffer *buffer{BufferList ? BufferList->mBuffer : nullptr};
  1811. values[0] = buffer ? static_cast<int>(buffer->id) : 0;
  1812. }
  1813. return true;
  1814. case AL_SOURCE_STATE:
  1815. CHECKSIZE(values, 1);
  1816. values[0] = GetSourceState(Source, GetSourceVoice(Source, Context));
  1817. return true;
  1818. case AL_BUFFERS_QUEUED:
  1819. CHECKSIZE(values, 1);
  1820. values[0] = static_cast<int>(Source->mQueue.size());
  1821. return true;
  1822. case AL_BUFFERS_PROCESSED:
  1823. CHECKSIZE(values, 1);
  1824. if(Source->Looping || Source->SourceType != AL_STREAMING)
  1825. {
  1826. /* Buffers on a looping source are in a perpetual state of PENDING,
  1827. * so don't report any as PROCESSED
  1828. */
  1829. values[0] = 0;
  1830. }
  1831. else
  1832. {
  1833. int played{0};
  1834. if(Source->state != AL_INITIAL)
  1835. {
  1836. const VoiceBufferItem *Current{nullptr};
  1837. if(Voice *voice{GetSourceVoice(Source, Context)})
  1838. Current = voice->mCurrentBuffer.load(std::memory_order_relaxed);
  1839. for(auto &item : Source->mQueue)
  1840. {
  1841. if(&item == Current)
  1842. break;
  1843. ++played;
  1844. }
  1845. }
  1846. values[0] = played;
  1847. }
  1848. return true;
  1849. case AL_SOURCE_TYPE:
  1850. CHECKSIZE(values, 1);
  1851. values[0] = Source->SourceType;
  1852. return true;
  1853. case AL_DIRECT_FILTER_GAINHF_AUTO:
  1854. CHECKSIZE(values, 1);
  1855. values[0] = Source->DryGainHFAuto;
  1856. return true;
  1857. case AL_AUXILIARY_SEND_FILTER_GAIN_AUTO:
  1858. CHECKSIZE(values, 1);
  1859. values[0] = Source->WetGainAuto;
  1860. return true;
  1861. case AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO:
  1862. CHECKSIZE(values, 1);
  1863. values[0] = Source->WetGainHFAuto;
  1864. return true;
  1865. case AL_DIRECT_CHANNELS_SOFT:
  1866. CHECKSIZE(values, 1);
  1867. values[0] = EnumFromDirectMode(Source->DirectChannels);
  1868. return true;
  1869. case AL_DISTANCE_MODEL:
  1870. CHECKSIZE(values, 1);
  1871. values[0] = ALenumFromDistanceModel(Source->mDistanceModel);
  1872. return true;
  1873. case AL_BYTE_LENGTH_SOFT:
  1874. case AL_SAMPLE_LENGTH_SOFT:
  1875. case AL_SEC_LENGTH_SOFT:
  1876. CHECKSIZE(values, 1);
  1877. values[0] = static_cast<int>(mind(GetSourceLength(Source, prop),
  1878. std::numeric_limits<int>::max()));
  1879. return true;
  1880. case AL_SOURCE_RESAMPLER_SOFT:
  1881. CHECKSIZE(values, 1);
  1882. values[0] = static_cast<int>(Source->mResampler);
  1883. return true;
  1884. case AL_SOURCE_SPATIALIZE_SOFT:
  1885. CHECKSIZE(values, 1);
  1886. values[0] = EnumFromSpatializeMode(Source->mSpatialize);
  1887. return true;
  1888. case AL_STEREO_MODE_SOFT:
  1889. CHECKSIZE(values, 1);
  1890. values[0] = EnumFromStereoMode(Source->mStereoMode);
  1891. return true;
  1892. /* 1x float/double */
  1893. case AL_CONE_INNER_ANGLE:
  1894. case AL_CONE_OUTER_ANGLE:
  1895. case AL_PITCH:
  1896. case AL_GAIN:
  1897. case AL_MIN_GAIN:
  1898. case AL_MAX_GAIN:
  1899. case AL_REFERENCE_DISTANCE:
  1900. case AL_ROLLOFF_FACTOR:
  1901. case AL_CONE_OUTER_GAIN:
  1902. case AL_MAX_DISTANCE:
  1903. case AL_SEC_OFFSET:
  1904. case AL_SAMPLE_OFFSET:
  1905. case AL_BYTE_OFFSET:
  1906. case AL_DOPPLER_FACTOR:
  1907. case AL_AIR_ABSORPTION_FACTOR:
  1908. case AL_ROOM_ROLLOFF_FACTOR:
  1909. case AL_CONE_OUTER_GAINHF:
  1910. case AL_SOURCE_RADIUS:
  1911. case AL_SUPER_STEREO_WIDTH_SOFT:
  1912. CHECKSIZE(values, 1);
  1913. if((err=GetSourcedv(Source, Context, prop, {dvals, 1u})) != false)
  1914. values[0] = static_cast<int>(dvals[0]);
  1915. return err;
  1916. /* 3x float/double */
  1917. case AL_POSITION:
  1918. case AL_VELOCITY:
  1919. case AL_DIRECTION:
  1920. CHECKSIZE(values, 3);
  1921. if((err=GetSourcedv(Source, Context, prop, {dvals, 3u})) != false)
  1922. {
  1923. values[0] = static_cast<int>(dvals[0]);
  1924. values[1] = static_cast<int>(dvals[1]);
  1925. values[2] = static_cast<int>(dvals[2]);
  1926. }
  1927. return err;
  1928. /* 6x float/double */
  1929. case AL_ORIENTATION:
  1930. CHECKSIZE(values, 6);
  1931. if((err=GetSourcedv(Source, Context, prop, {dvals, 6u})) != false)
  1932. {
  1933. values[0] = static_cast<int>(dvals[0]);
  1934. values[1] = static_cast<int>(dvals[1]);
  1935. values[2] = static_cast<int>(dvals[2]);
  1936. values[3] = static_cast<int>(dvals[3]);
  1937. values[4] = static_cast<int>(dvals[4]);
  1938. values[5] = static_cast<int>(dvals[5]);
  1939. }
  1940. return err;
  1941. case AL_SAMPLE_OFFSET_LATENCY_SOFT:
  1942. case AL_SAMPLE_OFFSET_CLOCK_SOFT:
  1943. break; /* i64 only */
  1944. case AL_SEC_OFFSET_LATENCY_SOFT:
  1945. case AL_SEC_OFFSET_CLOCK_SOFT:
  1946. break; /* Double only */
  1947. case AL_STEREO_ANGLES:
  1948. break; /* Float/double only */
  1949. case AL_DIRECT_FILTER:
  1950. case AL_AUXILIARY_SEND_FILTER:
  1951. break; /* ??? */
  1952. }
  1953. ERR("Unexpected property: 0x%04x\n", prop);
  1954. Context->setError(AL_INVALID_ENUM, "Invalid source integer property 0x%04x", prop);
  1955. return false;
  1956. }
  1957. bool GetSourcei64v(ALsource *Source, ALCcontext *Context, SourceProp prop, const al::span<int64_t> values)
  1958. {
  1959. ALCdevice *device{Context->mALDevice.get()};
  1960. ClockLatency clocktime;
  1961. nanoseconds srcclock;
  1962. double dvals[MaxValues];
  1963. int ivals[MaxValues];
  1964. bool err;
  1965. switch(prop)
  1966. {
  1967. case AL_BYTE_LENGTH_SOFT:
  1968. case AL_SAMPLE_LENGTH_SOFT:
  1969. case AL_SEC_LENGTH_SOFT:
  1970. CHECKSIZE(values, 1);
  1971. values[0] = static_cast<int64_t>(GetSourceLength(Source, prop));
  1972. return true;
  1973. case AL_SAMPLE_OFFSET_LATENCY_SOFT:
  1974. CHECKSIZE(values, 2);
  1975. /* Get the source offset with the clock time first. Then get the clock
  1976. * time with the device latency. Order is important.
  1977. */
  1978. values[0] = GetSourceSampleOffset(Source, Context, &srcclock);
  1979. {
  1980. std::lock_guard<std::mutex> _{device->StateLock};
  1981. clocktime = GetClockLatency(device, device->Backend.get());
  1982. }
  1983. if(srcclock == clocktime.ClockTime)
  1984. values[1] = clocktime.Latency.count();
  1985. else
  1986. {
  1987. /* If the clock time incremented, reduce the latency by that much
  1988. * since it's that much closer to the source offset it got earlier.
  1989. */
  1990. const nanoseconds diff{clocktime.ClockTime - srcclock};
  1991. values[1] = nanoseconds{clocktime.Latency - std::min(clocktime.Latency, diff)}.count();
  1992. }
  1993. return true;
  1994. case AL_SAMPLE_OFFSET_CLOCK_SOFT:
  1995. CHECKSIZE(values, 2);
  1996. values[0] = GetSourceSampleOffset(Source, Context, &srcclock);
  1997. values[1] = srcclock.count();
  1998. return true;
  1999. /* 1x float/double */
  2000. case AL_CONE_INNER_ANGLE:
  2001. case AL_CONE_OUTER_ANGLE:
  2002. case AL_PITCH:
  2003. case AL_GAIN:
  2004. case AL_MIN_GAIN:
  2005. case AL_MAX_GAIN:
  2006. case AL_REFERENCE_DISTANCE:
  2007. case AL_ROLLOFF_FACTOR:
  2008. case AL_CONE_OUTER_GAIN:
  2009. case AL_MAX_DISTANCE:
  2010. case AL_SEC_OFFSET:
  2011. case AL_SAMPLE_OFFSET:
  2012. case AL_BYTE_OFFSET:
  2013. case AL_DOPPLER_FACTOR:
  2014. case AL_AIR_ABSORPTION_FACTOR:
  2015. case AL_ROOM_ROLLOFF_FACTOR:
  2016. case AL_CONE_OUTER_GAINHF:
  2017. case AL_SOURCE_RADIUS:
  2018. case AL_SUPER_STEREO_WIDTH_SOFT:
  2019. CHECKSIZE(values, 1);
  2020. if((err=GetSourcedv(Source, Context, prop, {dvals, 1u})) != false)
  2021. values[0] = static_cast<int64_t>(dvals[0]);
  2022. return err;
  2023. /* 3x float/double */
  2024. case AL_POSITION:
  2025. case AL_VELOCITY:
  2026. case AL_DIRECTION:
  2027. CHECKSIZE(values, 3);
  2028. if((err=GetSourcedv(Source, Context, prop, {dvals, 3u})) != false)
  2029. {
  2030. values[0] = static_cast<int64_t>(dvals[0]);
  2031. values[1] = static_cast<int64_t>(dvals[1]);
  2032. values[2] = static_cast<int64_t>(dvals[2]);
  2033. }
  2034. return err;
  2035. /* 6x float/double */
  2036. case AL_ORIENTATION:
  2037. CHECKSIZE(values, 6);
  2038. if((err=GetSourcedv(Source, Context, prop, {dvals, 6u})) != false)
  2039. {
  2040. values[0] = static_cast<int64_t>(dvals[0]);
  2041. values[1] = static_cast<int64_t>(dvals[1]);
  2042. values[2] = static_cast<int64_t>(dvals[2]);
  2043. values[3] = static_cast<int64_t>(dvals[3]);
  2044. values[4] = static_cast<int64_t>(dvals[4]);
  2045. values[5] = static_cast<int64_t>(dvals[5]);
  2046. }
  2047. return err;
  2048. /* 1x int */
  2049. case AL_SOURCE_RELATIVE:
  2050. case AL_LOOPING:
  2051. case AL_SOURCE_STATE:
  2052. case AL_BUFFERS_QUEUED:
  2053. case AL_BUFFERS_PROCESSED:
  2054. case AL_SOURCE_TYPE:
  2055. case AL_DIRECT_FILTER_GAINHF_AUTO:
  2056. case AL_AUXILIARY_SEND_FILTER_GAIN_AUTO:
  2057. case AL_AUXILIARY_SEND_FILTER_GAINHF_AUTO:
  2058. case AL_DIRECT_CHANNELS_SOFT:
  2059. case AL_DISTANCE_MODEL:
  2060. case AL_SOURCE_RESAMPLER_SOFT:
  2061. case AL_SOURCE_SPATIALIZE_SOFT:
  2062. case AL_STEREO_MODE_SOFT:
  2063. CHECKSIZE(values, 1);
  2064. if((err=GetSourceiv(Source, Context, prop, {ivals, 1u})) != false)
  2065. values[0] = ivals[0];
  2066. return err;
  2067. /* 1x uint */
  2068. case AL_BUFFER:
  2069. case AL_DIRECT_FILTER:
  2070. CHECKSIZE(values, 1);
  2071. if((err=GetSourceiv(Source, Context, prop, {ivals, 1u})) != false)
  2072. values[0] = static_cast<ALuint>(ivals[0]);
  2073. return err;
  2074. /* 3x uint */
  2075. case AL_AUXILIARY_SEND_FILTER:
  2076. CHECKSIZE(values, 3);
  2077. if((err=GetSourceiv(Source, Context, prop, {ivals, 3u})) != false)
  2078. {
  2079. values[0] = static_cast<ALuint>(ivals[0]);
  2080. values[1] = static_cast<ALuint>(ivals[1]);
  2081. values[2] = static_cast<ALuint>(ivals[2]);
  2082. }
  2083. return err;
  2084. case AL_SEC_OFFSET_LATENCY_SOFT:
  2085. case AL_SEC_OFFSET_CLOCK_SOFT:
  2086. break; /* Double only */
  2087. case AL_STEREO_ANGLES:
  2088. break; /* Float/double only */
  2089. }
  2090. ERR("Unexpected property: 0x%04x\n", prop);
  2091. Context->setError(AL_INVALID_ENUM, "Invalid source integer64 property 0x%04x", prop);
  2092. return false;
  2093. }
  2094. } // namespace
  2095. AL_API void AL_APIENTRY alGenSources(ALsizei n, ALuint *sources)
  2096. START_API_FUNC
  2097. {
  2098. ContextRef context{GetContextRef()};
  2099. if UNLIKELY(!context) return;
  2100. if UNLIKELY(n < 0)
  2101. context->setError(AL_INVALID_VALUE, "Generating %d sources", n);
  2102. if UNLIKELY(n <= 0) return;
  2103. #ifdef ALSOFT_EAX
  2104. const bool has_eax{context->has_eax()};
  2105. std::unique_lock<std::mutex> proplock{};
  2106. if(has_eax)
  2107. proplock = std::unique_lock<std::mutex>{context->mPropLock};
  2108. #endif
  2109. std::unique_lock<std::mutex> srclock{context->mSourceLock};
  2110. ALCdevice *device{context->mALDevice.get()};
  2111. if(static_cast<ALuint>(n) > device->SourcesMax-context->mNumSources)
  2112. {
  2113. context->setError(AL_OUT_OF_MEMORY, "Exceeding %u source limit (%u + %d)",
  2114. device->SourcesMax, context->mNumSources, n);
  2115. return;
  2116. }
  2117. if(!EnsureSources(context.get(), static_cast<ALuint>(n)))
  2118. {
  2119. context->setError(AL_OUT_OF_MEMORY, "Failed to allocate %d source%s", n, (n==1)?"":"s");
  2120. return;
  2121. }
  2122. if(n == 1)
  2123. {
  2124. ALsource *source{AllocSource(context.get())};
  2125. sources[0] = source->id;
  2126. #ifdef ALSOFT_EAX
  2127. if(has_eax)
  2128. source->eax_initialize(context.get());
  2129. #endif // ALSOFT_EAX
  2130. }
  2131. else
  2132. {
  2133. #ifdef ALSOFT_EAX
  2134. auto eax_sources = al::vector<ALsource*>{};
  2135. if(has_eax)
  2136. eax_sources.reserve(static_cast<ALuint>(n));
  2137. #endif // ALSOFT_EAX
  2138. al::vector<ALuint> ids;
  2139. ids.reserve(static_cast<ALuint>(n));
  2140. do {
  2141. ALsource *source{AllocSource(context.get())};
  2142. ids.emplace_back(source->id);
  2143. #ifdef ALSOFT_EAX
  2144. if(has_eax)
  2145. eax_sources.emplace_back(source);
  2146. #endif // ALSOFT_EAX
  2147. } while(--n);
  2148. std::copy(ids.cbegin(), ids.cend(), sources);
  2149. #ifdef ALSOFT_EAX
  2150. for(auto& eax_source : eax_sources)
  2151. eax_source->eax_initialize(context.get());
  2152. #endif // ALSOFT_EAX
  2153. }
  2154. }
  2155. END_API_FUNC
  2156. AL_API void AL_APIENTRY alDeleteSources(ALsizei n, const ALuint *sources)
  2157. START_API_FUNC
  2158. {
  2159. ContextRef context{GetContextRef()};
  2160. if UNLIKELY(!context) return;
  2161. if UNLIKELY(n < 0)
  2162. SETERR_RETURN(context, AL_INVALID_VALUE,, "Deleting %d sources", n);
  2163. std::lock_guard<std::mutex> _{context->mSourceLock};
  2164. /* Check that all Sources are valid */
  2165. auto validate_source = [&context](const ALuint sid) -> bool
  2166. { return LookupSource(context.get(), sid) != nullptr; };
  2167. const ALuint *sources_end = sources + n;
  2168. auto invsrc = std::find_if_not(sources, sources_end, validate_source);
  2169. if UNLIKELY(invsrc != sources_end)
  2170. {
  2171. context->setError(AL_INVALID_NAME, "Invalid source ID %u", *invsrc);
  2172. return;
  2173. }
  2174. /* All good. Delete source IDs. */
  2175. auto delete_source = [&context](const ALuint sid) -> void
  2176. {
  2177. ALsource *src{LookupSource(context.get(), sid)};
  2178. if(src) FreeSource(context.get(), src);
  2179. };
  2180. std::for_each(sources, sources_end, delete_source);
  2181. }
  2182. END_API_FUNC
  2183. AL_API ALboolean AL_APIENTRY alIsSource(ALuint source)
  2184. START_API_FUNC
  2185. {
  2186. ContextRef context{GetContextRef()};
  2187. if LIKELY(context)
  2188. {
  2189. std::lock_guard<std::mutex> _{context->mSourceLock};
  2190. if(LookupSource(context.get(), source) != nullptr)
  2191. return AL_TRUE;
  2192. }
  2193. return AL_FALSE;
  2194. }
  2195. END_API_FUNC
  2196. AL_API void AL_APIENTRY alSourcef(ALuint source, ALenum param, ALfloat value)
  2197. START_API_FUNC
  2198. {
  2199. ContextRef context{GetContextRef()};
  2200. if UNLIKELY(!context) return;
  2201. std::lock_guard<std::mutex> _{context->mPropLock};
  2202. std::lock_guard<std::mutex> __{context->mSourceLock};
  2203. ALsource *Source = LookupSource(context.get(), source);
  2204. if UNLIKELY(!Source)
  2205. context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
  2206. else
  2207. SetSourcefv(Source, context.get(), static_cast<SourceProp>(param), {&value, 1u});
  2208. }
  2209. END_API_FUNC
  2210. AL_API void AL_APIENTRY alSource3f(ALuint source, ALenum param, ALfloat value1, ALfloat value2, ALfloat value3)
  2211. START_API_FUNC
  2212. {
  2213. ContextRef context{GetContextRef()};
  2214. if UNLIKELY(!context) return;
  2215. std::lock_guard<std::mutex> _{context->mPropLock};
  2216. std::lock_guard<std::mutex> __{context->mSourceLock};
  2217. ALsource *Source = LookupSource(context.get(), source);
  2218. if UNLIKELY(!Source)
  2219. context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
  2220. else
  2221. {
  2222. const float fvals[3]{ value1, value2, value3 };
  2223. SetSourcefv(Source, context.get(), static_cast<SourceProp>(param), fvals);
  2224. }
  2225. }
  2226. END_API_FUNC
  2227. AL_API void AL_APIENTRY alSourcefv(ALuint source, ALenum param, const ALfloat *values)
  2228. START_API_FUNC
  2229. {
  2230. ContextRef context{GetContextRef()};
  2231. if UNLIKELY(!context) return;
  2232. std::lock_guard<std::mutex> _{context->mPropLock};
  2233. std::lock_guard<std::mutex> __{context->mSourceLock};
  2234. ALsource *Source = LookupSource(context.get(), source);
  2235. if UNLIKELY(!Source)
  2236. context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
  2237. else if UNLIKELY(!values)
  2238. context->setError(AL_INVALID_VALUE, "NULL pointer");
  2239. else
  2240. SetSourcefv(Source, context.get(), static_cast<SourceProp>(param), {values, MaxValues});
  2241. }
  2242. END_API_FUNC
  2243. AL_API void AL_APIENTRY alSourcedSOFT(ALuint source, ALenum param, ALdouble value)
  2244. START_API_FUNC
  2245. {
  2246. ContextRef context{GetContextRef()};
  2247. if UNLIKELY(!context) return;
  2248. std::lock_guard<std::mutex> _{context->mPropLock};
  2249. std::lock_guard<std::mutex> __{context->mSourceLock};
  2250. ALsource *Source = LookupSource(context.get(), source);
  2251. if UNLIKELY(!Source)
  2252. context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
  2253. else
  2254. {
  2255. const float fval[1]{static_cast<float>(value)};
  2256. SetSourcefv(Source, context.get(), static_cast<SourceProp>(param), fval);
  2257. }
  2258. }
  2259. END_API_FUNC
  2260. AL_API void AL_APIENTRY alSource3dSOFT(ALuint source, ALenum param, ALdouble value1, ALdouble value2, ALdouble value3)
  2261. START_API_FUNC
  2262. {
  2263. ContextRef context{GetContextRef()};
  2264. if UNLIKELY(!context) return;
  2265. std::lock_guard<std::mutex> _{context->mPropLock};
  2266. std::lock_guard<std::mutex> __{context->mSourceLock};
  2267. ALsource *Source = LookupSource(context.get(), source);
  2268. if UNLIKELY(!Source)
  2269. context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
  2270. else
  2271. {
  2272. const float fvals[3]{static_cast<float>(value1), static_cast<float>(value2),
  2273. static_cast<float>(value3)};
  2274. SetSourcefv(Source, context.get(), static_cast<SourceProp>(param), fvals);
  2275. }
  2276. }
  2277. END_API_FUNC
  2278. AL_API void AL_APIENTRY alSourcedvSOFT(ALuint source, ALenum param, const ALdouble *values)
  2279. START_API_FUNC
  2280. {
  2281. ContextRef context{GetContextRef()};
  2282. if UNLIKELY(!context) return;
  2283. std::lock_guard<std::mutex> _{context->mPropLock};
  2284. std::lock_guard<std::mutex> __{context->mSourceLock};
  2285. ALsource *Source = LookupSource(context.get(), source);
  2286. if UNLIKELY(!Source)
  2287. context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
  2288. else if UNLIKELY(!values)
  2289. context->setError(AL_INVALID_VALUE, "NULL pointer");
  2290. else
  2291. {
  2292. const ALuint count{DoubleValsByProp(param)};
  2293. float fvals[MaxValues];
  2294. for(ALuint i{0};i < count;i++)
  2295. fvals[i] = static_cast<float>(values[i]);
  2296. SetSourcefv(Source, context.get(), static_cast<SourceProp>(param), {fvals, count});
  2297. }
  2298. }
  2299. END_API_FUNC
  2300. AL_API void AL_APIENTRY alSourcei(ALuint source, ALenum param, ALint value)
  2301. START_API_FUNC
  2302. {
  2303. ContextRef context{GetContextRef()};
  2304. if UNLIKELY(!context) return;
  2305. std::lock_guard<std::mutex> _{context->mPropLock};
  2306. std::lock_guard<std::mutex> __{context->mSourceLock};
  2307. ALsource *Source = LookupSource(context.get(), source);
  2308. if UNLIKELY(!Source)
  2309. context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
  2310. else
  2311. SetSourceiv(Source, context.get(), static_cast<SourceProp>(param), {&value, 1u});
  2312. }
  2313. END_API_FUNC
  2314. AL_API void AL_APIENTRY alSource3i(ALuint source, ALenum param, ALint value1, ALint value2, ALint value3)
  2315. START_API_FUNC
  2316. {
  2317. ContextRef context{GetContextRef()};
  2318. if UNLIKELY(!context) return;
  2319. std::lock_guard<std::mutex> _{context->mPropLock};
  2320. std::lock_guard<std::mutex> __{context->mSourceLock};
  2321. ALsource *Source = LookupSource(context.get(), source);
  2322. if UNLIKELY(!Source)
  2323. context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
  2324. else
  2325. {
  2326. const int ivals[3]{ value1, value2, value3 };
  2327. SetSourceiv(Source, context.get(), static_cast<SourceProp>(param), ivals);
  2328. }
  2329. }
  2330. END_API_FUNC
  2331. AL_API void AL_APIENTRY alSourceiv(ALuint source, ALenum param, const ALint *values)
  2332. START_API_FUNC
  2333. {
  2334. ContextRef context{GetContextRef()};
  2335. if UNLIKELY(!context) return;
  2336. std::lock_guard<std::mutex> _{context->mPropLock};
  2337. std::lock_guard<std::mutex> __{context->mSourceLock};
  2338. ALsource *Source = LookupSource(context.get(), source);
  2339. if UNLIKELY(!Source)
  2340. context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
  2341. else if UNLIKELY(!values)
  2342. context->setError(AL_INVALID_VALUE, "NULL pointer");
  2343. else
  2344. SetSourceiv(Source, context.get(), static_cast<SourceProp>(param), {values, MaxValues});
  2345. }
  2346. END_API_FUNC
  2347. AL_API void AL_APIENTRY alSourcei64SOFT(ALuint source, ALenum param, ALint64SOFT value)
  2348. START_API_FUNC
  2349. {
  2350. ContextRef context{GetContextRef()};
  2351. if UNLIKELY(!context) return;
  2352. std::lock_guard<std::mutex> _{context->mPropLock};
  2353. std::lock_guard<std::mutex> __{context->mSourceLock};
  2354. ALsource *Source{LookupSource(context.get(), source)};
  2355. if UNLIKELY(!Source)
  2356. context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
  2357. else
  2358. SetSourcei64v(Source, context.get(), static_cast<SourceProp>(param), {&value, 1u});
  2359. }
  2360. END_API_FUNC
  2361. AL_API void AL_APIENTRY alSource3i64SOFT(ALuint source, ALenum param, ALint64SOFT value1, ALint64SOFT value2, ALint64SOFT value3)
  2362. START_API_FUNC
  2363. {
  2364. ContextRef context{GetContextRef()};
  2365. if UNLIKELY(!context) return;
  2366. std::lock_guard<std::mutex> _{context->mPropLock};
  2367. std::lock_guard<std::mutex> __{context->mSourceLock};
  2368. ALsource *Source{LookupSource(context.get(), source)};
  2369. if UNLIKELY(!Source)
  2370. context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
  2371. else
  2372. {
  2373. const int64_t i64vals[3]{ value1, value2, value3 };
  2374. SetSourcei64v(Source, context.get(), static_cast<SourceProp>(param), i64vals);
  2375. }
  2376. }
  2377. END_API_FUNC
  2378. AL_API void AL_APIENTRY alSourcei64vSOFT(ALuint source, ALenum param, const ALint64SOFT *values)
  2379. START_API_FUNC
  2380. {
  2381. ContextRef context{GetContextRef()};
  2382. if UNLIKELY(!context) return;
  2383. std::lock_guard<std::mutex> _{context->mPropLock};
  2384. std::lock_guard<std::mutex> __{context->mSourceLock};
  2385. ALsource *Source{LookupSource(context.get(), source)};
  2386. if UNLIKELY(!Source)
  2387. context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
  2388. else if UNLIKELY(!values)
  2389. context->setError(AL_INVALID_VALUE, "NULL pointer");
  2390. else
  2391. SetSourcei64v(Source, context.get(), static_cast<SourceProp>(param), {values, MaxValues});
  2392. }
  2393. END_API_FUNC
  2394. AL_API void AL_APIENTRY alGetSourcef(ALuint source, ALenum param, ALfloat *value)
  2395. START_API_FUNC
  2396. {
  2397. ContextRef context{GetContextRef()};
  2398. if UNLIKELY(!context) return;
  2399. std::lock_guard<std::mutex> _{context->mSourceLock};
  2400. ALsource *Source{LookupSource(context.get(), source)};
  2401. if UNLIKELY(!Source)
  2402. context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
  2403. else if UNLIKELY(!value)
  2404. context->setError(AL_INVALID_VALUE, "NULL pointer");
  2405. else
  2406. {
  2407. double dval[1];
  2408. if(GetSourcedv(Source, context.get(), static_cast<SourceProp>(param), dval))
  2409. *value = static_cast<float>(dval[0]);
  2410. }
  2411. }
  2412. END_API_FUNC
  2413. AL_API void AL_APIENTRY alGetSource3f(ALuint source, ALenum param, ALfloat *value1, ALfloat *value2, ALfloat *value3)
  2414. START_API_FUNC
  2415. {
  2416. ContextRef context{GetContextRef()};
  2417. if UNLIKELY(!context) return;
  2418. std::lock_guard<std::mutex> _{context->mSourceLock};
  2419. ALsource *Source{LookupSource(context.get(), source)};
  2420. if UNLIKELY(!Source)
  2421. context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
  2422. else if UNLIKELY(!(value1 && value2 && value3))
  2423. context->setError(AL_INVALID_VALUE, "NULL pointer");
  2424. else
  2425. {
  2426. double dvals[3];
  2427. if(GetSourcedv(Source, context.get(), static_cast<SourceProp>(param), dvals))
  2428. {
  2429. *value1 = static_cast<float>(dvals[0]);
  2430. *value2 = static_cast<float>(dvals[1]);
  2431. *value3 = static_cast<float>(dvals[2]);
  2432. }
  2433. }
  2434. }
  2435. END_API_FUNC
  2436. AL_API void AL_APIENTRY alGetSourcefv(ALuint source, ALenum param, ALfloat *values)
  2437. START_API_FUNC
  2438. {
  2439. ContextRef context{GetContextRef()};
  2440. if UNLIKELY(!context) return;
  2441. std::lock_guard<std::mutex> _{context->mSourceLock};
  2442. ALsource *Source{LookupSource(context.get(), source)};
  2443. if UNLIKELY(!Source)
  2444. context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
  2445. else if UNLIKELY(!values)
  2446. context->setError(AL_INVALID_VALUE, "NULL pointer");
  2447. else
  2448. {
  2449. const ALuint count{FloatValsByProp(param)};
  2450. double dvals[MaxValues];
  2451. if(GetSourcedv(Source, context.get(), static_cast<SourceProp>(param), {dvals, count}))
  2452. {
  2453. for(ALuint i{0};i < count;i++)
  2454. values[i] = static_cast<float>(dvals[i]);
  2455. }
  2456. }
  2457. }
  2458. END_API_FUNC
  2459. AL_API void AL_APIENTRY alGetSourcedSOFT(ALuint source, ALenum param, ALdouble *value)
  2460. START_API_FUNC
  2461. {
  2462. ContextRef context{GetContextRef()};
  2463. if UNLIKELY(!context) return;
  2464. std::lock_guard<std::mutex> _{context->mSourceLock};
  2465. ALsource *Source{LookupSource(context.get(), source)};
  2466. if UNLIKELY(!Source)
  2467. context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
  2468. else if UNLIKELY(!value)
  2469. context->setError(AL_INVALID_VALUE, "NULL pointer");
  2470. else
  2471. GetSourcedv(Source, context.get(), static_cast<SourceProp>(param), {value, 1u});
  2472. }
  2473. END_API_FUNC
  2474. AL_API void AL_APIENTRY alGetSource3dSOFT(ALuint source, ALenum param, ALdouble *value1, ALdouble *value2, ALdouble *value3)
  2475. START_API_FUNC
  2476. {
  2477. ContextRef context{GetContextRef()};
  2478. if UNLIKELY(!context) return;
  2479. std::lock_guard<std::mutex> _{context->mSourceLock};
  2480. ALsource *Source{LookupSource(context.get(), source)};
  2481. if UNLIKELY(!Source)
  2482. context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
  2483. else if UNLIKELY(!(value1 && value2 && value3))
  2484. context->setError(AL_INVALID_VALUE, "NULL pointer");
  2485. else
  2486. {
  2487. double dvals[3];
  2488. if(GetSourcedv(Source, context.get(), static_cast<SourceProp>(param), dvals))
  2489. {
  2490. *value1 = dvals[0];
  2491. *value2 = dvals[1];
  2492. *value3 = dvals[2];
  2493. }
  2494. }
  2495. }
  2496. END_API_FUNC
  2497. AL_API void AL_APIENTRY alGetSourcedvSOFT(ALuint source, ALenum param, ALdouble *values)
  2498. START_API_FUNC
  2499. {
  2500. ContextRef context{GetContextRef()};
  2501. if UNLIKELY(!context) return;
  2502. std::lock_guard<std::mutex> _{context->mSourceLock};
  2503. ALsource *Source{LookupSource(context.get(), source)};
  2504. if UNLIKELY(!Source)
  2505. context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
  2506. else if UNLIKELY(!values)
  2507. context->setError(AL_INVALID_VALUE, "NULL pointer");
  2508. else
  2509. GetSourcedv(Source, context.get(), static_cast<SourceProp>(param), {values, MaxValues});
  2510. }
  2511. END_API_FUNC
  2512. AL_API void AL_APIENTRY alGetSourcei(ALuint source, ALenum param, ALint *value)
  2513. START_API_FUNC
  2514. {
  2515. ContextRef context{GetContextRef()};
  2516. if UNLIKELY(!context) return;
  2517. std::lock_guard<std::mutex> _{context->mSourceLock};
  2518. ALsource *Source{LookupSource(context.get(), source)};
  2519. if UNLIKELY(!Source)
  2520. context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
  2521. else if UNLIKELY(!value)
  2522. context->setError(AL_INVALID_VALUE, "NULL pointer");
  2523. else
  2524. GetSourceiv(Source, context.get(), static_cast<SourceProp>(param), {value, 1u});
  2525. }
  2526. END_API_FUNC
  2527. AL_API void AL_APIENTRY alGetSource3i(ALuint source, ALenum param, ALint *value1, ALint *value2, ALint *value3)
  2528. START_API_FUNC
  2529. {
  2530. ContextRef context{GetContextRef()};
  2531. if UNLIKELY(!context) return;
  2532. std::lock_guard<std::mutex> _{context->mSourceLock};
  2533. ALsource *Source{LookupSource(context.get(), source)};
  2534. if UNLIKELY(!Source)
  2535. context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
  2536. else if UNLIKELY(!(value1 && value2 && value3))
  2537. context->setError(AL_INVALID_VALUE, "NULL pointer");
  2538. else
  2539. {
  2540. int ivals[3];
  2541. if(GetSourceiv(Source, context.get(), static_cast<SourceProp>(param), ivals))
  2542. {
  2543. *value1 = ivals[0];
  2544. *value2 = ivals[1];
  2545. *value3 = ivals[2];
  2546. }
  2547. }
  2548. }
  2549. END_API_FUNC
  2550. AL_API void AL_APIENTRY alGetSourceiv(ALuint source, ALenum param, ALint *values)
  2551. START_API_FUNC
  2552. {
  2553. ContextRef context{GetContextRef()};
  2554. if UNLIKELY(!context) return;
  2555. std::lock_guard<std::mutex> _{context->mSourceLock};
  2556. ALsource *Source{LookupSource(context.get(), source)};
  2557. if UNLIKELY(!Source)
  2558. context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
  2559. else if UNLIKELY(!values)
  2560. context->setError(AL_INVALID_VALUE, "NULL pointer");
  2561. else
  2562. GetSourceiv(Source, context.get(), static_cast<SourceProp>(param), {values, MaxValues});
  2563. }
  2564. END_API_FUNC
  2565. AL_API void AL_APIENTRY alGetSourcei64SOFT(ALuint source, ALenum param, ALint64SOFT *value)
  2566. START_API_FUNC
  2567. {
  2568. ContextRef context{GetContextRef()};
  2569. if UNLIKELY(!context) return;
  2570. std::lock_guard<std::mutex> _{context->mSourceLock};
  2571. ALsource *Source{LookupSource(context.get(), source)};
  2572. if UNLIKELY(!Source)
  2573. context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
  2574. else if UNLIKELY(!value)
  2575. context->setError(AL_INVALID_VALUE, "NULL pointer");
  2576. else
  2577. GetSourcei64v(Source, context.get(), static_cast<SourceProp>(param), {value, 1u});
  2578. }
  2579. END_API_FUNC
  2580. AL_API void AL_APIENTRY alGetSource3i64SOFT(ALuint source, ALenum param, ALint64SOFT *value1, ALint64SOFT *value2, ALint64SOFT *value3)
  2581. START_API_FUNC
  2582. {
  2583. ContextRef context{GetContextRef()};
  2584. if UNLIKELY(!context) return;
  2585. std::lock_guard<std::mutex> _{context->mSourceLock};
  2586. ALsource *Source{LookupSource(context.get(), source)};
  2587. if UNLIKELY(!Source)
  2588. context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
  2589. else if UNLIKELY(!(value1 && value2 && value3))
  2590. context->setError(AL_INVALID_VALUE, "NULL pointer");
  2591. else
  2592. {
  2593. int64_t i64vals[3];
  2594. if(GetSourcei64v(Source, context.get(), static_cast<SourceProp>(param), i64vals))
  2595. {
  2596. *value1 = i64vals[0];
  2597. *value2 = i64vals[1];
  2598. *value3 = i64vals[2];
  2599. }
  2600. }
  2601. }
  2602. END_API_FUNC
  2603. AL_API void AL_APIENTRY alGetSourcei64vSOFT(ALuint source, ALenum param, ALint64SOFT *values)
  2604. START_API_FUNC
  2605. {
  2606. ContextRef context{GetContextRef()};
  2607. if UNLIKELY(!context) return;
  2608. std::lock_guard<std::mutex> _{context->mSourceLock};
  2609. ALsource *Source{LookupSource(context.get(), source)};
  2610. if UNLIKELY(!Source)
  2611. context->setError(AL_INVALID_NAME, "Invalid source ID %u", source);
  2612. else if UNLIKELY(!values)
  2613. context->setError(AL_INVALID_VALUE, "NULL pointer");
  2614. else
  2615. GetSourcei64v(Source, context.get(), static_cast<SourceProp>(param), {values, MaxValues});
  2616. }
  2617. END_API_FUNC
  2618. AL_API void AL_APIENTRY alSourcePlay(ALuint source)
  2619. START_API_FUNC
  2620. { alSourcePlayv(1, &source); }
  2621. END_API_FUNC
  2622. AL_API void AL_APIENTRY alSourcePlayv(ALsizei n, const ALuint *sources)
  2623. START_API_FUNC
  2624. {
  2625. ContextRef context{GetContextRef()};
  2626. if UNLIKELY(!context) return;
  2627. if UNLIKELY(n < 0)
  2628. context->setError(AL_INVALID_VALUE, "Playing %d sources", n);
  2629. if UNLIKELY(n <= 0) return;
  2630. al::vector<ALsource*> extra_sources;
  2631. std::array<ALsource*,8> source_storage;
  2632. al::span<ALsource*> srchandles;
  2633. if LIKELY(static_cast<ALuint>(n) <= source_storage.size())
  2634. srchandles = {source_storage.data(), static_cast<ALuint>(n)};
  2635. else
  2636. {
  2637. extra_sources.resize(static_cast<ALuint>(n));
  2638. srchandles = {extra_sources.data(), extra_sources.size()};
  2639. }
  2640. std::lock_guard<std::mutex> _{context->mSourceLock};
  2641. for(auto &srchdl : srchandles)
  2642. {
  2643. srchdl = LookupSource(context.get(), *sources);
  2644. if(!srchdl)
  2645. SETERR_RETURN(context, AL_INVALID_NAME,, "Invalid source ID %u", *sources);
  2646. ++sources;
  2647. }
  2648. ALCdevice *device{context->mALDevice.get()};
  2649. /* If the device is disconnected, and voices stop on disconnect, go right
  2650. * to stopped.
  2651. */
  2652. if UNLIKELY(!device->Connected.load(std::memory_order_acquire))
  2653. {
  2654. if(context->mStopVoicesOnDisconnect.load(std::memory_order_acquire))
  2655. {
  2656. for(ALsource *source : srchandles)
  2657. {
  2658. /* TODO: Send state change event? */
  2659. source->Offset = 0.0;
  2660. source->OffsetType = AL_NONE;
  2661. source->state = AL_STOPPED;
  2662. }
  2663. return;
  2664. }
  2665. }
  2666. /* Count the number of reusable voices. */
  2667. auto voicelist = context->getVoicesSpan();
  2668. size_t free_voices{0};
  2669. for(const Voice *voice : voicelist)
  2670. {
  2671. free_voices += (voice->mPlayState.load(std::memory_order_acquire) == Voice::Stopped
  2672. && voice->mSourceID.load(std::memory_order_relaxed) == 0u
  2673. && voice->mPendingChange.load(std::memory_order_relaxed) == false);
  2674. if(free_voices == srchandles.size())
  2675. break;
  2676. }
  2677. if UNLIKELY(srchandles.size() != free_voices)
  2678. {
  2679. const size_t inc_amount{srchandles.size() - free_voices};
  2680. auto &allvoices = *context->mVoices.load(std::memory_order_relaxed);
  2681. if(inc_amount > allvoices.size() - voicelist.size())
  2682. {
  2683. /* Increase the number of voices to handle the request. */
  2684. context->allocVoices(inc_amount - (allvoices.size() - voicelist.size()));
  2685. }
  2686. context->mActiveVoiceCount.fetch_add(inc_amount, std::memory_order_release);
  2687. voicelist = context->getVoicesSpan();
  2688. }
  2689. auto voiceiter = voicelist.begin();
  2690. ALuint vidx{0};
  2691. VoiceChange *tail{}, *cur{};
  2692. for(ALsource *source : srchandles)
  2693. {
  2694. /* Check that there is a queue containing at least one valid, non zero
  2695. * length buffer.
  2696. */
  2697. auto BufferList = source->mQueue.begin();
  2698. for(;BufferList != source->mQueue.end();++BufferList)
  2699. {
  2700. if(BufferList->mSampleLen != 0 || BufferList->mCallback)
  2701. break;
  2702. }
  2703. /* If there's nothing to play, go right to stopped. */
  2704. if UNLIKELY(BufferList == source->mQueue.end())
  2705. {
  2706. /* NOTE: A source without any playable buffers should not have a
  2707. * Voice since it shouldn't be in a playing or paused state. So
  2708. * there's no need to look up its voice and clear the source.
  2709. */
  2710. source->Offset = 0.0;
  2711. source->OffsetType = AL_NONE;
  2712. source->state = AL_STOPPED;
  2713. continue;
  2714. }
  2715. if(!cur)
  2716. cur = tail = GetVoiceChanger(context.get());
  2717. else
  2718. {
  2719. cur->mNext.store(GetVoiceChanger(context.get()), std::memory_order_relaxed);
  2720. cur = cur->mNext.load(std::memory_order_relaxed);
  2721. }
  2722. Voice *voice{GetSourceVoice(source, context.get())};
  2723. switch(GetSourceState(source, voice))
  2724. {
  2725. case AL_PAUSED:
  2726. /* A source that's paused simply resumes. If there's no voice, it
  2727. * was lost from a disconnect, so just start over with a new one.
  2728. */
  2729. cur->mOldVoice = nullptr;
  2730. if(!voice) break;
  2731. cur->mVoice = voice;
  2732. cur->mSourceID = source->id;
  2733. cur->mState = VChangeState::Play;
  2734. source->state = AL_PLAYING;
  2735. #ifdef ALSOFT_EAX
  2736. if(source->eax_is_initialized())
  2737. source->eax_commit();
  2738. #endif // ALSOFT_EAX
  2739. continue;
  2740. case AL_PLAYING:
  2741. /* A source that's already playing is restarted from the beginning.
  2742. * Stop the current voice and start a new one so it properly cross-
  2743. * fades back to the beginning.
  2744. */
  2745. if(voice)
  2746. voice->mPendingChange.store(true, std::memory_order_relaxed);
  2747. cur->mOldVoice = voice;
  2748. voice = nullptr;
  2749. break;
  2750. default:
  2751. assert(voice == nullptr);
  2752. cur->mOldVoice = nullptr;
  2753. #ifdef ALSOFT_EAX
  2754. if(source->eax_is_initialized())
  2755. source->eax_commit();
  2756. #endif // ALSOFT_EAX
  2757. break;
  2758. }
  2759. /* Find the next unused voice to play this source with. */
  2760. for(;voiceiter != voicelist.end();++voiceiter,++vidx)
  2761. {
  2762. Voice *v{*voiceiter};
  2763. if(v->mPlayState.load(std::memory_order_acquire) == Voice::Stopped
  2764. && v->mSourceID.load(std::memory_order_relaxed) == 0u
  2765. && v->mPendingChange.load(std::memory_order_relaxed) == false)
  2766. {
  2767. voice = v;
  2768. break;
  2769. }
  2770. }
  2771. ASSUME(voice != nullptr);
  2772. voice->mPosition.store(0u, std::memory_order_relaxed);
  2773. voice->mPositionFrac.store(0, std::memory_order_relaxed);
  2774. voice->mCurrentBuffer.store(&source->mQueue.front(), std::memory_order_relaxed);
  2775. voice->mFlags.reset();
  2776. /* A source that's not playing or paused has any offset applied when it
  2777. * starts playing.
  2778. */
  2779. if(const ALenum offsettype{source->OffsetType})
  2780. {
  2781. const double offset{source->Offset};
  2782. source->OffsetType = AL_NONE;
  2783. source->Offset = 0.0;
  2784. if(auto vpos = GetSampleOffset(source->mQueue, offsettype, offset))
  2785. {
  2786. voice->mPosition.store(vpos->pos, std::memory_order_relaxed);
  2787. voice->mPositionFrac.store(vpos->frac, std::memory_order_relaxed);
  2788. voice->mCurrentBuffer.store(vpos->bufferitem, std::memory_order_relaxed);
  2789. if(vpos->pos!=0 || vpos->frac!=0 || vpos->bufferitem!=&source->mQueue.front())
  2790. voice->mFlags.set(VoiceIsFading);
  2791. }
  2792. }
  2793. InitVoice(voice, source, std::addressof(*BufferList), context.get(), device);
  2794. source->VoiceIdx = vidx;
  2795. source->state = AL_PLAYING;
  2796. cur->mVoice = voice;
  2797. cur->mSourceID = source->id;
  2798. cur->mState = VChangeState::Play;
  2799. }
  2800. if LIKELY(tail)
  2801. SendVoiceChanges(context.get(), tail);
  2802. }
  2803. END_API_FUNC
  2804. AL_API void AL_APIENTRY alSourcePause(ALuint source)
  2805. START_API_FUNC
  2806. { alSourcePausev(1, &source); }
  2807. END_API_FUNC
  2808. AL_API void AL_APIENTRY alSourcePausev(ALsizei n, const ALuint *sources)
  2809. START_API_FUNC
  2810. {
  2811. ContextRef context{GetContextRef()};
  2812. if UNLIKELY(!context) return;
  2813. if UNLIKELY(n < 0)
  2814. context->setError(AL_INVALID_VALUE, "Pausing %d sources", n);
  2815. if UNLIKELY(n <= 0) return;
  2816. al::vector<ALsource*> extra_sources;
  2817. std::array<ALsource*,8> source_storage;
  2818. al::span<ALsource*> srchandles;
  2819. if LIKELY(static_cast<ALuint>(n) <= source_storage.size())
  2820. srchandles = {source_storage.data(), static_cast<ALuint>(n)};
  2821. else
  2822. {
  2823. extra_sources.resize(static_cast<ALuint>(n));
  2824. srchandles = {extra_sources.data(), extra_sources.size()};
  2825. }
  2826. std::lock_guard<std::mutex> _{context->mSourceLock};
  2827. for(auto &srchdl : srchandles)
  2828. {
  2829. srchdl = LookupSource(context.get(), *sources);
  2830. if(!srchdl)
  2831. SETERR_RETURN(context, AL_INVALID_NAME,, "Invalid source ID %u", *sources);
  2832. ++sources;
  2833. }
  2834. /* Pausing has to be done in two steps. First, for each source that's
  2835. * detected to be playing, chamge the voice (asynchronously) to
  2836. * stopping/paused.
  2837. */
  2838. VoiceChange *tail{}, *cur{};
  2839. for(ALsource *source : srchandles)
  2840. {
  2841. Voice *voice{GetSourceVoice(source, context.get())};
  2842. if(GetSourceState(source, voice) == AL_PLAYING)
  2843. {
  2844. if(!cur)
  2845. cur = tail = GetVoiceChanger(context.get());
  2846. else
  2847. {
  2848. cur->mNext.store(GetVoiceChanger(context.get()), std::memory_order_relaxed);
  2849. cur = cur->mNext.load(std::memory_order_relaxed);
  2850. }
  2851. cur->mVoice = voice;
  2852. cur->mSourceID = source->id;
  2853. cur->mState = VChangeState::Pause;
  2854. }
  2855. }
  2856. if LIKELY(tail)
  2857. {
  2858. SendVoiceChanges(context.get(), tail);
  2859. /* Second, now that the voice changes have been sent, because it's
  2860. * possible that the voice stopped after it was detected playing and
  2861. * before the voice got paused, recheck that the source is still
  2862. * considered playing and set it to paused if so.
  2863. */
  2864. for(ALsource *source : srchandles)
  2865. {
  2866. Voice *voice{GetSourceVoice(source, context.get())};
  2867. if(GetSourceState(source, voice) == AL_PLAYING)
  2868. source->state = AL_PAUSED;
  2869. }
  2870. }
  2871. }
  2872. END_API_FUNC
  2873. AL_API void AL_APIENTRY alSourceStop(ALuint source)
  2874. START_API_FUNC
  2875. { alSourceStopv(1, &source); }
  2876. END_API_FUNC
  2877. AL_API void AL_APIENTRY alSourceStopv(ALsizei n, const ALuint *sources)
  2878. START_API_FUNC
  2879. {
  2880. ContextRef context{GetContextRef()};
  2881. if UNLIKELY(!context) return;
  2882. if UNLIKELY(n < 0)
  2883. context->setError(AL_INVALID_VALUE, "Stopping %d sources", n);
  2884. if UNLIKELY(n <= 0) return;
  2885. al::vector<ALsource*> extra_sources;
  2886. std::array<ALsource*,8> source_storage;
  2887. al::span<ALsource*> srchandles;
  2888. if LIKELY(static_cast<ALuint>(n) <= source_storage.size())
  2889. srchandles = {source_storage.data(), static_cast<ALuint>(n)};
  2890. else
  2891. {
  2892. extra_sources.resize(static_cast<ALuint>(n));
  2893. srchandles = {extra_sources.data(), extra_sources.size()};
  2894. }
  2895. std::lock_guard<std::mutex> _{context->mSourceLock};
  2896. for(auto &srchdl : srchandles)
  2897. {
  2898. srchdl = LookupSource(context.get(), *sources);
  2899. if(!srchdl)
  2900. SETERR_RETURN(context, AL_INVALID_NAME,, "Invalid source ID %u", *sources);
  2901. ++sources;
  2902. }
  2903. VoiceChange *tail{}, *cur{};
  2904. for(ALsource *source : srchandles)
  2905. {
  2906. if(Voice *voice{GetSourceVoice(source, context.get())})
  2907. {
  2908. if(!cur)
  2909. cur = tail = GetVoiceChanger(context.get());
  2910. else
  2911. {
  2912. cur->mNext.store(GetVoiceChanger(context.get()), std::memory_order_relaxed);
  2913. cur = cur->mNext.load(std::memory_order_relaxed);
  2914. }
  2915. voice->mPendingChange.store(true, std::memory_order_relaxed);
  2916. cur->mVoice = voice;
  2917. cur->mSourceID = source->id;
  2918. cur->mState = VChangeState::Stop;
  2919. source->state = AL_STOPPED;
  2920. }
  2921. source->Offset = 0.0;
  2922. source->OffsetType = AL_NONE;
  2923. source->VoiceIdx = INVALID_VOICE_IDX;
  2924. }
  2925. if LIKELY(tail)
  2926. SendVoiceChanges(context.get(), tail);
  2927. }
  2928. END_API_FUNC
  2929. AL_API void AL_APIENTRY alSourceRewind(ALuint source)
  2930. START_API_FUNC
  2931. { alSourceRewindv(1, &source); }
  2932. END_API_FUNC
  2933. AL_API void AL_APIENTRY alSourceRewindv(ALsizei n, const ALuint *sources)
  2934. START_API_FUNC
  2935. {
  2936. ContextRef context{GetContextRef()};
  2937. if UNLIKELY(!context) return;
  2938. if UNLIKELY(n < 0)
  2939. context->setError(AL_INVALID_VALUE, "Rewinding %d sources", n);
  2940. if UNLIKELY(n <= 0) return;
  2941. al::vector<ALsource*> extra_sources;
  2942. std::array<ALsource*,8> source_storage;
  2943. al::span<ALsource*> srchandles;
  2944. if LIKELY(static_cast<ALuint>(n) <= source_storage.size())
  2945. srchandles = {source_storage.data(), static_cast<ALuint>(n)};
  2946. else
  2947. {
  2948. extra_sources.resize(static_cast<ALuint>(n));
  2949. srchandles = {extra_sources.data(), extra_sources.size()};
  2950. }
  2951. std::lock_guard<std::mutex> _{context->mSourceLock};
  2952. for(auto &srchdl : srchandles)
  2953. {
  2954. srchdl = LookupSource(context.get(), *sources);
  2955. if(!srchdl)
  2956. SETERR_RETURN(context, AL_INVALID_NAME,, "Invalid source ID %u", *sources);
  2957. ++sources;
  2958. }
  2959. VoiceChange *tail{}, *cur{};
  2960. for(ALsource *source : srchandles)
  2961. {
  2962. Voice *voice{GetSourceVoice(source, context.get())};
  2963. if(source->state != AL_INITIAL)
  2964. {
  2965. if(!cur)
  2966. cur = tail = GetVoiceChanger(context.get());
  2967. else
  2968. {
  2969. cur->mNext.store(GetVoiceChanger(context.get()), std::memory_order_relaxed);
  2970. cur = cur->mNext.load(std::memory_order_relaxed);
  2971. }
  2972. if(voice)
  2973. voice->mPendingChange.store(true, std::memory_order_relaxed);
  2974. cur->mVoice = voice;
  2975. cur->mSourceID = source->id;
  2976. cur->mState = VChangeState::Reset;
  2977. source->state = AL_INITIAL;
  2978. }
  2979. source->Offset = 0.0;
  2980. source->OffsetType = AL_NONE;
  2981. source->VoiceIdx = INVALID_VOICE_IDX;
  2982. }
  2983. if LIKELY(tail)
  2984. SendVoiceChanges(context.get(), tail);
  2985. }
  2986. END_API_FUNC
  2987. AL_API void AL_APIENTRY alSourceQueueBuffers(ALuint src, ALsizei nb, const ALuint *buffers)
  2988. START_API_FUNC
  2989. {
  2990. ContextRef context{GetContextRef()};
  2991. if UNLIKELY(!context) return;
  2992. if UNLIKELY(nb < 0)
  2993. context->setError(AL_INVALID_VALUE, "Queueing %d buffers", nb);
  2994. if UNLIKELY(nb <= 0) return;
  2995. std::lock_guard<std::mutex> _{context->mSourceLock};
  2996. ALsource *source{LookupSource(context.get(),src)};
  2997. if UNLIKELY(!source)
  2998. SETERR_RETURN(context, AL_INVALID_NAME,, "Invalid source ID %u", src);
  2999. /* Can't queue on a Static Source */
  3000. if UNLIKELY(source->SourceType == AL_STATIC)
  3001. SETERR_RETURN(context, AL_INVALID_OPERATION,, "Queueing onto static source %u", src);
  3002. /* Check for a valid Buffer, for its frequency and format */
  3003. ALCdevice *device{context->mALDevice.get()};
  3004. ALbuffer *BufferFmt{nullptr};
  3005. for(auto &item : source->mQueue)
  3006. {
  3007. BufferFmt = item.mBuffer;
  3008. if(BufferFmt) break;
  3009. }
  3010. std::unique_lock<std::mutex> buflock{device->BufferLock};
  3011. const size_t NewListStart{source->mQueue.size()};
  3012. ALbufferQueueItem *BufferList{nullptr};
  3013. for(ALsizei i{0};i < nb;i++)
  3014. {
  3015. bool fmt_mismatch{false};
  3016. ALbuffer *buffer{nullptr};
  3017. if(buffers[i] && (buffer=LookupBuffer(device, buffers[i])) == nullptr)
  3018. {
  3019. context->setError(AL_INVALID_NAME, "Queueing invalid buffer ID %u", buffers[i]);
  3020. goto buffer_error;
  3021. }
  3022. if(buffer && buffer->mCallback)
  3023. {
  3024. context->setError(AL_INVALID_OPERATION, "Queueing callback buffer %u", buffers[i]);
  3025. goto buffer_error;
  3026. }
  3027. source->mQueue.emplace_back();
  3028. if(!BufferList)
  3029. BufferList = &source->mQueue.back();
  3030. else
  3031. {
  3032. auto &item = source->mQueue.back();
  3033. BufferList->mNext.store(&item, std::memory_order_relaxed);
  3034. BufferList = &item;
  3035. }
  3036. if(!buffer) continue;
  3037. BufferList->mSampleLen = buffer->mSampleLen;
  3038. BufferList->mLoopEnd = buffer->mSampleLen;
  3039. BufferList->mSamples = buffer->mData.data();
  3040. BufferList->mBuffer = buffer;
  3041. IncrementRef(buffer->ref);
  3042. if(buffer->MappedAccess != 0 && !(buffer->MappedAccess&AL_MAP_PERSISTENT_BIT_SOFT))
  3043. {
  3044. context->setError(AL_INVALID_OPERATION, "Queueing non-persistently mapped buffer %u",
  3045. buffer->id);
  3046. goto buffer_error;
  3047. }
  3048. if(BufferFmt == nullptr)
  3049. BufferFmt = buffer;
  3050. else
  3051. {
  3052. fmt_mismatch |= BufferFmt->mSampleRate != buffer->mSampleRate;
  3053. fmt_mismatch |= BufferFmt->mChannels != buffer->mChannels;
  3054. if(BufferFmt->isBFormat())
  3055. {
  3056. fmt_mismatch |= BufferFmt->mAmbiLayout != buffer->mAmbiLayout;
  3057. fmt_mismatch |= BufferFmt->mAmbiScaling != buffer->mAmbiScaling;
  3058. }
  3059. fmt_mismatch |= BufferFmt->mAmbiOrder != buffer->mAmbiOrder;
  3060. fmt_mismatch |= BufferFmt->OriginalType != buffer->OriginalType;
  3061. }
  3062. if UNLIKELY(fmt_mismatch)
  3063. {
  3064. context->setError(AL_INVALID_OPERATION, "Queueing buffer with mismatched format");
  3065. buffer_error:
  3066. /* A buffer failed (invalid ID or format), so unlock and release
  3067. * each buffer we had.
  3068. */
  3069. auto iter = source->mQueue.begin() + ptrdiff_t(NewListStart);
  3070. for(;iter != source->mQueue.end();++iter)
  3071. {
  3072. if(ALbuffer *buf{iter->mBuffer})
  3073. DecrementRef(buf->ref);
  3074. }
  3075. source->mQueue.resize(NewListStart);
  3076. return;
  3077. }
  3078. }
  3079. /* All buffers good. */
  3080. buflock.unlock();
  3081. /* Source is now streaming */
  3082. source->SourceType = AL_STREAMING;
  3083. if(NewListStart != 0)
  3084. {
  3085. auto iter = source->mQueue.begin() + ptrdiff_t(NewListStart);
  3086. (iter-1)->mNext.store(std::addressof(*iter), std::memory_order_release);
  3087. }
  3088. }
  3089. END_API_FUNC
  3090. AL_API void AL_APIENTRY alSourceUnqueueBuffers(ALuint src, ALsizei nb, ALuint *buffers)
  3091. START_API_FUNC
  3092. {
  3093. ContextRef context{GetContextRef()};
  3094. if UNLIKELY(!context) return;
  3095. if UNLIKELY(nb < 0)
  3096. context->setError(AL_INVALID_VALUE, "Unqueueing %d buffers", nb);
  3097. if UNLIKELY(nb <= 0) return;
  3098. std::lock_guard<std::mutex> _{context->mSourceLock};
  3099. ALsource *source{LookupSource(context.get(),src)};
  3100. if UNLIKELY(!source)
  3101. SETERR_RETURN(context, AL_INVALID_NAME,, "Invalid source ID %u", src);
  3102. if UNLIKELY(source->SourceType != AL_STREAMING)
  3103. SETERR_RETURN(context, AL_INVALID_VALUE,, "Unqueueing from a non-streaming source %u",
  3104. src);
  3105. if UNLIKELY(source->Looping)
  3106. SETERR_RETURN(context, AL_INVALID_VALUE,, "Unqueueing from looping source %u", src);
  3107. /* Make sure enough buffers have been processed to unqueue. */
  3108. uint processed{0u};
  3109. if LIKELY(source->state != AL_INITIAL)
  3110. {
  3111. VoiceBufferItem *Current{nullptr};
  3112. if(Voice *voice{GetSourceVoice(source, context.get())})
  3113. Current = voice->mCurrentBuffer.load(std::memory_order_relaxed);
  3114. for(auto &item : source->mQueue)
  3115. {
  3116. if(&item == Current)
  3117. break;
  3118. ++processed;
  3119. }
  3120. }
  3121. if UNLIKELY(processed < static_cast<ALuint>(nb))
  3122. SETERR_RETURN(context, AL_INVALID_VALUE,, "Unqueueing %d buffer%s (only %u processed)",
  3123. nb, (nb==1)?"":"s", processed);
  3124. do {
  3125. auto &head = source->mQueue.front();
  3126. if(ALbuffer *buffer{head.mBuffer})
  3127. {
  3128. *(buffers++) = buffer->id;
  3129. DecrementRef(buffer->ref);
  3130. }
  3131. else
  3132. *(buffers++) = 0;
  3133. source->mQueue.pop_front();
  3134. } while(--nb);
  3135. }
  3136. END_API_FUNC
  3137. AL_API void AL_APIENTRY alSourceQueueBufferLayersSOFT(ALuint, ALsizei, const ALuint*)
  3138. START_API_FUNC
  3139. {
  3140. ContextRef context{GetContextRef()};
  3141. if UNLIKELY(!context) return;
  3142. context->setError(AL_INVALID_OPERATION, "alSourceQueueBufferLayersSOFT not supported");
  3143. }
  3144. END_API_FUNC
  3145. ALsource::ALsource()
  3146. {
  3147. Direct.Gain = 1.0f;
  3148. Direct.GainHF = 1.0f;
  3149. Direct.HFReference = LOWPASSFREQREF;
  3150. Direct.GainLF = 1.0f;
  3151. Direct.LFReference = HIGHPASSFREQREF;
  3152. for(auto &send : Send)
  3153. {
  3154. send.Slot = nullptr;
  3155. send.Gain = 1.0f;
  3156. send.GainHF = 1.0f;
  3157. send.HFReference = LOWPASSFREQREF;
  3158. send.GainLF = 1.0f;
  3159. send.LFReference = HIGHPASSFREQREF;
  3160. }
  3161. }
  3162. ALsource::~ALsource()
  3163. {
  3164. for(auto &item : mQueue)
  3165. {
  3166. if(ALbuffer *buffer{item.mBuffer})
  3167. DecrementRef(buffer->ref);
  3168. }
  3169. auto clear_send = [](ALsource::SendData &send) -> void
  3170. { if(send.Slot) DecrementRef(send.Slot->ref); };
  3171. std::for_each(Send.begin(), Send.end(), clear_send);
  3172. }
  3173. void UpdateAllSourceProps(ALCcontext *context)
  3174. {
  3175. std::lock_guard<std::mutex> _{context->mSourceLock};
  3176. #ifdef ALSOFT_EAX
  3177. if(context->has_eax())
  3178. {
  3179. /* If EAX is enabled, we need to go through and commit all sources' EAX
  3180. * changes, along with updating its voice, if any.
  3181. */
  3182. for(auto &sublist : context->mSourceList)
  3183. {
  3184. uint64_t usemask{~sublist.FreeMask};
  3185. while(usemask)
  3186. {
  3187. const int idx{al::countr_zero(usemask)};
  3188. usemask &= ~(1_u64 << idx);
  3189. ALsource *source{sublist.Sources + idx};
  3190. source->eax_commit();
  3191. if(Voice *voice{GetSourceVoice(source, context)})
  3192. {
  3193. if(std::exchange(source->mPropsDirty, false))
  3194. UpdateSourceProps(source, voice, context);
  3195. }
  3196. }
  3197. }
  3198. }
  3199. else
  3200. #endif
  3201. {
  3202. auto voicelist = context->getVoicesSpan();
  3203. ALuint vidx{0u};
  3204. for(Voice *voice : voicelist)
  3205. {
  3206. ALuint sid{voice->mSourceID.load(std::memory_order_acquire)};
  3207. ALsource *source = sid ? LookupSource(context, sid) : nullptr;
  3208. if(source && source->VoiceIdx == vidx)
  3209. {
  3210. if(std::exchange(source->mPropsDirty, false))
  3211. UpdateSourceProps(source, voice, context);
  3212. }
  3213. ++vidx;
  3214. }
  3215. }
  3216. }
  3217. SourceSubList::~SourceSubList()
  3218. {
  3219. uint64_t usemask{~FreeMask};
  3220. while(usemask)
  3221. {
  3222. const int idx{al::countr_zero(usemask)};
  3223. usemask &= ~(1_u64 << idx);
  3224. al::destroy_at(Sources+idx);
  3225. }
  3226. FreeMask = ~usemask;
  3227. al_free(Sources);
  3228. Sources = nullptr;
  3229. }
  3230. #ifdef ALSOFT_EAX
  3231. class EaxSourceException :
  3232. public EaxException
  3233. {
  3234. public:
  3235. explicit EaxSourceException(
  3236. const char* message)
  3237. :
  3238. EaxException{"EAX_SOURCE", message}
  3239. {
  3240. }
  3241. }; // EaxSourceException
  3242. class EaxSourceActiveFxSlotsException :
  3243. public EaxException
  3244. {
  3245. public:
  3246. explicit EaxSourceActiveFxSlotsException(
  3247. const char* message)
  3248. :
  3249. EaxException{"EAX_SOURCE_ACTIVE_FX_SLOTS", message}
  3250. {
  3251. }
  3252. }; // EaxSourceActiveFxSlotsException
  3253. class EaxSourceSendException :
  3254. public EaxException
  3255. {
  3256. public:
  3257. explicit EaxSourceSendException(
  3258. const char* message)
  3259. :
  3260. EaxException{"EAX_SOURCE_SEND", message}
  3261. {
  3262. }
  3263. }; // EaxSourceSendException
  3264. void EaxUpdateSourceVoice(ALsource *source, ALCcontext *context)
  3265. {
  3266. if(Voice *voice{GetSourceVoice(source, context)})
  3267. {
  3268. if(std::exchange(source->mPropsDirty, false))
  3269. UpdateSourceProps(source, voice, context);
  3270. }
  3271. }
  3272. void ALsource::eax_initialize(ALCcontext *context) noexcept
  3273. {
  3274. assert(context);
  3275. eax_al_context_ = context;
  3276. eax_set_defaults();
  3277. eax_initialize_fx_slots();
  3278. eax_d_ = eax_;
  3279. }
  3280. void ALsource::eax_update_filters()
  3281. {
  3282. eax_update_filters_internal();
  3283. }
  3284. void ALsource::eax_update(EaxContextSharedDirtyFlags)
  3285. {
  3286. /* NOTE: EaxContextSharedDirtyFlags only has one flag (primary_fx_slot_id),
  3287. * which must be true for this to be called.
  3288. */
  3289. if(eax_uses_primary_id_)
  3290. eax_update_primary_fx_slot_id();
  3291. }
  3292. void ALsource::eax_commit_and_update()
  3293. {
  3294. eax_apply_deferred();
  3295. EaxUpdateSourceVoice(this, eax_al_context_);
  3296. }
  3297. ALsource* ALsource::eax_lookup_source(
  3298. ALCcontext& al_context,
  3299. ALuint source_id) noexcept
  3300. {
  3301. return LookupSource(&al_context, source_id);
  3302. }
  3303. [[noreturn]]
  3304. void ALsource::eax_fail(
  3305. const char* message)
  3306. {
  3307. throw EaxSourceException{message};
  3308. }
  3309. void ALsource::eax_set_source_defaults() noexcept
  3310. {
  3311. eax1_.fMix = EAX_REVERBMIX_USEDISTANCE;
  3312. eax_.source.lDirect = EAXSOURCE_DEFAULTDIRECT;
  3313. eax_.source.lDirectHF = EAXSOURCE_DEFAULTDIRECTHF;
  3314. eax_.source.lRoom = EAXSOURCE_DEFAULTROOM;
  3315. eax_.source.lRoomHF = EAXSOURCE_DEFAULTROOMHF;
  3316. eax_.source.lObstruction = EAXSOURCE_DEFAULTOBSTRUCTION;
  3317. eax_.source.flObstructionLFRatio = EAXSOURCE_DEFAULTOBSTRUCTIONLFRATIO;
  3318. eax_.source.lOcclusion = EAXSOURCE_DEFAULTOCCLUSION;
  3319. eax_.source.flOcclusionLFRatio = EAXSOURCE_DEFAULTOCCLUSIONLFRATIO;
  3320. eax_.source.flOcclusionRoomRatio = EAXSOURCE_DEFAULTOCCLUSIONROOMRATIO;
  3321. eax_.source.flOcclusionDirectRatio = EAXSOURCE_DEFAULTOCCLUSIONDIRECTRATIO;
  3322. eax_.source.lExclusion = EAXSOURCE_DEFAULTEXCLUSION;
  3323. eax_.source.flExclusionLFRatio = EAXSOURCE_DEFAULTEXCLUSIONLFRATIO;
  3324. eax_.source.lOutsideVolumeHF = EAXSOURCE_DEFAULTOUTSIDEVOLUMEHF;
  3325. eax_.source.flDopplerFactor = EAXSOURCE_DEFAULTDOPPLERFACTOR;
  3326. eax_.source.flRolloffFactor = EAXSOURCE_DEFAULTROLLOFFFACTOR;
  3327. eax_.source.flRoomRolloffFactor = EAXSOURCE_DEFAULTROOMROLLOFFFACTOR;
  3328. eax_.source.flAirAbsorptionFactor = EAXSOURCE_DEFAULTAIRABSORPTIONFACTOR;
  3329. eax_.source.ulFlags = EAXSOURCE_DEFAULTFLAGS;
  3330. eax_.source.flMacroFXFactor = EAXSOURCE_DEFAULTMACROFXFACTOR;
  3331. }
  3332. void ALsource::eax_set_active_fx_slots_defaults() noexcept
  3333. {
  3334. eax_.active_fx_slots = EAX50SOURCE_3DDEFAULTACTIVEFXSLOTID;
  3335. }
  3336. void ALsource::eax_set_send_defaults(EAXSOURCEALLSENDPROPERTIES& eax_send) noexcept
  3337. {
  3338. eax_send.guidReceivingFXSlotID = EAX_NULL_GUID;
  3339. eax_send.lSend = EAXSOURCE_DEFAULTSEND;
  3340. eax_send.lSendHF = EAXSOURCE_DEFAULTSENDHF;
  3341. eax_send.lOcclusion = EAXSOURCE_DEFAULTOCCLUSION;
  3342. eax_send.flOcclusionLFRatio = EAXSOURCE_DEFAULTOCCLUSIONLFRATIO;
  3343. eax_send.flOcclusionRoomRatio = EAXSOURCE_DEFAULTOCCLUSIONROOMRATIO;
  3344. eax_send.flOcclusionDirectRatio = EAXSOURCE_DEFAULTOCCLUSIONDIRECTRATIO;
  3345. eax_send.lExclusion = EAXSOURCE_DEFAULTEXCLUSION;
  3346. eax_send.flExclusionLFRatio = EAXSOURCE_DEFAULTEXCLUSIONLFRATIO;
  3347. }
  3348. void ALsource::eax_set_sends_defaults() noexcept
  3349. {
  3350. for (auto& eax_send : eax_.sends)
  3351. {
  3352. eax_set_send_defaults(eax_send);
  3353. }
  3354. }
  3355. void ALsource::eax_set_speaker_levels_defaults() noexcept
  3356. {
  3357. std::fill(eax_.speaker_levels.begin(), eax_.speaker_levels.end(), EAXSOURCE_DEFAULTSPEAKERLEVEL);
  3358. }
  3359. void ALsource::eax_set_defaults() noexcept
  3360. {
  3361. eax_set_source_defaults();
  3362. eax_set_active_fx_slots_defaults();
  3363. eax_set_sends_defaults();
  3364. eax_set_speaker_levels_defaults();
  3365. }
  3366. float ALsource::eax_calculate_dst_occlusion_mb(
  3367. long src_occlusion_mb,
  3368. float path_ratio,
  3369. float lf_ratio) noexcept
  3370. {
  3371. const auto ratio_1 = path_ratio + lf_ratio - 1.0F;
  3372. const auto ratio_2 = path_ratio * lf_ratio;
  3373. const auto ratio = (ratio_2 > ratio_1) ? ratio_2 : ratio_1;
  3374. const auto dst_occlustion_mb = static_cast<float>(src_occlusion_mb) * ratio;
  3375. return dst_occlustion_mb;
  3376. }
  3377. EaxAlLowPassParam ALsource::eax_create_direct_filter_param() const noexcept
  3378. {
  3379. auto gain_mb =
  3380. static_cast<float>(eax_.source.lDirect) +
  3381. (static_cast<float>(eax_.source.lObstruction) * eax_.source.flObstructionLFRatio) +
  3382. eax_calculate_dst_occlusion_mb(
  3383. eax_.source.lOcclusion,
  3384. eax_.source.flOcclusionDirectRatio,
  3385. eax_.source.flOcclusionLFRatio);
  3386. auto gain_hf_mb =
  3387. static_cast<float>(eax_.source.lDirectHF) +
  3388. static_cast<float>(eax_.source.lObstruction) +
  3389. (static_cast<float>(eax_.source.lOcclusion) * eax_.source.flOcclusionDirectRatio);
  3390. for (auto i = std::size_t{}; i < EAX_MAX_FXSLOTS; ++i)
  3391. {
  3392. if (eax_active_fx_slots_[i])
  3393. {
  3394. const auto& send = eax_.sends[i];
  3395. gain_mb += eax_calculate_dst_occlusion_mb(
  3396. send.lOcclusion,
  3397. send.flOcclusionDirectRatio,
  3398. send.flOcclusionLFRatio);
  3399. gain_hf_mb += static_cast<float>(send.lOcclusion) * send.flOcclusionDirectRatio;
  3400. }
  3401. }
  3402. const auto al_low_pass_param = EaxAlLowPassParam
  3403. {
  3404. level_mb_to_gain(gain_mb),
  3405. minf(level_mb_to_gain(gain_hf_mb), 1.0f)
  3406. };
  3407. return al_low_pass_param;
  3408. }
  3409. EaxAlLowPassParam ALsource::eax_create_room_filter_param(
  3410. const ALeffectslot& fx_slot,
  3411. const EAXSOURCEALLSENDPROPERTIES& send) const noexcept
  3412. {
  3413. const auto& fx_slot_eax = fx_slot.eax_get_eax_fx_slot();
  3414. const auto gain_mb =
  3415. static_cast<float>(
  3416. eax_.source.lRoom +
  3417. send.lSend) +
  3418. eax_calculate_dst_occlusion_mb(
  3419. eax_.source.lOcclusion,
  3420. eax_.source.flOcclusionRoomRatio,
  3421. eax_.source.flOcclusionLFRatio
  3422. ) +
  3423. eax_calculate_dst_occlusion_mb(
  3424. send.lOcclusion,
  3425. send.flOcclusionRoomRatio,
  3426. send.flOcclusionLFRatio
  3427. ) +
  3428. (static_cast<float>(eax_.source.lExclusion) * eax_.source.flExclusionLFRatio) +
  3429. (static_cast<float>(send.lExclusion) * send.flExclusionLFRatio) +
  3430. 0.0F;
  3431. const auto gain_hf_mb =
  3432. static_cast<float>(
  3433. eax_.source.lRoomHF +
  3434. send.lSendHF) +
  3435. (static_cast<float>(fx_slot_eax.lOcclusion + eax_.source.lOcclusion) * eax_.source.flOcclusionRoomRatio) +
  3436. (static_cast<float>(send.lOcclusion) * send.flOcclusionRoomRatio) +
  3437. static_cast<float>(
  3438. eax_.source.lExclusion +
  3439. send.lExclusion) +
  3440. 0.0F;
  3441. const auto al_low_pass_param = EaxAlLowPassParam
  3442. {
  3443. level_mb_to_gain(gain_mb),
  3444. minf(level_mb_to_gain(gain_hf_mb), 1.0f)
  3445. };
  3446. return al_low_pass_param;
  3447. }
  3448. void ALsource::eax_set_fx_slots()
  3449. {
  3450. eax_uses_primary_id_ = false;
  3451. eax_has_active_fx_slots_ = false;
  3452. eax_active_fx_slots_.fill(false);
  3453. for(const auto& eax_active_fx_slot_id : eax_.active_fx_slots.guidActiveFXSlots)
  3454. {
  3455. auto fx_slot_index = EaxFxSlotIndex{};
  3456. if(eax_active_fx_slot_id == EAX_PrimaryFXSlotID)
  3457. {
  3458. eax_uses_primary_id_ = true;
  3459. fx_slot_index = eax_al_context_->eax_get_primary_fx_slot_index();
  3460. }
  3461. else
  3462. {
  3463. fx_slot_index = eax_active_fx_slot_id;
  3464. }
  3465. if(fx_slot_index.has_value())
  3466. {
  3467. eax_has_active_fx_slots_ = true;
  3468. eax_active_fx_slots_[*fx_slot_index] = true;
  3469. }
  3470. }
  3471. for(auto i = 0u;i < eax_active_fx_slots_.size();++i)
  3472. {
  3473. if(!eax_active_fx_slots_[i])
  3474. eax_set_al_source_send(nullptr, i, EaxAlLowPassParam{1.0f, 1.0f});
  3475. }
  3476. }
  3477. void ALsource::eax_initialize_fx_slots()
  3478. {
  3479. eax_set_fx_slots();
  3480. eax_update_filters_internal();
  3481. }
  3482. void ALsource::eax_update_direct_filter_internal()
  3483. {
  3484. const auto& direct_param = eax_create_direct_filter_param();
  3485. Direct.Gain = direct_param.gain;
  3486. Direct.GainHF = direct_param.gain_hf;
  3487. Direct.HFReference = LOWPASSFREQREF;
  3488. Direct.GainLF = 1.0f;
  3489. Direct.LFReference = HIGHPASSFREQREF;
  3490. mPropsDirty = true;
  3491. }
  3492. void ALsource::eax_update_room_filters_internal()
  3493. {
  3494. if (!eax_has_active_fx_slots_)
  3495. {
  3496. return;
  3497. }
  3498. for (auto i = 0u; i < EAX_MAX_FXSLOTS; ++i)
  3499. {
  3500. if (eax_active_fx_slots_[i])
  3501. {
  3502. auto& fx_slot = eax_al_context_->eax_get_fx_slot(static_cast<std::size_t>(i));
  3503. const auto& send = eax_.sends[i];
  3504. const auto& room_param = eax_create_room_filter_param(fx_slot, send);
  3505. eax_set_al_source_send(&fx_slot, i, room_param);
  3506. }
  3507. }
  3508. }
  3509. void ALsource::eax_update_filters_internal()
  3510. {
  3511. eax_update_direct_filter_internal();
  3512. eax_update_room_filters_internal();
  3513. }
  3514. void ALsource::eax_update_primary_fx_slot_id()
  3515. {
  3516. const auto& previous_primary_fx_slot_index = eax_al_context_->eax_get_previous_primary_fx_slot_index();
  3517. const auto& primary_fx_slot_index = eax_al_context_->eax_get_primary_fx_slot_index();
  3518. if (previous_primary_fx_slot_index == primary_fx_slot_index)
  3519. {
  3520. return;
  3521. }
  3522. if (previous_primary_fx_slot_index.has_value())
  3523. {
  3524. const auto fx_slot_index = previous_primary_fx_slot_index.value();
  3525. eax_active_fx_slots_[fx_slot_index] = false;
  3526. eax_set_al_source_send(nullptr, fx_slot_index, EaxAlLowPassParam{1.0f, 1.0f});
  3527. }
  3528. if (primary_fx_slot_index.has_value())
  3529. {
  3530. const auto fx_slot_index = primary_fx_slot_index.value();
  3531. eax_active_fx_slots_[fx_slot_index] = true;
  3532. auto& fx_slot = eax_al_context_->eax_get_fx_slot(fx_slot_index);
  3533. const auto& send = eax_.sends[fx_slot_index];
  3534. const auto& room_param = eax_create_room_filter_param(fx_slot, send);
  3535. eax_set_al_source_send(&fx_slot, fx_slot_index, room_param);
  3536. }
  3537. eax_has_active_fx_slots_ = std::any_of(
  3538. eax_active_fx_slots_.cbegin(),
  3539. eax_active_fx_slots_.cend(),
  3540. [](const auto& item)
  3541. {
  3542. return item;
  3543. }
  3544. );
  3545. }
  3546. void ALsource::eax_defer_active_fx_slots(
  3547. const EaxEaxCall& eax_call)
  3548. {
  3549. const auto active_fx_slots_span =
  3550. eax_call.get_values<EaxSourceActiveFxSlotsException, const GUID>();
  3551. const auto fx_slot_count = active_fx_slots_span.size();
  3552. if (fx_slot_count <= 0 || fx_slot_count > EAX_MAX_FXSLOTS)
  3553. {
  3554. throw EaxSourceActiveFxSlotsException{"Count out of range."};
  3555. }
  3556. for (auto i = std::size_t{}; i < fx_slot_count; ++i)
  3557. {
  3558. const auto& fx_slot_guid = active_fx_slots_span[i];
  3559. if (fx_slot_guid != EAX_NULL_GUID &&
  3560. fx_slot_guid != EAX_PrimaryFXSlotID &&
  3561. fx_slot_guid != EAXPROPERTYID_EAX40_FXSlot0 &&
  3562. fx_slot_guid != EAXPROPERTYID_EAX50_FXSlot0 &&
  3563. fx_slot_guid != EAXPROPERTYID_EAX40_FXSlot1 &&
  3564. fx_slot_guid != EAXPROPERTYID_EAX50_FXSlot1 &&
  3565. fx_slot_guid != EAXPROPERTYID_EAX40_FXSlot2 &&
  3566. fx_slot_guid != EAXPROPERTYID_EAX50_FXSlot2 &&
  3567. fx_slot_guid != EAXPROPERTYID_EAX40_FXSlot3 &&
  3568. fx_slot_guid != EAXPROPERTYID_EAX50_FXSlot3)
  3569. {
  3570. throw EaxSourceActiveFxSlotsException{"Unsupported GUID."};
  3571. }
  3572. }
  3573. for (auto i = std::size_t{}; i < fx_slot_count; ++i)
  3574. {
  3575. eax_d_.active_fx_slots.guidActiveFXSlots[i] = active_fx_slots_span[i];
  3576. }
  3577. for (auto i = fx_slot_count; i < EAX_MAX_FXSLOTS; ++i)
  3578. {
  3579. eax_d_.active_fx_slots.guidActiveFXSlots[i] = EAX_NULL_GUID;
  3580. }
  3581. eax_are_active_fx_slots_dirty_ = (eax_d_.active_fx_slots != eax_.active_fx_slots);
  3582. }
  3583. const char* ALsource::eax_get_exclusion_name() noexcept
  3584. {
  3585. return "Exclusion";
  3586. }
  3587. const char* ALsource::eax_get_exclusion_lf_ratio_name() noexcept
  3588. {
  3589. return "Exclusion LF Ratio";
  3590. }
  3591. const char* ALsource::eax_get_occlusion_name() noexcept
  3592. {
  3593. return "Occlusion";
  3594. }
  3595. const char* ALsource::eax_get_occlusion_lf_ratio_name() noexcept
  3596. {
  3597. return "Occlusion LF Ratio";
  3598. }
  3599. const char* ALsource::eax_get_occlusion_direct_ratio_name() noexcept
  3600. {
  3601. return "Occlusion Direct Ratio";
  3602. }
  3603. const char* ALsource::eax_get_occlusion_room_ratio_name() noexcept
  3604. {
  3605. return "Occlusion Room Ratio";
  3606. }
  3607. void ALsource::eax1_validate_reverb_mix(float reverb_mix)
  3608. {
  3609. if (reverb_mix == EAX_REVERBMIX_USEDISTANCE)
  3610. return;
  3611. eax_validate_range<EaxSourceSendException>("Reverb Mix", reverb_mix, EAX_BUFFER_MINREVERBMIX, EAX_BUFFER_MAXREVERBMIX);
  3612. }
  3613. void ALsource::eax_validate_send_receiving_fx_slot_guid(
  3614. const GUID& guidReceivingFXSlotID)
  3615. {
  3616. if (guidReceivingFXSlotID != EAXPROPERTYID_EAX40_FXSlot0 &&
  3617. guidReceivingFXSlotID != EAXPROPERTYID_EAX50_FXSlot0 &&
  3618. guidReceivingFXSlotID != EAXPROPERTYID_EAX40_FXSlot1 &&
  3619. guidReceivingFXSlotID != EAXPROPERTYID_EAX50_FXSlot1 &&
  3620. guidReceivingFXSlotID != EAXPROPERTYID_EAX40_FXSlot2 &&
  3621. guidReceivingFXSlotID != EAXPROPERTYID_EAX50_FXSlot2 &&
  3622. guidReceivingFXSlotID != EAXPROPERTYID_EAX40_FXSlot3 &&
  3623. guidReceivingFXSlotID != EAXPROPERTYID_EAX50_FXSlot3)
  3624. {
  3625. throw EaxSourceSendException{"Unsupported receiving FX slot GUID."};
  3626. }
  3627. }
  3628. void ALsource::eax_validate_send_send(
  3629. long lSend)
  3630. {
  3631. eax_validate_range<EaxSourceSendException>(
  3632. "Send",
  3633. lSend,
  3634. EAXSOURCE_MINSEND,
  3635. EAXSOURCE_MAXSEND);
  3636. }
  3637. void ALsource::eax_validate_send_send_hf(
  3638. long lSendHF)
  3639. {
  3640. eax_validate_range<EaxSourceSendException>(
  3641. "Send HF",
  3642. lSendHF,
  3643. EAXSOURCE_MINSENDHF,
  3644. EAXSOURCE_MAXSENDHF);
  3645. }
  3646. void ALsource::eax_validate_send_occlusion(
  3647. long lOcclusion)
  3648. {
  3649. eax_validate_range<EaxSourceSendException>(
  3650. eax_get_occlusion_name(),
  3651. lOcclusion,
  3652. EAXSOURCE_MINOCCLUSION,
  3653. EAXSOURCE_MAXOCCLUSION);
  3654. }
  3655. void ALsource::eax_validate_send_occlusion_lf_ratio(
  3656. float flOcclusionLFRatio)
  3657. {
  3658. eax_validate_range<EaxSourceSendException>(
  3659. eax_get_occlusion_lf_ratio_name(),
  3660. flOcclusionLFRatio,
  3661. EAXSOURCE_MINOCCLUSIONLFRATIO,
  3662. EAXSOURCE_MAXOCCLUSIONLFRATIO);
  3663. }
  3664. void ALsource::eax_validate_send_occlusion_room_ratio(
  3665. float flOcclusionRoomRatio)
  3666. {
  3667. eax_validate_range<EaxSourceSendException>(
  3668. eax_get_occlusion_room_ratio_name(),
  3669. flOcclusionRoomRatio,
  3670. EAXSOURCE_MINOCCLUSIONROOMRATIO,
  3671. EAXSOURCE_MAXOCCLUSIONROOMRATIO);
  3672. }
  3673. void ALsource::eax_validate_send_occlusion_direct_ratio(
  3674. float flOcclusionDirectRatio)
  3675. {
  3676. eax_validate_range<EaxSourceSendException>(
  3677. eax_get_occlusion_direct_ratio_name(),
  3678. flOcclusionDirectRatio,
  3679. EAXSOURCE_MINOCCLUSIONDIRECTRATIO,
  3680. EAXSOURCE_MAXOCCLUSIONDIRECTRATIO);
  3681. }
  3682. void ALsource::eax_validate_send_exclusion(
  3683. long lExclusion)
  3684. {
  3685. eax_validate_range<EaxSourceSendException>(
  3686. eax_get_exclusion_name(),
  3687. lExclusion,
  3688. EAXSOURCE_MINEXCLUSION,
  3689. EAXSOURCE_MAXEXCLUSION);
  3690. }
  3691. void ALsource::eax_validate_send_exclusion_lf_ratio(
  3692. float flExclusionLFRatio)
  3693. {
  3694. eax_validate_range<EaxSourceSendException>(
  3695. eax_get_exclusion_lf_ratio_name(),
  3696. flExclusionLFRatio,
  3697. EAXSOURCE_MINEXCLUSIONLFRATIO,
  3698. EAXSOURCE_MAXEXCLUSIONLFRATIO);
  3699. }
  3700. void ALsource::eax_validate_send(
  3701. const EAXSOURCESENDPROPERTIES& all)
  3702. {
  3703. eax_validate_send_receiving_fx_slot_guid(all.guidReceivingFXSlotID);
  3704. eax_validate_send_send(all.lSend);
  3705. eax_validate_send_send_hf(all.lSendHF);
  3706. }
  3707. void ALsource::eax_validate_send_exclusion_all(
  3708. const EAXSOURCEEXCLUSIONSENDPROPERTIES& all)
  3709. {
  3710. eax_validate_send_receiving_fx_slot_guid(all.guidReceivingFXSlotID);
  3711. eax_validate_send_exclusion(all.lExclusion);
  3712. eax_validate_send_exclusion_lf_ratio(all.flExclusionLFRatio);
  3713. }
  3714. void ALsource::eax_validate_send_occlusion_all(
  3715. const EAXSOURCEOCCLUSIONSENDPROPERTIES& all)
  3716. {
  3717. eax_validate_send_receiving_fx_slot_guid(all.guidReceivingFXSlotID);
  3718. eax_validate_send_occlusion(all.lOcclusion);
  3719. eax_validate_send_occlusion_lf_ratio(all.flOcclusionLFRatio);
  3720. eax_validate_send_occlusion_room_ratio(all.flOcclusionRoomRatio);
  3721. eax_validate_send_occlusion_direct_ratio(all.flOcclusionDirectRatio);
  3722. }
  3723. void ALsource::eax_validate_send_all(
  3724. const EAXSOURCEALLSENDPROPERTIES& all)
  3725. {
  3726. eax_validate_send_receiving_fx_slot_guid(all.guidReceivingFXSlotID);
  3727. eax_validate_send_send(all.lSend);
  3728. eax_validate_send_send_hf(all.lSendHF);
  3729. eax_validate_send_occlusion(all.lOcclusion);
  3730. eax_validate_send_occlusion_lf_ratio(all.flOcclusionLFRatio);
  3731. eax_validate_send_occlusion_room_ratio(all.flOcclusionRoomRatio);
  3732. eax_validate_send_occlusion_direct_ratio(all.flOcclusionDirectRatio);
  3733. eax_validate_send_exclusion(all.lExclusion);
  3734. eax_validate_send_exclusion_lf_ratio(all.flExclusionLFRatio);
  3735. }
  3736. EaxFxSlotIndexValue ALsource::eax_get_send_index(
  3737. const GUID& send_guid)
  3738. {
  3739. if (false)
  3740. {
  3741. }
  3742. else if (send_guid == EAXPROPERTYID_EAX40_FXSlot0 || send_guid == EAXPROPERTYID_EAX50_FXSlot0)
  3743. {
  3744. return 0;
  3745. }
  3746. else if (send_guid == EAXPROPERTYID_EAX40_FXSlot1 || send_guid == EAXPROPERTYID_EAX50_FXSlot1)
  3747. {
  3748. return 1;
  3749. }
  3750. else if (send_guid == EAXPROPERTYID_EAX40_FXSlot2 || send_guid == EAXPROPERTYID_EAX50_FXSlot2)
  3751. {
  3752. return 2;
  3753. }
  3754. else if (send_guid == EAXPROPERTYID_EAX40_FXSlot3 || send_guid == EAXPROPERTYID_EAX50_FXSlot3)
  3755. {
  3756. return 3;
  3757. }
  3758. else
  3759. {
  3760. throw EaxSourceSendException{"Unsupported receiving FX slot GUID."};
  3761. }
  3762. }
  3763. void ALsource::eax_defer_send_send(
  3764. long lSend,
  3765. EaxFxSlotIndexValue index)
  3766. {
  3767. eax_d_.sends[index].lSend = lSend;
  3768. eax_sends_dirty_flags_.sends[index].lSend =
  3769. (eax_.sends[index].lSend != eax_d_.sends[index].lSend);
  3770. }
  3771. void ALsource::eax_defer_send_send_hf(
  3772. long lSendHF,
  3773. EaxFxSlotIndexValue index)
  3774. {
  3775. eax_d_.sends[index].lSendHF = lSendHF;
  3776. eax_sends_dirty_flags_.sends[index].lSendHF =
  3777. (eax_.sends[index].lSendHF != eax_d_.sends[index].lSendHF);
  3778. }
  3779. void ALsource::eax_defer_send_occlusion(
  3780. long lOcclusion,
  3781. EaxFxSlotIndexValue index)
  3782. {
  3783. eax_d_.sends[index].lOcclusion = lOcclusion;
  3784. eax_sends_dirty_flags_.sends[index].lOcclusion =
  3785. (eax_.sends[index].lOcclusion != eax_d_.sends[index].lOcclusion);
  3786. }
  3787. void ALsource::eax_defer_send_occlusion_lf_ratio(
  3788. float flOcclusionLFRatio,
  3789. EaxFxSlotIndexValue index)
  3790. {
  3791. eax_d_.sends[index].flOcclusionLFRatio = flOcclusionLFRatio;
  3792. eax_sends_dirty_flags_.sends[index].flOcclusionLFRatio =
  3793. (eax_.sends[index].flOcclusionLFRatio != eax_d_.sends[index].flOcclusionLFRatio);
  3794. }
  3795. void ALsource::eax_defer_send_occlusion_room_ratio(
  3796. float flOcclusionRoomRatio,
  3797. EaxFxSlotIndexValue index)
  3798. {
  3799. eax_d_.sends[index].flOcclusionRoomRatio = flOcclusionRoomRatio;
  3800. eax_sends_dirty_flags_.sends[index].flOcclusionRoomRatio =
  3801. (eax_.sends[index].flOcclusionRoomRatio != eax_d_.sends[index].flOcclusionRoomRatio);
  3802. }
  3803. void ALsource::eax_defer_send_occlusion_direct_ratio(
  3804. float flOcclusionDirectRatio,
  3805. EaxFxSlotIndexValue index)
  3806. {
  3807. eax_d_.sends[index].flOcclusionDirectRatio = flOcclusionDirectRatio;
  3808. eax_sends_dirty_flags_.sends[index].flOcclusionDirectRatio =
  3809. (eax_.sends[index].flOcclusionDirectRatio != eax_d_.sends[index].flOcclusionDirectRatio);
  3810. }
  3811. void ALsource::eax_defer_send_exclusion(
  3812. long lExclusion,
  3813. EaxFxSlotIndexValue index)
  3814. {
  3815. eax_d_.sends[index].lExclusion = lExclusion;
  3816. eax_sends_dirty_flags_.sends[index].lExclusion =
  3817. (eax_.sends[index].lExclusion != eax_d_.sends[index].lExclusion);
  3818. }
  3819. void ALsource::eax_defer_send_exclusion_lf_ratio(
  3820. float flExclusionLFRatio,
  3821. EaxFxSlotIndexValue index)
  3822. {
  3823. eax_d_.sends[index].flExclusionLFRatio = flExclusionLFRatio;
  3824. eax_sends_dirty_flags_.sends[index].flExclusionLFRatio =
  3825. (eax_.sends[index].flExclusionLFRatio != eax_d_.sends[index].flExclusionLFRatio);
  3826. }
  3827. void ALsource::eax_defer_send(
  3828. const EAXSOURCESENDPROPERTIES& all,
  3829. EaxFxSlotIndexValue index)
  3830. {
  3831. eax_defer_send_send(all.lSend, index);
  3832. eax_defer_send_send_hf(all.lSendHF, index);
  3833. }
  3834. void ALsource::eax_defer_send_exclusion_all(
  3835. const EAXSOURCEEXCLUSIONSENDPROPERTIES& all,
  3836. EaxFxSlotIndexValue index)
  3837. {
  3838. eax_defer_send_exclusion(all.lExclusion, index);
  3839. eax_defer_send_exclusion_lf_ratio(all.flExclusionLFRatio, index);
  3840. }
  3841. void ALsource::eax_defer_send_occlusion_all(
  3842. const EAXSOURCEOCCLUSIONSENDPROPERTIES& all,
  3843. EaxFxSlotIndexValue index)
  3844. {
  3845. eax_defer_send_occlusion(all.lOcclusion, index);
  3846. eax_defer_send_occlusion_lf_ratio(all.flOcclusionLFRatio, index);
  3847. eax_defer_send_occlusion_room_ratio(all.flOcclusionRoomRatio, index);
  3848. eax_defer_send_occlusion_direct_ratio(all.flOcclusionDirectRatio, index);
  3849. }
  3850. void ALsource::eax_defer_send_all(
  3851. const EAXSOURCEALLSENDPROPERTIES& all,
  3852. EaxFxSlotIndexValue index)
  3853. {
  3854. eax_defer_send_send(all.lSend, index);
  3855. eax_defer_send_send_hf(all.lSendHF, index);
  3856. eax_defer_send_occlusion(all.lOcclusion, index);
  3857. eax_defer_send_occlusion_lf_ratio(all.flOcclusionLFRatio, index);
  3858. eax_defer_send_occlusion_room_ratio(all.flOcclusionRoomRatio, index);
  3859. eax_defer_send_occlusion_direct_ratio(all.flOcclusionDirectRatio, index);
  3860. eax_defer_send_exclusion(all.lExclusion, index);
  3861. eax_defer_send_exclusion_lf_ratio(all.flExclusionLFRatio, index);
  3862. }
  3863. void ALsource::eax_defer_send(
  3864. const EaxEaxCall& eax_call)
  3865. {
  3866. const auto eax_all_span =
  3867. eax_call.get_values<EaxSourceException, const EAXSOURCESENDPROPERTIES>();
  3868. const auto count = eax_all_span.size();
  3869. if (count <= 0 || count > EAX_MAX_FXSLOTS)
  3870. {
  3871. throw EaxSourceSendException{"Send count out of range."};
  3872. }
  3873. for (auto i = std::size_t{}; i < count; ++i)
  3874. {
  3875. const auto& all = eax_all_span[i];
  3876. eax_validate_send(all);
  3877. }
  3878. for (auto i = std::size_t{}; i < count; ++i)
  3879. {
  3880. const auto& all = eax_all_span[i];
  3881. const auto send_index = eax_get_send_index(all.guidReceivingFXSlotID);
  3882. eax_defer_send(all, send_index);
  3883. }
  3884. }
  3885. void ALsource::eax_defer_send_exclusion_all(
  3886. const EaxEaxCall& eax_call)
  3887. {
  3888. const auto eax_all_span =
  3889. eax_call.get_values<EaxSourceException, const EAXSOURCEEXCLUSIONSENDPROPERTIES>();
  3890. const auto count = eax_all_span.size();
  3891. if (count <= 0 || count > EAX_MAX_FXSLOTS)
  3892. {
  3893. throw EaxSourceSendException{"Send exclusion all count out of range."};
  3894. }
  3895. for (auto i = std::size_t{}; i < count; ++i)
  3896. {
  3897. const auto& all = eax_all_span[i];
  3898. eax_validate_send_exclusion_all(all);
  3899. }
  3900. for (auto i = std::size_t{}; i < count; ++i)
  3901. {
  3902. const auto& all = eax_all_span[i];
  3903. const auto send_index = eax_get_send_index(all.guidReceivingFXSlotID);
  3904. eax_defer_send_exclusion_all(all, send_index);
  3905. }
  3906. }
  3907. void ALsource::eax_defer_send_occlusion_all(
  3908. const EaxEaxCall& eax_call)
  3909. {
  3910. const auto eax_all_span =
  3911. eax_call.get_values<EaxSourceException, const EAXSOURCEOCCLUSIONSENDPROPERTIES>();
  3912. const auto count = eax_all_span.size();
  3913. if (count <= 0 || count > EAX_MAX_FXSLOTS)
  3914. {
  3915. throw EaxSourceSendException{"Send occlusion all count out of range."};
  3916. }
  3917. for (auto i = std::size_t{}; i < count; ++i)
  3918. {
  3919. const auto& all = eax_all_span[i];
  3920. eax_validate_send_occlusion_all(all);
  3921. }
  3922. for (auto i = std::size_t{}; i < count; ++i)
  3923. {
  3924. const auto& all = eax_all_span[i];
  3925. const auto send_index = eax_get_send_index(all.guidReceivingFXSlotID);
  3926. eax_defer_send_occlusion_all(all, send_index);
  3927. }
  3928. }
  3929. void ALsource::eax_defer_send_all(
  3930. const EaxEaxCall& eax_call)
  3931. {
  3932. const auto eax_all_span =
  3933. eax_call.get_values<EaxSourceException, const EAXSOURCEALLSENDPROPERTIES>();
  3934. const auto count = eax_all_span.size();
  3935. if (count <= 0 || count > EAX_MAX_FXSLOTS)
  3936. {
  3937. throw EaxSourceSendException{"Send all count out of range."};
  3938. }
  3939. for (auto i = std::size_t{}; i < count; ++i)
  3940. {
  3941. const auto& all = eax_all_span[i];
  3942. eax_validate_send_all(all);
  3943. }
  3944. for (auto i = std::size_t{}; i < count; ++i)
  3945. {
  3946. const auto& all = eax_all_span[i];
  3947. const auto send_index = eax_get_send_index(all.guidReceivingFXSlotID);
  3948. eax_defer_send_all(all, send_index);
  3949. }
  3950. }
  3951. void ALsource::eax_validate_source_direct(
  3952. long direct)
  3953. {
  3954. eax_validate_range<EaxSourceException>(
  3955. "Direct",
  3956. direct,
  3957. EAXSOURCE_MINDIRECT,
  3958. EAXSOURCE_MAXDIRECT);
  3959. }
  3960. void ALsource::eax_validate_source_direct_hf(
  3961. long direct_hf)
  3962. {
  3963. eax_validate_range<EaxSourceException>(
  3964. "Direct HF",
  3965. direct_hf,
  3966. EAXSOURCE_MINDIRECTHF,
  3967. EAXSOURCE_MAXDIRECTHF);
  3968. }
  3969. void ALsource::eax_validate_source_room(
  3970. long room)
  3971. {
  3972. eax_validate_range<EaxSourceException>(
  3973. "Room",
  3974. room,
  3975. EAXSOURCE_MINROOM,
  3976. EAXSOURCE_MAXROOM);
  3977. }
  3978. void ALsource::eax_validate_source_room_hf(
  3979. long room_hf)
  3980. {
  3981. eax_validate_range<EaxSourceException>(
  3982. "Room HF",
  3983. room_hf,
  3984. EAXSOURCE_MINROOMHF,
  3985. EAXSOURCE_MAXROOMHF);
  3986. }
  3987. void ALsource::eax_validate_source_obstruction(
  3988. long obstruction)
  3989. {
  3990. eax_validate_range<EaxSourceException>(
  3991. "Obstruction",
  3992. obstruction,
  3993. EAXSOURCE_MINOBSTRUCTION,
  3994. EAXSOURCE_MAXOBSTRUCTION);
  3995. }
  3996. void ALsource::eax_validate_source_obstruction_lf_ratio(
  3997. float obstruction_lf_ratio)
  3998. {
  3999. eax_validate_range<EaxSourceException>(
  4000. "Obstruction LF Ratio",
  4001. obstruction_lf_ratio,
  4002. EAXSOURCE_MINOBSTRUCTIONLFRATIO,
  4003. EAXSOURCE_MAXOBSTRUCTIONLFRATIO);
  4004. }
  4005. void ALsource::eax_validate_source_occlusion(
  4006. long occlusion)
  4007. {
  4008. eax_validate_range<EaxSourceException>(
  4009. eax_get_occlusion_name(),
  4010. occlusion,
  4011. EAXSOURCE_MINOCCLUSION,
  4012. EAXSOURCE_MAXOCCLUSION);
  4013. }
  4014. void ALsource::eax_validate_source_occlusion_lf_ratio(
  4015. float occlusion_lf_ratio)
  4016. {
  4017. eax_validate_range<EaxSourceException>(
  4018. eax_get_occlusion_lf_ratio_name(),
  4019. occlusion_lf_ratio,
  4020. EAXSOURCE_MINOCCLUSIONLFRATIO,
  4021. EAXSOURCE_MAXOCCLUSIONLFRATIO);
  4022. }
  4023. void ALsource::eax_validate_source_occlusion_room_ratio(
  4024. float occlusion_room_ratio)
  4025. {
  4026. eax_validate_range<EaxSourceException>(
  4027. eax_get_occlusion_room_ratio_name(),
  4028. occlusion_room_ratio,
  4029. EAXSOURCE_MINOCCLUSIONROOMRATIO,
  4030. EAXSOURCE_MAXOCCLUSIONROOMRATIO);
  4031. }
  4032. void ALsource::eax_validate_source_occlusion_direct_ratio(
  4033. float occlusion_direct_ratio)
  4034. {
  4035. eax_validate_range<EaxSourceException>(
  4036. eax_get_occlusion_direct_ratio_name(),
  4037. occlusion_direct_ratio,
  4038. EAXSOURCE_MINOCCLUSIONDIRECTRATIO,
  4039. EAXSOURCE_MAXOCCLUSIONDIRECTRATIO);
  4040. }
  4041. void ALsource::eax_validate_source_exclusion(
  4042. long exclusion)
  4043. {
  4044. eax_validate_range<EaxSourceException>(
  4045. eax_get_exclusion_name(),
  4046. exclusion,
  4047. EAXSOURCE_MINEXCLUSION,
  4048. EAXSOURCE_MAXEXCLUSION);
  4049. }
  4050. void ALsource::eax_validate_source_exclusion_lf_ratio(
  4051. float exclusion_lf_ratio)
  4052. {
  4053. eax_validate_range<EaxSourceException>(
  4054. eax_get_exclusion_lf_ratio_name(),
  4055. exclusion_lf_ratio,
  4056. EAXSOURCE_MINEXCLUSIONLFRATIO,
  4057. EAXSOURCE_MAXEXCLUSIONLFRATIO);
  4058. }
  4059. void ALsource::eax_validate_source_outside_volume_hf(
  4060. long outside_volume_hf)
  4061. {
  4062. eax_validate_range<EaxSourceException>(
  4063. "Outside Volume HF",
  4064. outside_volume_hf,
  4065. EAXSOURCE_MINOUTSIDEVOLUMEHF,
  4066. EAXSOURCE_MAXOUTSIDEVOLUMEHF);
  4067. }
  4068. void ALsource::eax_validate_source_doppler_factor(
  4069. float doppler_factor)
  4070. {
  4071. eax_validate_range<EaxSourceException>(
  4072. "Doppler Factor",
  4073. doppler_factor,
  4074. EAXSOURCE_MINDOPPLERFACTOR,
  4075. EAXSOURCE_MAXDOPPLERFACTOR);
  4076. }
  4077. void ALsource::eax_validate_source_rolloff_factor(
  4078. float rolloff_factor)
  4079. {
  4080. eax_validate_range<EaxSourceException>(
  4081. "Rolloff Factor",
  4082. rolloff_factor,
  4083. EAXSOURCE_MINROLLOFFFACTOR,
  4084. EAXSOURCE_MAXROLLOFFFACTOR);
  4085. }
  4086. void ALsource::eax_validate_source_room_rolloff_factor(
  4087. float room_rolloff_factor)
  4088. {
  4089. eax_validate_range<EaxSourceException>(
  4090. "Room Rolloff Factor",
  4091. room_rolloff_factor,
  4092. EAXSOURCE_MINROOMROLLOFFFACTOR,
  4093. EAXSOURCE_MAXROOMROLLOFFFACTOR);
  4094. }
  4095. void ALsource::eax_validate_source_air_absorption_factor(
  4096. float air_absorption_factor)
  4097. {
  4098. eax_validate_range<EaxSourceException>(
  4099. "Air Absorption Factor",
  4100. air_absorption_factor,
  4101. EAXSOURCE_MINAIRABSORPTIONFACTOR,
  4102. EAXSOURCE_MAXAIRABSORPTIONFACTOR);
  4103. }
  4104. void ALsource::eax_validate_source_flags(
  4105. unsigned long flags,
  4106. int eax_version)
  4107. {
  4108. eax_validate_range<EaxSourceException>(
  4109. "Flags",
  4110. flags,
  4111. 0UL,
  4112. ~((eax_version == 5) ? EAX50SOURCEFLAGS_RESERVED : EAX20SOURCEFLAGS_RESERVED));
  4113. }
  4114. void ALsource::eax_validate_source_macro_fx_factor(
  4115. float macro_fx_factor)
  4116. {
  4117. eax_validate_range<EaxSourceException>(
  4118. "Macro FX Factor",
  4119. macro_fx_factor,
  4120. EAXSOURCE_MINMACROFXFACTOR,
  4121. EAXSOURCE_MAXMACROFXFACTOR);
  4122. }
  4123. void ALsource::eax_validate_source_2d_all(
  4124. const EAXSOURCE2DPROPERTIES& all,
  4125. int eax_version)
  4126. {
  4127. eax_validate_source_direct(all.lDirect);
  4128. eax_validate_source_direct_hf(all.lDirectHF);
  4129. eax_validate_source_room(all.lRoom);
  4130. eax_validate_source_room_hf(all.lRoomHF);
  4131. eax_validate_source_flags(all.ulFlags, eax_version);
  4132. }
  4133. void ALsource::eax_validate_source_obstruction_all(
  4134. const EAXOBSTRUCTIONPROPERTIES& all)
  4135. {
  4136. eax_validate_source_obstruction(all.lObstruction);
  4137. eax_validate_source_obstruction_lf_ratio(all.flObstructionLFRatio);
  4138. }
  4139. void ALsource::eax_validate_source_exclusion_all(
  4140. const EAXEXCLUSIONPROPERTIES& all)
  4141. {
  4142. eax_validate_source_exclusion(all.lExclusion);
  4143. eax_validate_source_exclusion_lf_ratio(all.flExclusionLFRatio);
  4144. }
  4145. void ALsource::eax_validate_source_occlusion_all(
  4146. const EAXOCCLUSIONPROPERTIES& all)
  4147. {
  4148. eax_validate_source_occlusion(all.lOcclusion);
  4149. eax_validate_source_occlusion_lf_ratio(all.flOcclusionLFRatio);
  4150. eax_validate_source_occlusion_room_ratio(all.flOcclusionRoomRatio);
  4151. eax_validate_source_occlusion_direct_ratio(all.flOcclusionDirectRatio);
  4152. }
  4153. void ALsource::eax_validate_source_all(
  4154. const EAX20BUFFERPROPERTIES& all,
  4155. int eax_version)
  4156. {
  4157. eax_validate_source_direct(all.lDirect);
  4158. eax_validate_source_direct_hf(all.lDirectHF);
  4159. eax_validate_source_room(all.lRoom);
  4160. eax_validate_source_room_hf(all.lRoomHF);
  4161. eax_validate_source_obstruction(all.lObstruction);
  4162. eax_validate_source_obstruction_lf_ratio(all.flObstructionLFRatio);
  4163. eax_validate_source_occlusion(all.lOcclusion);
  4164. eax_validate_source_occlusion_lf_ratio(all.flOcclusionLFRatio);
  4165. eax_validate_source_occlusion_room_ratio(all.flOcclusionRoomRatio);
  4166. eax_validate_source_outside_volume_hf(all.lOutsideVolumeHF);
  4167. eax_validate_source_room_rolloff_factor(all.flRoomRolloffFactor);
  4168. eax_validate_source_air_absorption_factor(all.flAirAbsorptionFactor);
  4169. eax_validate_source_flags(all.dwFlags, eax_version);
  4170. }
  4171. void ALsource::eax_validate_source_all(
  4172. const EAX30SOURCEPROPERTIES& all,
  4173. int eax_version)
  4174. {
  4175. eax_validate_source_direct(all.lDirect);
  4176. eax_validate_source_direct_hf(all.lDirectHF);
  4177. eax_validate_source_room(all.lRoom);
  4178. eax_validate_source_room_hf(all.lRoomHF);
  4179. eax_validate_source_obstruction(all.lObstruction);
  4180. eax_validate_source_obstruction_lf_ratio(all.flObstructionLFRatio);
  4181. eax_validate_source_occlusion(all.lOcclusion);
  4182. eax_validate_source_occlusion_lf_ratio(all.flOcclusionLFRatio);
  4183. eax_validate_source_occlusion_room_ratio(all.flOcclusionRoomRatio);
  4184. eax_validate_source_occlusion_direct_ratio(all.flOcclusionDirectRatio);
  4185. eax_validate_source_exclusion(all.lExclusion);
  4186. eax_validate_source_exclusion_lf_ratio(all.flExclusionLFRatio);
  4187. eax_validate_source_outside_volume_hf(all.lOutsideVolumeHF);
  4188. eax_validate_source_doppler_factor(all.flDopplerFactor);
  4189. eax_validate_source_rolloff_factor(all.flRolloffFactor);
  4190. eax_validate_source_room_rolloff_factor(all.flRoomRolloffFactor);
  4191. eax_validate_source_air_absorption_factor(all.flAirAbsorptionFactor);
  4192. eax_validate_source_flags(all.ulFlags, eax_version);
  4193. }
  4194. void ALsource::eax_validate_source_all(
  4195. const EAX50SOURCEPROPERTIES& all,
  4196. int eax_version)
  4197. {
  4198. eax_validate_source_all(static_cast<EAX30SOURCEPROPERTIES>(all), eax_version);
  4199. eax_validate_source_macro_fx_factor(all.flMacroFXFactor);
  4200. }
  4201. void ALsource::eax_validate_source_speaker_id(
  4202. long speaker_id)
  4203. {
  4204. eax_validate_range<EaxSourceException>(
  4205. "Speaker Id",
  4206. speaker_id,
  4207. static_cast<long>(EAXSPEAKER_FRONT_LEFT),
  4208. static_cast<long>(EAXSPEAKER_LOW_FREQUENCY));
  4209. }
  4210. void ALsource::eax_validate_source_speaker_level(
  4211. long speaker_level)
  4212. {
  4213. eax_validate_range<EaxSourceException>(
  4214. "Speaker Level",
  4215. speaker_level,
  4216. EAXSOURCE_MINSPEAKERLEVEL,
  4217. EAXSOURCE_MAXSPEAKERLEVEL);
  4218. }
  4219. void ALsource::eax_validate_source_speaker_level_all(
  4220. const EAXSPEAKERLEVELPROPERTIES& all)
  4221. {
  4222. eax_validate_source_speaker_id(all.lSpeakerID);
  4223. eax_validate_source_speaker_level(all.lLevel);
  4224. }
  4225. void ALsource::eax_defer_source_direct(
  4226. long lDirect)
  4227. {
  4228. eax_d_.source.lDirect = lDirect;
  4229. eax_source_dirty_filter_flags_.lDirect = (eax_.source.lDirect != eax_d_.source.lDirect);
  4230. }
  4231. void ALsource::eax_defer_source_direct_hf(
  4232. long lDirectHF)
  4233. {
  4234. eax_d_.source.lDirectHF = lDirectHF;
  4235. eax_source_dirty_filter_flags_.lDirectHF = (eax_.source.lDirectHF != eax_d_.source.lDirectHF);
  4236. }
  4237. void ALsource::eax_defer_source_room(
  4238. long lRoom)
  4239. {
  4240. eax_d_.source.lRoom = lRoom;
  4241. eax_source_dirty_filter_flags_.lRoom = (eax_.source.lRoom != eax_d_.source.lRoom);
  4242. }
  4243. void ALsource::eax_defer_source_room_hf(
  4244. long lRoomHF)
  4245. {
  4246. eax_d_.source.lRoomHF = lRoomHF;
  4247. eax_source_dirty_filter_flags_.lRoomHF = (eax_.source.lRoomHF != eax_d_.source.lRoomHF);
  4248. }
  4249. void ALsource::eax_defer_source_obstruction(
  4250. long lObstruction)
  4251. {
  4252. eax_d_.source.lObstruction = lObstruction;
  4253. eax_source_dirty_filter_flags_.lObstruction = (eax_.source.lObstruction != eax_d_.source.lObstruction);
  4254. }
  4255. void ALsource::eax_defer_source_obstruction_lf_ratio(
  4256. float flObstructionLFRatio)
  4257. {
  4258. eax_d_.source.flObstructionLFRatio = flObstructionLFRatio;
  4259. eax_source_dirty_filter_flags_.flObstructionLFRatio = (eax_.source.flObstructionLFRatio != eax_d_.source.flObstructionLFRatio);
  4260. }
  4261. void ALsource::eax_defer_source_occlusion(
  4262. long lOcclusion)
  4263. {
  4264. eax_d_.source.lOcclusion = lOcclusion;
  4265. eax_source_dirty_filter_flags_.lOcclusion = (eax_.source.lOcclusion != eax_d_.source.lOcclusion);
  4266. }
  4267. void ALsource::eax_defer_source_occlusion_lf_ratio(
  4268. float flOcclusionLFRatio)
  4269. {
  4270. eax_d_.source.flOcclusionLFRatio = flOcclusionLFRatio;
  4271. eax_source_dirty_filter_flags_.flOcclusionLFRatio = (eax_.source.flOcclusionLFRatio != eax_d_.source.flOcclusionLFRatio);
  4272. }
  4273. void ALsource::eax_defer_source_occlusion_room_ratio(
  4274. float flOcclusionRoomRatio)
  4275. {
  4276. eax_d_.source.flOcclusionRoomRatio = flOcclusionRoomRatio;
  4277. eax_source_dirty_filter_flags_.flOcclusionRoomRatio = (eax_.source.flOcclusionRoomRatio != eax_d_.source.flOcclusionRoomRatio);
  4278. }
  4279. void ALsource::eax_defer_source_occlusion_direct_ratio(
  4280. float flOcclusionDirectRatio)
  4281. {
  4282. eax_d_.source.flOcclusionDirectRatio = flOcclusionDirectRatio;
  4283. eax_source_dirty_filter_flags_.flOcclusionDirectRatio = (eax_.source.flOcclusionDirectRatio != eax_d_.source.flOcclusionDirectRatio);
  4284. }
  4285. void ALsource::eax_defer_source_exclusion(
  4286. long lExclusion)
  4287. {
  4288. eax_d_.source.lExclusion = lExclusion;
  4289. eax_source_dirty_filter_flags_.lExclusion = (eax_.source.lExclusion != eax_d_.source.lExclusion);
  4290. }
  4291. void ALsource::eax_defer_source_exclusion_lf_ratio(
  4292. float flExclusionLFRatio)
  4293. {
  4294. eax_d_.source.flExclusionLFRatio = flExclusionLFRatio;
  4295. eax_source_dirty_filter_flags_.flExclusionLFRatio = (eax_.source.flExclusionLFRatio != eax_d_.source.flExclusionLFRatio);
  4296. }
  4297. void ALsource::eax_defer_source_outside_volume_hf(
  4298. long lOutsideVolumeHF)
  4299. {
  4300. eax_d_.source.lOutsideVolumeHF = lOutsideVolumeHF;
  4301. eax_source_dirty_misc_flags_.lOutsideVolumeHF = (eax_.source.lOutsideVolumeHF != eax_d_.source.lOutsideVolumeHF);
  4302. }
  4303. void ALsource::eax_defer_source_doppler_factor(
  4304. float flDopplerFactor)
  4305. {
  4306. eax_d_.source.flDopplerFactor = flDopplerFactor;
  4307. eax_source_dirty_misc_flags_.flDopplerFactor = (eax_.source.flDopplerFactor != eax_d_.source.flDopplerFactor);
  4308. }
  4309. void ALsource::eax_defer_source_rolloff_factor(
  4310. float flRolloffFactor)
  4311. {
  4312. eax_d_.source.flRolloffFactor = flRolloffFactor;
  4313. eax_source_dirty_misc_flags_.flRolloffFactor = (eax_.source.flRolloffFactor != eax_d_.source.flRolloffFactor);
  4314. }
  4315. void ALsource::eax_defer_source_room_rolloff_factor(
  4316. float flRoomRolloffFactor)
  4317. {
  4318. eax_d_.source.flRoomRolloffFactor = flRoomRolloffFactor;
  4319. eax_source_dirty_misc_flags_.flRoomRolloffFactor = (eax_.source.flRoomRolloffFactor != eax_d_.source.flRoomRolloffFactor);
  4320. }
  4321. void ALsource::eax_defer_source_air_absorption_factor(
  4322. float flAirAbsorptionFactor)
  4323. {
  4324. eax_d_.source.flAirAbsorptionFactor = flAirAbsorptionFactor;
  4325. eax_source_dirty_misc_flags_.flAirAbsorptionFactor = (eax_.source.flAirAbsorptionFactor != eax_d_.source.flAirAbsorptionFactor);
  4326. }
  4327. void ALsource::eax_defer_source_flags(
  4328. unsigned long ulFlags)
  4329. {
  4330. eax_d_.source.ulFlags = ulFlags;
  4331. eax_source_dirty_misc_flags_.ulFlags = (eax_.source.ulFlags != eax_d_.source.ulFlags);
  4332. }
  4333. void ALsource::eax_defer_source_macro_fx_factor(
  4334. float flMacroFXFactor)
  4335. {
  4336. eax_d_.source.flMacroFXFactor = flMacroFXFactor;
  4337. eax_source_dirty_misc_flags_.flMacroFXFactor = (eax_.source.flMacroFXFactor != eax_d_.source.flMacroFXFactor);
  4338. }
  4339. void ALsource::eax_defer_source_2d_all(
  4340. const EAXSOURCE2DPROPERTIES& all)
  4341. {
  4342. eax_defer_source_direct(all.lDirect);
  4343. eax_defer_source_direct_hf(all.lDirectHF);
  4344. eax_defer_source_room(all.lRoom);
  4345. eax_defer_source_room_hf(all.lRoomHF);
  4346. eax_defer_source_flags(all.ulFlags);
  4347. }
  4348. void ALsource::eax_defer_source_obstruction_all(
  4349. const EAXOBSTRUCTIONPROPERTIES& all)
  4350. {
  4351. eax_defer_source_obstruction(all.lObstruction);
  4352. eax_defer_source_obstruction_lf_ratio(all.flObstructionLFRatio);
  4353. }
  4354. void ALsource::eax_defer_source_exclusion_all(
  4355. const EAXEXCLUSIONPROPERTIES& all)
  4356. {
  4357. eax_defer_source_exclusion(all.lExclusion);
  4358. eax_defer_source_exclusion_lf_ratio(all.flExclusionLFRatio);
  4359. }
  4360. void ALsource::eax_defer_source_occlusion_all(
  4361. const EAXOCCLUSIONPROPERTIES& all)
  4362. {
  4363. eax_defer_source_occlusion(all.lOcclusion);
  4364. eax_defer_source_occlusion_lf_ratio(all.flOcclusionLFRatio);
  4365. eax_defer_source_occlusion_room_ratio(all.flOcclusionRoomRatio);
  4366. eax_defer_source_occlusion_direct_ratio(all.flOcclusionDirectRatio);
  4367. }
  4368. void ALsource::eax_defer_source_all(
  4369. const EAX20BUFFERPROPERTIES& all)
  4370. {
  4371. eax_defer_source_direct(all.lDirect);
  4372. eax_defer_source_direct_hf(all.lDirectHF);
  4373. eax_defer_source_room(all.lRoom);
  4374. eax_defer_source_room_hf(all.lRoomHF);
  4375. eax_defer_source_obstruction(all.lObstruction);
  4376. eax_defer_source_obstruction_lf_ratio(all.flObstructionLFRatio);
  4377. eax_defer_source_occlusion(all.lOcclusion);
  4378. eax_defer_source_occlusion_lf_ratio(all.flOcclusionLFRatio);
  4379. eax_defer_source_occlusion_room_ratio(all.flOcclusionRoomRatio);
  4380. eax_defer_source_outside_volume_hf(all.lOutsideVolumeHF);
  4381. eax_defer_source_room_rolloff_factor(all.flRoomRolloffFactor);
  4382. eax_defer_source_air_absorption_factor(all.flAirAbsorptionFactor);
  4383. eax_defer_source_flags(all.dwFlags);
  4384. }
  4385. void ALsource::eax_defer_source_all(
  4386. const EAX30SOURCEPROPERTIES& all)
  4387. {
  4388. eax_defer_source_direct(all.lDirect);
  4389. eax_defer_source_direct_hf(all.lDirectHF);
  4390. eax_defer_source_room(all.lRoom);
  4391. eax_defer_source_room_hf(all.lRoomHF);
  4392. eax_defer_source_obstruction(all.lObstruction);
  4393. eax_defer_source_obstruction_lf_ratio(all.flObstructionLFRatio);
  4394. eax_defer_source_occlusion(all.lOcclusion);
  4395. eax_defer_source_occlusion_lf_ratio(all.flOcclusionLFRatio);
  4396. eax_defer_source_occlusion_room_ratio(all.flOcclusionRoomRatio);
  4397. eax_defer_source_occlusion_direct_ratio(all.flOcclusionDirectRatio);
  4398. eax_defer_source_exclusion(all.lExclusion);
  4399. eax_defer_source_exclusion_lf_ratio(all.flExclusionLFRatio);
  4400. eax_defer_source_outside_volume_hf(all.lOutsideVolumeHF);
  4401. eax_defer_source_doppler_factor(all.flDopplerFactor);
  4402. eax_defer_source_rolloff_factor(all.flRolloffFactor);
  4403. eax_defer_source_room_rolloff_factor(all.flRoomRolloffFactor);
  4404. eax_defer_source_air_absorption_factor(all.flAirAbsorptionFactor);
  4405. eax_defer_source_flags(all.ulFlags);
  4406. }
  4407. void ALsource::eax_defer_source_all(
  4408. const EAX50SOURCEPROPERTIES& all)
  4409. {
  4410. eax_defer_source_all(static_cast<const EAX30SOURCEPROPERTIES&>(all));
  4411. eax_defer_source_macro_fx_factor(all.flMacroFXFactor);
  4412. }
  4413. void ALsource::eax_defer_source_speaker_level_all(
  4414. const EAXSPEAKERLEVELPROPERTIES& all)
  4415. {
  4416. const auto speaker_index = static_cast<std::size_t>(all.lSpeakerID - 1);
  4417. auto& speaker_level_d = eax_d_.speaker_levels[speaker_index];
  4418. const auto& speaker_level = eax_.speaker_levels[speaker_index];
  4419. if (speaker_level != speaker_level_d)
  4420. {
  4421. eax_source_dirty_misc_flags_.speaker_levels = true;
  4422. }
  4423. }
  4424. void ALsource::eax1_set_efx()
  4425. {
  4426. const auto primary_fx_slot_index = eax_al_context_->eax_get_primary_fx_slot_index();
  4427. if (!primary_fx_slot_index.has_value())
  4428. return;
  4429. WetGainAuto = (eax1_.fMix == EAX_REVERBMIX_USEDISTANCE);
  4430. const auto filter_gain = (WetGainAuto ? 1.0F : eax1_.fMix);
  4431. auto& fx_slot = eax_al_context_->eax_get_fx_slot(*primary_fx_slot_index);
  4432. eax_set_al_source_send(&fx_slot, *primary_fx_slot_index, EaxAlLowPassParam{filter_gain, 1.0F});
  4433. mPropsDirty = true;
  4434. }
  4435. void ALsource::eax1_set_reverb_mix(const EaxEaxCall& eax_call)
  4436. {
  4437. const auto reverb_mix = eax_call.get_value<EaxSourceException, const decltype(EAXBUFFER_REVERBPROPERTIES::fMix)>();
  4438. eax1_validate_reverb_mix(reverb_mix);
  4439. if (eax1_.fMix == reverb_mix)
  4440. return;
  4441. eax1_.fMix = reverb_mix;
  4442. eax1_set_efx();
  4443. }
  4444. void ALsource::eax_defer_source_direct(
  4445. const EaxEaxCall& eax_call)
  4446. {
  4447. const auto direct =
  4448. eax_call.get_value<EaxSourceException, const decltype(EAX30SOURCEPROPERTIES::lDirect)>();
  4449. eax_validate_source_direct(direct);
  4450. eax_defer_source_direct(direct);
  4451. }
  4452. void ALsource::eax_defer_source_direct_hf(
  4453. const EaxEaxCall& eax_call)
  4454. {
  4455. const auto direct_hf =
  4456. eax_call.get_value<EaxSourceException, const decltype(EAX30SOURCEPROPERTIES::lDirectHF)>();
  4457. eax_validate_source_direct_hf(direct_hf);
  4458. eax_defer_source_direct_hf(direct_hf);
  4459. }
  4460. void ALsource::eax_defer_source_room(
  4461. const EaxEaxCall& eax_call)
  4462. {
  4463. const auto room =
  4464. eax_call.get_value<EaxSourceException, const decltype(EAX30SOURCEPROPERTIES::lRoom)>();
  4465. eax_validate_source_room(room);
  4466. eax_defer_source_room(room);
  4467. }
  4468. void ALsource::eax_defer_source_room_hf(
  4469. const EaxEaxCall& eax_call)
  4470. {
  4471. const auto room_hf =
  4472. eax_call.get_value<EaxSourceException, const decltype(EAX30SOURCEPROPERTIES::lRoomHF)>();
  4473. eax_validate_source_room_hf(room_hf);
  4474. eax_defer_source_room_hf(room_hf);
  4475. }
  4476. void ALsource::eax_defer_source_obstruction(
  4477. const EaxEaxCall& eax_call)
  4478. {
  4479. const auto obstruction =
  4480. eax_call.get_value<EaxSourceException, const decltype(EAX30SOURCEPROPERTIES::lObstruction)>();
  4481. eax_validate_source_obstruction(obstruction);
  4482. eax_defer_source_obstruction(obstruction);
  4483. }
  4484. void ALsource::eax_defer_source_obstruction_lf_ratio(
  4485. const EaxEaxCall& eax_call)
  4486. {
  4487. const auto obstruction_lf_ratio =
  4488. eax_call.get_value<EaxSourceException, const decltype(EAX30SOURCEPROPERTIES::flObstructionLFRatio)>();
  4489. eax_validate_source_obstruction_lf_ratio(obstruction_lf_ratio);
  4490. eax_defer_source_obstruction_lf_ratio(obstruction_lf_ratio);
  4491. }
  4492. void ALsource::eax_defer_source_occlusion(
  4493. const EaxEaxCall& eax_call)
  4494. {
  4495. const auto occlusion =
  4496. eax_call.get_value<EaxSourceException, const decltype(EAX30SOURCEPROPERTIES::lOcclusion)>();
  4497. eax_validate_source_occlusion(occlusion);
  4498. eax_defer_source_occlusion(occlusion);
  4499. }
  4500. void ALsource::eax_defer_source_occlusion_lf_ratio(
  4501. const EaxEaxCall& eax_call)
  4502. {
  4503. const auto occlusion_lf_ratio =
  4504. eax_call.get_value<EaxSourceException, const decltype(EAX30SOURCEPROPERTIES::flOcclusionLFRatio)>();
  4505. eax_validate_source_occlusion_lf_ratio(occlusion_lf_ratio);
  4506. eax_defer_source_occlusion_lf_ratio(occlusion_lf_ratio);
  4507. }
  4508. void ALsource::eax_defer_source_occlusion_room_ratio(
  4509. const EaxEaxCall& eax_call)
  4510. {
  4511. const auto occlusion_room_ratio =
  4512. eax_call.get_value<EaxSourceException, const decltype(EAX30SOURCEPROPERTIES::flOcclusionRoomRatio)>();
  4513. eax_validate_source_occlusion_room_ratio(occlusion_room_ratio);
  4514. eax_defer_source_occlusion_room_ratio(occlusion_room_ratio);
  4515. }
  4516. void ALsource::eax_defer_source_occlusion_direct_ratio(
  4517. const EaxEaxCall& eax_call)
  4518. {
  4519. const auto occlusion_direct_ratio =
  4520. eax_call.get_value<EaxSourceException, const decltype(EAX30SOURCEPROPERTIES::flOcclusionDirectRatio)>();
  4521. eax_validate_source_occlusion_direct_ratio(occlusion_direct_ratio);
  4522. eax_defer_source_occlusion_direct_ratio(occlusion_direct_ratio);
  4523. }
  4524. void ALsource::eax_defer_source_exclusion(
  4525. const EaxEaxCall& eax_call)
  4526. {
  4527. const auto exclusion =
  4528. eax_call.get_value<EaxSourceException, const decltype(EAX30SOURCEPROPERTIES::lExclusion)>();
  4529. eax_validate_source_exclusion(exclusion);
  4530. eax_defer_source_exclusion(exclusion);
  4531. }
  4532. void ALsource::eax_defer_source_exclusion_lf_ratio(
  4533. const EaxEaxCall& eax_call)
  4534. {
  4535. const auto exclusion_lf_ratio =
  4536. eax_call.get_value<EaxSourceException, const decltype(EAX30SOURCEPROPERTIES::flExclusionLFRatio)>();
  4537. eax_validate_source_exclusion_lf_ratio(exclusion_lf_ratio);
  4538. eax_defer_source_exclusion_lf_ratio(exclusion_lf_ratio);
  4539. }
  4540. void ALsource::eax_defer_source_outside_volume_hf(
  4541. const EaxEaxCall& eax_call)
  4542. {
  4543. const auto outside_volume_hf =
  4544. eax_call.get_value<EaxSourceException, const decltype(EAX30SOURCEPROPERTIES::lOutsideVolumeHF)>();
  4545. eax_validate_source_outside_volume_hf(outside_volume_hf);
  4546. eax_defer_source_outside_volume_hf(outside_volume_hf);
  4547. }
  4548. void ALsource::eax_defer_source_doppler_factor(
  4549. const EaxEaxCall& eax_call)
  4550. {
  4551. const auto doppler_factor =
  4552. eax_call.get_value<EaxSourceException, const decltype(EAX30SOURCEPROPERTIES::flDopplerFactor)>();
  4553. eax_validate_source_doppler_factor(doppler_factor);
  4554. eax_defer_source_doppler_factor(doppler_factor);
  4555. }
  4556. void ALsource::eax_defer_source_rolloff_factor(
  4557. const EaxEaxCall& eax_call)
  4558. {
  4559. const auto rolloff_factor =
  4560. eax_call.get_value<EaxSourceException, const decltype(EAX30SOURCEPROPERTIES::flRolloffFactor)>();
  4561. eax_validate_source_rolloff_factor(rolloff_factor);
  4562. eax_defer_source_rolloff_factor(rolloff_factor);
  4563. }
  4564. void ALsource::eax_defer_source_room_rolloff_factor(
  4565. const EaxEaxCall& eax_call)
  4566. {
  4567. const auto room_rolloff_factor =
  4568. eax_call.get_value<EaxSourceException, const decltype(EAX30SOURCEPROPERTIES::flRoomRolloffFactor)>();
  4569. eax_validate_source_room_rolloff_factor(room_rolloff_factor);
  4570. eax_defer_source_room_rolloff_factor(room_rolloff_factor);
  4571. }
  4572. void ALsource::eax_defer_source_air_absorption_factor(
  4573. const EaxEaxCall& eax_call)
  4574. {
  4575. const auto air_absorption_factor =
  4576. eax_call.get_value<EaxSourceException, const decltype(EAX30SOURCEPROPERTIES::flAirAbsorptionFactor)>();
  4577. eax_validate_source_air_absorption_factor(air_absorption_factor);
  4578. eax_defer_source_air_absorption_factor(air_absorption_factor);
  4579. }
  4580. void ALsource::eax_defer_source_flags(
  4581. const EaxEaxCall& eax_call)
  4582. {
  4583. const auto flags =
  4584. eax_call.get_value<EaxSourceException, const decltype(EAX30SOURCEPROPERTIES::ulFlags)>();
  4585. eax_validate_source_flags(flags, eax_call.get_version());
  4586. eax_defer_source_flags(flags);
  4587. }
  4588. void ALsource::eax_defer_source_macro_fx_factor(
  4589. const EaxEaxCall& eax_call)
  4590. {
  4591. const auto macro_fx_factor =
  4592. eax_call.get_value<EaxSourceException, const decltype(EAX50SOURCEPROPERTIES::flMacroFXFactor)>();
  4593. eax_validate_source_macro_fx_factor(macro_fx_factor);
  4594. eax_defer_source_macro_fx_factor(macro_fx_factor);
  4595. }
  4596. void ALsource::eax_defer_source_2d_all(
  4597. const EaxEaxCall& eax_call)
  4598. {
  4599. const auto all = eax_call.get_value<EaxSourceException, const EAXSOURCE2DPROPERTIES>();
  4600. eax_validate_source_2d_all(all, eax_call.get_version());
  4601. eax_defer_source_2d_all(all);
  4602. }
  4603. void ALsource::eax_defer_source_obstruction_all(
  4604. const EaxEaxCall& eax_call)
  4605. {
  4606. const auto all = eax_call.get_value<EaxSourceException, const EAXOBSTRUCTIONPROPERTIES>();
  4607. eax_validate_source_obstruction_all(all);
  4608. eax_defer_source_obstruction_all(all);
  4609. }
  4610. void ALsource::eax_defer_source_exclusion_all(
  4611. const EaxEaxCall& eax_call)
  4612. {
  4613. const auto all = eax_call.get_value<EaxSourceException, const EAXEXCLUSIONPROPERTIES>();
  4614. eax_validate_source_exclusion_all(all);
  4615. eax_defer_source_exclusion_all(all);
  4616. }
  4617. void ALsource::eax_defer_source_occlusion_all(
  4618. const EaxEaxCall& eax_call)
  4619. {
  4620. const auto all = eax_call.get_value<EaxSourceException, const EAXOCCLUSIONPROPERTIES>();
  4621. eax_validate_source_occlusion_all(all);
  4622. eax_defer_source_occlusion_all(all);
  4623. }
  4624. void ALsource::eax_defer_source_all(
  4625. const EaxEaxCall& eax_call)
  4626. {
  4627. const auto eax_version = eax_call.get_version();
  4628. if (eax_version == 2)
  4629. {
  4630. const auto all = eax_call.get_value<EaxSourceException, const EAX20BUFFERPROPERTIES>();
  4631. eax_validate_source_all(all, eax_version);
  4632. eax_defer_source_all(all);
  4633. }
  4634. else if (eax_version < 5)
  4635. {
  4636. const auto all = eax_call.get_value<EaxSourceException, const EAX30SOURCEPROPERTIES>();
  4637. eax_validate_source_all(all, eax_version);
  4638. eax_defer_source_all(all);
  4639. }
  4640. else
  4641. {
  4642. const auto all = eax_call.get_value<EaxSourceException, const EAX50SOURCEPROPERTIES>();
  4643. eax_validate_source_all(all, eax_version);
  4644. eax_defer_source_all(all);
  4645. }
  4646. }
  4647. void ALsource::eax_defer_source_speaker_level_all(
  4648. const EaxEaxCall& eax_call)
  4649. {
  4650. const auto speaker_level_properties = eax_call.get_value<EaxSourceException, const EAXSPEAKERLEVELPROPERTIES>();
  4651. eax_validate_source_speaker_level_all(speaker_level_properties);
  4652. eax_defer_source_speaker_level_all(speaker_level_properties);
  4653. }
  4654. void ALsource::eax_set_outside_volume_hf()
  4655. {
  4656. const auto efx_gain_hf = clamp(
  4657. level_mb_to_gain(static_cast<float>(eax_.source.lOutsideVolumeHF)),
  4658. AL_MIN_CONE_OUTER_GAINHF,
  4659. AL_MAX_CONE_OUTER_GAINHF
  4660. );
  4661. OuterGainHF = efx_gain_hf;
  4662. }
  4663. void ALsource::eax_set_doppler_factor()
  4664. {
  4665. DopplerFactor = eax_.source.flDopplerFactor;
  4666. }
  4667. void ALsource::eax_set_rolloff_factor()
  4668. {
  4669. RolloffFactor2 = eax_.source.flRolloffFactor;
  4670. }
  4671. void ALsource::eax_set_room_rolloff_factor()
  4672. {
  4673. RoomRolloffFactor = eax_.source.flRoomRolloffFactor;
  4674. }
  4675. void ALsource::eax_set_air_absorption_factor()
  4676. {
  4677. AirAbsorptionFactor = eax_.source.flAirAbsorptionFactor;
  4678. }
  4679. void ALsource::eax_set_direct_hf_auto_flag()
  4680. {
  4681. const auto is_enable = (eax_.source.ulFlags & EAXSOURCEFLAGS_DIRECTHFAUTO) != 0;
  4682. DryGainHFAuto = is_enable;
  4683. }
  4684. void ALsource::eax_set_room_auto_flag()
  4685. {
  4686. const auto is_enable = (eax_.source.ulFlags & EAXSOURCEFLAGS_ROOMAUTO) != 0;
  4687. WetGainAuto = is_enable;
  4688. }
  4689. void ALsource::eax_set_room_hf_auto_flag()
  4690. {
  4691. const auto is_enable = (eax_.source.ulFlags & EAXSOURCEFLAGS_ROOMHFAUTO) != 0;
  4692. WetGainHFAuto = is_enable;
  4693. }
  4694. void ALsource::eax_set_flags()
  4695. {
  4696. eax_set_direct_hf_auto_flag();
  4697. eax_set_room_auto_flag();
  4698. eax_set_room_hf_auto_flag();
  4699. eax_set_speaker_levels();
  4700. }
  4701. void ALsource::eax_set_macro_fx_factor()
  4702. {
  4703. // TODO
  4704. }
  4705. void ALsource::eax_set_speaker_levels()
  4706. {
  4707. // TODO
  4708. }
  4709. void ALsource::eax1_set(const EaxEaxCall& eax_call)
  4710. {
  4711. switch (eax_call.get_property_id())
  4712. {
  4713. case DSPROPERTY_EAXBUFFER_ALL:
  4714. case DSPROPERTY_EAXBUFFER_REVERBMIX:
  4715. eax1_set_reverb_mix(eax_call);
  4716. break;
  4717. default:
  4718. eax_fail("Unsupported property id.");
  4719. }
  4720. }
  4721. void ALsource::eax_apply_deferred()
  4722. {
  4723. if (!eax_are_active_fx_slots_dirty_ &&
  4724. eax_sends_dirty_flags_ == EaxSourceSendsDirtyFlags{} &&
  4725. eax_source_dirty_filter_flags_ == EaxSourceSourceFilterDirtyFlags{} &&
  4726. eax_source_dirty_misc_flags_ == EaxSourceSourceMiscDirtyFlags{})
  4727. {
  4728. return;
  4729. }
  4730. eax_ = eax_d_;
  4731. if (eax_are_active_fx_slots_dirty_)
  4732. {
  4733. eax_are_active_fx_slots_dirty_ = false;
  4734. eax_set_fx_slots();
  4735. eax_update_filters_internal();
  4736. }
  4737. else if (eax_has_active_fx_slots_)
  4738. {
  4739. if (eax_source_dirty_filter_flags_ != EaxSourceSourceFilterDirtyFlags{})
  4740. {
  4741. eax_update_filters_internal();
  4742. }
  4743. else if (eax_sends_dirty_flags_ != EaxSourceSendsDirtyFlags{})
  4744. {
  4745. for (auto i = std::size_t{}; i < EAX_MAX_FXSLOTS; ++i)
  4746. {
  4747. if (eax_active_fx_slots_[i])
  4748. {
  4749. if (eax_sends_dirty_flags_.sends[i] != EaxSourceSendDirtyFlags{})
  4750. {
  4751. eax_update_filters_internal();
  4752. break;
  4753. }
  4754. }
  4755. }
  4756. }
  4757. }
  4758. if (eax_source_dirty_misc_flags_ != EaxSourceSourceMiscDirtyFlags{})
  4759. {
  4760. if (eax_source_dirty_misc_flags_.lOutsideVolumeHF)
  4761. {
  4762. eax_set_outside_volume_hf();
  4763. }
  4764. if (eax_source_dirty_misc_flags_.flDopplerFactor)
  4765. {
  4766. eax_set_doppler_factor();
  4767. }
  4768. if (eax_source_dirty_misc_flags_.flRolloffFactor)
  4769. {
  4770. eax_set_rolloff_factor();
  4771. }
  4772. if (eax_source_dirty_misc_flags_.flRoomRolloffFactor)
  4773. {
  4774. eax_set_room_rolloff_factor();
  4775. }
  4776. if (eax_source_dirty_misc_flags_.flAirAbsorptionFactor)
  4777. {
  4778. eax_set_air_absorption_factor();
  4779. }
  4780. if (eax_source_dirty_misc_flags_.ulFlags)
  4781. {
  4782. eax_set_flags();
  4783. }
  4784. if (eax_source_dirty_misc_flags_.flMacroFXFactor)
  4785. {
  4786. eax_set_macro_fx_factor();
  4787. }
  4788. mPropsDirty = true;
  4789. eax_source_dirty_misc_flags_ = EaxSourceSourceMiscDirtyFlags{};
  4790. }
  4791. eax_sends_dirty_flags_ = EaxSourceSendsDirtyFlags{};
  4792. eax_source_dirty_filter_flags_ = EaxSourceSourceFilterDirtyFlags{};
  4793. }
  4794. void ALsource::eax_set(
  4795. const EaxEaxCall& eax_call)
  4796. {
  4797. if (eax_call.get_version() == 1)
  4798. {
  4799. eax1_set(eax_call);
  4800. return;
  4801. }
  4802. switch (eax_call.get_property_id())
  4803. {
  4804. case EAXSOURCE_NONE:
  4805. break;
  4806. case EAXSOURCE_ALLPARAMETERS:
  4807. eax_defer_source_all(eax_call);
  4808. break;
  4809. case EAXSOURCE_OBSTRUCTIONPARAMETERS:
  4810. eax_defer_source_obstruction_all(eax_call);
  4811. break;
  4812. case EAXSOURCE_OCCLUSIONPARAMETERS:
  4813. eax_defer_source_occlusion_all(eax_call);
  4814. break;
  4815. case EAXSOURCE_EXCLUSIONPARAMETERS:
  4816. eax_defer_source_exclusion_all(eax_call);
  4817. break;
  4818. case EAXSOURCE_DIRECT:
  4819. eax_defer_source_direct(eax_call);
  4820. break;
  4821. case EAXSOURCE_DIRECTHF:
  4822. eax_defer_source_direct_hf(eax_call);
  4823. break;
  4824. case EAXSOURCE_ROOM:
  4825. eax_defer_source_room(eax_call);
  4826. break;
  4827. case EAXSOURCE_ROOMHF:
  4828. eax_defer_source_room_hf(eax_call);
  4829. break;
  4830. case EAXSOURCE_OBSTRUCTION:
  4831. eax_defer_source_obstruction(eax_call);
  4832. break;
  4833. case EAXSOURCE_OBSTRUCTIONLFRATIO:
  4834. eax_defer_source_obstruction_lf_ratio(eax_call);
  4835. break;
  4836. case EAXSOURCE_OCCLUSION:
  4837. eax_defer_source_occlusion(eax_call);
  4838. break;
  4839. case EAXSOURCE_OCCLUSIONLFRATIO:
  4840. eax_defer_source_occlusion_lf_ratio(eax_call);
  4841. break;
  4842. case EAXSOURCE_OCCLUSIONROOMRATIO:
  4843. eax_defer_source_occlusion_room_ratio(eax_call);
  4844. break;
  4845. case EAXSOURCE_OCCLUSIONDIRECTRATIO:
  4846. eax_defer_source_occlusion_direct_ratio(eax_call);
  4847. break;
  4848. case EAXSOURCE_EXCLUSION:
  4849. eax_defer_source_exclusion(eax_call);
  4850. break;
  4851. case EAXSOURCE_EXCLUSIONLFRATIO:
  4852. eax_defer_source_exclusion_lf_ratio(eax_call);
  4853. break;
  4854. case EAXSOURCE_OUTSIDEVOLUMEHF:
  4855. eax_defer_source_outside_volume_hf(eax_call);
  4856. break;
  4857. case EAXSOURCE_DOPPLERFACTOR:
  4858. eax_defer_source_doppler_factor(eax_call);
  4859. break;
  4860. case EAXSOURCE_ROLLOFFFACTOR:
  4861. eax_defer_source_rolloff_factor(eax_call);
  4862. break;
  4863. case EAXSOURCE_ROOMROLLOFFFACTOR:
  4864. eax_defer_source_room_rolloff_factor(eax_call);
  4865. break;
  4866. case EAXSOURCE_AIRABSORPTIONFACTOR:
  4867. eax_defer_source_air_absorption_factor(eax_call);
  4868. break;
  4869. case EAXSOURCE_FLAGS:
  4870. eax_defer_source_flags(eax_call);
  4871. break;
  4872. case EAXSOURCE_SENDPARAMETERS:
  4873. eax_defer_send(eax_call);
  4874. break;
  4875. case EAXSOURCE_ALLSENDPARAMETERS:
  4876. eax_defer_send_all(eax_call);
  4877. break;
  4878. case EAXSOURCE_OCCLUSIONSENDPARAMETERS:
  4879. eax_defer_send_occlusion_all(eax_call);
  4880. break;
  4881. case EAXSOURCE_EXCLUSIONSENDPARAMETERS:
  4882. eax_defer_send_exclusion_all(eax_call);
  4883. break;
  4884. case EAXSOURCE_ACTIVEFXSLOTID:
  4885. eax_defer_active_fx_slots(eax_call);
  4886. break;
  4887. case EAXSOURCE_MACROFXFACTOR:
  4888. eax_defer_source_macro_fx_factor(eax_call);
  4889. break;
  4890. case EAXSOURCE_SPEAKERLEVELS:
  4891. eax_defer_source_speaker_level_all(eax_call);
  4892. break;
  4893. case EAXSOURCE_ALL2DPARAMETERS:
  4894. eax_defer_source_2d_all(eax_call);
  4895. break;
  4896. default:
  4897. eax_fail("Unsupported property id.");
  4898. }
  4899. }
  4900. const GUID& ALsource::eax_get_send_fx_slot_guid(
  4901. int eax_version,
  4902. EaxFxSlotIndexValue fx_slot_index)
  4903. {
  4904. switch (eax_version)
  4905. {
  4906. case 4:
  4907. switch (fx_slot_index)
  4908. {
  4909. case 0:
  4910. return EAXPROPERTYID_EAX40_FXSlot0;
  4911. case 1:
  4912. return EAXPROPERTYID_EAX40_FXSlot1;
  4913. case 2:
  4914. return EAXPROPERTYID_EAX40_FXSlot2;
  4915. case 3:
  4916. return EAXPROPERTYID_EAX40_FXSlot3;
  4917. default:
  4918. eax_fail("FX slot index out of range.");
  4919. }
  4920. case 5:
  4921. switch (fx_slot_index)
  4922. {
  4923. case 0:
  4924. return EAXPROPERTYID_EAX50_FXSlot0;
  4925. case 1:
  4926. return EAXPROPERTYID_EAX50_FXSlot1;
  4927. case 2:
  4928. return EAXPROPERTYID_EAX50_FXSlot2;
  4929. case 3:
  4930. return EAXPROPERTYID_EAX50_FXSlot3;
  4931. default:
  4932. eax_fail("FX slot index out of range.");
  4933. }
  4934. default:
  4935. eax_fail("Unsupported EAX version.");
  4936. }
  4937. }
  4938. void ALsource::eax_copy_send(
  4939. const EAXSOURCEALLSENDPROPERTIES& src_send,
  4940. EAXSOURCESENDPROPERTIES& dst_send)
  4941. {
  4942. dst_send.lSend = src_send.lSend;
  4943. dst_send.lSendHF = src_send.lSendHF;
  4944. }
  4945. void ALsource::eax_copy_send(
  4946. const EAXSOURCEALLSENDPROPERTIES& src_send,
  4947. EAXSOURCEALLSENDPROPERTIES& dst_send)
  4948. {
  4949. dst_send = src_send;
  4950. }
  4951. void ALsource::eax_copy_send(
  4952. const EAXSOURCEALLSENDPROPERTIES& src_send,
  4953. EAXSOURCEOCCLUSIONSENDPROPERTIES& dst_send)
  4954. {
  4955. dst_send.lOcclusion = src_send.lOcclusion;
  4956. dst_send.flOcclusionLFRatio = src_send.flOcclusionLFRatio;
  4957. dst_send.flOcclusionRoomRatio = src_send.flOcclusionRoomRatio;
  4958. dst_send.flOcclusionDirectRatio = src_send.flOcclusionDirectRatio;
  4959. }
  4960. void ALsource::eax_copy_send(
  4961. const EAXSOURCEALLSENDPROPERTIES& src_send,
  4962. EAXSOURCEEXCLUSIONSENDPROPERTIES& dst_send)
  4963. {
  4964. dst_send.lExclusion = src_send.lExclusion;
  4965. dst_send.flExclusionLFRatio = src_send.flExclusionLFRatio;
  4966. }
  4967. void ALsource::eax1_get(const EaxEaxCall& eax_call)
  4968. {
  4969. switch (eax_call.get_property_id())
  4970. {
  4971. case DSPROPERTY_EAXBUFFER_ALL:
  4972. case DSPROPERTY_EAXBUFFER_REVERBMIX:
  4973. eax_call.set_value<EaxSourceException>(eax1_);
  4974. break;
  4975. default:
  4976. eax_fail("Unsupported property id.");
  4977. }
  4978. }
  4979. void ALsource::eax_api_get_source_all_v2(
  4980. const EaxEaxCall& eax_call)
  4981. {
  4982. auto eax_2_all = EAX20BUFFERPROPERTIES{};
  4983. eax_2_all.lDirect = eax_.source.lDirect;
  4984. eax_2_all.lDirectHF = eax_.source.lDirectHF;
  4985. eax_2_all.lRoom = eax_.source.lRoom;
  4986. eax_2_all.lRoomHF = eax_.source.lRoomHF;
  4987. eax_2_all.flRoomRolloffFactor = eax_.source.flRoomRolloffFactor;
  4988. eax_2_all.lObstruction = eax_.source.lObstruction;
  4989. eax_2_all.flObstructionLFRatio = eax_.source.flObstructionLFRatio;
  4990. eax_2_all.lOcclusion = eax_.source.lOcclusion;
  4991. eax_2_all.flOcclusionLFRatio = eax_.source.flOcclusionLFRatio;
  4992. eax_2_all.flOcclusionRoomRatio = eax_.source.flOcclusionRoomRatio;
  4993. eax_2_all.lOutsideVolumeHF = eax_.source.lOutsideVolumeHF;
  4994. eax_2_all.flAirAbsorptionFactor = eax_.source.flAirAbsorptionFactor;
  4995. eax_2_all.dwFlags = eax_.source.ulFlags;
  4996. eax_call.set_value<EaxSourceException>(eax_2_all);
  4997. }
  4998. void ALsource::eax_api_get_source_all_v3(
  4999. const EaxEaxCall& eax_call)
  5000. {
  5001. eax_call.set_value<EaxSourceException>(static_cast<const EAX30SOURCEPROPERTIES&>(eax_.source));
  5002. }
  5003. void ALsource::eax_api_get_source_all_v5(
  5004. const EaxEaxCall& eax_call)
  5005. {
  5006. eax_call.set_value<EaxSourceException>(eax_.source);
  5007. }
  5008. void ALsource::eax_api_get_source_all(
  5009. const EaxEaxCall& eax_call)
  5010. {
  5011. switch (eax_call.get_version())
  5012. {
  5013. case 2:
  5014. eax_api_get_source_all_v2(eax_call);
  5015. break;
  5016. case 3:
  5017. case 4:
  5018. eax_api_get_source_all_v3(eax_call);
  5019. break;
  5020. case 5:
  5021. eax_api_get_source_all_v5(eax_call);
  5022. break;
  5023. default:
  5024. eax_fail("Unsupported EAX version.");
  5025. }
  5026. }
  5027. void ALsource::eax_api_get_source_all_obstruction(
  5028. const EaxEaxCall& eax_call)
  5029. {
  5030. auto eax_obstruction_all = EAXOBSTRUCTIONPROPERTIES{};
  5031. eax_obstruction_all.lObstruction = eax_.source.lObstruction;
  5032. eax_obstruction_all.flObstructionLFRatio = eax_.source.flObstructionLFRatio;
  5033. eax_call.set_value<EaxSourceException>(eax_obstruction_all);
  5034. }
  5035. void ALsource::eax_api_get_source_all_occlusion(
  5036. const EaxEaxCall& eax_call)
  5037. {
  5038. auto eax_occlusion_all = EAXOCCLUSIONPROPERTIES{};
  5039. eax_occlusion_all.lOcclusion = eax_.source.lOcclusion;
  5040. eax_occlusion_all.flOcclusionLFRatio = eax_.source.flOcclusionLFRatio;
  5041. eax_occlusion_all.flOcclusionRoomRatio = eax_.source.flOcclusionRoomRatio;
  5042. eax_occlusion_all.flOcclusionDirectRatio = eax_.source.flOcclusionDirectRatio;
  5043. eax_call.set_value<EaxSourceException>(eax_occlusion_all);
  5044. }
  5045. void ALsource::eax_api_get_source_all_exclusion(
  5046. const EaxEaxCall& eax_call)
  5047. {
  5048. auto eax_exclusion_all = EAXEXCLUSIONPROPERTIES{};
  5049. eax_exclusion_all.lExclusion = eax_.source.lExclusion;
  5050. eax_exclusion_all.flExclusionLFRatio = eax_.source.flExclusionLFRatio;
  5051. eax_call.set_value<EaxSourceException>(eax_exclusion_all);
  5052. }
  5053. void ALsource::eax_api_get_source_active_fx_slot_id(
  5054. const EaxEaxCall& eax_call)
  5055. {
  5056. switch (eax_call.get_version())
  5057. {
  5058. case 4:
  5059. {
  5060. const auto& active_fx_slots = reinterpret_cast<const EAX40ACTIVEFXSLOTS&>(eax_.active_fx_slots);
  5061. eax_call.set_value<EaxSourceException>(active_fx_slots);
  5062. }
  5063. break;
  5064. case 5:
  5065. {
  5066. const auto& active_fx_slots = reinterpret_cast<const EAX50ACTIVEFXSLOTS&>(eax_.active_fx_slots);
  5067. eax_call.set_value<EaxSourceException>(active_fx_slots);
  5068. }
  5069. break;
  5070. default:
  5071. eax_fail("Unsupported EAX version.");
  5072. }
  5073. }
  5074. void ALsource::eax_api_get_source_all_2d(
  5075. const EaxEaxCall& eax_call)
  5076. {
  5077. auto eax_2d_all = EAXSOURCE2DPROPERTIES{};
  5078. eax_2d_all.lDirect = eax_.source.lDirect;
  5079. eax_2d_all.lDirectHF = eax_.source.lDirectHF;
  5080. eax_2d_all.lRoom = eax_.source.lRoom;
  5081. eax_2d_all.lRoomHF = eax_.source.lRoomHF;
  5082. eax_2d_all.ulFlags = eax_.source.ulFlags;
  5083. eax_call.set_value<EaxSourceException>(eax_2d_all);
  5084. }
  5085. void ALsource::eax_api_get_source_speaker_level_all(
  5086. const EaxEaxCall& eax_call)
  5087. {
  5088. auto& all = eax_call.get_value<EaxSourceException, EAXSPEAKERLEVELPROPERTIES>();
  5089. eax_validate_source_speaker_id(all.lSpeakerID);
  5090. const auto speaker_index = static_cast<std::size_t>(all.lSpeakerID - 1);
  5091. all.lLevel = eax_.speaker_levels[speaker_index];
  5092. }
  5093. void ALsource::eax_get(
  5094. const EaxEaxCall& eax_call)
  5095. {
  5096. if (eax_call.get_version() == 1)
  5097. {
  5098. eax1_get(eax_call);
  5099. return;
  5100. }
  5101. switch (eax_call.get_property_id())
  5102. {
  5103. case EAXSOURCE_NONE:
  5104. break;
  5105. case EAXSOURCE_ALLPARAMETERS:
  5106. eax_api_get_source_all(eax_call);
  5107. break;
  5108. case EAXSOURCE_OBSTRUCTIONPARAMETERS:
  5109. eax_api_get_source_all_obstruction(eax_call);
  5110. break;
  5111. case EAXSOURCE_OCCLUSIONPARAMETERS:
  5112. eax_api_get_source_all_occlusion(eax_call);
  5113. break;
  5114. case EAXSOURCE_EXCLUSIONPARAMETERS:
  5115. eax_api_get_source_all_exclusion(eax_call);
  5116. break;
  5117. case EAXSOURCE_DIRECT:
  5118. eax_call.set_value<EaxSourceException>(eax_.source.lDirect);
  5119. break;
  5120. case EAXSOURCE_DIRECTHF:
  5121. eax_call.set_value<EaxSourceException>(eax_.source.lDirectHF);
  5122. break;
  5123. case EAXSOURCE_ROOM:
  5124. eax_call.set_value<EaxSourceException>(eax_.source.lRoom);
  5125. break;
  5126. case EAXSOURCE_ROOMHF:
  5127. eax_call.set_value<EaxSourceException>(eax_.source.lRoomHF);
  5128. break;
  5129. case EAXSOURCE_OBSTRUCTION:
  5130. eax_call.set_value<EaxSourceException>(eax_.source.lObstruction);
  5131. break;
  5132. case EAXSOURCE_OBSTRUCTIONLFRATIO:
  5133. eax_call.set_value<EaxSourceException>(eax_.source.flObstructionLFRatio);
  5134. break;
  5135. case EAXSOURCE_OCCLUSION:
  5136. eax_call.set_value<EaxSourceException>(eax_.source.lOcclusion);
  5137. break;
  5138. case EAXSOURCE_OCCLUSIONLFRATIO:
  5139. eax_call.set_value<EaxSourceException>(eax_.source.flOcclusionLFRatio);
  5140. break;
  5141. case EAXSOURCE_OCCLUSIONROOMRATIO:
  5142. eax_call.set_value<EaxSourceException>(eax_.source.flOcclusionRoomRatio);
  5143. break;
  5144. case EAXSOURCE_OCCLUSIONDIRECTRATIO:
  5145. eax_call.set_value<EaxSourceException>(eax_.source.flOcclusionDirectRatio);
  5146. break;
  5147. case EAXSOURCE_EXCLUSION:
  5148. eax_call.set_value<EaxSourceException>(eax_.source.lExclusion);
  5149. break;
  5150. case EAXSOURCE_EXCLUSIONLFRATIO:
  5151. eax_call.set_value<EaxSourceException>(eax_.source.flExclusionLFRatio);
  5152. break;
  5153. case EAXSOURCE_OUTSIDEVOLUMEHF:
  5154. eax_call.set_value<EaxSourceException>(eax_.source.lOutsideVolumeHF);
  5155. break;
  5156. case EAXSOURCE_DOPPLERFACTOR:
  5157. eax_call.set_value<EaxSourceException>(eax_.source.flDopplerFactor);
  5158. break;
  5159. case EAXSOURCE_ROLLOFFFACTOR:
  5160. eax_call.set_value<EaxSourceException>(eax_.source.flRolloffFactor);
  5161. break;
  5162. case EAXSOURCE_ROOMROLLOFFFACTOR:
  5163. eax_call.set_value<EaxSourceException>(eax_.source.flRoomRolloffFactor);
  5164. break;
  5165. case EAXSOURCE_AIRABSORPTIONFACTOR:
  5166. eax_call.set_value<EaxSourceException>(eax_.source.flAirAbsorptionFactor);
  5167. break;
  5168. case EAXSOURCE_FLAGS:
  5169. eax_call.set_value<EaxSourceException>(eax_.source.ulFlags);
  5170. break;
  5171. case EAXSOURCE_SENDPARAMETERS:
  5172. eax_api_get_send_properties<EaxSourceException, EAXSOURCESENDPROPERTIES>(eax_call);
  5173. break;
  5174. case EAXSOURCE_ALLSENDPARAMETERS:
  5175. eax_api_get_send_properties<EaxSourceException, EAXSOURCEALLSENDPROPERTIES>(eax_call);
  5176. break;
  5177. case EAXSOURCE_OCCLUSIONSENDPARAMETERS:
  5178. eax_api_get_send_properties<EaxSourceException, EAXSOURCEOCCLUSIONSENDPROPERTIES>(eax_call);
  5179. break;
  5180. case EAXSOURCE_EXCLUSIONSENDPARAMETERS:
  5181. eax_api_get_send_properties<EaxSourceException, EAXSOURCEEXCLUSIONSENDPROPERTIES>(eax_call);
  5182. break;
  5183. case EAXSOURCE_ACTIVEFXSLOTID:
  5184. eax_api_get_source_active_fx_slot_id(eax_call);
  5185. break;
  5186. case EAXSOURCE_MACROFXFACTOR:
  5187. eax_call.set_value<EaxSourceException>(eax_.source.flMacroFXFactor);
  5188. break;
  5189. case EAXSOURCE_SPEAKERLEVELS:
  5190. eax_api_get_source_speaker_level_all(eax_call);
  5191. break;
  5192. case EAXSOURCE_ALL2DPARAMETERS:
  5193. eax_api_get_source_all_2d(eax_call);
  5194. break;
  5195. default:
  5196. eax_fail("Unsupported property id.");
  5197. }
  5198. }
  5199. void ALsource::eax_set_al_source_send(
  5200. ALeffectslot *slot,
  5201. size_t sendidx,
  5202. const EaxAlLowPassParam &filter)
  5203. {
  5204. if(sendidx >= EAX_MAX_FXSLOTS)
  5205. return;
  5206. auto &send = Send[sendidx];
  5207. send.Gain = filter.gain;
  5208. send.GainHF = filter.gain_hf;
  5209. send.HFReference = LOWPASSFREQREF;
  5210. send.GainLF = 1.0f;
  5211. send.LFReference = HIGHPASSFREQREF;
  5212. if(slot) IncrementRef(slot->ref);
  5213. if(auto *oldslot = send.Slot)
  5214. DecrementRef(oldslot->ref);
  5215. send.Slot = slot;
  5216. mPropsDirty = true;
  5217. }
  5218. #endif // ALSOFT_EAX