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.

462 lines
15KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE examples.
  4. Copyright (c) 2022 - 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, vs2022, 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 ProcessorWithLevels : public AudioProcessor,
  38. private AsyncUpdater,
  39. private Timer
  40. {
  41. public:
  42. ProcessorWithLevels()
  43. : AudioProcessor (BusesProperties().withInput ("Input", AudioChannelSet::stereo())
  44. .withInput ("Aux", AudioChannelSet::stereo(), false)
  45. .withOutput ("Output", AudioChannelSet::stereo())
  46. .withOutput ("Aux", AudioChannelSet::stereo(), false))
  47. {
  48. startTimerHz (60);
  49. applyBusLayouts (getBusesLayout());
  50. }
  51. ~ProcessorWithLevels() override
  52. {
  53. stopTimer();
  54. cancelPendingUpdate();
  55. }
  56. void prepareToPlay (double, int) override
  57. {
  58. samplesToPlay = (int) getSampleRate();
  59. reset();
  60. }
  61. void processBlock (AudioBuffer<float>& audio, MidiBuffer&) override { processAudio (audio); }
  62. void processBlock (AudioBuffer<double>& audio, MidiBuffer&) override { processAudio (audio); }
  63. void releaseResources() override { reset(); }
  64. float getLevel (int bus, int channel) const
  65. {
  66. return readableLevels[(size_t) getChannelIndexInProcessBlockBuffer (true, bus, channel)];
  67. }
  68. bool isBusesLayoutSupported (const BusesLayout& layouts) const override
  69. {
  70. const auto isSetValid = [] (const AudioChannelSet& set)
  71. {
  72. return ! set.isDisabled()
  73. && ! (set.isDiscreteLayout() && set.getChannelIndexForType (AudioChannelSet::discreteChannel0) == -1);
  74. };
  75. return isSetValid (layouts.getMainOutputChannelSet())
  76. && isSetValid (layouts.getMainInputChannelSet());
  77. }
  78. void reset() override
  79. {
  80. channelClicked = 0;
  81. samplesPlayed = samplesToPlay;
  82. }
  83. bool applyBusLayouts (const BusesLayout& layouts) override
  84. {
  85. // Some very badly-behaved hosts will call this during processing!
  86. const SpinLock::ScopedLockType lock (levelMutex);
  87. const auto result = AudioProcessor::applyBusLayouts (layouts);
  88. size_t numInputChannels = 0;
  89. for (auto i = 0; i < getBusCount (true); ++i)
  90. numInputChannels += (size_t) getBus (true, i)->getLastEnabledLayout().size();
  91. incomingLevels = readableLevels = std::vector<float> (numInputChannels, 0.0f);
  92. triggerAsyncUpdate();
  93. return result;
  94. }
  95. //==============================================================================
  96. const String getName() const override { return "Surround PlugIn"; }
  97. bool acceptsMidi() const override { return false; }
  98. bool producesMidi() const override { return false; }
  99. double getTailLengthSeconds() const override { return 0; }
  100. //==============================================================================
  101. int getNumPrograms() override { return 1; }
  102. int getCurrentProgram() override { return 0; }
  103. void setCurrentProgram (int) override {}
  104. const String getProgramName (int) override { return "None"; }
  105. void changeProgramName (int, const String&) override {}
  106. //==============================================================================
  107. void getStateInformation (MemoryBlock&) override {}
  108. void setStateInformation (const void*, int) override {}
  109. void channelButtonClicked (int bus, int channelIndex)
  110. {
  111. channelClicked = getChannelIndexInProcessBlockBuffer (false, bus, channelIndex);
  112. samplesPlayed = 0;
  113. }
  114. std::function<void()> updateEditor;
  115. private:
  116. void handleAsyncUpdate() override
  117. {
  118. NullCheckedInvocation::invoke (updateEditor);
  119. }
  120. template <typename Float>
  121. void processAudio (AudioBuffer<Float>& audio)
  122. {
  123. {
  124. SpinLock::ScopedTryLockType lock (levelMutex);
  125. if (lock.isLocked())
  126. {
  127. const auto numInputChannels = (size_t) getTotalNumInputChannels();
  128. for (size_t i = 0; i < numInputChannels; ++i)
  129. {
  130. const auto minMax = audio.findMinMax ((int) i, 0, audio.getNumSamples());
  131. const auto newMax = (float) std::max (std::abs (minMax.getStart()), std::abs (minMax.getEnd()));
  132. auto& toUpdate = incomingLevels[i];
  133. toUpdate = jmax (toUpdate, newMax);
  134. }
  135. }
  136. }
  137. audio.clear (0, audio.getNumSamples());
  138. auto fillSamples = jmin (samplesToPlay - samplesPlayed, audio.getNumSamples());
  139. if (isPositiveAndBelow (channelClicked, audio.getNumChannels()))
  140. {
  141. auto* channelBuffer = audio.getWritePointer (channelClicked);
  142. auto freq = (float) (440.0 / getSampleRate());
  143. for (auto i = 0; i < fillSamples; ++i)
  144. channelBuffer[i] += std::sin (MathConstants<float>::twoPi * freq * (float) samplesPlayed++);
  145. }
  146. }
  147. void timerCallback() override
  148. {
  149. const SpinLock::ScopedLockType lock (levelMutex);
  150. for (size_t i = 0; i < readableLevels.size(); ++i)
  151. readableLevels[i] = std::max (readableLevels[i] * 0.95f, std::exchange (incomingLevels[i], 0.0f));
  152. }
  153. SpinLock levelMutex;
  154. std::vector<float> incomingLevels;
  155. std::vector<float> readableLevels;
  156. int channelClicked;
  157. int samplesPlayed;
  158. int samplesToPlay;
  159. };
  160. //==============================================================================
  161. const Colour textColour = Colours::white.withAlpha (0.8f);
  162. inline void drawBackground (Component& comp, Graphics& g)
  163. {
  164. g.setColour (comp.getLookAndFeel().findColour (ResizableWindow::backgroundColourId).darker (0.8f));
  165. g.fillRoundedRectangle (comp.getLocalBounds().toFloat(), 4.0f);
  166. }
  167. inline void configureLabel (Label& label, const AudioProcessor::Bus* layout)
  168. {
  169. const auto text = layout != nullptr
  170. ? (layout->getName() + ": " + layout->getCurrentLayout().getDescription())
  171. : "";
  172. label.setText (text, dontSendNotification);
  173. label.setJustificationType (Justification::centred);
  174. label.setColour (Label::textColourId, textColour);
  175. }
  176. class InputBusViewer : public Component,
  177. private Timer
  178. {
  179. public:
  180. InputBusViewer (ProcessorWithLevels& proc, int busNumber)
  181. : processor (proc),
  182. bus (busNumber)
  183. {
  184. configureLabel (layoutName, processor.getBus (true, bus));
  185. addAndMakeVisible (layoutName);
  186. startTimerHz (60);
  187. }
  188. ~InputBusViewer() override
  189. {
  190. stopTimer();
  191. }
  192. void paint (Graphics& g) override
  193. {
  194. drawBackground (*this, g);
  195. auto* layout = processor.getBus (true, bus);
  196. if (layout == nullptr)
  197. return;
  198. const auto channelSet = layout->getCurrentLayout();
  199. const auto numChannels = channelSet.size();
  200. Grid grid;
  201. grid.autoFlow = Grid::AutoFlow::column;
  202. grid.autoColumns = grid.autoRows = Grid::TrackInfo (Grid::Fr (1));
  203. grid.items.insertMultiple (0, GridItem(), numChannels);
  204. grid.performLayout (getLocalBounds());
  205. const auto minDb = -50.0f;
  206. const auto maxDb = 6.0f;
  207. for (auto i = 0; i < numChannels; ++i)
  208. {
  209. g.setColour (Colours::orange.darker());
  210. const auto levelInDb = Decibels::gainToDecibels (processor.getLevel (bus, i), minDb);
  211. const auto fractionOfHeight = jmap (levelInDb, minDb, maxDb, 0.0f, 1.0f);
  212. const auto bounds = grid.items[i].currentBounds;
  213. const auto trackBounds = bounds.withSizeKeepingCentre (16, bounds.getHeight() - 10).toFloat();
  214. g.fillRect (trackBounds.withHeight (trackBounds.proportionOfHeight (fractionOfHeight)).withBottomY (trackBounds.getBottom()));
  215. g.setColour (textColour);
  216. g.drawText (channelSet.getAbbreviatedChannelTypeName (channelSet.getTypeOfChannel (i)),
  217. bounds,
  218. Justification::centredBottom);
  219. }
  220. }
  221. void resized() override
  222. {
  223. layoutName.setBounds (getLocalBounds().removeFromTop (20));
  224. }
  225. int getNumChannels() const
  226. {
  227. if (auto* b = processor.getBus (true, bus))
  228. return b->getCurrentLayout().size();
  229. return 0;
  230. }
  231. private:
  232. void timerCallback() override { repaint(); }
  233. ProcessorWithLevels& processor;
  234. int bus = 0;
  235. Label layoutName;
  236. };
  237. //==============================================================================
  238. class OutputBusViewer : public Component
  239. {
  240. public:
  241. OutputBusViewer (ProcessorWithLevels& proc, int busNumber)
  242. : processor (proc),
  243. bus (busNumber)
  244. {
  245. auto* layout = processor.getBus (false, bus);
  246. configureLabel (layoutName, layout);
  247. addAndMakeVisible (layoutName);
  248. if (layout == nullptr)
  249. return;
  250. const auto& channelSet = layout->getCurrentLayout();
  251. const auto numChannels = channelSet.size();
  252. for (auto i = 0; i < numChannels; ++i)
  253. {
  254. const auto channelName = channelSet.getAbbreviatedChannelTypeName (channelSet.getTypeOfChannel (i));
  255. channelButtons.emplace_back (channelName, channelName);
  256. auto& newButton = channelButtons.back();
  257. newButton.onClick = [&proc = processor, bus = bus, i] { proc.channelButtonClicked (bus, i); };
  258. addAndMakeVisible (newButton);
  259. }
  260. resized();
  261. }
  262. void paint (Graphics& g) override
  263. {
  264. drawBackground (*this, g);
  265. }
  266. void resized() override
  267. {
  268. auto b = getLocalBounds();
  269. layoutName.setBounds (b.removeFromBottom (20));
  270. Grid grid;
  271. grid.autoFlow = Grid::AutoFlow::column;
  272. grid.autoColumns = grid.autoRows = Grid::TrackInfo (Grid::Fr (1));
  273. for (auto& channelButton : channelButtons)
  274. grid.items.add (GridItem (channelButton));
  275. grid.performLayout (b.reduced (2));
  276. }
  277. int getNumChannels() const
  278. {
  279. if (auto* b = processor.getBus (false, bus))
  280. return b->getCurrentLayout().size();
  281. return 0;
  282. }
  283. private:
  284. ProcessorWithLevels& processor;
  285. int bus = 0;
  286. Label layoutName;
  287. std::list<TextButton> channelButtons;
  288. };
  289. //==============================================================================
  290. class SurroundEditor : public AudioProcessorEditor
  291. {
  292. public:
  293. explicit SurroundEditor (ProcessorWithLevels& parent)
  294. : AudioProcessorEditor (parent),
  295. customProcessor (parent),
  296. scopedUpdateEditor (customProcessor.updateEditor, [this] { updateGUI(); })
  297. {
  298. updateGUI();
  299. setResizable (true, true);
  300. }
  301. void resized() override
  302. {
  303. auto r = getLocalBounds();
  304. doLayout (inputViewers, r.removeFromTop (proportionOfHeight (0.5f)));
  305. doLayout (outputViewers, r);
  306. }
  307. void paint (Graphics& g) override
  308. {
  309. g.fillAll (getLookAndFeel().findColour (ResizableWindow::backgroundColourId));
  310. }
  311. private:
  312. template <typename Range>
  313. void doLayout (Range& range, Rectangle<int> bounds) const
  314. {
  315. FlexBox fb;
  316. for (auto& viewer : range)
  317. {
  318. if (viewer.getNumChannels() != 0)
  319. {
  320. fb.items.add (FlexItem (viewer)
  321. .withFlex ((float) viewer.getNumChannels())
  322. .withMargin (4.0f));
  323. }
  324. }
  325. fb.performLayout (bounds);
  326. }
  327. void updateGUI()
  328. {
  329. inputViewers.clear();
  330. outputViewers.clear();
  331. const auto inputBuses = getAudioProcessor()->getBusCount (true);
  332. for (auto i = 0; i < inputBuses; ++i)
  333. {
  334. inputViewers.emplace_back (customProcessor, i);
  335. addAndMakeVisible (inputViewers.back());
  336. }
  337. const auto outputBuses = getAudioProcessor()->getBusCount (false);
  338. for (auto i = 0; i < outputBuses; ++i)
  339. {
  340. outputViewers.emplace_back (customProcessor, i);
  341. addAndMakeVisible (outputViewers.back());
  342. }
  343. const auto channels = jmax (processor.getTotalNumInputChannels(),
  344. processor.getTotalNumOutputChannels());
  345. setSize (jmax (150, channels * 40), 200);
  346. resized();
  347. }
  348. ProcessorWithLevels& customProcessor;
  349. ScopedValueSetter<std::function<void()>> scopedUpdateEditor;
  350. std::list<InputBusViewer> inputViewers;
  351. std::list<OutputBusViewer> outputViewers;
  352. };
  353. //==============================================================================
  354. struct SurroundProcessor : public ProcessorWithLevels
  355. {
  356. AudioProcessorEditor* createEditor() override { return new SurroundEditor (*this); }
  357. bool hasEditor() const override { return true; }
  358. };