The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

146 lines
5.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce::dsp
  19. {
  20. /**
  21. Applies a gain to audio samples as single samples or AudioBlocks.
  22. @tags{DSP}
  23. */
  24. template <typename FloatType>
  25. class Gain
  26. {
  27. public:
  28. Gain() noexcept = default;
  29. //==============================================================================
  30. /** Applies a new gain as a linear value. */
  31. void setGainLinear (FloatType newGain) noexcept { gain.setTargetValue (newGain); }
  32. /** Applies a new gain as a decibel value. */
  33. void setGainDecibels (FloatType newGainDecibels) noexcept { setGainLinear (Decibels::decibelsToGain<FloatType> (newGainDecibels)); }
  34. /** Returns the current gain as a linear value. */
  35. FloatType getGainLinear() const noexcept { return gain.getTargetValue(); }
  36. /** Returns the current gain in decibels. */
  37. FloatType getGainDecibels() const noexcept { return Decibels::gainToDecibels<FloatType> (getGainLinear()); }
  38. /** Sets the length of the ramp used for smoothing gain changes. */
  39. void setRampDurationSeconds (double newDurationSeconds) noexcept
  40. {
  41. if (! approximatelyEqual (rampDurationSeconds, newDurationSeconds))
  42. {
  43. rampDurationSeconds = newDurationSeconds;
  44. reset();
  45. }
  46. }
  47. /** Returns the ramp duration in seconds. */
  48. double getRampDurationSeconds() const noexcept { return rampDurationSeconds; }
  49. /** Returns true if the current value is currently being interpolated. */
  50. bool isSmoothing() const noexcept { return gain.isSmoothing(); }
  51. //==============================================================================
  52. /** Called before processing starts. */
  53. void prepare (const ProcessSpec& spec) noexcept
  54. {
  55. sampleRate = spec.sampleRate;
  56. reset();
  57. }
  58. /** Resets the internal state of the gain */
  59. void reset() noexcept
  60. {
  61. if (sampleRate > 0)
  62. gain.reset (sampleRate, rampDurationSeconds);
  63. }
  64. //==============================================================================
  65. /** Returns the result of processing a single sample. */
  66. template <typename SampleType>
  67. SampleType JUCE_VECTOR_CALLTYPE processSample (SampleType s) noexcept
  68. {
  69. return s * gain.getNextValue();
  70. }
  71. /** Processes the input and output buffers supplied in the processing context. */
  72. template <typename ProcessContext>
  73. void process (const ProcessContext& context) noexcept
  74. {
  75. auto&& inBlock = context.getInputBlock();
  76. auto&& outBlock = context.getOutputBlock();
  77. jassert (inBlock.getNumChannels() == outBlock.getNumChannels());
  78. jassert (inBlock.getNumSamples() == outBlock.getNumSamples());
  79. auto len = inBlock.getNumSamples();
  80. auto numChannels = inBlock.getNumChannels();
  81. if (context.isBypassed)
  82. {
  83. gain.skip (static_cast<int> (len));
  84. if (context.usesSeparateInputAndOutputBlocks())
  85. outBlock.copyFrom (inBlock);
  86. return;
  87. }
  88. if (numChannels == 1)
  89. {
  90. auto* src = inBlock.getChannelPointer (0);
  91. auto* dst = outBlock.getChannelPointer (0);
  92. for (size_t i = 0; i < len; ++i)
  93. dst[i] = src[i] * gain.getNextValue();
  94. }
  95. else
  96. {
  97. JUCE_BEGIN_IGNORE_WARNINGS_MSVC (6255 6386)
  98. auto* gains = static_cast<FloatType*> (alloca (sizeof (FloatType) * len));
  99. for (size_t i = 0; i < len; ++i)
  100. gains[i] = gain.getNextValue();
  101. JUCE_END_IGNORE_WARNINGS_MSVC
  102. for (size_t chan = 0; chan < numChannels; ++chan)
  103. FloatVectorOperations::multiply (outBlock.getChannelPointer (chan),
  104. inBlock.getChannelPointer (chan),
  105. gains, static_cast<int> (len));
  106. }
  107. }
  108. private:
  109. //==============================================================================
  110. SmoothedValue<FloatType> gain;
  111. double sampleRate = 0, rampDurationSeconds = 0;
  112. };
  113. } // namespace juce::dsp