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.

1569 lines
51KB

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