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.

570 lines
22KB

  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: InterAppAudioEffectPlugin
  20. version: 1.0.0
  21. vendor: JUCE
  22. website: http://juce.com
  23. description: Inter-app audio effect 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_iphone
  29. type: AudioProcessor
  30. mainClass: IAAEffectProcessor
  31. useLocalCopy: 1
  32. END_JUCE_PIP_METADATA
  33. *******************************************************************************/
  34. #pragma once
  35. #include <array>
  36. //==============================================================================
  37. // A very simple decaying meter.
  38. class SimpleMeter : public Component,
  39. private Timer
  40. {
  41. public:
  42. SimpleMeter()
  43. {
  44. startTimerHz (30);
  45. }
  46. //==============================================================================
  47. void paint (Graphics& g) override
  48. {
  49. g.fillAll (Colours::transparentBlack);
  50. auto area = g.getClipBounds();
  51. g.setColour (getLookAndFeel().findColour (Slider::thumbColourId));
  52. g.fillRoundedRectangle (area.toFloat(), 6.0);
  53. auto unfilledHeight = area.getHeight() * (1.0 - level);
  54. g.reduceClipRegion (area.getX(), area.getY(),
  55. area.getWidth(), (int) unfilledHeight);
  56. g.setColour (getLookAndFeel().findColour (Slider::trackColourId));
  57. g.fillRoundedRectangle (area.toFloat(), 6.0);
  58. }
  59. void resized() override {}
  60. //==============================================================================
  61. // Called from the audio thread.
  62. void update (float newLevel)
  63. {
  64. // We don't care if maxLevel gets set to zero (in timerCallback) between the
  65. // load and the assignment.
  66. maxLevel = jmax (maxLevel.load(), newLevel);
  67. }
  68. private:
  69. //==============================================================================
  70. void timerCallback() override
  71. {
  72. auto callbackLevel = maxLevel.exchange (0.0);
  73. auto decayFactor = 0.95;
  74. if (callbackLevel > level)
  75. level = callbackLevel;
  76. else if (level > 0.001)
  77. level *= decayFactor;
  78. else
  79. level = 0;
  80. repaint();
  81. }
  82. std::atomic<float> maxLevel {0.0};
  83. float level = 0;
  84. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SimpleMeter)
  85. };
  86. #if JUCE_PROJUCER_LIVE_BUILD
  87. // Animate the meter in the Projucer live build.
  88. struct MockSimpleMeter : public Component,
  89. private Timer
  90. {
  91. MockSimpleMeter()
  92. {
  93. addAndMakeVisible (meter);
  94. resized();
  95. startTimerHz (100);
  96. }
  97. void paint (Graphics&) override {}
  98. void resized() override
  99. {
  100. meter.setBounds (getBounds());
  101. }
  102. void timerCallback() override
  103. {
  104. meter.update (std::pow (randomNumberGenerator.nextFloat(), 2));
  105. }
  106. SimpleMeter meter;
  107. Random randomNumberGenerator;
  108. };
  109. #endif
  110. //==============================================================================
  111. // A simple Inter-App Audio plug-in with a gain control and some meters.
  112. class IAAEffectProcessor : public AudioProcessor
  113. {
  114. public:
  115. IAAEffectProcessor()
  116. : AudioProcessor (BusesProperties()
  117. .withInput ("Input", AudioChannelSet::stereo(), true)
  118. .withOutput ("Output", AudioChannelSet::stereo(), true)),
  119. parameters (*this, nullptr)
  120. {
  121. parameters.createAndAddParameter ("gain",
  122. "Gain",
  123. {},
  124. NormalisableRange<float> (0.0f, 1.0f),
  125. (float) (1.0 / 3.14),
  126. nullptr,
  127. nullptr);
  128. parameters.state = ValueTree (Identifier ("InterAppAudioEffect"));
  129. }
  130. ~IAAEffectProcessor() {}
  131. //==============================================================================
  132. void prepareToPlay (double, int) override
  133. {
  134. previousGain = *parameters.getRawParameterValue ("gain");
  135. }
  136. void releaseResources() override {}
  137. bool isBusesLayoutSupported (const BusesLayout& layouts) const override
  138. {
  139. if (layouts.getMainInputChannels() > 2)
  140. return false;
  141. if (layouts.getMainOutputChannelSet() != layouts.getMainInputChannelSet())
  142. return false;
  143. return true;
  144. }
  145. void processBlock (AudioBuffer<float>& buffer, MidiBuffer&) override
  146. {
  147. auto gain = *parameters.getRawParameterValue ("gain");
  148. auto totalNumInputChannels = getTotalNumInputChannels();
  149. auto totalNumOutputChannels = getTotalNumOutputChannels();
  150. auto numSamples = buffer.getNumSamples();
  151. for (auto i = totalNumInputChannels; i < totalNumOutputChannels; ++i)
  152. buffer.clear (i, 0, buffer.getNumSamples());
  153. // Apply the gain to the samples using a ramp to avoid discontinuities in
  154. // the audio between processed buffers.
  155. for (auto channel = 0; channel < totalNumInputChannels; ++channel)
  156. {
  157. buffer.applyGainRamp (channel, 0, numSamples, previousGain, gain);
  158. auto newLevel = buffer.getMagnitude (channel, 0, numSamples);
  159. meterListeners.call ([=] (MeterListener& l) { l.handleNewMeterValue (channel, newLevel); });
  160. }
  161. previousGain = gain;
  162. // Now ask the host for the current time so we can store it to be displayed later.
  163. updateCurrentTimeInfoFromHost (lastPosInfo);
  164. }
  165. //==============================================================================
  166. AudioProcessorEditor* createEditor() override
  167. {
  168. return new IAAEffectEditor (*this, parameters);
  169. }
  170. bool hasEditor() const override { return true; }
  171. //==============================================================================
  172. const String getName() const override { return JucePlugin_Name; }
  173. bool acceptsMidi() const override { return false; }
  174. bool producesMidi() const override { return false; }
  175. double getTailLengthSeconds() const override { return 0.0; }
  176. //==============================================================================
  177. int getNumPrograms() override { return 1; }
  178. int getCurrentProgram() override { return 0; }
  179. void setCurrentProgram (int) override {}
  180. const String getProgramName (int) override { return {}; }
  181. void changeProgramName (int, const String&) override {}
  182. //==============================================================================
  183. void getStateInformation (MemoryBlock& destData) override
  184. {
  185. auto xml = std::unique_ptr<XmlElement> (parameters.state.createXml());
  186. copyXmlToBinary (*xml, destData);
  187. }
  188. void setStateInformation (const void* data, int sizeInBytes) override
  189. {
  190. auto xmlState = std::unique_ptr<XmlElement> (getXmlFromBinary (data, sizeInBytes));
  191. if (xmlState.get() != nullptr)
  192. if (xmlState->hasTagName (parameters.state.getType()))
  193. parameters.state = ValueTree::fromXml (*xmlState);
  194. }
  195. //==============================================================================
  196. bool updateCurrentTimeInfoFromHost (AudioPlayHead::CurrentPositionInfo& posInfo)
  197. {
  198. if (auto* ph = getPlayHead())
  199. {
  200. AudioPlayHead::CurrentPositionInfo newTime;
  201. if (ph->getCurrentPosition (newTime))
  202. {
  203. posInfo = newTime; // Successfully got the current time from the host.
  204. return true;
  205. }
  206. }
  207. // If the host fails to provide the current time, we'll just reset our copy to a default.
  208. lastPosInfo.resetToDefault();
  209. return false;
  210. }
  211. // Allow an IAAAudioProcessorEditor to register as a listener to receive new
  212. // meter values directly from the audio thread.
  213. struct MeterListener
  214. {
  215. virtual ~MeterListener() {};
  216. virtual void handleNewMeterValue (int, float) = 0;
  217. };
  218. void addMeterListener (MeterListener& listener) { meterListeners.add (&listener); };
  219. void removeMeterListener (MeterListener& listener) { meterListeners.remove (&listener); };
  220. private:
  221. //==============================================================================
  222. class IAAEffectEditor : public AudioProcessorEditor,
  223. private IAAEffectProcessor::MeterListener,
  224. private Timer
  225. {
  226. public:
  227. IAAEffectEditor (IAAEffectProcessor& p,
  228. AudioProcessorValueTreeState& vts)
  229. : AudioProcessorEditor (p),
  230. processor (p),
  231. parameters (vts)
  232. {
  233. // Register for meter value updates.
  234. processor.addMeterListener (*this);
  235. gainSlider.setSliderStyle (Slider::SliderStyle::LinearVertical);
  236. gainSlider.setTextBoxStyle (Slider::TextEntryBoxPosition::TextBoxAbove, false, 60, 20);
  237. addAndMakeVisible (gainSlider);
  238. for (auto& meter : meters)
  239. addAndMakeVisible (meter);
  240. // Configure all the graphics for the transport control.
  241. transportText.setFont (Font (Font::getDefaultMonospacedFontName(), 18.0f, Font::plain));
  242. transportText.setJustificationType (Justification::topLeft);
  243. addChildComponent (transportText);
  244. Path rewindShape;
  245. rewindShape.addRectangle (0.0, 0.0, 5.0, buttonSize);
  246. rewindShape.addTriangle (0.0, buttonSize / 2, buttonSize, 0.0, buttonSize, buttonSize);
  247. rewindButton.setShape (rewindShape, true, true, false);
  248. rewindButton.onClick = [this]
  249. {
  250. if (transportControllable())
  251. processor.getPlayHead()->transportRewind();
  252. };
  253. addChildComponent (rewindButton);
  254. Path playShape;
  255. playShape.addTriangle (0.0, 0.0, 0.0, buttonSize, buttonSize, buttonSize / 2);
  256. playButton.setShape (playShape, true, true, false);
  257. playButton.onClick = [this]
  258. {
  259. if (transportControllable())
  260. processor.getPlayHead()->transportPlay (! lastPosInfo.isPlaying);
  261. };
  262. addChildComponent (playButton);
  263. Path recordShape;
  264. recordShape.addEllipse (0.0, 0.0, buttonSize, buttonSize);
  265. recordButton.setShape (recordShape, true, true, false);
  266. recordButton.onClick = [this]
  267. {
  268. if (transportControllable())
  269. processor.getPlayHead()->transportRecord (! lastPosInfo.isRecording);
  270. };
  271. addChildComponent (recordButton);
  272. // Configure the switch to host button.
  273. switchToHostButtonLabel.setFont (Font (Font::getDefaultMonospacedFontName(), 18.0f, Font::plain));
  274. switchToHostButtonLabel.setJustificationType (Justification::centredRight);
  275. switchToHostButtonLabel.setText ("Switch to\nhost app:", dontSendNotification);
  276. addChildComponent (switchToHostButtonLabel);
  277. switchToHostButton.onClick = [this]
  278. {
  279. if (transportControllable())
  280. {
  281. PluginHostType hostType;
  282. hostType.switchToHostApplication();
  283. }
  284. };
  285. addChildComponent (switchToHostButton);
  286. auto screenSize = Desktop::getInstance().getDisplays().getMainDisplay().userArea;
  287. setSize (screenSize.getWidth(), screenSize.getHeight());
  288. resized();
  289. startTimerHz (60);
  290. }
  291. ~IAAEffectEditor()
  292. {
  293. processor.removeMeterListener (*this);
  294. }
  295. //==============================================================================
  296. void paint (Graphics& g) override
  297. {
  298. g.fillAll (getLookAndFeel().findColour (ResizableWindow::backgroundColourId));
  299. }
  300. void resized() override
  301. {
  302. auto area = getBounds().reduced (20);
  303. gainSlider.setBounds (area.removeFromLeft (60));
  304. for (auto& meter : meters)
  305. {
  306. area.removeFromLeft (10);
  307. meter.setBounds (area.removeFromLeft (20));
  308. }
  309. area.removeFromLeft (20);
  310. transportText.setBounds (area.removeFromTop (120));
  311. auto navigationArea = area.removeFromTop (buttonSize);
  312. rewindButton.setTopLeftPosition (navigationArea.getPosition());
  313. navigationArea.removeFromLeft (buttonSize + 10);
  314. playButton.setTopLeftPosition (navigationArea.getPosition());
  315. navigationArea.removeFromLeft (buttonSize + 10);
  316. recordButton.setTopLeftPosition (navigationArea.getPosition());
  317. area.removeFromTop (30);
  318. auto appSwitchArea = area.removeFromTop (buttonSize);
  319. switchToHostButtonLabel.setBounds (appSwitchArea.removeFromLeft (100));
  320. appSwitchArea.removeFromLeft (5);
  321. switchToHostButton.setBounds (appSwitchArea.removeFromLeft (buttonSize));
  322. }
  323. private:
  324. //==============================================================================
  325. // Called from the audio thread.
  326. void handleNewMeterValue (int channel, float value) override
  327. {
  328. meters[(size_t) channel].update (value);
  329. }
  330. //==============================================================================
  331. void timerCallback () override
  332. {
  333. auto timeInfoSuccess = processor.updateCurrentTimeInfoFromHost (lastPosInfo);
  334. transportText.setVisible (timeInfoSuccess);
  335. if (timeInfoSuccess)
  336. updateTransportTextDisplay();
  337. updateTransportButtonsDisplay();
  338. updateSwitchToHostDisplay();
  339. }
  340. //==============================================================================
  341. bool transportControllable()
  342. {
  343. auto playHead = processor.getPlayHead();
  344. return playHead != nullptr && playHead->canControlTransport();
  345. }
  346. //==============================================================================
  347. // quick-and-dirty function to format a timecode string
  348. String timeToTimecodeString (double seconds)
  349. {
  350. auto millisecs = roundToInt (seconds * 1000.0);
  351. auto absMillisecs = std::abs (millisecs);
  352. return String::formatted ("%02d:%02d:%02d.%03d",
  353. millisecs / 360000,
  354. (absMillisecs / 60000) % 60,
  355. (absMillisecs / 1000) % 60,
  356. absMillisecs % 1000);
  357. }
  358. // A quick-and-dirty function to format a bars/beats string.
  359. String quarterNotePositionToBarsBeatsString (double quarterNotes, int numerator, int denominator)
  360. {
  361. if (numerator == 0 || denominator == 0)
  362. return "1|1|000";
  363. auto quarterNotesPerBar = (numerator * 4 / denominator);
  364. auto beats = (fmod (quarterNotes, quarterNotesPerBar) / quarterNotesPerBar) * numerator;
  365. auto bar = ((int) quarterNotes) / quarterNotesPerBar + 1;
  366. auto beat = ((int) beats) + 1;
  367. auto ticks = ((int) (fmod (beats, 1.0) * 960.0 + 0.5));
  368. return String::formatted ("%d|%d|%03d", bar, beat, ticks);
  369. }
  370. void updateTransportTextDisplay()
  371. {
  372. MemoryOutputStream displayText;
  373. displayText << "[" << SystemStats::getJUCEVersion() << "]\n"
  374. << String (lastPosInfo.bpm, 2) << " bpm\n"
  375. << lastPosInfo.timeSigNumerator << '/' << lastPosInfo.timeSigDenominator << "\n"
  376. << timeToTimecodeString (lastPosInfo.timeInSeconds) << "\n"
  377. << quarterNotePositionToBarsBeatsString (lastPosInfo.ppqPosition,
  378. lastPosInfo.timeSigNumerator,
  379. lastPosInfo.timeSigDenominator) << "\n";
  380. if (lastPosInfo.isRecording)
  381. displayText << "(recording)";
  382. else if (lastPosInfo.isPlaying)
  383. displayText << "(playing)";
  384. transportText.setText (displayText.toString(), dontSendNotification);
  385. }
  386. void updateTransportButtonsDisplay()
  387. {
  388. auto visible = processor.getPlayHead() != nullptr
  389. && processor.getPlayHead()->canControlTransport();
  390. if (rewindButton.isVisible() != visible)
  391. {
  392. rewindButton.setVisible (visible);
  393. playButton.setVisible (visible);
  394. recordButton.setVisible (visible);
  395. }
  396. if (visible)
  397. {
  398. auto playColour = lastPosInfo.isPlaying ? Colours::green : defaultButtonColour;
  399. playButton.setColours (playColour, playColour, playColour);
  400. playButton.repaint();
  401. auto recordColour = lastPosInfo.isRecording ? Colours::red : defaultButtonColour;
  402. recordButton.setColours (recordColour, recordColour, recordColour);
  403. recordButton.repaint();
  404. }
  405. }
  406. void updateSwitchToHostDisplay()
  407. {
  408. PluginHostType hostType;
  409. auto visible = hostType.isInterAppAudioConnected();
  410. if (switchToHostButtonLabel.isVisible() != visible)
  411. {
  412. switchToHostButtonLabel.setVisible (visible);
  413. switchToHostButton.setVisible (visible);
  414. if (visible)
  415. {
  416. auto icon = hostType.getHostIcon (buttonSize);
  417. switchToHostButton.setImages(false, true, true,
  418. icon, 1.0, Colours::transparentBlack,
  419. icon, 1.0, Colours::transparentBlack,
  420. icon, 1.0, Colours::transparentBlack);
  421. }
  422. }
  423. }
  424. IAAEffectProcessor& processor;
  425. AudioProcessorValueTreeState& parameters;
  426. const int buttonSize = 30;
  427. const Colour defaultButtonColour = Colours::darkgrey;
  428. ShapeButton rewindButton {"Rewind", defaultButtonColour, defaultButtonColour, defaultButtonColour};
  429. ShapeButton playButton {"Play", defaultButtonColour, defaultButtonColour, defaultButtonColour};
  430. ShapeButton recordButton {"Record", defaultButtonColour, defaultButtonColour, defaultButtonColour};
  431. Slider gainSlider;
  432. AudioProcessorValueTreeState::SliderAttachment gainAttachment = { parameters, "gain", gainSlider };
  433. std::array<SimpleMeter, 2> meters;
  434. ImageButton switchToHostButton;
  435. Label transportText, switchToHostButtonLabel;
  436. AudioPlayHead::CurrentPositionInfo lastPosInfo;
  437. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (IAAEffectEditor)
  438. };
  439. //==============================================================================
  440. AudioProcessorValueTreeState parameters;
  441. float previousGain = 0.0f;
  442. std::array<float, 2> meterValues = { { 0, 0 } };
  443. // This keeps a copy of the last set of timing info that was acquired during an
  444. // audio callback - the UI component will display this.
  445. AudioPlayHead::CurrentPositionInfo lastPosInfo;
  446. ListenerList<MeterListener> meterListeners;
  447. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (IAAEffectProcessor)
  448. };