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.

1468 lines
61KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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. #include "../utility/juce_PluginBusUtilities.h"
  29. #undef Component
  30. #ifdef __clang__
  31. #pragma clang diagnostic push
  32. #pragma clang diagnostic ignored "-Wnon-virtual-dtor"
  33. #pragma clang diagnostic ignored "-Wsign-conversion"
  34. #endif
  35. #ifdef _MSC_VER
  36. #pragma warning (push)
  37. #pragma warning (disable : 4127)
  38. #endif
  39. #include "AAX_Exports.cpp"
  40. #include "AAX_ICollection.h"
  41. #include "AAX_IComponentDescriptor.h"
  42. #include "AAX_IEffectDescriptor.h"
  43. #include "AAX_IPropertyMap.h"
  44. #include "AAX_CEffectParameters.h"
  45. #include "AAX_Errors.h"
  46. #include "AAX_CBinaryTaperDelegate.h"
  47. #include "AAX_CBinaryDisplayDelegate.h"
  48. #include "AAX_CLinearTaperDelegate.h"
  49. #include "AAX_CNumberDisplayDelegate.h"
  50. #include "AAX_CEffectGUI.h"
  51. #include "AAX_IViewContainer.h"
  52. #include "AAX_ITransport.h"
  53. #include "AAX_IMIDINode.h"
  54. #include "AAX_UtilsNative.h"
  55. #include "AAX_Enums.h"
  56. #ifdef _MSC_VER
  57. #pragma warning (pop)
  58. #endif
  59. #ifdef __clang__
  60. #pragma clang diagnostic pop
  61. #endif
  62. #if JUCE_WINDOWS
  63. #ifndef JucePlugin_AAXLibs_path
  64. #error "You need to define the JucePlugin_AAXLibs_path macro. (This is best done via the introjucer)"
  65. #endif
  66. #if JUCE_64BIT
  67. #define JUCE_AAX_LIB "AAXLibrary_x64"
  68. #else
  69. #define JUCE_AAX_LIB "AAXLibrary"
  70. #endif
  71. #if JUCE_DEBUG
  72. #define JUCE_AAX_LIB_PATH "\\Debug\\"
  73. #define JUCE_AAX_LIB_SUFFIX "_D"
  74. #else
  75. #define JUCE_AAX_LIB_PATH "\\Release\\"
  76. #define JUCE_AAX_LIB_SUFFIX ""
  77. #endif
  78. #pragma comment(lib, JucePlugin_AAXLibs_path JUCE_AAX_LIB_PATH JUCE_AAX_LIB JUCE_AAX_LIB_SUFFIX ".lib")
  79. #endif
  80. #undef check
  81. using juce::Component;
  82. const int32_t juceChunkType = 'juce';
  83. //==============================================================================
  84. struct AAXClasses
  85. {
  86. static void check (AAX_Result result)
  87. {
  88. jassert (result == AAX_SUCCESS); (void) result;
  89. }
  90. static int getParamIndexFromID (AAX_CParamID paramID) noexcept
  91. {
  92. return atoi (paramID);
  93. }
  94. static bool isBypassParam (AAX_CParamID paramID) noexcept
  95. {
  96. return AAX::IsParameterIDEqual (paramID, cDefaultMasterBypassID) != 0;
  97. }
  98. static AAX_EStemFormat getFormatForAudioChannelSet (const AudioChannelSet& set, bool ignoreLayout) noexcept
  99. {
  100. // if the plug-in ignores layout, it is ok to convert between formats only by their numchannnels
  101. if (ignoreLayout)
  102. {
  103. switch (set.size())
  104. {
  105. case 0: return AAX_eStemFormat_None;
  106. case 1: return AAX_eStemFormat_Mono;
  107. case 2: return AAX_eStemFormat_Stereo;
  108. case 3: return AAX_eStemFormat_LCR;
  109. case 4: return AAX_eStemFormat_Quad;
  110. case 5: return AAX_eStemFormat_5_0;
  111. case 6: return AAX_eStemFormat_5_1;
  112. case 7: return AAX_eStemFormat_7_0_DTS;
  113. case 8: return AAX_eStemFormat_7_1_DTS;
  114. default:
  115. break;
  116. }
  117. return AAX_eStemFormat_INT32_MAX;
  118. }
  119. if (set == AudioChannelSet::disabled()) return AAX_eStemFormat_None;
  120. if (set == AudioChannelSet::mono()) return AAX_eStemFormat_Mono;
  121. if (set == AudioChannelSet::stereo()) return AAX_eStemFormat_Stereo;
  122. if (set == AudioChannelSet::createLCR()) return AAX_eStemFormat_LCR;
  123. if (set == AudioChannelSet::createLCRS()) return AAX_eStemFormat_LCRS;
  124. if (set == AudioChannelSet::quadraphonic()) return AAX_eStemFormat_Quad;
  125. if (set == AudioChannelSet::create5point0()) return AAX_eStemFormat_5_0;
  126. if (set == AudioChannelSet::create5point1()) return AAX_eStemFormat_5_1;
  127. if (set == AudioChannelSet::create6point0()) return AAX_eStemFormat_6_0;
  128. if (set == AudioChannelSet::create6point1()) return AAX_eStemFormat_6_1;
  129. if (set == AudioChannelSet::create7point0()) return AAX_eStemFormat_7_0_DTS;
  130. if (set == AudioChannelSet::create7point1()) return AAX_eStemFormat_7_1_DTS;
  131. if (set == AudioChannelSet::createFront7point0()) return AAX_eStemFormat_7_0_SDDS;
  132. if (set == AudioChannelSet::createFront7point1()) return AAX_eStemFormat_7_1_SDDS;
  133. return AAX_eStemFormat_INT32_MAX;
  134. }
  135. static AudioChannelSet channelSetFromStemFormat (AAX_EStemFormat format, bool ignoreLayout) noexcept
  136. {
  137. if (! ignoreLayout)
  138. {
  139. switch (format)
  140. {
  141. case AAX_eStemFormat_None: return AudioChannelSet::disabled();
  142. case AAX_eStemFormat_Mono: return AudioChannelSet::mono();
  143. case AAX_eStemFormat_Stereo: return AudioChannelSet::stereo();
  144. case AAX_eStemFormat_LCR: return AudioChannelSet::createLCR();
  145. case AAX_eStemFormat_LCRS: return AudioChannelSet::createLCRS();
  146. case AAX_eStemFormat_Quad: return AudioChannelSet::quadraphonic();
  147. case AAX_eStemFormat_5_0: return AudioChannelSet::create5point0();
  148. case AAX_eStemFormat_5_1: return AudioChannelSet::create5point1();
  149. case AAX_eStemFormat_6_0: return AudioChannelSet::create6point0();
  150. case AAX_eStemFormat_6_1: return AudioChannelSet::create6point1();
  151. case AAX_eStemFormat_7_0_SDDS: return AudioChannelSet::createFront7point0();
  152. case AAX_eStemFormat_7_0_DTS: return AudioChannelSet::create7point0();
  153. case AAX_eStemFormat_7_1_SDDS: return AudioChannelSet::createFront7point1();
  154. case AAX_eStemFormat_7_1_DTS: return AudioChannelSet::create7point1();
  155. default:
  156. break;
  157. }
  158. return AudioChannelSet::disabled();
  159. }
  160. return AudioChannelSet::discreteChannels (jmax (0, static_cast<int> (AAX_STEM_FORMAT_CHANNEL_COUNT (format))));
  161. }
  162. static const char* getSpeakerArrangementString (AAX_EStemFormat format) noexcept
  163. {
  164. switch (format)
  165. {
  166. case AAX_eStemFormat_Mono: return "M";
  167. case AAX_eStemFormat_Stereo: return "L R";
  168. case AAX_eStemFormat_LCR: return "L C R";
  169. case AAX_eStemFormat_LCRS: return "L C R S";
  170. case AAX_eStemFormat_Quad: return "L R Ls Rs";
  171. case AAX_eStemFormat_5_0: return "L C R Ls Rs";
  172. case AAX_eStemFormat_5_1: return "L C R Ls Rs LFE";
  173. case AAX_eStemFormat_6_0: return "L C R Ls Cs Rs";
  174. case AAX_eStemFormat_6_1: return "L C R Ls Cs Rs LFE";
  175. case AAX_eStemFormat_7_0_SDDS: return "L Lc C Rc R Ls Rs";
  176. case AAX_eStemFormat_7_1_SDDS: return "L Lc C Rc R Ls Rs LFE";
  177. case AAX_eStemFormat_7_0_DTS: return "L C R Lss Rss Lsr Rsr";
  178. case AAX_eStemFormat_7_1_DTS: return "L C R Lss Rss Lsr Rsr LFE";
  179. default: break;
  180. }
  181. return nullptr;
  182. }
  183. static Colour getColourFromHighlightEnum (AAX_EHighlightColor colour) noexcept
  184. {
  185. switch (colour)
  186. {
  187. case AAX_eHighlightColor_Red: return Colours::red;
  188. case AAX_eHighlightColor_Blue: return Colours::blue;
  189. case AAX_eHighlightColor_Green: return Colours::green;
  190. case AAX_eHighlightColor_Yellow: return Colours::yellow;
  191. default: jassertfalse; break;
  192. }
  193. return Colours::black;
  194. }
  195. //==============================================================================
  196. class JuceAAX_Processor;
  197. struct PluginInstanceInfo
  198. {
  199. PluginInstanceInfo (JuceAAX_Processor& p) : parameters (p) {}
  200. JuceAAX_Processor& parameters;
  201. JUCE_DECLARE_NON_COPYABLE (PluginInstanceInfo)
  202. };
  203. //==============================================================================
  204. struct JUCEAlgorithmContext
  205. {
  206. float** inputChannels;
  207. float** outputChannels;
  208. int32_t* bufferSize;
  209. int32_t* bypass;
  210. #if JucePlugin_WantsMidiInput || JucePlugin_IsMidiEffect
  211. AAX_IMIDINode* midiNodeIn;
  212. #endif
  213. #if JucePlugin_ProducesMidiOutput || JucePlugin_IsSynth || JucePlugin_IsMidiEffect
  214. AAX_IMIDINode* midiNodeOut;
  215. #endif
  216. PluginInstanceInfo* pluginInstance;
  217. int32_t* isPrepared;
  218. int32_t* sideChainBuffers;
  219. };
  220. struct JUCEAlgorithmIDs
  221. {
  222. enum
  223. {
  224. inputChannels = AAX_FIELD_INDEX (JUCEAlgorithmContext, inputChannels),
  225. outputChannels = AAX_FIELD_INDEX (JUCEAlgorithmContext, outputChannels),
  226. bufferSize = AAX_FIELD_INDEX (JUCEAlgorithmContext, bufferSize),
  227. bypass = AAX_FIELD_INDEX (JUCEAlgorithmContext, bypass),
  228. #if JucePlugin_WantsMidiInput || JucePlugin_IsMidiEffect
  229. midiNodeIn = AAX_FIELD_INDEX (JUCEAlgorithmContext, midiNodeIn),
  230. #endif
  231. #if JucePlugin_ProducesMidiOutput || JucePlugin_IsSynth || JucePlugin_IsMidiEffect
  232. midiNodeOut = AAX_FIELD_INDEX (JUCEAlgorithmContext, midiNodeOut),
  233. #endif
  234. pluginInstance = AAX_FIELD_INDEX (JUCEAlgorithmContext, pluginInstance),
  235. preparedFlag = AAX_FIELD_INDEX (JUCEAlgorithmContext, isPrepared),
  236. sideChainBuffers = AAX_FIELD_INDEX (JUCEAlgorithmContext, sideChainBuffers)
  237. };
  238. };
  239. #if JucePlugin_WantsMidiInput || JucePlugin_IsMidiEffect
  240. static AAX_IMIDINode* getMidiNodeIn (const JUCEAlgorithmContext& c) noexcept { return c.midiNodeIn; }
  241. #else
  242. static AAX_IMIDINode* getMidiNodeIn (const JUCEAlgorithmContext&) noexcept { return nullptr; }
  243. #endif
  244. #if JucePlugin_ProducesMidiOutput || JucePlugin_IsSynth || JucePlugin_IsMidiEffect
  245. AAX_IMIDINode* midiNodeOut;
  246. static AAX_IMIDINode* getMidiNodeOut (const JUCEAlgorithmContext& c) noexcept { return c.midiNodeOut; }
  247. #else
  248. static AAX_IMIDINode* getMidiNodeOut (const JUCEAlgorithmContext&) noexcept { return nullptr; }
  249. #endif
  250. //==============================================================================
  251. class JuceAAX_GUI : public AAX_CEffectGUI
  252. {
  253. public:
  254. JuceAAX_GUI() {}
  255. ~JuceAAX_GUI() { DeleteViewContainer(); }
  256. static AAX_IEffectGUI* AAX_CALLBACK Create() { return new JuceAAX_GUI(); }
  257. void CreateViewContents() override
  258. {
  259. if (component == nullptr)
  260. {
  261. if (JuceAAX_Processor* params = dynamic_cast<JuceAAX_Processor*> (GetEffectParameters()))
  262. component = new ContentWrapperComponent (*this, params->getPluginInstance());
  263. else
  264. jassertfalse;
  265. }
  266. }
  267. void CreateViewContainer() override
  268. {
  269. CreateViewContents();
  270. if (void* nativeViewToAttachTo = GetViewContainerPtr())
  271. {
  272. #if JUCE_MAC
  273. if (GetViewContainerType() == AAX_eViewContainer_Type_NSView)
  274. #else
  275. if (GetViewContainerType() == AAX_eViewContainer_Type_HWND)
  276. #endif
  277. {
  278. component->setVisible (true);
  279. component->addToDesktop (0, nativeViewToAttachTo);
  280. }
  281. }
  282. }
  283. void DeleteViewContainer() override
  284. {
  285. if (component != nullptr)
  286. {
  287. JUCE_AUTORELEASEPOOL
  288. {
  289. component->removeFromDesktop();
  290. component = nullptr;
  291. }
  292. }
  293. }
  294. AAX_Result GetViewSize (AAX_Point* viewSize) const override
  295. {
  296. if (component != nullptr)
  297. {
  298. viewSize->horz = (float) component->getWidth();
  299. viewSize->vert = (float) component->getHeight();
  300. return AAX_SUCCESS;
  301. }
  302. return AAX_ERROR_NULL_OBJECT;
  303. }
  304. AAX_Result ParameterUpdated (AAX_CParamID) override
  305. {
  306. return AAX_SUCCESS;
  307. }
  308. AAX_Result SetControlHighlightInfo (AAX_CParamID paramID, AAX_CBoolean isHighlighted, AAX_EHighlightColor colour) override
  309. {
  310. if (component != nullptr && component->pluginEditor != nullptr)
  311. {
  312. if (! isBypassParam (paramID))
  313. {
  314. AudioProcessorEditor::ParameterControlHighlightInfo info;
  315. info.parameterIndex = getParamIndexFromID (paramID);
  316. info.isHighlighted = (isHighlighted != 0);
  317. info.suggestedColour = getColourFromHighlightEnum (colour);
  318. component->pluginEditor->setControlHighlight (info);
  319. }
  320. return AAX_SUCCESS;
  321. }
  322. return AAX_ERROR_NULL_OBJECT;
  323. }
  324. private:
  325. struct ContentWrapperComponent : public juce::Component
  326. {
  327. ContentWrapperComponent (JuceAAX_GUI& gui, AudioProcessor& plugin)
  328. : owner (gui)
  329. {
  330. setOpaque (true);
  331. setBroughtToFrontOnMouseClick (true);
  332. addAndMakeVisible (pluginEditor = plugin.createEditorIfNeeded());
  333. if (pluginEditor != nullptr)
  334. {
  335. setBounds (pluginEditor->getLocalBounds());
  336. pluginEditor->addMouseListener (this, true);
  337. }
  338. }
  339. ~ContentWrapperComponent()
  340. {
  341. if (pluginEditor != nullptr)
  342. {
  343. PopupMenu::dismissAllActiveMenus();
  344. pluginEditor->removeMouseListener (this);
  345. pluginEditor->processor.editorBeingDeleted (pluginEditor);
  346. }
  347. }
  348. void paint (Graphics& g) override
  349. {
  350. g.fillAll (Colours::black);
  351. }
  352. template <typename MethodType>
  353. void callMouseMethod (const MouseEvent& e, MethodType method)
  354. {
  355. if (AAX_IViewContainer* vc = owner.GetViewContainer())
  356. {
  357. const int parameterIndex = pluginEditor->getControlParameterIndex (*e.eventComponent);
  358. if (parameterIndex >= 0)
  359. {
  360. uint32_t mods = 0;
  361. vc->GetModifiers (&mods);
  362. (vc->*method) (IndexAsParamID (parameterIndex), mods);
  363. }
  364. }
  365. }
  366. void mouseDown (const MouseEvent& e) override { callMouseMethod (e, &AAX_IViewContainer::HandleParameterMouseDown); }
  367. void mouseUp (const MouseEvent& e) override { callMouseMethod (e, &AAX_IViewContainer::HandleParameterMouseUp); }
  368. void mouseDrag (const MouseEvent& e) override { callMouseMethod (e, &AAX_IViewContainer::HandleParameterMouseDrag); }
  369. void childBoundsChanged (Component*) override
  370. {
  371. if (pluginEditor != nullptr)
  372. {
  373. const int w = pluginEditor->getWidth();
  374. const int h = pluginEditor->getHeight();
  375. setSize (w, h);
  376. AAX_Point newSize ((float) h, (float) w);
  377. owner.GetViewContainer()->SetViewSize (newSize);
  378. }
  379. }
  380. ScopedPointer<AudioProcessorEditor> pluginEditor;
  381. JuceAAX_GUI& owner;
  382. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ContentWrapperComponent)
  383. };
  384. ScopedPointer<ContentWrapperComponent> component;
  385. ScopedJuceInitialiser_GUI libraryInitialiser;
  386. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (JuceAAX_GUI)
  387. };
  388. //==============================================================================
  389. class JuceAAX_Processor : public AAX_CEffectParameters,
  390. public juce::AudioPlayHead,
  391. public AudioProcessorListener
  392. {
  393. public:
  394. JuceAAX_Processor() : pluginInstance (createPluginFilterOfType (AudioProcessor::wrapperType_AAX)),
  395. busUtils (*pluginInstance, false),
  396. sampleRate (0), lastBufferSize (1024), maxBufferSize (1024),
  397. hasSidechain (false)
  398. {
  399. pluginInstance->setPlayHead (this);
  400. pluginInstance->addListener (this);
  401. busUtils.findAllCompatibleLayouts();
  402. AAX_CEffectParameters::GetNumberOfChunks (&juceChunkIndex);
  403. }
  404. static AAX_CEffectParameters* AAX_CALLBACK Create() { return new JuceAAX_Processor(); }
  405. AAX_Result EffectInit() override
  406. {
  407. AAX_Result err;
  408. check (Controller()->GetSampleRate (&sampleRate));
  409. if ((err = preparePlugin()) != AAX_SUCCESS)
  410. return err;
  411. addBypassParameter();
  412. addAudioProcessorParameters();
  413. return AAX_SUCCESS;
  414. }
  415. AAX_Result GetNumberOfChunks (int32_t* numChunks) const override
  416. {
  417. // The juceChunk is the last chunk.
  418. *numChunks = juceChunkIndex + 1;
  419. return AAX_SUCCESS;
  420. }
  421. AAX_Result GetChunkIDFromIndex (int32_t index, AAX_CTypeID* chunkID) const override
  422. {
  423. if (index != juceChunkIndex)
  424. return AAX_CEffectParameters::GetChunkIDFromIndex (index, chunkID);
  425. *chunkID = juceChunkType;
  426. return AAX_SUCCESS;
  427. }
  428. juce::MemoryBlock& getTemporaryChunkMemory() const
  429. {
  430. ScopedLock sl (perThreadDataLock);
  431. const Thread::ThreadID currentThread = Thread::getCurrentThreadId();
  432. if (ChunkMemoryBlock::Ptr m = perThreadFilterData [currentThread])
  433. return m->data;
  434. ChunkMemoryBlock::Ptr m (new ChunkMemoryBlock());
  435. perThreadFilterData.set (currentThread, m);
  436. return m->data;
  437. }
  438. AAX_Result GetChunkSize (AAX_CTypeID chunkID, uint32_t* oSize) const override
  439. {
  440. if (chunkID != juceChunkType)
  441. return AAX_CEffectParameters::GetChunkSize (chunkID, oSize);
  442. juce::MemoryBlock& tempFilterData = getTemporaryChunkMemory();
  443. tempFilterData.reset();
  444. pluginInstance->getStateInformation (tempFilterData);
  445. *oSize = (uint32_t) tempFilterData.getSize();
  446. return AAX_SUCCESS;
  447. }
  448. AAX_Result GetChunk (AAX_CTypeID chunkID, AAX_SPlugInChunk* oChunk) const override
  449. {
  450. if (chunkID != juceChunkType)
  451. return AAX_CEffectParameters::GetChunk (chunkID, oChunk);
  452. juce::MemoryBlock& tempFilterData = getTemporaryChunkMemory();
  453. if (tempFilterData.getSize() == 0)
  454. return 20700 /*AAX_ERROR_PLUGIN_API_INVALID_THREAD*/;
  455. oChunk->fSize = (int32_t) tempFilterData.getSize();
  456. tempFilterData.copyTo (oChunk->fData, 0, tempFilterData.getSize());
  457. tempFilterData.reset();
  458. return AAX_SUCCESS;
  459. }
  460. AAX_Result SetChunk (AAX_CTypeID chunkID, const AAX_SPlugInChunk* chunk) override
  461. {
  462. if (chunkID != juceChunkType)
  463. return AAX_CEffectParameters::SetChunk (chunkID, chunk);
  464. pluginInstance->setStateInformation ((void*) chunk->fData, chunk->fSize);
  465. // Notify Pro Tools that the parameters were updated.
  466. // Without it a bug happens in these circumstances:
  467. // * A preset is saved with the RTAS version of the plugin (".tfx" preset format).
  468. // * The preset is loaded in PT 10 using the AAX version.
  469. // * The session is then saved, and closed.
  470. // * The saved session is loaded, but acting as if the preset was never loaded.
  471. const int numParameters = pluginInstance->getNumParameters();
  472. for (int i = 0; i < numParameters; ++i)
  473. SetParameterNormalizedValue (IndexAsParamID (i), (double) pluginInstance->getParameter(i));
  474. return AAX_SUCCESS;
  475. }
  476. AAX_Result ResetFieldData (AAX_CFieldIndex fieldIndex, void* data, uint32_t dataSize) const override
  477. {
  478. switch (fieldIndex)
  479. {
  480. case JUCEAlgorithmIDs::pluginInstance:
  481. {
  482. const size_t numObjects = dataSize / sizeof (PluginInstanceInfo);
  483. PluginInstanceInfo* const objects = static_cast<PluginInstanceInfo*> (data);
  484. jassert (numObjects == 1); // not sure how to handle more than one..
  485. for (size_t i = 0; i < numObjects; ++i)
  486. new (objects + i) PluginInstanceInfo (const_cast<JuceAAX_Processor&> (*this));
  487. break;
  488. }
  489. case JUCEAlgorithmIDs::preparedFlag:
  490. {
  491. const_cast<JuceAAX_Processor*>(this)->preparePlugin();
  492. const size_t numObjects = dataSize / sizeof (uint32_t);
  493. uint32_t* const objects = static_cast<uint32_t*> (data);
  494. for (size_t i = 0; i < numObjects; ++i)
  495. new (objects + i) uint32_t (1);
  496. break;
  497. }
  498. }
  499. return AAX_SUCCESS;
  500. }
  501. AAX_Result UpdateParameterNormalizedValue (AAX_CParamID paramID, double value, AAX_EUpdateSource source) override
  502. {
  503. AAX_Result result = AAX_CEffectParameters::UpdateParameterNormalizedValue (paramID, value, source);
  504. if (! isBypassParam (paramID))
  505. pluginInstance->setParameter (getParamIndexFromID (paramID), (float) value);
  506. return result;
  507. }
  508. AAX_Result GetParameterValueFromString (AAX_CParamID paramID, double* result, const AAX_IString& text) const override
  509. {
  510. if (isBypassParam (paramID))
  511. {
  512. *result = (text.Get()[0] == 'B') ? 1 : 0;
  513. return AAX_SUCCESS;
  514. }
  515. if (AudioProcessorParameter* param = pluginInstance->getParameters() [getParamIndexFromID (paramID)])
  516. {
  517. *result = param->getValueForText (text.Get());
  518. return AAX_SUCCESS;
  519. }
  520. return AAX_CEffectParameters::GetParameterValueFromString (paramID, result, text);
  521. }
  522. AAX_Result GetParameterStringFromValue (AAX_CParamID paramID, double value, AAX_IString* result, int32_t maxLen) const override
  523. {
  524. if (isBypassParam (paramID))
  525. {
  526. result->Set (value == 0 ? "Off" : (maxLen >= 8 ? "Bypassed" : "Byp"));
  527. }
  528. else
  529. {
  530. const int paramIndex = getParamIndexFromID (paramID);
  531. juce::String text;
  532. if (AudioProcessorParameter* param = pluginInstance->getParameters() [paramIndex])
  533. text = param->getText ((float) value, maxLen);
  534. else
  535. text = pluginInstance->getParameterText (paramIndex, maxLen);
  536. result->Set (text.toRawUTF8());
  537. }
  538. return AAX_SUCCESS;
  539. }
  540. AAX_Result GetParameterNumberofSteps (AAX_CParamID paramID, int32_t* result) const
  541. {
  542. if (isBypassParam (paramID))
  543. *result = 2;
  544. else
  545. *result = pluginInstance->getParameterNumSteps (getParamIndexFromID (paramID));
  546. return AAX_SUCCESS;
  547. }
  548. AAX_Result GetParameterNormalizedValue (AAX_CParamID paramID, double* result) const override
  549. {
  550. if (isBypassParam (paramID))
  551. return AAX_CEffectParameters::GetParameterNormalizedValue (paramID, result);
  552. *result = pluginInstance->getParameter (getParamIndexFromID (paramID));
  553. return AAX_SUCCESS;
  554. }
  555. AAX_Result SetParameterNormalizedValue (AAX_CParamID paramID, double newValue) override
  556. {
  557. if (isBypassParam (paramID))
  558. return AAX_CEffectParameters::SetParameterNormalizedValue (paramID, newValue);
  559. if (AAX_IParameter* p = const_cast<AAX_IParameter*> (mParameterManager.GetParameterByID (paramID)))
  560. p->SetValueWithFloat ((float) newValue);
  561. pluginInstance->setParameter (getParamIndexFromID (paramID), (float) newValue);
  562. return AAX_SUCCESS;
  563. }
  564. AAX_Result SetParameterNormalizedRelative (AAX_CParamID paramID, double newDeltaValue) override
  565. {
  566. if (isBypassParam (paramID))
  567. return AAX_CEffectParameters::SetParameterNormalizedRelative (paramID, newDeltaValue);
  568. const int paramIndex = getParamIndexFromID (paramID);
  569. const float newValue = pluginInstance->getParameter (paramIndex) + (float) newDeltaValue;
  570. pluginInstance->setParameter (paramIndex, jlimit (0.0f, 1.0f, newValue));
  571. if (AAX_IParameter* p = const_cast<AAX_IParameter*> (mParameterManager.GetParameterByID (paramID)))
  572. p->SetValueWithFloat (newValue);
  573. return AAX_SUCCESS;
  574. }
  575. AAX_Result GetParameterNameOfLength (AAX_CParamID paramID, AAX_IString* result, int32_t maxLen) const override
  576. {
  577. if (isBypassParam (paramID))
  578. result->Set (maxLen >= 13 ? "Master Bypass"
  579. : (maxLen >= 8 ? "Mast Byp"
  580. : (maxLen >= 6 ? "MstByp" : "MByp")));
  581. else
  582. result->Set (pluginInstance->getParameterName (getParamIndexFromID (paramID), maxLen).toRawUTF8());
  583. return AAX_SUCCESS;
  584. }
  585. AAX_Result GetParameterName (AAX_CParamID paramID, AAX_IString* result) const override
  586. {
  587. if (isBypassParam (paramID))
  588. result->Set ("Master Bypass");
  589. else
  590. result->Set (pluginInstance->getParameterName (getParamIndexFromID (paramID), 31).toRawUTF8());
  591. return AAX_SUCCESS;
  592. }
  593. AAX_Result GetParameterDefaultNormalizedValue (AAX_CParamID paramID, double* result) const override
  594. {
  595. if (! isBypassParam (paramID))
  596. {
  597. *result = (double) pluginInstance->getParameterDefaultValue (getParamIndexFromID (paramID));
  598. jassert (*result >= 0 && *result <= 1.0f);
  599. }
  600. return AAX_SUCCESS;
  601. }
  602. AudioProcessor& getPluginInstance() const noexcept { return *pluginInstance; }
  603. bool getCurrentPosition (juce::AudioPlayHead::CurrentPositionInfo& info) override
  604. {
  605. const AAX_ITransport& transport = *Transport();
  606. info.bpm = 0.0;
  607. check (transport.GetCurrentTempo (&info.bpm));
  608. int32_t num = 4, den = 4;
  609. transport.GetCurrentMeter (&num, &den);
  610. info.timeSigNumerator = (int) num;
  611. info.timeSigDenominator = (int) den;
  612. info.timeInSamples = 0;
  613. if (transport.IsTransportPlaying (&info.isPlaying) != AAX_SUCCESS)
  614. info.isPlaying = false;
  615. if (info.isPlaying
  616. || transport.GetTimelineSelectionStartPosition (&info.timeInSamples) != AAX_SUCCESS)
  617. check (transport.GetCurrentNativeSampleLocation (&info.timeInSamples));
  618. info.timeInSeconds = info.timeInSamples / sampleRate;
  619. int64_t ticks = 0;
  620. check (transport.GetCurrentTickPosition (&ticks));
  621. info.ppqPosition = ticks / 960000.0;
  622. info.isLooping = false;
  623. int64_t loopStartTick = 0, loopEndTick = 0;
  624. check (transport.GetCurrentLoopPosition (&info.isLooping, &loopStartTick, &loopEndTick));
  625. info.ppqLoopStart = loopStartTick / 960000.0;
  626. info.ppqLoopEnd = loopEndTick / 960000.0;
  627. info.editOriginTime = 0;
  628. info.frameRate = AudioPlayHead::fpsUnknown;
  629. AAX_EFrameRate frameRate;
  630. int32_t offset;
  631. if (transport.GetTimeCodeInfo (&frameRate, &offset) == AAX_SUCCESS)
  632. {
  633. double framesPerSec = 24.0;
  634. switch (frameRate)
  635. {
  636. case AAX_eFrameRate_Undeclared: break;
  637. case AAX_eFrameRate_24Frame: info.frameRate = AudioPlayHead::fps24; break;
  638. case AAX_eFrameRate_25Frame: info.frameRate = AudioPlayHead::fps25; framesPerSec = 25.0; break;
  639. case AAX_eFrameRate_2997NonDrop: info.frameRate = AudioPlayHead::fps2997; framesPerSec = 29.97002997; break;
  640. case AAX_eFrameRate_2997DropFrame: info.frameRate = AudioPlayHead::fps2997drop; framesPerSec = 29.97002997; break;
  641. case AAX_eFrameRate_30NonDrop: info.frameRate = AudioPlayHead::fps30; framesPerSec = 30.0; break;
  642. case AAX_eFrameRate_30DropFrame: info.frameRate = AudioPlayHead::fps30drop; framesPerSec = 30.0; break;
  643. case AAX_eFrameRate_23976: info.frameRate = AudioPlayHead::fps24; framesPerSec = 23.976; break;
  644. default: break;
  645. }
  646. info.editOriginTime = offset / framesPerSec;
  647. }
  648. // No way to get these: (?)
  649. info.isRecording = false;
  650. info.ppqPositionOfLastBarStart = 0;
  651. return true;
  652. }
  653. void audioProcessorParameterChanged (AudioProcessor* /*processor*/, int parameterIndex, float newValue) override
  654. {
  655. SetParameterNormalizedValue (IndexAsParamID (parameterIndex), (double) newValue);
  656. }
  657. void audioProcessorChanged (AudioProcessor* processor) override
  658. {
  659. ++mNumPlugInChanges;
  660. check (Controller()->SetSignalLatency (processor->getLatencySamples()));
  661. }
  662. void audioProcessorParameterChangeGestureBegin (AudioProcessor* /*processor*/, int parameterIndex) override
  663. {
  664. TouchParameter (IndexAsParamID (parameterIndex));
  665. }
  666. void audioProcessorParameterChangeGestureEnd (AudioProcessor* /*processor*/, int parameterIndex) override
  667. {
  668. ReleaseParameter (IndexAsParamID (parameterIndex));
  669. }
  670. AAX_Result NotificationReceived (AAX_CTypeID type, const void* data, uint32_t size) override
  671. {
  672. if (type == AAX_eNotificationEvent_EnteringOfflineMode) pluginInstance->setNonRealtime (true);
  673. if (type == AAX_eNotificationEvent_ExitingOfflineMode) pluginInstance->setNonRealtime (false);
  674. return AAX_CEffectParameters::NotificationReceived (type, data, size);
  675. }
  676. const float* getAudioBufferForInput (const float* const* inputs, const int sidechain, const int mainNumIns, int idx) const noexcept
  677. {
  678. jassert (idx < (mainNumIns + 1));
  679. if (idx < mainNumIns)
  680. return inputs[idx];
  681. return (sidechain != -1 ? inputs[sidechain] : sideChainBuffer.getData());
  682. }
  683. void process (const float* const* inputs, float* const* outputs, const int sideChainBufferIdx,
  684. const int bufferSize, const bool bypass,
  685. AAX_IMIDINode* midiNodeIn, AAX_IMIDINode* midiNodesOut)
  686. {
  687. const int numIns = pluginInstance->getTotalNumInputChannels();
  688. const int numOuts = pluginInstance->getTotalNumOutputChannels();
  689. const int mainNumIns = numIns > 0 ? pluginInstance->busArrangement.inputBuses.getReference (0).channels.size() : 0;
  690. const int sidechain = busUtils.getNumEnabledBuses (true) >= 2 ? sideChainBufferIdx : -1;
  691. if (numOuts >= numIns)
  692. {
  693. for (int i = 0; i < numIns; ++i)
  694. memcpy (outputs[i], getAudioBufferForInput (inputs, sidechain, mainNumIns, i), (size_t) bufferSize * sizeof (float));
  695. process (outputs, numOuts, bufferSize, bypass, midiNodeIn, midiNodesOut);
  696. }
  697. else
  698. {
  699. if (channelList.size() <= numIns)
  700. channelList.insertMultiple (-1, nullptr, 1 + numIns - channelList.size());
  701. float** channels = channelList.getRawDataPointer();
  702. for (int i = 0; i < numOuts; ++i)
  703. {
  704. memcpy (outputs[i], getAudioBufferForInput (inputs, sidechain, mainNumIns, i), (size_t) bufferSize * sizeof (float));
  705. channels[i] = outputs[i];
  706. }
  707. for (int i = numOuts; i < numIns; ++i)
  708. channels[i] = const_cast<float*> (getAudioBufferForInput (inputs, sidechain, mainNumIns, i));
  709. process (channels, numIns, bufferSize, bypass, midiNodeIn, midiNodesOut);
  710. }
  711. }
  712. bool supportsSidechain() const noexcept { return hasSidechain; };
  713. private:
  714. void process (float* const* channels, const int numChans, const int bufferSize,
  715. const bool bypass, AAX_IMIDINode* midiNodeIn, AAX_IMIDINode* midiNodesOut)
  716. {
  717. AudioSampleBuffer buffer (channels, numChans, bufferSize);
  718. midiBuffer.clear();
  719. (void) midiNodeIn;
  720. (void) midiNodesOut;
  721. #if JucePlugin_WantsMidiInput || JucePlugin_IsMidiEffect
  722. {
  723. AAX_CMidiStream* const midiStream = midiNodeIn->GetNodeBuffer();
  724. const uint32_t numMidiEvents = midiStream->mBufferSize;
  725. for (uint32_t i = 0; i < numMidiEvents; ++i)
  726. {
  727. const AAX_CMidiPacket& m = midiStream->mBuffer[i];
  728. jassert ((int) m.mTimestamp < bufferSize);
  729. midiBuffer.addEvent (m.mData, (int) m.mLength,
  730. jlimit (0, (int) bufferSize - 1, (int) m.mTimestamp));
  731. }
  732. }
  733. #endif
  734. {
  735. if (lastBufferSize != bufferSize)
  736. {
  737. lastBufferSize = bufferSize;
  738. pluginInstance->setRateAndBufferSizeDetails (sampleRate, bufferSize);
  739. if (bufferSize > maxBufferSize)
  740. {
  741. // we only call prepareToPlay here if the new buffer size is larger than
  742. // the one used last time prepareToPlay was called.
  743. // currently, this should never actually happen, because as of Pro Tools 12,
  744. // the maximum possible value is 1024, and we call prepareToPlay with that
  745. // value during initialisation.
  746. pluginInstance->prepareToPlay (sampleRate, bufferSize);
  747. maxBufferSize = bufferSize;
  748. sideChainBuffer.realloc (static_cast<size_t> (maxBufferSize));
  749. }
  750. }
  751. const ScopedLock sl (pluginInstance->getCallbackLock());
  752. if (bypass)
  753. pluginInstance->processBlockBypassed (buffer, midiBuffer);
  754. else
  755. pluginInstance->processBlock (buffer, midiBuffer);
  756. }
  757. #if JucePlugin_ProducesMidiOutput || JucePlugin_IsMidiEffect
  758. {
  759. const juce::uint8* midiEventData;
  760. int midiEventSize, midiEventPosition;
  761. MidiBuffer::Iterator i (midiBuffer);
  762. AAX_CMidiPacket packet;
  763. packet.mIsImmediate = false;
  764. while (i.getNextEvent (midiEventData, midiEventSize, midiEventPosition))
  765. {
  766. jassert (isPositiveAndBelow (midiEventPosition, bufferSize));
  767. if (midiEventSize <= 4)
  768. {
  769. packet.mTimestamp = (uint32_t) midiEventPosition;
  770. packet.mLength = (uint32_t) midiEventSize;
  771. memcpy (packet.mData, midiEventData, (size_t) midiEventSize);
  772. check (midiNodesOut->PostMIDIPacket (&packet));
  773. }
  774. }
  775. }
  776. #else
  777. (void) midiNodesOut;
  778. #endif
  779. }
  780. void addBypassParameter()
  781. {
  782. AAX_IParameter* masterBypass = new AAX_CParameter<bool> (cDefaultMasterBypassID,
  783. AAX_CString ("Master Bypass"),
  784. false,
  785. AAX_CBinaryTaperDelegate<bool>(),
  786. AAX_CBinaryDisplayDelegate<bool> ("bypass", "on"),
  787. true);
  788. masterBypass->SetNumberOfSteps (2);
  789. masterBypass->SetType (AAX_eParameterType_Discrete);
  790. mParameterManager.AddParameter (masterBypass);
  791. mPacketDispatcher.RegisterPacket (cDefaultMasterBypassID, JUCEAlgorithmIDs::bypass);
  792. }
  793. void addAudioProcessorParameters()
  794. {
  795. AudioProcessor& audioProcessor = getPluginInstance();
  796. const int numParameters = audioProcessor.getNumParameters();
  797. for (int parameterIndex = 0; parameterIndex < numParameters; ++parameterIndex)
  798. {
  799. AAX_CString paramName (audioProcessor.getParameterName (parameterIndex, 31).toRawUTF8());
  800. AAX_IParameter* parameter
  801. = new AAX_CParameter<float> (IndexAsParamID (parameterIndex),
  802. paramName,
  803. audioProcessor.getParameterDefaultValue (parameterIndex),
  804. AAX_CLinearTaperDelegate<float, 0>(),
  805. AAX_CNumberDisplayDelegate<float, 3>(),
  806. audioProcessor.isParameterAutomatable (parameterIndex));
  807. parameter->AddShortenedName (audioProcessor.getParameterName (parameterIndex, 4).toRawUTF8());
  808. const int parameterNumSteps = audioProcessor.getParameterNumSteps (parameterIndex);
  809. parameter->SetNumberOfSteps ((uint32_t) parameterNumSteps);
  810. parameter->SetType (parameterNumSteps > 1000 ? AAX_eParameterType_Continuous
  811. : AAX_eParameterType_Discrete);
  812. parameter->SetOrientation (audioProcessor.isParameterOrientationInverted (parameterIndex)
  813. ? (AAX_eParameterOrientation_RightMinLeftMax | AAX_eParameterOrientation_TopMinBottomMax
  814. | AAX_eParameterOrientation_RotarySingleDotMode | AAX_eParameterOrientation_RotaryRightMinLeftMax)
  815. : (AAX_eParameterOrientation_LeftMinRightMax | AAX_eParameterOrientation_BottomMinTopMax
  816. | AAX_eParameterOrientation_RotarySingleDotMode | AAX_eParameterOrientation_RotaryLeftMinRightMax));
  817. mParameterManager.AddParameter (parameter);
  818. }
  819. }
  820. AAX_Result preparePlugin()
  821. {
  822. AudioProcessor& audioProcessor = getPluginInstance();
  823. #if JucePlugin_IsMidiEffect
  824. // MIDI effect plug-ins do not support any audio channels
  825. jassert (audioProcessor.busArrangement.getTotalNumInputChannels() == 0
  826. && audioProcessor.busArrangement.getTotalNumOutputChannels() == 0);
  827. #else
  828. AAX_EStemFormat inputStemFormat = AAX_eStemFormat_None;
  829. check (Controller()->GetInputStemFormat (&inputStemFormat));
  830. AAX_EStemFormat outputStemFormat = AAX_eStemFormat_None;
  831. check (Controller()->GetOutputStemFormat (&outputStemFormat));
  832. const AudioChannelSet inputSet = channelSetFromStemFormat (inputStemFormat, busUtils.busIgnoresLayout (true, 0));
  833. const AudioChannelSet outputSet = channelSetFromStemFormat (outputStemFormat, busUtils.busIgnoresLayout (false, 0));
  834. if ( (inputSet == AudioChannelSet::disabled() && inputStemFormat != AAX_eStemFormat_None)
  835. || (outputSet == AudioChannelSet::disabled() && outputStemFormat != AAX_eStemFormat_None))
  836. return AAX_ERROR_UNIMPLEMENTED;
  837. bool success = true;
  838. if (busUtils.getBusCount (true) > 0)
  839. success = audioProcessor.setPreferredBusArrangement (true, 0, inputSet);
  840. if (success && busUtils.getBusCount (false) > 0)
  841. success = audioProcessor.setPreferredBusArrangement (false, 0, outputSet);
  842. // This should never happen as the plugin reported that this layout is supported
  843. jassert (success);
  844. hasSidechain = enableAuxBusesForCurrentFormat (busUtils, inputSet, outputSet);
  845. if (hasSidechain)
  846. sideChainBuffer.realloc (static_cast<size_t> (maxBufferSize));
  847. // recheck the format
  848. if ( (busUtils.getBusCount (true) > 0 && busUtils.getChannelSet (true, 0) != inputSet)
  849. || (busUtils.getBusCount (false) > 0 && busUtils.getChannelSet (false, 0) != outputSet)
  850. || (hasSidechain && busUtils.getNumChannels(true, 1) != 1))
  851. return AAX_ERROR_UNIMPLEMENTED;
  852. #endif
  853. audioProcessor.setRateAndBufferSizeDetails (sampleRate, maxBufferSize);
  854. audioProcessor.prepareToPlay (sampleRate, lastBufferSize);
  855. maxBufferSize = lastBufferSize;
  856. check (Controller()->SetSignalLatency (audioProcessor.getLatencySamples()));
  857. return AAX_SUCCESS;
  858. }
  859. ScopedJuceInitialiser_GUI libraryInitialiser;
  860. ScopedPointer<AudioProcessor> pluginInstance;
  861. PluginBusUtilities busUtils;
  862. MidiBuffer midiBuffer;
  863. Array<float*> channelList;
  864. int32_t juceChunkIndex;
  865. AAX_CSampleRate sampleRate;
  866. int lastBufferSize, maxBufferSize;
  867. bool hasSidechain;
  868. HeapBlock<float> sideChainBuffer;
  869. struct ChunkMemoryBlock : public ReferenceCountedObject
  870. {
  871. juce::MemoryBlock data;
  872. typedef ReferenceCountedObjectPtr<ChunkMemoryBlock> Ptr;
  873. };
  874. // temporary filter data is generated in GetChunkSize
  875. // and the size of the data returned. To avoid generating
  876. // it again in GetChunk, we need to store it somewhere.
  877. // However, as GetChunkSize and GetChunk can be called
  878. // on different threads, we store it in thread dependant storage
  879. // in a hash map with the thread id as a key.
  880. mutable HashMap<Thread::ThreadID, ChunkMemoryBlock::Ptr> perThreadFilterData;
  881. CriticalSection perThreadDataLock;
  882. JUCE_DECLARE_NON_COPYABLE (JuceAAX_Processor)
  883. };
  884. //==============================================================================
  885. struct IndexAsParamID
  886. {
  887. inline explicit IndexAsParamID (int i) noexcept : index (i) {}
  888. operator AAX_CParamID() noexcept
  889. {
  890. jassert (index >= 0);
  891. char* t = name + sizeof (name);
  892. *--t = 0;
  893. int v = index;
  894. do
  895. {
  896. *--t = (char) ('0' + (v % 10));
  897. v /= 10;
  898. } while (v > 0);
  899. return static_cast<AAX_CParamID> (t);
  900. }
  901. private:
  902. int index;
  903. char name[32];
  904. JUCE_DECLARE_NON_COPYABLE (IndexAsParamID)
  905. };
  906. //==============================================================================
  907. struct AAXFormatConfiguration
  908. {
  909. AAXFormatConfiguration() noexcept
  910. : inputFormat (AAX_eStemFormat_None), outputFormat (AAX_eStemFormat_None) {}
  911. AAXFormatConfiguration (AAX_EStemFormat inFormat, AAX_EStemFormat outFormat) noexcept
  912. : inputFormat (inFormat), outputFormat (outFormat) {}
  913. AAX_EStemFormat inputFormat, outputFormat;
  914. bool operator== (const AAXFormatConfiguration other) const noexcept { return (inputFormat == other.inputFormat) && (outputFormat == other.outputFormat); }
  915. bool operator< (const AAXFormatConfiguration other) const noexcept
  916. {
  917. return (inputFormat == other.inputFormat) ? (outputFormat < other.outputFormat) : (inputFormat < other.inputFormat);
  918. }
  919. };
  920. //==============================================================================
  921. static void AAX_CALLBACK algorithmProcessCallback (JUCEAlgorithmContext* const instancesBegin[],
  922. const void* const instancesEnd)
  923. {
  924. for (JUCEAlgorithmContext* const* iter = instancesBegin; iter < instancesEnd; ++iter)
  925. {
  926. const JUCEAlgorithmContext& i = **iter;
  927. int sideChainBufferIdx = static_cast<int> (i.pluginInstance->parameters.supportsSidechain() && i.sideChainBuffers != nullptr ? *i.sideChainBuffers : static_cast<int32_t> (-1));
  928. i.pluginInstance->parameters.process (i.inputChannels, i.outputChannels, sideChainBufferIdx,
  929. *(i.bufferSize), *(i.bypass) != 0,
  930. getMidiNodeIn(i), getMidiNodeOut(i));
  931. }
  932. }
  933. static bool enableAuxBusesForCurrentFormat (PluginBusUtilities& busUtils, const AudioChannelSet& inputLayout,
  934. const AudioChannelSet& outputLayout)
  935. {
  936. const int numOutBuses = busUtils.getBusCount (false);
  937. const int numInputBuses = busUtils.getBusCount(true);
  938. if (numOutBuses > 1)
  939. {
  940. PluginBusUtilities::ScopedBusRestorer layoutRestorer (busUtils);
  941. // enable all possible output buses
  942. for (int busIdx = 1; busIdx < busUtils.getBusCount (false); ++busIdx)
  943. {
  944. AudioChannelSet layout = busUtils.getChannelSet (false, busIdx);
  945. // bus disabled by default? try to enable it with the default layout
  946. if (layout == AudioChannelSet::disabled())
  947. {
  948. layout = busUtils.getDefaultLayoutForBus (false, busIdx);
  949. busUtils.juceFilter.setPreferredBusArrangement (false, busIdx, layout);
  950. }
  951. }
  952. // changing output buses may have changed main bus layout
  953. bool success = true;
  954. if (numInputBuses > 0)
  955. success = busUtils.juceFilter.setPreferredBusArrangement (true, 0, inputLayout);
  956. if (success)
  957. success = busUtils.juceFilter.setPreferredBusArrangement (false, 0, outputLayout);
  958. // was the above successful
  959. if (success && (numInputBuses == 0 || busUtils.getChannelSet (true, 0) == inputLayout)
  960. && busUtils.getChannelSet (false, 0) == outputLayout)
  961. layoutRestorer.release();
  962. }
  963. // does the plug-in have side-chain support? Check the following:
  964. // 1) does it have an input bus with index = 1 which supports mono
  965. // 2) can all other input buses be disabled
  966. // 3) does the format of the main buses not change when enabling the first bus
  967. if (numInputBuses > 1)
  968. {
  969. bool success = true;
  970. bool hasSidechain = false;
  971. if (const AudioChannelSet* set = busUtils.getSupportedBusLayouts (true, 1).getDefaultLayoutForChannelNum (1))
  972. hasSidechain = busUtils.juceFilter.setPreferredBusArrangement (true, 1, *set);
  973. if (! hasSidechain)
  974. success = busUtils.juceFilter.setPreferredBusArrangement (true, 1, AudioChannelSet::disabled());
  975. // AAX requires your processor's first sidechain to be either mono or that
  976. // it can be disabled
  977. jassert(success);
  978. // disable all other input buses
  979. for (int busIdx = 2; busIdx < numInputBuses; ++busIdx)
  980. {
  981. success = busUtils.juceFilter.setPreferredBusArrangement (true, busIdx, AudioChannelSet::disabled());
  982. // AAX can only have a single side-chain input. Therefore, your processor must either
  983. // only have a single side-chain input or allow disabling all other side-chains
  984. jassert (success);
  985. }
  986. if (hasSidechain)
  987. {
  988. if (busUtils.getBusCount (false) == 0 || busUtils.getBusCount (true) == 0 ||
  989. (busUtils.getChannelSet (true, 0) == inputLayout && busUtils.getChannelSet (false, 0) == outputLayout))
  990. return true;
  991. // restore the old layout
  992. if (busUtils.getBusCount(true) > 0)
  993. busUtils.juceFilter.setPreferredBusArrangement (true, 0, inputLayout);
  994. if (busUtils.getBusCount (false) > 0)
  995. busUtils.juceFilter.setPreferredBusArrangement (false, 0, outputLayout);
  996. }
  997. }
  998. return false;
  999. }
  1000. //==============================================================================
  1001. static void createDescriptor (AAX_IComponentDescriptor& desc, int configIndex, PluginBusUtilities& busUtils,
  1002. const AudioChannelSet& inputLayout, const AudioChannelSet& outputLayout,
  1003. const AAX_EStemFormat aaxInputFormat, const AAX_EStemFormat aaxOutputFormat)
  1004. {
  1005. check (desc.AddAudioIn (JUCEAlgorithmIDs::inputChannels));
  1006. check (desc.AddAudioOut (JUCEAlgorithmIDs::outputChannels));
  1007. check (desc.AddAudioBufferLength (JUCEAlgorithmIDs::bufferSize));
  1008. check (desc.AddDataInPort (JUCEAlgorithmIDs::bypass, sizeof (int32_t)));
  1009. #if JucePlugin_WantsMidiInput || JucePlugin_IsMidiEffect
  1010. check (desc.AddMIDINode (JUCEAlgorithmIDs::midiNodeIn, AAX_eMIDINodeType_LocalInput,
  1011. JucePlugin_Name, 0xffff));
  1012. #endif
  1013. #if JucePlugin_ProducesMidiOutput || JucePlugin_IsSynth || JucePlugin_IsMidiEffect
  1014. check (desc.AddMIDINode (JUCEAlgorithmIDs::midiNodeOut, AAX_eMIDINodeType_LocalOutput,
  1015. JucePlugin_Name " Out", 0xffff));
  1016. #endif
  1017. check (desc.AddPrivateData (JUCEAlgorithmIDs::pluginInstance, sizeof (PluginInstanceInfo)));
  1018. check (desc.AddPrivateData (JUCEAlgorithmIDs::preparedFlag, sizeof (int32_t)));
  1019. // Create a property map
  1020. AAX_IPropertyMap* const properties = desc.NewPropertyMap();
  1021. jassert (properties != nullptr);
  1022. properties->AddProperty (AAX_eProperty_ManufacturerID, JucePlugin_AAXManufacturerCode);
  1023. properties->AddProperty (AAX_eProperty_ProductID, JucePlugin_AAXProductId);
  1024. #if JucePlugin_AAXDisableBypass
  1025. properties->AddProperty (AAX_eProperty_CanBypass, false);
  1026. #else
  1027. properties->AddProperty (AAX_eProperty_CanBypass, true);
  1028. #endif
  1029. properties->AddProperty (AAX_eProperty_InputStemFormat, static_cast<AAX_CPropertyValue> (aaxInputFormat));
  1030. properties->AddProperty (AAX_eProperty_OutputStemFormat, static_cast<AAX_CPropertyValue> (aaxOutputFormat));
  1031. // This value needs to match the RTAS wrapper's Type ID, so that
  1032. // the host knows that the RTAS/AAX plugins are equivalent.
  1033. properties->AddProperty (AAX_eProperty_PlugInID_Native, 'jcaa' + configIndex);
  1034. #if ! JucePlugin_AAXDisableAudioSuite
  1035. properties->AddProperty (AAX_eProperty_PlugInID_AudioSuite, 'jyaa' + configIndex);
  1036. #endif
  1037. #if JucePlugin_AAXDisableMultiMono
  1038. properties->AddProperty (AAX_eProperty_Constraint_MultiMonoSupport, false);
  1039. #else
  1040. properties->AddProperty (AAX_eProperty_Constraint_MultiMonoSupport, true);
  1041. #endif
  1042. if (enableAuxBusesForCurrentFormat (busUtils, inputLayout, outputLayout))
  1043. {
  1044. check (desc.AddSideChainIn (JUCEAlgorithmIDs::sideChainBuffers));
  1045. properties->AddProperty (AAX_eProperty_SupportsSideChainInput, true);
  1046. }
  1047. // add the output buses
  1048. // This is incrdibly dumb: the output bus format must be well defined
  1049. // for every main bus in/out format pair. This means that there cannot
  1050. // be two configurations with different aux formats but
  1051. // identical main bus in/out formats.
  1052. for (int busIdx = 1; busIdx < busUtils.getBusCount (false); ++busIdx)
  1053. {
  1054. AudioChannelSet outBusLayout = busUtils.getChannelSet (false, busIdx);
  1055. if (outBusLayout != AudioChannelSet::disabled())
  1056. {
  1057. AAX_EStemFormat auxFormat = getFormatForAudioChannelSet (outBusLayout, busUtils.busIgnoresLayout (false, busIdx));
  1058. if (auxFormat != AAX_eStemFormat_INT32_MAX && auxFormat != AAX_eStemFormat_None)
  1059. {
  1060. const String& name = busUtils.juceFilter.busArrangement.outputBuses.getReference (busIdx).name;
  1061. check (desc.AddAuxOutputStem (0, static_cast<int32_t> (auxFormat), name.toRawUTF8()));
  1062. }
  1063. }
  1064. }
  1065. // this assertion should be covered by the assertions above
  1066. // if not please report a bug
  1067. jassert (busUtils.getNumEnabledBuses (true) <= 2);
  1068. check (desc.AddProcessProc_Native (algorithmProcessCallback, properties));
  1069. }
  1070. static void getPlugInDescription (AAX_IEffectDescriptor& descriptor)
  1071. {
  1072. ScopedPointer<AudioProcessor> plugin = createPluginFilterOfType (AudioProcessor::wrapperType_AAX);
  1073. PluginBusUtilities busUtils (*plugin, false);
  1074. busUtils.findAllCompatibleLayouts();
  1075. descriptor.AddName (JucePlugin_Desc);
  1076. descriptor.AddName (JucePlugin_Name);
  1077. descriptor.AddCategory (JucePlugin_AAXCategory);
  1078. #ifdef JucePlugin_AAXPageTableFile
  1079. // optional page table setting - define this macro in your AppConfig.h if you
  1080. // want to set this value - see Avid documentation for details about its format.
  1081. descriptor.AddResourceInfo (AAX_eResourceType_PageTable, JucePlugin_AAXPageTableFile);
  1082. #endif
  1083. check (descriptor.AddProcPtr ((void*) JuceAAX_GUI::Create, kAAX_ProcPtrID_Create_EffectGUI));
  1084. check (descriptor.AddProcPtr ((void*) JuceAAX_Processor::Create, kAAX_ProcPtrID_Create_EffectParameters));
  1085. SortedSet<AAXFormatConfiguration> aaxFormats;
  1086. SortedSet<AudioChannelSet> inLayouts = busUtils.getBusCount (true) > 0 ? busUtils.getSupportedBusLayouts (true, 0).supportedLayouts : SortedSet<AudioChannelSet>();
  1087. SortedSet<AudioChannelSet> outLayouts = busUtils.getBusCount (false) > 0 ? busUtils.getSupportedBusLayouts (false, 0).supportedLayouts : SortedSet<AudioChannelSet>();
  1088. const int numIns = inLayouts. size();
  1089. const int numOuts = outLayouts.size();
  1090. #if JucePlugin_IsMidiEffect
  1091. // MIDI effect plug-ins do not support any audio channels
  1092. jassert (numIns == 0 && numOuts == 0);
  1093. if (AAX_IComponentDescriptor* const desc = descriptor.NewComponentDescriptor())
  1094. {
  1095. createDescriptor (*desc, 0, busUtils,
  1096. AudioChannelSet::disabled(), AudioChannelSet::disabled(),
  1097. AAX_eStemFormat_Mono, AAX_eStemFormat_Mono);
  1098. check (descriptor.AddComponent (desc));
  1099. }
  1100. #else
  1101. int configIndex = 0;
  1102. for (int inIdx = 0; inIdx < jmax (numIns, 1); ++inIdx)
  1103. {
  1104. for (int outIdx = 0; outIdx < jmax (numOuts, 1); ++outIdx)
  1105. {
  1106. bool success = true;
  1107. if (numIns > 0)
  1108. success = busUtils.juceFilter.setPreferredBusArrangement (true, 0, inLayouts.getReference (inIdx));
  1109. if (numOuts > 0 && success)
  1110. success = busUtils.juceFilter.setPreferredBusArrangement (false, 0, outLayouts.getReference (outIdx));
  1111. // We should never hit this assertion: PluginBusUtilities reported this as supported.
  1112. // Please report this as a bug!
  1113. jassert (success);
  1114. AudioChannelSet inLayout = numIns > 0 ? busUtils.getChannelSet (true, 0) : AudioChannelSet();
  1115. AudioChannelSet outLayout = numOuts > 0 ? busUtils.getChannelSet (false, 0) : AudioChannelSet();
  1116. // if we can't set both in AND out formats simultaneously then ignore this format!
  1117. if (numIns > 0 && numOuts > 0 && (inLayout != inLayouts.getReference (inIdx) || (outLayout != outLayouts.getReference (outIdx))))
  1118. continue;
  1119. AAX_EStemFormat aaxInFormat = getFormatForAudioChannelSet (inLayout, busUtils.busIgnoresLayout (true, 0));
  1120. AAX_EStemFormat aaxOutFormat = getFormatForAudioChannelSet (outLayout, busUtils.busIgnoresLayout (false, 0));
  1121. // does AAX support this layout?
  1122. if (aaxInFormat == AAX_eStemFormat_INT32_MAX || aaxOutFormat == AAX_eStemFormat_INT32_MAX)
  1123. continue;
  1124. // AAX requires a single input if this plug-in is a synth
  1125. #if JucePlugin_IsSynth
  1126. if (numIns == 0)
  1127. aaxInFormat = aaxOutFormat;
  1128. #endif
  1129. if (aaxInFormat == AAX_eStemFormat_None && aaxOutFormat == AAX_eStemFormat_None)
  1130. continue;
  1131. AAXFormatConfiguration aaxFormat (aaxInFormat, aaxOutFormat);
  1132. if (aaxFormats.indexOf (aaxFormat) < 0)
  1133. {
  1134. aaxFormats.add (aaxFormat);
  1135. if (AAX_IComponentDescriptor* const desc = descriptor.NewComponentDescriptor())
  1136. {
  1137. createDescriptor (*desc, configIndex++, busUtils, inLayout, outLayout, aaxInFormat, aaxOutFormat);
  1138. check (descriptor.AddComponent (desc));
  1139. }
  1140. }
  1141. }
  1142. }
  1143. // You don't have any supported layouts
  1144. jassert (configIndex > 0);
  1145. #endif
  1146. }
  1147. };
  1148. //==============================================================================
  1149. AAX_Result JUCE_CDECL GetEffectDescriptions (AAX_ICollection*);
  1150. AAX_Result JUCE_CDECL GetEffectDescriptions (AAX_ICollection* collection)
  1151. {
  1152. ScopedJuceInitialiser_GUI libraryInitialiser;
  1153. if (AAX_IEffectDescriptor* const descriptor = collection->NewDescriptor())
  1154. {
  1155. AAXClasses::getPlugInDescription (*descriptor);
  1156. collection->AddEffect (JUCE_STRINGIFY (JucePlugin_AAXIdentifier), descriptor);
  1157. collection->SetManufacturerName (JucePlugin_Manufacturer);
  1158. collection->AddPackageName (JucePlugin_Desc);
  1159. collection->AddPackageName (JucePlugin_Name);
  1160. collection->SetPackageVersion (JucePlugin_VersionCode);
  1161. return AAX_SUCCESS;
  1162. }
  1163. return AAX_ERROR_NULL_OBJECT;
  1164. }
  1165. #endif