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.

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