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.

115 lines
3.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - 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 the technical preview this file cannot be licensed commercially.
  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. // Ballistics filter with peak rectifier
  70. auto env = envelopeFilter.processSample (channel, inputValue);
  71. // VCA
  72. auto gain = (env < threshold) ? static_cast<SampleType> (1.0)
  73. : std::pow (env * thresholdInverse, ratioInverse - static_cast<SampleType> (1.0));
  74. // Output
  75. return gain * inputValue;
  76. }
  77. template <typename SampleType>
  78. void Compressor<SampleType>::update()
  79. {
  80. threshold = Decibels::decibelsToGain (thresholddB, static_cast<SampleType> (-200.0));
  81. thresholdInverse = static_cast<SampleType> (1.0) / threshold;
  82. ratioInverse = static_cast<SampleType> (1.0) / ratio;
  83. envelopeFilter.setAttackTime (attackTime);
  84. envelopeFilter.setReleaseTime (releaseTime);
  85. }
  86. //==============================================================================
  87. template class Compressor<float>;
  88. template class Compressor<double>;
  89. } // namespace dsp
  90. } // namespace juce