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.

1022 lines
34KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-10 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifdef _MSC_VER
  19. // (this is a workaround for a build problem in VC9)
  20. #define _DO_NOT_DECLARE_INTERLOCKED_INTRINSICS_IN_MEMORY
  21. #include <intrin.h>
  22. #endif
  23. #include "juce_RTAS_DigiCode_Header.h"
  24. #if JucePlugin_Build_RTAS
  25. #ifdef _MSC_VER
  26. #include "Mac2Win.H"
  27. #endif
  28. /* Note about include paths
  29. ------------------------
  30. To be able to include all the Digidesign headers correctly, you'll need to add this
  31. lot to your include path:
  32. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\EffectClasses
  33. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\ProcessClasses
  34. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\ProcessClasses\Interfaces
  35. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\Utilities
  36. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\RTASP_Adapt
  37. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\CoreClasses
  38. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\Controls
  39. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\Meters
  40. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\ViewClasses
  41. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\DSPClasses
  42. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\Interfaces
  43. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\common
  44. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\common\Platform
  45. c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\SignalProcessing\Public
  46. C:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugIns\DSPManager\Interfaces
  47. c:\yourdirectory\PT_80_SDK\AlturaPorts\SADriver\Interfaces
  48. c:\yourdirectory\PT_80_SDK\AlturaPorts\DigiPublic\Interfaces
  49. c:\yourdirectory\PT_80_SDK\AlturaPorts\Fic\Interfaces\DAEClient
  50. c:\yourdirectory\PT_80_SDK\AlturaPorts\NewFileLibs\Cmn
  51. c:\yourdirectory\PT_80_SDK\AlturaPorts\NewFileLibs\DOA
  52. c:\yourdirectory\PT_80_SDK\AlturaPorts\AlturaSource\PPC_H
  53. c:\yourdirectory\PT_80_SDK\AlturaPorts\AlturaSource\AppSupport
  54. c:\yourdirectory\PT_80_SDK\AvidCode\AVX2sdk\AVX\avx2\avx2sdk\inc
  55. C:\yourdirectory\PT_80_SDK\xplat\AVX\avx2\avx2sdk\inc
  56. NB. If you hit a huge pile of bugs around here, make sure that you've not got the
  57. Apple QuickTime headers before the PT headers in your path, because there are
  58. some filename clashes between them.
  59. */
  60. #include "CEffectGroupMIDI.h"
  61. #include "CEffectProcessMIDI.h"
  62. #include "CEffectProcessRTAS.h"
  63. #include "CCustomView.h"
  64. #include "CEffectTypeRTAS.h"
  65. #include "CPluginControl.h"
  66. #include "CPluginControl_OnOff.h"
  67. #include "FicProcessTokens.h"
  68. //==============================================================================
  69. #ifdef _MSC_VER
  70. #pragma pack (push, 8)
  71. #endif
  72. #include "../juce_PluginHeaders.h"
  73. #ifdef _MSC_VER
  74. #pragma pack (pop)
  75. #if JUCE_DEBUG
  76. #define PT_LIB_PATH JucePlugin_WinBag_path "\\Debug\\lib\\"
  77. #else
  78. #define PT_LIB_PATH JucePlugin_WinBag_path "\\Release\\lib\\"
  79. #endif
  80. #pragma comment(lib, PT_LIB_PATH "DAE.lib")
  81. #pragma comment(lib, PT_LIB_PATH "DigiExt.lib")
  82. #pragma comment(lib, PT_LIB_PATH "DSI.lib")
  83. #pragma comment(lib, PT_LIB_PATH "PluginLib.lib")
  84. #endif
  85. #undef Component
  86. #undef MemoryBlock
  87. //==============================================================================
  88. #if JUCE_WINDOWS
  89. extern void JUCE_CALLTYPE attachSubWindow (void* hostWindow, int& titleW, int& titleH, JUCE_NAMESPACE::Component* comp);
  90. extern void JUCE_CALLTYPE resizeHostWindow (void* hostWindow, int& titleW, int& titleH, JUCE_NAMESPACE::Component* comp);
  91. #if ! JucePlugin_EditorRequiresKeyboardFocus
  92. extern void JUCE_CALLTYPE passFocusToHostWindow (void* hostWindow);
  93. #endif
  94. #else
  95. extern void* attachSubWindow (void* hostWindowRef, JUCE_NAMESPACE::Component* comp);
  96. extern void removeSubWindow (void* nsWindow, JUCE_NAMESPACE::Component* comp);
  97. extern void forwardCurrentKeyEventToHostWindow();
  98. #endif
  99. const int midiBufferSize = 1024;
  100. const OSType juceChunkType = 'juce';
  101. static const int bypassControlIndex = 1;
  102. //==============================================================================
  103. /** Somewhere in the codebase of your plugin, you need to implement this function
  104. and make it return a new instance of the filter subclass that you're building.
  105. */
  106. extern AudioProcessor* JUCE_CALLTYPE createPluginFilter();
  107. //==============================================================================
  108. static float longToFloat (const long n) throw()
  109. {
  110. return (float) ((((double) n) + (double) 0x80000000) / (double) 0xffffffff);
  111. }
  112. static long floatToLong (const float n) throw()
  113. {
  114. return roundToInt (jlimit (-(double) 0x80000000,
  115. (double) 0x7fffffff,
  116. n * (double) 0xffffffff - (double) 0x80000000));
  117. }
  118. static int numInstances = 0;
  119. //==============================================================================
  120. class JucePlugInProcess : public CEffectProcessMIDI,
  121. public CEffectProcessRTAS,
  122. public AudioProcessorListener,
  123. public AudioPlayHead
  124. {
  125. public:
  126. //==============================================================================
  127. JucePlugInProcess()
  128. : prepared (false),
  129. sampleRate (44100.0)
  130. {
  131. asyncUpdater = new InternalAsyncUpdater (*this);
  132. juceFilter = createPluginFilter();
  133. jassert (juceFilter != 0);
  134. AddChunk (juceChunkType, "Juce Audio Plugin Data");
  135. ++numInstances;
  136. }
  137. ~JucePlugInProcess()
  138. {
  139. if (mLoggedIn)
  140. MIDILogOut();
  141. midiBufferNode = 0;
  142. midiTransport = 0;
  143. if (prepared)
  144. juceFilter->releaseResources();
  145. delete juceFilter;
  146. asyncUpdater = 0;
  147. if (--numInstances == 0)
  148. {
  149. #if JUCE_MAC
  150. // Hack to allow any NSWindows to clear themselves up before returning to PT..
  151. for (int i = 20; --i >= 0;)
  152. MessageManager::getInstance()->runDispatchLoopUntil (1);
  153. #endif
  154. shutdownJuce_GUI();
  155. }
  156. }
  157. //==============================================================================
  158. class JuceCustomUIView : public CCustomView,
  159. public Timer
  160. {
  161. public:
  162. //==============================================================================
  163. JuceCustomUIView (AudioProcessor* const filter_,
  164. JucePlugInProcess* const process_)
  165. : filter (filter_),
  166. process (process_)
  167. {
  168. // setting the size in here crashes PT for some reason, so keep it simple..
  169. }
  170. ~JuceCustomUIView()
  171. {
  172. deleteEditorComp();
  173. }
  174. //==============================================================================
  175. void updateSize()
  176. {
  177. if (editorComp == 0)
  178. {
  179. editorComp = filter->createEditorIfNeeded();
  180. jassert (editorComp != 0);
  181. }
  182. if (editorComp->getWidth() != 0 && editorComp->getHeight() != 0)
  183. {
  184. Rect oldRect;
  185. GetRect (&oldRect);
  186. Rect r;
  187. r.left = 0;
  188. r.top = 0;
  189. r.right = editorComp->getWidth();
  190. r.bottom = editorComp->getHeight();
  191. SetRect (&r);
  192. if ((oldRect.right != r.right) || (oldRect.bottom != r.bottom))
  193. startTimer (50);
  194. }
  195. }
  196. void timerCallback()
  197. {
  198. if (! JUCE_NAMESPACE::Component::isMouseButtonDownAnywhere())
  199. {
  200. stopTimer();
  201. // Send a token to the host to tell it about the resize
  202. SSetProcessWindowResizeToken token (process->fRootNameId, process->fRootNameId);
  203. FicSDSDispatchToken (&token);
  204. }
  205. }
  206. void attachToWindow (GrafPtr port)
  207. {
  208. if (port != 0)
  209. {
  210. updateSize();
  211. #if JUCE_WINDOWS
  212. void* const hostWindow = (void*) ASI_GethWnd ((WindowPtr) port);
  213. #else
  214. void* const hostWindow = (void*) GetWindowFromPort (port);
  215. #endif
  216. wrapper = 0;
  217. wrapper = new EditorCompWrapper (hostWindow, editorComp, this);
  218. process->touchAllParameters();
  219. }
  220. else
  221. {
  222. deleteEditorComp();
  223. }
  224. }
  225. void DrawContents (Rect*)
  226. {
  227. #if JUCE_WINDOWS
  228. if (wrapper != 0)
  229. {
  230. ComponentPeer* const peer = wrapper->getPeer();
  231. if (peer != 0)
  232. {
  233. // (seems to be required in PT6.4, but not in 7.x)
  234. peer->repaint (wrapper->getLocalBounds());
  235. }
  236. }
  237. #endif
  238. }
  239. void DrawBackground (Rect*) {}
  240. //==============================================================================
  241. private:
  242. AudioProcessor* const filter;
  243. JucePlugInProcess* const process;
  244. ScopedPointer<JUCE_NAMESPACE::Component> wrapper;
  245. ScopedPointer<AudioProcessorEditor> editorComp;
  246. void deleteEditorComp()
  247. {
  248. if (editorComp != 0 || wrapper != 0)
  249. {
  250. #if JUCE_MAC
  251. const ScopedAutoReleasePool pool;
  252. #endif
  253. PopupMenu::dismissAllActiveMenus();
  254. JUCE_NAMESPACE::Component* const modalComponent = JUCE_NAMESPACE::Component::getCurrentlyModalComponent();
  255. if (modalComponent != 0)
  256. modalComponent->exitModalState (0);
  257. filter->editorBeingDeleted (editorComp);
  258. editorComp = 0;
  259. wrapper = 0;
  260. }
  261. }
  262. //==============================================================================
  263. // A component to hold the AudioProcessorEditor, and cope with some housekeeping
  264. // chores when it changes or repaints.
  265. class EditorCompWrapper : public JUCE_NAMESPACE::Component
  266. #if ! JUCE_MAC
  267. , public FocusChangeListener
  268. #endif
  269. {
  270. public:
  271. EditorCompWrapper (void* const hostWindow_,
  272. Component* const editorComp,
  273. JuceCustomUIView* const owner_)
  274. : hostWindow (hostWindow_),
  275. owner (owner_),
  276. titleW (0),
  277. titleH (0)
  278. {
  279. #if ! JucePlugin_EditorRequiresKeyboardFocus
  280. setMouseClickGrabsKeyboardFocus (false);
  281. setWantsKeyboardFocus (false);
  282. #endif
  283. setOpaque (true);
  284. setBroughtToFrontOnMouseClick (true);
  285. setBounds (editorComp->getBounds());
  286. editorComp->setTopLeftPosition (0, 0);
  287. addAndMakeVisible (editorComp);
  288. #if JUCE_WINDOWS
  289. attachSubWindow (hostWindow, titleW, titleH, this);
  290. #else
  291. nsWindow = attachSubWindow (hostWindow, this);
  292. #endif
  293. setVisible (true);
  294. #if JUCE_WINDOWS && ! JucePlugin_EditorRequiresKeyboardFocus
  295. Desktop::getInstance().addFocusChangeListener (this);
  296. #endif
  297. }
  298. ~EditorCompWrapper()
  299. {
  300. #if JUCE_WINDOWS && ! JucePlugin_EditorRequiresKeyboardFocus
  301. Desktop::getInstance().removeFocusChangeListener (this);
  302. #endif
  303. #if JUCE_MAC
  304. removeSubWindow (nsWindow, this);
  305. #endif
  306. }
  307. void paint (Graphics&)
  308. {
  309. }
  310. void resized()
  311. {
  312. JUCE_NAMESPACE::Component* const c = getChildComponent (0);
  313. if (c != 0)
  314. c->setBounds (0, 0, getWidth(), getHeight());
  315. repaint();
  316. }
  317. #if JUCE_WINDOWS
  318. void globalFocusChanged (JUCE_NAMESPACE::Component*)
  319. {
  320. #if ! JucePlugin_EditorRequiresKeyboardFocus
  321. if (hasKeyboardFocus (true))
  322. passFocusToHostWindow (hostWindow);
  323. #endif
  324. }
  325. #endif
  326. void childBoundsChanged (JUCE_NAMESPACE::Component* child)
  327. {
  328. setSize (child->getWidth(), child->getHeight());
  329. child->setTopLeftPosition (0, 0);
  330. #if JUCE_WINDOWS
  331. resizeHostWindow (hostWindow, titleW, titleH, this);
  332. #endif
  333. owner->updateSize();
  334. }
  335. void userTriedToCloseWindow()
  336. {
  337. }
  338. #if JUCE_MAC && JucePlugin_EditorRequiresKeyboardFocus
  339. bool keyPressed (const KeyPress& kp)
  340. {
  341. owner->updateSize();
  342. forwardCurrentKeyEventToHostWindow();
  343. return true;
  344. }
  345. #endif
  346. //==============================================================================
  347. juce_UseDebuggingNewOperator
  348. private:
  349. void* const hostWindow;
  350. void* nsWindow;
  351. JuceCustomUIView* const owner;
  352. int titleW, titleH;
  353. };
  354. };
  355. JuceCustomUIView* getView() const
  356. {
  357. return dynamic_cast <JuceCustomUIView*> (fOurPlugInView);
  358. }
  359. void GetViewRect (Rect* size)
  360. {
  361. if (getView() != 0)
  362. getView()->updateSize();
  363. CEffectProcessRTAS::GetViewRect (size);
  364. }
  365. CPlugInView* CreateCPlugInView()
  366. {
  367. return new JuceCustomUIView (juceFilter, this);
  368. }
  369. void SetViewPort (GrafPtr port)
  370. {
  371. CEffectProcessRTAS::SetViewPort (port);
  372. if (getView() != 0)
  373. getView()->attachToWindow (port);
  374. }
  375. //==============================================================================
  376. protected:
  377. ComponentResult GetDelaySamplesLong (long* aNumSamples)
  378. {
  379. if (aNumSamples != 0)
  380. *aNumSamples = juceFilter != 0 ? juceFilter->getLatencySamples() : 0;
  381. return noErr;
  382. }
  383. //==============================================================================
  384. void EffectInit()
  385. {
  386. SFicPlugInStemFormats stems;
  387. GetProcessType()->GetStemFormats (&stems);
  388. juceFilter->setPlayConfigDetails (fNumInputs, fNumOutputs,
  389. juceFilter->getSampleRate(), juceFilter->getBlockSize());
  390. AddControl (new CPluginControl_OnOff ('bypa', "Master Bypass\nMastrByp\nMByp\nByp", false, true));
  391. DefineMasterBypassControlIndex (bypassControlIndex);
  392. for (int i = 0; i < juceFilter->getNumParameters(); ++i)
  393. AddControl (new JucePluginControl (juceFilter, i));
  394. // we need to do this midi log-in to get timecode, regardless of whether
  395. // the plugin actually uses midi...
  396. if (MIDILogIn() == noErr)
  397. {
  398. #if JucePlugin_WantsMidiInput
  399. CEffectType* const type = dynamic_cast <CEffectType*> (this->GetProcessType());
  400. if (type != 0)
  401. {
  402. char nodeName [64];
  403. type->GetProcessTypeName (63, nodeName);
  404. p2cstrcpy (nodeName, reinterpret_cast <unsigned char*> (nodeName));
  405. midiBufferNode = new CEffectMIDIOtherBufferedNode (&mMIDIWorld,
  406. 8192,
  407. eLocalNode,
  408. nodeName,
  409. midiBuffer);
  410. midiBufferNode->Initialize (1, true);
  411. }
  412. #endif
  413. }
  414. midiTransport = new CEffectMIDITransport (&mMIDIWorld);
  415. juceFilter->setPlayHead (this);
  416. juceFilter->addListener (this);
  417. midiEvents.ensureSize (2048);
  418. }
  419. void handleAsyncUpdate()
  420. {
  421. if (! prepared)
  422. {
  423. sampleRate = gProcessGroup->GetSampleRate();
  424. jassert (sampleRate > 0);
  425. channels.calloc (jmax (juceFilter->getNumInputChannels(),
  426. juceFilter->getNumOutputChannels()));
  427. juceFilter->setPlayConfigDetails (fNumInputs, fNumOutputs,
  428. sampleRate, mRTGlobals->mHWBufferSizeInSamples);
  429. juceFilter->prepareToPlay (sampleRate,
  430. mRTGlobals->mHWBufferSizeInSamples);
  431. prepared = true;
  432. }
  433. }
  434. void RenderAudio (float** inputs, float** outputs, long numSamples)
  435. {
  436. if (! prepared)
  437. {
  438. asyncUpdater->triggerAsyncUpdate();
  439. bypassBuffers (inputs, outputs, numSamples);
  440. return;
  441. }
  442. if (mBypassed)
  443. {
  444. bypassBuffers (inputs, outputs, numSamples);
  445. return;
  446. }
  447. #if JucePlugin_WantsMidiInput
  448. midiEvents.clear();
  449. const Cmn_UInt32 bufferSize = mRTGlobals->mHWBufferSizeInSamples;
  450. if (midiBufferNode != 0)
  451. {
  452. if (midiBufferNode->GetAdvanceScheduleTime() != bufferSize)
  453. midiBufferNode->SetAdvanceScheduleTime (bufferSize);
  454. if (midiBufferNode->FillMIDIBuffer (mRTGlobals->mRunningTime, numSamples) == noErr)
  455. {
  456. jassert (midiBufferNode->GetBufferPtr() != 0);
  457. const int numMidiEvents = midiBufferNode->GetBufferSize();
  458. for (int i = 0; i < numMidiEvents; ++i)
  459. {
  460. const DirectMidiPacket& m = midiBuffer[i];
  461. jassert ((int) m.mTimestamp < numSamples);
  462. midiEvents.addEvent (m.mData, m.mLength,
  463. jlimit (0, (int) numSamples - 1, (int) m.mTimestamp));
  464. }
  465. }
  466. }
  467. #endif
  468. #if defined (JUCE_DEBUG) || JUCE_LOG_ASSERTIONS
  469. const int numMidiEventsComingIn = midiEvents.getNumEvents();
  470. (void) numMidiEventsComingIn;
  471. #endif
  472. {
  473. const ScopedLock sl (juceFilter->getCallbackLock());
  474. const int numIn = juceFilter->getNumInputChannels();
  475. const int numOut = juceFilter->getNumOutputChannels();
  476. const int totalChans = jmax (numIn, numOut);
  477. if (juceFilter->isSuspended())
  478. {
  479. for (int i = 0; i < numOut; ++i)
  480. zeromem (outputs [i], sizeof (float) * numSamples);
  481. }
  482. else
  483. {
  484. {
  485. int i;
  486. for (i = 0; i < numOut; ++i)
  487. {
  488. channels[i] = outputs [i];
  489. if (i < numIn && inputs != outputs)
  490. memcpy (outputs [i], inputs[i], sizeof (float) * numSamples);
  491. }
  492. for (; i < numIn; ++i)
  493. channels [i] = inputs [i];
  494. }
  495. AudioSampleBuffer chans (channels, totalChans, numSamples);
  496. juceFilter->processBlock (chans, midiEvents);
  497. }
  498. }
  499. if (! midiEvents.isEmpty())
  500. {
  501. #if JucePlugin_ProducesMidiOutput
  502. const JUCE_NAMESPACE::uint8* midiEventData;
  503. int midiEventSize, midiEventPosition;
  504. MidiBuffer::Iterator i (midiEvents);
  505. while (i.getNextEvent (midiEventData, midiEventSize, midiEventPosition))
  506. {
  507. // jassert (midiEventPosition >= 0 && midiEventPosition < (int) numSamples);
  508. //xxx
  509. }
  510. #else
  511. // if your plugin creates midi messages, you'll need to set
  512. // the JucePlugin_ProducesMidiOutput macro to 1 in your
  513. // JucePluginCharacteristics.h file
  514. jassert (midiEvents.getNumEvents() <= numMidiEventsComingIn);
  515. #endif
  516. midiEvents.clear();
  517. }
  518. }
  519. //==============================================================================
  520. ComponentResult GetChunkSize (OSType chunkID, long* size)
  521. {
  522. if (chunkID == juceChunkType)
  523. {
  524. tempFilterData.setSize (0);
  525. juceFilter->getStateInformation (tempFilterData);
  526. *size = sizeof (SFicPlugInChunkHeader) + tempFilterData.getSize();
  527. return noErr;
  528. }
  529. return CEffectProcessMIDI::GetChunkSize (chunkID, size);
  530. }
  531. ComponentResult GetChunk (OSType chunkID, SFicPlugInChunk* chunk)
  532. {
  533. if (chunkID == juceChunkType)
  534. {
  535. if (tempFilterData.getSize() == 0)
  536. juceFilter->getStateInformation (tempFilterData);
  537. chunk->fSize = sizeof (SFicPlugInChunkHeader) + tempFilterData.getSize();
  538. tempFilterData.copyTo ((void*) chunk->fData, 0, tempFilterData.getSize());
  539. tempFilterData.setSize (0);
  540. return noErr;
  541. }
  542. return CEffectProcessMIDI::GetChunk (chunkID, chunk);
  543. }
  544. ComponentResult SetChunk (OSType chunkID, SFicPlugInChunk* chunk)
  545. {
  546. if (chunkID == juceChunkType)
  547. {
  548. tempFilterData.setSize (0);
  549. if (chunk->fSize - sizeof (SFicPlugInChunkHeader) > 0)
  550. {
  551. juceFilter->setStateInformation ((void*) chunk->fData,
  552. chunk->fSize - sizeof (SFicPlugInChunkHeader));
  553. }
  554. return noErr;
  555. }
  556. return CEffectProcessMIDI::SetChunk (chunkID, chunk);
  557. }
  558. //==============================================================================
  559. ComponentResult UpdateControlValue (long controlIndex, long value)
  560. {
  561. if (controlIndex != bypassControlIndex)
  562. juceFilter->setParameter (controlIndex - 2, longToFloat (value));
  563. else
  564. mBypassed = (value > 0);
  565. return CProcess::UpdateControlValue (controlIndex, value);
  566. }
  567. //==============================================================================
  568. bool getCurrentPosition (AudioPlayHead::CurrentPositionInfo& info)
  569. {
  570. // this method can only be called while the plugin is running
  571. jassert (prepared);
  572. Cmn_Float64 bpm = 120.0;
  573. Cmn_Int32 num = 4, denom = 4;
  574. Cmn_Int64 ticks = 0;
  575. Cmn_Bool isPlaying = false;
  576. if (midiTransport != 0)
  577. {
  578. midiTransport->GetCurrentTempo (&bpm);
  579. midiTransport->IsTransportPlaying (&isPlaying);
  580. midiTransport->GetCurrentMeter (&num, &denom);
  581. midiTransport->GetCurrentTickPosition (&ticks);
  582. }
  583. info.bpm = bpm;
  584. info.timeSigNumerator = num;
  585. info.timeSigDenominator = denom;
  586. info.isPlaying = isPlaying;
  587. info.isRecording = false;
  588. info.ppqPosition = ticks / 960000.0;
  589. info.ppqPositionOfLastBarStart = 0; //xxx no idea how to get this correctly..
  590. // xxx incorrect if there are tempo changes, but there's no
  591. // other way of getting this info..
  592. info.timeInSeconds = ticks * (60.0 / 960000.0) / bpm;
  593. double framesPerSec = 24.0;
  594. switch (fTimeCodeInfo.mFrameRate)
  595. {
  596. case ficFrameRate_24Frame:
  597. info.frameRate = AudioPlayHead::fps24;
  598. break;
  599. case ficFrameRate_25Frame:
  600. info.frameRate = AudioPlayHead::fps25;
  601. framesPerSec = 25.0;
  602. break;
  603. case ficFrameRate_2997NonDrop:
  604. info.frameRate = AudioPlayHead::fps2997;
  605. framesPerSec = 29.97002997;
  606. break;
  607. case ficFrameRate_2997DropFrame:
  608. info.frameRate = AudioPlayHead::fps2997drop;
  609. framesPerSec = 29.97002997;
  610. break;
  611. case ficFrameRate_30NonDrop:
  612. info.frameRate = AudioPlayHead::fps30;
  613. framesPerSec = 30.0;
  614. break;
  615. case ficFrameRate_30DropFrame:
  616. info.frameRate = AudioPlayHead::fps30drop;
  617. framesPerSec = 30.0;
  618. break;
  619. case ficFrameRate_23976:
  620. // xxx not strictly true..
  621. info.frameRate = AudioPlayHead::fps24;
  622. framesPerSec = 23.976;
  623. break;
  624. default:
  625. info.frameRate = AudioPlayHead::fpsUnknown;
  626. break;
  627. }
  628. info.editOriginTime = fTimeCodeInfo.mFrameOffset / framesPerSec;
  629. return true;
  630. }
  631. void audioProcessorParameterChanged (AudioProcessor*, int index, float newValue)
  632. {
  633. SetControlValue (index + 2, floatToLong (newValue));
  634. }
  635. void audioProcessorParameterChangeGestureBegin (AudioProcessor*, int index)
  636. {
  637. TouchControl (index + 2);
  638. }
  639. void audioProcessorParameterChangeGestureEnd (AudioProcessor*, int index)
  640. {
  641. ReleaseControl (index + 2);
  642. }
  643. void audioProcessorChanged (AudioProcessor*)
  644. {
  645. // xxx is there an RTAS equivalent?
  646. }
  647. void touchAllParameters()
  648. {
  649. for (int i = 0; i < juceFilter->getNumParameters(); ++i)
  650. {
  651. audioProcessorParameterChangeGestureBegin (0, i);
  652. audioProcessorParameterChanged (0, i, juceFilter->getParameter (i));
  653. audioProcessorParameterChangeGestureEnd (0, i);
  654. }
  655. }
  656. public:
  657. // Need to use an intermediate class here rather than inheriting from AsyncUpdater, so that it can
  658. // be deleted before shutting down juce in our destructor.
  659. class InternalAsyncUpdater : public AsyncUpdater
  660. {
  661. public:
  662. InternalAsyncUpdater (JucePlugInProcess& owner_) : owner (owner_) {}
  663. void handleAsyncUpdate() { owner.handleAsyncUpdate(); }
  664. private:
  665. JucePlugInProcess& owner;
  666. };
  667. //==============================================================================
  668. private:
  669. AudioProcessor* juceFilter;
  670. MidiBuffer midiEvents;
  671. ScopedPointer<CEffectMIDIOtherBufferedNode> midiBufferNode;
  672. ScopedPointer<CEffectMIDITransport> midiTransport;
  673. DirectMidiPacket midiBuffer [midiBufferSize];
  674. ScopedPointer<InternalAsyncUpdater> asyncUpdater;
  675. JUCE_NAMESPACE::MemoryBlock tempFilterData;
  676. HeapBlock <float*> channels;
  677. bool prepared;
  678. double sampleRate;
  679. void bypassBuffers (float** const inputs, float** const outputs, const long numSamples) const
  680. {
  681. for (int i = fNumOutputs; --i >= 0;)
  682. {
  683. if (i < fNumInputs)
  684. memcpy (outputs[i], inputs[i], numSamples * sizeof (float));
  685. else
  686. zeromem (outputs[i], numSamples * sizeof (float));
  687. }
  688. }
  689. //==============================================================================
  690. class JucePluginControl : public CPluginControl
  691. {
  692. public:
  693. //==============================================================================
  694. JucePluginControl (AudioProcessor* const juceFilter_, const int index_)
  695. : juceFilter (juceFilter_),
  696. index (index_)
  697. {
  698. }
  699. ~JucePluginControl()
  700. {
  701. }
  702. //==============================================================================
  703. OSType GetID() const { return index + 1; }
  704. long GetDefaultValue() const { return floatToLong (0); }
  705. void SetDefaultValue (long) {}
  706. long GetNumSteps() const { return 0xffffffff; }
  707. long ConvertStringToValue (const char* valueString) const
  708. {
  709. return floatToLong (String (valueString).getFloatValue());
  710. }
  711. Cmn_Bool IsKeyValid (long key) const { return true; }
  712. void GetNameOfLength (char* name, int maxLength, OSType inControllerType) const
  713. {
  714. juceFilter->getParameterName (index).copyToCString (name, maxLength);
  715. }
  716. long GetPriority() const { return kFicCooperativeTaskPriority; }
  717. long GetOrientation() const
  718. {
  719. return kDAE_LeftMinRightMax | kDAE_BottomMinTopMax
  720. | kDAE_RotarySingleDotMode | kDAE_RotaryLeftMinRightMax;
  721. }
  722. long GetControlType() const { return kDAE_ContinuousValues; }
  723. void GetValueString (char* valueString, int maxLength, long value) const
  724. {
  725. juceFilter->getParameterText (index).copyToCString (valueString, maxLength);
  726. }
  727. Cmn_Bool IsAutomatable() const
  728. {
  729. return juceFilter->isParameterAutomatable (index);
  730. }
  731. private:
  732. //==============================================================================
  733. AudioProcessor* const juceFilter;
  734. const int index;
  735. JucePluginControl (const JucePluginControl&);
  736. JucePluginControl& operator= (const JucePluginControl&);
  737. };
  738. };
  739. //==============================================================================
  740. class JucePlugInGroup : public CEffectGroupMIDI
  741. {
  742. public:
  743. //==============================================================================
  744. JucePlugInGroup()
  745. {
  746. DefineManufacturerNamesAndID (JucePlugin_Manufacturer, JucePlugin_RTASManufacturerCode);
  747. DefinePlugInNamesAndVersion (createRTASName().toCString(), JucePlugin_VersionCode);
  748. #ifndef JUCE_DEBUG
  749. AddGestalt (pluginGestalt_IsCacheable);
  750. #endif
  751. }
  752. ~JucePlugInGroup()
  753. {
  754. shutdownJuce_GUI();
  755. shutdownJuce_NonGUI();
  756. }
  757. //==============================================================================
  758. void CreateEffectTypes()
  759. {
  760. const short channelConfigs[][2] = { JucePlugin_PreferredChannelConfigurations };
  761. const int numConfigs = numElementsInArray (channelConfigs);
  762. // You need to actually add some configurations to the JucePlugin_PreferredChannelConfigurations
  763. // value in your JucePluginCharacteristics.h file..
  764. jassert (numConfigs > 0);
  765. for (int i = 0; i < numConfigs; ++i)
  766. {
  767. CEffectType* const type
  768. = new CEffectTypeRTAS ('jcaa' + i,
  769. JucePlugin_RTASProductId,
  770. JucePlugin_RTASCategory);
  771. type->DefineTypeNames (createRTASName().toCString());
  772. type->DefineSampleRateSupport (eSupports48kAnd96kAnd192k);
  773. type->DefineStemFormats (getFormatForChans (channelConfigs [i][0] != 0 ? channelConfigs [i][0] : channelConfigs [i][1]),
  774. getFormatForChans (channelConfigs [i][1] != 0 ? channelConfigs [i][1] : channelConfigs [i][0]));
  775. type->AddGestalt (pluginGestalt_CanBypass);
  776. type->AddGestalt (pluginGestalt_SupportsVariableQuanta);
  777. type->AttachEffectProcessCreator (createNewProcess);
  778. AddEffectType (type);
  779. }
  780. }
  781. void Initialize()
  782. {
  783. CEffectGroupMIDI::Initialize();
  784. }
  785. //==============================================================================
  786. private:
  787. static CEffectProcess* createNewProcess()
  788. {
  789. #if JUCE_WINDOWS
  790. PlatformUtilities::setCurrentModuleInstanceHandle (gThisModule);
  791. #endif
  792. initialiseJuce_GUI();
  793. return new JucePlugInProcess();
  794. }
  795. static const String createRTASName()
  796. {
  797. return String (JucePlugin_Name) + "\n"
  798. + String (JucePlugin_Name).substring (0, 4);
  799. }
  800. static EPlugIn_StemFormat getFormatForChans (const int numChans) throw()
  801. {
  802. switch (numChans)
  803. {
  804. case 0: return ePlugIn_StemFormat_Generic;
  805. case 1: return ePlugIn_StemFormat_Mono;
  806. case 2: return ePlugIn_StemFormat_Stereo;
  807. case 3: return ePlugIn_StemFormat_LCR;
  808. case 4: return ePlugIn_StemFormat_Quad;
  809. case 5: return ePlugIn_StemFormat_5dot0;
  810. case 6: return ePlugIn_StemFormat_5dot1;
  811. case 7: return ePlugIn_StemFormat_6dot1;
  812. case 8: return ePlugIn_StemFormat_7dot1;
  813. default: jassertfalse; break; // hmm - not a valid number of chans for RTAS..
  814. }
  815. return ePlugIn_StemFormat_Generic;
  816. }
  817. };
  818. void initialiseMacRTAS();
  819. CProcessGroupInterface* CProcessGroup::CreateProcessGroup()
  820. {
  821. #if JUCE_MAC
  822. initialiseMacRTAS();
  823. #endif
  824. initialiseJuce_NonGUI();
  825. return new JucePlugInGroup();
  826. }
  827. #endif