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.

1013 lines
34KB

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