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.

127 lines
3.5KB

  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. NoiseGate<SampleType>::NoiseGate()
  23. {
  24. update();
  25. RMSFilter.setLevelCalculationType (BallisticsFilterLevelCalculationType::RMS);
  26. RMSFilter.setAttackTime (static_cast<SampleType> (0.0));
  27. RMSFilter.setReleaseTime (static_cast<SampleType> (50.0));
  28. }
  29. template <typename SampleType>
  30. void NoiseGate<SampleType>::setThreshold (SampleType newValue)
  31. {
  32. thresholddB = newValue;
  33. update();
  34. }
  35. template <typename SampleType>
  36. void NoiseGate<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 NoiseGate<SampleType>::setAttack (SampleType newAttack)
  44. {
  45. attackTime = newAttack;
  46. update();
  47. }
  48. template <typename SampleType>
  49. void NoiseGate<SampleType>::setRelease (SampleType newRelease)
  50. {
  51. releaseTime = newRelease;
  52. update();
  53. }
  54. //==============================================================================
  55. template <typename SampleType>
  56. void NoiseGate<SampleType>::prepare (const ProcessSpec& spec)
  57. {
  58. jassert (spec.sampleRate > 0);
  59. jassert (spec.numChannels > 0);
  60. sampleRate = spec.sampleRate;
  61. RMSFilter.prepare (spec);
  62. envelopeFilter.prepare (spec);
  63. update();
  64. reset();
  65. }
  66. template <typename SampleType>
  67. void NoiseGate<SampleType>::reset()
  68. {
  69. RMSFilter.reset();
  70. envelopeFilter.reset();
  71. }
  72. //==============================================================================
  73. template <typename SampleType>
  74. SampleType NoiseGate<SampleType>::processSample (int channel, SampleType sample)
  75. {
  76. // RMS ballistics filter
  77. auto env = RMSFilter.processSample (channel, sample);
  78. // Ballistics filter
  79. env = envelopeFilter.processSample (channel, env);
  80. // VCA
  81. auto gain = (env > threshold) ? static_cast<SampleType> (1.0)
  82. : std::pow (env * thresholdInverse, currentRatio - static_cast<SampleType> (1.0));
  83. // Output
  84. return gain * sample;
  85. }
  86. template <typename SampleType>
  87. void NoiseGate<SampleType>::update()
  88. {
  89. threshold = Decibels::decibelsToGain (thresholddB, static_cast<SampleType> (-200.0));
  90. thresholdInverse = static_cast<SampleType> (1.0) / threshold;
  91. currentRatio = ratio;
  92. envelopeFilter.setAttackTime (attackTime);
  93. envelopeFilter.setReleaseTime (releaseTime);
  94. }
  95. //==============================================================================
  96. template class NoiseGate<float>;
  97. template class NoiseGate<double>;
  98. } // namespace juce::dsp