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.

148 lines
5.0KB

  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. namespace juce
  20. {
  21. namespace dsp
  22. {
  23. /**
  24. Applies a gain to audio samples as single samples or AudioBlocks.
  25. @tags{DSP}
  26. */
  27. template <typename FloatType>
  28. class Gain
  29. {
  30. public:
  31. Gain() noexcept {}
  32. //==============================================================================
  33. /** Applies a new gain as a linear value. */
  34. void setGainLinear (FloatType newGain) noexcept { gain.setValue (newGain); }
  35. /** Applies a new gain as a decibel value. */
  36. void setGainDecibels (FloatType newGainDecibels) noexcept { setGainLinear (Decibels::decibelsToGain<FloatType> (newGainDecibels)); }
  37. /** Returns the current gain as a linear value. */
  38. FloatType getGainLinear() const noexcept { return gain.getTargetValue(); }
  39. /** Returns the current gain in decibels. */
  40. FloatType getGainDecibels() const noexcept { return Decibels::gainToDecibels<FloatType> (getGainLinear()); }
  41. /** Sets the length of the ramp used for smoothing gain changes. */
  42. void setRampDurationSeconds (double newDurationSeconds) noexcept
  43. {
  44. if (rampDurationSeconds != newDurationSeconds)
  45. {
  46. rampDurationSeconds = newDurationSeconds;
  47. reset();
  48. }
  49. }
  50. /** Returns the ramp duration in seconds. */
  51. double getRampDurationSeconds() const noexcept { return rampDurationSeconds; }
  52. /** Returns true if the current value is currently being interpolated. */
  53. bool isSmoothing() const noexcept { return gain.isSmoothing(); }
  54. //==============================================================================
  55. /** Called before processing starts. */
  56. void prepare (const ProcessSpec& spec) noexcept
  57. {
  58. sampleRate = spec.sampleRate;
  59. reset();
  60. }
  61. /** Resets the internal state of the gain */
  62. void reset() noexcept
  63. {
  64. if (sampleRate > 0)
  65. gain.reset (sampleRate, rampDurationSeconds);
  66. }
  67. //==============================================================================
  68. /** Returns the result of processing a single sample. */
  69. template <typename SampleType>
  70. SampleType JUCE_VECTOR_CALLTYPE processSample (SampleType s) noexcept
  71. {
  72. return s * gain.getNextValue();
  73. }
  74. /** Processes the input and output buffers supplied in the processing context. */
  75. template <typename ProcessContext>
  76. void process (const ProcessContext& context) noexcept
  77. {
  78. auto&& inBlock = context.getInputBlock();
  79. auto&& outBlock = context.getOutputBlock();
  80. jassert (inBlock.getNumChannels() == outBlock.getNumChannels());
  81. jassert (inBlock.getNumSamples() == outBlock.getNumSamples());
  82. auto len = inBlock.getNumSamples();
  83. auto numChannels = inBlock.getNumChannels();
  84. if (context.isBypassed)
  85. {
  86. gain.skip (static_cast<int> (len));
  87. if (context.usesSeparateInputAndOutputBlocks())
  88. outBlock.copy (inBlock);
  89. return;
  90. }
  91. if (numChannels == 1)
  92. {
  93. auto* src = inBlock.getChannelPointer (0);
  94. auto* dst = outBlock.getChannelPointer (0);
  95. for (size_t i = 0; i < len; ++i)
  96. dst[i] = src[i] * gain.getNextValue();
  97. }
  98. else
  99. {
  100. auto* gains = static_cast<FloatType*> (alloca (sizeof (FloatType) * len));
  101. for (size_t i = 0; i < len; ++i)
  102. gains[i] = gain.getNextValue();
  103. for (size_t chan = 0; chan < numChannels; ++chan)
  104. FloatVectorOperations::multiply (outBlock.getChannelPointer (chan),
  105. inBlock.getChannelPointer (chan),
  106. gains, static_cast<int> (len));
  107. }
  108. }
  109. private:
  110. //==============================================================================
  111. LinearSmoothedValue<FloatType> gain;
  112. double sampleRate = 0, rampDurationSeconds = 0;
  113. };
  114. } // namespace dsp
  115. } // namespace juce