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.

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