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.

141 lines
5.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #include "../JuceLibraryCode/JuceHeader.h"
  20. class NoiseGate : public AudioProcessor
  21. {
  22. public:
  23. //==============================================================================
  24. //==============================================================================
  25. NoiseGate()
  26. : AudioProcessor (BusesProperties().withInput ("Input", AudioChannelSet::stereo())
  27. .withOutput ("Output", AudioChannelSet::stereo())
  28. .withInput ("Sidechain", AudioChannelSet::stereo()))
  29. {
  30. addParameter (threshold = new AudioParameterFloat ("threshold", "Threshold", 0.0f, 1.0f, 0.5f));
  31. addParameter (alpha = new AudioParameterFloat ("alpha", "Alpha", 0.0f, 1.0f, 0.8f));
  32. }
  33. ~NoiseGate() {}
  34. //==============================================================================
  35. bool isBusesLayoutSupported (const BusesLayout& layouts) const override
  36. {
  37. // the sidechain can take any layout, the main bus needs to be the same on the input and output
  38. return layouts.getMainInputChannelSet() == layouts.getMainOutputChannelSet()
  39. && ! layouts.getMainInputChannelSet().isDisabled();
  40. }
  41. //==============================================================================
  42. void prepareToPlay (double /*sampleRate*/, int /*maxBlockSize*/) override { lowPassCoeff = 0.0f; sampleCountDown = 0; }
  43. void releaseResources() override {}
  44. void processBlock (AudioBuffer<float>& buffer, MidiBuffer&) override
  45. {
  46. auto mainInputOutput = getBusBuffer (buffer, true, 0);
  47. auto sideChainInput = getBusBuffer (buffer, true, 1);
  48. float alphaCopy = *alpha;
  49. float thresholdCopy = *threshold;
  50. for (int j = 0; j < buffer.getNumSamples(); ++j)
  51. {
  52. float mixedSamples = 0.0f;
  53. for (int i = 0; i < sideChainInput.getNumChannels(); ++i)
  54. mixedSamples += sideChainInput.getReadPointer (i) [j];
  55. mixedSamples /= static_cast<float> (sideChainInput.getNumChannels());
  56. lowPassCoeff = (alphaCopy * lowPassCoeff) + ((1.0f - alphaCopy) * mixedSamples);
  57. if (lowPassCoeff >= thresholdCopy)
  58. sampleCountDown = (int) getSampleRate();
  59. // very in-effective way of doing this
  60. for (int i = 0; i < mainInputOutput.getNumChannels(); ++i)
  61. *mainInputOutput.getWritePointer (i, j) = sampleCountDown > 0 ? *mainInputOutput.getReadPointer (i, j) : 0.0f;
  62. if (sampleCountDown > 0)
  63. --sampleCountDown;
  64. }
  65. }
  66. //==============================================================================
  67. AudioProcessorEditor* createEditor() override { return new GenericAudioProcessorEditor (this); }
  68. bool hasEditor() const override { return true; }
  69. const String getName() const override { return "NoiseGate"; }
  70. bool acceptsMidi() const override { return false; }
  71. bool producesMidi() const override { return false; }
  72. double getTailLengthSeconds() const override { return 0.0; }
  73. int getNumPrograms() override { return 1; }
  74. int getCurrentProgram() override { return 0; }
  75. void setCurrentProgram (int) override {}
  76. const String getProgramName (int) override { return ""; }
  77. void changeProgramName (int, const String&) override {}
  78. bool isVST2() const noexcept { return (wrapperType == wrapperType_VST); }
  79. //==============================================================================
  80. void getStateInformation (MemoryBlock& destData) override
  81. {
  82. MemoryOutputStream stream (destData, true);
  83. stream.writeFloat (*threshold);
  84. stream.writeFloat (*alpha);
  85. }
  86. void setStateInformation (const void* data, int sizeInBytes) override
  87. {
  88. MemoryInputStream stream (data, static_cast<size_t> (sizeInBytes), false);
  89. threshold->setValueNotifyingHost (stream.readFloat());
  90. alpha->setValueNotifyingHost (stream.readFloat());
  91. }
  92. enum
  93. {
  94. kVST2MaxChannels = 8
  95. };
  96. private:
  97. //==============================================================================
  98. AudioParameterFloat* threshold;
  99. AudioParameterFloat* alpha;
  100. int sampleCountDown;
  101. float lowPassCoeff;
  102. //==============================================================================
  103. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NoiseGate)
  104. };
  105. //==============================================================================
  106. // This creates new instances of the plugin..
  107. AudioProcessor* JUCE_CALLTYPE createPluginFilter()
  108. {
  109. return new NoiseGate();
  110. }