dedicated.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 "alcmain.h"
  25. #include "alcontext.h"
  26. #include "alu.h"
  27. #include "effectslot.h"
  28. namespace {
  29. struct DedicatedState final : public EffectState {
  30. float mCurrentGains[MAX_OUTPUT_CHANNELS];
  31. float mTargetGains[MAX_OUTPUT_CHANNELS];
  32. void deviceUpdate(const ALCdevice *device, const Buffer &buffer) override;
  33. void update(const ALCcontext *context, const EffectSlot *slot, const EffectProps *props,
  34. const EffectTarget target) override;
  35. void process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn,
  36. const al::span<FloatBufferLine> samplesOut) override;
  37. DEF_NEWDEL(DedicatedState)
  38. };
  39. void DedicatedState::deviceUpdate(const ALCdevice*, const Buffer&)
  40. {
  41. std::fill(std::begin(mCurrentGains), std::end(mCurrentGains), 0.0f);
  42. }
  43. void DedicatedState::update(const ALCcontext*, const EffectSlot *slot,
  44. const EffectProps *props, const EffectTarget target)
  45. {
  46. std::fill(std::begin(mTargetGains), std::end(mTargetGains), 0.0f);
  47. const float Gain{slot->Gain * props->Dedicated.Gain};
  48. if(slot->EffectType == EffectSlotType::DedicatedLFE)
  49. {
  50. const uint idx{!target.RealOut ? INVALID_CHANNEL_INDEX :
  51. GetChannelIdxByName(*target.RealOut, LFE)};
  52. if(idx != INVALID_CHANNEL_INDEX)
  53. {
  54. mOutTarget = target.RealOut->Buffer;
  55. mTargetGains[idx] = Gain;
  56. }
  57. }
  58. else if(slot->EffectType == EffectSlotType::DedicatedDialog)
  59. {
  60. /* Dialog goes to the front-center speaker if it exists, otherwise it
  61. * plays from the front-center location. */
  62. const uint idx{!target.RealOut ? INVALID_CHANNEL_INDEX :
  63. GetChannelIdxByName(*target.RealOut, FrontCenter)};
  64. if(idx != INVALID_CHANNEL_INDEX)
  65. {
  66. mOutTarget = target.RealOut->Buffer;
  67. mTargetGains[idx] = Gain;
  68. }
  69. else
  70. {
  71. const auto coeffs = CalcDirectionCoeffs({0.0f, 0.0f, -1.0f}, 0.0f);
  72. mOutTarget = target.Main->Buffer;
  73. ComputePanGains(target.Main, coeffs.data(), Gain, mTargetGains);
  74. }
  75. }
  76. }
  77. void DedicatedState::process(const size_t samplesToDo, const al::span<const FloatBufferLine> samplesIn, const al::span<FloatBufferLine> samplesOut)
  78. {
  79. MixSamples({samplesIn[0].data(), samplesToDo}, samplesOut, mCurrentGains, mTargetGains,
  80. samplesToDo, 0);
  81. }
  82. struct DedicatedStateFactory final : public EffectStateFactory {
  83. al::intrusive_ptr<EffectState> create() override
  84. { return al::intrusive_ptr<EffectState>{new DedicatedState{}}; }
  85. };
  86. } // namespace
  87. EffectStateFactory *DedicatedStateFactory_getFactory()
  88. {
  89. static DedicatedStateFactory DedicatedFactory{};
  90. return &DedicatedFactory;
  91. }