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.

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