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.

969 lines
34KB

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