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.

130 lines
3.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-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. NoiseGate<SampleType>::NoiseGate()
  25. {
  26. update();
  27. RMSFilter.setLevelCalculationType (BallisticsFilterLevelCalculationType::RMS);
  28. RMSFilter.setAttackTime (static_cast<SampleType> (0.0));
  29. RMSFilter.setReleaseTime (static_cast<SampleType> (50.0));
  30. }
  31. template <typename SampleType>
  32. void NoiseGate<SampleType>::setThreshold (SampleType newValue)
  33. {
  34. thresholddB = newValue;
  35. update();
  36. }
  37. template <typename SampleType>
  38. void NoiseGate<SampleType>::setRatio (SampleType newRatio)
  39. {
  40. jassert (newRatio >= static_cast<SampleType> (1.0));
  41. ratio = newRatio;
  42. update();
  43. }
  44. template <typename SampleType>
  45. void NoiseGate<SampleType>::setAttack (SampleType newAttack)
  46. {
  47. attackTime = newAttack;
  48. update();
  49. }
  50. template <typename SampleType>
  51. void NoiseGate<SampleType>::setRelease (SampleType newRelease)
  52. {
  53. releaseTime = newRelease;
  54. update();
  55. }
  56. //==============================================================================
  57. template <typename SampleType>
  58. void NoiseGate<SampleType>::prepare (const ProcessSpec& spec)
  59. {
  60. jassert (spec.sampleRate > 0);
  61. jassert (spec.numChannels > 0);
  62. sampleRate = spec.sampleRate;
  63. RMSFilter.prepare (spec);
  64. envelopeFilter.prepare (spec);
  65. update();
  66. reset();
  67. }
  68. template <typename SampleType>
  69. void NoiseGate<SampleType>::reset()
  70. {
  71. RMSFilter.reset();
  72. envelopeFilter.reset();
  73. }
  74. //==============================================================================
  75. template <typename SampleType>
  76. SampleType NoiseGate<SampleType>::processSample (int channel, SampleType sample)
  77. {
  78. // RMS ballistics filter
  79. auto env = RMSFilter.processSample (channel, sample);
  80. // Ballistics filter
  81. env = envelopeFilter.processSample (channel, env);
  82. // VCA
  83. auto gain = (env > threshold) ? static_cast<SampleType> (1.0)
  84. : std::pow (env * thresholdInverse, currentRatio - static_cast<SampleType> (1.0));
  85. // Output
  86. return gain * sample;
  87. }
  88. template <typename SampleType>
  89. void NoiseGate<SampleType>::update()
  90. {
  91. threshold = Decibels::decibelsToGain (thresholddB, static_cast<SampleType> (-200.0));
  92. thresholdInverse = static_cast<SampleType> (1.0) / threshold;
  93. currentRatio = ratio;
  94. envelopeFilter.setAttackTime (attackTime);
  95. envelopeFilter.setReleaseTime (releaseTime);
  96. }
  97. //==============================================================================
  98. template class NoiseGate<float>;
  99. template class NoiseGate<double>;
  100. } // namespace dsp
  101. } // namespace juce