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.

340 lines
12KB

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