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.

152 lines
6.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE examples.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. The code included in this file is provided under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  7. To use, copy, modify, and/or distribute this software for any purpose with or
  8. without fee is hereby granted provided that the above copyright notice and
  9. this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES,
  11. WHETHER EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR
  12. PURPOSE, ARE DISCLAIMED.
  13. ==============================================================================
  14. */
  15. /*******************************************************************************
  16. The block below describes the properties of this PIP. A PIP is a short snippet
  17. of code that can be read by the Projucer and used to generate a JUCE project.
  18. BEGIN_JUCE_PIP_METADATA
  19. name: NoiseGatePlugin
  20. version: 1.0.0
  21. vendor: JUCE
  22. website: http://juce.com
  23. description: Noise gate audio plugin.
  24. dependencies: juce_audio_basics, juce_audio_devices, juce_audio_formats,
  25. juce_audio_plugin_client, juce_audio_processors,
  26. juce_audio_utils, juce_core, juce_data_structures,
  27. juce_events, juce_graphics, juce_gui_basics, juce_gui_extra
  28. exporters: xcode_mac, vs2022
  29. moduleFlags: JUCE_STRICT_REFCOUNTEDPOINTER=1
  30. type: AudioProcessor
  31. mainClass: NoiseGate
  32. useLocalCopy: 1
  33. END_JUCE_PIP_METADATA
  34. *******************************************************************************/
  35. #pragma once
  36. //==============================================================================
  37. class NoiseGate : public AudioProcessor
  38. {
  39. public:
  40. //==============================================================================
  41. NoiseGate()
  42. : AudioProcessor (BusesProperties().withInput ("Input", AudioChannelSet::stereo())
  43. .withOutput ("Output", AudioChannelSet::stereo())
  44. .withInput ("Sidechain", AudioChannelSet::stereo()))
  45. {
  46. addParameter (threshold = new AudioParameterFloat ({ "threshold", 1 }, "Threshold", 0.0f, 1.0f, 0.5f));
  47. addParameter (alpha = new AudioParameterFloat ({ "alpha", 1 }, "Alpha", 0.0f, 1.0f, 0.8f));
  48. }
  49. //==============================================================================
  50. bool isBusesLayoutSupported (const BusesLayout& layouts) const override
  51. {
  52. // the sidechain can take any layout, the main bus needs to be the same on the input and output
  53. return layouts.getMainInputChannelSet() == layouts.getMainOutputChannelSet()
  54. && ! layouts.getMainInputChannelSet().isDisabled();
  55. }
  56. //==============================================================================
  57. void prepareToPlay (double, int) override { lowPassCoeff = 0.0f; sampleCountDown = 0; }
  58. void releaseResources() override {}
  59. void processBlock (AudioBuffer<float>& buffer, MidiBuffer&) override
  60. {
  61. auto mainInputOutput = getBusBuffer (buffer, true, 0);
  62. auto sideChainInput = getBusBuffer (buffer, true, 1);
  63. auto alphaCopy = alpha->get();
  64. auto thresholdCopy = threshold->get();
  65. for (int j = 0; j < buffer.getNumSamples(); ++j)
  66. {
  67. auto mixedSamples = 0.0f;
  68. for (int i = 0; i < sideChainInput.getNumChannels(); ++i)
  69. mixedSamples += sideChainInput.getReadPointer (i)[j];
  70. mixedSamples /= static_cast<float> (sideChainInput.getNumChannels());
  71. lowPassCoeff = (alphaCopy * lowPassCoeff) + ((1.0f - alphaCopy) * mixedSamples);
  72. if (lowPassCoeff >= thresholdCopy)
  73. sampleCountDown = (int) getSampleRate();
  74. // very in-effective way of doing this
  75. for (int i = 0; i < mainInputOutput.getNumChannels(); ++i)
  76. *mainInputOutput.getWritePointer (i, j) = sampleCountDown > 0 ? *mainInputOutput.getReadPointer (i, j) : 0.0f;
  77. if (sampleCountDown > 0)
  78. --sampleCountDown;
  79. }
  80. }
  81. using AudioProcessor::processBlock;
  82. //==============================================================================
  83. AudioProcessorEditor* createEditor() override { return new GenericAudioProcessorEditor (*this); }
  84. bool hasEditor() const override { return true; }
  85. const String getName() const override { return "NoiseGate"; }
  86. bool acceptsMidi() const override { return false; }
  87. bool producesMidi() const override { return false; }
  88. double getTailLengthSeconds() const override { return 0.0; }
  89. int getNumPrograms() override { return 1; }
  90. int getCurrentProgram() override { return 0; }
  91. void setCurrentProgram (int) override {}
  92. const String getProgramName (int) override { return "None"; }
  93. void changeProgramName (int, const String&) override {}
  94. bool isVST2() const noexcept { return (wrapperType == wrapperType_VST); }
  95. //==============================================================================
  96. void getStateInformation (MemoryBlock& destData) override
  97. {
  98. MemoryOutputStream stream (destData, true);
  99. stream.writeFloat (*threshold);
  100. stream.writeFloat (*alpha);
  101. }
  102. void setStateInformation (const void* data, int sizeInBytes) override
  103. {
  104. MemoryInputStream stream (data, static_cast<size_t> (sizeInBytes), false);
  105. threshold->setValueNotifyingHost (stream.readFloat());
  106. alpha->setValueNotifyingHost (stream.readFloat());
  107. }
  108. private:
  109. //==============================================================================
  110. AudioParameterFloat* threshold;
  111. AudioParameterFloat* alpha;
  112. int sampleCountDown;
  113. float lowPassCoeff;
  114. //==============================================================================
  115. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NoiseGate)
  116. };