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.

106 lines
3.6KB

  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. A simple limiter with standard threshold and release time controls, featuring
  24. two compressors and a hard clipper at 0 dB.
  25. @tags{DSP}
  26. */
  27. template <typename SampleType>
  28. class Limiter
  29. {
  30. public:
  31. //==============================================================================
  32. /** Constructor. */
  33. Limiter() = default;
  34. //==============================================================================
  35. /** Sets the threshold in dB of the limiter.*/
  36. void setThreshold (SampleType newThreshold);
  37. /** Sets the release time in milliseconds of the limiter.*/
  38. void setRelease (SampleType newRelease);
  39. //==============================================================================
  40. /** Initialises the processor. */
  41. void prepare (const ProcessSpec& spec);
  42. /** Resets the internal state variables of the processor. */
  43. void reset();
  44. //==============================================================================
  45. /** Processes the input and output samples supplied in the processing context. */
  46. template <typename ProcessContext>
  47. void process (const ProcessContext& context) noexcept
  48. {
  49. const auto& inputBlock = context.getInputBlock();
  50. auto& outputBlock = context.getOutputBlock();
  51. const auto numChannels = outputBlock.getNumChannels();
  52. const auto numSamples = outputBlock.getNumSamples();
  53. jassert (inputBlock.getNumChannels() == numChannels);
  54. jassert (inputBlock.getNumSamples() == numSamples);
  55. if (context.isBypassed)
  56. {
  57. outputBlock.copyFrom (inputBlock);
  58. return;
  59. }
  60. firstStageCompressor.process (context);
  61. auto secondContext = ProcessContextReplacing<SampleType> (outputBlock);
  62. secondStageCompressor.process (secondContext);
  63. outputBlock.multiplyBy (outputVolume);
  64. for (size_t channel = 0; channel < numChannels; ++channel)
  65. {
  66. FloatVectorOperations::clip (outputBlock.getChannelPointer (channel), outputBlock.getChannelPointer (channel),
  67. (SampleType) -1.0, (SampleType) 1.0, (int) numSamples);
  68. }
  69. }
  70. private:
  71. //==============================================================================
  72. void update();
  73. //==============================================================================
  74. Compressor<SampleType> firstStageCompressor, secondStageCompressor;
  75. SmoothedValue<SampleType, ValueSmoothingTypes::Linear> outputVolume;
  76. double sampleRate = 44100.0;
  77. SampleType thresholddB = -10.0, releaseTime = 100.0;
  78. };
  79. } // namespace dsp
  80. } // namespace juce