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.

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