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.

129 lines
4.7KB

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