dedicated.cpp 5.8 KB

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