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.

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