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.

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