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.

146 lines
6.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #include "../JuceLibraryCode/JuceHeader.h"
  18. #include "../../GenericEditor.h"
  19. class NoiseGate : public AudioProcessor
  20. {
  21. public:
  22. //==============================================================================
  23. NoiseGate()
  24. {
  25. addParameter (threshold = new AudioParameterFloat ("threshold", "Threshold", 0.0f, 1.0f, 0.5f));
  26. addParameter (alpha = new AudioParameterFloat ("alpha", "Alpha", 0.0f, 1.0f, 0.8f));
  27. // add single side-chain bus
  28. busArrangement.inputBuses. add (AudioProcessorBus ("Sidechain In", AudioChannelSet::stereo()));
  29. busArrangement.outputBuses.add (AudioProcessorBus ("Sidechain Out", AudioChannelSet::stereo()));
  30. }
  31. ~NoiseGate() {}
  32. //==============================================================================
  33. bool setPreferredBusArrangement (bool isInputBus, int busIndex, const AudioChannelSet& preferred) override
  34. {
  35. const int numChannels = preferred.size();
  36. // do not allow disabling channels
  37. if (numChannels == 0) return false;
  38. // only allow stereo on the side-chain bus
  39. if (busIndex == 1 && numChannels != 2) return false;
  40. // always have the same channel layout on both input and output on the main bus
  41. if (! AudioProcessor::setPreferredBusArrangement (! isInputBus, busIndex, preferred))
  42. return false;
  43. return AudioProcessor::setPreferredBusArrangement (isInputBus, busIndex, preferred);
  44. }
  45. //==============================================================================
  46. void prepareToPlay (double /*sampleRate*/, int /*maxBlockSize*/) override { lowPassCoeff = 0.0f; sampleCountDown = 0; }
  47. void releaseResources() override {}
  48. void processBlock (AudioSampleBuffer& buffer, MidiBuffer&) override
  49. {
  50. for (int i = getTotalNumInputChannels(); i < getTotalNumOutputChannels(); ++i)
  51. buffer.clear (i, 0, buffer.getNumSamples());
  52. AudioSampleBuffer mainInputOutput = busArrangement.getBusBuffer (buffer, true, 0);
  53. AudioSampleBuffer sideChainInput = busArrangement.getBusBuffer (buffer, true, 1);
  54. float alphaCopy = *alpha;
  55. float thresholdCopy = *threshold;
  56. for (int j = 0; j < buffer.getNumSamples(); ++j)
  57. {
  58. float mixedSamples = 0.0f;
  59. for (int i = 0; i < sideChainInput.getNumChannels(); ++i)
  60. mixedSamples += sideChainInput.getReadPointer (i) [j];
  61. mixedSamples /= static_cast<float> (sideChainInput.getNumChannels());
  62. lowPassCoeff = (alphaCopy * lowPassCoeff) + ((1.0f - alphaCopy) * mixedSamples);
  63. if (lowPassCoeff >= thresholdCopy)
  64. sampleCountDown = (int) getSampleRate();
  65. // very in-effective way of doing this
  66. for (int i = 0; i < mainInputOutput.getNumChannels(); ++i)
  67. *mainInputOutput.getWritePointer (i, j) = sampleCountDown > 0 ? *mainInputOutput.getReadPointer (i, j) : 0.0f;
  68. if (sampleCountDown > 0)
  69. --sampleCountDown;
  70. }
  71. }
  72. //==============================================================================
  73. AudioProcessorEditor* createEditor() override { return new GenericEditor (*this); }
  74. bool hasEditor() const override { return true; }
  75. const String getName() const override { return "NoiseGate"; }
  76. bool acceptsMidi() const override { return false; }
  77. bool producesMidi() const override { return false; }
  78. double getTailLengthSeconds() const override { return 0.0; }
  79. int getNumPrograms() override { return 1; }
  80. int getCurrentProgram() override { return 0; }
  81. void setCurrentProgram (int) override {}
  82. const String getProgramName (int) override { return ""; }
  83. void changeProgramName (int, const String&) override {}
  84. //==============================================================================
  85. void getStateInformation (MemoryBlock& destData) override
  86. {
  87. MemoryOutputStream stream (destData, true);
  88. stream.writeFloat (*threshold);
  89. stream.writeFloat (*alpha);
  90. }
  91. void setStateInformation (const void* data, int sizeInBytes) override
  92. {
  93. MemoryInputStream stream (data, static_cast<size_t> (sizeInBytes), false);
  94. threshold->setValueNotifyingHost (stream.readFloat());
  95. alpha->setValueNotifyingHost (stream.readFloat());
  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. }