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.

1021 lines
33KB

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