Audio plugin host https://kx.studio/carla
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.

juce_Gain.h 5.1KB

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