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.

164 lines
6.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #include "../JuceLibraryCode/JuceHeader.h"
  20. #include "SurroundEditor.h"
  21. //==============================================================================
  22. /**
  23. */
  24. class SurroundProcessor : public AudioProcessor,
  25. public ChannelClickListener,
  26. private AsyncUpdater
  27. {
  28. public:
  29. SurroundProcessor()
  30. : AudioProcessor(BusesProperties().withInput ("Input", AudioChannelSet::stereo())
  31. .withOutput ("Output", AudioChannelSet::stereo()))
  32. {}
  33. ~SurroundProcessor() {}
  34. //==============================================================================
  35. void prepareToPlay (double sampleRate, int samplesPerBlock) override
  36. {
  37. channelClicked = 0;
  38. sampleOffset = static_cast<int> (std::ceil (sampleRate));
  39. const int numChannels = getChannelCountOfBus (true, 0);
  40. channelActive.resize (numChannels);
  41. alphaCoeffs.resize (numChannels);
  42. reset();
  43. triggerAsyncUpdate();
  44. ignoreUnused (samplesPerBlock);
  45. }
  46. void releaseResources() override { reset(); }
  47. void processBlock (AudioSampleBuffer& buffer, MidiBuffer&) override
  48. {
  49. for (int ch = 0; ch < buffer.getNumChannels(); ++ch)
  50. {
  51. int& channelTime = channelActive.getReference (ch);
  52. float& alpha = alphaCoeffs.getReference (ch);
  53. for (int j = 0; j < buffer.getNumSamples(); ++j)
  54. {
  55. float sample = buffer.getReadPointer (ch)[j];
  56. alpha = (0.8f * alpha) + (0.2f * sample);
  57. if (fabsf (alpha) >= 0.1f)
  58. channelTime = static_cast<int> (getSampleRate() / 2.0);
  59. }
  60. channelTime = jmax (0, channelTime - buffer.getNumSamples());
  61. }
  62. const int fillSamples = jmin (static_cast<int> (std::ceil (getSampleRate())) - sampleOffset,
  63. buffer.getNumSamples());
  64. if (isPositiveAndBelow (channelClicked, buffer.getNumChannels()))
  65. {
  66. float* const channelBuffer = buffer.getWritePointer (channelClicked);
  67. const float freq = (float) (440.0 / getSampleRate());
  68. for (int i = 0; i < fillSamples; ++i)
  69. channelBuffer[i] += std::sin (2.0f * float_Pi * freq * static_cast<float> (sampleOffset++));
  70. }
  71. }
  72. //==============================================================================
  73. AudioProcessorEditor* createEditor() override { return new SurroundEditor (*this); }
  74. bool hasEditor() const override { return true; }
  75. //==============================================================================
  76. bool isBusesLayoutSupported (const BusesLayout& layouts) const override
  77. {
  78. return ((! layouts.getMainInputChannelSet() .isDiscreteLayout())
  79. && (! layouts.getMainOutputChannelSet().isDiscreteLayout())
  80. && (layouts.getMainInputChannelSet() == layouts.getMainOutputChannelSet())
  81. && (! layouts.getMainInputChannelSet().isDisabled()));
  82. }
  83. void reset() override
  84. {
  85. for (int i = 0; i < channelActive.size(); ++i)
  86. channelActive.getReference (i) = 0;
  87. }
  88. //==============================================================================
  89. const String getName() const override { return "Surround PlugIn"; }
  90. bool acceptsMidi() const override { return false; }
  91. bool producesMidi() const override { return false; }
  92. double getTailLengthSeconds() const override { return 0; }
  93. //==============================================================================
  94. int getNumPrograms() override { return 1; }
  95. int getCurrentProgram() override { return 0; }
  96. void setCurrentProgram (int) override {}
  97. const String getProgramName (int) override { return String(); }
  98. void changeProgramName (int , const String& ) override { }
  99. //==============================================================================
  100. void getStateInformation (MemoryBlock&) override {}
  101. void setStateInformation (const void* , int) override {}
  102. void channelButtonClicked (int channelIndex) override
  103. {
  104. channelClicked = channelIndex;
  105. sampleOffset = 0;
  106. }
  107. bool isChannelActive (int channelIndex) override
  108. {
  109. return channelActive [channelIndex] > 0;
  110. }
  111. void handleAsyncUpdate() override
  112. {
  113. if (AudioProcessorEditor* editor = getActiveEditor())
  114. if (SurroundEditor* surroundEditor = dynamic_cast<SurroundEditor*> (editor))
  115. surroundEditor->updateGUI();
  116. }
  117. private:
  118. Array<int> channelActive;
  119. Array<float> alphaCoeffs;
  120. int channelClicked;
  121. int sampleOffset;
  122. //==============================================================================
  123. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SurroundProcessor)
  124. };
  125. //==============================================================================
  126. // This creates new instances of the plugin..
  127. AudioProcessor* JUCE_CALLTYPE createPluginFilter()
  128. {
  129. return new SurroundProcessor();
  130. }