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.

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