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.

123 lines
3.3KB

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