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.

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