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.

1615 lines
50KB

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