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.

1017 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\AlturaPorts\DigiPublic
  59. c:\yourdirectory\PT_80_SDK\AvidCode\AVX2sdk\AVX\avx2\avx2sdk\inc
  60. c:\yourdirectory\PT_80_SDK\xplat\AVX\avx2\avx2sdk\inc
  61. NB. If you hit a huge pile of bugs around here, make sure that you've not got the
  62. Apple QuickTime headers before the PT headers in your path, because there are
  63. some filename clashes between them.
  64. */
  65. #include <CEffectGroupMIDI.h>
  66. #include <CEffectProcessMIDI.h>
  67. #include <CEffectProcessRTAS.h>
  68. #include <CCustomView.h>
  69. #include <CEffectTypeRTAS.h>
  70. #include <CPluginControl.h>
  71. #include <CPluginControl_OnOff.h>
  72. #include <FicProcessTokens.h>
  73. #include <ExternalVersionDefines.h>
  74. //==============================================================================
  75. #ifdef _MSC_VER
  76. #pragma pack (push, 8)
  77. #pragma warning (disable: 4263 4264)
  78. #endif
  79. #include "../utility/juce_IncludeModuleHeaders.h"
  80. #ifdef _MSC_VER
  81. #pragma pack (pop)
  82. #if JUCE_DEBUGxxx // (the debug lib in the 8.0 SDK fails to link, so we'll stick to the release one...)
  83. #define PT_LIB_PATH JucePlugin_WinBag_path "\\Debug\\lib\\"
  84. #else
  85. #define PT_LIB_PATH JucePlugin_WinBag_path "\\Release\\lib\\"
  86. #endif
  87. #pragma comment(lib, PT_LIB_PATH "DAE.lib")
  88. #pragma comment(lib, PT_LIB_PATH "DigiExt.lib")
  89. #pragma comment(lib, PT_LIB_PATH "DSI.lib")
  90. #pragma comment(lib, PT_LIB_PATH "PluginLib.lib")
  91. #endif
  92. #undef Component
  93. #undef MemoryBlock
  94. //==============================================================================
  95. #if JUCE_WINDOWS
  96. extern void JUCE_CALLTYPE attachSubWindow (void* hostWindow, int& titleW, int& titleH, juce::Component* comp);
  97. extern void JUCE_CALLTYPE resizeHostWindow (void* hostWindow, int& titleW, int& titleH, juce::Component* comp);
  98. #if ! JucePlugin_EditorRequiresKeyboardFocus
  99. extern void JUCE_CALLTYPE passFocusToHostWindow (void* hostWindow);
  100. #endif
  101. #else
  102. extern void* attachSubWindow (void* hostWindowRef, juce::Component* comp);
  103. extern void removeSubWindow (void* nsWindow, juce::Component* comp);
  104. extern void forwardCurrentKeyEventToHostWindow();
  105. #endif
  106. const int midiBufferSize = 1024;
  107. const OSType juceChunkType = 'juce';
  108. static const int bypassControlIndex = 1;
  109. //==============================================================================
  110. /** Somewhere in the codebase of your plugin, you need to implement this function
  111. and make it return a new instance of the filter subclass that you're building.
  112. */
  113. extern AudioProcessor* JUCE_CALLTYPE createPluginFilter();
  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 = createPluginFilter();
  129. jassert (juceFilter != 0);
  130. AddChunk (juceChunkType, "Juce Audio Plugin Data");
  131. ++numInstances;
  132. }
  133. ~JucePlugInProcess()
  134. {
  135. JUCE_AUTORELEASEPOOL
  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. class JuceCustomUIView : public CCustomView,
  156. public Timer
  157. {
  158. public:
  159. //==============================================================================
  160. JuceCustomUIView (AudioProcessor* const filter_,
  161. JucePlugInProcess* const process_)
  162. : filter (filter_),
  163. process (process_)
  164. {
  165. // setting the size in here crashes PT for some reason, so keep it simple..
  166. }
  167. ~JuceCustomUIView()
  168. {
  169. deleteEditorComp();
  170. }
  171. //==============================================================================
  172. void updateSize()
  173. {
  174. if (editorComp == nullptr)
  175. {
  176. editorComp = filter->createEditorIfNeeded();
  177. jassert (editorComp != nullptr);
  178. }
  179. if (editorComp->getWidth() != 0 && editorComp->getHeight() != 0)
  180. {
  181. Rect oldRect;
  182. GetRect (&oldRect);
  183. Rect r;
  184. r.left = 0;
  185. r.top = 0;
  186. r.right = editorComp->getWidth();
  187. r.bottom = editorComp->getHeight();
  188. SetRect (&r);
  189. if ((oldRect.right != r.right) || (oldRect.bottom != r.bottom))
  190. startTimer (50);
  191. }
  192. }
  193. void timerCallback()
  194. {
  195. if (! juce::Component::isMouseButtonDownAnywhere())
  196. {
  197. stopTimer();
  198. // Send a token to the host to tell it about the resize
  199. SSetProcessWindowResizeToken token (process->fRootNameId, process->fRootNameId);
  200. FicSDSDispatchToken (&token);
  201. }
  202. }
  203. void attachToWindow (GrafPtr port)
  204. {
  205. if (port != 0)
  206. {
  207. JUCE_AUTORELEASEPOOL
  208. updateSize();
  209. #if JUCE_WINDOWS
  210. void* const hostWindow = (void*) ASI_GethWnd ((WindowPtr) port);
  211. #else
  212. void* const hostWindow = (void*) GetWindowFromPort (port);
  213. #endif
  214. wrapper = nullptr;
  215. wrapper = new EditorCompWrapper (hostWindow, editorComp, this);
  216. process->touchAllParameters();
  217. }
  218. else
  219. {
  220. deleteEditorComp();
  221. }
  222. }
  223. void DrawContents (Rect*)
  224. {
  225. #if JUCE_WINDOWS
  226. if (wrapper != nullptr)
  227. {
  228. ComponentPeer* const peer = wrapper->getPeer();
  229. if (peer != nullptr)
  230. {
  231. // (seems to be required in PT6.4, but not in 7.x)
  232. peer->repaint (wrapper->getLocalBounds());
  233. }
  234. }
  235. #endif
  236. }
  237. void DrawBackground (Rect*) {}
  238. //==============================================================================
  239. private:
  240. AudioProcessor* const filter;
  241. JucePlugInProcess* const process;
  242. ScopedPointer<juce::Component> wrapper;
  243. ScopedPointer<AudioProcessorEditor> editorComp;
  244. void deleteEditorComp()
  245. {
  246. if (editorComp != 0 || wrapper != nullptr)
  247. {
  248. JUCE_AUTORELEASEPOOL
  249. PopupMenu::dismissAllActiveMenus();
  250. juce::Component* const modalComponent = juce::Component::getCurrentlyModalComponent();
  251. if (modalComponent != nullptr)
  252. modalComponent->exitModalState (0);
  253. filter->editorBeingDeleted (editorComp);
  254. editorComp = nullptr;
  255. wrapper = nullptr;
  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. juce::Component* const ed = getEditor();
  308. if (ed != nullptr)
  309. ed->setBounds (getLocalBounds());
  310. repaint();
  311. }
  312. #if JUCE_WINDOWS
  313. void globalFocusChanged (juce::Component*)
  314. {
  315. #if ! JucePlugin_EditorRequiresKeyboardFocus
  316. if (hasKeyboardFocus (true))
  317. passFocusToHostWindow (hostWindow);
  318. #endif
  319. }
  320. #endif
  321. void childBoundsChanged (juce::Component* child)
  322. {
  323. setSize (child->getWidth(), child->getHeight());
  324. child->setTopLeftPosition (0, 0);
  325. #if JUCE_WINDOWS
  326. resizeHostWindow (hostWindow, titleW, titleH, this);
  327. #endif
  328. owner->updateSize();
  329. }
  330. void userTriedToCloseWindow() {}
  331. #if JUCE_MAC && JucePlugin_EditorRequiresKeyboardFocus
  332. bool keyPressed (const KeyPress& kp)
  333. {
  334. owner->updateSize();
  335. forwardCurrentKeyEventToHostWindow();
  336. return true;
  337. }
  338. #endif
  339. private:
  340. //==============================================================================
  341. void* const hostWindow;
  342. void* nsWindow;
  343. JuceCustomUIView* const owner;
  344. int titleW, titleH;
  345. Component* getEditor() const { return getChildComponent (0); }
  346. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (EditorCompWrapper);
  347. };
  348. };
  349. JuceCustomUIView* getView() const
  350. {
  351. return dynamic_cast <JuceCustomUIView*> (fOurPlugInView);
  352. }
  353. void GetViewRect (Rect* size)
  354. {
  355. if (getView() != nullptr)
  356. getView()->updateSize();
  357. CEffectProcessRTAS::GetViewRect (size);
  358. }
  359. CPlugInView* CreateCPlugInView()
  360. {
  361. return new JuceCustomUIView (juceFilter, this);
  362. }
  363. void SetViewPort (GrafPtr port)
  364. {
  365. CEffectProcessRTAS::SetViewPort (port);
  366. if (getView() != nullptr)
  367. getView()->attachToWindow (port);
  368. }
  369. //==============================================================================
  370. protected:
  371. ComponentResult GetDelaySamplesLong (long* aNumSamples)
  372. {
  373. if (aNumSamples != nullptr)
  374. *aNumSamples = juceFilter != nullptr ? juceFilter->getLatencySamples() : 0;
  375. return noErr;
  376. }
  377. //==============================================================================
  378. void EffectInit()
  379. {
  380. SFicPlugInStemFormats stems;
  381. GetProcessType()->GetStemFormats (&stems);
  382. juceFilter->setPlayConfigDetails (fNumInputs, fNumOutputs,
  383. juceFilter->getSampleRate(), juceFilter->getBlockSize());
  384. AddControl (new CPluginControl_OnOff ('bypa', "Master Bypass\nMastrByp\nMByp\nByp", false, true));
  385. DefineMasterBypassControlIndex (bypassControlIndex);
  386. for (int i = 0; i < juceFilter->getNumParameters(); ++i)
  387. AddControl (new JucePluginControl (juceFilter, i));
  388. // we need to do this midi log-in to get timecode, regardless of whether
  389. // the plugin actually uses midi...
  390. if (MIDILogIn() == noErr)
  391. {
  392. #if JucePlugin_WantsMidiInput
  393. CEffectType* const type = dynamic_cast <CEffectType*> (this->GetProcessType());
  394. if (type != nullptr)
  395. {
  396. char nodeName [64];
  397. type->GetProcessTypeName (63, nodeName);
  398. p2cstrcpy (nodeName, reinterpret_cast <unsigned char*> (nodeName));
  399. midiBufferNode = new CEffectMIDIOtherBufferedNode (&mMIDIWorld,
  400. 8192,
  401. eLocalNode,
  402. nodeName,
  403. midiBuffer);
  404. midiBufferNode->Initialize (1, true);
  405. }
  406. #endif
  407. }
  408. midiTransport = new CEffectMIDITransport (&mMIDIWorld);
  409. juceFilter->setPlayHead (this);
  410. juceFilter->addListener (this);
  411. midiEvents.ensureSize (2048);
  412. }
  413. void handleAsyncUpdate()
  414. {
  415. if (! prepared)
  416. {
  417. sampleRate = gProcessGroup->GetSampleRate();
  418. jassert (sampleRate > 0);
  419. channels.calloc (jmax (juceFilter->getNumInputChannels(),
  420. juceFilter->getNumOutputChannels()));
  421. juceFilter->setPlayConfigDetails (fNumInputs, fNumOutputs,
  422. sampleRate, mRTGlobals->mHWBufferSizeInSamples);
  423. juceFilter->prepareToPlay (sampleRate, 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 != nullptr)
  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. info.timeInSamples = (int64) sampleLocation;
  581. info.timeInSeconds = sampleLocation / sampleRate;
  582. }
  583. else
  584. {
  585. info.timeInSamples = 0;
  586. info.timeInSeconds = 0;
  587. }
  588. info.bpm = bpm;
  589. info.timeSigNumerator = num;
  590. info.timeSigDenominator = denom;
  591. info.isPlaying = isPlaying;
  592. info.isRecording = false;
  593. info.ppqPosition = ticks / 960000.0;
  594. info.ppqPositionOfLastBarStart = 0; //xxx no idea how to get this correctly..
  595. info.isLooping = false;
  596. info.ppqLoopStart = 0;
  597. info.ppqLoopEnd = 0;
  598. double framesPerSec = 24.0;
  599. switch (fTimeCodeInfo.mFrameRate)
  600. {
  601. case ficFrameRate_24Frame: info.frameRate = AudioPlayHead::fps24; break;
  602. case ficFrameRate_25Frame: info.frameRate = AudioPlayHead::fps25; framesPerSec = 25.0; break;
  603. case ficFrameRate_2997NonDrop: info.frameRate = AudioPlayHead::fps2997; framesPerSec = 29.97002997; break;
  604. case ficFrameRate_2997DropFrame: info.frameRate = AudioPlayHead::fps2997drop; framesPerSec = 29.97002997; break;
  605. case ficFrameRate_30NonDrop: info.frameRate = AudioPlayHead::fps30; framesPerSec = 30.0; break;
  606. case ficFrameRate_30DropFrame: info.frameRate = AudioPlayHead::fps30drop; framesPerSec = 30.0; break;
  607. case ficFrameRate_23976: info.frameRate = AudioPlayHead::fps24; framesPerSec = 23.976; break;
  608. default: info.frameRate = AudioPlayHead::fpsUnknown; break;
  609. }
  610. info.editOriginTime = fTimeCodeInfo.mFrameOffset / framesPerSec;
  611. return true;
  612. }
  613. void audioProcessorParameterChanged (AudioProcessor*, int index, float newValue)
  614. {
  615. SetControlValue (index + 2, floatToLong (newValue));
  616. }
  617. void audioProcessorParameterChangeGestureBegin (AudioProcessor*, int index)
  618. {
  619. TouchControl (index + 2);
  620. }
  621. void audioProcessorParameterChangeGestureEnd (AudioProcessor*, int index)
  622. {
  623. ReleaseControl (index + 2);
  624. }
  625. void audioProcessorChanged (AudioProcessor*)
  626. {
  627. // xxx is there an RTAS equivalent?
  628. }
  629. void touchAllParameters()
  630. {
  631. for (int i = 0; i < juceFilter->getNumParameters(); ++i)
  632. {
  633. audioProcessorParameterChangeGestureBegin (0, i);
  634. audioProcessorParameterChanged (0, i, juceFilter->getParameter (i));
  635. audioProcessorParameterChangeGestureEnd (0, i);
  636. }
  637. }
  638. public:
  639. // Need to use an intermediate class here rather than inheriting from AsyncUpdater, so that it can
  640. // be deleted before shutting down juce in our destructor.
  641. class InternalAsyncUpdater : public AsyncUpdater
  642. {
  643. public:
  644. InternalAsyncUpdater (JucePlugInProcess& owner_) : owner (owner_) {}
  645. void handleAsyncUpdate() { owner.handleAsyncUpdate(); }
  646. private:
  647. JucePlugInProcess& owner;
  648. };
  649. //==============================================================================
  650. private:
  651. ScopedPointer<AudioProcessor> juceFilter;
  652. MidiBuffer midiEvents;
  653. ScopedPointer<CEffectMIDIOtherBufferedNode> midiBufferNode;
  654. ScopedPointer<CEffectMIDITransport> midiTransport;
  655. DirectMidiPacket midiBuffer [midiBufferSize];
  656. ScopedPointer<InternalAsyncUpdater> asyncUpdater;
  657. juce::MemoryBlock tempFilterData;
  658. HeapBlock <float*> channels;
  659. bool prepared;
  660. double sampleRate;
  661. static float longToFloat (const long n) noexcept
  662. {
  663. return (float) ((((double) n) + (double) 0x80000000) / (double) 0xffffffff);
  664. }
  665. static long floatToLong (const float n) noexcept
  666. {
  667. return roundToInt (jlimit (-(double) 0x80000000, (double) 0x7fffffff,
  668. n * (double) 0xffffffff - (double) 0x80000000));
  669. }
  670. void bypassBuffers (float** const inputs, float** const outputs, const long numSamples) const
  671. {
  672. for (int i = fNumOutputs; --i >= 0;)
  673. {
  674. if (i < fNumInputs)
  675. memcpy (outputs[i], inputs[i], numSamples * sizeof (float));
  676. else
  677. zeromem (outputs[i], numSamples * sizeof (float));
  678. }
  679. }
  680. //==============================================================================
  681. class JucePluginControl : public CPluginControl
  682. {
  683. public:
  684. //==============================================================================
  685. JucePluginControl (AudioProcessor* const juceFilter_, const int index_)
  686. : juceFilter (juceFilter_),
  687. index (index_)
  688. {
  689. }
  690. //==============================================================================
  691. OSType GetID() const { return index + 1; }
  692. long GetDefaultValue() const { return floatToLong (0); }
  693. void SetDefaultValue (long) {}
  694. long GetNumSteps() const { return 0xffffffff; }
  695. long ConvertStringToValue (const char* valueString) const
  696. {
  697. return floatToLong (String (valueString).getFloatValue());
  698. }
  699. Cmn_Bool IsKeyValid (long key) const { return true; }
  700. void GetNameOfLength (char* name, int maxLength, OSType inControllerType) const
  701. {
  702. // Pro-tools expects all your parameters to have valid names!
  703. jassert (juceFilter->getParameterName (index).isNotEmpty());
  704. juceFilter->getParameterName (index).copyToUTF8 (name, maxLength);
  705. }
  706. long GetPriority() const { return kFicCooperativeTaskPriority; }
  707. long GetOrientation() const
  708. {
  709. return kDAE_LeftMinRightMax | kDAE_BottomMinTopMax
  710. | kDAE_RotarySingleDotMode | kDAE_RotaryLeftMinRightMax;
  711. }
  712. long GetControlType() const { return kDAE_ContinuousValues; }
  713. void GetValueString (char* valueString, int maxLength, long value) const
  714. {
  715. juceFilter->getParameterText (index).copyToUTF8 (valueString, maxLength);
  716. }
  717. Cmn_Bool IsAutomatable() const
  718. {
  719. return juceFilter->isParameterAutomatable (index);
  720. }
  721. private:
  722. //==============================================================================
  723. AudioProcessor* const juceFilter;
  724. const int index;
  725. JUCE_DECLARE_NON_COPYABLE (JucePluginControl);
  726. };
  727. };
  728. //==============================================================================
  729. class JucePlugInGroup : public CEffectGroupMIDI
  730. {
  731. public:
  732. //==============================================================================
  733. JucePlugInGroup()
  734. {
  735. DefineManufacturerNamesAndID (JucePlugin_Manufacturer, JucePlugin_RTASManufacturerCode);
  736. DefinePlugInNamesAndVersion (createRTASName().toUTF8(), JucePlugin_VersionCode);
  737. #ifndef JUCE_DEBUG
  738. AddGestalt (pluginGestalt_IsCacheable);
  739. #endif
  740. }
  741. ~JucePlugInGroup()
  742. {
  743. shutdownJuce_GUI();
  744. }
  745. //==============================================================================
  746. void CreateEffectTypes()
  747. {
  748. const short channelConfigs[][2] = { JucePlugin_PreferredChannelConfigurations };
  749. const int numConfigs = numElementsInArray (channelConfigs);
  750. // You need to actually add some configurations to the JucePlugin_PreferredChannelConfigurations
  751. // value in your JucePluginCharacteristics.h file..
  752. jassert (numConfigs > 0);
  753. for (int i = 0; i < numConfigs; ++i)
  754. {
  755. CEffectType* const type
  756. = new CEffectTypeRTAS ('jcaa' + i,
  757. JucePlugin_RTASProductId,
  758. JucePlugin_RTASCategory);
  759. type->DefineTypeNames (createRTASName().toUTF8().getAddress());
  760. type->DefineSampleRateSupport (eSupports48kAnd96kAnd192k);
  761. type->DefineStemFormats (getFormatForChans (channelConfigs [i][0] != 0 ? channelConfigs [i][0] : channelConfigs [i][1]),
  762. getFormatForChans (channelConfigs [i][1] != 0 ? channelConfigs [i][1] : channelConfigs [i][0]));
  763. #if ! JucePlugin_RTASDisableBypass
  764. type->AddGestalt (pluginGestalt_CanBypass);
  765. #endif
  766. type->AddGestalt (pluginGestalt_SupportsVariableQuanta);
  767. type->AttachEffectProcessCreator (createNewProcess);
  768. AddEffectType (type);
  769. }
  770. }
  771. void Initialize()
  772. {
  773. CEffectGroupMIDI::Initialize();
  774. }
  775. //==============================================================================
  776. private:
  777. static CEffectProcess* createNewProcess()
  778. {
  779. #if JUCE_WINDOWS
  780. Process::setCurrentModuleInstanceHandle (gThisModule);
  781. #endif
  782. initialiseJuce_GUI();
  783. return new JucePlugInProcess();
  784. }
  785. static String createRTASName()
  786. {
  787. return String (JucePlugin_Name) + "\n"
  788. + String (JucePlugin_Desc);
  789. }
  790. static EPlugIn_StemFormat getFormatForChans (const int numChans) noexcept
  791. {
  792. switch (numChans)
  793. {
  794. case 0: return ePlugIn_StemFormat_Generic;
  795. case 1: return ePlugIn_StemFormat_Mono;
  796. case 2: return ePlugIn_StemFormat_Stereo;
  797. case 3: return ePlugIn_StemFormat_LCR;
  798. case 4: return ePlugIn_StemFormat_Quad;
  799. case 5: return ePlugIn_StemFormat_5dot0;
  800. case 6: return ePlugIn_StemFormat_5dot1;
  801. case 7: return ePlugIn_StemFormat_6dot1;
  802. #if PT_VERS_MAJOR >= 9
  803. case 8: return ePlugIn_StemFormat_7dot1DTS;
  804. #else
  805. case 8: return ePlugIn_StemFormat_7dot1;
  806. #endif
  807. default: jassertfalse; break; // hmm - not a valid number of chans for RTAS..
  808. }
  809. return ePlugIn_StemFormat_Generic;
  810. }
  811. };
  812. void initialiseMacRTAS();
  813. CProcessGroupInterface* CProcessGroup::CreateProcessGroup()
  814. {
  815. #if JUCE_MAC
  816. initialiseMacRTAS();
  817. #endif
  818. return new JucePlugInGroup();
  819. }
  820. #endif