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.

1545 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. #include "../utility/juce_CheckSettingMacros.h"
  21. #if JucePlugin_Build_VST
  22. #ifdef _MSC_VER
  23. #pragma warning (disable : 4996 4100)
  24. #endif
  25. #ifdef _WIN32
  26. #undef _WIN32_WINNT
  27. #define _WIN32_WINNT 0x500
  28. #undef STRICT
  29. #define STRICT 1
  30. #include <windows.h>
  31. #elif defined (LINUX)
  32. #include <X11/Xlib.h>
  33. #include <X11/Xutil.h>
  34. #include <X11/Xatom.h>
  35. #undef KeyPress
  36. #else
  37. #include <Carbon/Carbon.h>
  38. #endif
  39. #ifdef PRAGMA_ALIGN_SUPPORTED
  40. #undef PRAGMA_ALIGN_SUPPORTED
  41. #define PRAGMA_ALIGN_SUPPORTED 1
  42. #endif
  43. //==============================================================================
  44. #ifndef _MSC_VER
  45. #define __cdecl
  46. #endif
  47. #ifdef __clang__
  48. #pragma clang diagnostic push
  49. #pragma clang diagnostic ignored "-Wconversion"
  50. #pragma clang diagnostic ignored "-Wshadow"
  51. #pragma clang diagnostic ignored "-Wdeprecated-register"
  52. #pragma clang diagnostic ignored "-Wunused-parameter"
  53. #pragma clang diagnostic ignored "-Wdeprecated-writable-strings"
  54. #endif
  55. /* These files come with the Steinberg VST SDK - to get them, you'll need to
  56. visit the Steinberg website and agree to whatever is currently required to
  57. get them. The best version to get is the VST3 SDK, which also contains
  58. the older VST2.4 files.
  59. Then, you'll need to make sure your include path contains your "VST SDK3"
  60. directory (or whatever you've named it on your machine). The introjucer has
  61. a special box for setting this path.
  62. */
  63. #include <public.sdk/source/vst2.x/audioeffectx.h>
  64. #include <public.sdk/source/vst2.x/aeffeditor.h>
  65. #include <public.sdk/source/vst2.x/audioeffectx.cpp>
  66. #include <public.sdk/source/vst2.x/audioeffect.cpp>
  67. #if ! VST_2_4_EXTENSIONS
  68. #error "It looks like you're trying to include an out-of-date VSTSDK version - make sure you have at least version 2.4"
  69. #endif
  70. #ifdef __clang__
  71. #pragma clang diagnostic pop
  72. #endif
  73. //==============================================================================
  74. #ifdef _MSC_VER
  75. #pragma pack (push, 8)
  76. #endif
  77. #include "../utility/juce_IncludeModuleHeaders.h"
  78. #include "../utility/juce_FakeMouseMoveGenerator.h"
  79. #include "../utility/juce_WindowsHooks.h"
  80. #ifdef _MSC_VER
  81. #pragma pack (pop)
  82. #endif
  83. #undef MemoryBlock
  84. class JuceVSTWrapper;
  85. static bool recursionCheck = false;
  86. static juce::uint32 lastMasterIdleCall = 0;
  87. namespace juce
  88. {
  89. #if JUCE_MAC
  90. extern void initialiseMac();
  91. extern void* attachComponentToWindowRef (Component*, void* parent, bool isNSView);
  92. extern void detachComponentFromWindowRef (Component*, void* window, bool isNSView);
  93. extern void setNativeHostWindowSize (void* window, Component*, int newWidth, int newHeight, bool isNSView);
  94. extern void checkWindowVisibility (void* window, Component*, bool isNSView);
  95. extern bool forwardCurrentKeyEventToHost (Component*, bool isNSView);
  96. #if ! JUCE_64BIT
  97. extern void updateEditorCompBounds (Component*);
  98. #endif
  99. #endif
  100. #if JUCE_LINUX
  101. extern Display* display;
  102. #endif
  103. }
  104. //==============================================================================
  105. #if JUCE_WINDOWS
  106. namespace
  107. {
  108. HWND findMDIParentOf (HWND w)
  109. {
  110. const int frameThickness = GetSystemMetrics (SM_CYFIXEDFRAME);
  111. while (w != 0)
  112. {
  113. HWND parent = GetParent (w);
  114. if (parent == 0)
  115. break;
  116. TCHAR windowType[32] = { 0 };
  117. GetClassName (parent, windowType, 31);
  118. if (String (windowType).equalsIgnoreCase ("MDIClient"))
  119. return parent;
  120. RECT windowPos, parentPos;
  121. GetWindowRect (w, &windowPos);
  122. GetWindowRect (parent, &parentPos);
  123. const int dw = (parentPos.right - parentPos.left) - (windowPos.right - windowPos.left);
  124. const int dh = (parentPos.bottom - parentPos.top) - (windowPos.bottom - windowPos.top);
  125. if (dw > 100 || dh > 100)
  126. break;
  127. w = parent;
  128. if (dw == 2 * frameThickness)
  129. break;
  130. }
  131. return w;
  132. }
  133. static bool messageThreadIsDefinitelyCorrect = false;
  134. }
  135. //==============================================================================
  136. #elif JUCE_LINUX
  137. class SharedMessageThread : public Thread
  138. {
  139. public:
  140. SharedMessageThread()
  141. : Thread ("VstMessageThread"),
  142. initialised (false)
  143. {
  144. startThread (7);
  145. while (! initialised)
  146. sleep (1);
  147. }
  148. ~SharedMessageThread()
  149. {
  150. signalThreadShouldExit();
  151. JUCEApplicationBase::quit();
  152. waitForThreadToExit (5000);
  153. clearSingletonInstance();
  154. }
  155. void run() override
  156. {
  157. initialiseJuce_GUI();
  158. initialised = true;
  159. MessageManager::getInstance()->setCurrentThreadAsMessageThread();
  160. while ((! threadShouldExit()) && MessageManager::getInstance()->runDispatchLoopUntil (250))
  161. {}
  162. }
  163. juce_DeclareSingleton (SharedMessageThread, false);
  164. private:
  165. bool initialised;
  166. };
  167. juce_ImplementSingleton (SharedMessageThread)
  168. #endif
  169. static Array<void*> activePlugins;
  170. //==============================================================================
  171. /**
  172. This is an AudioEffectX object that holds and wraps our AudioProcessor...
  173. */
  174. class JuceVSTWrapper : public AudioEffectX,
  175. private Timer,
  176. public AudioProcessorListener,
  177. public AudioPlayHead
  178. {
  179. public:
  180. //==============================================================================
  181. JuceVSTWrapper (audioMasterCallback audioMaster, AudioProcessor* const af)
  182. : AudioEffectX (audioMaster, af->getNumPrograms(), af->getNumParameters()),
  183. filter (af),
  184. chunkMemoryTime (0),
  185. speakerIn (kSpeakerArrEmpty),
  186. speakerOut (kSpeakerArrEmpty),
  187. numInChans (JucePlugin_MaxNumInputChannels),
  188. numOutChans (JucePlugin_MaxNumOutputChannels),
  189. isProcessing (false),
  190. isBypassed (false),
  191. hasShutdown (false),
  192. firstProcessCallback (true),
  193. shouldDeleteEditor (false),
  194. #if JUCE_64BIT
  195. useNSView (true),
  196. #else
  197. useNSView (false),
  198. #endif
  199. processTempBuffer (1, 1),
  200. hostWindow (0)
  201. {
  202. filter->setPlayConfigDetails (numInChans, numOutChans, 0, 0);
  203. filter->setPlayHead (this);
  204. filter->addListener (this);
  205. cEffect.flags |= effFlagsHasEditor;
  206. cEffect.version = convertHexVersionToDecimal (JucePlugin_VersionCode);
  207. setUniqueID ((int) (JucePlugin_VSTUniqueID));
  208. setNumInputs (numInChans);
  209. setNumOutputs (numOutChans);
  210. canProcessReplacing (true);
  211. isSynth ((JucePlugin_IsSynth) != 0);
  212. noTail (filter->getTailLengthSeconds() <= 0);
  213. setInitialDelay (filter->getLatencySamples());
  214. programsAreChunks (true);
  215. activePlugins.add (this);
  216. }
  217. ~JuceVSTWrapper()
  218. {
  219. JUCE_AUTORELEASEPOOL
  220. {
  221. {
  222. #if JUCE_LINUX
  223. MessageManagerLock mmLock;
  224. #endif
  225. stopTimer();
  226. deleteEditor (false);
  227. hasShutdown = true;
  228. delete filter;
  229. filter = nullptr;
  230. jassert (editorComp == 0);
  231. channels.free();
  232. deleteTempChannels();
  233. jassert (activePlugins.contains (this));
  234. activePlugins.removeFirstMatchingValue (this);
  235. }
  236. if (activePlugins.size() == 0)
  237. {
  238. #if JUCE_LINUX
  239. SharedMessageThread::deleteInstance();
  240. #endif
  241. shutdownJuce_GUI();
  242. #if JUCE_WINDOWS
  243. messageThreadIsDefinitelyCorrect = false;
  244. #endif
  245. }
  246. }
  247. }
  248. void open()
  249. {
  250. // Note: most hosts call this on the UI thread, but wavelab doesn't, so be careful in here.
  251. if (filter->hasEditor())
  252. cEffect.flags |= effFlagsHasEditor;
  253. else
  254. cEffect.flags &= ~effFlagsHasEditor;
  255. }
  256. void close()
  257. {
  258. // Note: most hosts call this on the UI thread, but wavelab doesn't, so be careful in here.
  259. stopTimer();
  260. if (MessageManager::getInstance()->isThisTheMessageThread())
  261. deleteEditor (false);
  262. }
  263. //==============================================================================
  264. bool getEffectName (char* name) override
  265. {
  266. String (JucePlugin_Name).copyToUTF8 (name, 64);
  267. return true;
  268. }
  269. bool getVendorString (char* text) override
  270. {
  271. String (JucePlugin_Manufacturer).copyToUTF8 (text, 64);
  272. return true;
  273. }
  274. bool getProductString (char* text) override { return getEffectName (text); }
  275. VstInt32 getVendorVersion() override { return convertHexVersionToDecimal (JucePlugin_VersionCode); }
  276. VstPlugCategory getPlugCategory() override { return JucePlugin_VSTCategory; }
  277. bool keysRequired() { return (JucePlugin_EditorRequiresKeyboardFocus) != 0; }
  278. VstInt32 canDo (char* text) override
  279. {
  280. if (strcmp (text, "receiveVstEvents") == 0
  281. || strcmp (text, "receiveVstMidiEvent") == 0
  282. || strcmp (text, "receiveVstMidiEvents") == 0)
  283. {
  284. #if JucePlugin_WantsMidiInput
  285. return 1;
  286. #else
  287. return -1;
  288. #endif
  289. }
  290. if (strcmp (text, "sendVstEvents") == 0
  291. || strcmp (text, "sendVstMidiEvent") == 0
  292. || strcmp (text, "sendVstMidiEvents") == 0)
  293. {
  294. #if JucePlugin_ProducesMidiOutput
  295. return 1;
  296. #else
  297. return -1;
  298. #endif
  299. }
  300. if (strcmp (text, "receiveVstTimeInfo") == 0
  301. || strcmp (text, "conformsToWindowRules") == 0
  302. || strcmp (text, "bypass") == 0)
  303. {
  304. return 1;
  305. }
  306. if (strcmp (text, "openCloseAnyThread") == 0)
  307. {
  308. // This tells Wavelab to use the UI thread to invoke open/close,
  309. // like all other hosts do.
  310. return -1;
  311. }
  312. #if JUCE_MAC
  313. if (strcmp (text, "hasCockosViewAsConfig") == 0)
  314. {
  315. useNSView = true;
  316. return 0xbeef0000;
  317. }
  318. #endif
  319. return 0;
  320. }
  321. bool getInputProperties (VstInt32 index, VstPinProperties* properties)
  322. {
  323. if (filter == nullptr || index >= JucePlugin_MaxNumInputChannels)
  324. return false;
  325. setPinProperties (*properties, filter->getInputChannelName ((int) index),
  326. speakerIn, filter->isInputChannelStereoPair ((int) index));
  327. return true;
  328. }
  329. bool getOutputProperties (VstInt32 index, VstPinProperties* properties)
  330. {
  331. if (filter == nullptr || index >= JucePlugin_MaxNumOutputChannels)
  332. return false;
  333. setPinProperties (*properties, filter->getOutputChannelName ((int) index),
  334. speakerOut, filter->isOutputChannelStereoPair ((int) index));
  335. return true;
  336. }
  337. static void setPinProperties (VstPinProperties& properties, const String& name,
  338. VstSpeakerArrangementType type, const bool isPair)
  339. {
  340. name.copyToUTF8 (properties.label, (size_t) (kVstMaxLabelLen - 1));
  341. name.copyToUTF8 (properties.shortLabel, (size_t) (kVstMaxShortLabelLen - 1));
  342. if (type != kSpeakerArrEmpty)
  343. {
  344. properties.flags = kVstPinUseSpeaker;
  345. properties.arrangementType = type;
  346. }
  347. else
  348. {
  349. properties.flags = kVstPinIsActive;
  350. properties.arrangementType = 0;
  351. if (isPair)
  352. properties.flags |= kVstPinIsStereo;
  353. }
  354. }
  355. bool setBypass (bool b)
  356. {
  357. isBypassed = b;
  358. return true;
  359. }
  360. VstInt32 getGetTailSize()
  361. {
  362. if (filter != nullptr)
  363. return (VstInt32) (filter->getTailLengthSeconds() * getSampleRate());
  364. return 0;
  365. }
  366. //==============================================================================
  367. VstInt32 processEvents (VstEvents* events)
  368. {
  369. #if JucePlugin_WantsMidiInput
  370. VSTMidiEventList::addEventsToMidiBuffer (events, midiEvents);
  371. return 1;
  372. #else
  373. (void) events;
  374. return 0;
  375. #endif
  376. }
  377. void process (float** inputs, float** outputs, VstInt32 numSamples)
  378. {
  379. const int numIn = numInChans;
  380. const int numOut = numOutChans;
  381. processTempBuffer.setSize (numIn, numSamples, false, false, true);
  382. for (int i = numIn; --i >= 0;)
  383. processTempBuffer.copyFrom (i, 0, outputs[i], numSamples);
  384. processReplacing (inputs, outputs, numSamples);
  385. AudioSampleBuffer dest (outputs, numOut, numSamples);
  386. for (int i = jmin (numIn, numOut); --i >= 0;)
  387. dest.addFrom (i, 0, processTempBuffer, i, 0, numSamples);
  388. }
  389. void processReplacing (float** inputs, float** outputs, VstInt32 numSamples)
  390. {
  391. if (firstProcessCallback)
  392. {
  393. firstProcessCallback = false;
  394. // if this fails, the host hasn't called resume() before processing
  395. jassert (isProcessing);
  396. // (tragically, some hosts actually need this, although it's stupid to have
  397. // to do it here..)
  398. if (! isProcessing)
  399. resume();
  400. filter->setNonRealtime (getCurrentProcessLevel() == 4 /* kVstProcessLevelOffline */);
  401. #if JUCE_WINDOWS
  402. if (GetThreadPriority (GetCurrentThread()) <= THREAD_PRIORITY_NORMAL
  403. && GetThreadPriority (GetCurrentThread()) >= THREAD_PRIORITY_LOWEST)
  404. filter->setNonRealtime (true);
  405. #endif
  406. }
  407. #if JUCE_DEBUG && ! JucePlugin_ProducesMidiOutput
  408. const int numMidiEventsComingIn = midiEvents.getNumEvents();
  409. #endif
  410. jassert (activePlugins.contains (this));
  411. {
  412. const ScopedLock sl (filter->getCallbackLock());
  413. const int numIn = numInChans;
  414. const int numOut = numOutChans;
  415. if (filter->isSuspended())
  416. {
  417. for (int i = 0; i < numOut; ++i)
  418. FloatVectorOperations::clear (outputs[i], numSamples);
  419. }
  420. else
  421. {
  422. int i;
  423. for (i = 0; i < numOut; ++i)
  424. {
  425. float* chan = tempChannels.getUnchecked(i);
  426. if (chan == nullptr)
  427. {
  428. chan = outputs[i];
  429. // if some output channels are disabled, some hosts supply the same buffer
  430. // for multiple channels - this buggers up our method of copying the
  431. // inputs over the outputs, so we need to create unique temp buffers in this case..
  432. for (int j = i; --j >= 0;)
  433. {
  434. if (outputs[j] == chan)
  435. {
  436. chan = new float [blockSize * 2];
  437. tempChannels.set (i, chan);
  438. break;
  439. }
  440. }
  441. }
  442. if (i < numIn && chan != inputs[i])
  443. memcpy (chan, inputs[i], sizeof (float) * (size_t) numSamples);
  444. channels[i] = chan;
  445. }
  446. for (; i < numIn; ++i)
  447. channels[i] = inputs[i];
  448. {
  449. AudioSampleBuffer chans (channels, jmax (numIn, numOut), numSamples);
  450. if (isBypassed)
  451. filter->processBlockBypassed (chans, midiEvents);
  452. else
  453. filter->processBlock (chans, midiEvents);
  454. }
  455. // copy back any temp channels that may have been used..
  456. for (i = 0; i < numOut; ++i)
  457. if (const float* const chan = tempChannels.getUnchecked(i))
  458. memcpy (outputs[i], chan, sizeof (float) * (size_t) numSamples);
  459. }
  460. }
  461. if (! midiEvents.isEmpty())
  462. {
  463. #if JucePlugin_ProducesMidiOutput
  464. const int numEvents = midiEvents.getNumEvents();
  465. outgoingEvents.ensureSize (numEvents);
  466. outgoingEvents.clear();
  467. const juce::uint8* midiEventData;
  468. int midiEventSize, midiEventPosition;
  469. MidiBuffer::Iterator i (midiEvents);
  470. while (i.getNextEvent (midiEventData, midiEventSize, midiEventPosition))
  471. {
  472. jassert (midiEventPosition >= 0 && midiEventPosition < numSamples);
  473. outgoingEvents.addEvent (midiEventData, midiEventSize, midiEventPosition);
  474. }
  475. sendVstEventsToHost (outgoingEvents.events);
  476. #elif JUCE_DEBUG
  477. /* This assertion is caused when you've added some events to the
  478. midiMessages array in your processBlock() method, which usually means
  479. that you're trying to send them somewhere. But in this case they're
  480. getting thrown away.
  481. If your plugin does want to send midi messages, you'll need to set
  482. the JucePlugin_ProducesMidiOutput macro to 1 in your
  483. JucePluginCharacteristics.h file.
  484. If you don't want to produce any midi output, then you should clear the
  485. midiMessages array at the end of your processBlock() method, to
  486. indicate that you don't want any of the events to be passed through
  487. to the output.
  488. */
  489. jassert (midiEvents.getNumEvents() <= numMidiEventsComingIn);
  490. #endif
  491. midiEvents.clear();
  492. }
  493. }
  494. //==============================================================================
  495. VstInt32 startProcess() { return 0; }
  496. VstInt32 stopProcess() { return 0; }
  497. void resume()
  498. {
  499. if (filter != nullptr)
  500. {
  501. isProcessing = true;
  502. channels.calloc ((size_t) (numInChans + numOutChans));
  503. double rate = getSampleRate();
  504. jassert (rate > 0);
  505. if (rate <= 0.0)
  506. rate = 44100.0;
  507. const int blockSize = getBlockSize();
  508. jassert (blockSize > 0);
  509. firstProcessCallback = true;
  510. filter->setNonRealtime (getCurrentProcessLevel() == 4 /* kVstProcessLevelOffline */);
  511. filter->setPlayConfigDetails (numInChans, numOutChans, rate, blockSize);
  512. deleteTempChannels();
  513. filter->prepareToPlay (rate, blockSize);
  514. midiEvents.ensureSize (2048);
  515. midiEvents.clear();
  516. setInitialDelay (filter->getLatencySamples());
  517. AudioEffectX::resume();
  518. #if JucePlugin_ProducesMidiOutput
  519. outgoingEvents.ensureSize (512);
  520. #endif
  521. }
  522. }
  523. void suspend()
  524. {
  525. if (filter != nullptr)
  526. {
  527. AudioEffectX::suspend();
  528. filter->releaseResources();
  529. outgoingEvents.freeEvents();
  530. isProcessing = false;
  531. channels.free();
  532. deleteTempChannels();
  533. }
  534. }
  535. bool getCurrentPosition (AudioPlayHead::CurrentPositionInfo& info)
  536. {
  537. const VstTimeInfo* const ti = getTimeInfo (kVstPpqPosValid | kVstTempoValid | kVstBarsValid | kVstCyclePosValid
  538. | kVstTimeSigValid | kVstSmpteValid | kVstClockValid);
  539. if (ti == nullptr || ti->sampleRate <= 0)
  540. return false;
  541. info.bpm = (ti->flags & kVstTempoValid) != 0 ? ti->tempo : 0.0;
  542. if ((ti->flags & kVstTimeSigValid) != 0)
  543. {
  544. info.timeSigNumerator = ti->timeSigNumerator;
  545. info.timeSigDenominator = ti->timeSigDenominator;
  546. }
  547. else
  548. {
  549. info.timeSigNumerator = 4;
  550. info.timeSigDenominator = 4;
  551. }
  552. info.timeInSamples = (int64) (ti->samplePos + 0.5);
  553. info.timeInSeconds = ti->samplePos / ti->sampleRate;
  554. info.ppqPosition = (ti->flags & kVstPpqPosValid) != 0 ? ti->ppqPos : 0.0;
  555. info.ppqPositionOfLastBarStart = (ti->flags & kVstBarsValid) != 0 ? ti->barStartPos : 0.0;
  556. if ((ti->flags & kVstSmpteValid) != 0)
  557. {
  558. AudioPlayHead::FrameRateType rate = AudioPlayHead::fpsUnknown;
  559. double fps = 1.0;
  560. switch (ti->smpteFrameRate)
  561. {
  562. case kVstSmpte24fps: rate = AudioPlayHead::fps24; fps = 24.0; break;
  563. case kVstSmpte25fps: rate = AudioPlayHead::fps25; fps = 25.0; break;
  564. case kVstSmpte2997fps: rate = AudioPlayHead::fps2997; fps = 29.97; break;
  565. case kVstSmpte30fps: rate = AudioPlayHead::fps30; fps = 30.0; break;
  566. case kVstSmpte2997dfps: rate = AudioPlayHead::fps2997drop; fps = 29.97; break;
  567. case kVstSmpte30dfps: rate = AudioPlayHead::fps30drop; fps = 30.0; break;
  568. case kVstSmpteFilm16mm:
  569. case kVstSmpteFilm35mm: fps = 24.0; break;
  570. case kVstSmpte239fps: fps = 23.976; break;
  571. case kVstSmpte249fps: fps = 24.976; break;
  572. case kVstSmpte599fps: fps = 59.94; break;
  573. case kVstSmpte60fps: fps = 60; break;
  574. default: jassertfalse; // unknown frame-rate..
  575. }
  576. info.frameRate = rate;
  577. info.editOriginTime = ti->smpteOffset / (80.0 * fps);
  578. }
  579. else
  580. {
  581. info.frameRate = AudioPlayHead::fpsUnknown;
  582. info.editOriginTime = 0;
  583. }
  584. info.isRecording = (ti->flags & kVstTransportRecording) != 0;
  585. info.isPlaying = (ti->flags & (kVstTransportRecording | kVstTransportPlaying)) != 0;
  586. info.isLooping = (ti->flags & kVstTransportCycleActive) != 0;
  587. if ((ti->flags & kVstCyclePosValid) != 0)
  588. {
  589. info.ppqLoopStart = ti->cycleStartPos;
  590. info.ppqLoopEnd = ti->cycleEndPos;
  591. }
  592. else
  593. {
  594. info.ppqLoopStart = 0;
  595. info.ppqLoopEnd = 0;
  596. }
  597. return true;
  598. }
  599. //==============================================================================
  600. VstInt32 getProgram()
  601. {
  602. return filter != nullptr ? filter->getCurrentProgram() : 0;
  603. }
  604. void setProgram (VstInt32 program)
  605. {
  606. if (filter != nullptr)
  607. filter->setCurrentProgram (program);
  608. }
  609. void setProgramName (char* name)
  610. {
  611. if (filter != nullptr)
  612. filter->changeProgramName (filter->getCurrentProgram(), name);
  613. }
  614. void getProgramName (char* name)
  615. {
  616. if (filter != nullptr)
  617. filter->getProgramName (filter->getCurrentProgram()).copyToUTF8 (name, 24);
  618. }
  619. bool getProgramNameIndexed (VstInt32 /*category*/, VstInt32 index, char* text)
  620. {
  621. if (filter != nullptr && isPositiveAndBelow (index, filter->getNumPrograms()))
  622. {
  623. filter->getProgramName (index).copyToUTF8 (text, 24);
  624. return true;
  625. }
  626. return false;
  627. }
  628. //==============================================================================
  629. float getParameter (VstInt32 index)
  630. {
  631. if (filter == nullptr)
  632. return 0.0f;
  633. jassert (isPositiveAndBelow (index, filter->getNumParameters()));
  634. return filter->getParameter (index);
  635. }
  636. void setParameter (VstInt32 index, float value)
  637. {
  638. if (filter != nullptr)
  639. {
  640. jassert (isPositiveAndBelow (index, filter->getNumParameters()));
  641. filter->setParameter (index, value);
  642. }
  643. }
  644. void getParameterDisplay (VstInt32 index, char* text)
  645. {
  646. if (filter != nullptr)
  647. {
  648. jassert (isPositiveAndBelow (index, filter->getNumParameters()));
  649. filter->getParameterText (index, 24).copyToUTF8 (text, 24); // length should technically be kVstMaxParamStrLen, which is 8, but hosts will normally allow a bit more.
  650. }
  651. }
  652. void getParameterName (VstInt32 index, char* text)
  653. {
  654. if (filter != nullptr)
  655. {
  656. jassert (isPositiveAndBelow (index, filter->getNumParameters()));
  657. filter->getParameterName (index, 16).copyToUTF8 (text, 16); // length should technically be kVstMaxParamStrLen, which is 8, but hosts will normally allow a bit more.
  658. }
  659. }
  660. void audioProcessorParameterChanged (AudioProcessor*, int index, float newValue)
  661. {
  662. if (audioMaster != nullptr)
  663. audioMaster (&cEffect, audioMasterAutomate, index, 0, 0, newValue);
  664. }
  665. void audioProcessorParameterChangeGestureBegin (AudioProcessor*, int index) { beginEdit (index); }
  666. void audioProcessorParameterChangeGestureEnd (AudioProcessor*, int index) { endEdit (index); }
  667. void audioProcessorChanged (AudioProcessor*)
  668. {
  669. setInitialDelay (filter->getLatencySamples());
  670. ioChanged();
  671. updateDisplay();
  672. }
  673. bool canParameterBeAutomated (VstInt32 index)
  674. {
  675. return filter != nullptr && filter->isParameterAutomatable ((int) index);
  676. }
  677. struct ChannelConfigComparator
  678. {
  679. static int compareElements (const short* const first, const short* const second) noexcept
  680. {
  681. if (first[0] < second[0]) return -1;
  682. if (first[0] > second[0]) return 1;
  683. if (first[1] < second[1]) return -1;
  684. if (first[1] > second[1]) return 1;
  685. return 0;
  686. }
  687. };
  688. bool setSpeakerArrangement (VstSpeakerArrangement* pluginInput,
  689. VstSpeakerArrangement* pluginOutput)
  690. {
  691. short channelConfigs[][2] = { JucePlugin_PreferredChannelConfigurations };
  692. Array <short*> channelConfigsSorted;
  693. ChannelConfigComparator comp;
  694. for (int i = 0; i < numElementsInArray (channelConfigs); ++i)
  695. channelConfigsSorted.addSorted (comp, channelConfigs[i]);
  696. for (int i = channelConfigsSorted.size(); --i >= 0;)
  697. {
  698. const short* const config = channelConfigsSorted.getUnchecked(i);
  699. bool inCountMatches = (config[0] == pluginInput->numChannels);
  700. bool outCountMatches = (config[1] == pluginOutput->numChannels);
  701. if (inCountMatches && outCountMatches)
  702. {
  703. speakerIn = (VstSpeakerArrangementType) pluginInput->type;
  704. speakerOut = (VstSpeakerArrangementType) pluginOutput->type;
  705. numInChans = pluginInput->numChannels;
  706. numOutChans = pluginOutput->numChannels;
  707. filter->setPlayConfigDetails (numInChans, numOutChans,
  708. filter->getSampleRate(),
  709. filter->getBlockSize());
  710. filter->setSpeakerArrangement (getSpeakerArrangementString (speakerIn),
  711. getSpeakerArrangementString (speakerOut));
  712. return true;
  713. }
  714. }
  715. filter->setSpeakerArrangement (String::empty, String::empty);
  716. return false;
  717. }
  718. static const char* getSpeakerArrangementString (VstSpeakerArrangementType type) noexcept
  719. {
  720. switch (type)
  721. {
  722. case kSpeakerArrMono: return "M";
  723. case kSpeakerArrStereo: return "L R";
  724. case kSpeakerArrStereoSurround: return "Ls Rs";
  725. case kSpeakerArrStereoCenter: return "Lc Rc";
  726. case kSpeakerArrStereoSide: return "Sl Sr";
  727. case kSpeakerArrStereoCLfe: return "C Lfe";
  728. case kSpeakerArr30Cine: return "L R C";
  729. case kSpeakerArr30Music: return "L R S";
  730. case kSpeakerArr31Cine: return "L R C Lfe";
  731. case kSpeakerArr31Music: return "L R Lfe S";
  732. case kSpeakerArr40Cine: return "L R C S";
  733. case kSpeakerArr40Music: return "L R Ls Rs";
  734. case kSpeakerArr41Cine: return "L R C Lfe S";
  735. case kSpeakerArr41Music: return "L R Lfe Ls Rs";
  736. case kSpeakerArr50: return "L R C Ls Rs" ;
  737. case kSpeakerArr51: return "L R C Lfe Ls Rs";
  738. case kSpeakerArr60Cine: return "L R C Ls Rs Cs";
  739. case kSpeakerArr60Music: return "L R Ls Rs Sl Sr ";
  740. case kSpeakerArr61Cine: return "L R C Lfe Ls Rs Cs";
  741. case kSpeakerArr61Music: return "L R Lfe Ls Rs Sl Sr";
  742. case kSpeakerArr70Cine: return "L R C Ls Rs Lc Rc ";
  743. case kSpeakerArr70Music: return "L R C Ls Rs Sl Sr";
  744. case kSpeakerArr71Cine: return "L R C Lfe Ls Rs Lc Rc";
  745. case kSpeakerArr71Music: return "L R C Lfe Ls Rs Sl Sr";
  746. case kSpeakerArr80Cine: return "L R C Ls Rs Lc Rc Cs";
  747. case kSpeakerArr80Music: return "L R C Ls Rs Cs Sl Sr";
  748. case kSpeakerArr81Cine: return "L R C Lfe Ls Rs Lc Rc Cs";
  749. case kSpeakerArr81Music: return "L R C Lfe Ls Rs Cs Sl Sr" ;
  750. case kSpeakerArr102: return "L R C Lfe Ls Rs Tfl Tfc Tfr Trl Trr Lfe2";
  751. default: break;
  752. }
  753. return nullptr;
  754. }
  755. //==============================================================================
  756. VstInt32 getChunk (void** data, bool onlyStoreCurrentProgramData)
  757. {
  758. if (filter == nullptr)
  759. return 0;
  760. chunkMemory.reset();
  761. if (onlyStoreCurrentProgramData)
  762. filter->getCurrentProgramStateInformation (chunkMemory);
  763. else
  764. filter->getStateInformation (chunkMemory);
  765. *data = (void*) chunkMemory.getData();
  766. // because the chunk is only needed temporarily by the host (or at least you'd
  767. // hope so) we'll give it a while and then free it in the timer callback.
  768. chunkMemoryTime = juce::Time::getApproximateMillisecondCounter();
  769. return (VstInt32) chunkMemory.getSize();
  770. }
  771. VstInt32 setChunk (void* data, VstInt32 byteSize, bool onlyRestoreCurrentProgramData)
  772. {
  773. if (filter != nullptr)
  774. {
  775. chunkMemory.reset();
  776. chunkMemoryTime = 0;
  777. if (byteSize > 0 && data != nullptr)
  778. {
  779. if (onlyRestoreCurrentProgramData)
  780. filter->setCurrentProgramStateInformation (data, byteSize);
  781. else
  782. filter->setStateInformation (data, byteSize);
  783. }
  784. }
  785. return 0;
  786. }
  787. void timerCallback() override
  788. {
  789. if (shouldDeleteEditor)
  790. {
  791. shouldDeleteEditor = false;
  792. deleteEditor (true);
  793. }
  794. if (chunkMemoryTime > 0
  795. && chunkMemoryTime < juce::Time::getApproximateMillisecondCounter() - 2000
  796. && ! recursionCheck)
  797. {
  798. chunkMemory.reset();
  799. chunkMemoryTime = 0;
  800. }
  801. #if JUCE_MAC
  802. if (hostWindow != 0)
  803. checkWindowVisibility (hostWindow, editorComp, useNSView);
  804. #endif
  805. tryMasterIdle();
  806. }
  807. void tryMasterIdle()
  808. {
  809. if (Component::isMouseButtonDownAnywhere() && ! recursionCheck)
  810. {
  811. const juce::uint32 now = juce::Time::getMillisecondCounter();
  812. if (now > lastMasterIdleCall + 20 && editorComp != nullptr)
  813. {
  814. lastMasterIdleCall = now;
  815. recursionCheck = true;
  816. masterIdle();
  817. recursionCheck = false;
  818. }
  819. }
  820. }
  821. void doIdleCallback()
  822. {
  823. // (wavelab calls this on a separate thread and causes a deadlock)..
  824. if (MessageManager::getInstance()->isThisTheMessageThread()
  825. && ! recursionCheck)
  826. {
  827. recursionCheck = true;
  828. JUCE_AUTORELEASEPOOL
  829. {
  830. Timer::callPendingTimersSynchronously();
  831. for (int i = ComponentPeer::getNumPeers(); --i >= 0;)
  832. ComponentPeer::getPeer (i)->performAnyPendingRepaintsNow();
  833. recursionCheck = false;
  834. }
  835. }
  836. }
  837. void createEditorComp()
  838. {
  839. if (hasShutdown || filter == nullptr)
  840. return;
  841. if (editorComp == nullptr)
  842. {
  843. if (AudioProcessorEditor* const ed = filter->createEditorIfNeeded())
  844. {
  845. cEffect.flags |= effFlagsHasEditor;
  846. ed->setOpaque (true);
  847. ed->setVisible (true);
  848. editorComp = new EditorCompWrapper (*this, ed);
  849. }
  850. else
  851. {
  852. cEffect.flags &= ~effFlagsHasEditor;
  853. }
  854. }
  855. shouldDeleteEditor = false;
  856. }
  857. void deleteEditor (bool canDeleteLaterIfModal)
  858. {
  859. JUCE_AUTORELEASEPOOL
  860. {
  861. PopupMenu::dismissAllActiveMenus();
  862. jassert (! recursionCheck);
  863. recursionCheck = true;
  864. if (editorComp != nullptr)
  865. {
  866. if (Component* const modalComponent = Component::getCurrentlyModalComponent())
  867. {
  868. modalComponent->exitModalState (0);
  869. if (canDeleteLaterIfModal)
  870. {
  871. shouldDeleteEditor = true;
  872. recursionCheck = false;
  873. return;
  874. }
  875. }
  876. #if JUCE_MAC
  877. if (hostWindow != 0)
  878. {
  879. detachComponentFromWindowRef (editorComp, hostWindow, useNSView);
  880. hostWindow = 0;
  881. }
  882. #endif
  883. filter->editorBeingDeleted (editorComp->getEditorComp());
  884. editorComp = nullptr;
  885. // there's some kind of component currently modal, but the host
  886. // is trying to delete our plugin. You should try to avoid this happening..
  887. jassert (Component::getCurrentlyModalComponent() == nullptr);
  888. }
  889. #if JUCE_LINUX
  890. hostWindow = 0;
  891. #endif
  892. recursionCheck = false;
  893. }
  894. }
  895. VstIntPtr dispatcher (VstInt32 opCode, VstInt32 index, VstIntPtr value, void* ptr, float opt)
  896. {
  897. if (hasShutdown)
  898. return 0;
  899. if (opCode == effEditIdle)
  900. {
  901. doIdleCallback();
  902. return 0;
  903. }
  904. else if (opCode == effEditOpen)
  905. {
  906. checkWhetherMessageThreadIsCorrect();
  907. const MessageManagerLock mmLock;
  908. jassert (! recursionCheck);
  909. startTimer (1000 / 4); // performs misc housekeeping chores
  910. deleteEditor (true);
  911. createEditorComp();
  912. if (editorComp != nullptr)
  913. {
  914. editorComp->setOpaque (true);
  915. editorComp->setVisible (false);
  916. #if JUCE_WINDOWS
  917. editorComp->addToDesktop (0, ptr);
  918. hostWindow = (HWND) ptr;
  919. #elif JUCE_LINUX
  920. editorComp->addToDesktop (0);
  921. hostWindow = (Window) ptr;
  922. Window editorWnd = (Window) editorComp->getWindowHandle();
  923. XReparentWindow (display, editorWnd, hostWindow, 0, 0);
  924. #else
  925. hostWindow = attachComponentToWindowRef (editorComp, ptr, useNSView);
  926. #endif
  927. editorComp->setVisible (true);
  928. return 1;
  929. }
  930. }
  931. else if (opCode == effEditClose)
  932. {
  933. checkWhetherMessageThreadIsCorrect();
  934. const MessageManagerLock mmLock;
  935. deleteEditor (true);
  936. return 0;
  937. }
  938. else if (opCode == effEditGetRect)
  939. {
  940. checkWhetherMessageThreadIsCorrect();
  941. const MessageManagerLock mmLock;
  942. createEditorComp();
  943. if (editorComp != nullptr)
  944. {
  945. editorSize.left = 0;
  946. editorSize.top = 0;
  947. editorSize.right = (VstInt16) editorComp->getWidth();
  948. editorSize.bottom = (VstInt16) editorComp->getHeight();
  949. *((ERect**) ptr) = &editorSize;
  950. return (VstIntPtr) (pointer_sized_int) &editorSize;
  951. }
  952. return 0;
  953. }
  954. return AudioEffectX::dispatcher (opCode, index, value, ptr, opt);
  955. }
  956. void resizeHostWindow (int newWidth, int newHeight)
  957. {
  958. if (editorComp != nullptr)
  959. {
  960. if (! (canHostDo (const_cast <char*> ("sizeWindow")) && sizeWindow (newWidth, newHeight)))
  961. {
  962. // some hosts don't support the sizeWindow call, so do it manually..
  963. #if JUCE_MAC
  964. setNativeHostWindowSize (hostWindow, editorComp, newWidth, newHeight, useNSView);
  965. #elif JUCE_LINUX
  966. // (Currently, all linux hosts support sizeWindow, so this should never need to happen)
  967. editorComp->setSize (newWidth, newHeight);
  968. #else
  969. int dw = 0;
  970. int dh = 0;
  971. const int frameThickness = GetSystemMetrics (SM_CYFIXEDFRAME);
  972. HWND w = (HWND) editorComp->getWindowHandle();
  973. while (w != 0)
  974. {
  975. HWND parent = GetParent (w);
  976. if (parent == 0)
  977. break;
  978. TCHAR windowType [32] = { 0 };
  979. GetClassName (parent, windowType, 31);
  980. if (String (windowType).equalsIgnoreCase ("MDIClient"))
  981. break;
  982. RECT windowPos, parentPos;
  983. GetWindowRect (w, &windowPos);
  984. GetWindowRect (parent, &parentPos);
  985. SetWindowPos (w, 0, 0, 0, newWidth + dw, newHeight + dh,
  986. SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOOWNERZORDER);
  987. dw = (parentPos.right - parentPos.left) - (windowPos.right - windowPos.left);
  988. dh = (parentPos.bottom - parentPos.top) - (windowPos.bottom - windowPos.top);
  989. w = parent;
  990. if (dw == 2 * frameThickness)
  991. break;
  992. if (dw > 100 || dh > 100)
  993. w = 0;
  994. }
  995. if (w != 0)
  996. SetWindowPos (w, 0, 0, 0, newWidth + dw, newHeight + dh,
  997. SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOOWNERZORDER);
  998. #endif
  999. }
  1000. if (ComponentPeer* peer = editorComp->getPeer())
  1001. peer->handleMovedOrResized();
  1002. }
  1003. }
  1004. //==============================================================================
  1005. // A component to hold the AudioProcessorEditor, and cope with some housekeeping
  1006. // chores when it changes or repaints.
  1007. class EditorCompWrapper : public Component,
  1008. public AsyncUpdater
  1009. {
  1010. public:
  1011. EditorCompWrapper (JuceVSTWrapper& w, AudioProcessorEditor* editor)
  1012. : wrapper (w)
  1013. {
  1014. setOpaque (true);
  1015. editor->setOpaque (true);
  1016. setBounds (editor->getBounds());
  1017. editor->setTopLeftPosition (0, 0);
  1018. addAndMakeVisible (editor);
  1019. #if JUCE_WINDOWS
  1020. if (! getHostType().isReceptor())
  1021. addMouseListener (this, true);
  1022. #endif
  1023. }
  1024. ~EditorCompWrapper()
  1025. {
  1026. deleteAllChildren(); // note that we can't use a ScopedPointer because the editor may
  1027. // have been transferred to another parent which takes over ownership.
  1028. }
  1029. void paint (Graphics&) override {}
  1030. void paintOverChildren (Graphics&) override
  1031. {
  1032. // this causes an async call to masterIdle() to help
  1033. // creaky old DAWs like Nuendo repaint themselves while we're
  1034. // repainting. Otherwise they just seem to give up and sit there
  1035. // waiting.
  1036. triggerAsyncUpdate();
  1037. }
  1038. #if JUCE_MAC
  1039. bool keyPressed (const KeyPress&) override
  1040. {
  1041. // If we have an unused keypress, move the key-focus to a host window
  1042. // and re-inject the event..
  1043. return forwardCurrentKeyEventToHost (this, wrapper.useNSView);
  1044. }
  1045. #endif
  1046. AudioProcessorEditor* getEditorComp() const
  1047. {
  1048. return dynamic_cast<AudioProcessorEditor*> (getChildComponent(0));
  1049. }
  1050. void resized() override
  1051. {
  1052. if (Component* const editor = getChildComponent(0))
  1053. editor->setBounds (getLocalBounds());
  1054. #if JUCE_MAC && ! JUCE_64BIT
  1055. if (! wrapper.useNSView)
  1056. updateEditorCompBounds (this);
  1057. #endif
  1058. }
  1059. void childBoundsChanged (Component* child) override
  1060. {
  1061. child->setTopLeftPosition (0, 0);
  1062. const int cw = child->getWidth();
  1063. const int ch = child->getHeight();
  1064. #if JUCE_MAC
  1065. if (wrapper.useNSView)
  1066. setTopLeftPosition (0, getHeight() - ch);
  1067. #endif
  1068. wrapper.resizeHostWindow (cw, ch);
  1069. #if ! JUCE_LINUX // setSize() on linux causes renoise and energyxt to fail.
  1070. setSize (cw, ch);
  1071. #else
  1072. XResizeWindow (display, (Window) getWindowHandle(), cw, ch);
  1073. #endif
  1074. #if JUCE_MAC
  1075. wrapper.resizeHostWindow (cw, ch); // (doing this a second time seems to be necessary in tracktion)
  1076. #endif
  1077. }
  1078. void handleAsyncUpdate() override
  1079. {
  1080. wrapper.tryMasterIdle();
  1081. }
  1082. #if JUCE_WINDOWS
  1083. void mouseDown (const MouseEvent&) override
  1084. {
  1085. broughtToFront();
  1086. }
  1087. void broughtToFront() override
  1088. {
  1089. // for hosts like nuendo, need to also pop the MDI container to the
  1090. // front when our comp is clicked on.
  1091. if (HWND parent = findMDIParentOf ((HWND) getWindowHandle()))
  1092. SetWindowPos (parent, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
  1093. }
  1094. #endif
  1095. private:
  1096. //==============================================================================
  1097. JuceVSTWrapper& wrapper;
  1098. FakeMouseMoveGenerator fakeMouseGenerator;
  1099. #if JUCE_WINDOWS
  1100. WindowsHooks hooks;
  1101. #endif
  1102. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (EditorCompWrapper)
  1103. };
  1104. //==============================================================================
  1105. private:
  1106. AudioProcessor* filter;
  1107. juce::MemoryBlock chunkMemory;
  1108. juce::uint32 chunkMemoryTime;
  1109. ScopedPointer<EditorCompWrapper> editorComp;
  1110. ERect editorSize;
  1111. MidiBuffer midiEvents;
  1112. VSTMidiEventList outgoingEvents;
  1113. VstSpeakerArrangementType speakerIn, speakerOut;
  1114. int numInChans, numOutChans;
  1115. bool isProcessing, isBypassed, hasShutdown, firstProcessCallback;
  1116. bool shouldDeleteEditor, useNSView;
  1117. HeapBlock<float*> channels;
  1118. Array<float*> tempChannels; // see note in processReplacing()
  1119. AudioSampleBuffer processTempBuffer;
  1120. #if JUCE_MAC
  1121. void* hostWindow;
  1122. #elif JUCE_LINUX
  1123. Window hostWindow;
  1124. #else
  1125. HWND hostWindow;
  1126. #endif
  1127. static inline VstInt32 convertHexVersionToDecimal (const unsigned int hexVersion)
  1128. {
  1129. #if JUCE_VST_RETURN_HEX_VERSION_NUMBER_DIRECTLY
  1130. return (VstInt32) hexVersion;
  1131. #else
  1132. return (VstInt32) (((hexVersion >> 24) & 0xff) * 1000
  1133. + ((hexVersion >> 16) & 0xff) * 100
  1134. + ((hexVersion >> 8) & 0xff) * 10
  1135. + (hexVersion & 0xff));
  1136. #endif
  1137. }
  1138. //==============================================================================
  1139. #if JUCE_WINDOWS
  1140. // Workarounds for hosts which attempt to open editor windows on a non-GUI thread.. (Grrrr...)
  1141. static void checkWhetherMessageThreadIsCorrect()
  1142. {
  1143. const PluginHostType host (getHostType());
  1144. if (host.isWavelab() || host.isCubaseBridged() || host.isPremiere())
  1145. {
  1146. if (! messageThreadIsDefinitelyCorrect)
  1147. {
  1148. MessageManager::getInstance()->setCurrentThreadAsMessageThread();
  1149. class MessageThreadCallback : public CallbackMessage
  1150. {
  1151. public:
  1152. MessageThreadCallback (bool& tr) : triggered (tr) {}
  1153. void messageCallback() override
  1154. {
  1155. triggered = true;
  1156. }
  1157. private:
  1158. bool& triggered;
  1159. };
  1160. (new MessageThreadCallback (messageThreadIsDefinitelyCorrect))->post();
  1161. }
  1162. }
  1163. }
  1164. #else
  1165. static void checkWhetherMessageThreadIsCorrect() {}
  1166. #endif
  1167. //==============================================================================
  1168. void deleteTempChannels()
  1169. {
  1170. for (int i = tempChannels.size(); --i >= 0;)
  1171. delete[] (tempChannels.getUnchecked(i));
  1172. tempChannels.clear();
  1173. if (filter != nullptr)
  1174. tempChannels.insertMultiple (0, nullptr, filter->getNumInputChannels() + filter->getNumOutputChannels());
  1175. }
  1176. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (JuceVSTWrapper)
  1177. };
  1178. //==============================================================================
  1179. namespace
  1180. {
  1181. AEffect* pluginEntryPoint (audioMasterCallback audioMaster)
  1182. {
  1183. JUCE_AUTORELEASEPOOL
  1184. {
  1185. initialiseJuce_GUI();
  1186. try
  1187. {
  1188. if (audioMaster (0, audioMasterVersion, 0, 0, 0, 0) != 0)
  1189. {
  1190. #if JUCE_LINUX
  1191. MessageManagerLock mmLock;
  1192. #endif
  1193. AudioProcessor* const filter = createPluginFilterOfType (AudioProcessor::wrapperType_VST);
  1194. JuceVSTWrapper* const wrapper = new JuceVSTWrapper (audioMaster, filter);
  1195. return wrapper->getAeffect();
  1196. }
  1197. }
  1198. catch (...)
  1199. {}
  1200. }
  1201. return nullptr;
  1202. }
  1203. }
  1204. #if ! JUCE_WINDOWS
  1205. #define JUCE_EXPORTED_FUNCTION extern "C" __attribute__ ((visibility("default")))
  1206. #endif
  1207. //==============================================================================
  1208. // Mac startup code..
  1209. #if JUCE_MAC
  1210. JUCE_EXPORTED_FUNCTION AEffect* VSTPluginMain (audioMasterCallback audioMaster);
  1211. JUCE_EXPORTED_FUNCTION AEffect* VSTPluginMain (audioMasterCallback audioMaster)
  1212. {
  1213. initialiseMac();
  1214. return pluginEntryPoint (audioMaster);
  1215. }
  1216. JUCE_EXPORTED_FUNCTION AEffect* main_macho (audioMasterCallback audioMaster);
  1217. JUCE_EXPORTED_FUNCTION AEffect* main_macho (audioMasterCallback audioMaster)
  1218. {
  1219. initialiseMac();
  1220. return pluginEntryPoint (audioMaster);
  1221. }
  1222. //==============================================================================
  1223. // Linux startup code..
  1224. #elif JUCE_LINUX
  1225. JUCE_EXPORTED_FUNCTION AEffect* VSTPluginMain (audioMasterCallback audioMaster);
  1226. JUCE_EXPORTED_FUNCTION AEffect* VSTPluginMain (audioMasterCallback audioMaster)
  1227. {
  1228. SharedMessageThread::getInstance();
  1229. return pluginEntryPoint (audioMaster);
  1230. }
  1231. JUCE_EXPORTED_FUNCTION AEffect* main_plugin (audioMasterCallback audioMaster) asm ("main");
  1232. JUCE_EXPORTED_FUNCTION AEffect* main_plugin (audioMasterCallback audioMaster)
  1233. {
  1234. return VSTPluginMain (audioMaster);
  1235. }
  1236. // don't put initialiseJuce_GUI or shutdownJuce_GUI in these... it will crash!
  1237. __attribute__((constructor)) void myPluginInit() {}
  1238. __attribute__((destructor)) void myPluginFini() {}
  1239. //==============================================================================
  1240. // Win32 startup code..
  1241. #else
  1242. extern "C" __declspec (dllexport) AEffect* VSTPluginMain (audioMasterCallback audioMaster)
  1243. {
  1244. return pluginEntryPoint (audioMaster);
  1245. }
  1246. #ifndef JUCE_64BIT // (can't compile this on win64, but it's not needed anyway with VST2.4)
  1247. extern "C" __declspec (dllexport) int main (audioMasterCallback audioMaster)
  1248. {
  1249. return (int) pluginEntryPoint (audioMaster);
  1250. }
  1251. #endif
  1252. #endif
  1253. #endif