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.

1016 lines
33KB

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