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
5.7KB

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