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.

140 lines
4.8KB

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