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.

111 lines
3.8KB

  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. A simple noise gate with standard threshold, ratio, attack time and
  24. release time controls. Can be used as an expander if the ratio is low.
  25. @tags{DSP}
  26. */
  27. template <typename SampleType>
  28. class NoiseGate
  29. {
  30. public:
  31. //==============================================================================
  32. /** Constructor. */
  33. NoiseGate();
  34. //==============================================================================
  35. /** Sets the threshold in dB of the noise-gate.*/
  36. void setThreshold (SampleType newThreshold);
  37. /** Sets the ratio of the noise-gate (must be higher or equal to 1).*/
  38. void setRatio (SampleType newRatio);
  39. /** Sets the attack time in milliseconds of the noise-gate.*/
  40. void setAttack (SampleType newAttack);
  41. /** Sets the release time in milliseconds of the noise-gate.*/
  42. void setRelease (SampleType newRelease);
  43. //==============================================================================
  44. /** Initialises the processor. */
  45. void prepare (const ProcessSpec& spec);
  46. /** Resets the internal state variables of the processor. */
  47. void reset();
  48. //==============================================================================
  49. /** Processes the input and output samples supplied in the processing context. */
  50. template <typename ProcessContext>
  51. void process (const ProcessContext& context) noexcept
  52. {
  53. const auto& inputBlock = context.getInputBlock();
  54. auto& outputBlock = context.getOutputBlock();
  55. const auto numChannels = outputBlock.getNumChannels();
  56. const auto numSamples = outputBlock.getNumSamples();
  57. jassert (inputBlock.getNumChannels() == numChannels);
  58. jassert (inputBlock.getNumSamples() == numSamples);
  59. if (context.isBypassed)
  60. {
  61. outputBlock.copyFrom (inputBlock);
  62. return;
  63. }
  64. for (size_t channel = 0; channel < numChannels; ++channel)
  65. {
  66. auto* inputSamples = inputBlock .getChannelPointer (channel);
  67. auto* outputSamples = outputBlock.getChannelPointer (channel);
  68. for (size_t i = 0; i < numSamples; ++i)
  69. outputSamples[i] = processSample ((int) channel, inputSamples[i]);
  70. }
  71. }
  72. /** Performs the processing operation on a single sample at a time. */
  73. SampleType processSample (int channel, SampleType inputValue);
  74. private:
  75. //==============================================================================
  76. void update();
  77. //==============================================================================
  78. SampleType threshold, thresholdInverse, currentRatio;
  79. BallisticsFilter<SampleType> envelopeFilter, RMSFilter;
  80. double sampleRate = 44100.0;
  81. SampleType thresholddB = -100, ratio = 10.0, attackTime = 1.0, releaseTime = 100.0;
  82. };
  83. } // namespace dsp
  84. } // namespace juce