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

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