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.

1010 lines
35KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. // Your project must contain an AppConfig.h file with your project-specific settings in it,
  19. // and your header search path must make it accessible to the module's files.
  20. #include "AppConfig.h"
  21. #include "../utility/juce_CheckSettingMacros.h"
  22. #if JucePlugin_Build_RTAS
  23. #ifdef _MSC_VER
  24. // (this is a workaround for a build problem in VC9)
  25. #define _DO_NOT_DECLARE_INTERLOCKED_INTRINSICS_IN_MEMORY
  26. #include <intrin.h>
  27. #endif
  28. #include "juce_RTAS_DigiCode_Header.h"
  29. #ifdef _MSC_VER
  30. #include <Mac2Win.H>
  31. #endif
  32. /* Note about include paths
  33. ------------------------
  34. To be able to include all the Digidesign headers correctly, you'll need to add this
  35. lot to your include path:
  36. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\EffectClasses
  37. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\ProcessClasses
  38. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\ProcessClasses\Interfaces
  39. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\Utilities
  40. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\RTASP_Adapt
  41. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\CoreClasses
  42. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\Controls
  43. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\Meters
  44. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\ViewClasses
  45. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\DSPClasses
  46. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\Interfaces
  47. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\common
  48. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\common\Platform
  49. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\SignalProcessing\Public
  50. C:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugIns\DSPManager\Interfaces
  51. c:\yourdirectory\PT_80_SDK\AlturaPorts\SADriver\Interfaces
  52. c:\yourdirectory\PT_80_SDK\AlturaPorts\DigiPublic\Interfaces
  53. c:\yourdirectory\PT_80_SDK\AlturaPorts\Fic\Interfaces\DAEClient
  54. c:\yourdirectory\PT_80_SDK\AlturaPorts\NewFileLibs\Cmn
  55. c:\yourdirectory\PT_80_SDK\AlturaPorts\NewFileLibs\DOA
  56. c:\yourdirectory\PT_80_SDK\AlturaPorts\AlturaSource\PPC_H
  57. c:\yourdirectory\PT_80_SDK\AlturaPorts\AlturaSource\AppSupport
  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. #endif
  91. #undef Component
  92. #undef MemoryBlock
  93. //==============================================================================
  94. #if JUCE_WINDOWS
  95. extern void JUCE_CALLTYPE attachSubWindow (void* hostWindow, int& titleW, int& titleH, juce::Component* comp);
  96. extern void JUCE_CALLTYPE resizeHostWindow (void* hostWindow, int& titleW, int& titleH, juce::Component* comp);
  97. #if ! JucePlugin_EditorRequiresKeyboardFocus
  98. extern void JUCE_CALLTYPE passFocusToHostWindow (void* hostWindow);
  99. #endif
  100. #else
  101. extern void* attachSubWindow (void* hostWindowRef, juce::Component* comp);
  102. extern void removeSubWindow (void* nsWindow, juce::Component* comp);
  103. extern void forwardCurrentKeyEventToHostWindow();
  104. #endif
  105. const int midiBufferSize = 1024;
  106. const OSType juceChunkType = 'juce';
  107. static const int bypassControlIndex = 1;
  108. //==============================================================================
  109. /** Somewhere in the codebase of your plugin, you need to implement this function
  110. and make it return a new instance of the filter subclass that you're building.
  111. */
  112. extern AudioProcessor* JUCE_CALLTYPE createPluginFilter();
  113. static int numInstances = 0;
  114. //==============================================================================
  115. class JucePlugInProcess : public CEffectProcessMIDI,
  116. public CEffectProcessRTAS,
  117. public AudioProcessorListener,
  118. public AudioPlayHead
  119. {
  120. public:
  121. //==============================================================================
  122. JucePlugInProcess()
  123. : prepared (false),
  124. sampleRate (44100.0)
  125. {
  126. asyncUpdater = new InternalAsyncUpdater (*this);
  127. juceFilter = createPluginFilter();
  128. jassert (juceFilter != 0);
  129. AddChunk (juceChunkType, "Juce Audio Plugin Data");
  130. ++numInstances;
  131. }
  132. ~JucePlugInProcess()
  133. {
  134. JUCE_AUTORELEASEPOOL
  135. if (mLoggedIn)
  136. MIDILogOut();
  137. midiBufferNode = nullptr;
  138. midiTransport = nullptr;
  139. if (prepared)
  140. juceFilter->releaseResources();
  141. juceFilter = nullptr;
  142. asyncUpdater = nullptr;
  143. if (--numInstances == 0)
  144. {
  145. #if JUCE_MAC
  146. // Hack to allow any NSWindows to clear themselves up before returning to PT..
  147. for (int i = 20; --i >= 0;)
  148. MessageManager::getInstance()->runDispatchLoopUntil (1);
  149. #endif
  150. shutdownJuce_GUI();
  151. }
  152. }
  153. //==============================================================================
  154. class JuceCustomUIView : public CCustomView,
  155. public Timer
  156. {
  157. public:
  158. //==============================================================================
  159. JuceCustomUIView (AudioProcessor* const filter_,
  160. JucePlugInProcess* const process_)
  161. : filter (filter_),
  162. process (process_)
  163. {
  164. // setting the size in here crashes PT for some reason, so keep it simple..
  165. }
  166. ~JuceCustomUIView()
  167. {
  168. deleteEditorComp();
  169. }
  170. //==============================================================================
  171. void updateSize()
  172. {
  173. if (editorComp == nullptr)
  174. {
  175. editorComp = filter->createEditorIfNeeded();
  176. jassert (editorComp != nullptr);
  177. }
  178. if (editorComp->getWidth() != 0 && editorComp->getHeight() != 0)
  179. {
  180. Rect oldRect;
  181. GetRect (&oldRect);
  182. Rect r;
  183. r.left = 0;
  184. r.top = 0;
  185. r.right = editorComp->getWidth();
  186. r.bottom = editorComp->getHeight();
  187. SetRect (&r);
  188. if ((oldRect.right != r.right) || (oldRect.bottom != r.bottom))
  189. startTimer (50);
  190. }
  191. }
  192. void timerCallback()
  193. {
  194. if (! juce::Component::isMouseButtonDownAnywhere())
  195. {
  196. stopTimer();
  197. // Send a token to the host to tell it about the resize
  198. SSetProcessWindowResizeToken token (process->fRootNameId, process->fRootNameId);
  199. FicSDSDispatchToken (&token);
  200. }
  201. }
  202. void attachToWindow (GrafPtr port)
  203. {
  204. if (port != 0)
  205. {
  206. JUCE_AUTORELEASEPOOL
  207. updateSize();
  208. #if JUCE_WINDOWS
  209. void* const hostWindow = (void*) ASI_GethWnd ((WindowPtr) port);
  210. #else
  211. void* const hostWindow = (void*) GetWindowFromPort (port);
  212. #endif
  213. wrapper = nullptr;
  214. wrapper = new EditorCompWrapper (hostWindow, editorComp, this);
  215. process->touchAllParameters();
  216. }
  217. else
  218. {
  219. deleteEditorComp();
  220. }
  221. }
  222. void DrawContents (Rect*)
  223. {
  224. #if JUCE_WINDOWS
  225. if (wrapper != nullptr)
  226. {
  227. ComponentPeer* const peer = wrapper->getPeer();
  228. if (peer != nullptr)
  229. {
  230. // (seems to be required in PT6.4, but not in 7.x)
  231. peer->repaint (wrapper->getLocalBounds());
  232. }
  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 != 0 || wrapper != nullptr)
  246. {
  247. JUCE_AUTORELEASEPOOL
  248. PopupMenu::dismissAllActiveMenus();
  249. juce::Component* const modalComponent = juce::Component::getCurrentlyModalComponent();
  250. if (modalComponent != nullptr)
  251. modalComponent->exitModalState (0);
  252. filter->editorBeingDeleted (editorComp);
  253. editorComp = nullptr;
  254. wrapper = nullptr;
  255. }
  256. }
  257. //==============================================================================
  258. // A component to hold the AudioProcessorEditor, and cope with some housekeeping
  259. // chores when it changes or repaints.
  260. class EditorCompWrapper : public juce::Component
  261. #if ! JUCE_MAC
  262. , public FocusChangeListener
  263. #endif
  264. {
  265. public:
  266. EditorCompWrapper (void* const hostWindow_,
  267. Component* const editorComp,
  268. JuceCustomUIView* const owner_)
  269. : hostWindow (hostWindow_),
  270. owner (owner_),
  271. titleW (0),
  272. titleH (0)
  273. {
  274. #if ! JucePlugin_EditorRequiresKeyboardFocus
  275. setMouseClickGrabsKeyboardFocus (false);
  276. setWantsKeyboardFocus (false);
  277. #endif
  278. setOpaque (true);
  279. setBroughtToFrontOnMouseClick (true);
  280. setBounds (editorComp->getBounds());
  281. editorComp->setTopLeftPosition (0, 0);
  282. addAndMakeVisible (editorComp);
  283. #if JUCE_WINDOWS
  284. attachSubWindow (hostWindow, titleW, titleH, this);
  285. #else
  286. nsWindow = attachSubWindow (hostWindow, this);
  287. #endif
  288. setVisible (true);
  289. #if JUCE_WINDOWS && ! JucePlugin_EditorRequiresKeyboardFocus
  290. Desktop::getInstance().addFocusChangeListener (this);
  291. #endif
  292. }
  293. ~EditorCompWrapper()
  294. {
  295. removeChildComponent (getEditor());
  296. #if JUCE_WINDOWS && ! JucePlugin_EditorRequiresKeyboardFocus
  297. Desktop::getInstance().removeFocusChangeListener (this);
  298. #endif
  299. #if JUCE_MAC
  300. removeSubWindow (nsWindow, this);
  301. #endif
  302. }
  303. void paint (Graphics&) {}
  304. void resized()
  305. {
  306. juce::Component* const ed = getEditor();
  307. if (ed != nullptr)
  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 (getView() != nullptr)
  355. getView()->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 (getView() != nullptr)
  366. getView()->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. CEffectType* const type = dynamic_cast <CEffectType*> (this->GetProcessType());
  393. if (type != nullptr)
  394. {
  395. char nodeName [64];
  396. type->GetProcessTypeName (63, nodeName);
  397. p2cstrcpy (nodeName, reinterpret_cast <unsigned char*> (nodeName));
  398. midiBufferNode = new CEffectMIDIOtherBufferedNode (&mMIDIWorld,
  399. 8192,
  400. eLocalNode,
  401. nodeName,
  402. midiBuffer);
  403. midiBufferNode->Initialize (1, true);
  404. }
  405. #endif
  406. }
  407. midiTransport = new CEffectMIDITransport (&mMIDIWorld);
  408. juceFilter->setPlayHead (this);
  409. juceFilter->addListener (this);
  410. midiEvents.ensureSize (2048);
  411. }
  412. void handleAsyncUpdate()
  413. {
  414. if (! prepared)
  415. {
  416. sampleRate = gProcessGroup->GetSampleRate();
  417. jassert (sampleRate > 0);
  418. channels.calloc (jmax (juceFilter->getNumInputChannels(),
  419. juceFilter->getNumOutputChannels()));
  420. juceFilter->setPlayConfigDetails (fNumInputs, fNumOutputs,
  421. sampleRate, mRTGlobals->mHWBufferSizeInSamples);
  422. juceFilter->prepareToPlay (sampleRate,
  423. mRTGlobals->mHWBufferSizeInSamples);
  424. prepared = true;
  425. }
  426. }
  427. void RenderAudio (float** inputs, float** outputs, long numSamples)
  428. {
  429. if (! prepared)
  430. {
  431. asyncUpdater->triggerAsyncUpdate();
  432. bypassBuffers (inputs, outputs, numSamples);
  433. return;
  434. }
  435. if (mBypassed)
  436. {
  437. bypassBuffers (inputs, outputs, numSamples);
  438. return;
  439. }
  440. #if JucePlugin_WantsMidiInput
  441. midiEvents.clear();
  442. const Cmn_UInt32 bufferSize = mRTGlobals->mHWBufferSizeInSamples;
  443. if (midiBufferNode != 0)
  444. {
  445. if (midiBufferNode->GetAdvanceScheduleTime() != bufferSize)
  446. midiBufferNode->SetAdvanceScheduleTime (bufferSize);
  447. if (midiBufferNode->FillMIDIBuffer (mRTGlobals->mRunningTime, numSamples) == noErr)
  448. {
  449. jassert (midiBufferNode->GetBufferPtr() != 0);
  450. const int numMidiEvents = midiBufferNode->GetBufferSize();
  451. for (int i = 0; i < numMidiEvents; ++i)
  452. {
  453. const DirectMidiPacket& m = midiBuffer[i];
  454. jassert ((int) m.mTimestamp < numSamples);
  455. midiEvents.addEvent (m.mData, m.mLength,
  456. jlimit (0, (int) numSamples - 1, (int) m.mTimestamp));
  457. }
  458. }
  459. }
  460. #endif
  461. #if JUCE_DEBUG || JUCE_LOG_ASSERTIONS
  462. const int numMidiEventsComingIn = midiEvents.getNumEvents();
  463. (void) numMidiEventsComingIn;
  464. #endif
  465. {
  466. const ScopedLock sl (juceFilter->getCallbackLock());
  467. const int numIn = juceFilter->getNumInputChannels();
  468. const int numOut = juceFilter->getNumOutputChannels();
  469. const int totalChans = jmax (numIn, numOut);
  470. if (juceFilter->isSuspended())
  471. {
  472. for (int i = 0; i < numOut; ++i)
  473. zeromem (outputs [i], sizeof (float) * numSamples);
  474. }
  475. else
  476. {
  477. {
  478. int i;
  479. for (i = 0; i < numOut; ++i)
  480. {
  481. channels[i] = outputs [i];
  482. if (i < numIn && inputs != outputs)
  483. memcpy (outputs [i], inputs[i], sizeof (float) * numSamples);
  484. }
  485. for (; i < numIn; ++i)
  486. channels [i] = inputs [i];
  487. }
  488. AudioSampleBuffer chans (channels, totalChans, numSamples);
  489. juceFilter->processBlock (chans, midiEvents);
  490. }
  491. }
  492. if (! midiEvents.isEmpty())
  493. {
  494. #if JucePlugin_ProducesMidiOutput
  495. const juce::uint8* midiEventData;
  496. int midiEventSize, midiEventPosition;
  497. MidiBuffer::Iterator i (midiEvents);
  498. while (i.getNextEvent (midiEventData, midiEventSize, midiEventPosition))
  499. {
  500. //jassert (midiEventPosition >= 0 && midiEventPosition < (int) numSamples);
  501. }
  502. #elif JUCE_DEBUG || JUCE_LOG_ASSERTIONS
  503. // if your plugin creates midi messages, you'll need to set
  504. // the JucePlugin_ProducesMidiOutput macro to 1 in your
  505. // JucePluginCharacteristics.h file
  506. jassert (midiEvents.getNumEvents() <= numMidiEventsComingIn);
  507. #endif
  508. midiEvents.clear();
  509. }
  510. }
  511. //==============================================================================
  512. ComponentResult GetChunkSize (OSType chunkID, long* size)
  513. {
  514. if (chunkID == juceChunkType)
  515. {
  516. tempFilterData.setSize (0);
  517. juceFilter->getStateInformation (tempFilterData);
  518. *size = sizeof (SFicPlugInChunkHeader) + tempFilterData.getSize();
  519. return noErr;
  520. }
  521. return CEffectProcessMIDI::GetChunkSize (chunkID, size);
  522. }
  523. ComponentResult GetChunk (OSType chunkID, SFicPlugInChunk* chunk)
  524. {
  525. if (chunkID == juceChunkType)
  526. {
  527. if (tempFilterData.getSize() == 0)
  528. juceFilter->getStateInformation (tempFilterData);
  529. chunk->fSize = sizeof (SFicPlugInChunkHeader) + tempFilterData.getSize();
  530. tempFilterData.copyTo ((void*) chunk->fData, 0, tempFilterData.getSize());
  531. tempFilterData.setSize (0);
  532. return noErr;
  533. }
  534. return CEffectProcessMIDI::GetChunk (chunkID, chunk);
  535. }
  536. ComponentResult SetChunk (OSType chunkID, SFicPlugInChunk* chunk)
  537. {
  538. if (chunkID == juceChunkType)
  539. {
  540. tempFilterData.setSize (0);
  541. if (chunk->fSize - sizeof (SFicPlugInChunkHeader) > 0)
  542. {
  543. juceFilter->setStateInformation ((void*) chunk->fData,
  544. chunk->fSize - sizeof (SFicPlugInChunkHeader));
  545. }
  546. return noErr;
  547. }
  548. return CEffectProcessMIDI::SetChunk (chunkID, chunk);
  549. }
  550. //==============================================================================
  551. ComponentResult UpdateControlValue (long controlIndex, long value)
  552. {
  553. if (controlIndex != bypassControlIndex)
  554. juceFilter->setParameter (controlIndex - 2, longToFloat (value));
  555. else
  556. mBypassed = (value > 0);
  557. return CProcess::UpdateControlValue (controlIndex, value);
  558. }
  559. //==============================================================================
  560. bool getCurrentPosition (AudioPlayHead::CurrentPositionInfo& info)
  561. {
  562. // this method can only be called while the plugin is running
  563. jassert (prepared);
  564. Cmn_Float64 bpm = 120.0;
  565. Cmn_Int32 num = 4, denom = 4;
  566. Cmn_Int64 ticks = 0;
  567. Cmn_Bool isPlaying = false;
  568. if (midiTransport != 0)
  569. {
  570. midiTransport->GetCurrentTempo (&bpm);
  571. midiTransport->IsTransportPlaying (&isPlaying);
  572. midiTransport->GetCurrentMeter (&num, &denom);
  573. // (The following is a work-around because GetCurrentTickPosition() doesn't work correctly).
  574. Cmn_Int64 sampleLocation;
  575. if (isPlaying)
  576. midiTransport->GetCurrentRTASSampleLocation (&sampleLocation);
  577. else
  578. midiTransport->GetCurrentTDMSampleLocation (&sampleLocation);
  579. midiTransport->GetCustomTickPosition (&ticks, sampleLocation);
  580. }
  581. info.bpm = bpm;
  582. info.timeSigNumerator = num;
  583. info.timeSigDenominator = denom;
  584. info.isPlaying = isPlaying;
  585. info.isRecording = false;
  586. info.ppqPosition = ticks / 960000.0;
  587. info.ppqPositionOfLastBarStart = 0; //xxx no idea how to get this correctly..
  588. info.isLooping = false;
  589. info.ppqLoopStart = 0;
  590. info.ppqLoopEnd = 0;
  591. // xxx incorrect if there are tempo changes, but there's no
  592. // other way of getting this info..
  593. info.timeInSeconds = ticks * (60.0 / 960000.0) / bpm;
  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. memcpy (outputs[i], inputs[i], numSamples * sizeof (float));
  672. else
  673. zeromem (outputs[i], numSamples * sizeof (float));
  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, 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, 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. #ifndef JUCE_DEBUG
  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().toUTF8().getAddress());
  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. type->AddGestalt (pluginGestalt_CanBypass);
  760. type->AddGestalt (pluginGestalt_SupportsVariableQuanta);
  761. type->AttachEffectProcessCreator (createNewProcess);
  762. AddEffectType (type);
  763. }
  764. }
  765. void Initialize()
  766. {
  767. CEffectGroupMIDI::Initialize();
  768. }
  769. //==============================================================================
  770. private:
  771. static CEffectProcess* createNewProcess()
  772. {
  773. #if JUCE_WINDOWS
  774. Process::setCurrentModuleInstanceHandle (gThisModule);
  775. #endif
  776. initialiseJuce_GUI();
  777. return new JucePlugInProcess();
  778. }
  779. static String createRTASName()
  780. {
  781. return String (JucePlugin_Name) + "\n"
  782. + String (JucePlugin_Desc);
  783. }
  784. static EPlugIn_StemFormat getFormatForChans (const int numChans) noexcept
  785. {
  786. switch (numChans)
  787. {
  788. case 0: return ePlugIn_StemFormat_Generic;
  789. case 1: return ePlugIn_StemFormat_Mono;
  790. case 2: return ePlugIn_StemFormat_Stereo;
  791. case 3: return ePlugIn_StemFormat_LCR;
  792. case 4: return ePlugIn_StemFormat_Quad;
  793. case 5: return ePlugIn_StemFormat_5dot0;
  794. case 6: return ePlugIn_StemFormat_5dot1;
  795. case 7: return ePlugIn_StemFormat_6dot1;
  796. #if PT_VERS_MAJOR >= 9
  797. case 8: return ePlugIn_StemFormat_7dot1DTS;
  798. #else
  799. case 8: return ePlugIn_StemFormat_7dot1;
  800. #endif
  801. default: jassertfalse; break; // hmm - not a valid number of chans for RTAS..
  802. }
  803. return ePlugIn_StemFormat_Generic;
  804. }
  805. };
  806. void initialiseMacRTAS();
  807. CProcessGroupInterface* CProcessGroup::CreateProcessGroup()
  808. {
  809. #if JUCE_MAC
  810. initialiseMacRTAS();
  811. #endif
  812. return new JucePlugInGroup();
  813. }
  814. #endif