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.

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