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.

455 lines
21KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. #include <JuceHeader.h>
  14. #include <juce_audio_plugin_client/juce_audio_plugin_client.h>
  15. #include "InternalPlugins.h"
  16. #include "PluginGraph.h"
  17. #include "../../../../examples/Plugins/AUv3SynthPluginDemo.h"
  18. #include "../../../../examples/Plugins/ArpeggiatorPluginDemo.h"
  19. #include "../../../../examples/Plugins/AudioPluginDemo.h"
  20. #include "../../../../examples/Plugins/DSPModulePluginDemo.h"
  21. #include "../../../../examples/Plugins/GainPluginDemo.h"
  22. #include "../../../../examples/Plugins/MidiLoggerPluginDemo.h"
  23. #include "../../../../examples/Plugins/MultiOutSynthPluginDemo.h"
  24. #include "../../../../examples/Plugins/NoiseGatePluginDemo.h"
  25. #include "../../../../examples/Plugins/SamplerPluginDemo.h"
  26. #include "../../../../examples/Plugins/SurroundPluginDemo.h"
  27. //==============================================================================
  28. class InternalPlugin : public AudioPluginInstance
  29. {
  30. public:
  31. explicit InternalPlugin (std::unique_ptr<AudioProcessor> innerIn)
  32. : inner (std::move (innerIn))
  33. {
  34. jassert (inner != nullptr);
  35. for (auto isInput : { true, false })
  36. matchChannels (isInput);
  37. setBusesLayout (inner->getBusesLayout());
  38. }
  39. //==============================================================================
  40. const String getName() const override { return inner->getName(); }
  41. StringArray getAlternateDisplayNames() const override { return inner->getAlternateDisplayNames(); }
  42. double getTailLengthSeconds() const override { return inner->getTailLengthSeconds(); }
  43. bool acceptsMidi() const override { return inner->acceptsMidi(); }
  44. bool producesMidi() const override { return inner->producesMidi(); }
  45. AudioProcessorEditor* createEditor() override { return inner->createEditor(); }
  46. bool hasEditor() const override { return inner->hasEditor(); }
  47. int getNumPrograms() override { return inner->getNumPrograms(); }
  48. int getCurrentProgram() override { return inner->getCurrentProgram(); }
  49. void setCurrentProgram (int i) override { inner->setCurrentProgram (i); }
  50. const String getProgramName (int i) override { return inner->getProgramName (i); }
  51. void changeProgramName (int i, const String& n) override { inner->changeProgramName (i, n); }
  52. void getStateInformation (juce::MemoryBlock& b) override { inner->getStateInformation (b); }
  53. void setStateInformation (const void* d, int s) override { inner->setStateInformation (d, s); }
  54. void getCurrentProgramStateInformation (juce::MemoryBlock& b) override { inner->getCurrentProgramStateInformation (b); }
  55. void setCurrentProgramStateInformation (const void* d, int s) override { inner->setCurrentProgramStateInformation (d, s); }
  56. void prepareToPlay (double sr, int bs) override { inner->setRateAndBufferSizeDetails (sr, bs); inner->prepareToPlay (sr, bs); }
  57. void releaseResources() override { inner->releaseResources(); }
  58. void memoryWarningReceived() override { inner->memoryWarningReceived(); }
  59. void processBlock (AudioBuffer<float>& a, MidiBuffer& m) override { inner->processBlock (a, m); }
  60. void processBlock (AudioBuffer<double>& a, MidiBuffer& m) override { inner->processBlock (a, m); }
  61. void processBlockBypassed (AudioBuffer<float>& a, MidiBuffer& m) override { inner->processBlockBypassed (a, m); }
  62. void processBlockBypassed (AudioBuffer<double>& a, MidiBuffer& m) override { inner->processBlockBypassed (a, m); }
  63. bool supportsDoublePrecisionProcessing() const override { return inner->supportsDoublePrecisionProcessing(); }
  64. bool supportsMPE() const override { return inner->supportsMPE(); }
  65. bool isMidiEffect() const override { return inner->isMidiEffect(); }
  66. void reset() override { inner->reset(); }
  67. void setNonRealtime (bool b) noexcept override { inner->setNonRealtime (b); }
  68. void refreshParameterList() override { inner->refreshParameterList(); }
  69. void numChannelsChanged() override { inner->numChannelsChanged(); }
  70. void numBusesChanged() override { inner->numBusesChanged(); }
  71. void processorLayoutsChanged() override { inner->processorLayoutsChanged(); }
  72. void setPlayHead (AudioPlayHead* p) override { inner->setPlayHead (p); }
  73. void updateTrackProperties (const TrackProperties& p) override { inner->updateTrackProperties (p); }
  74. bool isBusesLayoutSupported (const BusesLayout& layout) const override { return inner->checkBusesLayoutSupported (layout); }
  75. bool canAddBus (bool) const override { return true; }
  76. bool canRemoveBus (bool) const override { return true; }
  77. //==============================================================================
  78. void fillInPluginDescription (PluginDescription& description) const override
  79. {
  80. description = getPluginDescription (*inner);
  81. }
  82. private:
  83. static PluginDescription getPluginDescription (const AudioProcessor& proc)
  84. {
  85. const auto ins = proc.getTotalNumInputChannels();
  86. const auto outs = proc.getTotalNumOutputChannels();
  87. const auto identifier = proc.getName();
  88. const auto registerAsGenerator = ins == 0;
  89. const auto acceptsMidi = proc.acceptsMidi();
  90. PluginDescription descr;
  91. descr.name = identifier;
  92. descr.descriptiveName = identifier;
  93. descr.pluginFormatName = InternalPluginFormat::getIdentifier();
  94. descr.category = (registerAsGenerator ? (acceptsMidi ? "Synth" : "Generator") : "Effect");
  95. descr.manufacturerName = "JUCE";
  96. descr.version = ProjectInfo::versionString;
  97. descr.fileOrIdentifier = identifier;
  98. descr.uid = identifier.hashCode();
  99. descr.isInstrument = (acceptsMidi && registerAsGenerator);
  100. descr.numInputChannels = ins;
  101. descr.numOutputChannels = outs;
  102. return descr;
  103. }
  104. void matchChannels (bool isInput)
  105. {
  106. const auto inBuses = inner->getBusCount (isInput);
  107. while (getBusCount (isInput) < inBuses)
  108. addBus (isInput);
  109. while (inBuses < getBusCount (isInput))
  110. removeBus (isInput);
  111. }
  112. std::unique_ptr<AudioProcessor> inner;
  113. //==============================================================================
  114. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (InternalPlugin)
  115. };
  116. //==============================================================================
  117. class SineWaveSynth : public AudioProcessor
  118. {
  119. public:
  120. SineWaveSynth()
  121. : AudioProcessor (BusesProperties().withOutput ("Output", AudioChannelSet::stereo()))
  122. {
  123. const int numVoices = 8;
  124. // Add some voices...
  125. for (int i = numVoices; --i >= 0;)
  126. synth.addVoice (new SineWaveVoice());
  127. // ..and give the synth a sound to play
  128. synth.addSound (new SineWaveSound());
  129. }
  130. static String getIdentifier()
  131. {
  132. return "Sine Wave Synth";
  133. }
  134. //==============================================================================
  135. void prepareToPlay (double newSampleRate, int) override
  136. {
  137. synth.setCurrentPlaybackSampleRate (newSampleRate);
  138. }
  139. void releaseResources() override {}
  140. //==============================================================================
  141. void processBlock (AudioBuffer<float>& buffer, MidiBuffer& midiMessages) override
  142. {
  143. const int numSamples = buffer.getNumSamples();
  144. buffer.clear();
  145. synth.renderNextBlock (buffer, midiMessages, 0, numSamples);
  146. buffer.applyGain (0.8f);
  147. }
  148. using AudioProcessor::processBlock;
  149. const String getName() const override { return getIdentifier(); }
  150. double getTailLengthSeconds() const override { return 0.0; }
  151. bool acceptsMidi() const override { return true; }
  152. bool producesMidi() const override { return true; }
  153. AudioProcessorEditor* createEditor() override { return nullptr; }
  154. bool hasEditor() const override { return false; }
  155. int getNumPrograms() override { return 1; }
  156. int getCurrentProgram() override { return 0; }
  157. void setCurrentProgram (int) override {}
  158. const String getProgramName (int) override { return {}; }
  159. void changeProgramName (int, const String&) override {}
  160. void getStateInformation (juce::MemoryBlock&) override {}
  161. void setStateInformation (const void*, int) override {}
  162. private:
  163. //==============================================================================
  164. struct SineWaveSound : public SynthesiserSound
  165. {
  166. SineWaveSound() = default;
  167. bool appliesToNote (int /*midiNoteNumber*/) override { return true; }
  168. bool appliesToChannel (int /*midiChannel*/) override { return true; }
  169. };
  170. struct SineWaveVoice : public SynthesiserVoice
  171. {
  172. SineWaveVoice() = default;
  173. bool canPlaySound (SynthesiserSound* sound) override
  174. {
  175. return dynamic_cast<SineWaveSound*> (sound) != nullptr;
  176. }
  177. void startNote (int midiNoteNumber, float velocity,
  178. SynthesiserSound* /*sound*/,
  179. int /*currentPitchWheelPosition*/) override
  180. {
  181. currentAngle = 0.0;
  182. level = velocity * 0.15;
  183. tailOff = 0.0;
  184. double cyclesPerSecond = MidiMessage::getMidiNoteInHertz (midiNoteNumber);
  185. double cyclesPerSample = cyclesPerSecond / getSampleRate();
  186. angleDelta = cyclesPerSample * 2.0 * double_Pi;
  187. }
  188. void stopNote (float /*velocity*/, bool allowTailOff) override
  189. {
  190. if (allowTailOff)
  191. {
  192. // start a tail-off by setting this flag. The render callback will pick up on
  193. // this and do a fade out, calling clearCurrentNote() when it's finished.
  194. if (tailOff == 0.0) // we only need to begin a tail-off if it's not already doing so - the
  195. // stopNote method could be called more than once.
  196. tailOff = 1.0;
  197. }
  198. else
  199. {
  200. // we're being told to stop playing immediately, so reset everything..
  201. clearCurrentNote();
  202. angleDelta = 0.0;
  203. }
  204. }
  205. void pitchWheelMoved (int /*newValue*/) override
  206. {
  207. // not implemented for the purposes of this demo!
  208. }
  209. void controllerMoved (int /*controllerNumber*/, int /*newValue*/) override
  210. {
  211. // not implemented for the purposes of this demo!
  212. }
  213. void renderNextBlock (AudioBuffer<float>& outputBuffer, int startSample, int numSamples) override
  214. {
  215. if (angleDelta != 0.0)
  216. {
  217. if (tailOff > 0)
  218. {
  219. while (--numSamples >= 0)
  220. {
  221. const float currentSample = (float) (sin (currentAngle) * level * tailOff);
  222. for (int i = outputBuffer.getNumChannels(); --i >= 0;)
  223. outputBuffer.addSample (i, startSample, currentSample);
  224. currentAngle += angleDelta;
  225. ++startSample;
  226. tailOff *= 0.99;
  227. if (tailOff <= 0.005)
  228. {
  229. // tells the synth that this voice has stopped
  230. clearCurrentNote();
  231. angleDelta = 0.0;
  232. break;
  233. }
  234. }
  235. }
  236. else
  237. {
  238. while (--numSamples >= 0)
  239. {
  240. const float currentSample = (float) (sin (currentAngle) * level);
  241. for (int i = outputBuffer.getNumChannels(); --i >= 0;)
  242. outputBuffer.addSample (i, startSample, currentSample);
  243. currentAngle += angleDelta;
  244. ++startSample;
  245. }
  246. }
  247. }
  248. }
  249. using SynthesiserVoice::renderNextBlock;
  250. private:
  251. double currentAngle = 0, angleDelta = 0, level = 0, tailOff = 0;
  252. };
  253. //==============================================================================
  254. Synthesiser synth;
  255. //==============================================================================
  256. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SineWaveSynth)
  257. };
  258. //==============================================================================
  259. class ReverbPlugin : public AudioProcessor
  260. {
  261. public:
  262. ReverbPlugin()
  263. : AudioProcessor (BusesProperties().withInput ("Input", AudioChannelSet::stereo())
  264. .withOutput ("Output", AudioChannelSet::stereo()))
  265. {}
  266. static String getIdentifier()
  267. {
  268. return "Reverb";
  269. }
  270. void prepareToPlay (double newSampleRate, int) override
  271. {
  272. reverb.setSampleRate (newSampleRate);
  273. }
  274. void reset() override
  275. {
  276. reverb.reset();
  277. }
  278. void releaseResources() override {}
  279. void processBlock (AudioBuffer<float>& buffer, MidiBuffer&) override
  280. {
  281. auto numChannels = buffer.getNumChannels();
  282. if (numChannels == 1)
  283. reverb.processMono (buffer.getWritePointer (0), buffer.getNumSamples());
  284. else
  285. reverb.processStereo (buffer.getWritePointer (0),
  286. buffer.getWritePointer (1),
  287. buffer.getNumSamples());
  288. for (int ch = 2; ch < numChannels; ++ch)
  289. buffer.clear (ch, 0, buffer.getNumSamples());
  290. }
  291. using AudioProcessor::processBlock;
  292. const String getName() const override { return getIdentifier(); }
  293. double getTailLengthSeconds() const override { return 0.0; }
  294. bool acceptsMidi() const override { return false; }
  295. bool producesMidi() const override { return false; }
  296. AudioProcessorEditor* createEditor() override { return nullptr; }
  297. bool hasEditor() const override { return false; }
  298. int getNumPrograms() override { return 1; }
  299. int getCurrentProgram() override { return 0; }
  300. void setCurrentProgram (int) override {}
  301. const String getProgramName (int) override { return {}; }
  302. void changeProgramName (int, const String&) override {}
  303. void getStateInformation (juce::MemoryBlock&) override {}
  304. void setStateInformation (const void*, int) override {}
  305. private:
  306. Reverb reverb;
  307. };
  308. //==============================================================================
  309. InternalPluginFormat::InternalPluginFactory::InternalPluginFactory (const std::initializer_list<Constructor>& constructorsIn)
  310. : constructors (constructorsIn),
  311. descriptions ([&]
  312. {
  313. std::vector<PluginDescription> result;
  314. for (const auto& constructor : constructors)
  315. result.push_back (constructor()->getPluginDescription());
  316. return result;
  317. }())
  318. {}
  319. std::unique_ptr<AudioPluginInstance> InternalPluginFormat::InternalPluginFactory::createInstance (const String& name) const
  320. {
  321. const auto begin = descriptions.begin();
  322. const auto it = std::find_if (begin,
  323. descriptions.end(),
  324. [&] (const PluginDescription& desc) { return name == desc.name; });
  325. if (it == descriptions.end())
  326. return nullptr;
  327. const auto index = (size_t) std::distance (begin, it);
  328. return constructors[index]();
  329. }
  330. InternalPluginFormat::InternalPluginFormat()
  331. : factory {
  332. [] { return std::make_unique<AudioProcessorGraph::AudioGraphIOProcessor> (AudioProcessorGraph::AudioGraphIOProcessor::audioInputNode); },
  333. [] { return std::make_unique<AudioProcessorGraph::AudioGraphIOProcessor> (AudioProcessorGraph::AudioGraphIOProcessor::midiInputNode); },
  334. [] { return std::make_unique<AudioProcessorGraph::AudioGraphIOProcessor> (AudioProcessorGraph::AudioGraphIOProcessor::audioOutputNode); },
  335. [] { return std::make_unique<AudioProcessorGraph::AudioGraphIOProcessor> (AudioProcessorGraph::AudioGraphIOProcessor::midiOutputNode); },
  336. [] { return std::make_unique<InternalPlugin> (std::make_unique<SineWaveSynth>()); },
  337. [] { return std::make_unique<InternalPlugin> (std::make_unique<ReverbPlugin>()); },
  338. [] { return std::make_unique<InternalPlugin> (std::make_unique<AUv3SynthProcessor>()); },
  339. [] { return std::make_unique<InternalPlugin> (std::make_unique<Arpeggiator>()); },
  340. [] { return std::make_unique<InternalPlugin> (std::make_unique<DspModulePluginDemoAudioProcessor>()); },
  341. [] { return std::make_unique<InternalPlugin> (std::make_unique<GainProcessor>()); },
  342. [] { return std::make_unique<InternalPlugin> (std::make_unique<JuceDemoPluginAudioProcessor>()); },
  343. [] { return std::make_unique<InternalPlugin> (std::make_unique<MidiLoggerPluginDemoProcessor>()); },
  344. [] { return std::make_unique<InternalPlugin> (std::make_unique<MultiOutSynth>()); },
  345. [] { return std::make_unique<InternalPlugin> (std::make_unique<NoiseGate>()); },
  346. [] { return std::make_unique<InternalPlugin> (std::make_unique<SamplerAudioProcessor>()); },
  347. [] { return std::make_unique<InternalPlugin> (std::make_unique<SurroundProcessor>()); }
  348. }
  349. {
  350. }
  351. std::unique_ptr<AudioPluginInstance> InternalPluginFormat::createInstance (const String& name)
  352. {
  353. return factory.createInstance (name);
  354. }
  355. void InternalPluginFormat::createPluginInstance (const PluginDescription& desc,
  356. double /*initialSampleRate*/, int /*initialBufferSize*/,
  357. PluginCreationCallback callback)
  358. {
  359. if (auto p = createInstance (desc.name))
  360. callback (std::move (p), {});
  361. else
  362. callback (nullptr, NEEDS_TRANS ("Invalid internal plugin name"));
  363. }
  364. bool InternalPluginFormat::requiresUnblockedMessageThreadDuringCreation (const PluginDescription&) const
  365. {
  366. return false;
  367. }
  368. const std::vector<PluginDescription>& InternalPluginFormat::getAllTypes() const
  369. {
  370. return factory.getDescriptions();
  371. }