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.

96 lines
2.9KB

  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. void Limiter<SampleType>::setThreshold (SampleType newThreshold)
  23. {
  24. thresholddB = newThreshold;
  25. update();
  26. }
  27. template <typename SampleType>
  28. void Limiter<SampleType>::setRelease (SampleType newRelease)
  29. {
  30. releaseTime = newRelease;
  31. update();
  32. }
  33. //==============================================================================
  34. template <typename SampleType>
  35. void Limiter<SampleType>::prepare (const ProcessSpec& spec)
  36. {
  37. jassert (spec.sampleRate > 0);
  38. jassert (spec.numChannels > 0);
  39. sampleRate = spec.sampleRate;
  40. firstStageCompressor.prepare (spec);
  41. secondStageCompressor.prepare (spec);
  42. update();
  43. reset();
  44. }
  45. template <typename SampleType>
  46. void Limiter<SampleType>::reset()
  47. {
  48. firstStageCompressor.reset();
  49. secondStageCompressor.reset();
  50. outputVolume.reset (sampleRate, 0.001);
  51. }
  52. //==============================================================================
  53. template <typename SampleType>
  54. void Limiter<SampleType>::update()
  55. {
  56. firstStageCompressor.setThreshold ((SampleType) -10.0);
  57. firstStageCompressor.setRatio ((SampleType) 4.0);
  58. firstStageCompressor.setAttack ((SampleType) 2.0);
  59. firstStageCompressor.setRelease ((SampleType) 200.0);
  60. secondStageCompressor.setThreshold (thresholddB);
  61. secondStageCompressor.setRatio ((SampleType) 1000.0);
  62. secondStageCompressor.setAttack ((SampleType) 0.001);
  63. secondStageCompressor.setRelease (releaseTime);
  64. auto ratioInverse = (SampleType) (1.0 / 4.0);
  65. auto gain = (SampleType) std::pow (10.0, 10.0 * (1.0 - ratioInverse) / 40.0);
  66. gain *= Decibels::decibelsToGain (-thresholddB, (SampleType) -100.0);
  67. outputVolume.setTargetValue (gain);
  68. }
  69. //==============================================================================
  70. template class Limiter<float>;
  71. template class Limiter<double>;
  72. } // namespace juce::dsp