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.

677 lines
25KB

  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: AudioPluginDemo
  20. version: 1.0.0
  21. vendor: JUCE
  22. website: http://juce.com
  23. description: Synthesiser 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, vs2019, linux_make, xcode_iphone, androidstudio
  29. moduleFlags: JUCE_STRICT_REFCOUNTEDPOINTER=1
  30. type: AudioProcessor
  31. mainClass: JuceDemoPluginAudioProcessor
  32. useLocalCopy: 1
  33. pluginCharacteristics: pluginIsSynth, pluginWantsMidiIn, pluginProducesMidiOut,
  34. pluginEditorRequiresKeys
  35. extraPluginFormats: AUv3
  36. END_JUCE_PIP_METADATA
  37. *******************************************************************************/
  38. #pragma once
  39. //==============================================================================
  40. /** A demo synth sound that's just a basic sine wave.. */
  41. class SineWaveSound : public SynthesiserSound
  42. {
  43. public:
  44. SineWaveSound() {}
  45. bool appliesToNote (int /*midiNoteNumber*/) override { return true; }
  46. bool appliesToChannel (int /*midiChannel*/) override { return true; }
  47. };
  48. //==============================================================================
  49. /** A simple demo synth voice that just plays a sine wave.. */
  50. class SineWaveVoice : public SynthesiserVoice
  51. {
  52. public:
  53. SineWaveVoice() {}
  54. bool canPlaySound (SynthesiserSound* sound) override
  55. {
  56. return dynamic_cast<SineWaveSound*> (sound) != nullptr;
  57. }
  58. void startNote (int midiNoteNumber, float velocity,
  59. SynthesiserSound* /*sound*/,
  60. int /*currentPitchWheelPosition*/) override
  61. {
  62. currentAngle = 0.0;
  63. level = velocity * 0.15;
  64. tailOff = 0.0;
  65. auto cyclesPerSecond = MidiMessage::getMidiNoteInHertz (midiNoteNumber);
  66. auto cyclesPerSample = cyclesPerSecond / getSampleRate();
  67. angleDelta = cyclesPerSample * MathConstants<double>::twoPi;
  68. }
  69. void stopNote (float /*velocity*/, bool allowTailOff) override
  70. {
  71. if (allowTailOff)
  72. {
  73. // start a tail-off by setting this flag. The render callback will pick up on
  74. // this and do a fade out, calling clearCurrentNote() when it's finished.
  75. if (tailOff == 0.0) // we only need to begin a tail-off if it's not already doing so - the
  76. // stopNote method could be called more than once.
  77. tailOff = 1.0;
  78. }
  79. else
  80. {
  81. // we're being told to stop playing immediately, so reset everything..
  82. clearCurrentNote();
  83. angleDelta = 0.0;
  84. }
  85. }
  86. void pitchWheelMoved (int /*newValue*/) override
  87. {
  88. // not implemented for the purposes of this demo!
  89. }
  90. void controllerMoved (int /*controllerNumber*/, int /*newValue*/) override
  91. {
  92. // not implemented for the purposes of this demo!
  93. }
  94. void renderNextBlock (AudioBuffer<float>& outputBuffer, int startSample, int numSamples) override
  95. {
  96. if (angleDelta != 0.0)
  97. {
  98. if (tailOff > 0.0)
  99. {
  100. while (--numSamples >= 0)
  101. {
  102. auto currentSample = (float) (sin (currentAngle) * level * tailOff);
  103. for (auto i = outputBuffer.getNumChannels(); --i >= 0;)
  104. outputBuffer.addSample (i, startSample, currentSample);
  105. currentAngle += angleDelta;
  106. ++startSample;
  107. tailOff *= 0.99;
  108. if (tailOff <= 0.005)
  109. {
  110. // tells the synth that this voice has stopped
  111. clearCurrentNote();
  112. angleDelta = 0.0;
  113. break;
  114. }
  115. }
  116. }
  117. else
  118. {
  119. while (--numSamples >= 0)
  120. {
  121. auto currentSample = (float) (sin (currentAngle) * level);
  122. for (auto i = outputBuffer.getNumChannels(); --i >= 0;)
  123. outputBuffer.addSample (i, startSample, currentSample);
  124. currentAngle += angleDelta;
  125. ++startSample;
  126. }
  127. }
  128. }
  129. }
  130. using SynthesiserVoice::renderNextBlock;
  131. private:
  132. double currentAngle = 0.0;
  133. double angleDelta = 0.0;
  134. double level = 0.0;
  135. double tailOff = 0.0;
  136. };
  137. //==============================================================================
  138. /** As the name suggest, this class does the actual audio processing. */
  139. class JuceDemoPluginAudioProcessor : public AudioProcessor
  140. {
  141. public:
  142. //==============================================================================
  143. JuceDemoPluginAudioProcessor()
  144. : AudioProcessor (getBusesProperties()),
  145. state (*this, nullptr, "state",
  146. { std::make_unique<AudioParameterFloat> ("gain", "Gain", NormalisableRange<float> (0.0f, 1.0f), 0.9f),
  147. std::make_unique<AudioParameterFloat> ("delay", "Delay Feedback", NormalisableRange<float> (0.0f, 1.0f), 0.5f) })
  148. {
  149. // Add a sub-tree to store the state of our UI
  150. state.state.addChild ({ "uiState", { { "width", 400 }, { "height", 200 } }, {} }, -1, nullptr);
  151. initialiseSynth();
  152. }
  153. ~JuceDemoPluginAudioProcessor() override = default;
  154. //==============================================================================
  155. bool isBusesLayoutSupported (const BusesLayout& layouts) const override
  156. {
  157. // Only mono/stereo and input/output must have same layout
  158. const auto& mainOutput = layouts.getMainOutputChannelSet();
  159. const auto& mainInput = layouts.getMainInputChannelSet();
  160. // input and output layout must either be the same or the input must be disabled altogether
  161. if (! mainInput.isDisabled() && mainInput != mainOutput)
  162. return false;
  163. // do not allow disabling the main buses
  164. if (mainOutput.isDisabled())
  165. return false;
  166. // only allow stereo and mono
  167. if (mainOutput.size() > 2)
  168. return false;
  169. return true;
  170. }
  171. void prepareToPlay (double newSampleRate, int /*samplesPerBlock*/) override
  172. {
  173. // Use this method as the place to do any pre-playback
  174. // initialisation that you need..
  175. synth.setCurrentPlaybackSampleRate (newSampleRate);
  176. keyboardState.reset();
  177. if (isUsingDoublePrecision())
  178. {
  179. delayBufferDouble.setSize (2, 12000);
  180. delayBufferFloat .setSize (1, 1);
  181. }
  182. else
  183. {
  184. delayBufferFloat .setSize (2, 12000);
  185. delayBufferDouble.setSize (1, 1);
  186. }
  187. reset();
  188. }
  189. void releaseResources() override
  190. {
  191. // When playback stops, you can use this as an opportunity to free up any
  192. // spare memory, etc.
  193. keyboardState.reset();
  194. }
  195. void reset() override
  196. {
  197. // Use this method as the place to clear any delay lines, buffers, etc, as it
  198. // means there's been a break in the audio's continuity.
  199. delayBufferFloat .clear();
  200. delayBufferDouble.clear();
  201. }
  202. //==============================================================================
  203. void processBlock (AudioBuffer<float>& buffer, MidiBuffer& midiMessages) override
  204. {
  205. jassert (! isUsingDoublePrecision());
  206. process (buffer, midiMessages, delayBufferFloat);
  207. }
  208. void processBlock (AudioBuffer<double>& buffer, MidiBuffer& midiMessages) override
  209. {
  210. jassert (isUsingDoublePrecision());
  211. process (buffer, midiMessages, delayBufferDouble);
  212. }
  213. //==============================================================================
  214. bool hasEditor() const override { return true; }
  215. AudioProcessorEditor* createEditor() override
  216. {
  217. return new JuceDemoPluginAudioProcessorEditor (*this);
  218. }
  219. //==============================================================================
  220. const String getName() const override { return "AudioPluginDemo"; }
  221. bool acceptsMidi() const override { return true; }
  222. bool producesMidi() const override { return true; }
  223. double getTailLengthSeconds() const override { return 0.0; }
  224. //==============================================================================
  225. int getNumPrograms() override { return 0; }
  226. int getCurrentProgram() override { return 0; }
  227. void setCurrentProgram (int) override {}
  228. const String getProgramName (int) override { return {}; }
  229. void changeProgramName (int, const String&) override {}
  230. //==============================================================================
  231. void getStateInformation (MemoryBlock& destData) override
  232. {
  233. // Store an xml representation of our state.
  234. if (auto xmlState = state.copyState().createXml())
  235. copyXmlToBinary (*xmlState, destData);
  236. }
  237. void setStateInformation (const void* data, int sizeInBytes) override
  238. {
  239. // Restore our plug-in's state from the xml representation stored in the above
  240. // method.
  241. if (auto xmlState = getXmlFromBinary (data, sizeInBytes))
  242. state.replaceState (ValueTree::fromXml (*xmlState));
  243. }
  244. //==============================================================================
  245. void updateTrackProperties (const TrackProperties& properties) override
  246. {
  247. {
  248. const ScopedLock sl (trackPropertiesLock);
  249. trackProperties = properties;
  250. }
  251. MessageManager::callAsync ([this]
  252. {
  253. if (auto* editor = dynamic_cast<JuceDemoPluginAudioProcessorEditor*> (getActiveEditor()))
  254. editor->updateTrackProperties();
  255. });
  256. }
  257. TrackProperties getTrackProperties() const
  258. {
  259. const ScopedLock sl (trackPropertiesLock);
  260. return trackProperties;
  261. }
  262. class SpinLockedPosInfo
  263. {
  264. public:
  265. SpinLockedPosInfo() { info.resetToDefault(); }
  266. // Wait-free, but setting new info may fail if the main thread is currently
  267. // calling `get`. This is unlikely to matter in practice because
  268. // we'll be calling `set` much more frequently than `get`.
  269. void set (const AudioPlayHead::CurrentPositionInfo& newInfo)
  270. {
  271. const juce::SpinLock::ScopedTryLockType lock (mutex);
  272. if (lock.isLocked())
  273. info = newInfo;
  274. }
  275. AudioPlayHead::CurrentPositionInfo get() const noexcept
  276. {
  277. const juce::SpinLock::ScopedLockType lock (mutex);
  278. return info;
  279. }
  280. private:
  281. juce::SpinLock mutex;
  282. AudioPlayHead::CurrentPositionInfo info;
  283. };
  284. //==============================================================================
  285. // These properties are public so that our editor component can access them
  286. // A bit of a hacky way to do it, but it's only a demo! Obviously in your own
  287. // code you'll do this much more neatly..
  288. // this is kept up to date with the midi messages that arrive, and the UI component
  289. // registers with it so it can represent the incoming messages
  290. MidiKeyboardState keyboardState;
  291. // this keeps a copy of the last set of time info that was acquired during an audio
  292. // callback - the UI component will read this and display it.
  293. SpinLockedPosInfo lastPosInfo;
  294. // Our plug-in's current state
  295. AudioProcessorValueTreeState state;
  296. private:
  297. //==============================================================================
  298. /** This is the editor component that our filter will display. */
  299. class JuceDemoPluginAudioProcessorEditor : public AudioProcessorEditor,
  300. private Timer,
  301. private Value::Listener
  302. {
  303. public:
  304. JuceDemoPluginAudioProcessorEditor (JuceDemoPluginAudioProcessor& owner)
  305. : AudioProcessorEditor (owner),
  306. midiKeyboard (owner.keyboardState, MidiKeyboardComponent::horizontalKeyboard),
  307. gainAttachment (owner.state, "gain", gainSlider),
  308. delayAttachment (owner.state, "delay", delaySlider)
  309. {
  310. // add some sliders..
  311. addAndMakeVisible (gainSlider);
  312. gainSlider.setSliderStyle (Slider::Rotary);
  313. addAndMakeVisible (delaySlider);
  314. delaySlider.setSliderStyle (Slider::Rotary);
  315. // add some labels for the sliders..
  316. gainLabel.attachToComponent (&gainSlider, false);
  317. gainLabel.setFont (Font (11.0f));
  318. delayLabel.attachToComponent (&delaySlider, false);
  319. delayLabel.setFont (Font (11.0f));
  320. // add the midi keyboard component..
  321. addAndMakeVisible (midiKeyboard);
  322. // add a label that will display the current timecode and status..
  323. addAndMakeVisible (timecodeDisplayLabel);
  324. timecodeDisplayLabel.setFont (Font (Font::getDefaultMonospacedFontName(), 15.0f, Font::plain));
  325. // set resize limits for this plug-in
  326. setResizeLimits (400, 200, 1024, 700);
  327. lastUIWidth .referTo (owner.state.state.getChildWithName ("uiState").getPropertyAsValue ("width", nullptr));
  328. lastUIHeight.referTo (owner.state.state.getChildWithName ("uiState").getPropertyAsValue ("height", nullptr));
  329. // set our component's initial size to be the last one that was stored in the filter's settings
  330. setSize (lastUIWidth.getValue(), lastUIHeight.getValue());
  331. lastUIWidth. addListener (this);
  332. lastUIHeight.addListener (this);
  333. updateTrackProperties();
  334. // start a timer which will keep our timecode display updated
  335. startTimerHz (30);
  336. }
  337. ~JuceDemoPluginAudioProcessorEditor() override {}
  338. //==============================================================================
  339. void paint (Graphics& g) override
  340. {
  341. g.setColour (backgroundColour);
  342. g.fillAll();
  343. }
  344. void resized() override
  345. {
  346. // This lays out our child components...
  347. auto r = getLocalBounds().reduced (8);
  348. timecodeDisplayLabel.setBounds (r.removeFromTop (26));
  349. midiKeyboard .setBounds (r.removeFromBottom (70));
  350. r.removeFromTop (20);
  351. auto sliderArea = r.removeFromTop (60);
  352. gainSlider.setBounds (sliderArea.removeFromLeft (jmin (180, sliderArea.getWidth() / 2)));
  353. delaySlider.setBounds (sliderArea.removeFromLeft (jmin (180, sliderArea.getWidth())));
  354. lastUIWidth = getWidth();
  355. lastUIHeight = getHeight();
  356. }
  357. void timerCallback() override
  358. {
  359. updateTimecodeDisplay (getProcessor().lastPosInfo.get());
  360. }
  361. void hostMIDIControllerIsAvailable (bool controllerIsAvailable) override
  362. {
  363. midiKeyboard.setVisible (! controllerIsAvailable);
  364. }
  365. int getControlParameterIndex (Component& control) override
  366. {
  367. if (&control == &gainSlider)
  368. return 0;
  369. if (&control == &delaySlider)
  370. return 1;
  371. return -1;
  372. }
  373. void updateTrackProperties()
  374. {
  375. auto trackColour = getProcessor().getTrackProperties().colour;
  376. auto& lf = getLookAndFeel();
  377. backgroundColour = (trackColour == Colour() ? lf.findColour (ResizableWindow::backgroundColourId)
  378. : trackColour.withAlpha (1.0f).withBrightness (0.266f));
  379. repaint();
  380. }
  381. private:
  382. MidiKeyboardComponent midiKeyboard;
  383. Label timecodeDisplayLabel,
  384. gainLabel { {}, "Throughput level:" },
  385. delayLabel { {}, "Delay:" };
  386. Slider gainSlider, delaySlider;
  387. AudioProcessorValueTreeState::SliderAttachment gainAttachment, delayAttachment;
  388. Colour backgroundColour;
  389. // these are used to persist the UI's size - the values are stored along with the
  390. // filter's other parameters, and the UI component will update them when it gets
  391. // resized.
  392. Value lastUIWidth, lastUIHeight;
  393. //==============================================================================
  394. JuceDemoPluginAudioProcessor& getProcessor() const
  395. {
  396. return static_cast<JuceDemoPluginAudioProcessor&> (processor);
  397. }
  398. //==============================================================================
  399. // quick-and-dirty function to format a timecode string
  400. static String timeToTimecodeString (double seconds)
  401. {
  402. auto millisecs = roundToInt (seconds * 1000.0);
  403. auto absMillisecs = std::abs (millisecs);
  404. return String::formatted ("%02d:%02d:%02d.%03d",
  405. millisecs / 3600000,
  406. (absMillisecs / 60000) % 60,
  407. (absMillisecs / 1000) % 60,
  408. absMillisecs % 1000);
  409. }
  410. // quick-and-dirty function to format a bars/beats string
  411. static String quarterNotePositionToBarsBeatsString (double quarterNotes, int numerator, int denominator)
  412. {
  413. if (numerator == 0 || denominator == 0)
  414. return "1|1|000";
  415. auto quarterNotesPerBar = (numerator * 4 / denominator);
  416. auto beats = (fmod (quarterNotes, quarterNotesPerBar) / quarterNotesPerBar) * numerator;
  417. auto bar = ((int) quarterNotes) / quarterNotesPerBar + 1;
  418. auto beat = ((int) beats) + 1;
  419. auto ticks = ((int) (fmod (beats, 1.0) * 960.0 + 0.5));
  420. return String::formatted ("%d|%d|%03d", bar, beat, ticks);
  421. }
  422. // Updates the text in our position label.
  423. void updateTimecodeDisplay (AudioPlayHead::CurrentPositionInfo pos)
  424. {
  425. MemoryOutputStream displayText;
  426. displayText << "[" << SystemStats::getJUCEVersion() << "] "
  427. << String (pos.bpm, 2) << " bpm, "
  428. << pos.timeSigNumerator << '/' << pos.timeSigDenominator
  429. << " - " << timeToTimecodeString (pos.timeInSeconds)
  430. << " - " << quarterNotePositionToBarsBeatsString (pos.ppqPosition,
  431. pos.timeSigNumerator,
  432. pos.timeSigDenominator);
  433. if (pos.isRecording)
  434. displayText << " (recording)";
  435. else if (pos.isPlaying)
  436. displayText << " (playing)";
  437. timecodeDisplayLabel.setText (displayText.toString(), dontSendNotification);
  438. }
  439. // called when the stored window size changes
  440. void valueChanged (Value&) override
  441. {
  442. setSize (lastUIWidth.getValue(), lastUIHeight.getValue());
  443. }
  444. };
  445. //==============================================================================
  446. template <typename FloatType>
  447. void process (AudioBuffer<FloatType>& buffer, MidiBuffer& midiMessages, AudioBuffer<FloatType>& delayBuffer)
  448. {
  449. auto gainParamValue = state.getParameter ("gain") ->getValue();
  450. auto delayParamValue = state.getParameter ("delay")->getValue();
  451. auto numSamples = buffer.getNumSamples();
  452. // In case we have more outputs than inputs, we'll clear any output
  453. // channels that didn't contain input data, (because these aren't
  454. // guaranteed to be empty - they may contain garbage).
  455. for (auto i = getTotalNumInputChannels(); i < getTotalNumOutputChannels(); ++i)
  456. buffer.clear (i, 0, numSamples);
  457. // Now pass any incoming midi messages to our keyboard state object, and let it
  458. // add messages to the buffer if the user is clicking on the on-screen keys
  459. keyboardState.processNextMidiBuffer (midiMessages, 0, numSamples, true);
  460. // and now get our synth to process these midi events and generate its output.
  461. synth.renderNextBlock (buffer, midiMessages, 0, numSamples);
  462. // Apply our delay effect to the new output..
  463. applyDelay (buffer, delayBuffer, delayParamValue);
  464. // Apply our gain change to the outgoing data..
  465. applyGain (buffer, delayBuffer, gainParamValue);
  466. // Now ask the host for the current time so we can store it to be displayed later...
  467. updateCurrentTimeInfoFromHost();
  468. }
  469. template <typename FloatType>
  470. void applyGain (AudioBuffer<FloatType>& buffer, AudioBuffer<FloatType>& delayBuffer, float gainLevel)
  471. {
  472. ignoreUnused (delayBuffer);
  473. for (auto channel = 0; channel < getTotalNumOutputChannels(); ++channel)
  474. buffer.applyGain (channel, 0, buffer.getNumSamples(), gainLevel);
  475. }
  476. template <typename FloatType>
  477. void applyDelay (AudioBuffer<FloatType>& buffer, AudioBuffer<FloatType>& delayBuffer, float delayLevel)
  478. {
  479. auto numSamples = buffer.getNumSamples();
  480. auto delayPos = 0;
  481. for (auto channel = 0; channel < getTotalNumOutputChannels(); ++channel)
  482. {
  483. auto channelData = buffer.getWritePointer (channel);
  484. auto delayData = delayBuffer.getWritePointer (jmin (channel, delayBuffer.getNumChannels() - 1));
  485. delayPos = delayPosition;
  486. for (auto i = 0; i < numSamples; ++i)
  487. {
  488. auto in = channelData[i];
  489. channelData[i] += delayData[delayPos];
  490. delayData[delayPos] = (delayData[delayPos] + in) * delayLevel;
  491. if (++delayPos >= delayBuffer.getNumSamples())
  492. delayPos = 0;
  493. }
  494. }
  495. delayPosition = delayPos;
  496. }
  497. AudioBuffer<float> delayBufferFloat;
  498. AudioBuffer<double> delayBufferDouble;
  499. int delayPosition = 0;
  500. Synthesiser synth;
  501. CriticalSection trackPropertiesLock;
  502. TrackProperties trackProperties;
  503. void initialiseSynth()
  504. {
  505. auto numVoices = 8;
  506. // Add some voices...
  507. for (auto i = 0; i < numVoices; ++i)
  508. synth.addVoice (new SineWaveVoice());
  509. // ..and give the synth a sound to play
  510. synth.addSound (new SineWaveSound());
  511. }
  512. void updateCurrentTimeInfoFromHost()
  513. {
  514. const auto newInfo = [&]
  515. {
  516. if (auto* ph = getPlayHead())
  517. {
  518. AudioPlayHead::CurrentPositionInfo result;
  519. if (ph->getCurrentPosition (result))
  520. return result;
  521. }
  522. // If the host fails to provide the current time, we'll just use default values
  523. AudioPlayHead::CurrentPositionInfo result;
  524. result.resetToDefault();
  525. return result;
  526. }();
  527. lastPosInfo.set (newInfo);
  528. }
  529. static BusesProperties getBusesProperties()
  530. {
  531. return BusesProperties().withInput ("Input", AudioChannelSet::stereo(), true)
  532. .withOutput ("Output", AudioChannelSet::stereo(), true);
  533. }
  534. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (JuceDemoPluginAudioProcessor)
  535. };