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.

1094 lines
43KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. // Your project must contain an AppConfig.h file with your project-specific settings in it,
  18. // and your header search path must make it accessible to the module's files.
  19. #include "AppConfig.h"
  20. #include "../utility/juce_CheckSettingMacros.h"
  21. #if JucePlugin_Build_AAX && (JUCE_INCLUDED_AAX_IN_MM || defined (_WIN32) || defined (_WIN64))
  22. #ifdef _MSC_VER
  23. #include <windows.h>
  24. #else
  25. #include <Cocoa/Cocoa.h>
  26. #endif
  27. #include "../utility/juce_IncludeModuleHeaders.h"
  28. #undef Component
  29. #ifdef __clang__
  30. #pragma clang diagnostic push
  31. #pragma clang diagnostic ignored "-Wnon-virtual-dtor"
  32. #pragma clang diagnostic ignored "-Wsign-conversion"
  33. #endif
  34. #include "AAX_Exports.cpp"
  35. #include "AAX_ICollection.h"
  36. #include "AAX_IComponentDescriptor.h"
  37. #include "AAX_IEffectDescriptor.h"
  38. #include "AAX_IPropertyMap.h"
  39. #include "AAX_CEffectParameters.h"
  40. #include "AAX_Errors.h"
  41. #include "AAX_CBinaryTaperDelegate.h"
  42. #include "AAX_CBinaryDisplayDelegate.h"
  43. #include "AAX_CLinearTaperDelegate.h"
  44. #include "AAX_CNumberDisplayDelegate.h"
  45. #include "AAX_CEffectGUI.h"
  46. #include "AAX_IViewContainer.h"
  47. #include "AAX_ITransport.h"
  48. #include "AAX_IMIDINode.h"
  49. #include "AAX_UtilsNative.h"
  50. #include "AAX_Enums.h"
  51. #ifdef __clang__
  52. #pragma clang diagnostic pop
  53. #endif
  54. #if JUCE_WINDOWS
  55. #ifndef JucePlugin_AAXLibs_path
  56. #error "You need to define the JucePlugin_AAXLibs_path macro. (This is best done via the introjucer)"
  57. #endif
  58. #if JUCE_64BIT
  59. #define JUCE_AAX_LIB "AAXLibrary_x64"
  60. #else
  61. #define JUCE_AAX_LIB "AAXLibrary"
  62. #endif
  63. #if JUCE_DEBUG
  64. #define JUCE_AAX_LIB_PATH "\\Debug\\"
  65. #define JUCE_AAX_LIB_SUFFIX "_D"
  66. #else
  67. #define JUCE_AAX_LIB_PATH "\\Release\\"
  68. #define JUCE_AAX_LIB_SUFFIX ""
  69. #endif
  70. #pragma comment(lib, JucePlugin_AAXLibs_path JUCE_AAX_LIB_PATH JUCE_AAX_LIB JUCE_AAX_LIB_SUFFIX ".lib")
  71. #endif
  72. using juce::Component;
  73. const int32_t juceChunkType = 'juce';
  74. //==============================================================================
  75. struct AAXClasses
  76. {
  77. static void check (AAX_Result result)
  78. {
  79. jassert (result == AAX_SUCCESS); (void) result;
  80. }
  81. static int getParamIndexFromID (AAX_CParamID paramID) noexcept
  82. {
  83. return atoi (paramID);
  84. }
  85. static bool isBypassParam (AAX_CParamID paramID) noexcept
  86. {
  87. return AAX::IsParameterIDEqual (paramID, cDefaultMasterBypassID) != 0;
  88. }
  89. static AAX_EStemFormat getFormatForChans (const int numChans) noexcept
  90. {
  91. switch (numChans)
  92. {
  93. case 0: return AAX_eStemFormat_None;
  94. case 1: return AAX_eStemFormat_Mono;
  95. case 2: return AAX_eStemFormat_Stereo;
  96. case 3: return AAX_eStemFormat_LCR;
  97. case 4: return AAX_eStemFormat_Quad;
  98. case 5: return AAX_eStemFormat_5_0;
  99. case 6: return AAX_eStemFormat_5_1;
  100. case 7: return AAX_eStemFormat_7_0_DTS;
  101. case 8: return AAX_eStemFormat_7_1_DTS;
  102. default: jassertfalse; break;
  103. }
  104. return AAX_eStemFormat_None;
  105. }
  106. static int getNumChannelsForStemFormat (AAX_EStemFormat format) noexcept
  107. {
  108. switch (format)
  109. {
  110. case AAX_eStemFormat_None: return 0;
  111. case AAX_eStemFormat_Mono: return 1;
  112. case AAX_eStemFormat_Stereo: return 2;
  113. case AAX_eStemFormat_LCR: return 3;
  114. case AAX_eStemFormat_LCRS:
  115. case AAX_eStemFormat_Quad: return 4;
  116. case AAX_eStemFormat_5_0: return 5;
  117. case AAX_eStemFormat_5_1:
  118. case AAX_eStemFormat_6_0: return 6;
  119. case AAX_eStemFormat_6_1:
  120. case AAX_eStemFormat_7_0_SDDS:
  121. case AAX_eStemFormat_7_0_DTS: return 7;
  122. case AAX_eStemFormat_7_1_SDDS:
  123. case AAX_eStemFormat_7_1_DTS: return 8;
  124. default: jassertfalse; break;
  125. }
  126. return 0;
  127. }
  128. static const char* getSpeakerArrangementString (AAX_EStemFormat format) noexcept
  129. {
  130. switch (format)
  131. {
  132. case AAX_eStemFormat_Mono: return "M";
  133. case AAX_eStemFormat_Stereo: return "L R";
  134. case AAX_eStemFormat_LCR: return "L C R";
  135. case AAX_eStemFormat_LCRS: return "L C R S";
  136. case AAX_eStemFormat_Quad: return "L R Ls Rs";
  137. case AAX_eStemFormat_5_0: return "L C R Ls Rs";
  138. case AAX_eStemFormat_5_1: return "L C R Ls Rs LFE";
  139. case AAX_eStemFormat_6_0: return "L C R Ls Cs Rs";
  140. case AAX_eStemFormat_6_1: return "L C R Ls Cs Rs LFE";
  141. case AAX_eStemFormat_7_0_SDDS: return "L Lc C Rc R Ls Rs";
  142. case AAX_eStemFormat_7_1_SDDS: return "L Lc C Rc R Ls Rs LFE";
  143. case AAX_eStemFormat_7_0_DTS: return "L C R Lss Rss Lsr Rsr";
  144. case AAX_eStemFormat_7_1_DTS: return "L C R Lss Rss Lsr Rsr LFE";
  145. default: break;
  146. }
  147. return nullptr;
  148. }
  149. static Colour getColourFromHighlightEnum (AAX_EHighlightColor colour) noexcept
  150. {
  151. switch (colour)
  152. {
  153. case AAX_eHighlightColor_Red: return Colours::red;
  154. case AAX_eHighlightColor_Blue: return Colours::blue;
  155. case AAX_eHighlightColor_Green: return Colours::green;
  156. case AAX_eHighlightColor_Yellow: return Colours::yellow;
  157. default: jassertfalse; break;
  158. }
  159. return Colours::black;
  160. }
  161. //==============================================================================
  162. class JuceAAX_Processor;
  163. struct PluginInstanceInfo
  164. {
  165. PluginInstanceInfo (JuceAAX_Processor& p) : parameters (p) {}
  166. JuceAAX_Processor& parameters;
  167. JUCE_DECLARE_NON_COPYABLE (PluginInstanceInfo)
  168. };
  169. //==============================================================================
  170. struct JUCEAlgorithmContext
  171. {
  172. float** inputChannels;
  173. float** outputChannels;
  174. int32_t* bufferSize;
  175. int32_t* bypass;
  176. #if JucePlugin_WantsMidiInput
  177. AAX_IMIDINode* midiNodeIn;
  178. #endif
  179. #if JucePlugin_ProducesMidiOutput
  180. AAX_IMIDINode* midiNodeOut;
  181. #endif
  182. PluginInstanceInfo* pluginInstance;
  183. int32_t* isPrepared;
  184. };
  185. struct JUCEAlgorithmIDs
  186. {
  187. enum
  188. {
  189. inputChannels = AAX_FIELD_INDEX (JUCEAlgorithmContext, inputChannels),
  190. outputChannels = AAX_FIELD_INDEX (JUCEAlgorithmContext, outputChannels),
  191. bufferSize = AAX_FIELD_INDEX (JUCEAlgorithmContext, bufferSize),
  192. bypass = AAX_FIELD_INDEX (JUCEAlgorithmContext, bypass),
  193. #if JucePlugin_WantsMidiInput
  194. midiNodeIn = AAX_FIELD_INDEX (JUCEAlgorithmContext, midiNodeIn),
  195. #endif
  196. #if JucePlugin_ProducesMidiOutput
  197. midiNodeOut = AAX_FIELD_INDEX (JUCEAlgorithmContext, midiNodeOut),
  198. #endif
  199. pluginInstance = AAX_FIELD_INDEX (JUCEAlgorithmContext, pluginInstance),
  200. preparedFlag = AAX_FIELD_INDEX (JUCEAlgorithmContext, isPrepared)
  201. };
  202. };
  203. #if JucePlugin_WantsMidiInput
  204. static AAX_IMIDINode* getMidiNodeIn (const JUCEAlgorithmContext& c) noexcept { return c.midiNodeIn; }
  205. #else
  206. static AAX_IMIDINode* getMidiNodeIn (const JUCEAlgorithmContext&) noexcept { return nullptr; }
  207. #endif
  208. #if JucePlugin_ProducesMidiOutput
  209. AAX_IMIDINode* midiNodeOut;
  210. static AAX_IMIDINode* getMidiNodeOut (const JUCEAlgorithmContext& c) noexcept { return c.midiNodeOut; }
  211. #else
  212. static AAX_IMIDINode* getMidiNodeOut (const JUCEAlgorithmContext&) noexcept { return nullptr; }
  213. #endif
  214. //==============================================================================
  215. class JuceAAX_GUI : public AAX_CEffectGUI
  216. {
  217. public:
  218. JuceAAX_GUI() {}
  219. ~JuceAAX_GUI() { DeleteViewContainer(); }
  220. static AAX_IEffectGUI* AAX_CALLBACK Create() { return new JuceAAX_GUI(); }
  221. void CreateViewContents() override
  222. {
  223. if (component == nullptr)
  224. {
  225. if (JuceAAX_Processor* params = dynamic_cast<JuceAAX_Processor*> (GetEffectParameters()))
  226. component = new ContentWrapperComponent (*this, params->getPluginInstance());
  227. else
  228. jassertfalse;
  229. }
  230. }
  231. void CreateViewContainer() override
  232. {
  233. CreateViewContents();
  234. if (void* nativeViewToAttachTo = GetViewContainerPtr())
  235. {
  236. #if JUCE_MAC
  237. if (GetViewContainerType() == AAX_eViewContainer_Type_NSView)
  238. #else
  239. if (GetViewContainerType() == AAX_eViewContainer_Type_HWND)
  240. #endif
  241. {
  242. component->setVisible (true);
  243. component->addToDesktop (0, nativeViewToAttachTo);
  244. }
  245. }
  246. }
  247. void DeleteViewContainer() override
  248. {
  249. if (component != nullptr)
  250. {
  251. JUCE_AUTORELEASEPOOL
  252. {
  253. component->removeFromDesktop();
  254. component = nullptr;
  255. }
  256. }
  257. }
  258. AAX_Result GetViewSize (AAX_Point* viewSize) const override
  259. {
  260. if (component != nullptr)
  261. {
  262. viewSize->horz = (float) component->getWidth();
  263. viewSize->vert = (float) component->getHeight();
  264. return AAX_SUCCESS;
  265. }
  266. return AAX_ERROR_NULL_OBJECT;
  267. }
  268. AAX_Result ParameterUpdated (AAX_CParamID) override
  269. {
  270. return AAX_SUCCESS;
  271. }
  272. AAX_Result SetControlHighlightInfo (AAX_CParamID paramID, AAX_CBoolean isHighlighted, AAX_EHighlightColor colour) override
  273. {
  274. if (component != nullptr && component->pluginEditor != nullptr)
  275. {
  276. AudioProcessorEditor::ParameterControlHighlightInfo info;
  277. info.parameterIndex = getParamIndexFromID (paramID);
  278. info.isHighlighted = isHighlighted;
  279. info.suggestedColour = getColourFromHighlightEnum (colour);
  280. component->pluginEditor->setControlHighlight (info);
  281. return AAX_SUCCESS;
  282. }
  283. return AAX_ERROR_NULL_OBJECT;
  284. }
  285. private:
  286. struct ContentWrapperComponent : public juce::Component
  287. {
  288. ContentWrapperComponent (JuceAAX_GUI& gui, AudioProcessor& plugin)
  289. : owner (gui)
  290. {
  291. setOpaque (true);
  292. setBroughtToFrontOnMouseClick (true);
  293. addAndMakeVisible (pluginEditor = plugin.createEditorIfNeeded());
  294. if (pluginEditor != nullptr)
  295. {
  296. setBounds (pluginEditor->getLocalBounds());
  297. pluginEditor->addMouseListener (this, true);
  298. }
  299. }
  300. ~ContentWrapperComponent()
  301. {
  302. if (pluginEditor != nullptr)
  303. {
  304. PopupMenu::dismissAllActiveMenus();
  305. pluginEditor->removeMouseListener (this);
  306. pluginEditor->processor.editorBeingDeleted (pluginEditor);
  307. }
  308. }
  309. void paint (Graphics& g) override
  310. {
  311. g.fillAll (Colours::black);
  312. }
  313. template <typename MethodType>
  314. void callMouseMethod (const MouseEvent& e, MethodType method)
  315. {
  316. if (AAX_IViewContainer* vc = owner.GetViewContainer())
  317. {
  318. const int parameterIndex = pluginEditor->getControlParameterIndex (*e.eventComponent);
  319. if (parameterIndex >= 0)
  320. {
  321. uint32_t mods = 0;
  322. vc->GetModifiers (&mods);
  323. (vc->*method) (IndexAsParamID (parameterIndex), mods);
  324. }
  325. }
  326. }
  327. void mouseDown (const MouseEvent& e) override { callMouseMethod (e, &AAX_IViewContainer::HandleParameterMouseDown); }
  328. void mouseUp (const MouseEvent& e) override { callMouseMethod (e, &AAX_IViewContainer::HandleParameterMouseUp); }
  329. void mouseDrag (const MouseEvent& e) override { callMouseMethod (e, &AAX_IViewContainer::HandleParameterMouseDrag); }
  330. void childBoundsChanged (Component*) override
  331. {
  332. if (pluginEditor != nullptr)
  333. {
  334. const int w = pluginEditor->getWidth();
  335. const int h = pluginEditor->getHeight();
  336. setSize (w, h);
  337. AAX_Point newSize ((float) h, (float) w);
  338. owner.GetViewContainer()->SetViewSize (newSize);
  339. }
  340. }
  341. ScopedPointer<AudioProcessorEditor> pluginEditor;
  342. JuceAAX_GUI& owner;
  343. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ContentWrapperComponent)
  344. };
  345. ScopedPointer<ContentWrapperComponent> component;
  346. ScopedJuceInitialiser_GUI libraryInitialiser;
  347. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (JuceAAX_GUI)
  348. };
  349. //==============================================================================
  350. class JuceAAX_Processor : public AAX_CEffectParameters,
  351. public juce::AudioPlayHead,
  352. public AudioProcessorListener
  353. {
  354. public:
  355. JuceAAX_Processor() : sampleRate (0), lastBufferSize (1024)
  356. {
  357. pluginInstance = createPluginFilterOfType (AudioProcessor::wrapperType_AAX);
  358. pluginInstance->setPlayHead (this);
  359. pluginInstance->addListener (this);
  360. AAX_CEffectParameters::GetNumberOfChunks (&juceChunkIndex);
  361. }
  362. static AAX_CEffectParameters* AAX_CALLBACK Create() { return new JuceAAX_Processor(); }
  363. AAX_Result EffectInit() override
  364. {
  365. check (Controller()->GetSampleRate (&sampleRate));
  366. preparePlugin();
  367. addBypassParameter();
  368. addAudioProcessorParameters();
  369. return AAX_SUCCESS;
  370. }
  371. AAX_Result GetNumberOfChunks (int32_t* numChunks) const override
  372. {
  373. // The juceChunk is the last chunk.
  374. *numChunks = juceChunkIndex + 1;
  375. return AAX_SUCCESS;
  376. }
  377. AAX_Result GetChunkIDFromIndex (int32_t index, AAX_CTypeID* chunkID) const override
  378. {
  379. if (index != juceChunkIndex)
  380. return AAX_CEffectParameters::GetChunkIDFromIndex (index, chunkID);
  381. *chunkID = juceChunkType;
  382. return AAX_SUCCESS;
  383. }
  384. AAX_Result GetChunkSize (AAX_CTypeID chunkID, uint32_t* oSize) const override
  385. {
  386. if (chunkID != juceChunkType)
  387. return AAX_CEffectParameters::GetChunkSize (chunkID, oSize);
  388. tempFilterData.reset();
  389. pluginInstance->getStateInformation (tempFilterData);
  390. *oSize = (uint32_t) tempFilterData.getSize();
  391. return AAX_SUCCESS;
  392. }
  393. AAX_Result GetChunk (AAX_CTypeID chunkID, AAX_SPlugInChunk* oChunk) const override
  394. {
  395. if (chunkID != juceChunkType)
  396. return AAX_CEffectParameters::GetChunk (chunkID, oChunk);
  397. if (tempFilterData.getSize() == 0)
  398. pluginInstance->getStateInformation (tempFilterData);
  399. oChunk->fSize = (int32_t) tempFilterData.getSize();
  400. tempFilterData.copyTo (oChunk->fData, 0, tempFilterData.getSize());
  401. tempFilterData.reset();
  402. return AAX_SUCCESS;
  403. }
  404. AAX_Result SetChunk (AAX_CTypeID chunkID, const AAX_SPlugInChunk* chunk) override
  405. {
  406. if (chunkID != juceChunkType)
  407. return AAX_CEffectParameters::SetChunk (chunkID, chunk);
  408. pluginInstance->setStateInformation ((void*) chunk->fData, chunk->fSize);
  409. // Notify Pro Tools that the parameters were updated.
  410. // Without it a bug happens in these circumstances:
  411. // * A preset is saved with the RTAS version of the plugin (".tfx" preset format).
  412. // * The preset is loaded in PT 10 using the AAX version.
  413. // * The session is then saved, and closed.
  414. // * The saved session is loaded, but acting as if the preset was never loaded.
  415. const int numParameters = pluginInstance->getNumParameters();
  416. for (int i = 0; i < numParameters; ++i)
  417. SetParameterNormalizedValue (IndexAsParamID (i), (double) pluginInstance->getParameter(i));
  418. return AAX_SUCCESS;
  419. }
  420. AAX_Result ResetFieldData (AAX_CFieldIndex fieldIndex, void* data, uint32_t dataSize) const override
  421. {
  422. switch (fieldIndex)
  423. {
  424. case JUCEAlgorithmIDs::pluginInstance:
  425. {
  426. const size_t numObjects = dataSize / sizeof (PluginInstanceInfo);
  427. PluginInstanceInfo* const objects = static_cast<PluginInstanceInfo*> (data);
  428. jassert (numObjects == 1); // not sure how to handle more than one..
  429. for (size_t i = 0; i < numObjects; ++i)
  430. new (objects + i) PluginInstanceInfo (const_cast<JuceAAX_Processor&> (*this));
  431. break;
  432. }
  433. case JUCEAlgorithmIDs::preparedFlag:
  434. {
  435. const_cast<JuceAAX_Processor*>(this)->preparePlugin();
  436. const size_t numObjects = dataSize / sizeof (uint32_t);
  437. uint32_t* const objects = static_cast<uint32_t*> (data);
  438. for (size_t i = 0; i < numObjects; ++i)
  439. new (objects + i) uint32_t (1);
  440. break;
  441. }
  442. }
  443. return AAX_SUCCESS;
  444. }
  445. AAX_Result UpdateParameterNormalizedValue (AAX_CParamID paramID, double value, AAX_EUpdateSource source) override
  446. {
  447. AAX_Result result = AAX_CEffectParameters::UpdateParameterNormalizedValue (paramID, value, source);
  448. if (! isBypassParam (paramID))
  449. pluginInstance->setParameter (getParamIndexFromID (paramID), (float) value);
  450. return result;
  451. }
  452. AAX_Result GetParameterStringFromValue (AAX_CParamID paramID, double value, AAX_IString* result, int32_t maxLen) const override
  453. {
  454. if (isBypassParam (paramID))
  455. result->Set (value == 0 ? "Off"
  456. : (maxLen >= 8 ? "Bypassed" : "Byp"));
  457. else
  458. result->Set (pluginInstance->getParameterText (getParamIndexFromID (paramID), maxLen).toRawUTF8());
  459. return AAX_SUCCESS;
  460. }
  461. AAX_Result GetParameterNumberofSteps (AAX_CParamID paramID, int32_t* result) const
  462. {
  463. if (isBypassParam (paramID))
  464. *result = 2;
  465. else
  466. *result = pluginInstance->getParameterNumSteps (getParamIndexFromID (paramID));
  467. return AAX_SUCCESS;
  468. }
  469. AAX_Result GetParameterNormalizedValue (AAX_CParamID paramID, double* result) const override
  470. {
  471. if (isBypassParam (paramID))
  472. return AAX_CEffectParameters::GetParameterNormalizedValue (paramID, result);
  473. *result = pluginInstance->getParameter (getParamIndexFromID (paramID));
  474. return AAX_SUCCESS;
  475. }
  476. AAX_Result SetParameterNormalizedValue (AAX_CParamID paramID, double newValue) override
  477. {
  478. if (isBypassParam (paramID))
  479. return AAX_CEffectParameters::SetParameterNormalizedValue (paramID, newValue);
  480. if (AAX_IParameter* p = const_cast<AAX_IParameter*> (mParameterManager.GetParameterByID (paramID)))
  481. p->SetValueWithFloat ((float) newValue);
  482. pluginInstance->setParameter (getParamIndexFromID (paramID), (float) newValue);
  483. return AAX_SUCCESS;
  484. }
  485. AAX_Result SetParameterNormalizedRelative (AAX_CParamID paramID, double newDeltaValue) override
  486. {
  487. if (isBypassParam (paramID))
  488. return AAX_CEffectParameters::SetParameterNormalizedRelative (paramID, newDeltaValue);
  489. const int paramIndex = getParamIndexFromID (paramID);
  490. const float newValue = pluginInstance->getParameter (paramIndex) + (float) newDeltaValue;
  491. pluginInstance->setParameter (paramIndex, jlimit (0.0f, 1.0f, newValue));
  492. if (AAX_IParameter* p = const_cast<AAX_IParameter*> (mParameterManager.GetParameterByID (paramID)))
  493. p->SetValueWithFloat (newValue);
  494. return AAX_SUCCESS;
  495. }
  496. AAX_Result GetParameterNameOfLength (AAX_CParamID paramID, AAX_IString* result, int32_t maxLen) const override
  497. {
  498. if (isBypassParam (paramID))
  499. result->Set (maxLen >= 13 ? "Master Bypass"
  500. : (maxLen >= 8 ? "Mast Byp"
  501. : (maxLen >= 6 ? "MstByp" : "MByp")));
  502. else
  503. result->Set (pluginInstance->getParameterName (getParamIndexFromID (paramID), maxLen).toRawUTF8());
  504. return AAX_SUCCESS;
  505. }
  506. AAX_Result GetParameterName (AAX_CParamID paramID, AAX_IString* result) const override
  507. {
  508. if (isBypassParam (paramID))
  509. result->Set ("Master Bypass");
  510. else
  511. result->Set (pluginInstance->getParameterName (getParamIndexFromID (paramID), 31).toRawUTF8());
  512. return AAX_SUCCESS;
  513. }
  514. AAX_Result GetParameterDefaultNormalizedValue (AAX_CParamID paramID, double* result) const override
  515. {
  516. if (! isBypassParam (paramID))
  517. {
  518. *result = (double) pluginInstance->getParameterDefaultValue (getParamIndexFromID (paramID));
  519. jassert (*result >= 0 && *result <= 1.0f);
  520. }
  521. return AAX_SUCCESS;
  522. }
  523. AudioProcessor& getPluginInstance() const noexcept { return *pluginInstance; }
  524. bool getCurrentPosition (juce::AudioPlayHead::CurrentPositionInfo& info) override
  525. {
  526. const AAX_ITransport& transport = *Transport();
  527. info.bpm = 0.0;
  528. check (transport.GetCurrentTempo (&info.bpm));
  529. int32_t num = 4, den = 4;
  530. transport.GetCurrentMeter (&num, &den);
  531. info.timeSigNumerator = (int) num;
  532. info.timeSigDenominator = (int) den;
  533. info.timeInSamples = 0;
  534. if (transport.IsTransportPlaying (&info.isPlaying) != AAX_SUCCESS)
  535. info.isPlaying = false;
  536. if (info.isPlaying
  537. || transport.GetTimelineSelectionStartPosition (&info.timeInSamples) != AAX_SUCCESS)
  538. check (transport.GetCurrentNativeSampleLocation (&info.timeInSamples));
  539. info.timeInSeconds = info.timeInSamples / sampleRate;
  540. int64_t ticks = 0;
  541. check (transport.GetCurrentTickPosition (&ticks));
  542. info.ppqPosition = ticks / 960000.0;
  543. info.isLooping = false;
  544. int64_t loopStartTick = 0, loopEndTick = 0;
  545. check (transport.GetCurrentLoopPosition (&info.isLooping, &loopStartTick, &loopEndTick));
  546. info.ppqLoopStart = loopStartTick / 960000.0;
  547. info.ppqLoopEnd = loopEndTick / 960000.0;
  548. info.editOriginTime = 0;
  549. info.frameRate = AudioPlayHead::fpsUnknown;
  550. AAX_EFrameRate frameRate;
  551. int32_t offset;
  552. if (transport.GetTimeCodeInfo (&frameRate, &offset) == AAX_SUCCESS)
  553. {
  554. double framesPerSec = 24.0;
  555. switch (frameRate)
  556. {
  557. case AAX_eFrameRate_Undeclared: break;
  558. case AAX_eFrameRate_24Frame: info.frameRate = AudioPlayHead::fps24; break;
  559. case AAX_eFrameRate_25Frame: info.frameRate = AudioPlayHead::fps25; framesPerSec = 25.0; break;
  560. case AAX_eFrameRate_2997NonDrop: info.frameRate = AudioPlayHead::fps2997; framesPerSec = 29.97002997; break;
  561. case AAX_eFrameRate_2997DropFrame: info.frameRate = AudioPlayHead::fps2997drop; framesPerSec = 29.97002997; break;
  562. case AAX_eFrameRate_30NonDrop: info.frameRate = AudioPlayHead::fps30; framesPerSec = 30.0; break;
  563. case AAX_eFrameRate_30DropFrame: info.frameRate = AudioPlayHead::fps30drop; framesPerSec = 30.0; break;
  564. case AAX_eFrameRate_23976: info.frameRate = AudioPlayHead::fps24; framesPerSec = 23.976; break;
  565. default: break;
  566. }
  567. info.editOriginTime = offset / framesPerSec;
  568. }
  569. // No way to get these: (?)
  570. info.isRecording = false;
  571. info.ppqPositionOfLastBarStart = 0;
  572. return true;
  573. }
  574. void audioProcessorParameterChanged (AudioProcessor* /*processor*/, int parameterIndex, float newValue) override
  575. {
  576. SetParameterNormalizedValue (IndexAsParamID (parameterIndex), (double) newValue);
  577. }
  578. void audioProcessorChanged (AudioProcessor* processor) override
  579. {
  580. ++mNumPlugInChanges;
  581. check (Controller()->SetSignalLatency (processor->getLatencySamples()));
  582. }
  583. void audioProcessorParameterChangeGestureBegin (AudioProcessor* /*processor*/, int parameterIndex) override
  584. {
  585. TouchParameter (IndexAsParamID (parameterIndex));
  586. }
  587. void audioProcessorParameterChangeGestureEnd (AudioProcessor* /*processor*/, int parameterIndex) override
  588. {
  589. ReleaseParameter (IndexAsParamID (parameterIndex));
  590. }
  591. AAX_Result NotificationReceived (AAX_CTypeID type, const void* data, uint32_t size) override
  592. {
  593. if (type == AAX_eNotificationEvent_EnteringOfflineMode) pluginInstance->setNonRealtime (true);
  594. if (type == AAX_eNotificationEvent_ExitingOfflineMode) pluginInstance->setNonRealtime (false);
  595. return AAX_CEffectParameters::NotificationReceived (type, data, size);
  596. }
  597. void process (const float* const* inputs, float* const* outputs, const int bufferSize,
  598. const bool bypass, AAX_IMIDINode* midiNodeIn, AAX_IMIDINode* midiNodesOut)
  599. {
  600. const int numIns = pluginInstance->getNumInputChannels();
  601. const int numOuts = pluginInstance->getNumOutputChannels();
  602. if (numOuts >= numIns)
  603. {
  604. for (int i = 0; i < numIns; ++i)
  605. memcpy (outputs[i], inputs[i], (size_t) bufferSize * sizeof (float));
  606. process (outputs, numOuts, bufferSize, bypass, midiNodeIn, midiNodesOut);
  607. }
  608. else
  609. {
  610. if (channelList.size() <= numIns)
  611. channelList.insertMultiple (-1, nullptr, 1 + numIns - channelList.size());
  612. float** channels = channelList.getRawDataPointer();
  613. for (int i = 0; i < numOuts; ++i)
  614. {
  615. memcpy (outputs[i], inputs[i], (size_t) bufferSize * sizeof (float));
  616. channels[i] = outputs[i];
  617. }
  618. for (int i = numOuts; i < numIns; ++i)
  619. channels[i] = const_cast<float*> (inputs[i]);
  620. process (channels, numIns, bufferSize, bypass, midiNodeIn, midiNodesOut);
  621. }
  622. }
  623. private:
  624. void process (float* const* channels, const int numChans, const int bufferSize,
  625. const bool bypass, AAX_IMIDINode* midiNodeIn, AAX_IMIDINode* midiNodesOut)
  626. {
  627. AudioSampleBuffer buffer (channels, numChans, bufferSize);
  628. midiBuffer.clear();
  629. (void) midiNodeIn;
  630. (void) midiNodesOut;
  631. #if JucePlugin_WantsMidiInput
  632. {
  633. AAX_CMidiStream* const midiStream = midiNodeIn->GetNodeBuffer();
  634. const uint32_t numMidiEvents = midiStream->mBufferSize;
  635. for (uint32_t i = 0; i < numMidiEvents; ++i)
  636. {
  637. const AAX_CMidiPacket& m = midiStream->mBuffer[i];
  638. jassert ((int) m.mTimestamp < bufferSize);
  639. midiBuffer.addEvent (m.mData, (int) m.mLength,
  640. jlimit (0, (int) bufferSize - 1, (int) m.mTimestamp));
  641. }
  642. }
  643. #endif
  644. {
  645. if (lastBufferSize != bufferSize)
  646. {
  647. lastBufferSize = bufferSize;
  648. pluginInstance->setPlayConfigDetails (pluginInstance->getNumInputChannels(),
  649. pluginInstance->getNumOutputChannels(),
  650. sampleRate, bufferSize);
  651. pluginInstance->prepareToPlay (sampleRate, bufferSize);
  652. }
  653. const ScopedLock sl (pluginInstance->getCallbackLock());
  654. if (bypass)
  655. pluginInstance->processBlockBypassed (buffer, midiBuffer);
  656. else
  657. pluginInstance->processBlock (buffer, midiBuffer);
  658. }
  659. #if JucePlugin_ProducesMidiOutput
  660. {
  661. const juce::uint8* midiEventData;
  662. int midiEventSize, midiEventPosition;
  663. MidiBuffer::Iterator i (midiBuffer);
  664. AAX_CMidiPacket packet;
  665. packet.mIsImmediate = false;
  666. while (i.getNextEvent (midiEventData, midiEventSize, midiEventPosition))
  667. {
  668. jassert (isPositiveAndBelow (midiEventPosition, bufferSize));
  669. if (midiEventSize <= 4)
  670. {
  671. packet.mTimestamp = (uint32_t) midiEventPosition;
  672. packet.mLength = (uint32_t) midiEventSize;
  673. memcpy (packet.mData, midiEventData, (size_t) midiEventSize);
  674. check (midiNodesOut->PostMIDIPacket (&packet));
  675. }
  676. }
  677. }
  678. #else
  679. (void) midiNodesOut;
  680. #endif
  681. }
  682. void addBypassParameter()
  683. {
  684. AAX_IParameter* masterBypass = new AAX_CParameter<bool> (cDefaultMasterBypassID,
  685. AAX_CString ("Master Bypass"),
  686. false,
  687. AAX_CBinaryTaperDelegate<bool>(),
  688. AAX_CBinaryDisplayDelegate<bool> ("bypass", "on"),
  689. true);
  690. masterBypass->SetNumberOfSteps (2);
  691. masterBypass->SetType (AAX_eParameterType_Discrete);
  692. mParameterManager.AddParameter (masterBypass);
  693. mPacketDispatcher.RegisterPacket (cDefaultMasterBypassID, JUCEAlgorithmIDs::bypass);
  694. }
  695. void addAudioProcessorParameters()
  696. {
  697. AudioProcessor& audioProcessor = getPluginInstance();
  698. const int numParameters = audioProcessor.getNumParameters();
  699. for (int parameterIndex = 0; parameterIndex < numParameters; ++parameterIndex)
  700. {
  701. AAX_IParameter* parameter
  702. = new AAX_CParameter<float> (IndexAsParamID (parameterIndex),
  703. audioProcessor.getParameterName (parameterIndex, 31).toRawUTF8(),
  704. audioProcessor.getParameterDefaultValue (parameterIndex),
  705. AAX_CLinearTaperDelegate<float, 0>(),
  706. AAX_CNumberDisplayDelegate<float, 3>(),
  707. audioProcessor.isParameterAutomatable (parameterIndex));
  708. parameter->AddShortenedName (audioProcessor.getParameterName (parameterIndex, 4).toRawUTF8());
  709. const int parameterNumSteps = audioProcessor.getParameterNumSteps (parameterIndex);
  710. parameter->SetNumberOfSteps ((uint32_t) parameterNumSteps);
  711. parameter->SetType (parameterNumSteps > 1000 ? AAX_eParameterType_Continuous
  712. : AAX_eParameterType_Discrete);
  713. parameter->SetOrientation (audioProcessor.isParameterOrientationInverted (parameterIndex)
  714. ? (AAX_eParameterOrientation_RightMinLeftMax | AAX_eParameterOrientation_TopMinBottomMax
  715. | AAX_eParameterOrientation_RotarySingleDotMode | AAX_eParameterOrientation_RotaryRightMinLeftMax)
  716. : (AAX_eParameterOrientation_LeftMinRightMax | AAX_eParameterOrientation_BottomMinTopMax
  717. | AAX_eParameterOrientation_RotarySingleDotMode | AAX_eParameterOrientation_RotaryLeftMinRightMax));
  718. mParameterManager.AddParameter (parameter);
  719. }
  720. }
  721. void preparePlugin()
  722. {
  723. AAX_EStemFormat inputStemFormat = AAX_eStemFormat_None;
  724. check (Controller()->GetInputStemFormat (&inputStemFormat));
  725. const int numberOfInputChannels = getNumChannelsForStemFormat (inputStemFormat);
  726. AAX_EStemFormat outputStemFormat = AAX_eStemFormat_None;
  727. check (Controller()->GetOutputStemFormat (&outputStemFormat));
  728. const int numberOfOutputChannels = getNumChannelsForStemFormat (outputStemFormat);
  729. AudioProcessor& audioProcessor = getPluginInstance();
  730. audioProcessor.setSpeakerArrangement (getSpeakerArrangementString (inputStemFormat),
  731. getSpeakerArrangementString (outputStemFormat));
  732. audioProcessor.setPlayConfigDetails (numberOfInputChannels, numberOfOutputChannels, sampleRate, lastBufferSize);
  733. audioProcessor.prepareToPlay (sampleRate, lastBufferSize);
  734. check (Controller()->SetSignalLatency (audioProcessor.getLatencySamples()));
  735. }
  736. ScopedJuceInitialiser_GUI libraryInitialiser;
  737. ScopedPointer<AudioProcessor> pluginInstance;
  738. MidiBuffer midiBuffer;
  739. Array<float*> channelList;
  740. int32_t juceChunkIndex;
  741. AAX_CSampleRate sampleRate;
  742. int lastBufferSize;
  743. // tempFilterData is initialized in GetChunkSize.
  744. // To avoid generating it again in GetChunk, we keep it as a member.
  745. mutable juce::MemoryBlock tempFilterData;
  746. JUCE_DECLARE_NON_COPYABLE (JuceAAX_Processor)
  747. };
  748. //==============================================================================
  749. struct IndexAsParamID
  750. {
  751. inline explicit IndexAsParamID (int i) noexcept : index (i) {}
  752. operator AAX_CParamID() noexcept
  753. {
  754. jassert (index >= 0);
  755. char* t = name + sizeof (name);
  756. *--t = 0;
  757. int v = index;
  758. do
  759. {
  760. *--t = (char) ('0' + (v % 10));
  761. v /= 10;
  762. } while (v > 0);
  763. return static_cast<AAX_CParamID> (t);
  764. }
  765. private:
  766. int index;
  767. char name[32];
  768. JUCE_DECLARE_NON_COPYABLE (IndexAsParamID)
  769. };
  770. //==============================================================================
  771. static void AAX_CALLBACK algorithmProcessCallback (JUCEAlgorithmContext* const instancesBegin[],
  772. const void* const instancesEnd)
  773. {
  774. for (JUCEAlgorithmContext* const* iter = instancesBegin; iter < instancesEnd; ++iter)
  775. {
  776. const JUCEAlgorithmContext& i = **iter;
  777. i.pluginInstance->parameters.process (i.inputChannels, i.outputChannels,
  778. *(i.bufferSize), *(i.bypass) != 0,
  779. getMidiNodeIn(i), getMidiNodeOut(i));
  780. }
  781. }
  782. //==============================================================================
  783. static void createDescriptor (AAX_IComponentDescriptor& desc, int channelConfigIndex,
  784. int numInputs, int numOutputs)
  785. {
  786. check (desc.AddAudioIn (JUCEAlgorithmIDs::inputChannels));
  787. check (desc.AddAudioOut (JUCEAlgorithmIDs::outputChannels));
  788. check (desc.AddAudioBufferLength (JUCEAlgorithmIDs::bufferSize));
  789. check (desc.AddDataInPort (JUCEAlgorithmIDs::bypass, sizeof (int32_t)));
  790. #if JucePlugin_WantsMidiInput
  791. check (desc.AddMIDINode (JUCEAlgorithmIDs::midiNodeIn, AAX_eMIDINodeType_LocalInput,
  792. JucePlugin_Name, 0xffff));
  793. #endif
  794. #if JucePlugin_ProducesMidiOutput
  795. check (desc.AddMIDINode (JUCEAlgorithmIDs::midiNodeOut, AAX_eMIDINodeType_LocalOutput,
  796. JucePlugin_Name " Out", 0xffff));
  797. #endif
  798. check (desc.AddPrivateData (JUCEAlgorithmIDs::pluginInstance, sizeof (PluginInstanceInfo)));
  799. // Create a property map
  800. AAX_IPropertyMap* const properties = desc.NewPropertyMap();
  801. jassert (properties != nullptr);
  802. properties->AddProperty (AAX_eProperty_ManufacturerID, JucePlugin_AAXManufacturerCode);
  803. properties->AddProperty (AAX_eProperty_ProductID, JucePlugin_AAXProductId);
  804. #if JucePlugin_AAXDisableBypass
  805. properties->AddProperty (AAX_eProperty_CanBypass, false);
  806. #else
  807. properties->AddProperty (AAX_eProperty_CanBypass, true);
  808. #endif
  809. properties->AddProperty (AAX_eProperty_InputStemFormat, getFormatForChans (numInputs));
  810. properties->AddProperty (AAX_eProperty_OutputStemFormat, getFormatForChans (numOutputs));
  811. // This value needs to match the RTAS wrapper's Type ID, so that
  812. // the host knows that the RTAS/AAX plugins are equivalent.
  813. properties->AddProperty (AAX_eProperty_PlugInID_Native, 'jcaa' + channelConfigIndex);
  814. #if ! JucePlugin_AAXDisableAudioSuite
  815. properties->AddProperty (AAX_eProperty_PlugInID_AudioSuite, 'jyaa' + channelConfigIndex);
  816. #endif
  817. #if JucePlugin_AAXDisableMultiMono
  818. properties->AddProperty (AAX_eProperty_Constraint_MultiMonoSupport, false);
  819. #else
  820. properties->AddProperty (AAX_eProperty_Constraint_MultiMonoSupport, true);
  821. #endif
  822. check (desc.AddProcessProc_Native (algorithmProcessCallback, properties));
  823. }
  824. static void getPlugInDescription (AAX_IEffectDescriptor& descriptor)
  825. {
  826. descriptor.AddName (JucePlugin_Desc);
  827. descriptor.AddName (JucePlugin_Name);
  828. descriptor.AddCategory (JucePlugin_AAXCategory);
  829. #ifdef JucePlugin_AAXPageTableFile
  830. // optional page table setting - define this macro in your AppConfig.h if you
  831. // want to set this value - see Avid documentation for details about its format.
  832. descriptor.AddResourceInfo (AAX_eResourceType_PageTable, JucePlugin_AAXPageTableFile);
  833. #endif
  834. check (descriptor.AddProcPtr ((void*) JuceAAX_GUI::Create, kAAX_ProcPtrID_Create_EffectGUI));
  835. check (descriptor.AddProcPtr ((void*) JuceAAX_Processor::Create, kAAX_ProcPtrID_Create_EffectParameters));
  836. const short channelConfigs[][2] = { JucePlugin_PreferredChannelConfigurations };
  837. const int numConfigs = numElementsInArray (channelConfigs);
  838. // You need to actually add some configurations to the JucePlugin_PreferredChannelConfigurations
  839. // value in your JucePluginCharacteristics.h file..
  840. jassert (numConfigs > 0);
  841. for (int i = 0; i < numConfigs; ++i)
  842. {
  843. if (AAX_IComponentDescriptor* const desc = descriptor.NewComponentDescriptor())
  844. {
  845. const int numIns = channelConfigs [i][0];
  846. const int numOuts = channelConfigs [i][1];
  847. if (numIns <= 8 && numOuts <= 8) // AAX doesn't seem to handle more than 8 chans
  848. {
  849. createDescriptor (*desc, i, numIns, numOuts);
  850. check (descriptor.AddComponent (desc));
  851. }
  852. }
  853. }
  854. }
  855. };
  856. //==============================================================================
  857. AAX_Result JUCE_CDECL GetEffectDescriptions (AAX_ICollection*);
  858. AAX_Result JUCE_CDECL GetEffectDescriptions (AAX_ICollection* collection)
  859. {
  860. ScopedJuceInitialiser_GUI libraryInitialiser;
  861. if (AAX_IEffectDescriptor* const descriptor = collection->NewDescriptor())
  862. {
  863. AAXClasses::getPlugInDescription (*descriptor);
  864. collection->AddEffect (JUCE_STRINGIFY (JucePlugin_AAXIdentifier), descriptor);
  865. collection->SetManufacturerName (JucePlugin_Manufacturer);
  866. collection->AddPackageName (JucePlugin_Desc);
  867. collection->AddPackageName (JucePlugin_Name);
  868. collection->SetPackageVersion (JucePlugin_VersionCode);
  869. return AAX_SUCCESS;
  870. }
  871. return AAX_ERROR_NULL_OBJECT;
  872. }
  873. #endif