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.

104 lines
3.5KB

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