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.

987 lines
35KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #include "../../juce_core/system/juce_TargetPlatform.h"
  18. #include "../utility/juce_CheckSettingMacros.h"
  19. #if JucePlugin_Build_RTAS
  20. #ifdef _MSC_VER
  21. // (this is a workaround for a build problem in VC9)
  22. #define _DO_NOT_DECLARE_INTERLOCKED_INTRINSICS_IN_MEMORY
  23. #include <intrin.h>
  24. #endif
  25. #include "juce_RTAS_DigiCode_Header.h"
  26. #ifdef _MSC_VER
  27. #include <Mac2Win.H>
  28. #endif
  29. /* Note about include paths
  30. ------------------------
  31. To be able to include all the Digidesign headers correctly, you'll need to add this
  32. lot to your include path:
  33. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\EffectClasses
  34. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\ProcessClasses
  35. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\ProcessClasses\Interfaces
  36. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\Utilities
  37. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\RTASP_Adapt
  38. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\CoreClasses
  39. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\Controls
  40. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\Meters
  41. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\ViewClasses
  42. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\DSPClasses
  43. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\Interfaces
  44. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\common
  45. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\common\Platform
  46. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\SignalProcessing\Public
  47. C:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugIns\DSPManager\Interfaces
  48. c:\yourdirectory\PT_80_SDK\AlturaPorts\SADriver\Interfaces
  49. c:\yourdirectory\PT_80_SDK\AlturaPorts\DigiPublic\Interfaces
  50. c:\yourdirectory\PT_80_SDK\AlturaPorts\Fic\Interfaces\DAEClient
  51. c:\yourdirectory\PT_80_SDK\AlturaPorts\NewFileLibs\Cmn
  52. c:\yourdirectory\PT_80_SDK\AlturaPorts\NewFileLibs\DOA
  53. c:\yourdirectory\PT_80_SDK\AlturaPorts\AlturaSource\PPC_H
  54. c:\yourdirectory\PT_80_SDK\AlturaPorts\AlturaSource\AppSupport
  55. c:\yourdirectory\PT_80_SDK\AlturaPorts\DigiPublic
  56. c:\yourdirectory\PT_80_SDK\AvidCode\AVX2sdk\AVX\avx2\avx2sdk\inc
  57. c:\yourdirectory\PT_80_SDK\xplat\AVX\avx2\avx2sdk\inc
  58. NB. If you hit a huge pile of bugs around here, make sure that you've not got the
  59. Apple QuickTime headers before the PT headers in your path, because there are
  60. some filename clashes between them.
  61. */
  62. #include <CEffectGroupMIDI.h>
  63. #include <CEffectProcessMIDI.h>
  64. #include <CEffectProcessRTAS.h>
  65. #include <CCustomView.h>
  66. #include <CEffectTypeRTAS.h>
  67. #include <CPluginControl.h>
  68. #include <CPluginControl_OnOff.h>
  69. #include <FicProcessTokens.h>
  70. #include <ExternalVersionDefines.h>
  71. //==============================================================================
  72. #ifdef _MSC_VER
  73. #pragma pack (push, 8)
  74. #pragma warning (disable: 4263 4264)
  75. #endif
  76. #include "../utility/juce_IncludeModuleHeaders.h"
  77. #ifdef _MSC_VER
  78. #pragma pack (pop)
  79. // This JUCE_RTAS_LINK_TO_DEBUG_LIB setting can be used to force linkage
  80. // against only the release build of the RTAS lib, since in older SDKs there
  81. // can be problems with the debug build.
  82. #if JUCE_DEBUG && ! defined (JUCE_RTAS_LINK_TO_DEBUG_LIB)
  83. #define JUCE_RTAS_LINK_TO_DEBUG_LIB 1
  84. #endif
  85. #if JUCE_RTAS_LINK_TO_DEBUG_LIB
  86. #define PT_LIB_PATH JucePlugin_WinBag_path "\\Debug\\lib\\"
  87. #else
  88. #define PT_LIB_PATH JucePlugin_WinBag_path "\\Release\\lib\\"
  89. #endif
  90. #pragma comment(lib, PT_LIB_PATH "DAE.lib")
  91. #pragma comment(lib, PT_LIB_PATH "DigiExt.lib")
  92. #pragma comment(lib, PT_LIB_PATH "DSI.lib")
  93. #pragma comment(lib, PT_LIB_PATH "PluginLib.lib")
  94. #pragma comment(lib, PT_LIB_PATH "DSPManager.lib")
  95. #pragma comment(lib, PT_LIB_PATH "DSPManagerClientLib.lib")
  96. #pragma comment(lib, PT_LIB_PATH "RTASClientLib.lib")
  97. #endif
  98. #undef MemoryBlock
  99. //==============================================================================
  100. #if JUCE_WINDOWS
  101. extern void JUCE_CALLTYPE attachSubWindow (void* hostWindow, int& titleW, int& titleH, Component* comp);
  102. extern void JUCE_CALLTYPE resizeHostWindow (void* hostWindow, int& titleW, int& titleH, Component* comp);
  103. #if ! JucePlugin_EditorRequiresKeyboardFocus
  104. extern void JUCE_CALLTYPE passFocusToHostWindow (void* hostWindow);
  105. #endif
  106. #else
  107. extern void* attachSubWindow (void* hostWindowRef, Component* comp);
  108. extern void removeSubWindow (void* nsWindow, Component* comp);
  109. extern void forwardCurrentKeyEventToHostWindow();
  110. #endif
  111. #if ! (JUCE_DEBUG || defined (JUCE_RTAS_PLUGINGESTALT_IS_CACHEABLE))
  112. #define JUCE_RTAS_PLUGINGESTALT_IS_CACHEABLE 1
  113. #endif
  114. const int midiBufferSize = 1024;
  115. const OSType juceChunkType = 'juce';
  116. static const int bypassControlIndex = 1;
  117. static int numInstances = 0;
  118. //==============================================================================
  119. class JucePlugInProcess : public CEffectProcessMIDI,
  120. public CEffectProcessRTAS,
  121. public AudioProcessorListener,
  122. public AudioPlayHead
  123. {
  124. public:
  125. //==============================================================================
  126. JucePlugInProcess()
  127. : sampleRate (44100.0)
  128. {
  129. juceFilter = createPluginFilterOfType (AudioProcessor::wrapperType_RTAS);
  130. AddChunk (juceChunkType, "Juce Audio Plugin Data");
  131. ++numInstances;
  132. }
  133. ~JucePlugInProcess()
  134. {
  135. JUCE_AUTORELEASEPOOL
  136. {
  137. if (mLoggedIn)
  138. MIDILogOut();
  139. midiBufferNode = nullptr;
  140. midiTransport = nullptr;
  141. if (juceFilter != nullptr)
  142. {
  143. juceFilter->releaseResources();
  144. juceFilter = nullptr;
  145. }
  146. if (--numInstances == 0)
  147. {
  148. #if JUCE_MAC
  149. // Hack to allow any NSWindows to clear themselves up before returning to PT..
  150. for (int i = 20; --i >= 0;)
  151. MessageManager::getInstance()->runDispatchLoopUntil (1);
  152. #endif
  153. shutdownJuce_GUI();
  154. }
  155. }
  156. }
  157. //==============================================================================
  158. class JuceCustomUIView : public CCustomView,
  159. public Timer
  160. {
  161. public:
  162. //==============================================================================
  163. JuceCustomUIView (AudioProcessor* ap, JucePlugInProcess* p)
  164. : filter (ap), process (p)
  165. {
  166. // setting the size in here crashes PT for some reason, so keep it simple..
  167. }
  168. ~JuceCustomUIView()
  169. {
  170. deleteEditorComp();
  171. }
  172. //==============================================================================
  173. void updateSize()
  174. {
  175. if (editorComp == nullptr)
  176. {
  177. editorComp = filter->createEditorIfNeeded();
  178. jassert (editorComp != nullptr);
  179. }
  180. if (editorComp->getWidth() != 0 && editorComp->getHeight() != 0)
  181. {
  182. Rect oldRect;
  183. GetRect (&oldRect);
  184. Rect r;
  185. r.left = 0;
  186. r.top = 0;
  187. r.right = editorComp->getWidth();
  188. r.bottom = editorComp->getHeight();
  189. SetRect (&r);
  190. if (oldRect.right != r.right || oldRect.bottom != r.bottom)
  191. startTimer (50);
  192. }
  193. }
  194. void timerCallback() override
  195. {
  196. if (! Component::isMouseButtonDownAnywhere())
  197. {
  198. stopTimer();
  199. // Send a token to the host to tell it about the resize
  200. SSetProcessWindowResizeToken token (process->fRootNameId, process->fRootNameId);
  201. FicSDSDispatchToken (&token);
  202. }
  203. }
  204. void attachToWindow (GrafPtr port)
  205. {
  206. if (port != 0)
  207. {
  208. JUCE_AUTORELEASEPOOL
  209. {
  210. updateSize();
  211. #if JUCE_WINDOWS
  212. void* const hostWindow = (void*) ASI_GethWnd ((WindowPtr) port);
  213. #else
  214. void* const hostWindow = (void*) GetWindowFromPort (port);
  215. #endif
  216. wrapper = nullptr;
  217. wrapper = new EditorCompWrapper (hostWindow, editorComp, this);
  218. }
  219. }
  220. else
  221. {
  222. deleteEditorComp();
  223. }
  224. }
  225. void DrawContents (Rect*)
  226. {
  227. #if JUCE_WINDOWS
  228. if (wrapper != nullptr)
  229. {
  230. if (ComponentPeer* const peer = wrapper->getPeer())
  231. peer->repaint (wrapper->getLocalBounds()); // (seems to be required in PT6.4, but not in 7.x)
  232. }
  233. #endif
  234. }
  235. void DrawBackground (Rect*) {}
  236. //==============================================================================
  237. private:
  238. AudioProcessor* const filter;
  239. JucePlugInProcess* const process;
  240. ScopedPointer<Component> wrapper;
  241. ScopedPointer<AudioProcessorEditor> editorComp;
  242. void deleteEditorComp()
  243. {
  244. if (editorComp != nullptr || wrapper != nullptr)
  245. {
  246. JUCE_AUTORELEASEPOOL
  247. {
  248. PopupMenu::dismissAllActiveMenus();
  249. if (Component* const modalComponent = Component::getCurrentlyModalComponent())
  250. modalComponent->exitModalState (0);
  251. filter->editorBeingDeleted (editorComp);
  252. editorComp = nullptr;
  253. wrapper = nullptr;
  254. }
  255. }
  256. }
  257. //==============================================================================
  258. // A component to hold the AudioProcessorEditor, and cope with some housekeeping
  259. // chores when it changes or repaints.
  260. class EditorCompWrapper : public Component
  261. #if ! JUCE_MAC
  262. , public FocusChangeListener
  263. #endif
  264. {
  265. public:
  266. EditorCompWrapper (void* const hostWindow_,
  267. Component* const editorComp,
  268. JuceCustomUIView* const owner_)
  269. : hostWindow (hostWindow_),
  270. owner (owner_),
  271. titleW (0),
  272. titleH (0)
  273. {
  274. #if ! JucePlugin_EditorRequiresKeyboardFocus
  275. setMouseClickGrabsKeyboardFocus (false);
  276. setWantsKeyboardFocus (false);
  277. #endif
  278. setOpaque (true);
  279. setBroughtToFrontOnMouseClick (true);
  280. setBounds (editorComp->getBounds());
  281. editorComp->setTopLeftPosition (0, 0);
  282. addAndMakeVisible (editorComp);
  283. #if JUCE_WINDOWS
  284. attachSubWindow (hostWindow, titleW, titleH, this);
  285. #else
  286. nsWindow = attachSubWindow (hostWindow, this);
  287. #endif
  288. setVisible (true);
  289. #if JUCE_WINDOWS && ! JucePlugin_EditorRequiresKeyboardFocus
  290. Desktop::getInstance().addFocusChangeListener (this);
  291. #endif
  292. }
  293. ~EditorCompWrapper()
  294. {
  295. removeChildComponent (getEditor());
  296. #if JUCE_WINDOWS && ! JucePlugin_EditorRequiresKeyboardFocus
  297. Desktop::getInstance().removeFocusChangeListener (this);
  298. #endif
  299. #if JUCE_MAC
  300. removeSubWindow (nsWindow, this);
  301. #endif
  302. }
  303. void paint (Graphics&) override {}
  304. void resized() override
  305. {
  306. if (Component* const ed = getEditor())
  307. ed->setBounds (getLocalBounds());
  308. repaint();
  309. }
  310. #if JUCE_WINDOWS
  311. void globalFocusChanged (Component*) override
  312. {
  313. #if ! JucePlugin_EditorRequiresKeyboardFocus
  314. if (hasKeyboardFocus (true))
  315. passFocusToHostWindow (hostWindow);
  316. #endif
  317. }
  318. #endif
  319. void childBoundsChanged (Component* child) override
  320. {
  321. setSize (child->getWidth(), child->getHeight());
  322. child->setTopLeftPosition (0, 0);
  323. #if JUCE_WINDOWS
  324. resizeHostWindow (hostWindow, titleW, titleH, this);
  325. #endif
  326. owner->updateSize();
  327. }
  328. void userTriedToCloseWindow() override {}
  329. #if JUCE_MAC && JucePlugin_EditorRequiresKeyboardFocus
  330. bool keyPressed (const KeyPress& kp) override
  331. {
  332. owner->updateSize();
  333. forwardCurrentKeyEventToHostWindow();
  334. return true;
  335. }
  336. #endif
  337. private:
  338. //==============================================================================
  339. void* const hostWindow;
  340. void* nsWindow;
  341. JuceCustomUIView* const owner;
  342. int titleW, titleH;
  343. Component* getEditor() const { return getChildComponent (0); }
  344. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (EditorCompWrapper)
  345. };
  346. };
  347. JuceCustomUIView* getView() const
  348. {
  349. return dynamic_cast<JuceCustomUIView*> (fOurPlugInView);
  350. }
  351. void GetViewRect (Rect* size) override
  352. {
  353. if (JuceCustomUIView* const v = getView())
  354. v->updateSize();
  355. CEffectProcessRTAS::GetViewRect (size);
  356. }
  357. CPlugInView* CreateCPlugInView() override
  358. {
  359. return new JuceCustomUIView (juceFilter, this);
  360. }
  361. void SetViewPort (GrafPtr port) override
  362. {
  363. CEffectProcessRTAS::SetViewPort (port);
  364. if (JuceCustomUIView* const v = getView())
  365. v->attachToWindow (port);
  366. }
  367. //==============================================================================
  368. ComponentResult GetDelaySamplesLong (long* aNumSamples) override
  369. {
  370. if (aNumSamples != nullptr)
  371. *aNumSamples = juceFilter != nullptr ? juceFilter->getLatencySamples() : 0;
  372. return noErr;
  373. }
  374. //==============================================================================
  375. void EffectInit() override
  376. {
  377. sampleRate = (double) GetSampleRate();
  378. jassert (sampleRate > 0);
  379. const int maxBlockSize = (int) CEffectProcessRTAS::GetMaximumRTASQuantum();
  380. jassert (maxBlockSize > 0);
  381. SFicPlugInStemFormats stems;
  382. GetProcessType()->GetStemFormats (&stems);
  383. juceFilter->setPlayConfigDetails (fNumInputs, fNumOutputs, sampleRate, maxBlockSize);
  384. AddControl (new CPluginControl_OnOff ('bypa', "Master Bypass\nMastrByp\nMByp\nByp", false, true));
  385. DefineMasterBypassControlIndex (bypassControlIndex);
  386. for (int i = 0; i < juceFilter->getNumParameters(); ++i)
  387. AddControl (new JucePluginControl (*juceFilter, i));
  388. // we need to do this midi log-in to get timecode, regardless of whether
  389. // the plugin actually uses midi...
  390. if (MIDILogIn() == noErr)
  391. {
  392. #if JucePlugin_WantsMidiInput
  393. if (CEffectType* const type = dynamic_cast<CEffectType*> (this->GetProcessType()))
  394. {
  395. char nodeName [64];
  396. type->GetProcessTypeName (63, nodeName);
  397. p2cstrcpy (nodeName, reinterpret_cast<unsigned char*> (nodeName));
  398. midiBufferNode = new CEffectMIDIOtherBufferedNode (&mMIDIWorld,
  399. 8192,
  400. eLocalNode,
  401. nodeName,
  402. midiBuffer);
  403. midiBufferNode->Initialize (0xffff, true);
  404. }
  405. #endif
  406. }
  407. midiTransport = new CEffectMIDITransport (&mMIDIWorld);
  408. midiEvents.ensureSize (2048);
  409. channels.calloc (jmax (juceFilter->getTotalNumInputChannels(),
  410. juceFilter->getTotalNumOutputChannels()));
  411. juceFilter->setPlayHead (this);
  412. juceFilter->addListener (this);
  413. juceFilter->prepareToPlay (sampleRate, maxBlockSize);
  414. }
  415. void RenderAudio (float** inputs, float** outputs, long numSamples) override
  416. {
  417. #if JucePlugin_WantsMidiInput
  418. midiEvents.clear();
  419. const Cmn_UInt32 bufferSize = mRTGlobals->mHWBufferSizeInSamples;
  420. if (midiBufferNode != nullptr)
  421. {
  422. if (midiBufferNode->GetAdvanceScheduleTime() != bufferSize)
  423. midiBufferNode->SetAdvanceScheduleTime (bufferSize);
  424. if (midiBufferNode->FillMIDIBuffer (mRTGlobals->mRunningTime, numSamples) == noErr)
  425. {
  426. jassert (midiBufferNode->GetBufferPtr() != nullptr);
  427. const int numMidiEvents = midiBufferNode->GetBufferSize();
  428. for (int i = 0; i < numMidiEvents; ++i)
  429. {
  430. const DirectMidiPacket& m = midiBuffer[i];
  431. jassert ((int) m.mTimestamp < numSamples);
  432. midiEvents.addEvent (m.mData, m.mLength,
  433. jlimit (0, (int) numSamples - 1, (int) m.mTimestamp));
  434. }
  435. }
  436. }
  437. #endif
  438. #if JUCE_DEBUG || JUCE_LOG_ASSERTIONS
  439. const int numMidiEventsComingIn = midiEvents.getNumEvents();
  440. ignoreUnused (numMidiEventsComingIn);
  441. #endif
  442. {
  443. const ScopedLock sl (juceFilter->getCallbackLock());
  444. const int numIn = juceFilter->getTotalNumInputChannels();
  445. const int numOut = juceFilter->getTotalNumOutputChannels();
  446. const int totalChans = jmax (numIn, numOut);
  447. if (juceFilter->isSuspended())
  448. {
  449. for (int i = 0; i < numOut; ++i)
  450. FloatVectorOperations::clear (outputs [i], numSamples);
  451. }
  452. else
  453. {
  454. {
  455. int i;
  456. for (i = 0; i < numOut; ++i)
  457. {
  458. channels[i] = outputs [i];
  459. if (i < numIn && inputs != outputs)
  460. FloatVectorOperations::copy (outputs [i], inputs[i], numSamples);
  461. }
  462. for (; i < numIn; ++i)
  463. channels [i] = inputs [i];
  464. }
  465. AudioSampleBuffer chans (channels, totalChans, numSamples);
  466. if (mBypassed)
  467. juceFilter->processBlockBypassed (chans, midiEvents);
  468. else
  469. juceFilter->processBlock (chans, midiEvents);
  470. }
  471. }
  472. if (! midiEvents.isEmpty())
  473. {
  474. #if JucePlugin_ProducesMidiOutput
  475. const juce::uint8* midiEventData;
  476. int midiEventSize, midiEventPosition;
  477. MidiBuffer::Iterator i (midiEvents);
  478. while (i.getNextEvent (midiEventData, midiEventSize, midiEventPosition))
  479. {
  480. //jassert (midiEventPosition >= 0 && midiEventPosition < (int) numSamples);
  481. }
  482. #elif JUCE_DEBUG || JUCE_LOG_ASSERTIONS
  483. // if your plugin creates midi messages, you'll need to set
  484. // the JucePlugin_ProducesMidiOutput macro to 1 in your
  485. // JucePluginCharacteristics.h file
  486. jassert (midiEvents.getNumEvents() <= numMidiEventsComingIn);
  487. #endif
  488. midiEvents.clear();
  489. }
  490. }
  491. //==============================================================================
  492. ComponentResult GetChunkSize (OSType chunkID, long* size) override
  493. {
  494. if (chunkID == juceChunkType)
  495. {
  496. tempFilterData.reset();
  497. juceFilter->getStateInformation (tempFilterData);
  498. *size = sizeof (SFicPlugInChunkHeader) + tempFilterData.getSize();
  499. return noErr;
  500. }
  501. return CEffectProcessMIDI::GetChunkSize (chunkID, size);
  502. }
  503. ComponentResult GetChunk (OSType chunkID, SFicPlugInChunk* chunk) override
  504. {
  505. if (chunkID == juceChunkType)
  506. {
  507. if (tempFilterData.getSize() == 0)
  508. juceFilter->getStateInformation (tempFilterData);
  509. chunk->fSize = sizeof (SFicPlugInChunkHeader) + tempFilterData.getSize();
  510. tempFilterData.copyTo ((void*) chunk->fData, 0, tempFilterData.getSize());
  511. tempFilterData.reset();
  512. return noErr;
  513. }
  514. return CEffectProcessMIDI::GetChunk (chunkID, chunk);
  515. }
  516. ComponentResult SetChunk (OSType chunkID, SFicPlugInChunk* chunk) override
  517. {
  518. if (chunkID == juceChunkType)
  519. {
  520. tempFilterData.reset();
  521. if (chunk->fSize - sizeof (SFicPlugInChunkHeader) > 0)
  522. {
  523. juceFilter->setStateInformation ((void*) chunk->fData,
  524. chunk->fSize - sizeof (SFicPlugInChunkHeader));
  525. }
  526. return noErr;
  527. }
  528. return CEffectProcessMIDI::SetChunk (chunkID, chunk);
  529. }
  530. //==============================================================================
  531. ComponentResult UpdateControlValue (long controlIndex, long value) override
  532. {
  533. if (controlIndex != bypassControlIndex)
  534. juceFilter->setParameter (controlIndex - 2, longToFloat (value));
  535. else
  536. mBypassed = (value > 0);
  537. return CProcess::UpdateControlValue (controlIndex, value);
  538. }
  539. #if JUCE_WINDOWS
  540. Boolean HandleKeystroke (EventRecord* e) override
  541. {
  542. if (Component* modalComp = Component::getCurrentlyModalComponent())
  543. {
  544. if (Component* focused = modalComp->getCurrentlyFocusedComponent())
  545. {
  546. switch (e->message & charCodeMask)
  547. {
  548. case kReturnCharCode:
  549. case kEnterCharCode: focused->keyPressed (KeyPress (KeyPress::returnKey)); break;
  550. case kEscapeCharCode: focused->keyPressed (KeyPress (KeyPress::escapeKey)); break;
  551. default: break;
  552. }
  553. return true;
  554. }
  555. }
  556. return false;
  557. }
  558. #endif
  559. //==============================================================================
  560. bool getCurrentPosition (AudioPlayHead::CurrentPositionInfo& info) override
  561. {
  562. Cmn_Float64 bpm = 120.0;
  563. Cmn_Int32 num = 4, denom = 4;
  564. Cmn_Int64 ticks = 0;
  565. Cmn_Bool isPlaying = false;
  566. if (midiTransport != nullptr)
  567. {
  568. midiTransport->GetCurrentTempo (&bpm);
  569. midiTransport->IsTransportPlaying (&isPlaying);
  570. midiTransport->GetCurrentMeter (&num, &denom);
  571. // (The following is a work-around because GetCurrentTickPosition() doesn't work correctly).
  572. Cmn_Int64 sampleLocation;
  573. if (isPlaying)
  574. midiTransport->GetCurrentRTASSampleLocation (&sampleLocation);
  575. else
  576. midiTransport->GetCurrentTDMSampleLocation (&sampleLocation);
  577. midiTransport->GetCustomTickPosition (&ticks, sampleLocation);
  578. info.timeInSamples = (int64) sampleLocation;
  579. info.timeInSeconds = sampleLocation / sampleRate;
  580. }
  581. else
  582. {
  583. info.timeInSamples = 0;
  584. info.timeInSeconds = 0;
  585. }
  586. info.bpm = bpm;
  587. info.timeSigNumerator = num;
  588. info.timeSigDenominator = denom;
  589. info.isPlaying = isPlaying;
  590. info.isRecording = false;
  591. info.ppqPosition = ticks / 960000.0;
  592. info.ppqPositionOfLastBarStart = 0; //xxx no idea how to get this correctly..
  593. info.isLooping = false;
  594. info.ppqLoopStart = 0;
  595. info.ppqLoopEnd = 0;
  596. double framesPerSec = 24.0;
  597. switch (fTimeCodeInfo.mFrameRate)
  598. {
  599. case ficFrameRate_24Frame: info.frameRate = AudioPlayHead::fps24; break;
  600. case ficFrameRate_25Frame: info.frameRate = AudioPlayHead::fps25; framesPerSec = 25.0; break;
  601. case ficFrameRate_2997NonDrop: info.frameRate = AudioPlayHead::fps2997; framesPerSec = 29.97002997; break;
  602. case ficFrameRate_2997DropFrame: info.frameRate = AudioPlayHead::fps2997drop; framesPerSec = 29.97002997; break;
  603. case ficFrameRate_30NonDrop: info.frameRate = AudioPlayHead::fps30; framesPerSec = 30.0; break;
  604. case ficFrameRate_30DropFrame: info.frameRate = AudioPlayHead::fps30drop; framesPerSec = 30.0; break;
  605. case ficFrameRate_23976: info.frameRate = AudioPlayHead::fps24; framesPerSec = 23.976; break;
  606. default: info.frameRate = AudioPlayHead::fpsUnknown; break;
  607. }
  608. info.editOriginTime = fTimeCodeInfo.mFrameOffset / framesPerSec;
  609. return true;
  610. }
  611. void audioProcessorParameterChanged (AudioProcessor*, int index, float newValue) override
  612. {
  613. SetControlValue (index + 2, floatToLong (newValue));
  614. }
  615. void audioProcessorParameterChangeGestureBegin (AudioProcessor*, int index) override
  616. {
  617. TouchControl (index + 2);
  618. }
  619. void audioProcessorParameterChangeGestureEnd (AudioProcessor*, int index) override
  620. {
  621. ReleaseControl (index + 2);
  622. }
  623. void audioProcessorChanged (AudioProcessor*) override
  624. {
  625. // xxx is there an RTAS equivalent?
  626. }
  627. private:
  628. ScopedPointer<AudioProcessor> juceFilter;
  629. MidiBuffer midiEvents;
  630. ScopedPointer<CEffectMIDIOtherBufferedNode> midiBufferNode;
  631. ScopedPointer<CEffectMIDITransport> midiTransport;
  632. DirectMidiPacket midiBuffer [midiBufferSize];
  633. juce::MemoryBlock tempFilterData;
  634. HeapBlock<float*> channels;
  635. double sampleRate;
  636. static float longToFloat (const long n) noexcept
  637. {
  638. return (float) ((((double) n) + (double) 0x80000000) / (double) 0xffffffff);
  639. }
  640. static long floatToLong (const float n) noexcept
  641. {
  642. return roundToInt (jlimit (-(double) 0x80000000, (double) 0x7fffffff,
  643. n * (double) 0xffffffff - (double) 0x80000000));
  644. }
  645. void bypassBuffers (float** const inputs, float** const outputs, const long numSamples) const
  646. {
  647. for (int i = fNumOutputs; --i >= 0;)
  648. {
  649. if (i < fNumInputs)
  650. FloatVectorOperations::copy (outputs[i], inputs[i], numSamples);
  651. else
  652. FloatVectorOperations::clear (outputs[i], numSamples);
  653. }
  654. }
  655. //==============================================================================
  656. class JucePluginControl : public CPluginControl
  657. {
  658. public:
  659. //==============================================================================
  660. JucePluginControl (AudioProcessor& p, const int i)
  661. : processor (p), index (i)
  662. {
  663. CPluginControl::SetValue (GetDefaultValue());
  664. }
  665. //==============================================================================
  666. OSType GetID() const { return index + 1; }
  667. long GetDefaultValue() const { return floatToLong (processor.getParameterDefaultValue (index)); }
  668. void SetDefaultValue (long) {}
  669. long GetNumSteps() const { return processor.getParameterNumSteps (index); }
  670. long ConvertStringToValue (const char* valueString) const
  671. {
  672. return floatToLong (String (valueString).getFloatValue());
  673. }
  674. Cmn_Bool IsKeyValid (long key) const { return true; }
  675. void GetNameOfLength (char* name, int maxLength, OSType inControllerType) const
  676. {
  677. // Pro-tools expects all your parameters to have valid names!
  678. jassert (processor.getParameterName (index, maxLength).isNotEmpty());
  679. processor.getParameterName (index, maxLength).copyToUTF8 (name, (size_t) maxLength + 1);
  680. }
  681. long GetPriority() const { return kFicCooperativeTaskPriority; }
  682. long GetOrientation() const
  683. {
  684. return processor.isParameterOrientationInverted (index)
  685. ? kDAE_RightMinLeftMax | kDAE_TopMinBottomMax | kDAE_RotarySingleDotMode | kDAE_RotaryRightMinLeftMax
  686. : kDAE_LeftMinRightMax | kDAE_BottomMinTopMax | kDAE_RotarySingleDotMode | kDAE_RotaryLeftMinRightMax;
  687. }
  688. long GetControlType() const { return kDAE_ContinuousValues; }
  689. void GetValueString (char* valueString, int maxLength, long value) const
  690. {
  691. processor.getParameterText (index, maxLength).copyToUTF8 (valueString, (size_t) maxLength + 1);
  692. }
  693. Cmn_Bool IsAutomatable() const
  694. {
  695. return processor.isParameterAutomatable (index);
  696. }
  697. private:
  698. //==============================================================================
  699. AudioProcessor& processor;
  700. const int index;
  701. JUCE_DECLARE_NON_COPYABLE (JucePluginControl)
  702. };
  703. };
  704. //==============================================================================
  705. class JucePlugInGroup : public CEffectGroupMIDI
  706. {
  707. public:
  708. //==============================================================================
  709. JucePlugInGroup()
  710. {
  711. DefineManufacturerNamesAndID (JucePlugin_Manufacturer, JucePlugin_RTASManufacturerCode);
  712. DefinePlugInNamesAndVersion (createRTASName().toUTF8(), JucePlugin_VersionCode);
  713. #if JUCE_RTAS_PLUGINGESTALT_IS_CACHEABLE
  714. AddGestalt (pluginGestalt_IsCacheable);
  715. #endif
  716. }
  717. ~JucePlugInGroup()
  718. {
  719. shutdownJuce_GUI();
  720. }
  721. //==============================================================================
  722. void CreateEffectTypes()
  723. {
  724. const short channelConfigs[][2] = { JucePlugin_PreferredChannelConfigurations };
  725. const int numConfigs = numElementsInArray (channelConfigs);
  726. // You need to actually add some configurations to the JucePlugin_PreferredChannelConfigurations
  727. // value in your JucePluginCharacteristics.h file..
  728. jassert (numConfigs > 0);
  729. for (int i = 0; i < numConfigs; ++i)
  730. {
  731. if (channelConfigs[i][0] <= 8 && channelConfigs[i][1] <= 8)
  732. {
  733. CEffectType* const type
  734. = new CEffectTypeRTAS ('jcaa' + i,
  735. JucePlugin_RTASProductId,
  736. JucePlugin_RTASCategory);
  737. type->DefineTypeNames (createRTASName().toRawUTF8());
  738. type->DefineSampleRateSupport (eSupports48kAnd96kAnd192k);
  739. type->DefineStemFormats (getFormatForChans (channelConfigs [i][0] != 0 ? channelConfigs [i][0] : channelConfigs [i][1]),
  740. getFormatForChans (channelConfigs [i][1] != 0 ? channelConfigs [i][1] : channelConfigs [i][0]));
  741. #if ! JucePlugin_RTASDisableBypass
  742. type->AddGestalt (pluginGestalt_CanBypass);
  743. #endif
  744. #if JucePlugin_RTASDisableMultiMono
  745. type->AddGestalt (pluginGestalt_DoesntSupportMultiMono);
  746. #endif
  747. type->AddGestalt (pluginGestalt_SupportsVariableQuanta);
  748. type->AttachEffectProcessCreator (createNewProcess);
  749. AddEffectType (type);
  750. }
  751. }
  752. }
  753. void Initialize()
  754. {
  755. CEffectGroupMIDI::Initialize();
  756. }
  757. //==============================================================================
  758. private:
  759. static CEffectProcess* createNewProcess()
  760. {
  761. #if JUCE_WINDOWS
  762. Process::setCurrentModuleInstanceHandle (gThisModule);
  763. #endif
  764. initialiseJuce_GUI();
  765. return new JucePlugInProcess();
  766. }
  767. static String createRTASName()
  768. {
  769. return String (JucePlugin_Name) + "\n"
  770. + String (JucePlugin_Desc);
  771. }
  772. static EPlugIn_StemFormat getFormatForChans (const int numChans) noexcept
  773. {
  774. switch (numChans)
  775. {
  776. case 0: return ePlugIn_StemFormat_Generic;
  777. case 1: return ePlugIn_StemFormat_Mono;
  778. case 2: return ePlugIn_StemFormat_Stereo;
  779. case 3: return ePlugIn_StemFormat_LCR;
  780. case 4: return ePlugIn_StemFormat_Quad;
  781. case 5: return ePlugIn_StemFormat_5dot0;
  782. case 6: return ePlugIn_StemFormat_5dot1;
  783. #if PT_VERS_MAJOR >= 9
  784. case 7: return ePlugIn_StemFormat_7dot0DTS;
  785. case 8: return ePlugIn_StemFormat_7dot1DTS;
  786. #else
  787. case 7: return ePlugIn_StemFormat_7dot0;
  788. case 8: return ePlugIn_StemFormat_7dot1;
  789. #endif
  790. default: jassertfalse; break; // hmm - not a valid number of chans for RTAS..
  791. }
  792. return ePlugIn_StemFormat_Generic;
  793. }
  794. };
  795. void initialiseMacRTAS();
  796. CProcessGroupInterface* CProcessGroup::CreateProcessGroup()
  797. {
  798. #if JUCE_MAC
  799. initialiseMacRTAS();
  800. #endif
  801. return new JucePlugInGroup();
  802. }
  803. #endif