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

162 lines
6.0 KiB

  1. /**
  2. * OpenAL cross platform audio library
  3. * Copyright (C) 2011 by Chris Robinson.
  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 <cstdlib>
  22. #include <cmath>
  23. #include <algorithm>
  24. #include "alMain.h"
  25. #include "alcontext.h"
  26. #include "alAuxEffectSlot.h"
  27. #include "alError.h"
  28. #include "alu.h"
  29. namespace {
  30. struct DedicatedState final : public EffectState {
  31. ALfloat mCurrentGains[MAX_OUTPUT_CHANNELS];
  32. ALfloat mTargetGains[MAX_OUTPUT_CHANNELS];
  33. ALboolean deviceUpdate(const ALCdevice *device) override;
  34. void update(const ALCcontext *context, const ALeffectslot *slot, const EffectProps *props, const EffectTarget target) override;
  35. void process(ALsizei samplesToDo, const ALfloat (*RESTRICT samplesIn)[BUFFERSIZE], const ALsizei numInput, ALfloat (*RESTRICT samplesOut)[BUFFERSIZE], const ALsizei numOutput) override;
  36. DEF_NEWDEL(DedicatedState)
  37. };
  38. ALboolean DedicatedState::deviceUpdate(const ALCdevice *UNUSED(device))
  39. {
  40. std::fill(std::begin(mCurrentGains), std::end(mCurrentGains), 0.0f);
  41. return AL_TRUE;
  42. }
  43. void DedicatedState::update(const ALCcontext* UNUSED(context), const ALeffectslot *slot, const EffectProps *props, const EffectTarget target)
  44. {
  45. std::fill(std::begin(mTargetGains), std::end(mTargetGains), 0.0f);
  46. const ALfloat Gain{slot->Params.Gain * props->Dedicated.Gain};
  47. if(slot->Params.EffectType == AL_EFFECT_DEDICATED_LOW_FREQUENCY_EFFECT)
  48. {
  49. const int idx{!target.RealOut ? -1 : GetChannelIdxByName(*target.RealOut, LFE)};
  50. if(idx != -1)
  51. {
  52. mOutBuffer = target.RealOut->Buffer;
  53. mOutChannels = target.RealOut->NumChannels;
  54. mTargetGains[idx] = Gain;
  55. }
  56. }
  57. else if(slot->Params.EffectType == AL_EFFECT_DEDICATED_DIALOGUE)
  58. {
  59. /* Dialog goes to the front-center speaker if it exists, otherwise it
  60. * plays from the front-center location. */
  61. const int idx{!target.RealOut ? -1 : GetChannelIdxByName(*target.RealOut, FrontCenter)};
  62. if(idx != -1)
  63. {
  64. mOutBuffer = target.RealOut->Buffer;
  65. mOutChannels = target.RealOut->NumChannels;
  66. mTargetGains[idx] = Gain;
  67. }
  68. else
  69. {
  70. ALfloat coeffs[MAX_AMBI_CHANNELS];
  71. CalcDirectionCoeffs({0.0f, 0.0f, -1.0f}, 0.0f, coeffs);
  72. mOutBuffer = target.Main->Buffer;
  73. mOutChannels = target.Main->NumChannels;
  74. ComputePanGains(target.Main, coeffs, Gain, mTargetGains);
  75. }
  76. }
  77. }
  78. void DedicatedState::process(ALsizei samplesToDo, const ALfloat (*RESTRICT samplesIn)[BUFFERSIZE], const ALsizei /*numInput*/, ALfloat (*RESTRICT samplesOut)[BUFFERSIZE], const ALsizei numOutput)
  79. {
  80. MixSamples(samplesIn[0], numOutput, samplesOut, mCurrentGains, mTargetGains, samplesToDo, 0,
  81. samplesToDo);
  82. }
  83. void Dedicated_setParami(EffectProps*, ALCcontext *context, ALenum param, ALint)
  84. { alSetError(context, AL_INVALID_ENUM, "Invalid dedicated integer property 0x%04x", param); }
  85. void Dedicated_setParamiv(EffectProps*, ALCcontext *context, ALenum param, const ALint*)
  86. { alSetError(context, AL_INVALID_ENUM, "Invalid dedicated integer-vector property 0x%04x", param); }
  87. void Dedicated_setParamf(EffectProps *props, ALCcontext *context, ALenum param, ALfloat val)
  88. {
  89. switch(param)
  90. {
  91. case AL_DEDICATED_GAIN:
  92. if(!(val >= 0.0f && std::isfinite(val)))
  93. SETERR_RETURN(context, AL_INVALID_VALUE,, "Dedicated gain out of range");
  94. props->Dedicated.Gain = val;
  95. break;
  96. default:
  97. alSetError(context, AL_INVALID_ENUM, "Invalid dedicated float property 0x%04x", param);
  98. }
  99. }
  100. void Dedicated_setParamfv(EffectProps *props, ALCcontext *context, ALenum param, const ALfloat *vals)
  101. { Dedicated_setParamf(props, context, param, vals[0]); }
  102. void Dedicated_getParami(const EffectProps*, ALCcontext *context, ALenum param, ALint*)
  103. { alSetError(context, AL_INVALID_ENUM, "Invalid dedicated integer property 0x%04x", param); }
  104. void Dedicated_getParamiv(const EffectProps*, ALCcontext *context, ALenum param, ALint*)
  105. { alSetError(context, AL_INVALID_ENUM, "Invalid dedicated integer-vector property 0x%04x", param); }
  106. void Dedicated_getParamf(const EffectProps *props, ALCcontext *context, ALenum param, ALfloat *val)
  107. {
  108. switch(param)
  109. {
  110. case AL_DEDICATED_GAIN:
  111. *val = props->Dedicated.Gain;
  112. break;
  113. default:
  114. alSetError(context, AL_INVALID_ENUM, "Invalid dedicated float property 0x%04x", param);
  115. }
  116. }
  117. void Dedicated_getParamfv(const EffectProps *props, ALCcontext *context, ALenum param, ALfloat *vals)
  118. { Dedicated_getParamf(props, context, param, vals); }
  119. DEFINE_ALEFFECT_VTABLE(Dedicated);
  120. struct DedicatedStateFactory final : public EffectStateFactory {
  121. EffectState *create() override { return new DedicatedState{}; }
  122. EffectProps getDefaultProps() const noexcept override;
  123. const EffectVtable *getEffectVtable() const noexcept override { return &Dedicated_vtable; }
  124. };
  125. EffectProps DedicatedStateFactory::getDefaultProps() const noexcept
  126. {
  127. EffectProps props{};
  128. props.Dedicated.Gain = 1.0f;
  129. return props;
  130. }
  131. } // namespace
  132. EffectStateFactory *DedicatedStateFactory_getFactory()
  133. {
  134. static DedicatedStateFactory DedicatedFactory{};
  135. return &DedicatedFactory;
  136. }