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.

118 lines
3.2KB

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