The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1010 lines
35KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. // Your project must contain an AppConfig.h file with your project-specific settings in it,
  19. // and your header search path must make it accessible to the module's files.
  20. #include "AppConfig.h"
  21. #include "../utility/juce_CheckSettingMacros.h"
  22. #if JucePlugin_Build_RTAS
  23. #ifdef _MSC_VER
  24. // (this is a workaround for a build problem in VC9)
  25. #define _DO_NOT_DECLARE_INTERLOCKED_INTRINSICS_IN_MEMORY
  26. #include <intrin.h>
  27. #endif
  28. #include "juce_RTAS_DigiCode_Header.h"
  29. #ifdef _MSC_VER
  30. #include <Mac2Win.H>
  31. #endif
  32. /* Note about include paths
  33. ------------------------
  34. To be able to include all the Digidesign headers correctly, you'll need to add this
  35. lot to your include path:
  36. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\EffectClasses
  37. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\ProcessClasses
  38. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\ProcessClasses\Interfaces
  39. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\Utilities
  40. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\RTASP_Adapt
  41. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\CoreClasses
  42. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\Controls
  43. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\Meters
  44. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\ViewClasses
  45. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\DSPClasses
  46. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\Interfaces
  47. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\common
  48. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\common\Platform
  49. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\SignalProcessing\Public
  50. C:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugIns\DSPManager\Interfaces
  51. c:\yourdirectory\PT_80_SDK\AlturaPorts\SADriver\Interfaces
  52. c:\yourdirectory\PT_80_SDK\AlturaPorts\DigiPublic\Interfaces
  53. c:\yourdirectory\PT_80_SDK\AlturaPorts\Fic\Interfaces\DAEClient
  54. c:\yourdirectory\PT_80_SDK\AlturaPorts\NewFileLibs\Cmn
  55. c:\yourdirectory\PT_80_SDK\AlturaPorts\NewFileLibs\DOA
  56. c:\yourdirectory\PT_80_SDK\AlturaPorts\AlturaSource\PPC_H
  57. c:\yourdirectory\PT_80_SDK\AlturaPorts\AlturaSource\AppSupport
  58. c:\yourdirectory\PT_80_SDK\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); // your createPluginFilter() method must return an object!
  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. if (ComponentPeer* const peer = wrapper->getPeer())
  230. peer->repaint (wrapper->getLocalBounds()); // (seems to be required in PT6.4, but not in 7.x)
  231. }
  232. #endif
  233. }
  234. void DrawBackground (Rect*) {}
  235. //==============================================================================
  236. private:
  237. AudioProcessor* const filter;
  238. JucePlugInProcess* const process;
  239. ScopedPointer<juce::Component> wrapper;
  240. ScopedPointer<AudioProcessorEditor> editorComp;
  241. void deleteEditorComp()
  242. {
  243. if (editorComp != nullptr || wrapper != nullptr)
  244. {
  245. JUCE_AUTORELEASEPOOL
  246. PopupMenu::dismissAllActiveMenus();
  247. if (juce::Component* const modalComponent = juce::Component::getCurrentlyModalComponent())
  248. modalComponent->exitModalState (0);
  249. filter->editorBeingDeleted (editorComp);
  250. editorComp = nullptr;
  251. wrapper = nullptr;
  252. }
  253. }
  254. //==============================================================================
  255. // A component to hold the AudioProcessorEditor, and cope with some housekeeping
  256. // chores when it changes or repaints.
  257. class EditorCompWrapper : public juce::Component
  258. #if ! JUCE_MAC
  259. , public FocusChangeListener
  260. #endif
  261. {
  262. public:
  263. EditorCompWrapper (void* const hostWindow_,
  264. Component* const editorComp,
  265. JuceCustomUIView* const owner_)
  266. : hostWindow (hostWindow_),
  267. owner (owner_),
  268. titleW (0),
  269. titleH (0)
  270. {
  271. #if ! JucePlugin_EditorRequiresKeyboardFocus
  272. setMouseClickGrabsKeyboardFocus (false);
  273. setWantsKeyboardFocus (false);
  274. #endif
  275. setOpaque (true);
  276. setBroughtToFrontOnMouseClick (true);
  277. setBounds (editorComp->getBounds());
  278. editorComp->setTopLeftPosition (0, 0);
  279. addAndMakeVisible (editorComp);
  280. #if JUCE_WINDOWS
  281. attachSubWindow (hostWindow, titleW, titleH, this);
  282. #else
  283. nsWindow = attachSubWindow (hostWindow, this);
  284. #endif
  285. setVisible (true);
  286. #if JUCE_WINDOWS && ! JucePlugin_EditorRequiresKeyboardFocus
  287. Desktop::getInstance().addFocusChangeListener (this);
  288. #endif
  289. }
  290. ~EditorCompWrapper()
  291. {
  292. removeChildComponent (getEditor());
  293. #if JUCE_WINDOWS && ! JucePlugin_EditorRequiresKeyboardFocus
  294. Desktop::getInstance().removeFocusChangeListener (this);
  295. #endif
  296. #if JUCE_MAC
  297. removeSubWindow (nsWindow, this);
  298. #endif
  299. }
  300. void paint (Graphics&) {}
  301. void resized()
  302. {
  303. if (juce::Component* const ed = getEditor())
  304. ed->setBounds (getLocalBounds());
  305. repaint();
  306. }
  307. #if JUCE_WINDOWS
  308. void globalFocusChanged (juce::Component*)
  309. {
  310. #if ! JucePlugin_EditorRequiresKeyboardFocus
  311. if (hasKeyboardFocus (true))
  312. passFocusToHostWindow (hostWindow);
  313. #endif
  314. }
  315. #endif
  316. void childBoundsChanged (juce::Component* child)
  317. {
  318. setSize (child->getWidth(), child->getHeight());
  319. child->setTopLeftPosition (0, 0);
  320. #if JUCE_WINDOWS
  321. resizeHostWindow (hostWindow, titleW, titleH, this);
  322. #endif
  323. owner->updateSize();
  324. }
  325. void userTriedToCloseWindow() {}
  326. #if JUCE_MAC && JucePlugin_EditorRequiresKeyboardFocus
  327. bool keyPressed (const KeyPress& kp)
  328. {
  329. owner->updateSize();
  330. forwardCurrentKeyEventToHostWindow();
  331. return true;
  332. }
  333. #endif
  334. private:
  335. //==============================================================================
  336. void* const hostWindow;
  337. void* nsWindow;
  338. JuceCustomUIView* const owner;
  339. int titleW, titleH;
  340. Component* getEditor() const { return getChildComponent (0); }
  341. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (EditorCompWrapper);
  342. };
  343. };
  344. JuceCustomUIView* getView() const
  345. {
  346. return dynamic_cast <JuceCustomUIView*> (fOurPlugInView);
  347. }
  348. void GetViewRect (Rect* size)
  349. {
  350. if (JuceCustomUIView* const v = getView())
  351. v->updateSize();
  352. CEffectProcessRTAS::GetViewRect (size);
  353. }
  354. CPlugInView* CreateCPlugInView()
  355. {
  356. return new JuceCustomUIView (juceFilter, this);
  357. }
  358. void SetViewPort (GrafPtr port)
  359. {
  360. CEffectProcessRTAS::SetViewPort (port);
  361. if (JuceCustomUIView* const v = getView())
  362. v->attachToWindow (port);
  363. }
  364. //==============================================================================
  365. protected:
  366. ComponentResult GetDelaySamplesLong (long* aNumSamples)
  367. {
  368. if (aNumSamples != nullptr)
  369. *aNumSamples = juceFilter != nullptr ? juceFilter->getLatencySamples() : 0;
  370. return noErr;
  371. }
  372. //==============================================================================
  373. void EffectInit()
  374. {
  375. SFicPlugInStemFormats stems;
  376. GetProcessType()->GetStemFormats (&stems);
  377. juceFilter->setPlayConfigDetails (fNumInputs, fNumOutputs,
  378. juceFilter->getSampleRate(), juceFilter->getBlockSize());
  379. AddControl (new CPluginControl_OnOff ('bypa', "Master Bypass\nMastrByp\nMByp\nByp", false, true));
  380. DefineMasterBypassControlIndex (bypassControlIndex);
  381. for (int i = 0; i < juceFilter->getNumParameters(); ++i)
  382. AddControl (new JucePluginControl (juceFilter, i));
  383. // we need to do this midi log-in to get timecode, regardless of whether
  384. // the plugin actually uses midi...
  385. if (MIDILogIn() == noErr)
  386. {
  387. #if JucePlugin_WantsMidiInput
  388. if (CEffectType* const type = dynamic_cast <CEffectType*> (this->GetProcessType()))
  389. {
  390. char nodeName [64];
  391. type->GetProcessTypeName (63, nodeName);
  392. p2cstrcpy (nodeName, reinterpret_cast <unsigned char*> (nodeName));
  393. midiBufferNode = new CEffectMIDIOtherBufferedNode (&mMIDIWorld,
  394. 8192,
  395. eLocalNode,
  396. nodeName,
  397. midiBuffer);
  398. midiBufferNode->Initialize (1, true);
  399. }
  400. #endif
  401. }
  402. midiTransport = new CEffectMIDITransport (&mMIDIWorld);
  403. juceFilter->setPlayHead (this);
  404. juceFilter->addListener (this);
  405. midiEvents.ensureSize (2048);
  406. }
  407. void handleAsyncUpdate()
  408. {
  409. if (! prepared)
  410. {
  411. sampleRate = gProcessGroup->GetSampleRate();
  412. jassert (sampleRate > 0);
  413. channels.calloc (jmax (juceFilter->getNumInputChannels(),
  414. juceFilter->getNumOutputChannels()));
  415. juceFilter->setPlayConfigDetails (fNumInputs, fNumOutputs,
  416. sampleRate, mRTGlobals->mHWBufferSizeInSamples);
  417. juceFilter->prepareToPlay (sampleRate, mRTGlobals->mHWBufferSizeInSamples);
  418. prepared = true;
  419. }
  420. }
  421. void RenderAudio (float** inputs, float** outputs, long numSamples)
  422. {
  423. if (! prepared)
  424. {
  425. asyncUpdater->triggerAsyncUpdate();
  426. bypassBuffers (inputs, outputs, numSamples);
  427. return;
  428. }
  429. #if JucePlugin_WantsMidiInput
  430. midiEvents.clear();
  431. const Cmn_UInt32 bufferSize = mRTGlobals->mHWBufferSizeInSamples;
  432. if (midiBufferNode != nullptr)
  433. {
  434. if (midiBufferNode->GetAdvanceScheduleTime() != bufferSize)
  435. midiBufferNode->SetAdvanceScheduleTime (bufferSize);
  436. if (midiBufferNode->FillMIDIBuffer (mRTGlobals->mRunningTime, numSamples) == noErr)
  437. {
  438. jassert (midiBufferNode->GetBufferPtr() != nullptr);
  439. const int numMidiEvents = midiBufferNode->GetBufferSize();
  440. for (int i = 0; i < numMidiEvents; ++i)
  441. {
  442. const DirectMidiPacket& m = midiBuffer[i];
  443. jassert ((int) m.mTimestamp < numSamples);
  444. midiEvents.addEvent (m.mData, m.mLength,
  445. jlimit (0, (int) numSamples - 1, (int) m.mTimestamp));
  446. }
  447. }
  448. }
  449. #endif
  450. #if JUCE_DEBUG || JUCE_LOG_ASSERTIONS
  451. const int numMidiEventsComingIn = midiEvents.getNumEvents();
  452. (void) numMidiEventsComingIn;
  453. #endif
  454. {
  455. const ScopedLock sl (juceFilter->getCallbackLock());
  456. const int numIn = juceFilter->getNumInputChannels();
  457. const int numOut = juceFilter->getNumOutputChannels();
  458. const int totalChans = jmax (numIn, numOut);
  459. if (juceFilter->isSuspended())
  460. {
  461. for (int i = 0; i < numOut; ++i)
  462. zeromem (outputs [i], sizeof (float) * numSamples);
  463. }
  464. else
  465. {
  466. {
  467. int i;
  468. for (i = 0; i < numOut; ++i)
  469. {
  470. channels[i] = outputs [i];
  471. if (i < numIn && inputs != outputs)
  472. memcpy (outputs [i], inputs[i], sizeof (float) * numSamples);
  473. }
  474. for (; i < numIn; ++i)
  475. channels [i] = inputs [i];
  476. }
  477. AudioSampleBuffer chans (channels, totalChans, numSamples);
  478. if (mBypassed)
  479. juceFilter->processBlockBypassed (chans, midiEvents);
  480. else
  481. juceFilter->processBlock (chans, midiEvents);
  482. }
  483. }
  484. if (! midiEvents.isEmpty())
  485. {
  486. #if JucePlugin_ProducesMidiOutput
  487. const juce::uint8* midiEventData;
  488. int midiEventSize, midiEventPosition;
  489. MidiBuffer::Iterator i (midiEvents);
  490. while (i.getNextEvent (midiEventData, midiEventSize, midiEventPosition))
  491. {
  492. //jassert (midiEventPosition >= 0 && midiEventPosition < (int) numSamples);
  493. }
  494. #elif JUCE_DEBUG || JUCE_LOG_ASSERTIONS
  495. // if your plugin creates midi messages, you'll need to set
  496. // the JucePlugin_ProducesMidiOutput macro to 1 in your
  497. // JucePluginCharacteristics.h file
  498. jassert (midiEvents.getNumEvents() <= numMidiEventsComingIn);
  499. #endif
  500. midiEvents.clear();
  501. }
  502. }
  503. //==============================================================================
  504. ComponentResult GetChunkSize (OSType chunkID, long* size)
  505. {
  506. if (chunkID == juceChunkType)
  507. {
  508. tempFilterData.setSize (0);
  509. juceFilter->getStateInformation (tempFilterData);
  510. *size = sizeof (SFicPlugInChunkHeader) + tempFilterData.getSize();
  511. return noErr;
  512. }
  513. return CEffectProcessMIDI::GetChunkSize (chunkID, size);
  514. }
  515. ComponentResult GetChunk (OSType chunkID, SFicPlugInChunk* chunk)
  516. {
  517. if (chunkID == juceChunkType)
  518. {
  519. if (tempFilterData.getSize() == 0)
  520. juceFilter->getStateInformation (tempFilterData);
  521. chunk->fSize = sizeof (SFicPlugInChunkHeader) + tempFilterData.getSize();
  522. tempFilterData.copyTo ((void*) chunk->fData, 0, tempFilterData.getSize());
  523. tempFilterData.setSize (0);
  524. return noErr;
  525. }
  526. return CEffectProcessMIDI::GetChunk (chunkID, chunk);
  527. }
  528. ComponentResult SetChunk (OSType chunkID, SFicPlugInChunk* chunk)
  529. {
  530. if (chunkID == juceChunkType)
  531. {
  532. tempFilterData.setSize (0);
  533. if (chunk->fSize - sizeof (SFicPlugInChunkHeader) > 0)
  534. {
  535. juceFilter->setStateInformation ((void*) chunk->fData,
  536. chunk->fSize - sizeof (SFicPlugInChunkHeader));
  537. }
  538. return noErr;
  539. }
  540. return CEffectProcessMIDI::SetChunk (chunkID, chunk);
  541. }
  542. //==============================================================================
  543. ComponentResult UpdateControlValue (long controlIndex, long value)
  544. {
  545. if (controlIndex != bypassControlIndex)
  546. juceFilter->setParameter (controlIndex - 2, longToFloat (value));
  547. else
  548. mBypassed = (value > 0);
  549. return CProcess::UpdateControlValue (controlIndex, value);
  550. }
  551. //==============================================================================
  552. bool getCurrentPosition (AudioPlayHead::CurrentPositionInfo& info)
  553. {
  554. // this method can only be called while the plugin is running
  555. jassert (prepared);
  556. Cmn_Float64 bpm = 120.0;
  557. Cmn_Int32 num = 4, denom = 4;
  558. Cmn_Int64 ticks = 0;
  559. Cmn_Bool isPlaying = false;
  560. if (midiTransport != nullptr)
  561. {
  562. midiTransport->GetCurrentTempo (&bpm);
  563. midiTransport->IsTransportPlaying (&isPlaying);
  564. midiTransport->GetCurrentMeter (&num, &denom);
  565. // (The following is a work-around because GetCurrentTickPosition() doesn't work correctly).
  566. Cmn_Int64 sampleLocation;
  567. if (isPlaying)
  568. midiTransport->GetCurrentRTASSampleLocation (&sampleLocation);
  569. else
  570. midiTransport->GetCurrentTDMSampleLocation (&sampleLocation);
  571. midiTransport->GetCustomTickPosition (&ticks, sampleLocation);
  572. info.timeInSamples = (int64) sampleLocation;
  573. info.timeInSeconds = sampleLocation / sampleRate;
  574. }
  575. else
  576. {
  577. info.timeInSamples = 0;
  578. info.timeInSeconds = 0;
  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. double framesPerSec = 24.0;
  591. switch (fTimeCodeInfo.mFrameRate)
  592. {
  593. case ficFrameRate_24Frame: info.frameRate = AudioPlayHead::fps24; break;
  594. case ficFrameRate_25Frame: info.frameRate = AudioPlayHead::fps25; framesPerSec = 25.0; break;
  595. case ficFrameRate_2997NonDrop: info.frameRate = AudioPlayHead::fps2997; framesPerSec = 29.97002997; break;
  596. case ficFrameRate_2997DropFrame: info.frameRate = AudioPlayHead::fps2997drop; framesPerSec = 29.97002997; break;
  597. case ficFrameRate_30NonDrop: info.frameRate = AudioPlayHead::fps30; framesPerSec = 30.0; break;
  598. case ficFrameRate_30DropFrame: info.frameRate = AudioPlayHead::fps30drop; framesPerSec = 30.0; break;
  599. case ficFrameRate_23976: info.frameRate = AudioPlayHead::fps24; framesPerSec = 23.976; break;
  600. default: info.frameRate = AudioPlayHead::fpsUnknown; break;
  601. }
  602. info.editOriginTime = fTimeCodeInfo.mFrameOffset / framesPerSec;
  603. return true;
  604. }
  605. void audioProcessorParameterChanged (AudioProcessor*, int index, float newValue)
  606. {
  607. SetControlValue (index + 2, floatToLong (newValue));
  608. }
  609. void audioProcessorParameterChangeGestureBegin (AudioProcessor*, int index)
  610. {
  611. TouchControl (index + 2);
  612. }
  613. void audioProcessorParameterChangeGestureEnd (AudioProcessor*, int index)
  614. {
  615. ReleaseControl (index + 2);
  616. }
  617. void audioProcessorChanged (AudioProcessor*)
  618. {
  619. // xxx is there an RTAS equivalent?
  620. }
  621. void touchAllParameters()
  622. {
  623. for (int i = 0; i < juceFilter->getNumParameters(); ++i)
  624. {
  625. audioProcessorParameterChangeGestureBegin (0, i);
  626. audioProcessorParameterChanged (0, i, juceFilter->getParameter (i));
  627. audioProcessorParameterChangeGestureEnd (0, i);
  628. }
  629. }
  630. public:
  631. // Need to use an intermediate class here rather than inheriting from AsyncUpdater, so that it can
  632. // be deleted before shutting down juce in our destructor.
  633. class InternalAsyncUpdater : public AsyncUpdater
  634. {
  635. public:
  636. InternalAsyncUpdater (JucePlugInProcess& owner_) : owner (owner_) {}
  637. void handleAsyncUpdate() { owner.handleAsyncUpdate(); }
  638. private:
  639. JucePlugInProcess& owner;
  640. };
  641. //==============================================================================
  642. private:
  643. ScopedPointer<AudioProcessor> juceFilter;
  644. MidiBuffer midiEvents;
  645. ScopedPointer<CEffectMIDIOtherBufferedNode> midiBufferNode;
  646. ScopedPointer<CEffectMIDITransport> midiTransport;
  647. DirectMidiPacket midiBuffer [midiBufferSize];
  648. ScopedPointer<InternalAsyncUpdater> asyncUpdater;
  649. juce::MemoryBlock tempFilterData;
  650. HeapBlock <float*> channels;
  651. bool prepared;
  652. double sampleRate;
  653. static float longToFloat (const long n) noexcept
  654. {
  655. return (float) ((((double) n) + (double) 0x80000000) / (double) 0xffffffff);
  656. }
  657. static long floatToLong (const float n) noexcept
  658. {
  659. return roundToInt (jlimit (-(double) 0x80000000, (double) 0x7fffffff,
  660. n * (double) 0xffffffff - (double) 0x80000000));
  661. }
  662. void bypassBuffers (float** const inputs, float** const outputs, const long numSamples) const
  663. {
  664. for (int i = fNumOutputs; --i >= 0;)
  665. {
  666. if (i < fNumInputs)
  667. memcpy (outputs[i], inputs[i], numSamples * sizeof (float));
  668. else
  669. zeromem (outputs[i], numSamples * sizeof (float));
  670. }
  671. }
  672. //==============================================================================
  673. class JucePluginControl : public CPluginControl
  674. {
  675. public:
  676. //==============================================================================
  677. JucePluginControl (AudioProcessor* const juceFilter_, const int index_)
  678. : juceFilter (juceFilter_),
  679. index (index_)
  680. {
  681. }
  682. //==============================================================================
  683. OSType GetID() const { return index + 1; }
  684. long GetDefaultValue() const { return floatToLong (0); }
  685. void SetDefaultValue (long) {}
  686. long GetNumSteps() const { return 0xffffffff; }
  687. long ConvertStringToValue (const char* valueString) const
  688. {
  689. return floatToLong (String (valueString).getFloatValue());
  690. }
  691. Cmn_Bool IsKeyValid (long key) const { return true; }
  692. void GetNameOfLength (char* name, int maxLength, OSType inControllerType) const
  693. {
  694. // Pro-tools expects all your parameters to have valid names!
  695. jassert (juceFilter->getParameterName (index).isNotEmpty());
  696. juceFilter->getParameterName (index).copyToUTF8 (name, maxLength);
  697. }
  698. long GetPriority() const { return kFicCooperativeTaskPriority; }
  699. long GetOrientation() const
  700. {
  701. return kDAE_LeftMinRightMax | kDAE_BottomMinTopMax
  702. | kDAE_RotarySingleDotMode | kDAE_RotaryLeftMinRightMax;
  703. }
  704. long GetControlType() const { return kDAE_ContinuousValues; }
  705. void GetValueString (char* valueString, int maxLength, long value) const
  706. {
  707. juceFilter->getParameterText (index).copyToUTF8 (valueString, maxLength);
  708. }
  709. Cmn_Bool IsAutomatable() const
  710. {
  711. return juceFilter->isParameterAutomatable (index);
  712. }
  713. private:
  714. //==============================================================================
  715. AudioProcessor* const juceFilter;
  716. const int index;
  717. JUCE_DECLARE_NON_COPYABLE (JucePluginControl);
  718. };
  719. };
  720. //==============================================================================
  721. class JucePlugInGroup : public CEffectGroupMIDI
  722. {
  723. public:
  724. //==============================================================================
  725. JucePlugInGroup()
  726. {
  727. DefineManufacturerNamesAndID (JucePlugin_Manufacturer, JucePlugin_RTASManufacturerCode);
  728. DefinePlugInNamesAndVersion (createRTASName().toUTF8(), JucePlugin_VersionCode);
  729. #ifndef JUCE_DEBUG
  730. AddGestalt (pluginGestalt_IsCacheable);
  731. #endif
  732. }
  733. ~JucePlugInGroup()
  734. {
  735. shutdownJuce_GUI();
  736. }
  737. //==============================================================================
  738. void CreateEffectTypes()
  739. {
  740. const short channelConfigs[][2] = { JucePlugin_PreferredChannelConfigurations };
  741. const int numConfigs = numElementsInArray (channelConfigs);
  742. // You need to actually add some configurations to the JucePlugin_PreferredChannelConfigurations
  743. // value in your JucePluginCharacteristics.h file..
  744. jassert (numConfigs > 0);
  745. for (int i = 0; i < numConfigs; ++i)
  746. {
  747. CEffectType* const type
  748. = new CEffectTypeRTAS ('jcaa' + i,
  749. JucePlugin_RTASProductId,
  750. JucePlugin_RTASCategory);
  751. type->DefineTypeNames (createRTASName().toUTF8().getAddress());
  752. type->DefineSampleRateSupport (eSupports48kAnd96kAnd192k);
  753. type->DefineStemFormats (getFormatForChans (channelConfigs [i][0] != 0 ? channelConfigs [i][0] : channelConfigs [i][1]),
  754. getFormatForChans (channelConfigs [i][1] != 0 ? channelConfigs [i][1] : channelConfigs [i][0]));
  755. #if ! JucePlugin_RTASDisableBypass
  756. type->AddGestalt (pluginGestalt_CanBypass);
  757. #endif
  758. #if JucePlugin_RTASDisableMultiMono
  759. type->AddGestalt (pluginGestalt_DoesntSupportMultiMono);
  760. #endif
  761. type->AddGestalt (pluginGestalt_SupportsVariableQuanta);
  762. type->AttachEffectProcessCreator (createNewProcess);
  763. AddEffectType (type);
  764. }
  765. }
  766. void Initialize()
  767. {
  768. CEffectGroupMIDI::Initialize();
  769. }
  770. //==============================================================================
  771. private:
  772. static CEffectProcess* createNewProcess()
  773. {
  774. #if JUCE_WINDOWS
  775. Process::setCurrentModuleInstanceHandle (gThisModule);
  776. #endif
  777. initialiseJuce_GUI();
  778. return new JucePlugInProcess();
  779. }
  780. static String createRTASName()
  781. {
  782. return String (JucePlugin_Name) + "\n"
  783. + String (JucePlugin_Desc);
  784. }
  785. static EPlugIn_StemFormat getFormatForChans (const int numChans) noexcept
  786. {
  787. switch (numChans)
  788. {
  789. case 0: return ePlugIn_StemFormat_Generic;
  790. case 1: return ePlugIn_StemFormat_Mono;
  791. case 2: return ePlugIn_StemFormat_Stereo;
  792. case 3: return ePlugIn_StemFormat_LCR;
  793. case 4: return ePlugIn_StemFormat_Quad;
  794. case 5: return ePlugIn_StemFormat_5dot0;
  795. case 6: return ePlugIn_StemFormat_5dot1;
  796. case 7: return ePlugIn_StemFormat_6dot1;
  797. #if PT_VERS_MAJOR >= 9
  798. case 8: return ePlugIn_StemFormat_7dot1DTS;
  799. #else
  800. case 8: return ePlugIn_StemFormat_7dot1;
  801. #endif
  802. default: jassertfalse; break; // hmm - not a valid number of chans for RTAS..
  803. }
  804. return ePlugIn_StemFormat_Generic;
  805. }
  806. };
  807. void initialiseMacRTAS();
  808. CProcessGroupInterface* CProcessGroup::CreateProcessGroup()
  809. {
  810. #if JUCE_MAC
  811. initialiseMacRTAS();
  812. #endif
  813. return new JucePlugInGroup();
  814. }
  815. #endif