🛠️🐜 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.

4944 lines
158 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 <cassert>
  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_and_update();
  3191. }
  3192. }
  3193. }
  3194. else
  3195. #endif
  3196. {
  3197. auto voicelist = context->getVoicesSpan();
  3198. ALuint vidx{0u};
  3199. for(Voice *voice : voicelist)
  3200. {
  3201. ALuint sid{voice->mSourceID.load(std::memory_order_acquire)};
  3202. ALsource *source = sid ? LookupSource(context, sid) : nullptr;
  3203. if(source && source->VoiceIdx == vidx)
  3204. {
  3205. if(std::exchange(source->mPropsDirty, false))
  3206. UpdateSourceProps(source, voice, context);
  3207. }
  3208. ++vidx;
  3209. }
  3210. }
  3211. }
  3212. SourceSubList::~SourceSubList()
  3213. {
  3214. uint64_t usemask{~FreeMask};
  3215. while(usemask)
  3216. {
  3217. const int idx{al::countr_zero(usemask)};
  3218. usemask &= ~(1_u64 << idx);
  3219. al::destroy_at(Sources+idx);
  3220. }
  3221. FreeMask = ~usemask;
  3222. al_free(Sources);
  3223. Sources = nullptr;
  3224. }
  3225. #ifdef ALSOFT_EAX
  3226. void EaxUpdateSourceVoice(ALsource *source, ALCcontext *context)
  3227. {
  3228. if(Voice *voice{GetSourceVoice(source, context)})
  3229. {
  3230. if(std::exchange(source->mPropsDirty, false))
  3231. UpdateSourceProps(source, voice, context);
  3232. }
  3233. }
  3234. constexpr const ALsource::EaxFxSlotIds ALsource::eax4_fx_slot_ids;
  3235. constexpr const ALsource::EaxFxSlotIds ALsource::eax5_fx_slot_ids;
  3236. void ALsource::eax_initialize(ALCcontext *context) noexcept
  3237. {
  3238. assert(context != nullptr);
  3239. eax_al_context_ = context;
  3240. eax_primary_fx_slot_id_ = eax_al_context_->eax_get_primary_fx_slot_index();
  3241. eax_version_ = eax_al_context_->eax_get_version();
  3242. eax_set_defaults();
  3243. eax_commit(EaxCommitType::forced);
  3244. }
  3245. void ALsource::eax_dispatch(const EaxCall& call)
  3246. {
  3247. call.is_get() ? eax_get(call) : eax_set(call);
  3248. }
  3249. void ALsource::eax_commit_and_update()
  3250. {
  3251. eax_commit();
  3252. EaxUpdateSourceVoice(this, eax_al_context_);
  3253. }
  3254. ALsource* ALsource::eax_lookup_source(ALCcontext& al_context, ALuint source_id) noexcept
  3255. {
  3256. return LookupSource(&al_context, source_id);
  3257. }
  3258. [[noreturn]] void ALsource::eax_fail(const char* message)
  3259. {
  3260. throw Exception{message};
  3261. }
  3262. [[noreturn]] void ALsource::eax_fail_unknown_property_id()
  3263. {
  3264. eax_fail("Unknown property id.");
  3265. }
  3266. [[noreturn]] void ALsource::eax_fail_unknown_version()
  3267. {
  3268. eax_fail("Unknown version.");
  3269. }
  3270. [[noreturn]] void ALsource::eax_fail_unknown_active_fx_slot_id()
  3271. {
  3272. eax_fail("Unknown active FX slot ID.");
  3273. }
  3274. [[noreturn]] void ALsource::eax_fail_unknown_receiving_fx_slot_id()
  3275. {
  3276. eax_fail("Unknown receiving FX slot ID.");
  3277. }
  3278. void ALsource::eax_set_sends_defaults(EaxSends& sends, const EaxFxSlotIds& ids) noexcept
  3279. {
  3280. for (auto i = size_t{}; i < EAX_MAX_FXSLOTS; ++i) {
  3281. auto& send = sends[i];
  3282. send.guidReceivingFXSlotID = *(ids[i]);
  3283. send.lSend = EAXSOURCE_DEFAULTSEND;
  3284. send.lSendHF = EAXSOURCE_DEFAULTSENDHF;
  3285. send.lOcclusion = EAXSOURCE_DEFAULTOCCLUSION;
  3286. send.flOcclusionLFRatio = EAXSOURCE_DEFAULTOCCLUSIONLFRATIO;
  3287. send.flOcclusionRoomRatio = EAXSOURCE_DEFAULTOCCLUSIONROOMRATIO;
  3288. send.flOcclusionDirectRatio = EAXSOURCE_DEFAULTOCCLUSIONDIRECTRATIO;
  3289. send.lExclusion = EAXSOURCE_DEFAULTEXCLUSION;
  3290. send.flExclusionLFRatio = EAXSOURCE_DEFAULTEXCLUSIONLFRATIO;
  3291. }
  3292. }
  3293. void ALsource::eax1_set_defaults(Eax1Props& props) noexcept
  3294. {
  3295. props.fMix = EAX_REVERBMIX_USEDISTANCE;
  3296. }
  3297. void ALsource::eax1_set_defaults() noexcept
  3298. {
  3299. eax1_set_defaults(eax1_.i);
  3300. eax1_.d = eax1_.i;
  3301. }
  3302. void ALsource::eax2_set_defaults(Eax2Props& props) noexcept
  3303. {
  3304. props.lDirect = EAXSOURCE_DEFAULTDIRECT;
  3305. props.lDirectHF = EAXSOURCE_DEFAULTDIRECTHF;
  3306. props.lRoom = EAXSOURCE_DEFAULTROOM;
  3307. props.lRoomHF = EAXSOURCE_DEFAULTROOMHF;
  3308. props.flRoomRolloffFactor = EAXSOURCE_DEFAULTROOMROLLOFFFACTOR;
  3309. props.lObstruction = EAXSOURCE_DEFAULTOBSTRUCTION;
  3310. props.flObstructionLFRatio = EAXSOURCE_DEFAULTOBSTRUCTIONLFRATIO;
  3311. props.lOcclusion = EAXSOURCE_DEFAULTOCCLUSION;
  3312. props.flOcclusionLFRatio = EAXSOURCE_DEFAULTOCCLUSIONLFRATIO;
  3313. props.flOcclusionRoomRatio = EAXSOURCE_DEFAULTOCCLUSIONROOMRATIO;
  3314. props.lOutsideVolumeHF = EAXSOURCE_DEFAULTOUTSIDEVOLUMEHF;
  3315. props.flAirAbsorptionFactor = EAXSOURCE_DEFAULTAIRABSORPTIONFACTOR;
  3316. props.dwFlags = EAXSOURCE_DEFAULTFLAGS;
  3317. }
  3318. void ALsource::eax2_set_defaults() noexcept
  3319. {
  3320. eax2_set_defaults(eax2_.i);
  3321. eax2_.d = eax2_.i;
  3322. }
  3323. void ALsource::eax3_set_defaults(Eax3Props& props) noexcept
  3324. {
  3325. props.lDirect = EAXSOURCE_DEFAULTDIRECT;
  3326. props.lDirectHF = EAXSOURCE_DEFAULTDIRECTHF;
  3327. props.lRoom = EAXSOURCE_DEFAULTROOM;
  3328. props.lRoomHF = EAXSOURCE_DEFAULTROOMHF;
  3329. props.lObstruction = EAXSOURCE_DEFAULTOBSTRUCTION;
  3330. props.flObstructionLFRatio = EAXSOURCE_DEFAULTOBSTRUCTIONLFRATIO;
  3331. props.lOcclusion = EAXSOURCE_DEFAULTOCCLUSION;
  3332. props.flOcclusionLFRatio = EAXSOURCE_DEFAULTOCCLUSIONLFRATIO;
  3333. props.flOcclusionRoomRatio = EAXSOURCE_DEFAULTOCCLUSIONROOMRATIO;
  3334. props.flOcclusionDirectRatio = EAXSOURCE_DEFAULTOCCLUSIONDIRECTRATIO;
  3335. props.lExclusion = EAXSOURCE_DEFAULTEXCLUSION;
  3336. props.flExclusionLFRatio = EAXSOURCE_DEFAULTEXCLUSIONLFRATIO;
  3337. props.lOutsideVolumeHF = EAXSOURCE_DEFAULTOUTSIDEVOLUMEHF;
  3338. props.flDopplerFactor = EAXSOURCE_DEFAULTDOPPLERFACTOR;
  3339. props.flRolloffFactor = EAXSOURCE_DEFAULTROLLOFFFACTOR;
  3340. props.flRoomRolloffFactor = EAXSOURCE_DEFAULTROOMROLLOFFFACTOR;
  3341. props.flAirAbsorptionFactor = EAXSOURCE_DEFAULTAIRABSORPTIONFACTOR;
  3342. props.ulFlags = EAXSOURCE_DEFAULTFLAGS;
  3343. }
  3344. void ALsource::eax3_set_defaults() noexcept
  3345. {
  3346. eax3_set_defaults(eax3_.i);
  3347. eax3_.d = eax3_.i;
  3348. }
  3349. void ALsource::eax4_set_sends_defaults(EaxSends& sends) noexcept
  3350. {
  3351. eax_set_sends_defaults(sends, eax4_fx_slot_ids);
  3352. }
  3353. void ALsource::eax4_set_active_fx_slots_defaults(EAX40ACTIVEFXSLOTS& slots) noexcept
  3354. {
  3355. slots = EAX40SOURCE_DEFAULTACTIVEFXSLOTID;
  3356. }
  3357. void ALsource::eax4_set_defaults() noexcept
  3358. {
  3359. eax3_set_defaults(eax4_.i.source);
  3360. eax4_set_sends_defaults(eax4_.i.sends);
  3361. eax4_set_active_fx_slots_defaults(eax4_.i.active_fx_slots);
  3362. eax4_.d = eax4_.i;
  3363. }
  3364. void ALsource::eax5_set_source_defaults(EAX50SOURCEPROPERTIES& props) noexcept
  3365. {
  3366. eax3_set_defaults(static_cast<Eax3Props&>(props));
  3367. props.flMacroFXFactor = EAXSOURCE_DEFAULTMACROFXFACTOR;
  3368. }
  3369. void ALsource::eax5_set_sends_defaults(EaxSends& sends) noexcept
  3370. {
  3371. eax_set_sends_defaults(sends, eax5_fx_slot_ids);
  3372. }
  3373. void ALsource::eax5_set_active_fx_slots_defaults(EAX50ACTIVEFXSLOTS& slots) noexcept
  3374. {
  3375. slots = EAX50SOURCE_3DDEFAULTACTIVEFXSLOTID;
  3376. }
  3377. void ALsource::eax5_set_speaker_levels_defaults(EaxSpeakerLevels& speaker_levels) noexcept
  3378. {
  3379. for (auto i = size_t{}; i < eax_max_speakers; ++i) {
  3380. auto& speaker_level = speaker_levels[i];
  3381. speaker_level.lSpeakerID = static_cast<long>(EAXSPEAKER_FRONT_LEFT + i);
  3382. speaker_level.lLevel = EAXSOURCE_DEFAULTSPEAKERLEVEL;
  3383. }
  3384. }
  3385. void ALsource::eax5_set_defaults(Eax5Props& props) noexcept
  3386. {
  3387. eax5_set_source_defaults(props.source);
  3388. eax5_set_sends_defaults(props.sends);
  3389. eax5_set_active_fx_slots_defaults(props.active_fx_slots);
  3390. eax5_set_speaker_levels_defaults(props.speaker_levels);
  3391. }
  3392. void ALsource::eax5_set_defaults() noexcept
  3393. {
  3394. eax5_set_defaults(eax5_.i);
  3395. eax5_.d = eax5_.i;
  3396. }
  3397. void ALsource::eax_set_defaults() noexcept
  3398. {
  3399. eax1_set_defaults();
  3400. eax2_set_defaults();
  3401. eax3_set_defaults();
  3402. eax4_set_defaults();
  3403. eax5_set_defaults();
  3404. }
  3405. void ALsource::eax1_translate(const Eax1Props& src, Eax5Props& dst) noexcept
  3406. {
  3407. eax5_set_defaults(dst);
  3408. if (src.fMix == EAX_REVERBMIX_USEDISTANCE)
  3409. dst.source.ulFlags |= EAXSOURCEFLAGS_ROOMAUTO;
  3410. else
  3411. dst.source.ulFlags &= ~EAXSOURCEFLAGS_ROOMAUTO;
  3412. dst.sends[0].lSendHF = clamp(
  3413. static_cast<long>(gain_to_level_mb(src.fMix)),
  3414. EAXSOURCE_MINSENDHF,
  3415. EAXSOURCE_MAXSENDHF);
  3416. }
  3417. void ALsource::eax2_translate(const Eax2Props& src, Eax5Props& dst) noexcept
  3418. {
  3419. // Source.
  3420. //
  3421. dst.source.lDirect = src.lDirect;
  3422. dst.source.lDirectHF = src.lDirectHF;
  3423. dst.source.lRoom = src.lRoom;
  3424. dst.source.lRoomHF = src.lRoomHF;
  3425. dst.source.lObstruction = src.lObstruction;
  3426. dst.source.flObstructionLFRatio = src.flObstructionLFRatio;
  3427. dst.source.lOcclusion = src.lOcclusion;
  3428. dst.source.flOcclusionLFRatio = src.flOcclusionLFRatio;
  3429. dst.source.flOcclusionRoomRatio = src.flOcclusionRoomRatio;
  3430. dst.source.flOcclusionDirectRatio = EAXSOURCE_DEFAULTOCCLUSIONDIRECTRATIO;
  3431. dst.source.lExclusion = EAXSOURCE_DEFAULTEXCLUSION;
  3432. dst.source.flExclusionLFRatio = EAXSOURCE_DEFAULTEXCLUSIONLFRATIO;
  3433. dst.source.lOutsideVolumeHF = src.lOutsideVolumeHF;
  3434. dst.source.flDopplerFactor = EAXSOURCE_DEFAULTDOPPLERFACTOR;
  3435. dst.source.flRolloffFactor = EAXSOURCE_DEFAULTROLLOFFFACTOR;
  3436. dst.source.flRoomRolloffFactor = src.flRoomRolloffFactor;
  3437. dst.source.flAirAbsorptionFactor = src.flAirAbsorptionFactor;
  3438. dst.source.ulFlags = src.dwFlags;
  3439. dst.source.flMacroFXFactor = EAXSOURCE_DEFAULTMACROFXFACTOR;
  3440. // Set everyting else to defaults.
  3441. //
  3442. eax5_set_sends_defaults(dst.sends);
  3443. eax5_set_active_fx_slots_defaults(dst.active_fx_slots);
  3444. eax5_set_speaker_levels_defaults(dst.speaker_levels);
  3445. }
  3446. void ALsource::eax3_translate(const Eax3Props& src, Eax5Props& dst) noexcept
  3447. {
  3448. // Source.
  3449. //
  3450. static_cast<Eax3Props&>(dst.source) = src;
  3451. dst.source.flMacroFXFactor = EAXSOURCE_DEFAULTMACROFXFACTOR;
  3452. // Set everyting else to defaults.
  3453. //
  3454. eax5_set_sends_defaults(dst.sends);
  3455. eax5_set_active_fx_slots_defaults(dst.active_fx_slots);
  3456. eax5_set_speaker_levels_defaults(dst.speaker_levels);
  3457. }
  3458. void ALsource::eax4_translate(const Eax4Props& src, Eax5Props& dst) noexcept
  3459. {
  3460. // Source.
  3461. //
  3462. static_cast<Eax3Props&>(dst.source) = src.source;
  3463. dst.source.flMacroFXFactor = EAXSOURCE_DEFAULTMACROFXFACTOR;
  3464. // Sends.
  3465. //
  3466. dst.sends = src.sends;
  3467. for (auto i = size_t{}; i < EAX_MAX_FXSLOTS; ++i)
  3468. dst.sends[i].guidReceivingFXSlotID = *(eax5_fx_slot_ids[i]);
  3469. // Active FX slots.
  3470. //
  3471. for (auto i = 0; i < EAX50_MAX_ACTIVE_FXSLOTS; ++i) {
  3472. auto& dst_id = dst.active_fx_slots.guidActiveFXSlots[i];
  3473. if (i < EAX40_MAX_ACTIVE_FXSLOTS) {
  3474. const auto& src_id = src.active_fx_slots.guidActiveFXSlots[i];
  3475. if (src_id == EAX_NULL_GUID)
  3476. dst_id = EAX_NULL_GUID;
  3477. else if (src_id == EAX_PrimaryFXSlotID)
  3478. dst_id = EAX_PrimaryFXSlotID;
  3479. else if (src_id == EAXPROPERTYID_EAX40_FXSlot0)
  3480. dst_id = EAXPROPERTYID_EAX50_FXSlot0;
  3481. else if (src_id == EAXPROPERTYID_EAX40_FXSlot1)
  3482. dst_id = EAXPROPERTYID_EAX50_FXSlot1;
  3483. else if (src_id == EAXPROPERTYID_EAX40_FXSlot2)
  3484. dst_id = EAXPROPERTYID_EAX50_FXSlot2;
  3485. else if (src_id == EAXPROPERTYID_EAX40_FXSlot3)
  3486. dst_id = EAXPROPERTYID_EAX50_FXSlot3;
  3487. else
  3488. assert(false && "Unknown active FX slot ID.");
  3489. } else
  3490. dst_id = EAX_NULL_GUID;
  3491. }
  3492. // Speaker levels.
  3493. //
  3494. eax5_set_speaker_levels_defaults(dst.speaker_levels);
  3495. }
  3496. float ALsource::eax_calculate_dst_occlusion_mb(
  3497. long src_occlusion_mb,
  3498. float path_ratio,
  3499. float lf_ratio) noexcept
  3500. {
  3501. const auto ratio_1 = path_ratio + lf_ratio - 1.0F;
  3502. const auto ratio_2 = path_ratio * lf_ratio;
  3503. const auto ratio = (ratio_2 > ratio_1) ? ratio_2 : ratio_1;
  3504. const auto dst_occlustion_mb = static_cast<float>(src_occlusion_mb) * ratio;
  3505. return dst_occlustion_mb;
  3506. }
  3507. EaxAlLowPassParam ALsource::eax_create_direct_filter_param() const noexcept
  3508. {
  3509. auto gain_mb =
  3510. static_cast<float>(eax_.source.lDirect) +
  3511. (static_cast<float>(eax_.source.lObstruction) * eax_.source.flObstructionLFRatio) +
  3512. eax_calculate_dst_occlusion_mb(
  3513. eax_.source.lOcclusion,
  3514. eax_.source.flOcclusionDirectRatio,
  3515. eax_.source.flOcclusionLFRatio);
  3516. auto gain_hf_mb =
  3517. static_cast<float>(eax_.source.lDirectHF) +
  3518. static_cast<float>(eax_.source.lObstruction) +
  3519. (static_cast<float>(eax_.source.lOcclusion) * eax_.source.flOcclusionDirectRatio);
  3520. for (auto i = std::size_t{}; i < EAX_MAX_FXSLOTS; ++i)
  3521. {
  3522. if (!eax_active_fx_slots_[i])
  3523. {
  3524. continue;
  3525. }
  3526. const auto& send = eax_.sends[i];
  3527. gain_mb += eax_calculate_dst_occlusion_mb(
  3528. send.lOcclusion,
  3529. send.flOcclusionDirectRatio,
  3530. send.flOcclusionLFRatio);
  3531. gain_hf_mb += static_cast<float>(send.lOcclusion) * send.flOcclusionDirectRatio;
  3532. }
  3533. const auto al_low_pass_param = EaxAlLowPassParam{
  3534. level_mb_to_gain(gain_mb),
  3535. minf(level_mb_to_gain(gain_hf_mb), 1.0f)};
  3536. return al_low_pass_param;
  3537. }
  3538. EaxAlLowPassParam ALsource::eax_create_room_filter_param(
  3539. const ALeffectslot& fx_slot,
  3540. const EAXSOURCEALLSENDPROPERTIES& send) const noexcept
  3541. {
  3542. const auto& fx_slot_eax = fx_slot.eax_get_eax_fx_slot();
  3543. const auto gain_mb =
  3544. static_cast<float>(eax_.source.lRoom + send.lSend) +
  3545. eax_calculate_dst_occlusion_mb(
  3546. eax_.source.lOcclusion,
  3547. eax_.source.flOcclusionRoomRatio,
  3548. eax_.source.flOcclusionLFRatio) +
  3549. eax_calculate_dst_occlusion_mb(
  3550. send.lOcclusion,
  3551. send.flOcclusionRoomRatio,
  3552. send.flOcclusionLFRatio) +
  3553. (static_cast<float>(eax_.source.lExclusion) * eax_.source.flExclusionLFRatio) +
  3554. (static_cast<float>(send.lExclusion) * send.flExclusionLFRatio);
  3555. const auto gain_hf_mb =
  3556. static_cast<float>(eax_.source.lRoomHF + send.lSendHF) +
  3557. (static_cast<float>(fx_slot_eax.lOcclusion + eax_.source.lOcclusion) * eax_.source.flOcclusionRoomRatio) +
  3558. (static_cast<float>(send.lOcclusion) * send.flOcclusionRoomRatio) +
  3559. static_cast<float>(eax_.source.lExclusion + send.lExclusion);
  3560. const auto al_low_pass_param = EaxAlLowPassParam{
  3561. level_mb_to_gain(gain_mb),
  3562. minf(level_mb_to_gain(gain_hf_mb), 1.0f)};
  3563. return al_low_pass_param;
  3564. }
  3565. void ALsource::eax_update_direct_filter()
  3566. {
  3567. const auto& direct_param = eax_create_direct_filter_param();
  3568. Direct.Gain = direct_param.gain;
  3569. Direct.GainHF = direct_param.gain_hf;
  3570. Direct.HFReference = LOWPASSFREQREF;
  3571. Direct.GainLF = 1.0f;
  3572. Direct.LFReference = HIGHPASSFREQREF;
  3573. mPropsDirty = true;
  3574. }
  3575. void ALsource::eax_update_room_filters()
  3576. {
  3577. for (auto i = size_t{}; i < EAX_MAX_FXSLOTS; ++i) {
  3578. if (!eax_active_fx_slots_[i])
  3579. continue;
  3580. auto& fx_slot = eax_al_context_->eax_get_fx_slot(i);
  3581. const auto& send = eax_.sends[i];
  3582. const auto& room_param = eax_create_room_filter_param(fx_slot, send);
  3583. eax_set_al_source_send(&fx_slot, i, room_param);
  3584. }
  3585. }
  3586. void ALsource::eax_set_efx_outer_gain_hf()
  3587. {
  3588. OuterGainHF = clamp(
  3589. level_mb_to_gain(static_cast<float>(eax_.source.lOutsideVolumeHF)),
  3590. AL_MIN_CONE_OUTER_GAINHF,
  3591. AL_MAX_CONE_OUTER_GAINHF);
  3592. }
  3593. void ALsource::eax_set_efx_doppler_factor()
  3594. {
  3595. DopplerFactor = eax_.source.flDopplerFactor;
  3596. }
  3597. void ALsource::eax_set_efx_rolloff_factor()
  3598. {
  3599. RolloffFactor2 = eax_.source.flRolloffFactor;
  3600. }
  3601. void ALsource::eax_set_efx_room_rolloff_factor()
  3602. {
  3603. RoomRolloffFactor = eax_.source.flRoomRolloffFactor;
  3604. }
  3605. void ALsource::eax_set_efx_air_absorption_factor()
  3606. {
  3607. AirAbsorptionFactor = eax_.source.flAirAbsorptionFactor;
  3608. }
  3609. void ALsource::eax_set_efx_dry_gain_hf_auto()
  3610. {
  3611. DryGainHFAuto = ((eax_.source.ulFlags & EAXSOURCEFLAGS_DIRECTHFAUTO) != 0);
  3612. }
  3613. void ALsource::eax_set_efx_wet_gain_auto()
  3614. {
  3615. WetGainAuto = ((eax_.source.ulFlags & EAXSOURCEFLAGS_ROOMAUTO) != 0);
  3616. }
  3617. void ALsource::eax_set_efx_wet_gain_hf_auto()
  3618. {
  3619. WetGainHFAuto = ((eax_.source.ulFlags & EAXSOURCEFLAGS_ROOMHFAUTO) != 0);
  3620. }
  3621. void ALsource::eax1_set(const EaxCall& call, Eax1Props& props)
  3622. {
  3623. switch (call.get_property_id()) {
  3624. case DSPROPERTY_EAXBUFFER_ALL:
  3625. eax_defer<Eax1SourceAllValidator>(call, props);
  3626. break;
  3627. case DSPROPERTY_EAXBUFFER_REVERBMIX:
  3628. eax_defer<Eax1SourceReverbMixValidator>(call, props.fMix);
  3629. break;
  3630. default:
  3631. eax_fail_unknown_property_id();
  3632. }
  3633. }
  3634. void ALsource::eax2_set(const EaxCall& call, Eax2Props& props)
  3635. {
  3636. switch (call.get_property_id()) {
  3637. case DSPROPERTY_EAX20BUFFER_NONE:
  3638. break;
  3639. case DSPROPERTY_EAX20BUFFER_ALLPARAMETERS:
  3640. eax_defer<Eax2SourceAllValidator>(call, props);
  3641. break;
  3642. case DSPROPERTY_EAX20BUFFER_DIRECT:
  3643. eax_defer<Eax2SourceDirectValidator>(call, props.lDirect);
  3644. break;
  3645. case DSPROPERTY_EAX20BUFFER_DIRECTHF:
  3646. eax_defer<Eax2SourceDirectHfValidator>(call, props.lDirectHF);
  3647. break;
  3648. case DSPROPERTY_EAX20BUFFER_ROOM:
  3649. eax_defer<Eax2SourceRoomValidator>(call, props.lRoom);
  3650. break;
  3651. case DSPROPERTY_EAX20BUFFER_ROOMHF:
  3652. eax_defer<Eax2SourceRoomHfValidator>(call, props.lRoomHF);
  3653. break;
  3654. case DSPROPERTY_EAX20BUFFER_ROOMROLLOFFFACTOR:
  3655. eax_defer<Eax2SourceRoomRolloffFactorValidator>(call, props.flRoomRolloffFactor);
  3656. break;
  3657. case DSPROPERTY_EAX20BUFFER_OBSTRUCTION:
  3658. eax_defer<Eax2SourceObstructionValidator>(call, props.lObstruction);
  3659. break;
  3660. case DSPROPERTY_EAX20BUFFER_OBSTRUCTIONLFRATIO:
  3661. eax_defer<Eax2SourceObstructionLfRatioValidator>(call, props.flObstructionLFRatio);
  3662. break;
  3663. case DSPROPERTY_EAX20BUFFER_OCCLUSION:
  3664. eax_defer<Eax2SourceOcclusionValidator>(call, props.lOcclusion);
  3665. break;
  3666. case DSPROPERTY_EAX20BUFFER_OCCLUSIONLFRATIO:
  3667. eax_defer<Eax2SourceOcclusionLfRatioValidator>(call, props.flOcclusionLFRatio);
  3668. break;
  3669. case DSPROPERTY_EAX20BUFFER_OCCLUSIONROOMRATIO:
  3670. eax_defer<Eax2SourceOcclusionRoomRatioValidator>(call, props.flOcclusionRoomRatio);
  3671. break;
  3672. case DSPROPERTY_EAX20BUFFER_OUTSIDEVOLUMEHF:
  3673. eax_defer<Eax2SourceOutsideVolumeHfValidator>(call, props.lOutsideVolumeHF);
  3674. break;
  3675. case DSPROPERTY_EAX20BUFFER_AIRABSORPTIONFACTOR:
  3676. eax_defer<Eax2SourceAirAbsorptionFactorValidator>(call, props.flAirAbsorptionFactor);
  3677. break;
  3678. case DSPROPERTY_EAX20BUFFER_FLAGS:
  3679. eax_defer<Eax2SourceFlagsValidator>(call, props.dwFlags);
  3680. break;
  3681. default:
  3682. eax_fail_unknown_property_id();
  3683. }
  3684. }
  3685. void ALsource::eax3_set(const EaxCall& call, Eax3Props& props)
  3686. {
  3687. switch (call.get_property_id()) {
  3688. case EAXSOURCE_NONE:
  3689. break;
  3690. case EAXSOURCE_ALLPARAMETERS:
  3691. eax_defer<Eax3SourceAllValidator>(call, props);
  3692. break;
  3693. case EAXSOURCE_OBSTRUCTIONPARAMETERS:
  3694. eax_defer_sub<Eax4ObstructionValidator, EAXOBSTRUCTIONPROPERTIES>(call, props.lObstruction);
  3695. break;
  3696. case EAXSOURCE_OCCLUSIONPARAMETERS:
  3697. eax_defer_sub<Eax4OcclusionValidator, EAXOCCLUSIONPROPERTIES>(call, props.lOcclusion);
  3698. break;
  3699. case EAXSOURCE_EXCLUSIONPARAMETERS:
  3700. eax_defer_sub<Eax4ExclusionValidator, EAXEXCLUSIONPROPERTIES>(call, props.lExclusion);
  3701. break;
  3702. case EAXSOURCE_DIRECT:
  3703. eax_defer<Eax2SourceDirectValidator>(call, props.lDirect);
  3704. break;
  3705. case EAXSOURCE_DIRECTHF:
  3706. eax_defer<Eax2SourceDirectHfValidator>(call, props.lDirectHF);
  3707. break;
  3708. case EAXSOURCE_ROOM:
  3709. eax_defer<Eax2SourceRoomValidator>(call, props.lRoom);
  3710. break;
  3711. case EAXSOURCE_ROOMHF:
  3712. eax_defer<Eax2SourceRoomHfValidator>(call, props.lRoomHF);
  3713. break;
  3714. case EAXSOURCE_OBSTRUCTION:
  3715. eax_defer<Eax2SourceObstructionValidator>(call, props.lObstruction);
  3716. break;
  3717. case EAXSOURCE_OBSTRUCTIONLFRATIO:
  3718. eax_defer<Eax2SourceObstructionLfRatioValidator>(call, props.flObstructionLFRatio);
  3719. break;
  3720. case EAXSOURCE_OCCLUSION:
  3721. eax_defer<Eax2SourceOcclusionValidator>(call, props.lOcclusion);
  3722. break;
  3723. case EAXSOURCE_OCCLUSIONLFRATIO:
  3724. eax_defer<Eax2SourceOcclusionLfRatioValidator>(call, props.flOcclusionLFRatio);
  3725. break;
  3726. case EAXSOURCE_OCCLUSIONROOMRATIO:
  3727. eax_defer<Eax2SourceOcclusionRoomRatioValidator>(call, props.flOcclusionRoomRatio);
  3728. break;
  3729. case EAXSOURCE_OCCLUSIONDIRECTRATIO:
  3730. eax_defer<Eax3SourceOcclusionDirectRatioValidator>(call, props.flOcclusionDirectRatio);
  3731. break;
  3732. case EAXSOURCE_EXCLUSION:
  3733. eax_defer<Eax3SourceExclusionValidator>(call, props.lExclusion);
  3734. break;
  3735. case EAXSOURCE_EXCLUSIONLFRATIO:
  3736. eax_defer<Eax3SourceExclusionLfRatioValidator>(call, props.flExclusionLFRatio);
  3737. break;
  3738. case EAXSOURCE_OUTSIDEVOLUMEHF:
  3739. eax_defer<Eax2SourceOutsideVolumeHfValidator>(call, props.lOutsideVolumeHF);
  3740. break;
  3741. case EAXSOURCE_DOPPLERFACTOR:
  3742. eax_defer<Eax3SourceDopplerFactorValidator>(call, props.flDopplerFactor);
  3743. break;
  3744. case EAXSOURCE_ROLLOFFFACTOR:
  3745. eax_defer<Eax3SourceRolloffFactorValidator>(call, props.flRolloffFactor);
  3746. break;
  3747. case EAXSOURCE_ROOMROLLOFFFACTOR:
  3748. eax_defer<Eax2SourceRoomRolloffFactorValidator>(call, props.flRoomRolloffFactor);
  3749. break;
  3750. case EAXSOURCE_AIRABSORPTIONFACTOR:
  3751. eax_defer<Eax2SourceAirAbsorptionFactorValidator>(call, props.flAirAbsorptionFactor);
  3752. break;
  3753. case EAXSOURCE_FLAGS:
  3754. eax_defer<Eax2SourceFlagsValidator>(call, props.ulFlags);
  3755. break;
  3756. default:
  3757. eax_fail_unknown_property_id();
  3758. }
  3759. }
  3760. void ALsource::eax4_set(const EaxCall& call, Eax4Props& props)
  3761. {
  3762. switch (call.get_property_id()) {
  3763. case EAXSOURCE_NONE:
  3764. case EAXSOURCE_ALLPARAMETERS:
  3765. case EAXSOURCE_OBSTRUCTIONPARAMETERS:
  3766. case EAXSOURCE_OCCLUSIONPARAMETERS:
  3767. case EAXSOURCE_EXCLUSIONPARAMETERS:
  3768. case EAXSOURCE_DIRECT:
  3769. case EAXSOURCE_DIRECTHF:
  3770. case EAXSOURCE_ROOM:
  3771. case EAXSOURCE_ROOMHF:
  3772. case EAXSOURCE_OBSTRUCTION:
  3773. case EAXSOURCE_OBSTRUCTIONLFRATIO:
  3774. case EAXSOURCE_OCCLUSION:
  3775. case EAXSOURCE_OCCLUSIONLFRATIO:
  3776. case EAXSOURCE_OCCLUSIONROOMRATIO:
  3777. case EAXSOURCE_OCCLUSIONDIRECTRATIO:
  3778. case EAXSOURCE_EXCLUSION:
  3779. case EAXSOURCE_EXCLUSIONLFRATIO:
  3780. case EAXSOURCE_OUTSIDEVOLUMEHF:
  3781. case EAXSOURCE_DOPPLERFACTOR:
  3782. case EAXSOURCE_ROLLOFFFACTOR:
  3783. case EAXSOURCE_ROOMROLLOFFFACTOR:
  3784. case EAXSOURCE_AIRABSORPTIONFACTOR:
  3785. case EAXSOURCE_FLAGS:
  3786. eax3_set(call, props.source);
  3787. break;
  3788. case EAXSOURCE_SENDPARAMETERS:
  3789. eax4_defer_sends<Eax4SendValidator, EAXSOURCESENDPROPERTIES>(call, props.sends);
  3790. break;
  3791. case EAXSOURCE_ALLSENDPARAMETERS:
  3792. eax4_defer_sends<Eax4AllSendValidator, EAXSOURCEALLSENDPROPERTIES>(call, props.sends);
  3793. break;
  3794. case EAXSOURCE_OCCLUSIONSENDPARAMETERS:
  3795. eax4_defer_sends<Eax4OcclusionSendValidator, EAXSOURCEOCCLUSIONSENDPROPERTIES>(call, props.sends);
  3796. break;
  3797. case EAXSOURCE_EXCLUSIONSENDPARAMETERS:
  3798. eax4_defer_sends<Eax4ExclusionSendValidator, EAXSOURCEEXCLUSIONSENDPROPERTIES>(call, props.sends);
  3799. break;
  3800. case EAXSOURCE_ACTIVEFXSLOTID:
  3801. eax4_defer_active_fx_slot_id(call, props.active_fx_slots.guidActiveFXSlots);
  3802. break;
  3803. default:
  3804. eax_fail_unknown_property_id();
  3805. }
  3806. }
  3807. void ALsource::eax5_defer_all_2d(const EaxCall& call, EAX50SOURCEPROPERTIES& props)
  3808. {
  3809. const auto& src_props = call.get_value<Exception, const EAXSOURCE2DPROPERTIES>();
  3810. Eax5SourceAll2dValidator{}(src_props);
  3811. props.lDirect = src_props.lDirect;
  3812. props.lDirectHF = src_props.lDirectHF;
  3813. props.lRoom = src_props.lRoom;
  3814. props.lRoomHF = src_props.lRoomHF;
  3815. props.ulFlags = src_props.ulFlags;
  3816. }
  3817. void ALsource::eax5_defer_speaker_levels(const EaxCall& call, EaxSpeakerLevels& props)
  3818. {
  3819. const auto values = call.get_values<const EAXSPEAKERLEVELPROPERTIES>(eax_max_speakers);
  3820. std::for_each(values.cbegin(), values.cend(), Eax5SpeakerAllValidator{});
  3821. for (const auto& value : values) {
  3822. const auto index = static_cast<size_t>(value.lSpeakerID - EAXSPEAKER_FRONT_LEFT);
  3823. props[index].lLevel = value.lLevel;
  3824. }
  3825. }
  3826. void ALsource::eax5_set(const EaxCall& call, Eax5Props& props)
  3827. {
  3828. switch (call.get_property_id()) {
  3829. case EAXSOURCE_NONE:
  3830. break;
  3831. case EAXSOURCE_ALLPARAMETERS:
  3832. eax_defer<Eax5SourceAllValidator>(call, props.source);
  3833. break;
  3834. case EAXSOURCE_OBSTRUCTIONPARAMETERS:
  3835. case EAXSOURCE_OCCLUSIONPARAMETERS:
  3836. case EAXSOURCE_EXCLUSIONPARAMETERS:
  3837. case EAXSOURCE_DIRECT:
  3838. case EAXSOURCE_DIRECTHF:
  3839. case EAXSOURCE_ROOM:
  3840. case EAXSOURCE_ROOMHF:
  3841. case EAXSOURCE_OBSTRUCTION:
  3842. case EAXSOURCE_OBSTRUCTIONLFRATIO:
  3843. case EAXSOURCE_OCCLUSION:
  3844. case EAXSOURCE_OCCLUSIONLFRATIO:
  3845. case EAXSOURCE_OCCLUSIONROOMRATIO:
  3846. case EAXSOURCE_OCCLUSIONDIRECTRATIO:
  3847. case EAXSOURCE_EXCLUSION:
  3848. case EAXSOURCE_EXCLUSIONLFRATIO:
  3849. case EAXSOURCE_OUTSIDEVOLUMEHF:
  3850. case EAXSOURCE_DOPPLERFACTOR:
  3851. case EAXSOURCE_ROLLOFFFACTOR:
  3852. case EAXSOURCE_ROOMROLLOFFFACTOR:
  3853. case EAXSOURCE_AIRABSORPTIONFACTOR:
  3854. case EAXSOURCE_FLAGS:
  3855. eax3_set(call, props.source);
  3856. break;
  3857. case EAXSOURCE_SENDPARAMETERS:
  3858. eax5_defer_sends<Eax5SendValidator, EAXSOURCESENDPROPERTIES>(call, props.sends);
  3859. break;
  3860. case EAXSOURCE_ALLSENDPARAMETERS:
  3861. eax5_defer_sends<Eax5AllSendValidator, EAXSOURCEALLSENDPROPERTIES>(call, props.sends);
  3862. break;
  3863. case EAXSOURCE_OCCLUSIONSENDPARAMETERS:
  3864. eax5_defer_sends<Eax5OcclusionSendValidator, EAXSOURCEOCCLUSIONSENDPROPERTIES>(call, props.sends);
  3865. break;
  3866. case EAXSOURCE_EXCLUSIONSENDPARAMETERS:
  3867. eax5_defer_sends<Eax5ExclusionSendValidator, EAXSOURCEEXCLUSIONSENDPROPERTIES>(call, props.sends);
  3868. break;
  3869. case EAXSOURCE_ACTIVEFXSLOTID:
  3870. eax5_defer_active_fx_slot_id(call, props.active_fx_slots.guidActiveFXSlots);
  3871. break;
  3872. case EAXSOURCE_MACROFXFACTOR:
  3873. eax_defer<Eax5SourceMacroFXFactorValidator>(call, props.source.flMacroFXFactor);
  3874. break;
  3875. case EAXSOURCE_SPEAKERLEVELS:
  3876. eax5_defer_speaker_levels(call, props.speaker_levels);
  3877. break;
  3878. case EAXSOURCE_ALL2DPARAMETERS:
  3879. eax5_defer_all_2d(call, props.source);
  3880. break;
  3881. default:
  3882. eax_fail_unknown_property_id();
  3883. }
  3884. }
  3885. void ALsource::eax_set(const EaxCall& call)
  3886. {
  3887. const auto eax_version = call.get_version();
  3888. switch(eax_version)
  3889. {
  3890. case 1: eax1_set(call, eax1_.d); break;
  3891. case 2: eax2_set(call, eax2_.d); break;
  3892. case 3: eax3_set(call, eax3_.d); break;
  3893. case 4: eax4_set(call, eax4_.d); break;
  3894. case 5: eax5_set(call, eax5_.d); break;
  3895. default: eax_fail_unknown_property_id();
  3896. }
  3897. eax_changed_ = true;
  3898. eax_version_ = eax_version;
  3899. }
  3900. void ALsource::eax1_get(const EaxCall& call, const Eax1Props& props)
  3901. {
  3902. switch (call.get_property_id()) {
  3903. case DSPROPERTY_EAXBUFFER_ALL:
  3904. case DSPROPERTY_EAXBUFFER_REVERBMIX:
  3905. call.set_value<Exception>(props.fMix);
  3906. break;
  3907. default:
  3908. eax_fail_unknown_property_id();
  3909. }
  3910. }
  3911. void ALsource::eax2_get(const EaxCall& call, const Eax2Props& props)
  3912. {
  3913. switch (call.get_property_id()) {
  3914. case DSPROPERTY_EAX20BUFFER_NONE:
  3915. break;
  3916. case DSPROPERTY_EAX20BUFFER_ALLPARAMETERS:
  3917. call.set_value<Exception>(props);
  3918. break;
  3919. case DSPROPERTY_EAX20BUFFER_DIRECT:
  3920. call.set_value<Exception>(props.lDirect);
  3921. break;
  3922. case DSPROPERTY_EAX20BUFFER_DIRECTHF:
  3923. call.set_value<Exception>(props.lDirectHF);
  3924. break;
  3925. case DSPROPERTY_EAX20BUFFER_ROOM:
  3926. call.set_value<Exception>(props.lRoom);
  3927. break;
  3928. case DSPROPERTY_EAX20BUFFER_ROOMHF:
  3929. call.set_value<Exception>(props.lRoomHF);
  3930. break;
  3931. case DSPROPERTY_EAX20BUFFER_ROOMROLLOFFFACTOR:
  3932. call.set_value<Exception>(props.flRoomRolloffFactor);
  3933. break;
  3934. case DSPROPERTY_EAX20BUFFER_OBSTRUCTION:
  3935. call.set_value<Exception>(props.lObstruction);
  3936. break;
  3937. case DSPROPERTY_EAX20BUFFER_OBSTRUCTIONLFRATIO:
  3938. call.set_value<Exception>(props.flObstructionLFRatio);
  3939. break;
  3940. case DSPROPERTY_EAX20BUFFER_OCCLUSION:
  3941. call.set_value<Exception>(props.lOcclusion);
  3942. break;
  3943. case DSPROPERTY_EAX20BUFFER_OCCLUSIONLFRATIO:
  3944. call.set_value<Exception>(props.flOcclusionLFRatio);
  3945. break;
  3946. case DSPROPERTY_EAX20BUFFER_OCCLUSIONROOMRATIO:
  3947. call.set_value<Exception>(props.flOcclusionRoomRatio);
  3948. break;
  3949. case DSPROPERTY_EAX20BUFFER_OUTSIDEVOLUMEHF:
  3950. call.set_value<Exception>(props.lOutsideVolumeHF);
  3951. break;
  3952. case DSPROPERTY_EAX20BUFFER_AIRABSORPTIONFACTOR:
  3953. call.set_value<Exception>(props.flAirAbsorptionFactor);
  3954. break;
  3955. case DSPROPERTY_EAX20BUFFER_FLAGS:
  3956. call.set_value<Exception>(props.dwFlags);
  3957. break;
  3958. default:
  3959. eax_fail_unknown_property_id();
  3960. }
  3961. }
  3962. void ALsource::eax3_get_obstruction(const EaxCall& call, const Eax3Props& props)
  3963. {
  3964. const auto& subprops = reinterpret_cast<const EAXOBSTRUCTIONPROPERTIES&>(props.lObstruction);
  3965. call.set_value<Exception>(subprops);
  3966. }
  3967. void ALsource::eax3_get_occlusion(const EaxCall& call, const Eax3Props& props)
  3968. {
  3969. const auto& subprops = reinterpret_cast<const EAXOCCLUSIONPROPERTIES&>(props.lOcclusion);
  3970. call.set_value<Exception>(subprops);
  3971. }
  3972. void ALsource::eax3_get_exclusion(const EaxCall& call, const Eax3Props& props)
  3973. {
  3974. const auto& subprops = reinterpret_cast<const EAXEXCLUSIONPROPERTIES&>(props.lExclusion);
  3975. call.set_value<Exception>(subprops);
  3976. }
  3977. void ALsource::eax3_get(const EaxCall& call, const Eax3Props& props)
  3978. {
  3979. switch (call.get_property_id()) {
  3980. case EAXSOURCE_NONE:
  3981. break;
  3982. case EAXSOURCE_ALLPARAMETERS:
  3983. call.set_value<Exception>(props);
  3984. break;
  3985. case EAXSOURCE_OBSTRUCTIONPARAMETERS:
  3986. eax3_get_obstruction(call, props);
  3987. break;
  3988. case EAXSOURCE_OCCLUSIONPARAMETERS:
  3989. eax3_get_occlusion(call, props);
  3990. break;
  3991. case EAXSOURCE_EXCLUSIONPARAMETERS:
  3992. eax3_get_exclusion(call, props);
  3993. break;
  3994. case EAXSOURCE_DIRECT:
  3995. call.set_value<Exception>(props.lDirect);
  3996. break;
  3997. case EAXSOURCE_DIRECTHF:
  3998. call.set_value<Exception>(props.lDirectHF);
  3999. break;
  4000. case EAXSOURCE_ROOM:
  4001. call.set_value<Exception>(props.lRoom);
  4002. break;
  4003. case EAXSOURCE_ROOMHF:
  4004. call.set_value<Exception>(props.lRoomHF);
  4005. break;
  4006. case EAXSOURCE_OBSTRUCTION:
  4007. call.set_value<Exception>(props.lObstruction);
  4008. break;
  4009. case EAXSOURCE_OBSTRUCTIONLFRATIO:
  4010. call.set_value<Exception>(props.flObstructionLFRatio);
  4011. break;
  4012. case EAXSOURCE_OCCLUSION:
  4013. call.set_value<Exception>(props.lOcclusion);
  4014. break;
  4015. case EAXSOURCE_OCCLUSIONLFRATIO:
  4016. call.set_value<Exception>(props.flOcclusionLFRatio);
  4017. break;
  4018. case EAXSOURCE_OCCLUSIONROOMRATIO:
  4019. call.set_value<Exception>(props.flOcclusionRoomRatio);
  4020. break;
  4021. case EAXSOURCE_OCCLUSIONDIRECTRATIO:
  4022. call.set_value<Exception>(props.flOcclusionDirectRatio);
  4023. break;
  4024. case EAXSOURCE_EXCLUSION:
  4025. call.set_value<Exception>(props.lExclusion);
  4026. break;
  4027. case EAXSOURCE_EXCLUSIONLFRATIO:
  4028. call.set_value<Exception>(props.flExclusionLFRatio);
  4029. break;
  4030. case EAXSOURCE_OUTSIDEVOLUMEHF:
  4031. call.set_value<Exception>(props.lOutsideVolumeHF);
  4032. break;
  4033. case EAXSOURCE_DOPPLERFACTOR:
  4034. call.set_value<Exception>(props.flDopplerFactor);
  4035. break;
  4036. case EAXSOURCE_ROLLOFFFACTOR:
  4037. call.set_value<Exception>(props.flRolloffFactor);
  4038. break;
  4039. case EAXSOURCE_ROOMROLLOFFFACTOR:
  4040. call.set_value<Exception>(props.flRoomRolloffFactor);
  4041. break;
  4042. case EAXSOURCE_AIRABSORPTIONFACTOR:
  4043. call.set_value<Exception>(props.flAirAbsorptionFactor);
  4044. break;
  4045. case EAXSOURCE_FLAGS:
  4046. call.set_value<Exception>(props.ulFlags);
  4047. break;
  4048. default:
  4049. eax_fail_unknown_property_id();
  4050. }
  4051. }
  4052. void ALsource::eax4_get(const EaxCall& call, const Eax4Props& props)
  4053. {
  4054. switch (call.get_property_id()) {
  4055. case EAXSOURCE_NONE:
  4056. break;
  4057. case EAXSOURCE_ALLPARAMETERS:
  4058. case EAXSOURCE_OBSTRUCTIONPARAMETERS:
  4059. case EAXSOURCE_OCCLUSIONPARAMETERS:
  4060. case EAXSOURCE_EXCLUSIONPARAMETERS:
  4061. case EAXSOURCE_DIRECT:
  4062. case EAXSOURCE_DIRECTHF:
  4063. case EAXSOURCE_ROOM:
  4064. case EAXSOURCE_ROOMHF:
  4065. case EAXSOURCE_OBSTRUCTION:
  4066. case EAXSOURCE_OBSTRUCTIONLFRATIO:
  4067. case EAXSOURCE_OCCLUSION:
  4068. case EAXSOURCE_OCCLUSIONLFRATIO:
  4069. case EAXSOURCE_OCCLUSIONROOMRATIO:
  4070. case EAXSOURCE_OCCLUSIONDIRECTRATIO:
  4071. case EAXSOURCE_EXCLUSION:
  4072. case EAXSOURCE_EXCLUSIONLFRATIO:
  4073. case EAXSOURCE_OUTSIDEVOLUMEHF:
  4074. case EAXSOURCE_DOPPLERFACTOR:
  4075. case EAXSOURCE_ROLLOFFFACTOR:
  4076. case EAXSOURCE_ROOMROLLOFFFACTOR:
  4077. case EAXSOURCE_AIRABSORPTIONFACTOR:
  4078. case EAXSOURCE_FLAGS:
  4079. eax3_get(call, props.source);
  4080. break;
  4081. case EAXSOURCE_SENDPARAMETERS:
  4082. eax_get_sends<EAXSOURCESENDPROPERTIES>(call, props.sends);
  4083. break;
  4084. case EAXSOURCE_ALLSENDPARAMETERS:
  4085. eax_get_sends<EAXSOURCEALLSENDPROPERTIES>(call, props.sends);
  4086. break;
  4087. case EAXSOURCE_OCCLUSIONSENDPARAMETERS:
  4088. eax_get_sends<EAXSOURCEOCCLUSIONSENDPROPERTIES>(call, props.sends);
  4089. break;
  4090. case EAXSOURCE_EXCLUSIONSENDPARAMETERS:
  4091. eax_get_sends<EAXSOURCEEXCLUSIONSENDPROPERTIES>(call, props.sends);
  4092. break;
  4093. case EAXSOURCE_ACTIVEFXSLOTID:
  4094. call.set_value<Exception>(props.active_fx_slots);
  4095. break;
  4096. default:
  4097. eax_fail_unknown_property_id();
  4098. }
  4099. }
  4100. void ALsource::eax5_get_all_2d(const EaxCall& call, const EAX50SOURCEPROPERTIES& props)
  4101. {
  4102. auto& subprops = call.get_value<Exception, EAXSOURCE2DPROPERTIES>();
  4103. subprops.lDirect = props.lDirect;
  4104. subprops.lDirectHF = props.lDirectHF;
  4105. subprops.lRoom = props.lRoom;
  4106. subprops.lRoomHF = props.lRoomHF;
  4107. subprops.ulFlags = props.ulFlags;
  4108. }
  4109. void ALsource::eax5_get_speaker_levels(const EaxCall& call, const EaxSpeakerLevels& props)
  4110. {
  4111. const auto subprops = call.get_values<EAXSPEAKERLEVELPROPERTIES>(eax_max_speakers);
  4112. std::uninitialized_copy_n(props.cbegin(), subprops.size(), subprops.begin());
  4113. }
  4114. void ALsource::eax5_get(const EaxCall& call, const Eax5Props& props)
  4115. {
  4116. switch (call.get_property_id()) {
  4117. case EAXSOURCE_NONE:
  4118. break;
  4119. case EAXSOURCE_ALLPARAMETERS:
  4120. case EAXSOURCE_OBSTRUCTIONPARAMETERS:
  4121. case EAXSOURCE_OCCLUSIONPARAMETERS:
  4122. case EAXSOURCE_EXCLUSIONPARAMETERS:
  4123. case EAXSOURCE_DIRECT:
  4124. case EAXSOURCE_DIRECTHF:
  4125. case EAXSOURCE_ROOM:
  4126. case EAXSOURCE_ROOMHF:
  4127. case EAXSOURCE_OBSTRUCTION:
  4128. case EAXSOURCE_OBSTRUCTIONLFRATIO:
  4129. case EAXSOURCE_OCCLUSION:
  4130. case EAXSOURCE_OCCLUSIONLFRATIO:
  4131. case EAXSOURCE_OCCLUSIONROOMRATIO:
  4132. case EAXSOURCE_OCCLUSIONDIRECTRATIO:
  4133. case EAXSOURCE_EXCLUSION:
  4134. case EAXSOURCE_EXCLUSIONLFRATIO:
  4135. case EAXSOURCE_OUTSIDEVOLUMEHF:
  4136. case EAXSOURCE_DOPPLERFACTOR:
  4137. case EAXSOURCE_ROLLOFFFACTOR:
  4138. case EAXSOURCE_ROOMROLLOFFFACTOR:
  4139. case EAXSOURCE_AIRABSORPTIONFACTOR:
  4140. case EAXSOURCE_FLAGS:
  4141. eax3_get(call, props.source);
  4142. break;
  4143. case EAXSOURCE_SENDPARAMETERS:
  4144. eax_get_sends<EAXSOURCESENDPROPERTIES>(call, props.sends);
  4145. break;
  4146. case EAXSOURCE_ALLSENDPARAMETERS:
  4147. eax_get_sends<EAXSOURCEALLSENDPROPERTIES>(call, props.sends);
  4148. break;
  4149. case EAXSOURCE_OCCLUSIONSENDPARAMETERS:
  4150. eax_get_sends<EAXSOURCEOCCLUSIONSENDPROPERTIES>(call, props.sends);
  4151. break;
  4152. case EAXSOURCE_EXCLUSIONSENDPARAMETERS:
  4153. eax_get_sends<EAXSOURCEEXCLUSIONSENDPROPERTIES>(call, props.sends);
  4154. break;
  4155. case EAXSOURCE_ACTIVEFXSLOTID:
  4156. call.set_value<Exception>(props.active_fx_slots);
  4157. break;
  4158. case EAXSOURCE_MACROFXFACTOR:
  4159. call.set_value<Exception>(props.source.flMacroFXFactor);
  4160. break;
  4161. case EAXSOURCE_SPEAKERLEVELS:
  4162. call.set_value<Exception>(props.speaker_levels);
  4163. break;
  4164. case EAXSOURCE_ALL2DPARAMETERS:
  4165. eax5_get_all_2d(call, props.source);
  4166. break;
  4167. default:
  4168. eax_fail_unknown_property_id();
  4169. }
  4170. }
  4171. void ALsource::eax_get(const EaxCall& call)
  4172. {
  4173. switch (call.get_version()) {
  4174. case 1: eax1_get(call, eax1_.i); break;
  4175. case 2: eax2_get(call, eax2_.i); break;
  4176. case 3: eax3_get(call, eax3_.i); break;
  4177. case 4: eax4_get(call, eax4_.i); break;
  4178. case 5: eax5_get(call, eax5_.i); break;
  4179. default: eax_fail_unknown_version();
  4180. }
  4181. }
  4182. void ALsource::eax_set_al_source_send(ALeffectslot *slot, size_t sendidx, const EaxAlLowPassParam &filter)
  4183. {
  4184. if(sendidx >= EAX_MAX_FXSLOTS)
  4185. return;
  4186. auto &send = Send[sendidx];
  4187. send.Gain = filter.gain;
  4188. send.GainHF = filter.gain_hf;
  4189. send.HFReference = LOWPASSFREQREF;
  4190. send.GainLF = 1.0f;
  4191. send.LFReference = HIGHPASSFREQREF;
  4192. if(slot != nullptr)
  4193. IncrementRef(slot->ref);
  4194. if(auto *oldslot = send.Slot)
  4195. DecrementRef(oldslot->ref);
  4196. send.Slot = slot;
  4197. mPropsDirty = true;
  4198. }
  4199. void ALsource::eax_commit_active_fx_slots()
  4200. {
  4201. // Mark all slots as non-active.
  4202. eax_active_fx_slots_.fill(false);
  4203. // Mark primary FX slot as active.
  4204. if (eax_primary_fx_slot_id_.has_value())
  4205. eax_active_fx_slots_[*eax_primary_fx_slot_id_] = true;
  4206. // Mark the other FX slots as active.
  4207. for (const auto& slot_id : eax_.active_fx_slots.guidActiveFXSlots) {
  4208. if (slot_id == EAXPROPERTYID_EAX50_FXSlot0)
  4209. eax_active_fx_slots_[0] = true;
  4210. else if (slot_id == EAXPROPERTYID_EAX50_FXSlot1)
  4211. eax_active_fx_slots_[1] = true;
  4212. else if (slot_id == EAXPROPERTYID_EAX50_FXSlot2)
  4213. eax_active_fx_slots_[2] = true;
  4214. else if (slot_id == EAXPROPERTYID_EAX50_FXSlot3)
  4215. eax_active_fx_slots_[3] = true;
  4216. }
  4217. // Deactivate EFX auxiliary effect slots.
  4218. for (auto i = size_t{}; i < EAX_MAX_FXSLOTS; ++i) {
  4219. if (!eax_active_fx_slots_[i])
  4220. eax_set_al_source_send(nullptr, i, EaxAlLowPassParam{1.0f, 1.0f});
  4221. }
  4222. }
  4223. void ALsource::eax_commit_filters()
  4224. {
  4225. eax_update_direct_filter();
  4226. eax_update_room_filters();
  4227. }
  4228. void ALsource::eax_commit(EaxCommitType commit_type)
  4229. {
  4230. const auto primary_fx_slot_id = eax_al_context_->eax_get_primary_fx_slot_index();
  4231. const auto is_primary_fx_slot_id_changed = (eax_primary_fx_slot_id_ != primary_fx_slot_id);
  4232. if(commit_type != EaxCommitType::forced && !is_primary_fx_slot_id_changed && !eax_changed_)
  4233. return;
  4234. eax_primary_fx_slot_id_ = primary_fx_slot_id;
  4235. eax_changed_ = false;
  4236. switch(eax_version_)
  4237. {
  4238. case 1:
  4239. eax1_.i = eax1_.d;
  4240. eax1_translate(eax1_.i, eax_);
  4241. break;
  4242. case 2:
  4243. eax2_.i = eax2_.d;
  4244. eax2_translate(eax2_.i, eax_);
  4245. break;
  4246. case 3:
  4247. eax3_.i = eax3_.d;
  4248. eax3_translate(eax3_.i, eax_);
  4249. break;
  4250. case 4:
  4251. eax4_.i = eax4_.d;
  4252. eax4_translate(eax4_.i, eax_);
  4253. break;
  4254. case 5:
  4255. eax5_.i = eax5_.d;
  4256. eax_ = eax5_.d;
  4257. break;
  4258. default:
  4259. eax_fail_unknown_version();
  4260. }
  4261. eax_set_efx_outer_gain_hf();
  4262. eax_set_efx_doppler_factor();
  4263. eax_set_efx_rolloff_factor();
  4264. eax_set_efx_room_rolloff_factor();
  4265. eax_set_efx_air_absorption_factor();
  4266. eax_set_efx_dry_gain_hf_auto();
  4267. eax_set_efx_wet_gain_auto();
  4268. eax_set_efx_wet_gain_hf_auto();
  4269. eax_commit_active_fx_slots();
  4270. eax_commit_filters();
  4271. }
  4272. #endif // ALSOFT_EAX