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.

130 lines
5.4KB

  1. /*
  2. ==============================================================================
  3. NoiseGate.cpp
  4. Created: 23 Nov 2015 3:08:33pm
  5. Author: Fabian Renn
  6. ==============================================================================
  7. */
  8. #include "../JuceLibraryCode/JuceHeader.h"
  9. #include "../../GenericEditor.h"
  10. class NoiseGate : public AudioProcessor
  11. {
  12. public:
  13. //==============================================================================
  14. NoiseGate()
  15. {
  16. addParameter (threshold = new AudioParameterFloat ("threshold", "Threshold", 0.0f, 1.0f, 0.5f));
  17. addParameter (alpha = new AudioParameterFloat ("alpha", "Alpha", 0.0f, 1.0f, 0.8f));
  18. // add single side-chain bus
  19. busArrangement.inputBuses. add (AudioProcessorBus ("Sidechain", AudioChannelSet::stereo()));
  20. }
  21. ~NoiseGate() {}
  22. //==============================================================================
  23. bool setPreferredBusArrangement (bool isInputBus, int busIndex, const AudioChannelSet& preferred) override
  24. {
  25. const int numChannels = preferred.size();
  26. const bool isMainBus = (busIndex == 0);
  27. // do not allow disabling channels
  28. if (numChannels == 0) return false;
  29. // always have the same channel layout on both input and output on the main bus
  30. if (isMainBus && (! AudioProcessor::setPreferredBusArrangement (! isInputBus, busIndex, preferred)))
  31. return false;
  32. return AudioProcessor::setPreferredBusArrangement (isInputBus, busIndex, preferred);
  33. }
  34. //==============================================================================
  35. void prepareToPlay (double /*sampleRate*/, int /*maxBlockSize*/) override { lowPassCoeff = 0.0f; sampleCountDown = 0; }
  36. void releaseResources() override {}
  37. void processBlock (AudioSampleBuffer& buffer, MidiBuffer&) override
  38. {
  39. for (int i = getTotalNumInputChannels(); i < getTotalNumOutputChannels(); ++i)
  40. buffer.clear (i, 0, buffer.getNumSamples());
  41. AudioSampleBuffer mainInputOutput = busArrangement.getBusBuffer (buffer, true, 0);
  42. AudioSampleBuffer sideChainInput = busArrangement.getBusBuffer (buffer, true, 1);
  43. float alphaCopy = *alpha;
  44. float thresholdCopy = *threshold;
  45. for (int j = 0; j < buffer.getNumSamples(); ++j)
  46. {
  47. float mixedSamples = 0.0f;
  48. for (int i = 0; i < sideChainInput.getNumChannels(); ++i)
  49. mixedSamples += sideChainInput.getReadPointer (i) [j];
  50. mixedSamples /= static_cast<float> (sideChainInput.getNumChannels());
  51. lowPassCoeff = (alphaCopy * lowPassCoeff) + ((1.0f - alphaCopy) * mixedSamples);
  52. if (lowPassCoeff >= thresholdCopy)
  53. sampleCountDown = (int) getSampleRate();
  54. // very in-effective way of doing this
  55. for (int i = 0; i < mainInputOutput.getNumChannels(); ++i)
  56. *mainInputOutput.getWritePointer (i, j) = sampleCountDown > 0 ? *mainInputOutput.getReadPointer (i, j) : 0.0f;
  57. if (sampleCountDown > 0)
  58. --sampleCountDown;
  59. }
  60. }
  61. //==============================================================================
  62. AudioProcessorEditor* createEditor() override { return new GenericEditor (*this); }
  63. bool hasEditor() const override { return true; }
  64. const String getName() const override { return "NoiseGate"; }
  65. bool acceptsMidi() const override { return false; }
  66. bool producesMidi() const override { return false; }
  67. bool silenceInProducesSilenceOut() const override { return true; }
  68. double getTailLengthSeconds() const override { return 0.0; }
  69. int getNumPrograms() override { return 1; }
  70. int getCurrentProgram() override { return 0; }
  71. void setCurrentProgram (int) override {}
  72. const String getProgramName (int) override { return ""; }
  73. void changeProgramName (int, const String&) override {}
  74. //==============================================================================
  75. void getStateInformation (MemoryBlock& destData) override
  76. {
  77. MemoryOutputStream stream (destData, true);
  78. stream.writeFloat (*threshold);
  79. stream.writeFloat (*alpha);
  80. }
  81. void setStateInformation (const void* data, int sizeInBytes) override
  82. {
  83. MemoryInputStream stream (data, static_cast<size_t> (sizeInBytes), false);
  84. threshold->setValueNotifyingHost (stream.readFloat());
  85. alpha->setValueNotifyingHost (stream.readFloat());
  86. }
  87. private:
  88. //==============================================================================
  89. AudioParameterFloat* threshold;
  90. AudioParameterFloat* alpha;
  91. int sampleCountDown;
  92. float lowPassCoeff;
  93. //==============================================================================
  94. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NoiseGate)
  95. };
  96. //==============================================================================
  97. // This creates new instances of the plugin..
  98. AudioProcessor* JUCE_CALLTYPE createPluginFilter()
  99. {
  100. return new NoiseGate();
  101. }