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.

162 lines
6.2KB

  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 "SurroundEditor.h"
  19. //==============================================================================
  20. /**
  21. */
  22. class SurroundProcessor : public AudioProcessor,
  23. public ChannelClickListener,
  24. private AsyncUpdater
  25. {
  26. public:
  27. SurroundProcessor()
  28. : AudioProcessor(BusesProperties().withInput ("Input", AudioChannelSet::stereo())
  29. .withOutput ("Output", AudioChannelSet::stereo()))
  30. {}
  31. ~SurroundProcessor() {}
  32. //==============================================================================
  33. void prepareToPlay (double sampleRate, int samplesPerBlock) override
  34. {
  35. channelClicked = 0;
  36. sampleOffset = static_cast<int> (std::ceil (sampleRate));
  37. const int numChannels = getChannelCountOfBus (true, 0);
  38. channelActive.resize (numChannels);
  39. alphaCoeffs.resize (numChannels);
  40. reset();
  41. triggerAsyncUpdate();
  42. ignoreUnused (samplesPerBlock);
  43. }
  44. void releaseResources() override { reset(); }
  45. void processBlock (AudioSampleBuffer& buffer, MidiBuffer&) override
  46. {
  47. for (int ch = 0; ch < buffer.getNumChannels(); ++ch)
  48. {
  49. int& channelTime = channelActive.getReference (ch);
  50. float& alpha = alphaCoeffs.getReference (ch);
  51. for (int j = 0; j < buffer.getNumSamples(); ++j)
  52. {
  53. float sample = buffer.getReadPointer (ch)[j];
  54. alpha = (0.8f * alpha) + (0.2f * sample);
  55. if (fabsf (alpha) >= 0.1f)
  56. channelTime = static_cast<int> (getSampleRate() / 2.0);
  57. }
  58. channelTime = jmax (0, channelTime - buffer.getNumSamples());
  59. }
  60. const int fillSamples = jmin (static_cast<int> (std::ceil (getSampleRate())) - sampleOffset,
  61. buffer.getNumSamples());
  62. if (isPositiveAndBelow (channelClicked, buffer.getNumChannels()))
  63. {
  64. float* const channelBuffer = buffer.getWritePointer (channelClicked);
  65. const float freq = (float) (440.0 / getSampleRate());
  66. for (int i = 0; i < fillSamples; ++i)
  67. channelBuffer[i] += std::sin (2.0f * float_Pi * freq * static_cast<float> (sampleOffset++));
  68. }
  69. }
  70. //==============================================================================
  71. AudioProcessorEditor* createEditor() override { return new SurroundEditor (*this); }
  72. bool hasEditor() const override { return true; }
  73. //==============================================================================
  74. bool isBusesLayoutSupported (const BusesLayout& layouts) const override
  75. {
  76. return ((! layouts.getMainInputChannelSet() .isDiscreteLayout())
  77. && (! layouts.getMainOutputChannelSet().isDiscreteLayout())
  78. && (layouts.getMainInputChannelSet() == layouts.getMainOutputChannelSet())
  79. && (! layouts.getMainInputChannelSet().isDisabled()));
  80. }
  81. void reset() override
  82. {
  83. for (int i = 0; i < channelActive.size(); ++i)
  84. channelActive.getReference (i) = 0;
  85. }
  86. //==============================================================================
  87. const String getName() const override { return "Surround PlugIn"; }
  88. bool acceptsMidi() const override { return false; }
  89. bool producesMidi() const override { return false; }
  90. double getTailLengthSeconds() const override { return 0; }
  91. //==============================================================================
  92. int getNumPrograms() override { return 1; }
  93. int getCurrentProgram() override { return 0; }
  94. void setCurrentProgram (int) override {}
  95. const String getProgramName (int) override { return String(); }
  96. void changeProgramName (int , const String& ) override { }
  97. //==============================================================================
  98. void getStateInformation (MemoryBlock&) override {}
  99. void setStateInformation (const void* , int) override {}
  100. void channelButtonClicked (int channelIndex) override
  101. {
  102. channelClicked = channelIndex;
  103. sampleOffset = 0;
  104. }
  105. bool isChannelActive (int channelIndex) override
  106. {
  107. return channelActive [channelIndex] > 0;
  108. }
  109. void handleAsyncUpdate() override
  110. {
  111. if (AudioProcessorEditor* editor = getActiveEditor())
  112. if (SurroundEditor* surroundEditor = dynamic_cast<SurroundEditor*> (editor))
  113. surroundEditor->updateGUI();
  114. }
  115. private:
  116. Array<int> channelActive;
  117. Array<float> alphaCoeffs;
  118. int channelClicked;
  119. int sampleOffset;
  120. //==============================================================================
  121. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SurroundProcessor)
  122. };
  123. //==============================================================================
  124. // This creates new instances of the plugin..
  125. AudioProcessor* JUCE_CALLTYPE createPluginFilter()
  126. {
  127. return new SurroundProcessor();
  128. }