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.

119 lines
3.4KB

  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. template <typename SampleType>
  22. Compressor<SampleType>::Compressor()
  23. {
  24. update();
  25. }
  26. //==============================================================================
  27. template <typename SampleType>
  28. void Compressor<SampleType>::setThreshold (SampleType newThreshold)
  29. {
  30. thresholddB = newThreshold;
  31. update();
  32. }
  33. template <typename SampleType>
  34. void Compressor<SampleType>::setRatio (SampleType newRatio)
  35. {
  36. jassert (newRatio >= static_cast<SampleType> (1.0));
  37. ratio = newRatio;
  38. update();
  39. }
  40. template <typename SampleType>
  41. void Compressor<SampleType>::setAttack (SampleType newAttack)
  42. {
  43. attackTime = newAttack;
  44. update();
  45. }
  46. template <typename SampleType>
  47. void Compressor<SampleType>::setRelease (SampleType newRelease)
  48. {
  49. releaseTime = newRelease;
  50. update();
  51. }
  52. //==============================================================================
  53. template <typename SampleType>
  54. void Compressor<SampleType>::prepare (const ProcessSpec& spec)
  55. {
  56. jassert (spec.sampleRate > 0);
  57. jassert (spec.numChannels > 0);
  58. sampleRate = spec.sampleRate;
  59. envelopeFilter.prepare (spec);
  60. update();
  61. reset();
  62. }
  63. template <typename SampleType>
  64. void Compressor<SampleType>::reset()
  65. {
  66. envelopeFilter.reset();
  67. }
  68. //==============================================================================
  69. template <typename SampleType>
  70. SampleType Compressor<SampleType>::processSample (int channel, SampleType inputValue)
  71. {
  72. // Ballistics filter with peak rectifier
  73. auto env = envelopeFilter.processSample (channel, inputValue);
  74. // VCA
  75. auto gain = (env < threshold) ? static_cast<SampleType> (1.0)
  76. : std::pow (env * thresholdInverse, ratioInverse - static_cast<SampleType> (1.0));
  77. // Output
  78. return gain * inputValue;
  79. }
  80. template <typename SampleType>
  81. void Compressor<SampleType>::update()
  82. {
  83. threshold = Decibels::decibelsToGain (thresholddB, static_cast<SampleType> (-200.0));
  84. thresholdInverse = static_cast<SampleType> (1.0) / threshold;
  85. ratioInverse = static_cast<SampleType> (1.0) / ratio;
  86. envelopeFilter.setAttackTime (attackTime);
  87. envelopeFilter.setReleaseTime (releaseTime);
  88. }
  89. //==============================================================================
  90. template class Compressor<float>;
  91. template class Compressor<double>;
  92. } // namespace juce::dsp