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.

338 lines
12KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE examples.
  4. Copyright (c) 2020 - 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: SurroundPlugin
  20. version: 1.0.0
  21. vendor: JUCE
  22. website: http://juce.com
  23. description: Surround 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, vs2019, linux_make
  29. moduleFlags: JUCE_STRICT_REFCOUNTEDPOINTER=1
  30. type: AudioProcessor
  31. mainClass: SurroundProcessor
  32. useLocalCopy: 1
  33. END_JUCE_PIP_METADATA
  34. *******************************************************************************/
  35. #pragma once
  36. //==============================================================================
  37. class ChannelClickListener
  38. {
  39. public:
  40. virtual ~ChannelClickListener() {}
  41. virtual void channelButtonClicked (int channelIndex) = 0;
  42. virtual bool isChannelActive (int channelIndex) = 0;
  43. };
  44. class SurroundEditor : public AudioProcessorEditor,
  45. private Timer
  46. {
  47. public:
  48. SurroundEditor (AudioProcessor& parent)
  49. : AudioProcessorEditor (parent),
  50. currentChannelLayout (AudioChannelSet::disabled()),
  51. layoutTitle ("LayoutTitleLabel", getLayoutName())
  52. {
  53. layoutTitle.setJustificationType (Justification::centred);
  54. addAndMakeVisible (layoutTitle);
  55. addAndMakeVisible (noChannelsLabel);
  56. setSize (600, 100);
  57. lastSuspended = ! getAudioProcessor()->isSuspended();
  58. timerCallback();
  59. startTimer (500);
  60. }
  61. void resized() override
  62. {
  63. auto r = getLocalBounds();
  64. layoutTitle.setBounds (r.removeFromBottom (16));
  65. noChannelsLabel.setBounds (r);
  66. if (channelButtons.size() > 0)
  67. {
  68. auto buttonWidth = r.getWidth() / channelButtons.size();
  69. for (auto channelButton : channelButtons)
  70. channelButton->setBounds (r.removeFromLeft (buttonWidth));
  71. }
  72. }
  73. void paint (Graphics& g) override
  74. {
  75. g.fillAll (getLookAndFeel().findColour (ResizableWindow::backgroundColourId));
  76. }
  77. void updateButton (Button* btn)
  78. {
  79. if (auto* textButton = dynamic_cast<TextButton*> (btn))
  80. {
  81. auto channelIndex = channelButtons.indexOf (textButton);
  82. if (auto* listener = dynamic_cast<ChannelClickListener*> (getAudioProcessor()))
  83. listener->channelButtonClicked (channelIndex);
  84. }
  85. }
  86. void updateGUI()
  87. {
  88. const auto& channelSet = getAudioProcessor()->getChannelLayoutOfBus (false, 0);
  89. if (channelSet != currentChannelLayout)
  90. {
  91. currentChannelLayout = channelSet;
  92. layoutTitle.setText (currentChannelLayout.getDescription(), NotificationType::dontSendNotification);
  93. channelButtons.clear();
  94. activeChannels.resize (currentChannelLayout.size());
  95. if (currentChannelLayout == AudioChannelSet::disabled())
  96. {
  97. noChannelsLabel.setVisible (true);
  98. }
  99. else
  100. {
  101. auto numChannels = currentChannelLayout.size();
  102. for (auto i = 0; i < numChannels; ++i)
  103. {
  104. auto channelName =
  105. AudioChannelSet::getAbbreviatedChannelTypeName (currentChannelLayout.getTypeOfChannel (i));
  106. TextButton* newButton;
  107. channelButtons.add (newButton = new TextButton (channelName, channelName));
  108. newButton->onClick = [this, newButton] { updateButton (newButton); };
  109. addAndMakeVisible (newButton);
  110. }
  111. noChannelsLabel.setVisible (false);
  112. resized();
  113. }
  114. if (auto* listener = dynamic_cast<ChannelClickListener*> (getAudioProcessor()))
  115. {
  116. auto activeColour = getLookAndFeel().findColour (Slider::thumbColourId);
  117. auto inactiveColour = getLookAndFeel().findColour (Slider::trackColourId);
  118. for (auto i = 0; i < activeChannels.size(); ++i)
  119. {
  120. auto isActive = listener->isChannelActive (i);
  121. activeChannels.getReference (i) = isActive;
  122. channelButtons[i]->setColour (TextButton::buttonColourId, isActive ? activeColour : inactiveColour);
  123. channelButtons[i]->repaint();
  124. }
  125. }
  126. }
  127. }
  128. private:
  129. String getLayoutName() const
  130. {
  131. if (auto* p = getAudioProcessor())
  132. return p->getChannelLayoutOfBus (false, 0).getDescription();
  133. return "Unknown";
  134. }
  135. void timerCallback() override
  136. {
  137. if (getAudioProcessor()->isSuspended() != lastSuspended)
  138. {
  139. lastSuspended = getAudioProcessor()->isSuspended();
  140. updateGUI();
  141. }
  142. if (! lastSuspended)
  143. {
  144. if (auto* listener = dynamic_cast<ChannelClickListener*> (getAudioProcessor()))
  145. {
  146. auto activeColour = getLookAndFeel().findColour (Slider::thumbColourId);
  147. auto inactiveColour = getLookAndFeel().findColour (Slider::trackColourId);
  148. for (auto i = 0; i < activeChannels.size(); ++i)
  149. {
  150. auto isActive = listener->isChannelActive (i);
  151. if (activeChannels.getReference (i) != isActive)
  152. {
  153. activeChannels.getReference (i) = isActive;
  154. channelButtons[i]->setColour (TextButton::buttonColourId, isActive ? activeColour : inactiveColour);
  155. channelButtons[i]->repaint();
  156. }
  157. }
  158. }
  159. }
  160. }
  161. AudioChannelSet currentChannelLayout;
  162. Label noChannelsLabel { "noChannelsLabel", "Input disabled" },
  163. layoutTitle;
  164. OwnedArray<TextButton> channelButtons;
  165. Array<bool> activeChannels;
  166. bool lastSuspended;
  167. };
  168. //==============================================================================
  169. class SurroundProcessor : public AudioProcessor,
  170. public ChannelClickListener,
  171. private AsyncUpdater
  172. {
  173. public:
  174. SurroundProcessor()
  175. : AudioProcessor(BusesProperties().withInput ("Input", AudioChannelSet::stereo())
  176. .withOutput ("Output", AudioChannelSet::stereo()))
  177. {}
  178. //==============================================================================
  179. void prepareToPlay (double sampleRate, int samplesPerBlock) override
  180. {
  181. channelClicked = 0;
  182. sampleOffset = static_cast<int> (std::ceil (sampleRate));
  183. auto numChannels = getChannelCountOfBus (true, 0);
  184. channelActive.resize (numChannels);
  185. alphaCoeffs .resize (numChannels);
  186. reset();
  187. triggerAsyncUpdate();
  188. ignoreUnused (samplesPerBlock);
  189. }
  190. void releaseResources() override { reset(); }
  191. void processBlock (AudioBuffer<float>& buffer, MidiBuffer&) override
  192. {
  193. for (auto ch = 0; ch < buffer.getNumChannels(); ++ch)
  194. {
  195. auto& channelTime = channelActive.getReference (ch);
  196. auto& alpha = alphaCoeffs .getReference (ch);
  197. for (auto j = 0; j < buffer.getNumSamples(); ++j)
  198. {
  199. auto sample = buffer.getReadPointer (ch)[j];
  200. alpha = (0.8f * alpha) + (0.2f * sample);
  201. if (std::abs (alpha) >= 0.1f)
  202. channelTime = static_cast<int> (getSampleRate() / 2.0);
  203. }
  204. channelTime = jmax (0, channelTime - buffer.getNumSamples());
  205. }
  206. auto fillSamples = jmin (static_cast<int> (std::ceil (getSampleRate())) - sampleOffset,
  207. buffer.getNumSamples());
  208. if (isPositiveAndBelow (channelClicked, buffer.getNumChannels()))
  209. {
  210. auto* channelBuffer = buffer.getWritePointer (channelClicked);
  211. auto freq = (float) (440.0 / getSampleRate());
  212. for (auto i = 0; i < fillSamples; ++i)
  213. channelBuffer[i] += std::sin (MathConstants<float>::twoPi * freq * static_cast<float> (sampleOffset++));
  214. }
  215. }
  216. using AudioProcessor::processBlock;
  217. //==============================================================================
  218. AudioProcessorEditor* createEditor() override { return new SurroundEditor (*this); }
  219. bool hasEditor() const override { return true; }
  220. //==============================================================================
  221. bool isBusesLayoutSupported (const BusesLayout& layouts) const override
  222. {
  223. return ((! layouts.getMainInputChannelSet() .isDiscreteLayout())
  224. && (! layouts.getMainOutputChannelSet().isDiscreteLayout())
  225. && (layouts.getMainInputChannelSet() == layouts.getMainOutputChannelSet())
  226. && (! layouts.getMainInputChannelSet().isDisabled()));
  227. }
  228. void reset() override
  229. {
  230. for (auto& channel : channelActive)
  231. channel = 0;
  232. }
  233. //==============================================================================
  234. const String getName() const override { return "Surround PlugIn"; }
  235. bool acceptsMidi() const override { return false; }
  236. bool producesMidi() const override { return false; }
  237. double getTailLengthSeconds() const override { return 0; }
  238. //==============================================================================
  239. int getNumPrograms() override { return 1; }
  240. int getCurrentProgram() override { return 0; }
  241. void setCurrentProgram (int) override {}
  242. const String getProgramName (int) override { return {}; }
  243. void changeProgramName (int, const String&) override {}
  244. //==============================================================================
  245. void getStateInformation (MemoryBlock&) override {}
  246. void setStateInformation (const void*, int) override {}
  247. void channelButtonClicked (int channelIndex) override
  248. {
  249. channelClicked = channelIndex;
  250. sampleOffset = 0;
  251. }
  252. bool isChannelActive (int channelIndex) override
  253. {
  254. return channelActive[channelIndex] > 0;
  255. }
  256. void handleAsyncUpdate() override
  257. {
  258. if (auto* editor = getActiveEditor())
  259. if (auto* surroundEditor = dynamic_cast<SurroundEditor*> (editor))
  260. surroundEditor->updateGUI();
  261. }
  262. private:
  263. Array<int> channelActive;
  264. Array<float> alphaCoeffs;
  265. int channelClicked;
  266. int sampleOffset;
  267. //==============================================================================
  268. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SurroundProcessor)
  269. };