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.

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