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.

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