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.

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