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.

99 lines
3.4KB

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