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.

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