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.

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