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.

991 lines
35KB

  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. juce::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. ComponentResult GetDelaySamplesLong (long* aNumSamples) override
  372. {
  373. if (aNumSamples != nullptr)
  374. *aNumSamples = juceFilter != nullptr ? juceFilter->getLatencySamples() : 0;
  375. return noErr;
  376. }
  377. //==============================================================================
  378. void EffectInit() override
  379. {
  380. sampleRate = (double) GetSampleRate();
  381. jassert (sampleRate > 0);
  382. const int maxBlockSize = (int) CEffectProcessRTAS::GetMaximumRTASQuantum();
  383. jassert (maxBlockSize > 0);
  384. SFicPlugInStemFormats stems;
  385. GetProcessType()->GetStemFormats (&stems);
  386. juceFilter->setPlayConfigDetails (fNumInputs, fNumOutputs, sampleRate, maxBlockSize);
  387. AddControl (new CPluginControl_OnOff ('bypa', "Master Bypass\nMastrByp\nMByp\nByp", false, true));
  388. DefineMasterBypassControlIndex (bypassControlIndex);
  389. for (int i = 0; i < juceFilter->getNumParameters(); ++i)
  390. AddControl (new JucePluginControl (*juceFilter, i));
  391. // we need to do this midi log-in to get timecode, regardless of whether
  392. // the plugin actually uses midi...
  393. if (MIDILogIn() == noErr)
  394. {
  395. #if JucePlugin_WantsMidiInput
  396. if (CEffectType* const type = dynamic_cast <CEffectType*> (this->GetProcessType()))
  397. {
  398. char nodeName [64];
  399. type->GetProcessTypeName (63, nodeName);
  400. p2cstrcpy (nodeName, reinterpret_cast <unsigned char*> (nodeName));
  401. midiBufferNode = new CEffectMIDIOtherBufferedNode (&mMIDIWorld,
  402. 8192,
  403. eLocalNode,
  404. nodeName,
  405. midiBuffer);
  406. midiBufferNode->Initialize (0xffff, true);
  407. }
  408. #endif
  409. }
  410. midiTransport = new CEffectMIDITransport (&mMIDIWorld);
  411. midiEvents.ensureSize (2048);
  412. channels.calloc (jmax (juceFilter->getNumInputChannels(),
  413. juceFilter->getNumOutputChannels()));
  414. juceFilter->setPlayHead (this);
  415. juceFilter->addListener (this);
  416. juceFilter->prepareToPlay (sampleRate, maxBlockSize);
  417. }
  418. void RenderAudio (float** inputs, float** outputs, long numSamples) override
  419. {
  420. #if JucePlugin_WantsMidiInput
  421. midiEvents.clear();
  422. const Cmn_UInt32 bufferSize = mRTGlobals->mHWBufferSizeInSamples;
  423. if (midiBufferNode != nullptr)
  424. {
  425. if (midiBufferNode->GetAdvanceScheduleTime() != bufferSize)
  426. midiBufferNode->SetAdvanceScheduleTime (bufferSize);
  427. if (midiBufferNode->FillMIDIBuffer (mRTGlobals->mRunningTime, numSamples) == noErr)
  428. {
  429. jassert (midiBufferNode->GetBufferPtr() != nullptr);
  430. const int numMidiEvents = midiBufferNode->GetBufferSize();
  431. for (int i = 0; i < numMidiEvents; ++i)
  432. {
  433. const DirectMidiPacket& m = midiBuffer[i];
  434. jassert ((int) m.mTimestamp < numSamples);
  435. midiEvents.addEvent (m.mData, m.mLength,
  436. jlimit (0, (int) numSamples - 1, (int) m.mTimestamp));
  437. }
  438. }
  439. }
  440. #endif
  441. #if JUCE_DEBUG || JUCE_LOG_ASSERTIONS
  442. const int numMidiEventsComingIn = midiEvents.getNumEvents();
  443. (void) numMidiEventsComingIn;
  444. #endif
  445. {
  446. const ScopedLock sl (juceFilter->getCallbackLock());
  447. const int numIn = juceFilter->getNumInputChannels();
  448. const int numOut = juceFilter->getNumOutputChannels();
  449. const int totalChans = jmax (numIn, numOut);
  450. if (juceFilter->isSuspended())
  451. {
  452. for (int i = 0; i < numOut; ++i)
  453. FloatVectorOperations::clear (outputs [i], numSamples);
  454. }
  455. else
  456. {
  457. {
  458. int i;
  459. for (i = 0; i < numOut; ++i)
  460. {
  461. channels[i] = outputs [i];
  462. if (i < numIn && inputs != outputs)
  463. FloatVectorOperations::copy (outputs [i], inputs[i], numSamples);
  464. }
  465. for (; i < numIn; ++i)
  466. channels [i] = inputs [i];
  467. }
  468. AudioSampleBuffer chans (channels, totalChans, numSamples);
  469. if (mBypassed)
  470. juceFilter->processBlockBypassed (chans, midiEvents);
  471. else
  472. juceFilter->processBlock (chans, midiEvents);
  473. }
  474. }
  475. if (! midiEvents.isEmpty())
  476. {
  477. #if JucePlugin_ProducesMidiOutput
  478. const juce::uint8* midiEventData;
  479. int midiEventSize, midiEventPosition;
  480. MidiBuffer::Iterator i (midiEvents);
  481. while (i.getNextEvent (midiEventData, midiEventSize, midiEventPosition))
  482. {
  483. //jassert (midiEventPosition >= 0 && midiEventPosition < (int) numSamples);
  484. }
  485. #elif JUCE_DEBUG || JUCE_LOG_ASSERTIONS
  486. // if your plugin creates midi messages, you'll need to set
  487. // the JucePlugin_ProducesMidiOutput macro to 1 in your
  488. // JucePluginCharacteristics.h file
  489. jassert (midiEvents.getNumEvents() <= numMidiEventsComingIn);
  490. #endif
  491. midiEvents.clear();
  492. }
  493. }
  494. //==============================================================================
  495. ComponentResult GetChunkSize (OSType chunkID, long* size) override
  496. {
  497. if (chunkID == juceChunkType)
  498. {
  499. tempFilterData.reset();
  500. juceFilter->getStateInformation (tempFilterData);
  501. *size = sizeof (SFicPlugInChunkHeader) + tempFilterData.getSize();
  502. return noErr;
  503. }
  504. return CEffectProcessMIDI::GetChunkSize (chunkID, size);
  505. }
  506. ComponentResult GetChunk (OSType chunkID, SFicPlugInChunk* chunk) override
  507. {
  508. if (chunkID == juceChunkType)
  509. {
  510. if (tempFilterData.getSize() == 0)
  511. juceFilter->getStateInformation (tempFilterData);
  512. chunk->fSize = sizeof (SFicPlugInChunkHeader) + tempFilterData.getSize();
  513. tempFilterData.copyTo ((void*) chunk->fData, 0, tempFilterData.getSize());
  514. tempFilterData.reset();
  515. return noErr;
  516. }
  517. return CEffectProcessMIDI::GetChunk (chunkID, chunk);
  518. }
  519. ComponentResult SetChunk (OSType chunkID, SFicPlugInChunk* chunk) override
  520. {
  521. if (chunkID == juceChunkType)
  522. {
  523. tempFilterData.reset();
  524. if (chunk->fSize - sizeof (SFicPlugInChunkHeader) > 0)
  525. {
  526. juceFilter->setStateInformation ((void*) chunk->fData,
  527. chunk->fSize - sizeof (SFicPlugInChunkHeader));
  528. }
  529. return noErr;
  530. }
  531. return CEffectProcessMIDI::SetChunk (chunkID, chunk);
  532. }
  533. //==============================================================================
  534. ComponentResult UpdateControlValue (long controlIndex, long value) override
  535. {
  536. if (controlIndex != bypassControlIndex)
  537. juceFilter->setParameter (controlIndex - 2, longToFloat (value));
  538. else
  539. mBypassed = (value > 0);
  540. return CProcess::UpdateControlValue (controlIndex, value);
  541. }
  542. #if JUCE_WINDOWS
  543. Boolean HandleKeystroke (EventRecord* e) override
  544. {
  545. if (juce::Component* modalComp = juce::Component::getCurrentlyModalComponent())
  546. {
  547. if (juce::Component* focused = modalComp->getCurrentlyFocusedComponent())
  548. {
  549. switch (e->message & charCodeMask)
  550. {
  551. case kReturnCharCode:
  552. case kEnterCharCode: focused->keyPressed (KeyPress (KeyPress::returnKey)); break;
  553. case kEscapeCharCode: focused->keyPressed (KeyPress (KeyPress::escapeKey)); break;
  554. default: break;
  555. }
  556. return true;
  557. }
  558. }
  559. return false;
  560. }
  561. #endif
  562. //==============================================================================
  563. bool getCurrentPosition (AudioPlayHead::CurrentPositionInfo& info) override
  564. {
  565. Cmn_Float64 bpm = 120.0;
  566. Cmn_Int32 num = 4, denom = 4;
  567. Cmn_Int64 ticks = 0;
  568. Cmn_Bool isPlaying = false;
  569. if (midiTransport != nullptr)
  570. {
  571. midiTransport->GetCurrentTempo (&bpm);
  572. midiTransport->IsTransportPlaying (&isPlaying);
  573. midiTransport->GetCurrentMeter (&num, &denom);
  574. // (The following is a work-around because GetCurrentTickPosition() doesn't work correctly).
  575. Cmn_Int64 sampleLocation;
  576. if (isPlaying)
  577. midiTransport->GetCurrentRTASSampleLocation (&sampleLocation);
  578. else
  579. midiTransport->GetCurrentTDMSampleLocation (&sampleLocation);
  580. midiTransport->GetCustomTickPosition (&ticks, sampleLocation);
  581. info.timeInSamples = (int64) sampleLocation;
  582. info.timeInSeconds = sampleLocation / sampleRate;
  583. }
  584. else
  585. {
  586. info.timeInSamples = 0;
  587. info.timeInSeconds = 0;
  588. }
  589. info.bpm = bpm;
  590. info.timeSigNumerator = num;
  591. info.timeSigDenominator = denom;
  592. info.isPlaying = isPlaying;
  593. info.isRecording = false;
  594. info.ppqPosition = ticks / 960000.0;
  595. info.ppqPositionOfLastBarStart = 0; //xxx no idea how to get this correctly..
  596. info.isLooping = false;
  597. info.ppqLoopStart = 0;
  598. info.ppqLoopEnd = 0;
  599. double framesPerSec = 24.0;
  600. switch (fTimeCodeInfo.mFrameRate)
  601. {
  602. case ficFrameRate_24Frame: info.frameRate = AudioPlayHead::fps24; break;
  603. case ficFrameRate_25Frame: info.frameRate = AudioPlayHead::fps25; framesPerSec = 25.0; break;
  604. case ficFrameRate_2997NonDrop: info.frameRate = AudioPlayHead::fps2997; framesPerSec = 29.97002997; break;
  605. case ficFrameRate_2997DropFrame: info.frameRate = AudioPlayHead::fps2997drop; framesPerSec = 29.97002997; break;
  606. case ficFrameRate_30NonDrop: info.frameRate = AudioPlayHead::fps30; framesPerSec = 30.0; break;
  607. case ficFrameRate_30DropFrame: info.frameRate = AudioPlayHead::fps30drop; framesPerSec = 30.0; break;
  608. case ficFrameRate_23976: info.frameRate = AudioPlayHead::fps24; framesPerSec = 23.976; break;
  609. default: info.frameRate = AudioPlayHead::fpsUnknown; break;
  610. }
  611. info.editOriginTime = fTimeCodeInfo.mFrameOffset / framesPerSec;
  612. return true;
  613. }
  614. void audioProcessorParameterChanged (AudioProcessor*, int index, float newValue) override
  615. {
  616. SetControlValue (index + 2, floatToLong (newValue));
  617. }
  618. void audioProcessorParameterChangeGestureBegin (AudioProcessor*, int index) override
  619. {
  620. TouchControl (index + 2);
  621. }
  622. void audioProcessorParameterChangeGestureEnd (AudioProcessor*, int index) override
  623. {
  624. ReleaseControl (index + 2);
  625. }
  626. void audioProcessorChanged (AudioProcessor*) override
  627. {
  628. // xxx is there an RTAS equivalent?
  629. }
  630. private:
  631. ScopedPointer<AudioProcessor> juceFilter;
  632. MidiBuffer midiEvents;
  633. ScopedPointer<CEffectMIDIOtherBufferedNode> midiBufferNode;
  634. ScopedPointer<CEffectMIDITransport> midiTransport;
  635. DirectMidiPacket midiBuffer [midiBufferSize];
  636. juce::MemoryBlock tempFilterData;
  637. HeapBlock<float*> channels;
  638. double sampleRate;
  639. static float longToFloat (const long n) noexcept
  640. {
  641. return (float) ((((double) n) + (double) 0x80000000) / (double) 0xffffffff);
  642. }
  643. static long floatToLong (const float n) noexcept
  644. {
  645. return roundToInt (jlimit (-(double) 0x80000000, (double) 0x7fffffff,
  646. n * (double) 0xffffffff - (double) 0x80000000));
  647. }
  648. void bypassBuffers (float** const inputs, float** const outputs, const long numSamples) const
  649. {
  650. for (int i = fNumOutputs; --i >= 0;)
  651. {
  652. if (i < fNumInputs)
  653. FloatVectorOperations::copy (outputs[i], inputs[i], numSamples);
  654. else
  655. FloatVectorOperations::clear (outputs[i], numSamples);
  656. }
  657. }
  658. //==============================================================================
  659. class JucePluginControl : public CPluginControl
  660. {
  661. public:
  662. //==============================================================================
  663. JucePluginControl (AudioProcessor& p, const int i)
  664. : processor (p), index (i)
  665. {
  666. CPluginControl::SetValue (GetDefaultValue());
  667. }
  668. //==============================================================================
  669. OSType GetID() const { return index + 1; }
  670. long GetDefaultValue() const { return floatToLong (processor.getParameterDefaultValue (index)); }
  671. void SetDefaultValue (long) {}
  672. long GetNumSteps() const { return processor.getParameterNumSteps (index); }
  673. long ConvertStringToValue (const char* valueString) const
  674. {
  675. return floatToLong (String (valueString).getFloatValue());
  676. }
  677. Cmn_Bool IsKeyValid (long key) const { return true; }
  678. void GetNameOfLength (char* name, int maxLength, OSType inControllerType) const
  679. {
  680. // Pro-tools expects all your parameters to have valid names!
  681. jassert (processor.getParameterName (index, maxLength).isNotEmpty());
  682. processor.getParameterName (index, maxLength).copyToUTF8 (name, (size_t) maxLength + 1);
  683. }
  684. long GetPriority() const { return kFicCooperativeTaskPriority; }
  685. long GetOrientation() const
  686. {
  687. return processor.isParameterOrientationInverted (index)
  688. ? kDAE_RightMinLeftMax | kDAE_TopMinBottomMax | kDAE_RotarySingleDotMode | kDAE_RotaryRightMinLeftMax
  689. : kDAE_LeftMinRightMax | kDAE_BottomMinTopMax | kDAE_RotarySingleDotMode | kDAE_RotaryLeftMinRightMax;
  690. }
  691. long GetControlType() const { return kDAE_ContinuousValues; }
  692. void GetValueString (char* valueString, int maxLength, long value) const
  693. {
  694. processor.getParameterText (index, maxLength).copyToUTF8 (valueString, (size_t) maxLength + 1);
  695. }
  696. Cmn_Bool IsAutomatable() const
  697. {
  698. return processor.isParameterAutomatable (index);
  699. }
  700. private:
  701. //==============================================================================
  702. AudioProcessor& processor;
  703. const int index;
  704. JUCE_DECLARE_NON_COPYABLE (JucePluginControl)
  705. };
  706. };
  707. //==============================================================================
  708. class JucePlugInGroup : public CEffectGroupMIDI
  709. {
  710. public:
  711. //==============================================================================
  712. JucePlugInGroup()
  713. {
  714. DefineManufacturerNamesAndID (JucePlugin_Manufacturer, JucePlugin_RTASManufacturerCode);
  715. DefinePlugInNamesAndVersion (createRTASName().toUTF8(), JucePlugin_VersionCode);
  716. #if JUCE_RTAS_PLUGINGESTALT_IS_CACHEABLE
  717. AddGestalt (pluginGestalt_IsCacheable);
  718. #endif
  719. }
  720. ~JucePlugInGroup()
  721. {
  722. shutdownJuce_GUI();
  723. }
  724. //==============================================================================
  725. void CreateEffectTypes()
  726. {
  727. const short channelConfigs[][2] = { JucePlugin_PreferredChannelConfigurations };
  728. const int numConfigs = numElementsInArray (channelConfigs);
  729. // You need to actually add some configurations to the JucePlugin_PreferredChannelConfigurations
  730. // value in your JucePluginCharacteristics.h file..
  731. jassert (numConfigs > 0);
  732. for (int i = 0; i < numConfigs; ++i)
  733. {
  734. if (channelConfigs[i][0] <= 8 && channelConfigs[i][1] <= 8)
  735. {
  736. CEffectType* const type
  737. = new CEffectTypeRTAS ('jcaa' + i,
  738. JucePlugin_RTASProductId,
  739. JucePlugin_RTASCategory);
  740. type->DefineTypeNames (createRTASName().toRawUTF8());
  741. type->DefineSampleRateSupport (eSupports48kAnd96kAnd192k);
  742. type->DefineStemFormats (getFormatForChans (channelConfigs [i][0] != 0 ? channelConfigs [i][0] : channelConfigs [i][1]),
  743. getFormatForChans (channelConfigs [i][1] != 0 ? channelConfigs [i][1] : channelConfigs [i][0]));
  744. #if ! JucePlugin_RTASDisableBypass
  745. type->AddGestalt (pluginGestalt_CanBypass);
  746. #endif
  747. #if JucePlugin_RTASDisableMultiMono
  748. type->AddGestalt (pluginGestalt_DoesntSupportMultiMono);
  749. #endif
  750. type->AddGestalt (pluginGestalt_SupportsVariableQuanta);
  751. type->AttachEffectProcessCreator (createNewProcess);
  752. AddEffectType (type);
  753. }
  754. }
  755. }
  756. void Initialize()
  757. {
  758. CEffectGroupMIDI::Initialize();
  759. }
  760. //==============================================================================
  761. private:
  762. static CEffectProcess* createNewProcess()
  763. {
  764. #if JUCE_WINDOWS
  765. Process::setCurrentModuleInstanceHandle (gThisModule);
  766. #endif
  767. initialiseJuce_GUI();
  768. return new JucePlugInProcess();
  769. }
  770. static String createRTASName()
  771. {
  772. return String (JucePlugin_Name) + "\n"
  773. + String (JucePlugin_Desc);
  774. }
  775. static EPlugIn_StemFormat getFormatForChans (const int numChans) noexcept
  776. {
  777. switch (numChans)
  778. {
  779. case 0: return ePlugIn_StemFormat_Generic;
  780. case 1: return ePlugIn_StemFormat_Mono;
  781. case 2: return ePlugIn_StemFormat_Stereo;
  782. case 3: return ePlugIn_StemFormat_LCR;
  783. case 4: return ePlugIn_StemFormat_Quad;
  784. case 5: return ePlugIn_StemFormat_5dot0;
  785. case 6: return ePlugIn_StemFormat_5dot1;
  786. #if PT_VERS_MAJOR >= 9
  787. case 7: return ePlugIn_StemFormat_7dot0DTS;
  788. case 8: return ePlugIn_StemFormat_7dot1DTS;
  789. #else
  790. case 7: return ePlugIn_StemFormat_7dot0;
  791. case 8: return ePlugIn_StemFormat_7dot1;
  792. #endif
  793. default: jassertfalse; break; // hmm - not a valid number of chans for RTAS..
  794. }
  795. return ePlugIn_StemFormat_Generic;
  796. }
  797. };
  798. void initialiseMacRTAS();
  799. CProcessGroupInterface* CProcessGroup::CreateProcessGroup()
  800. {
  801. #if JUCE_MAC
  802. initialiseMacRTAS();
  803. #endif
  804. return new JucePlugInGroup();
  805. }
  806. #endif