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.

1522 lines
51KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. // Your project must contain an AppConfig.h file with your project-specific settings in it,
  18. // and your header search path must make it accessible to the module's files.
  19. #include "AppConfig.h"
  20. //==============================================================================
  21. #if JucePlugin_Build_VST3 && (__APPLE_CPP__ || __APPLE_CC__ || _WIN32 || _WIN64)
  22. #include "../../juce_audio_processors/format_types/juce_VST3Headers.h"
  23. #include "../utility/juce_CheckSettingMacros.h"
  24. #include "../utility/juce_IncludeModuleHeaders.h"
  25. #include "../../juce_audio_processors/format_types/juce_VST3Common.h"
  26. #undef Point
  27. #undef Component
  28. using namespace Steinberg;
  29. //==============================================================================
  30. class JuceLibraryRefCount
  31. {
  32. public:
  33. JuceLibraryRefCount() { if ((getCount()++) == 0) initialiseJuce_GUI(); }
  34. ~JuceLibraryRefCount() { if ((--getCount()) == 0) shutdownJuce_GUI(); }
  35. private:
  36. int& getCount() noexcept
  37. {
  38. static int count = 0;
  39. return count;
  40. }
  41. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (JuceLibraryRefCount)
  42. };
  43. //==============================================================================
  44. namespace juce
  45. {
  46. #if JUCE_MAC
  47. extern void initialiseMac();
  48. extern void* attachComponentToWindowRef (Component*, void* parent, bool isNSView);
  49. extern void detachComponentFromWindowRef (Component*, void* window, bool isNSView);
  50. extern void setNativeHostWindowSize (void* window, Component*, int newWidth, int newHeight, bool isNSView);
  51. #endif
  52. }
  53. //==============================================================================
  54. static const FUID componentFUID = FUID (0x1AA169A8, 0x4800FC31, JucePlugin_ManufacturerCode, JucePlugin_PluginCode);
  55. static const FUID controllerFUID = FUID (0x2BB270B9, 0x0BA11ADE, JucePlugin_ManufacturerCode, JucePlugin_PluginCode);
  56. //==============================================================================
  57. class JuceAudioProcessor : public FUnknown
  58. {
  59. public:
  60. JuceAudioProcessor (AudioProcessor* source) noexcept
  61. : refCount (0), audioProcessor (source) {}
  62. virtual ~JuceAudioProcessor() {}
  63. AudioProcessor* get() const noexcept { return audioProcessor; }
  64. JUCE_DECLARE_VST3_COM_QUERY_METHODS
  65. JUCE_DECLARE_VST3_COM_REF_METHODS
  66. static const FUID iid;
  67. private:
  68. Atomic<int> refCount;
  69. ScopedPointer<AudioProcessor> audioProcessor;
  70. JuceAudioProcessor() JUCE_DELETED_FUNCTION;
  71. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (JuceAudioProcessor)
  72. };
  73. #if JUCE_MSVC
  74. #pragma warning (push, 0)
  75. #pragma warning (disable: 4310)
  76. #elif JUCE_CLANG
  77. #pragma clang diagnostic push
  78. #pragma clang diagnostic ignored "-w"
  79. #endif
  80. DECLARE_CLASS_IID (JuceAudioProcessor, 0x0101ABAB, 0xABCDEF01, 0x1234ABCD, 0x9182FAEB)
  81. DEF_CLASS_IID (JuceAudioProcessor)
  82. #if JUCE_MSVC
  83. #pragma warning (pop)
  84. #elif JUCE_CLANG
  85. #pragma clang diagnostic pop
  86. #endif
  87. //==============================================================================
  88. class JuceVST3EditController : public Vst::EditController,
  89. public AudioProcessorListener
  90. {
  91. public:
  92. JuceVST3EditController() {}
  93. //==============================================================================
  94. tresult PLUGIN_API initialize (FUnknown* context) override
  95. {
  96. if (hostContext != context)
  97. {
  98. if (hostContext != nullptr)
  99. hostContext->release();
  100. hostContext = context;
  101. if (hostContext != nullptr)
  102. hostContext->addRef();
  103. }
  104. return kResultTrue;
  105. }
  106. tresult PLUGIN_API terminate() override
  107. {
  108. if (AudioProcessor* const pluginInstance = getPluginInstance())
  109. pluginInstance->removeListener (this);
  110. audioProcessor = nullptr;
  111. return EditController::terminate();
  112. }
  113. //==============================================================================
  114. struct Param : public Vst::Parameter
  115. {
  116. Param (AudioProcessor& p, int index) : owner (p), paramIndex (index)
  117. {
  118. info.id = (Vst::ParamID) index;
  119. toString128 (info.title, p.getParameterName (index));
  120. toString128 (info.shortTitle, p.getParameterName (index, 8));
  121. toString128 (info.units, p.getParameterLabel (index));
  122. info.stepCount = (Steinberg::int32) p.getParameterNumSteps (index);
  123. info.defaultNormalizedValue = p.getParameterDefaultValue (index);
  124. info.unitId = Vst::kRootUnitId;
  125. }
  126. virtual ~Param() {}
  127. bool setNormalized (Vst::ParamValue v) override
  128. {
  129. v = jlimit (0.0, 1.0, v);
  130. if (v != valueNormalized)
  131. {
  132. valueNormalized = v;
  133. changed();
  134. owner.setParameter (paramIndex, (float) v);
  135. return true;
  136. }
  137. return false;
  138. }
  139. void toString (Vst::ParamValue, Vst::String128 result) const override
  140. {
  141. toString128 (result, owner.getParameterText (paramIndex, 128));
  142. }
  143. Vst::ParamValue toPlain (Vst::ParamValue v) const override { return v; }
  144. Vst::ParamValue toNormalized (Vst::ParamValue v) const override { return v; }
  145. private:
  146. AudioProcessor& owner;
  147. int paramIndex;
  148. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Param)
  149. };
  150. //==============================================================================
  151. tresult PLUGIN_API connect (IConnectionPoint* other) override
  152. {
  153. if (other != nullptr)
  154. {
  155. audioProcessor.loadFrom (other);
  156. jassert (audioProcessor != nullptr);
  157. if (AudioProcessor* const pluginInstance = getPluginInstance())
  158. {
  159. pluginInstance->addListener (this);
  160. if (parameters.getParameterCount() <= 0)
  161. for (int i = 0; i < pluginInstance->getNumParameters(); ++i)
  162. parameters.addParameter (new Param (*pluginInstance, i));
  163. audioProcessorChanged (pluginInstance);
  164. return ComponentBase::connect (other);
  165. }
  166. ComponentBase::connect (other);
  167. }
  168. jassertfalse;
  169. return kResultFalse;
  170. }
  171. tresult PLUGIN_API disconnect (IConnectionPoint* other) override
  172. {
  173. return ComponentBase::disconnect (other);
  174. }
  175. //==============================================================================
  176. IPlugView* PLUGIN_API createView (const char* name) override
  177. {
  178. if (AudioProcessor* const pluginInstance = getPluginInstance())
  179. {
  180. if (pluginInstance->hasEditor() && name != nullptr
  181. && strcmp (name, Vst::ViewType::kEditor) == 0)
  182. {
  183. return new JuceVST3Editor (*this, *pluginInstance);
  184. }
  185. }
  186. return nullptr;
  187. }
  188. //==============================================================================
  189. void audioProcessorParameterChangeGestureBegin (AudioProcessor*, int index) override { beginEdit ((Steinberg::uint32) index); }
  190. void audioProcessorParameterChanged (AudioProcessor*, int index, float newValue) override { performEdit ((Steinberg::uint32) index, (double) newValue); }
  191. void audioProcessorParameterChangeGestureEnd (AudioProcessor*, int index) override { endEdit ((Steinberg::uint32) index); }
  192. void audioProcessorChanged (AudioProcessor*) override
  193. {
  194. if (componentHandler != nullptr)
  195. componentHandler->restartComponent (Vst::kLatencyChanged & Vst::kParamValuesChanged);
  196. }
  197. //==============================================================================
  198. AudioProcessor* getPluginInstance() const noexcept
  199. {
  200. if (audioProcessor != nullptr)
  201. return audioProcessor->get();
  202. return nullptr;
  203. }
  204. private:
  205. //==============================================================================
  206. ComSmartPtr<JuceAudioProcessor> audioProcessor;
  207. const JuceLibraryRefCount juceCount;
  208. //==============================================================================
  209. class JuceVST3Editor : public Vst::EditorView
  210. {
  211. public:
  212. JuceVST3Editor (JuceVST3EditController& ec, AudioProcessor& p)
  213. : Vst::EditorView (&ec, nullptr),
  214. owner (&ec), pluginInstance (p)
  215. {
  216. #if JUCE_MAC
  217. macHostWindow = nullptr;
  218. isNSView = false;
  219. #endif
  220. component = new ContentWrapperComponent (*this, p);
  221. }
  222. //==============================================================================
  223. tresult PLUGIN_API isPlatformTypeSupported (FIDString type) override
  224. {
  225. if (type != nullptr && pluginInstance.hasEditor())
  226. {
  227. #if JUCE_WINDOWS
  228. if (strcmp (type, kPlatformTypeHWND) == 0)
  229. #else
  230. if (strcmp (type, kPlatformTypeNSView) == 0 || strcmp (type, kPlatformTypeHIView) == 0)
  231. #endif
  232. return kResultTrue;
  233. }
  234. return kResultFalse;
  235. }
  236. tresult PLUGIN_API attached (void* parent, FIDString type) override
  237. {
  238. if (parent == nullptr || isPlatformTypeSupported (type) == kResultFalse)
  239. return kResultFalse;
  240. #if JUCE_WINDOWS
  241. component->addToDesktop (0, parent);
  242. component->setOpaque (true);
  243. component->setVisible (true);
  244. #else
  245. isNSView = (strcmp (type, kPlatformTypeNSView) == 0);
  246. macHostWindow = juce::attachComponentToWindowRef (component, parent, isNSView);
  247. #endif
  248. component->resizeHostWindow();
  249. systemWindow = parent;
  250. attachedToParent();
  251. return kResultTrue;
  252. }
  253. tresult PLUGIN_API removed() override
  254. {
  255. if (component != nullptr)
  256. {
  257. #if JUCE_WINDOWS
  258. component->removeFromDesktop();
  259. #else
  260. if (macHostWindow != nullptr)
  261. {
  262. juce::detachComponentFromWindowRef (component, macHostWindow, isNSView);
  263. macHostWindow = nullptr;
  264. }
  265. #endif
  266. component = nullptr;
  267. }
  268. return CPluginView::removed();
  269. }
  270. tresult PLUGIN_API onSize (ViewRect* newSize) override
  271. {
  272. if (newSize != nullptr)
  273. {
  274. rect = *newSize;
  275. if (component != nullptr)
  276. component->setSize (rect.getWidth(), rect.getHeight());
  277. return kResultTrue;
  278. }
  279. jassertfalse;
  280. return kResultFalse;
  281. }
  282. tresult PLUGIN_API getSize (ViewRect* size) override
  283. {
  284. if (size != nullptr && component != nullptr)
  285. {
  286. *size = ViewRect (0, 0, component->getWidth(), component->getHeight());
  287. return kResultTrue;
  288. }
  289. return kResultFalse;
  290. }
  291. tresult PLUGIN_API canResize() override
  292. {
  293. return kResultTrue;
  294. }
  295. tresult PLUGIN_API checkSizeConstraint (ViewRect* rect) override
  296. {
  297. if (rect != nullptr && component != nullptr)
  298. {
  299. rect->right = rect->left + component->getWidth();
  300. rect->bottom = rect->top + component->getHeight();
  301. return kResultTrue;
  302. }
  303. jassertfalse;
  304. return kResultFalse;
  305. }
  306. private:
  307. //==============================================================================
  308. class ContentWrapperComponent : public juce::Component
  309. {
  310. public:
  311. ContentWrapperComponent (JuceVST3Editor& editor, AudioProcessor& plugin)
  312. : owner (editor),
  313. pluginEditor (plugin.createEditorIfNeeded())
  314. {
  315. setOpaque (true);
  316. setBroughtToFrontOnMouseClick (true);
  317. // if hasEditor() returns true then createEditorIfNeeded has to return a valid editor
  318. jassert (pluginEditor != nullptr);
  319. if (pluginEditor != nullptr)
  320. {
  321. addAndMakeVisible (pluginEditor);
  322. setBounds (pluginEditor->getLocalBounds());
  323. resizeHostWindow();
  324. }
  325. }
  326. ~ContentWrapperComponent()
  327. {
  328. if (pluginEditor != nullptr)
  329. {
  330. PopupMenu::dismissAllActiveMenus();
  331. pluginEditor->getAudioProcessor()->editorBeingDeleted (pluginEditor);
  332. }
  333. }
  334. void paint (Graphics& g) override
  335. {
  336. g.fillAll (Colours::black);
  337. }
  338. void childBoundsChanged (Component*) override
  339. {
  340. resizeHostWindow();
  341. }
  342. void resized() override
  343. {
  344. if (pluginEditor != nullptr)
  345. pluginEditor->setBounds (getLocalBounds());
  346. }
  347. void resizeHostWindow()
  348. {
  349. if (pluginEditor != nullptr)
  350. {
  351. const int w = pluginEditor->getWidth();
  352. const int h = pluginEditor->getHeight();
  353. #if JUCE_WINDOWS
  354. setSize (w, h);
  355. #else
  356. if (owner.macHostWindow != nullptr)
  357. juce::setNativeHostWindowSize (owner.macHostWindow, this, w, h, owner.isNSView);
  358. #endif
  359. if (owner.plugFrame != nullptr)
  360. {
  361. ViewRect newSize (0, 0, w, h);
  362. owner.plugFrame->resizeView (&owner, &newSize);
  363. }
  364. }
  365. }
  366. private:
  367. JuceVST3Editor& owner;
  368. ScopedPointer<AudioProcessorEditor> pluginEditor;
  369. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ContentWrapperComponent)
  370. };
  371. //==============================================================================
  372. ComSmartPtr<JuceVST3EditController> owner;
  373. AudioProcessor& pluginInstance;
  374. ScopedPointer<ContentWrapperComponent> component;
  375. friend class ContentWrapperComponent;
  376. #if JUCE_MAC
  377. void* macHostWindow;
  378. bool isNSView;
  379. #endif
  380. //==============================================================================
  381. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (JuceVST3Editor)
  382. };
  383. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (JuceVST3EditController)
  384. };
  385. //==============================================================================
  386. class JuceVST3Component : public Vst::IComponent,
  387. public Vst::IAudioProcessor,
  388. public Vst::IUnitInfo,
  389. public Vst::IConnectionPoint,
  390. public AudioPlayHead
  391. {
  392. public:
  393. JuceVST3Component()
  394. : refCount (1),
  395. audioInputs (Vst::kAudio, Vst::kInput),
  396. audioOutputs (Vst::kAudio, Vst::kOutput),
  397. eventInputs (Vst::kEvent, Vst::kInput),
  398. eventOutputs (Vst::kEvent, Vst::kOutput)
  399. {
  400. pluginInstance = createPluginFilterOfType (AudioProcessor::wrapperType_VST3);
  401. comPluginInstance = new JuceAudioProcessor (pluginInstance);
  402. zerostruct (processContext);
  403. processSetup.maxSamplesPerBlock = 1024;
  404. processSetup.processMode = Vst::kRealtime;
  405. processSetup.sampleRate = 44100.0;
  406. processSetup.symbolicSampleSize = Vst::kSample32;
  407. }
  408. ~JuceVST3Component()
  409. {
  410. if (pluginInstance != nullptr)
  411. if (pluginInstance->getPlayHead() == this)
  412. pluginInstance->setPlayHead (nullptr);
  413. audioInputs.removeAll();
  414. audioOutputs.removeAll();
  415. eventInputs.removeAll();
  416. eventOutputs.removeAll();
  417. }
  418. //==============================================================================
  419. AudioProcessor& getPluginInstance() const noexcept { return *pluginInstance; }
  420. static PluginHostType& getHostType()
  421. {
  422. static PluginHostType hostType;
  423. return hostType;
  424. }
  425. //==============================================================================
  426. JUCE_DECLARE_VST3_COM_REF_METHODS
  427. tresult PLUGIN_API queryInterface (const TUID iid, void** obj) override
  428. {
  429. TEST_FOR_AND_RETURN_IF_VALID (Vst::IComponent)
  430. TEST_FOR_AND_RETURN_IF_VALID (Vst::IAudioProcessor)
  431. TEST_FOR_AND_RETURN_IF_VALID (Vst::IUnitInfo)
  432. TEST_FOR_AND_RETURN_IF_VALID (Vst::IConnectionPoint)
  433. if (doUIDsMatch (iid, JuceAudioProcessor::iid))
  434. {
  435. comPluginInstance->addRef();
  436. *obj = comPluginInstance;
  437. return kResultOk;
  438. }
  439. *obj = nullptr;
  440. return kNoInterface;
  441. }
  442. //==============================================================================
  443. tresult PLUGIN_API initialize (FUnknown*) override
  444. {
  445. #if JucePlugin_MaxNumInputChannels > 0
  446. addAudioBusTo (audioInputs, TRANS("Audio Input"),
  447. getArrangementForNumChannels (JucePlugin_MaxNumInputChannels));
  448. #endif
  449. #if JucePlugin_MaxNumOutputChannels > 0
  450. addAudioBusTo (audioOutputs, TRANS("Audio Output"),
  451. getArrangementForNumChannels (JucePlugin_MaxNumOutputChannels));
  452. #endif
  453. #if JucePlugin_WantsMidiInput
  454. addEventBusTo (eventInputs, TRANS("MIDI Input"));
  455. #endif
  456. #if JucePlugin_ProducesMidiOutput
  457. addEventBusTo (eventOutputs, TRANS("MIDI Output"));
  458. #endif
  459. {
  460. const ScopedLock sl (contextLock);
  461. processContext.sampleRate = processSetup.sampleRate;
  462. }
  463. preparePlugin (processSetup.sampleRate, (int) processSetup.maxSamplesPerBlock);
  464. return kResultTrue;
  465. }
  466. tresult PLUGIN_API terminate() override
  467. {
  468. getPluginInstance().releaseResources();
  469. return kResultTrue;
  470. }
  471. //==============================================================================
  472. tresult PLUGIN_API connect (IConnectionPoint* other) override
  473. {
  474. if (other != nullptr && editController == nullptr)
  475. {
  476. editController.loadFrom (other);
  477. juceVST3EditController = (JuceVST3EditController*) editController.get();
  478. jassert (juceVST3EditController != nullptr);
  479. }
  480. return editController != nullptr ? kResultTrue : kResultFalse;
  481. }
  482. tresult PLUGIN_API disconnect (IConnectionPoint*) override
  483. {
  484. juceVST3EditController = nullptr;
  485. editController = nullptr;
  486. return kResultTrue;
  487. }
  488. tresult PLUGIN_API notify (Vst::IMessage*) override
  489. {
  490. return kNotImplemented;
  491. }
  492. //==============================================================================
  493. tresult PLUGIN_API getControllerClassId (TUID classID) override
  494. {
  495. controllerFUID.toTUID (classID);
  496. return kResultTrue;
  497. }
  498. Steinberg::int32 PLUGIN_API getBusCount (Vst::MediaType type, Vst::BusDirection dir) override
  499. {
  500. if (Vst::BusList* const busList = getBusListFor (type, dir))
  501. return busList->total();
  502. return 0;
  503. }
  504. tresult PLUGIN_API getBusInfo (Vst::MediaType type, Vst::BusDirection dir,
  505. Steinberg::int32 index, Vst::BusInfo& info) override
  506. {
  507. if (Vst::BusList* const busList = getBusListFor (type, dir))
  508. {
  509. if (Vst::Bus* const bus = (Vst::Bus*) busList->at (index))
  510. {
  511. info.mediaType = type;
  512. info.direction = dir;
  513. if (bus->getInfo (info))
  514. return kResultTrue;
  515. }
  516. }
  517. zerostruct (info);
  518. return kResultFalse;
  519. }
  520. tresult PLUGIN_API activateBus (Vst::MediaType type, Vst::BusDirection dir,
  521. Steinberg::int32 index, TBool state) override
  522. {
  523. if (Vst::BusList* const busList = getBusListFor (type, dir))
  524. {
  525. if (Vst::Bus* const bus = (Vst::Bus*) busList->at (index))
  526. {
  527. bus->setActive (state);
  528. return kResultTrue;
  529. }
  530. }
  531. jassertfalse;
  532. return kResultFalse;
  533. }
  534. tresult PLUGIN_API setActive (TBool state) override
  535. {
  536. if (state == kResultFalse)
  537. {
  538. getPluginInstance().releaseResources();
  539. }
  540. else
  541. {
  542. double sampleRate = getPluginInstance().getSampleRate();
  543. int bufferSize = getPluginInstance().getBlockSize();
  544. {
  545. const ScopedLock sl (contextLock);
  546. sampleRate = processSetup.sampleRate > 0.0
  547. ? processSetup.sampleRate
  548. : sampleRate;
  549. bufferSize = processSetup.maxSamplesPerBlock > 0
  550. ? (int) processSetup.maxSamplesPerBlock
  551. : bufferSize;
  552. }
  553. preparePlugin (sampleRate, bufferSize);
  554. }
  555. return kResultOk;
  556. }
  557. tresult PLUGIN_API setIoMode (Vst::IoMode) override { return kNotImplemented; }
  558. tresult PLUGIN_API getRoutingInfo (Vst::RoutingInfo&, Vst::RoutingInfo&) override { return kNotImplemented; }
  559. tresult PLUGIN_API setState (IBStream* state) override
  560. {
  561. if (state != nullptr)
  562. {
  563. // Reset to the beginning of the stream:
  564. if (state->seek (0, IBStream::kIBSeekSet, nullptr) != kResultTrue)
  565. return kResultFalse;
  566. Steinberg::int64 end = -1;
  567. if (end < 0)
  568. {
  569. FUnknownPtr<ISizeableStream> s (state);
  570. if (s != nullptr)
  571. s->getStreamSize (end);
  572. }
  573. if (end < 0)
  574. {
  575. FUnknownPtr<MemoryStream> s (state);
  576. if (s != nullptr)
  577. {
  578. if (getHostType().isAdobeAudition())
  579. {
  580. // Adobe Audition CS6 hack to avoid trying to use corrupted streams:
  581. bool failed = true;
  582. if (const char* const data = s->getData())
  583. {
  584. if (s->getSize() >= 5 && data[0] != 'V' && data[1] != 'C'
  585. && data[2] != '2' && data[3] != '!' && data[4] != 'E')
  586. {
  587. failed = false;
  588. }
  589. }
  590. else
  591. {
  592. jassertfalse;
  593. }
  594. if (failed)
  595. return kResultFalse;
  596. }
  597. end = (Steinberg::int64) s->getSize();
  598. }
  599. }
  600. if (end <= 0)
  601. return kResultFalse;
  602. // Try reading the data, and setting the plugin state:
  603. Steinberg::int32 numBytes = (Steinberg::int32) jmin ((Steinberg::int64) std::numeric_limits<Steinberg::int32>::max(), end);
  604. Array<char> buff;
  605. buff.ensureStorageAllocated ((int) numBytes);
  606. void* buffer = buff.getRawDataPointer();
  607. if (state->read (buffer, numBytes, &numBytes) == kResultTrue
  608. && buffer != nullptr
  609. && numBytes > 0)
  610. {
  611. pluginInstance->setStateInformation (buffer, (int) numBytes);
  612. return kResultTrue;
  613. }
  614. return kResultFalse;
  615. }
  616. return kInvalidArgument;
  617. }
  618. tresult PLUGIN_API getState (IBStream* state) override
  619. {
  620. if (state != nullptr)
  621. {
  622. juce::MemoryBlock mem;
  623. pluginInstance->getStateInformation (mem);
  624. return state->write (mem.getData(), (Steinberg::int32) mem.getSize());
  625. }
  626. return kInvalidArgument;
  627. }
  628. //==============================================================================
  629. Steinberg::int32 PLUGIN_API getUnitCount() override
  630. {
  631. return 1;
  632. }
  633. tresult PLUGIN_API getUnitInfo (Steinberg::int32 unitIndex, Vst::UnitInfo& info) override
  634. {
  635. if (unitIndex == 0)
  636. {
  637. info.id = Vst::kRootUnitId;
  638. info.parentUnitId = Vst::kNoParentUnitId;
  639. info.programListId = Vst::kNoProgramListId;
  640. toString128 (info.name, TRANS("Root Unit"));
  641. return kResultTrue;
  642. }
  643. zerostruct (info);
  644. return kResultFalse;
  645. }
  646. Steinberg::int32 PLUGIN_API getProgramListCount() override
  647. {
  648. if (getPluginInstance().getNumPrograms() > 0)
  649. return 1;
  650. return 0;
  651. }
  652. tresult PLUGIN_API getProgramListInfo (Steinberg::int32 listIndex, Vst::ProgramListInfo& info) override
  653. {
  654. if (listIndex == 0)
  655. {
  656. info.id = paramPreset;
  657. info.programCount = (Steinberg::int32) getPluginInstance().getNumPrograms();
  658. toString128 (info.name, TRANS("Factory Presets"));
  659. return kResultTrue;
  660. }
  661. jassertfalse;
  662. zerostruct (info);
  663. return kResultFalse;
  664. }
  665. tresult PLUGIN_API getProgramName (Vst::ProgramListID listId, Steinberg::int32 programIndex, Vst::String128 name) override
  666. {
  667. if (listId == paramPreset
  668. && isPositiveAndBelow ((int) programIndex, getPluginInstance().getNumPrograms()))
  669. {
  670. toString128 (name, getPluginInstance().getProgramName ((int) programIndex));
  671. return kResultTrue;
  672. }
  673. jassertfalse;
  674. toString128 (name, juce::String());
  675. return kResultFalse;
  676. }
  677. tresult PLUGIN_API getProgramInfo (Vst::ProgramListID, Steinberg::int32, Vst::CString, Vst::String128) override { return kNotImplemented; }
  678. tresult PLUGIN_API hasProgramPitchNames (Vst::ProgramListID, Steinberg::int32) override { return kNotImplemented; }
  679. tresult PLUGIN_API getProgramPitchName (Vst::ProgramListID, Steinberg::int32, Steinberg::int16, Vst::String128) override { return kNotImplemented; }
  680. tresult PLUGIN_API selectUnit (Vst::UnitID) override { return kNotImplemented; }
  681. tresult PLUGIN_API setUnitProgramData (Steinberg::int32, Steinberg::int32, IBStream*) override { return kNotImplemented; }
  682. Vst::UnitID PLUGIN_API getSelectedUnit() override { return Vst::kRootUnitId; }
  683. tresult PLUGIN_API getUnitByBus (Vst::MediaType, Vst::BusDirection,
  684. Steinberg::int32, Steinberg::int32,
  685. Vst::UnitID& unitId) override
  686. {
  687. zerostruct (unitId);
  688. return kNotImplemented;
  689. }
  690. //==============================================================================
  691. bool getCurrentPosition (CurrentPositionInfo& info) override
  692. {
  693. const ScopedLock sl (contextLock);
  694. info.timeInSamples = jmax ((juce::int64) 0, processContext.projectTimeSamples);
  695. info.timeInSeconds = processContext.projectTimeMusic;
  696. info.bpm = jmax (1.0, processContext.tempo);
  697. info.timeSigNumerator = jmax (1, (int) processContext.timeSigNumerator);
  698. info.timeSigDenominator = jmax (1, (int) processContext.timeSigDenominator);
  699. info.ppqPositionOfLastBarStart = processContext.barPositionMusic;
  700. info.ppqPosition = processContext.projectTimeMusic;
  701. info.ppqLoopStart = processContext.cycleStartMusic;
  702. info.ppqLoopEnd = processContext.cycleEndMusic;
  703. info.isRecording = (processContext.state & Vst::ProcessContext::kRecording) != 0;
  704. info.isPlaying = (processContext.state & Vst::ProcessContext::kPlaying) != 0;
  705. info.isLooping = (processContext.state & Vst::ProcessContext::kCycleActive) != 0;
  706. info.editOriginTime = 0.0;
  707. info.frameRate = AudioPlayHead::fpsUnknown;
  708. if ((processContext.state & Vst::ProcessContext::kSmpteValid) != 0)
  709. {
  710. switch (processContext.frameRate.framesPerSecond)
  711. {
  712. case 24: info.frameRate = AudioPlayHead::fps24; break;
  713. case 25: info.frameRate = AudioPlayHead::fps25; break;
  714. case 29: info.frameRate = AudioPlayHead::fps30drop; break;
  715. case 30:
  716. {
  717. if ((processContext.frameRate.flags & Vst::FrameRate::kDropRate) != 0)
  718. info.frameRate = AudioPlayHead::fps30drop;
  719. else
  720. info.frameRate = AudioPlayHead::fps30;
  721. }
  722. break;
  723. default: break;
  724. }
  725. }
  726. return true;
  727. }
  728. //==============================================================================
  729. static tresult setBusArrangementFor (Vst::BusList& list,
  730. Vst::SpeakerArrangement* arrangement,
  731. Steinberg::int32 numBusses)
  732. {
  733. if (arrangement != nullptr && numBusses == 1) //Should only be 1 bus per BusList
  734. {
  735. Steinberg::int32 counter = 0;
  736. FOREACH_CAST (IPtr<Vst::Bus>, Vst::AudioBus, bus, list)
  737. if (counter < numBusses)
  738. bus->setArrangement (arrangement[counter]);
  739. counter++;
  740. ENDFOR
  741. return kResultTrue;
  742. }
  743. return kResultFalse;
  744. }
  745. tresult PLUGIN_API setBusArrangements (Vst::SpeakerArrangement* inputs, Steinberg::int32 numIns,
  746. Vst::SpeakerArrangement* outputs, Steinberg::int32 numOuts) override
  747. {
  748. #if JucePlugin_MaxNumInputChannels > 0
  749. if (setBusArrangementFor (audioInputs, inputs, numIns) != kResultTrue)
  750. return kResultFalse;
  751. #else
  752. if (numIns != 0)
  753. return kResultFalse;
  754. #endif
  755. #if JucePlugin_MaxNumOutputChannels > 0
  756. if (setBusArrangementFor (audioOutputs, outputs, numOuts) != kResultTrue)
  757. return kResultFalse;
  758. #else
  759. if (numOuts != 0)
  760. return kResultFalse;
  761. #endif
  762. return kResultTrue;
  763. }
  764. tresult PLUGIN_API getBusArrangement (Vst::BusDirection dir, Steinberg::int32 index, Vst::SpeakerArrangement& arr) override
  765. {
  766. if (Vst::BusList* const busList = getBusListFor (Vst::kAudio, dir))
  767. {
  768. if (Vst::AudioBus* const audioBus = FCast<Vst::AudioBus> (busList->at (index)))
  769. {
  770. arr = audioBus->getArrangement();
  771. return kResultTrue;
  772. }
  773. }
  774. return kResultFalse;
  775. }
  776. tresult PLUGIN_API canProcessSampleSize (Steinberg::int32 symbolicSampleSize) override
  777. {
  778. return symbolicSampleSize == Vst::kSample32 ? kResultTrue : kResultFalse;
  779. }
  780. Steinberg::uint32 PLUGIN_API getLatencySamples() override
  781. {
  782. return (Steinberg::uint32) jmax (0, getPluginInstance().getLatencySamples());
  783. }
  784. tresult PLUGIN_API setupProcessing (Vst::ProcessSetup& newSetup) override
  785. {
  786. if (canProcessSampleSize (newSetup.symbolicSampleSize) != kResultTrue)
  787. return kResultFalse;
  788. processSetup = newSetup;
  789. processContext.sampleRate = processSetup.sampleRate;
  790. preparePlugin (processSetup.sampleRate, processSetup.maxSamplesPerBlock);
  791. return kResultTrue;
  792. }
  793. tresult PLUGIN_API setProcessing (TBool state) override
  794. {
  795. if (state == kResultFalse)
  796. getPluginInstance().reset();
  797. return kResultTrue;
  798. }
  799. Steinberg::uint32 PLUGIN_API getTailSamples() override
  800. {
  801. const double tailLengthSeconds = getPluginInstance().getTailLengthSeconds();
  802. if (tailLengthSeconds <= 0.0 || processSetup.sampleRate > 0.0)
  803. return Vst::kNoTail;
  804. return (Steinberg::uint32) roundToIntAccurate (tailLengthSeconds * processSetup.sampleRate);
  805. }
  806. //==============================================================================
  807. void processParameterChanges (Vst::IParameterChanges& paramChanges)
  808. {
  809. jassert (pluginInstance != nullptr);
  810. const Steinberg::int32 numParamsChanged = paramChanges.getParameterCount();
  811. for (Steinberg::int32 i = 0; i < numParamsChanged; ++i)
  812. {
  813. if (Vst::IParamValueQueue* paramQueue = paramChanges.getParameterData (i))
  814. {
  815. const Steinberg::int32 numPoints = paramQueue->getPointCount();
  816. Steinberg::int32 offsetSamples;
  817. double value = 0.0;
  818. if (paramQueue->getPoint (numPoints - 1, offsetSamples, value) == kResultTrue)
  819. {
  820. const int id = (int) paramQueue->getParameterId();
  821. jassert (isPositiveAndBelow (id, pluginInstance->getNumParameters()));
  822. pluginInstance->setParameter (id, (float) value);
  823. }
  824. }
  825. }
  826. }
  827. tresult PLUGIN_API process (Vst::ProcessData& data) override
  828. {
  829. midiBuffer.clear();
  830. {
  831. const ScopedLock sl (contextLock);
  832. if (data.processContext != nullptr)
  833. processContext = *data.processContext;
  834. else
  835. zerostruct (processContext);
  836. }
  837. #if JucePlugin_WantsMidiInput
  838. if (data.inputEvents != nullptr)
  839. MidiEventList::toMidiBuffer (midiBuffer, *data.inputEvents);
  840. #endif
  841. #if JUCE_DEBUG && ! JucePlugin_ProducesMidiOutput
  842. const int numMidiEventsComingIn = midiBuffer.getNumEvents();
  843. #endif
  844. if (pluginInstance != nullptr
  845. && data.inputs != nullptr
  846. && data.outputs != nullptr
  847. && processContext.sampleRate > 0.0)
  848. {
  849. const ScopedLock sl (pluginInstance->getCallbackLock());
  850. pluginInstance->setNonRealtime (data.processMode == Vst::kOffline);
  851. if (Vst::IParameterChanges* const paramChanges = data.inputParameterChanges)
  852. processParameterChanges (*paramChanges);
  853. const int numSamples = (int) data.numSamples;
  854. const int numChannels = (int) data.inputs[0].numChannels;
  855. AudioSampleBuffer buffer (data.inputs[0].channelBuffers32, numChannels, numSamples);
  856. int startBusOffset = 1;
  857. if (pluginInstance->isSuspended())
  858. startBusOffset = 0;
  859. else
  860. pluginInstance->processBlock (buffer, midiBuffer);
  861. // Copy the audio:
  862. for (int i = 0; i < numChannels; ++i)
  863. FloatVectorOperations::copy (data.outputs[0].channelBuffers32[i], buffer.getSampleData (i), numSamples);
  864. // Clear the other busses:
  865. for (int i = startBusOffset; i < data.numOutputs; ++i)
  866. for (int f = 0; f < data.outputs[i].numChannels; ++f)
  867. FloatVectorOperations::clear (data.outputs[i].channelBuffers32[f], numSamples);
  868. }
  869. else
  870. {
  871. return kResultFalse;
  872. }
  873. #if JucePlugin_ProducesMidiOutput
  874. if (data.outputEvents != nullptr)
  875. MidiEventList::toEventList (*data.outputEvents, midiBuffer);
  876. #elif JUCE_DEBUG
  877. /* This assertion is caused when you've added some events to the
  878. midiMessages array in your processBlock() method, which usually means
  879. that you're trying to send them somewhere. But in this case they're
  880. getting thrown away.
  881. If your plugin does want to send MIDI messages, you'll need to set
  882. the JucePlugin_ProducesMidiOutput macro to 1 in your
  883. JucePluginCharacteristics.h file.
  884. If you don't want to produce any MIDI output, then you should clear the
  885. midiMessages array at the end of your processBlock() method, to
  886. indicate that you don't want any of the events to be passed through
  887. to the output.
  888. */
  889. jassert (midiBuffer.getNumEvents() <= numMidiEventsComingIn);
  890. #endif
  891. return kResultTrue;
  892. }
  893. private:
  894. //==============================================================================
  895. Atomic<int> refCount;
  896. AudioProcessor* pluginInstance;
  897. ComSmartPtr<JuceAudioProcessor> comPluginInstance;
  898. ComSmartPtr<Vst::IEditController> editController;
  899. JuceVST3EditController* juceVST3EditController;
  900. /**
  901. Since VST3 does not provide a way of knowing the buffer size and sample rate at any point,
  902. this object needs to be copied on every call to process() to be up-to-date...
  903. */
  904. CriticalSection contextLock; //Not sure how necessary this is...
  905. Vst::ProcessContext processContext;
  906. Vst::ProcessSetup processSetup;
  907. Vst::BusList audioInputs, audioOutputs, eventInputs, eventOutputs;
  908. MidiBuffer midiBuffer;
  909. const JuceLibraryRefCount juceCount;
  910. //==============================================================================
  911. void addBusTo (Vst::BusList& busList, Vst::Bus* newBus)
  912. {
  913. busList.append (IPtr<Vst::Bus> (newBus, false));
  914. }
  915. void addAudioBusTo (Vst::BusList& busList, const juce::String& name, Vst::SpeakerArrangement arr)
  916. {
  917. addBusTo (busList, new Vst::AudioBus (toString (name), Vst::kMain, Vst::BusInfo::kDefaultActive, arr));
  918. }
  919. void addEventBusTo (Vst::BusList& busList, const juce::String& name)
  920. {
  921. addBusTo (busList, new Vst::EventBus (toString (name), 16, Vst::kMain, Vst::BusInfo::kDefaultActive));
  922. }
  923. Vst::BusList* getBusListFor (Vst::MediaType type, Vst::BusDirection dir)
  924. {
  925. if (type == Vst::kAudio)
  926. return dir == Vst::kInput ? &audioInputs : &audioOutputs;
  927. if (type == Vst::kEvent)
  928. return dir == Vst::kInput ? &eventInputs : &eventOutputs;
  929. return nullptr;
  930. }
  931. //==============================================================================
  932. enum InternalParameters
  933. {
  934. paramPreset = 'prst'
  935. };
  936. void preparePlugin (double sampleRate, int bufferSize)
  937. {
  938. getPluginInstance().setPlayConfigDetails (JucePlugin_MaxNumInputChannels,
  939. JucePlugin_MaxNumOutputChannels,
  940. sampleRate, bufferSize);
  941. getPluginInstance().prepareToPlay (sampleRate, bufferSize);
  942. }
  943. //==============================================================================
  944. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (JuceVST3Component)
  945. };
  946. //==============================================================================
  947. bool initModule()
  948. {
  949. #if JUCE_MAC
  950. initialiseMac();
  951. #endif
  952. return true;
  953. }
  954. bool shutdownModule()
  955. {
  956. return true;
  957. }
  958. #undef JUCE_EXPORTED_FUNCTION
  959. #if JUCE_WINDOWS
  960. extern "C" __declspec (dllexport) bool InitDll() { return initModule(); }
  961. extern "C" __declspec (dllexport) bool ExitDll() { return shutdownModule(); }
  962. #define JUCE_EXPORTED_FUNCTION
  963. #else
  964. #define JUCE_EXPORTED_FUNCTION extern "C" __attribute__ ((visibility ("default")))
  965. CFBundleRef globalBundleInstance = nullptr;
  966. juce::uint32 numBundleRefs = 0;
  967. juce::Array<CFBundleRef> bundleRefs;
  968. enum { MaxPathLength = 2048 };
  969. char modulePath[MaxPathLength] = { 0 };
  970. void* moduleHandle = nullptr;
  971. JUCE_EXPORTED_FUNCTION bool bundleEntry (CFBundleRef ref)
  972. {
  973. if (ref != nullptr)
  974. {
  975. ++numBundleRefs;
  976. CFRetain (ref);
  977. bundleRefs.add (ref);
  978. if (moduleHandle == nullptr)
  979. {
  980. globalBundleInstance = ref;
  981. moduleHandle = ref;
  982. CFURLRef tempURL = CFBundleCopyBundleURL (ref);
  983. CFURLGetFileSystemRepresentation (tempURL, true, (UInt8*) modulePath, MaxPathLength);
  984. CFRelease (tempURL);
  985. }
  986. }
  987. return initModule();
  988. }
  989. JUCE_EXPORTED_FUNCTION bool bundleExit()
  990. {
  991. if (shutdownModule())
  992. {
  993. if (--numBundleRefs == 0)
  994. {
  995. for (size_t i = 0; i < bundleRefs.size(); ++i)
  996. CFRelease (bundleRefs.getUnchecked (i));
  997. bundleRefs.clear();
  998. }
  999. return true;
  1000. }
  1001. return false;
  1002. }
  1003. #endif
  1004. //==============================================================================
  1005. /** This typedef represents VST3's createInstance() function signature */
  1006. typedef FUnknown* (*CreateFunction) (void*);
  1007. static FUnknown* createComponentInstance (void*)
  1008. {
  1009. return (Vst::IAudioProcessor*) new JuceVST3Component();
  1010. }
  1011. static FUnknown* createControllerInstance (void*)
  1012. {
  1013. return (Vst::IEditController*) new JuceVST3EditController();
  1014. }
  1015. //==============================================================================
  1016. class JucePluginFactory;
  1017. JucePluginFactory* globalFactory = nullptr;
  1018. //==============================================================================
  1019. class JucePluginFactory : public IPluginFactory3
  1020. {
  1021. public:
  1022. JucePluginFactory()
  1023. : refCount (1), factoryInfo (JucePlugin_Manufacturer, "", "", Vst::kDefaultFactoryFlags)
  1024. {
  1025. }
  1026. virtual ~JucePluginFactory()
  1027. {
  1028. if (globalFactory == this)
  1029. globalFactory = nullptr;
  1030. }
  1031. //==============================================================================
  1032. bool registerClass (const PClassInfo2& info, CreateFunction createFunction)
  1033. {
  1034. if (createFunction == nullptr)
  1035. {
  1036. jassertfalse;
  1037. return false;
  1038. }
  1039. ClassEntry* entry = classes.add (new ClassEntry (info, createFunction));
  1040. entry->infoW.fromAscii (info);
  1041. return true;
  1042. }
  1043. bool isClassRegistered (const FUID& cid) const
  1044. {
  1045. for (int i = 0; i < classes.size(); ++i)
  1046. if (classes.getUnchecked (i)->infoW.cid == cid)
  1047. return true;
  1048. return false;
  1049. }
  1050. //==============================================================================
  1051. JUCE_DECLARE_VST3_COM_REF_METHODS
  1052. tresult PLUGIN_API queryInterface (const TUID iid, void** obj) override
  1053. {
  1054. TEST_FOR_AND_RETURN_IF_VALID (IPluginFactory3)
  1055. TEST_FOR_AND_RETURN_IF_VALID (IPluginFactory2)
  1056. TEST_FOR_AND_RETURN_IF_VALID (IPluginFactory)
  1057. TEST_FOR_AND_RETURN_IF_VALID (FUnknown)
  1058. jassertfalse; // Something new?
  1059. *obj = nullptr;
  1060. return kNotImplemented;
  1061. }
  1062. //==============================================================================
  1063. Steinberg::int32 PLUGIN_API countClasses() override
  1064. {
  1065. return (Steinberg::int32) classes.size();
  1066. }
  1067. tresult PLUGIN_API getFactoryInfo (PFactoryInfo* info) override
  1068. {
  1069. if (info == nullptr)
  1070. return kInvalidArgument;
  1071. std::memcpy (info, &factoryInfo, sizeof (PFactoryInfo));
  1072. return kResultOk;
  1073. }
  1074. tresult PLUGIN_API getClassInfo (Steinberg::int32 index, PClassInfo* info) override
  1075. {
  1076. return getPClassInfo<PClassInfo> (index, info);
  1077. }
  1078. tresult PLUGIN_API getClassInfo2 (Steinberg::int32 index, PClassInfo2* info) override
  1079. {
  1080. return getPClassInfo<PClassInfo2> (index, info);
  1081. }
  1082. tresult PLUGIN_API getClassInfoUnicode (Steinberg::int32 index, PClassInfoW* info) override
  1083. {
  1084. if (info != nullptr)
  1085. {
  1086. if (ClassEntry* entry = classes[(int) index])
  1087. {
  1088. std::memcpy (info, &entry->infoW, sizeof (PClassInfoW));
  1089. return kResultOk;
  1090. }
  1091. }
  1092. return kInvalidArgument;
  1093. }
  1094. tresult PLUGIN_API createInstance (FIDString cid, FIDString iid, void** obj) override
  1095. {
  1096. for (int i = 0; i < classes.size(); ++i)
  1097. {
  1098. const ClassEntry& entry = *classes.getUnchecked (i);
  1099. if (doUIDsMatch (entry.infoW.cid, cid))
  1100. {
  1101. if (FUnknown* const instance = entry.createFunction (nullptr))
  1102. {
  1103. const FReleaser releaser (instance);
  1104. if (instance->queryInterface (iid, obj) == kResultOk)
  1105. return kResultOk;
  1106. }
  1107. break;
  1108. }
  1109. }
  1110. *obj = nullptr;
  1111. return kNoInterface;
  1112. }
  1113. tresult PLUGIN_API setHostContext (FUnknown* context) override
  1114. {
  1115. host.loadFrom (context);
  1116. if (host != nullptr)
  1117. {
  1118. Vst::String128 name;
  1119. host->getName (name);
  1120. return kResultTrue;
  1121. }
  1122. return kNotImplemented;
  1123. }
  1124. private:
  1125. //==============================================================================
  1126. const JuceLibraryRefCount juceCount;
  1127. Atomic<int> refCount;
  1128. const PFactoryInfo factoryInfo;
  1129. ComSmartPtr<Vst::IHostApplication> host;
  1130. //==============================================================================
  1131. struct ClassEntry
  1132. {
  1133. ClassEntry() noexcept : createFunction (nullptr), isUnicode (false) {}
  1134. ClassEntry (const PClassInfo2& info, CreateFunction fn) noexcept
  1135. : info2 (info), createFunction (fn), isUnicode (false) {}
  1136. PClassInfo2 info2;
  1137. PClassInfoW infoW;
  1138. CreateFunction createFunction;
  1139. bool isUnicode;
  1140. private:
  1141. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ClassEntry)
  1142. };
  1143. OwnedArray<ClassEntry> classes;
  1144. //==============================================================================
  1145. template<class PClassInfoType>
  1146. tresult PLUGIN_API getPClassInfo (Steinberg::int32 index, PClassInfoType* info)
  1147. {
  1148. if (info != nullptr)
  1149. {
  1150. zerostruct (*info);
  1151. if (ClassEntry* entry = classes[(int) index])
  1152. {
  1153. if (entry->isUnicode)
  1154. return kResultFalse;
  1155. std::memcpy (info, &entry->info2, sizeof (PClassInfoType));
  1156. return kResultOk;
  1157. }
  1158. }
  1159. jassertfalse;
  1160. return kInvalidArgument;
  1161. }
  1162. //==============================================================================
  1163. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (JucePluginFactory)
  1164. };
  1165. //==============================================================================
  1166. #ifndef JucePlugin_Vst3ComponentFlags
  1167. #if JucePlugin_IsSynth
  1168. #define JucePlugin_Vst3ComponentFlags Vst::kSimpleModeSupported
  1169. #else
  1170. #define JucePlugin_Vst3ComponentFlags 0
  1171. #endif
  1172. #endif
  1173. #ifndef JucePlugin_Vst3Category
  1174. #if JucePlugin_IsSynth
  1175. #define JucePlugin_Vst3Category Vst::PlugType::kInstrumentSynth
  1176. #else
  1177. #define JucePlugin_Vst3Category Vst::PlugType::kFx
  1178. #endif
  1179. #endif
  1180. //==============================================================================
  1181. // The VST3 plugin entry point.
  1182. JUCE_EXPORTED_FUNCTION IPluginFactory* PLUGIN_API GetPluginFactory()
  1183. {
  1184. #if JUCE_WINDOWS
  1185. // Cunning trick to force this function to be exported. Life's too short to
  1186. // faff around creating .def files for this kind of thing.
  1187. #pragma comment(linker, "/EXPORT:" __FUNCTION__ "=" __FUNCDNAME__)
  1188. #endif
  1189. if (globalFactory == nullptr)
  1190. {
  1191. globalFactory = new JucePluginFactory();
  1192. TUID componentTUID;
  1193. componentFUID.toTUID (componentTUID);
  1194. static const PClassInfo2 componentClass (componentTUID,
  1195. PClassInfo::kManyInstances,
  1196. kVstAudioEffectClass,
  1197. JucePlugin_Name,
  1198. JucePlugin_Vst3ComponentFlags,
  1199. JucePlugin_Vst3Category,
  1200. JucePlugin_Manufacturer,
  1201. JucePlugin_VersionString,
  1202. kVstVersionString);
  1203. globalFactory->registerClass (componentClass, createComponentInstance);
  1204. TUID controllerTUID;
  1205. controllerFUID.toTUID (controllerTUID);
  1206. static const PClassInfo2 controllerClass (controllerTUID,
  1207. PClassInfo::kManyInstances,
  1208. kVstComponentControllerClass,
  1209. JucePlugin_Name,
  1210. JucePlugin_Vst3ComponentFlags,
  1211. JucePlugin_Vst3Category,
  1212. JucePlugin_Manufacturer,
  1213. JucePlugin_VersionString,
  1214. kVstVersionString);
  1215. globalFactory->registerClass (controllerClass, createControllerInstance);
  1216. }
  1217. else
  1218. {
  1219. globalFactory->addRef();
  1220. }
  1221. return dynamic_cast<IPluginFactory*> (globalFactory);
  1222. }
  1223. #endif //JucePlugin_Build_VST3