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.

1612 lines
49KB

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