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.

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