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.

92 lines
2.7KB

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