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.9KB

  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. #include "../../GenericEditor.h"
  21. class NoiseGate : public AudioProcessor
  22. {
  23. public:
  24. //==============================================================================
  25. //==============================================================================
  26. NoiseGate()
  27. : AudioProcessor (BusesProperties().withInput ("Input", AudioChannelSet::stereo())
  28. .withOutput ("Output", AudioChannelSet::stereo())
  29. .withInput ("Sidechain", AudioChannelSet::stereo()))
  30. {
  31. addParameter (threshold = new AudioParameterFloat ("threshold", "Threshold", 0.0f, 1.0f, 0.5f));
  32. addParameter (alpha = new AudioParameterFloat ("alpha", "Alpha", 0.0f, 1.0f, 0.8f));
  33. }
  34. ~NoiseGate() {}
  35. //==============================================================================
  36. bool isBusesLayoutSupported (const BusesLayout& layouts) const override
  37. {
  38. // the sidechain can take any layout, the main bus needs to be the same on the input and output
  39. return (layouts.getMainInputChannelSet() == layouts.getMainOutputChannelSet() &&
  40. (! layouts.getMainInputChannelSet().isDisabled()));
  41. }
  42. //==============================================================================
  43. void prepareToPlay (double /*sampleRate*/, int /*maxBlockSize*/) override { lowPassCoeff = 0.0f; sampleCountDown = 0; }
  44. void releaseResources() override {}
  45. void processBlock (AudioSampleBuffer& buffer, MidiBuffer&) override
  46. {
  47. AudioSampleBuffer mainInputOutput = getBusBuffer(buffer, true, 0);
  48. AudioSampleBuffer sideChainInput = getBusBuffer(buffer, true, 1);
  49. float alphaCopy = *alpha;
  50. float thresholdCopy = *threshold;
  51. for (int j = 0; j < buffer.getNumSamples(); ++j)
  52. {
  53. float mixedSamples = 0.0f;
  54. for (int i = 0; i < sideChainInput.getNumChannels(); ++i)
  55. mixedSamples += sideChainInput.getReadPointer (i) [j];
  56. mixedSamples /= static_cast<float> (sideChainInput.getNumChannels());
  57. lowPassCoeff = (alphaCopy * lowPassCoeff) + ((1.0f - alphaCopy) * mixedSamples);
  58. if (lowPassCoeff >= thresholdCopy)
  59. sampleCountDown = (int) getSampleRate();
  60. // very in-effective way of doing this
  61. for (int i = 0; i < mainInputOutput.getNumChannels(); ++i)
  62. *mainInputOutput.getWritePointer (i, j) = sampleCountDown > 0 ? *mainInputOutput.getReadPointer (i, j) : 0.0f;
  63. if (sampleCountDown > 0)
  64. --sampleCountDown;
  65. }
  66. }
  67. //==============================================================================
  68. AudioProcessorEditor* createEditor() override { return new GenericEditor (*this); }
  69. bool hasEditor() const override { return true; }
  70. const String getName() const override { return "NoiseGate"; }
  71. bool acceptsMidi() const override { return false; }
  72. bool producesMidi() const override { return false; }
  73. double getTailLengthSeconds() const override { return 0.0; }
  74. int getNumPrograms() override { return 1; }
  75. int getCurrentProgram() override { return 0; }
  76. void setCurrentProgram (int) override {}
  77. const String getProgramName (int) override { return ""; }
  78. void changeProgramName (int, const String&) override {}
  79. bool isVST2() const noexcept { return (wrapperType == wrapperType_VST); }
  80. //==============================================================================
  81. void getStateInformation (MemoryBlock& destData) override
  82. {
  83. MemoryOutputStream stream (destData, true);
  84. stream.writeFloat (*threshold);
  85. stream.writeFloat (*alpha);
  86. }
  87. void setStateInformation (const void* data, int sizeInBytes) override
  88. {
  89. MemoryInputStream stream (data, static_cast<size_t> (sizeInBytes), false);
  90. threshold->setValueNotifyingHost (stream.readFloat());
  91. alpha->setValueNotifyingHost (stream.readFloat());
  92. }
  93. enum
  94. {
  95. kVST2MaxChannels = 8
  96. };
  97. private:
  98. //==============================================================================
  99. AudioParameterFloat* threshold;
  100. AudioParameterFloat* alpha;
  101. int sampleCountDown;
  102. float lowPassCoeff;
  103. //==============================================================================
  104. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NoiseGate)
  105. };
  106. //==============================================================================
  107. // This creates new instances of the plugin..
  108. AudioProcessor* JUCE_CALLTYPE createPluginFilter()
  109. {
  110. return new NoiseGate();
  111. }