Audio plugin host https://kx.studio/carla
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.

juce_Compressor.cpp 3.4KB

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