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.

108 lines
3.7KB

  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 compressor with standard threshold, ratio, attack time and release time
  22. controls.
  23. @tags{DSP}
  24. */
  25. template <typename SampleType>
  26. class Compressor
  27. {
  28. public:
  29. //==============================================================================
  30. /** Constructor. */
  31. Compressor();
  32. //==============================================================================
  33. /** Sets the threshold in dB of the compressor.*/
  34. void setThreshold (SampleType newThreshold);
  35. /** Sets the ratio of the compressor (must be higher or equal to 1).*/
  36. void setRatio (SampleType newRatio);
  37. /** Sets the attack time in milliseconds of the compressor.*/
  38. void setAttack (SampleType newAttack);
  39. /** Sets the release time in milliseconds of the compressor.*/
  40. void setRelease (SampleType newRelease);
  41. //==============================================================================
  42. /** Initialises the processor. */
  43. void prepare (const ProcessSpec& spec);
  44. /** Resets the internal state variables of the processor. */
  45. void reset();
  46. //==============================================================================
  47. /** Processes the input and output samples supplied in the processing context. */
  48. template <typename ProcessContext>
  49. void process (const ProcessContext& context) noexcept
  50. {
  51. const auto& inputBlock = context.getInputBlock();
  52. auto& outputBlock = context.getOutputBlock();
  53. const auto numChannels = outputBlock.getNumChannels();
  54. const auto numSamples = outputBlock.getNumSamples();
  55. jassert (inputBlock.getNumChannels() == numChannels);
  56. jassert (inputBlock.getNumSamples() == numSamples);
  57. if (context.isBypassed)
  58. {
  59. outputBlock.copyFrom (inputBlock);
  60. return;
  61. }
  62. for (size_t channel = 0; channel < numChannels; ++channel)
  63. {
  64. auto* inputSamples = inputBlock .getChannelPointer (channel);
  65. auto* outputSamples = outputBlock.getChannelPointer (channel);
  66. for (size_t i = 0; i < numSamples; ++i)
  67. outputSamples[i] = processSample ((int) channel, inputSamples[i]);
  68. }
  69. }
  70. /** Performs the processing operation on a single sample at a time. */
  71. SampleType processSample (int channel, SampleType inputValue);
  72. private:
  73. //==============================================================================
  74. void update();
  75. //==============================================================================
  76. SampleType threshold, thresholdInverse, ratioInverse;
  77. BallisticsFilter<SampleType> envelopeFilter;
  78. double sampleRate = 44100.0;
  79. SampleType thresholddB = 0.0, ratio = 1.0, attackTime = 1.0, releaseTime = 100.0;
  80. };
  81. } // namespace juce::dsp