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.

1059 lines
36KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-7 by Raw Material Software ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the
  7. GNU General Public License, as published by the Free Software Foundation;
  8. either version 2 of the License, or (at your option) any later version.
  9. JUCE is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with JUCE; if not, visit www.gnu.org/licenses or write to the
  15. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. Boston, MA 02111-1307 USA
  17. ------------------------------------------------------------------------------
  18. If you'd like to release a closed-source product which uses JUCE, commercial
  19. licenses are also available: visit www.rawmaterialsoftware.com/juce for
  20. more information.
  21. ==============================================================================
  22. */
  23. #include "juce_RTASCompileFlags.h"
  24. #ifdef _MSC_VER
  25. // the Digidesign projects all use a struct alignment of 2..
  26. #pragma pack (2)
  27. #pragma warning (disable: 4267)
  28. #include "ForcedInclude.h"
  29. #include "Mac2Win.H"
  30. #endif
  31. /* Note about include paths
  32. ------------------------
  33. To be able to include all the Digidesign headers correctly, you'll need to add this
  34. lot to your include path:
  35. c:\yourdirectory\PT_711_SDK\AlturaPorts\TDMPlugins\PluginLibrary\EffectClasses
  36. c:\yourdirectory\PT_711_SDK\AlturaPorts\TDMPlugins\PluginLibrary\ProcessClasses
  37. c:\yourdirectory\PT_711_SDK\AlturaPorts\TDMPlugins\PluginLibrary\ProcessClasses\Interfaces
  38. c:\yourdirectory\PT_711_SDK\AlturaPorts\TDMPlugins\PluginLibrary\Utilities
  39. c:\yourdirectory\PT_711_SDK\AlturaPorts\TDMPlugins\PluginLibrary\RTASP_Adapt
  40. c:\yourdirectory\PT_711_SDK\AlturaPorts\TDMPlugins\PluginLibrary\CoreClasses
  41. c:\yourdirectory\PT_711_SDK\AlturaPorts\TDMPlugins\PluginLibrary\Controls
  42. c:\yourdirectory\PT_711_SDK\AlturaPorts\TDMPlugins\PluginLibrary\Meters
  43. c:\yourdirectory\PT_711_SDK\AlturaPorts\TDMPlugins\PluginLibrary\ViewClasses
  44. c:\yourdirectory\PT_711_SDK\AlturaPorts\TDMPlugins\PluginLibrary\DSPClasses
  45. c:\yourdirectory\PT_711_SDK\AlturaPorts\TDMPlugins\PluginLibrary\Interfaces
  46. c:\yourdirectory\PT_711_SDK\AlturaPorts\TDMPlugins\common
  47. c:\yourdirectory\PT_711_SDK\AlturaPorts\TDMPlugins\common\Platform
  48. c:\yourdirectory\PT_711_SDK\AlturaPorts\TDMPlugins\SignalProcessing\Public
  49. c:\yourdirectory\PT_711_SDK\AlturaPorts\TDMPlugIns\DSPManager\Interfaces
  50. c:\yourdirectory\PT_711_SDK\AlturaPorts\SADriver\Interfaces
  51. c:\yourdirectory\PT_711_SDK\AlturaPorts\DigiPublic\Interfaces
  52. c:\yourdirectory\PT_711_SDK\AlturaPorts\Fic\Interfaces\DAEClient
  53. c:\yourdirectory\PT_711_SDK\AlturaPorts\NewFileLibs\Cmn
  54. c:\yourdirectory\PT_711_SDK\AlturaPorts\NewFileLibs\DOA
  55. c:\yourdirectory\PT_711_SDK\AlturaPorts\AlturaSource\PPC_H
  56. c:\yourdirectory\PT_711_SDK\AlturaPorts\AlturaSource\AppSupport
  57. */
  58. #include "CEffectGroupMIDI.h"
  59. #include "CEffectProcessMIDI.h"
  60. #include "CEffectProcessRTAS.h"
  61. #include "CCustomView.h"
  62. #include "CEffectTypeRTAS.h"
  63. #include "CPluginControl.h"
  64. #include "CPluginControl_OnOff.h"
  65. //==============================================================================
  66. #include "../../juce_AudioFilterBase.h"
  67. #include "../../juce_AudioFilterEditor.h"
  68. #include "../../juce_IncludeCharacteristics.h"
  69. #undef Component
  70. //==============================================================================
  71. #if JUCE_WIN32
  72. extern void JUCE_CALLTYPE attachSubWindow (void* hostWindow, int& titleW, int& titleH, juce::Component* comp);
  73. extern void JUCE_CALLTYPE resizeHostWindow (void* hostWindow, int& titleW, int& titleH, juce::Component* comp);
  74. #if ! JucePlugin_EditorRequiresKeyboardFocus
  75. extern void JUCE_CALLTYPE passFocusToHostWindow (void* hostWindow);
  76. #endif
  77. #endif
  78. const int midiBufferSize = 1024;
  79. const OSType juceChunkType = 'juce';
  80. static const int bypassControlIndex = 1;
  81. //==============================================================================
  82. static float longToFloat (const long n) throw()
  83. {
  84. return (float) ((((double) n) + (double) 0x80000000) / (double) 0xffffffff);
  85. }
  86. static long floatToLong (const float n) throw()
  87. {
  88. return roundDoubleToInt (jlimit (-(double) 0x80000000,
  89. (double) 0x7fffffff,
  90. n * (double) 0xffffffff - (double) 0x80000000));
  91. }
  92. //==============================================================================
  93. class JucePlugInProcess : public CEffectProcessMIDI,
  94. public CEffectProcessRTAS,
  95. public AudioFilterBase::HostCallbacks,
  96. public AsyncUpdater
  97. {
  98. public:
  99. //==============================================================================
  100. JucePlugInProcess()
  101. : channels (0),
  102. prepared (false),
  103. midiBufferNode (0),
  104. midiTransport (0),
  105. sampleRate (44100.0)
  106. {
  107. juceFilter = createPluginFilter();
  108. jassert (juceFilter != 0);
  109. AddChunk (juceChunkType, "Juce Audio Plugin Data");
  110. }
  111. ~JucePlugInProcess()
  112. {
  113. if (mLoggedIn)
  114. MIDILogOut();
  115. deleteAndZero (midiBufferNode);
  116. deleteAndZero (midiTransport);
  117. if (prepared)
  118. juceFilter->releaseResources();
  119. delete juceFilter;
  120. juce_free (channels);
  121. }
  122. //==============================================================================
  123. class JuceCustomUIView : public CCustomView
  124. {
  125. public:
  126. //==============================================================================
  127. JuceCustomUIView (AudioFilterBase* const filter_)
  128. : filter (filter_),
  129. editorComp (0),
  130. wrapper (0)
  131. {
  132. // setting the size in here crashes PT for some reason, so keep it simple..
  133. }
  134. ~JuceCustomUIView()
  135. {
  136. deleteEditorComp();
  137. }
  138. //==============================================================================
  139. void updateSize()
  140. {
  141. if (editorComp == 0)
  142. {
  143. editorComp = filter->createEditorIfNeeded();
  144. jassert (editorComp != 0);
  145. }
  146. Rect r;
  147. r.left = 0;
  148. r.top = 0;
  149. r.right = editorComp->getWidth();
  150. r.bottom = editorComp->getHeight();
  151. SetRect (&r);
  152. }
  153. void attachToWindow (GrafPtr port)
  154. {
  155. if (port != 0)
  156. {
  157. updateSize();
  158. #if JUCE_WIN32
  159. void* const hostWindow = (void*) ASI_GethWnd ((WindowPtr) port);
  160. #else
  161. void* const hostWindow = (void*) GetWindowFromPort (port);
  162. #endif
  163. deleteAndZero (wrapper);
  164. wrapper = new EditorCompWrapper (hostWindow, editorComp, this);
  165. }
  166. else
  167. {
  168. deleteEditorComp();
  169. }
  170. }
  171. void DrawContents (Rect* r)
  172. {
  173. if (wrapper != 0)
  174. {
  175. ComponentPeer* const peer = wrapper->getPeer();
  176. if (peer != 0)
  177. {
  178. #if JUCE_WIN32
  179. // (seems to be required in PT6.4, but not in 7.x)
  180. peer->repaint (0, 0, wrapper->getWidth(), wrapper->getHeight());
  181. #elif JUCE_PPC
  182. // This crap is needed because if you resize a window, PT doesn't
  183. // update its clip region, so only part of your new window gets drawn.
  184. // This overrides the clipping region that's being passed into the Draw
  185. // method.
  186. Rect visible;
  187. GetVisibleRect (&visible);
  188. RestoreFocus();
  189. Focus (&visible);
  190. #endif
  191. peer->performAnyPendingRepaintsNow();
  192. }
  193. }
  194. }
  195. void DrawBackground (Rect* r)
  196. {
  197. }
  198. //==============================================================================
  199. private:
  200. AudioFilterBase* const filter;
  201. juce::Component* wrapper;
  202. AudioFilterEditor* editorComp;
  203. void deleteEditorComp()
  204. {
  205. if (editorComp != 0)
  206. {
  207. PopupMenu::dismissAllActiveMenus();
  208. juce::Component* const modalComponent = juce::Component::getCurrentlyModalComponent();
  209. if (modalComponent != 0)
  210. modalComponent->exitModalState (0);
  211. filter->editorBeingDeleted (editorComp);
  212. deleteAndZero (editorComp);
  213. deleteAndZero (wrapper);
  214. }
  215. }
  216. //==============================================================================
  217. // A component to hold the AudioFilterEditor, and cope with some housekeeping
  218. // chores when it changes or repaints.
  219. class EditorCompWrapper : public juce::Component,
  220. #if JUCE_MAC
  221. public Timer
  222. #else
  223. public FocusChangeListener
  224. #endif
  225. {
  226. public:
  227. EditorCompWrapper (void* const hostWindow_,
  228. AudioFilterEditor* const editorComp,
  229. JuceCustomUIView* const owner_)
  230. : hostWindow (hostWindow_),
  231. owner (owner_),
  232. titleW (0),
  233. titleH (0)
  234. #if JUCE_MAC
  235. , forcedRepaintTimer (0)
  236. #endif
  237. {
  238. #if ! JucePlugin_EditorRequiresKeyboardFocus
  239. setWantsKeyboardFocus (false);
  240. #endif
  241. setOpaque (true);
  242. setBroughtToFrontOnMouseClick (true);
  243. setBounds (editorComp->getBounds());
  244. editorComp->setTopLeftPosition (0, 0);
  245. addAndMakeVisible (editorComp);
  246. #if JUCE_WIN32
  247. attachSubWindow (hostWindow, titleW, titleH, this);
  248. setVisible (true);
  249. #else
  250. SetAutomaticControlDragTrackingEnabledForWindow ((WindowRef) hostWindow_, true);
  251. WindowAttributes attributes;
  252. GetWindowAttributes ((WindowRef) hostWindow_, &attributes);
  253. parentView = 0;
  254. if ((attributes & kWindowCompositingAttribute) != 0)
  255. {
  256. HIViewRef root = HIViewGetRoot ((WindowRef) hostWindow_);
  257. HIViewFindByID (root, kHIViewWindowContentID, &parentView);
  258. if (parentView == 0)
  259. parentView = root;
  260. }
  261. else
  262. {
  263. GetRootControl ((WindowRef) hostWindow_, (ControlRef*) &parentView);
  264. if (parentView == 0)
  265. CreateRootControl ((WindowRef) hostWindow_, (ControlRef*) &parentView);
  266. }
  267. jassert (parentView != 0);
  268. Rect clientRect;
  269. GetWindowBounds ((WindowRef) hostWindow, kWindowContentRgn, &clientRect);
  270. titleW = clientRect.right - clientRect.left;
  271. titleH = jmax (0, (clientRect.bottom - clientRect.top) - getHeight());
  272. setTopLeftPosition (0, 0);
  273. HIViewSetNeedsDisplay (parentView, true);
  274. setVisible (true);
  275. addToDesktop (ComponentPeer::windowRepaintedExplictly, (void*) parentView);
  276. HIViewRef pluginView = HIViewGetFirstSubview (parentView);
  277. #if ! JucePlugin_EditorRequiresKeyboardFocus
  278. HIViewSetActivated (pluginView, false);
  279. #endif
  280. /* This is a convoluted and desperate workaround for a Digi (or maybe Apple)
  281. layout bug. Until the parent control gets some kind of mouse-move
  282. event, then our plugin's HIView remains stuck at (0, 0) in the
  283. window (despite drawing correctly), which blocks mouse events from
  284. getting to the widgets above it.
  285. After days of frustration the only hack I can find that works
  286. is to use this arcane function to redirect mouse events to
  287. the parent, while running a timer to spot the moment when our
  288. view mysteriously snaps back to its correct location.
  289. If anyone at Digi or Apple is reading this and they realise that it's
  290. their fault, could they please give me back the week of my life that
  291. they made me waste on this rubbish. Thankyou.
  292. */
  293. SetControlSupervisor (pluginView, parentView);
  294. startTimer (150);
  295. #endif
  296. #if JUCE_WIN32 && ! JucePlugin_EditorRequiresKeyboardFocus
  297. Desktop::getInstance().addFocusChangeListener (this);
  298. #endif
  299. }
  300. ~EditorCompWrapper()
  301. {
  302. #if JUCE_WIN32 && ! JucePlugin_EditorRequiresKeyboardFocus
  303. Desktop::getInstance().removeFocusChangeListener (this);
  304. #endif
  305. #if JUCE_MAC
  306. delete forcedRepaintTimer;
  307. #endif
  308. }
  309. void paint (Graphics& g)
  310. {
  311. #if JUCE_MAC
  312. if (forcedRepaintTimer != 0)
  313. forcedRepaintTimer->stopTimer();
  314. #endif
  315. }
  316. void resized()
  317. {
  318. juce::Component* const c = getChildComponent (0);
  319. if (c != 0)
  320. c->setBounds (0, 0, getWidth(), getHeight());
  321. repaint();
  322. }
  323. #if JUCE_MAC
  324. void timerCallback()
  325. {
  326. // Wait for the moment when PT deigns to allow our view to
  327. // take up its actual location (see rant above)
  328. HIViewRef content = 0;
  329. HIViewFindByID (HIViewGetRoot ((WindowRef) hostWindow), kHIViewWindowContentID, &content);
  330. HIPoint p = { 0.0f, 0.0f };
  331. HIViewRef v = HIViewGetFirstSubview (parentView);
  332. HIViewConvertPoint (&p, v, content);
  333. if (p.y > 12)
  334. {
  335. HIViewRef v = HIViewGetFirstSubview (parentView);
  336. SetControlSupervisor (v, 0);
  337. stopTimer();
  338. forcedRepaintTimer = new RepaintCheckTimer (*this);
  339. }
  340. }
  341. #endif
  342. #if JUCE_WIN32
  343. void globalFocusChanged (juce::Component*)
  344. {
  345. #if ! JucePlugin_EditorRequiresKeyboardFocus
  346. if (hasKeyboardFocus (true))
  347. passFocusToHostWindow (hostWindow);
  348. #endif
  349. }
  350. #endif
  351. void childBoundsChanged (juce::Component* child)
  352. {
  353. setSize (child->getWidth(), child->getHeight());
  354. child->setTopLeftPosition (0, 0);
  355. #if JUCE_WIN32
  356. resizeHostWindow (hostWindow, titleW, titleH, this);
  357. #else
  358. Rect r;
  359. GetWindowBounds ((WindowRef) hostWindow, kWindowContentRgn, &r);
  360. HIRect p;
  361. zerostruct (p);
  362. HIViewConvertRect (&p, parentView, 0); // find the X position of our view in case there's space to the left of it
  363. r.right = r.left + jmax (titleW, ((int) p.origin.x) + getWidth());
  364. r.bottom = r.top + getHeight() + titleH;
  365. SetWindowBounds ((WindowRef) hostWindow, kWindowContentRgn, &r);
  366. owner->updateSize();
  367. owner->Invalidate();
  368. #endif
  369. }
  370. //==============================================================================
  371. juce_UseDebuggingNewOperator
  372. #if JUCE_MAC
  373. protected:
  374. void internalRepaint (int x, int y, int w, int h)
  375. {
  376. Component::internalRepaint (x, y, w, h);
  377. owner->Invalidate();
  378. if (forcedRepaintTimer != 0 && ! forcedRepaintTimer->isTimerRunning())
  379. forcedRepaintTimer->startTimer (1000 / 25);
  380. }
  381. HIViewRef parentView;
  382. #endif
  383. private:
  384. void* const hostWindow;
  385. JuceCustomUIView* const owner;
  386. int titleW, titleH;
  387. #if JUCE_MAC
  388. // if PT makes us wait too long for a redraw after we've
  389. // asked for one, this should kick in and force one to happen
  390. class RepaintCheckTimer : public Timer
  391. {
  392. public:
  393. RepaintCheckTimer (EditorCompWrapper& owner_)
  394. : owner (owner_)
  395. {
  396. }
  397. void timerCallback()
  398. {
  399. stopTimer();
  400. ComponentPeer* const peer = owner.getPeer();
  401. if (peer != 0)
  402. peer->performAnyPendingRepaintsNow();
  403. }
  404. EditorCompWrapper& owner;
  405. };
  406. RepaintCheckTimer* forcedRepaintTimer;
  407. #endif
  408. };
  409. };
  410. JuceCustomUIView* getView() const
  411. {
  412. return dynamic_cast <JuceCustomUIView*> (fOurPlugInView);
  413. }
  414. void GetViewRect (Rect* size)
  415. {
  416. if (getView() != 0)
  417. getView()->updateSize();
  418. CEffectProcessRTAS::GetViewRect (size);
  419. }
  420. CPlugInView* CreateCPlugInView()
  421. {
  422. return new JuceCustomUIView (juceFilter);
  423. }
  424. void SetViewPort (GrafPtr port)
  425. {
  426. CEffectProcessRTAS::SetViewPort (port);
  427. if (getView() != 0)
  428. getView()->attachToWindow (port);
  429. }
  430. //==============================================================================
  431. protected:
  432. ComponentResult GetDelaySamplesLong (long* aNumSamples)
  433. {
  434. if (aNumSamples != 0)
  435. *aNumSamples = JucePlugin_Latency;
  436. return noErr;
  437. }
  438. //==============================================================================
  439. void EffectInit()
  440. {
  441. SFicPlugInStemFormats stems;
  442. GetProcessType()->GetStemFormats (&stems);
  443. juceFilter->setPlayConfigDetails (fNumInputs, fNumOutputs,
  444. juceFilter->getSampleRate(), juceFilter->getBlockSize());
  445. AddControl (new CPluginControl_OnOff ('bypa', "Master Bypass\nMastrByp\nMByp\nByp", false, true));
  446. DefineMasterBypassControlIndex (bypassControlIndex);
  447. for (int i = 0; i < juceFilter->getNumParameters(); ++i)
  448. AddControl (new JucePluginControl (juceFilter, i));
  449. // we need to do this midi log-in to get timecode, regardless of whether
  450. // the plugin actually uses midi...
  451. if (MIDILogIn() == noErr)
  452. {
  453. #if JucePlugin_WantsMidiInput
  454. CEffectType* const type = dynamic_cast <CEffectType*> (this->GetProcessType());
  455. if (type != 0)
  456. {
  457. char nodeName [64];
  458. type->GetProcessTypeName (63, nodeName);
  459. p2cstrcpy (nodeName, reinterpret_cast <unsigned char*> (nodeName));
  460. midiBufferNode = new CEffectMIDIOtherBufferedNode (&mMIDIWorld,
  461. 8192,
  462. eLocalNode,
  463. nodeName,
  464. midiBuffer);
  465. midiBufferNode->Initialize (1, true);
  466. }
  467. #endif
  468. }
  469. midiTransport = new CEffectMIDITransport (&mMIDIWorld);
  470. juceFilter->setHostCallbacks (this);
  471. }
  472. void handleAsyncUpdate()
  473. {
  474. if (! prepared)
  475. {
  476. sampleRate = gProcessGroup->GetSampleRate();
  477. jassert (sampleRate > 0);
  478. juce_free (channels);
  479. channels = (float**) juce_calloc (sizeof (float*) * jmax (juceFilter->getNumInputChannels(),
  480. juceFilter->getNumOutputChannels()));
  481. juceFilter->setPlayConfigDetails (fNumInputs, fNumOutputs,
  482. sampleRate, mRTGlobals->mHWBufferSizeInSamples);
  483. juceFilter->prepareToPlay (sampleRate,
  484. mRTGlobals->mHWBufferSizeInSamples);
  485. prepared = true;
  486. }
  487. }
  488. void RenderAudio (float** inputs, float** outputs, long numSamples)
  489. {
  490. if (! prepared)
  491. {
  492. triggerAsyncUpdate();
  493. bypassBuffers (inputs, outputs, numSamples);
  494. return;
  495. }
  496. if (mBypassed)
  497. {
  498. bypassBuffers (inputs, outputs, numSamples);
  499. return;
  500. }
  501. #if JucePlugin_WantsMidiInput
  502. midiEvents.clear();
  503. const Cmn_UInt32 bufferSize = mRTGlobals->mHWBufferSizeInSamples;
  504. if (midiBufferNode->GetAdvanceScheduleTime() != bufferSize)
  505. midiBufferNode->SetAdvanceScheduleTime (bufferSize);
  506. if (midiBufferNode->FillMIDIBuffer (mRTGlobals->mRunningTime, numSamples) == noErr)
  507. {
  508. jassert (midiBufferNode->GetBufferPtr() != 0);
  509. const int numMidiEvents = midiBufferNode->GetBufferSize();
  510. for (int i = 0; i < numMidiEvents; ++i)
  511. {
  512. const DirectMidiPacket& m = midiBuffer[i];
  513. jassert ((int) m.mTimestamp < numSamples);
  514. midiEvents.addEvent (m.mData, m.mLength,
  515. jlimit (0, (int) numSamples - 1, (int) m.mTimestamp));
  516. }
  517. }
  518. #endif
  519. #if defined (JUCE_DEBUG) || JUCE_LOG_ASSERTIONS
  520. const int numMidiEventsComingIn = midiEvents.getNumEvents();
  521. #endif
  522. {
  523. const ScopedLock sl (juceFilter->getCallbackLock());
  524. const int numIn = juceFilter->getNumInputChannels();
  525. const int numOut = juceFilter->getNumOutputChannels();
  526. const int totalChans = jmax (numIn, numOut);
  527. if (juceFilter->isSuspended())
  528. {
  529. for (int i = 0; i < numOut; ++i)
  530. zeromem (outputs [i], sizeof (float) * numSamples);
  531. }
  532. else
  533. {
  534. {
  535. int i;
  536. for (i = 0; i < numOut; ++i)
  537. {
  538. channels[i] = outputs [i];
  539. if (i < numIn && inputs != outputs)
  540. memcpy (outputs [i], inputs[i], sizeof (float) * numSamples);
  541. }
  542. for (; i < numIn; ++i)
  543. channels [i] = inputs [i];
  544. }
  545. AudioSampleBuffer chans (channels, totalChans, numSamples);
  546. juceFilter->processBlock (chans, midiEvents);
  547. }
  548. }
  549. if (! midiEvents.isEmpty())
  550. {
  551. #if JucePlugin_ProducesMidiOutput
  552. const uint8* midiEventData;
  553. int midiEventSize, midiEventPosition;
  554. MidiBuffer::Iterator i (midiEvents);
  555. while (i.getNextEvent (midiEventData, midiEventSize, midiEventPosition))
  556. {
  557. // jassert (midiEventPosition >= 0 && midiEventPosition < (int) numSamples);
  558. //xxx
  559. }
  560. #else
  561. // if your plugin creates midi messages, you'll need to set
  562. // the JucePlugin_ProducesMidiOutput macro to 1 in your
  563. // JucePluginCharacteristics.h file
  564. jassert (midiEvents.getNumEvents() <= numMidiEventsComingIn);
  565. #endif
  566. midiEvents.clear();
  567. }
  568. }
  569. //==============================================================================
  570. ComponentResult GetChunkSize (OSType chunkID, long* size)
  571. {
  572. if (chunkID == juceChunkType)
  573. {
  574. tempFilterData.setSize (0);
  575. juceFilter->getStateInformation (tempFilterData);
  576. *size = sizeof (SFicPlugInChunkHeader) + tempFilterData.getSize();
  577. return noErr;
  578. }
  579. return CEffectProcessMIDI::GetChunkSize (chunkID, size);
  580. }
  581. ComponentResult GetChunk (OSType chunkID, SFicPlugInChunk* chunk)
  582. {
  583. if (chunkID == juceChunkType)
  584. {
  585. if (tempFilterData.getSize() == 0)
  586. juceFilter->getStateInformation (tempFilterData);
  587. chunk->fSize = sizeof (SFicPlugInChunkHeader) + tempFilterData.getSize();
  588. tempFilterData.copyTo ((void*) chunk->fData, 0, tempFilterData.getSize());
  589. tempFilterData.setSize (0);
  590. return noErr;
  591. }
  592. return CEffectProcessMIDI::GetChunk (chunkID, chunk);
  593. }
  594. ComponentResult SetChunk (OSType chunkID, SFicPlugInChunk* chunk)
  595. {
  596. if (chunkID == juceChunkType)
  597. {
  598. tempFilterData.setSize (0);
  599. if (chunk->fSize - sizeof (SFicPlugInChunkHeader) > 0)
  600. {
  601. juceFilter->setStateInformation ((void*) chunk->fData,
  602. chunk->fSize - sizeof (SFicPlugInChunkHeader));
  603. }
  604. return noErr;
  605. }
  606. return CEffectProcessMIDI::SetChunk (chunkID, chunk);
  607. }
  608. //==============================================================================
  609. ComponentResult UpdateControlValue (long controlIndex, long value)
  610. {
  611. if (controlIndex != bypassControlIndex)
  612. juceFilter->setParameter (controlIndex - 2, longToFloat (value));
  613. else
  614. mBypassed = (value > 0);
  615. return CProcess::UpdateControlValue (controlIndex, value);
  616. }
  617. //==============================================================================
  618. bool JUCE_CALLTYPE getCurrentPositionInfo (AudioFilterBase::CurrentPositionInfo& info)
  619. {
  620. // this method can only be called while the plugin is running
  621. jassert (prepared);
  622. Cmn_Float64 bpm = 120.0;
  623. Cmn_Int32 num = 4, denom = 4;
  624. Cmn_Int64 ticks = 0;
  625. Cmn_Bool isPlaying = false;
  626. if (midiTransport != 0)
  627. {
  628. midiTransport->GetCurrentTempo (&bpm);
  629. midiTransport->IsTransportPlaying (&isPlaying);
  630. midiTransport->GetCurrentMeter (&num, &denom);
  631. midiTransport->GetCurrentTickPosition (&ticks);
  632. }
  633. info.bpm = bpm;
  634. info.timeSigNumerator = num;
  635. info.timeSigDenominator = denom;
  636. info.isPlaying = isPlaying;
  637. info.isRecording = false;
  638. info.ppqPosition = ticks / 960000.0;
  639. info.ppqPositionOfLastBarStart = 0; //xxx no idea how to get this correctly..
  640. // xxx incorrect if there are tempo changes, but there's no
  641. // other way of getting this info..
  642. info.timeInSeconds = ticks * (60.0 / 960000.0) / bpm;
  643. double framesPerSec = 24.0;
  644. switch (fTimeCodeInfo.mFrameRate)
  645. {
  646. case ficFrameRate_24Frame:
  647. info.frameRate = AudioFilterBase::CurrentPositionInfo::fps24;
  648. break;
  649. case ficFrameRate_25Frame:
  650. info.frameRate = AudioFilterBase::CurrentPositionInfo::fps25;
  651. framesPerSec = 25.0;
  652. break;
  653. case ficFrameRate_2997NonDrop:
  654. info.frameRate = AudioFilterBase::CurrentPositionInfo::fps2997;
  655. framesPerSec = 29.97002997;
  656. break;
  657. case ficFrameRate_2997DropFrame:
  658. info.frameRate = AudioFilterBase::CurrentPositionInfo::fps2997drop;
  659. framesPerSec = 29.97002997;
  660. break;
  661. case ficFrameRate_30NonDrop:
  662. info.frameRate = AudioFilterBase::CurrentPositionInfo::fps30;
  663. framesPerSec = 30.0;
  664. break;
  665. case ficFrameRate_30DropFrame:
  666. info.frameRate = AudioFilterBase::CurrentPositionInfo::fps30drop;
  667. framesPerSec = 30.0;
  668. break;
  669. case ficFrameRate_23976:
  670. // xxx not strictly true..
  671. info.frameRate = AudioFilterBase::CurrentPositionInfo::fps24;
  672. framesPerSec = 23.976;
  673. break;
  674. default:
  675. info.frameRate = AudioFilterBase::CurrentPositionInfo::fpsUnknown;
  676. break;
  677. }
  678. info.editOriginTime = fTimeCodeInfo.mFrameOffset / framesPerSec;
  679. return true;
  680. }
  681. void JUCE_CALLTYPE informHostOfParameterChange (int index, float newValue)
  682. {
  683. if (juceFilter != 0)
  684. juceFilter->setParameter (index, newValue);
  685. SetControlValue (index + 2, floatToLong (newValue));
  686. }
  687. void JUCE_CALLTYPE informHostOfStateChange()
  688. {
  689. // xxx is there an RTAS equivalent?
  690. }
  691. //==============================================================================
  692. private:
  693. AudioFilterBase* juceFilter;
  694. MidiBuffer midiEvents;
  695. CEffectMIDIOtherBufferedNode* midiBufferNode;
  696. CEffectMIDITransport* midiTransport;
  697. DirectMidiPacket midiBuffer [midiBufferSize];
  698. juce::MemoryBlock tempFilterData;
  699. float** channels;
  700. bool prepared;
  701. double sampleRate;
  702. void bypassBuffers (float** const inputs, float** const outputs, const long numSamples) const
  703. {
  704. for (int i = fNumOutputs; --i >= 0;)
  705. {
  706. if (i < fNumInputs)
  707. memcpy (outputs[i], inputs[i], numSamples * sizeof (float));
  708. else
  709. zeromem (outputs[i], numSamples * sizeof (float));
  710. }
  711. }
  712. //==============================================================================
  713. class JucePluginControl : public CPluginControl
  714. {
  715. public:
  716. //==============================================================================
  717. JucePluginControl (AudioFilterBase* const juceFilter_, const int index_)
  718. : juceFilter (juceFilter_),
  719. index (index_)
  720. {
  721. }
  722. ~JucePluginControl()
  723. {
  724. }
  725. //==============================================================================
  726. OSType GetID() const { return index + 1; }
  727. long GetDefaultValue() const { return floatToLong (0); }
  728. void SetDefaultValue (long) {}
  729. long GetNumSteps() const { return 0xffffffff; }
  730. long ConvertStringToValue (const char* valueString) const
  731. {
  732. return floatToLong (String (valueString).getFloatValue());
  733. }
  734. Cmn_Bool IsKeyValid(long key) const { return true; }
  735. void GetNameOfLength (char* name, int maxLength, OSType inControllerType) const
  736. {
  737. juceFilter->getParameterName (index).copyToBuffer (name, maxLength);
  738. }
  739. long GetPriority() const { return kFicCooperativeTaskPriority; }
  740. long GetOrientation() const
  741. {
  742. return kDAE_LeftMinRightMax | kDAE_BottomMinTopMax
  743. | kDAE_RotarySingleDotMode | kDAE_RotaryLeftMinRightMax;
  744. }
  745. long GetControlType() const { return kDAE_ContinuousValues; }
  746. void GetValueString (char* valueString, int maxLength, long value) const
  747. {
  748. juceFilter->getParameterText (index).copyToBuffer (valueString, maxLength);
  749. }
  750. Cmn_Bool IsAutomatable() const { return true; }
  751. private:
  752. //==============================================================================
  753. AudioFilterBase* const juceFilter;
  754. const int index;
  755. JucePluginControl (const JucePluginControl&);
  756. const JucePluginControl& operator= (const JucePluginControl&);
  757. };
  758. };
  759. //==============================================================================
  760. class JucePlugInGroup : public CEffectGroupMIDI
  761. {
  762. public:
  763. //==============================================================================
  764. JucePlugInGroup()
  765. {
  766. DefineManufacturerNamesAndID (JucePlugin_Manufacturer, JucePlugin_RTASManufacturerCode);
  767. DefinePlugInNamesAndVersion (createRTASName(), JucePlugin_VersionCode);
  768. #ifndef JUCE_DEBUG
  769. AddGestalt (pluginGestalt_IsCacheable);
  770. #endif
  771. }
  772. ~JucePlugInGroup()
  773. {
  774. shutdownJuce_GUI();
  775. shutdownJuce_NonGUI();
  776. }
  777. //==============================================================================
  778. void CreateEffectTypes()
  779. {
  780. const short channelConfigs[][2] = { JucePlugin_PreferredChannelConfigurations };
  781. const int numConfigs = numElementsInArray (channelConfigs);
  782. // You need to actually add some configurations to the JucePlugin_PreferredChannelConfigurations
  783. // value in your JucePluginCharacteristics.h file..
  784. jassert (numConfigs > 0);
  785. for (int i = 0; i < numConfigs; ++i)
  786. {
  787. CEffectType* const type
  788. = new CEffectTypeRTAS ('jcaa' + i,
  789. JucePlugin_RTASProductId,
  790. JucePlugin_RTASCategory);
  791. type->DefineTypeNames (createRTASName());
  792. type->DefineSampleRateSupport (eSupports48kAnd96kAnd192k);
  793. type->DefineStemFormats (getFormatForChans (channelConfigs [i][0]),
  794. getFormatForChans (channelConfigs [i][1]));
  795. type->AddGestalt (pluginGestalt_CanBypass);
  796. type->AddGestalt (pluginGestalt_SupportsVariableQuanta);
  797. type->AttachEffectProcessCreator (createNewProcess);
  798. AddEffectType (type);
  799. }
  800. }
  801. void Initialize()
  802. {
  803. CEffectGroupMIDI::Initialize();
  804. }
  805. //==============================================================================
  806. private:
  807. static CEffectProcess* createNewProcess()
  808. {
  809. // Juce setup
  810. #if JUCE_WIN32
  811. PlatformUtilities::setCurrentModuleInstanceHandle (gThisModule);
  812. #endif
  813. initialiseJuce_GUI();
  814. return new JucePlugInProcess();
  815. }
  816. static const String createRTASName()
  817. {
  818. return String (JucePlugin_Name) + T("\n")
  819. + String (JucePlugin_Name).substring (0, 4);
  820. }
  821. static EPlugIn_StemFormat getFormatForChans (const int numChans) throw()
  822. {
  823. switch (numChans)
  824. {
  825. case 1:
  826. return ePlugIn_StemFormat_Mono;
  827. case 2:
  828. return ePlugIn_StemFormat_Stereo;
  829. case 3:
  830. return ePlugIn_StemFormat_LCR;
  831. case 4:
  832. return ePlugIn_StemFormat_Quad;
  833. case 5:
  834. return ePlugIn_StemFormat_5dot0;
  835. case 6:
  836. return ePlugIn_StemFormat_5dot1;
  837. case 7:
  838. return ePlugIn_StemFormat_6dot1;
  839. case 8:
  840. return ePlugIn_StemFormat_7dot1;
  841. default:
  842. jassertfalse // hmm - not a valid number of chans for RTAS..
  843. break;
  844. }
  845. return ePlugIn_StemFormat_Generic;
  846. }
  847. };
  848. CProcessGroupInterface* CProcessGroup::CreateProcessGroup()
  849. {
  850. initialiseJuce_NonGUI();
  851. return new JucePlugInGroup();
  852. }