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.

2049 lines
89KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. #include "../../juce_core/system/juce_TargetPlatform.h"
  20. #include "../utility/juce_CheckSettingMacros.h"
  21. #if JucePlugin_Build_AAX && (JUCE_INCLUDED_AAX_IN_MM || defined (_WIN32) || defined (_WIN64))
  22. #include "../utility/juce_IncludeSystemHeaders.h"
  23. #include "../utility/juce_IncludeModuleHeaders.h"
  24. #include "../utility/juce_WindowsHooks.h"
  25. #include "../utility/juce_FakeMouseMoveGenerator.h"
  26. #ifdef __clang__
  27. #pragma clang diagnostic push
  28. #pragma clang diagnostic ignored "-Wnon-virtual-dtor"
  29. #pragma clang diagnostic ignored "-Wsign-conversion"
  30. #pragma clang diagnostic ignored "-Wextra-semi"
  31. #endif
  32. #ifdef _MSC_VER
  33. #pragma warning (push)
  34. #pragma warning (disable : 4127 4512)
  35. #endif
  36. #include <AAX_Version.h>
  37. static_assert (AAX_SDK_CURRENT_REVISION >= AAX_SDK_2p3p0_REVISION, "JUCE requires AAX SDK version 2.3.0 or higher");
  38. #include <AAX_Exports.cpp>
  39. #include <AAX_ICollection.h>
  40. #include <AAX_IComponentDescriptor.h>
  41. #include <AAX_IEffectDescriptor.h>
  42. #include <AAX_IPropertyMap.h>
  43. #include <AAX_CEffectParameters.h>
  44. #include <AAX_Errors.h>
  45. #include <AAX_CBinaryTaperDelegate.h>
  46. #include <AAX_CBinaryDisplayDelegate.h>
  47. #include <AAX_CLinearTaperDelegate.h>
  48. #include <AAX_CNumberDisplayDelegate.h>
  49. #include <AAX_CEffectGUI.h>
  50. #include <AAX_IViewContainer.h>
  51. #include <AAX_ITransport.h>
  52. #include <AAX_IMIDINode.h>
  53. #include <AAX_UtilsNative.h>
  54. #include <AAX_Enums.h>
  55. #include <AAX_IDescriptionHost.h>
  56. #include <AAX_IFeatureInfo.h>
  57. #include <AAX_UIDs.h>
  58. #ifdef _MSC_VER
  59. #pragma warning (pop)
  60. #endif
  61. #ifdef __clang__
  62. #pragma clang diagnostic pop
  63. #endif
  64. #if JUCE_WINDOWS
  65. #ifndef JucePlugin_AAXLibs_path
  66. #error "You need to define the JucePlugin_AAXLibs_path macro. (This is best done via the Projucer)"
  67. #endif
  68. #if JUCE_64BIT
  69. #define JUCE_AAX_LIB "AAXLibrary_x64"
  70. #else
  71. #define JUCE_AAX_LIB "AAXLibrary"
  72. #endif
  73. #if JUCE_DEBUG
  74. #define JUCE_AAX_LIB_PATH "\\Debug\\"
  75. #define JUCE_AAX_LIB_SUFFIX "_D"
  76. #else
  77. #define JUCE_AAX_LIB_PATH "\\Release\\"
  78. #define JUCE_AAX_LIB_SUFFIX ""
  79. #endif
  80. #pragma comment(lib, JucePlugin_AAXLibs_path JUCE_AAX_LIB_PATH JUCE_AAX_LIB JUCE_AAX_LIB_SUFFIX ".lib")
  81. #endif
  82. #undef check
  83. #include "juce_AAX_Modifier_Injector.h"
  84. using namespace juce;
  85. #ifndef JucePlugin_AAX_Chunk_Identifier
  86. #define JucePlugin_AAX_Chunk_Identifier 'juce'
  87. #endif
  88. const int32_t juceChunkType = JucePlugin_AAX_Chunk_Identifier;
  89. const int maxAAXChannels = 8;
  90. //==============================================================================
  91. namespace AAXClasses
  92. {
  93. static int32 getAAXParamHash (AAX_CParamID paramID) noexcept
  94. {
  95. int32 result = 0;
  96. while (*paramID != 0)
  97. result = (31 * result) + (*paramID++);
  98. return result;
  99. }
  100. static void check (AAX_Result result)
  101. {
  102. jassert (result == AAX_SUCCESS); ignoreUnused (result);
  103. }
  104. static bool isBypassParam (AAX_CParamID paramID) noexcept
  105. {
  106. return AAX::IsParameterIDEqual (paramID, cDefaultMasterBypassID) != 0;
  107. }
  108. // maps a channel index of an AAX format to an index of a juce format
  109. struct AAXChannelStreamOrder
  110. {
  111. AAX_EStemFormat aaxStemFormat;
  112. AudioChannelSet::ChannelType speakerOrder[10];
  113. };
  114. static AAXChannelStreamOrder aaxChannelOrder[] =
  115. {
  116. {AAX_eStemFormat_Mono, {AudioChannelSet::centre, AudioChannelSet::unknown, AudioChannelSet::unknown, AudioChannelSet::unknown, AudioChannelSet::unknown, AudioChannelSet::unknown, AudioChannelSet::unknown, AudioChannelSet::unknown, AudioChannelSet::unknown, AudioChannelSet::unknown}},
  117. {AAX_eStemFormat_Stereo, {AudioChannelSet::left, AudioChannelSet::right, AudioChannelSet::unknown, AudioChannelSet::unknown, AudioChannelSet::unknown, AudioChannelSet::unknown, AudioChannelSet::unknown, AudioChannelSet::unknown, AudioChannelSet::unknown, AudioChannelSet::unknown}},
  118. {AAX_eStemFormat_LCR, {AudioChannelSet::left, AudioChannelSet::centre, AudioChannelSet::right, AudioChannelSet::unknown, AudioChannelSet::unknown, AudioChannelSet::unknown, AudioChannelSet::unknown, AudioChannelSet::unknown, AudioChannelSet::unknown, AudioChannelSet::unknown}},
  119. {AAX_eStemFormat_LCRS, {AudioChannelSet::left, AudioChannelSet::centre, AudioChannelSet::right, AudioChannelSet::centreSurround, AudioChannelSet::unknown, AudioChannelSet::unknown, AudioChannelSet::unknown, AudioChannelSet::unknown, AudioChannelSet::unknown, AudioChannelSet::unknown}},
  120. {AAX_eStemFormat_Quad, {AudioChannelSet::left, AudioChannelSet::right, AudioChannelSet::leftSurround, AudioChannelSet::rightSurround, AudioChannelSet::unknown, AudioChannelSet::unknown, AudioChannelSet::unknown, AudioChannelSet::unknown, AudioChannelSet::unknown, AudioChannelSet::unknown}},
  121. {AAX_eStemFormat_5_0, {AudioChannelSet::left, AudioChannelSet::centre, AudioChannelSet::right, AudioChannelSet::leftSurround, AudioChannelSet::rightSurround, AudioChannelSet::unknown, AudioChannelSet::unknown, AudioChannelSet::unknown, AudioChannelSet::unknown, AudioChannelSet::unknown}},
  122. {AAX_eStemFormat_5_1, {AudioChannelSet::left, AudioChannelSet::centre, AudioChannelSet::right, AudioChannelSet::leftSurround, AudioChannelSet::rightSurround, AudioChannelSet::LFE, AudioChannelSet::unknown, AudioChannelSet::unknown, AudioChannelSet::unknown, AudioChannelSet::unknown}},
  123. {AAX_eStemFormat_6_0, {AudioChannelSet::left, AudioChannelSet::centre, AudioChannelSet::right, AudioChannelSet::leftSurround, AudioChannelSet::centreSurround, AudioChannelSet::rightSurround, AudioChannelSet::unknown, AudioChannelSet::unknown, AudioChannelSet::unknown, AudioChannelSet::unknown}},
  124. {AAX_eStemFormat_6_1, {AudioChannelSet::left, AudioChannelSet::centre, AudioChannelSet::right, AudioChannelSet::leftSurround, AudioChannelSet::centreSurround, AudioChannelSet::rightSurround, AudioChannelSet::LFE, AudioChannelSet::unknown, AudioChannelSet::unknown, AudioChannelSet::unknown}},
  125. {AAX_eStemFormat_7_0_SDDS, {AudioChannelSet::left, AudioChannelSet::leftCentre, AudioChannelSet::centre, AudioChannelSet::rightCentre, AudioChannelSet::right, AudioChannelSet::leftSurround, AudioChannelSet::rightSurround, AudioChannelSet::unknown, AudioChannelSet::unknown, AudioChannelSet::unknown}},
  126. {AAX_eStemFormat_7_0_DTS, {AudioChannelSet::left, AudioChannelSet::centre, AudioChannelSet::right, AudioChannelSet::leftSurroundSide, AudioChannelSet::rightSurroundSide, AudioChannelSet::leftSurroundRear, AudioChannelSet::rightSurroundRear, AudioChannelSet::unknown, AudioChannelSet::unknown, AudioChannelSet::unknown}},
  127. {AAX_eStemFormat_7_1_SDDS, {AudioChannelSet::left, AudioChannelSet::leftCentre, AudioChannelSet::centre, AudioChannelSet::rightCentre, AudioChannelSet::right, AudioChannelSet::leftSurround, AudioChannelSet::rightSurround, AudioChannelSet::LFE, AudioChannelSet::unknown, AudioChannelSet::unknown}},
  128. {AAX_eStemFormat_7_1_DTS, {AudioChannelSet::left, AudioChannelSet::centre, AudioChannelSet::right, AudioChannelSet::leftSurroundSide, AudioChannelSet::rightSurroundSide, AudioChannelSet::leftSurroundRear, AudioChannelSet::rightSurroundRear, AudioChannelSet::LFE, AudioChannelSet::unknown, AudioChannelSet::unknown}},
  129. {AAX_eStemFormat_7_0_2, {AudioChannelSet::left, AudioChannelSet::centre, AudioChannelSet::right, AudioChannelSet::leftSurroundSide, AudioChannelSet::rightSurroundSide, AudioChannelSet::leftSurroundRear, AudioChannelSet::rightSurroundRear, AudioChannelSet::topSideLeft, AudioChannelSet::topSideRight, AudioChannelSet::unknown}},
  130. {AAX_eStemFormat_7_1_2, {AudioChannelSet::left, AudioChannelSet::centre, AudioChannelSet::right, AudioChannelSet::leftSurroundSide, AudioChannelSet::rightSurroundSide, AudioChannelSet::leftSurroundRear, AudioChannelSet::rightSurroundRear, AudioChannelSet::LFE, AudioChannelSet::topSideLeft, AudioChannelSet::topSideRight}},
  131. {AAX_eStemFormat_None, {AudioChannelSet::unknown, AudioChannelSet::unknown, AudioChannelSet::unknown, AudioChannelSet::unknown, AudioChannelSet::unknown, AudioChannelSet::unknown, AudioChannelSet::unknown, AudioChannelSet::unknown, AudioChannelSet::unknown, AudioChannelSet::unknown}},
  132. };
  133. static AAX_EStemFormat aaxFormats[] =
  134. {
  135. AAX_eStemFormat_Mono,
  136. AAX_eStemFormat_Stereo,
  137. AAX_eStemFormat_LCR,
  138. AAX_eStemFormat_LCRS,
  139. AAX_eStemFormat_Quad,
  140. AAX_eStemFormat_5_0,
  141. AAX_eStemFormat_5_1,
  142. AAX_eStemFormat_6_0,
  143. AAX_eStemFormat_6_1,
  144. AAX_eStemFormat_7_0_SDDS,
  145. AAX_eStemFormat_7_1_SDDS,
  146. AAX_eStemFormat_7_0_DTS,
  147. AAX_eStemFormat_7_1_DTS,
  148. AAX_eStemFormat_7_0_2,
  149. AAX_eStemFormat_7_1_2
  150. };
  151. static AAX_EStemFormat getFormatForAudioChannelSet (const AudioChannelSet& set, bool ignoreLayout) noexcept
  152. {
  153. // if the plug-in ignores layout, it is ok to convert between formats only by their numchannnels
  154. if (ignoreLayout)
  155. {
  156. switch (set.size())
  157. {
  158. case 0: return AAX_eStemFormat_None;
  159. case 1: return AAX_eStemFormat_Mono;
  160. case 2: return AAX_eStemFormat_Stereo;
  161. case 3: return AAX_eStemFormat_LCR;
  162. case 4: return AAX_eStemFormat_Quad;
  163. case 5: return AAX_eStemFormat_5_0;
  164. case 6: return AAX_eStemFormat_5_1;
  165. case 7: return AAX_eStemFormat_7_0_DTS;
  166. case 8: return AAX_eStemFormat_7_1_DTS;
  167. case 9: return AAX_eStemFormat_7_0_2;
  168. case 10: return AAX_eStemFormat_7_1_2;
  169. default:
  170. break;
  171. }
  172. return AAX_eStemFormat_INT32_MAX;
  173. }
  174. if (set == AudioChannelSet::disabled()) return AAX_eStemFormat_None;
  175. if (set == AudioChannelSet::mono()) return AAX_eStemFormat_Mono;
  176. if (set == AudioChannelSet::stereo()) return AAX_eStemFormat_Stereo;
  177. if (set == AudioChannelSet::createLCR()) return AAX_eStemFormat_LCR;
  178. if (set == AudioChannelSet::createLCRS()) return AAX_eStemFormat_LCRS;
  179. if (set == AudioChannelSet::quadraphonic()) return AAX_eStemFormat_Quad;
  180. if (set == AudioChannelSet::create5point0()) return AAX_eStemFormat_5_0;
  181. if (set == AudioChannelSet::create5point1()) return AAX_eStemFormat_5_1;
  182. if (set == AudioChannelSet::create6point0()) return AAX_eStemFormat_6_0;
  183. if (set == AudioChannelSet::create6point1()) return AAX_eStemFormat_6_1;
  184. if (set == AudioChannelSet::create7point0()) return AAX_eStemFormat_7_0_DTS;
  185. if (set == AudioChannelSet::create7point1()) return AAX_eStemFormat_7_1_DTS;
  186. if (set == AudioChannelSet::create7point0SDDS()) return AAX_eStemFormat_7_0_SDDS;
  187. if (set == AudioChannelSet::create7point1SDDS()) return AAX_eStemFormat_7_1_SDDS;
  188. if (set == AudioChannelSet::create7point0point2()) return AAX_eStemFormat_7_0_2;
  189. if (set == AudioChannelSet::create7point1point2()) return AAX_eStemFormat_7_1_2;
  190. return AAX_eStemFormat_INT32_MAX;
  191. }
  192. static AudioChannelSet channelSetFromStemFormat (AAX_EStemFormat format, bool ignoreLayout) noexcept
  193. {
  194. if (! ignoreLayout)
  195. {
  196. switch (format)
  197. {
  198. case AAX_eStemFormat_None: return AudioChannelSet::disabled();
  199. case AAX_eStemFormat_Mono: return AudioChannelSet::mono();
  200. case AAX_eStemFormat_Stereo: return AudioChannelSet::stereo();
  201. case AAX_eStemFormat_LCR: return AudioChannelSet::createLCR();
  202. case AAX_eStemFormat_LCRS: return AudioChannelSet::createLCRS();
  203. case AAX_eStemFormat_Quad: return AudioChannelSet::quadraphonic();
  204. case AAX_eStemFormat_5_0: return AudioChannelSet::create5point0();
  205. case AAX_eStemFormat_5_1: return AudioChannelSet::create5point1();
  206. case AAX_eStemFormat_6_0: return AudioChannelSet::create6point0();
  207. case AAX_eStemFormat_6_1: return AudioChannelSet::create6point1();
  208. case AAX_eStemFormat_7_0_SDDS: return AudioChannelSet::create7point0SDDS();
  209. case AAX_eStemFormat_7_0_DTS: return AudioChannelSet::create7point0();
  210. case AAX_eStemFormat_7_1_SDDS: return AudioChannelSet::create7point1SDDS();
  211. case AAX_eStemFormat_7_1_DTS: return AudioChannelSet::create7point1();
  212. case AAX_eStemFormat_7_0_2: return AudioChannelSet::create7point0point2();
  213. case AAX_eStemFormat_7_1_2: return AudioChannelSet::create7point1point2();
  214. default:
  215. break;
  216. }
  217. return AudioChannelSet::disabled();
  218. }
  219. return AudioChannelSet::discreteChannels (jmax (0, static_cast<int> (AAX_STEM_FORMAT_CHANNEL_COUNT (format))));
  220. }
  221. static Colour getColourFromHighlightEnum (AAX_EHighlightColor colour) noexcept
  222. {
  223. switch (colour)
  224. {
  225. case AAX_eHighlightColor_Red: return Colours::red;
  226. case AAX_eHighlightColor_Blue: return Colours::blue;
  227. case AAX_eHighlightColor_Green: return Colours::green;
  228. case AAX_eHighlightColor_Yellow: return Colours::yellow;
  229. default: jassertfalse; break;
  230. }
  231. return Colours::black;
  232. }
  233. static int juceChannelIndexToAax (int juceIndex, const AudioChannelSet& channelSet)
  234. {
  235. AAX_EStemFormat currentLayout = getFormatForAudioChannelSet (channelSet, false);
  236. int layoutIndex;
  237. for (layoutIndex = 0; aaxChannelOrder[layoutIndex].aaxStemFormat != currentLayout; ++layoutIndex)
  238. if (aaxChannelOrder[layoutIndex].aaxStemFormat == 0) return juceIndex;
  239. const AAXChannelStreamOrder& channelOrder = aaxChannelOrder[layoutIndex];
  240. const AudioChannelSet::ChannelType channelType = channelSet.getTypeOfChannel (static_cast<int> (juceIndex));
  241. auto numSpeakers = numElementsInArray (channelOrder.speakerOrder);
  242. for (int i = 0; i < numSpeakers && channelOrder.speakerOrder[i] != 0; ++i)
  243. if (channelOrder.speakerOrder[i] == channelType)
  244. return i;
  245. return juceIndex;
  246. }
  247. //==============================================================================
  248. class JuceAAX_Processor;
  249. struct PluginInstanceInfo
  250. {
  251. PluginInstanceInfo (JuceAAX_Processor& p) : parameters (p) {}
  252. JuceAAX_Processor& parameters;
  253. JUCE_DECLARE_NON_COPYABLE (PluginInstanceInfo)
  254. };
  255. //==============================================================================
  256. struct JUCEAlgorithmContext
  257. {
  258. float** inputChannels;
  259. float** outputChannels;
  260. int32_t* bufferSize;
  261. int32_t* bypass;
  262. #if JucePlugin_WantsMidiInput || JucePlugin_IsMidiEffect
  263. AAX_IMIDINode* midiNodeIn;
  264. #endif
  265. #if JucePlugin_ProducesMidiOutput || JucePlugin_IsSynth || JucePlugin_IsMidiEffect
  266. AAX_IMIDINode* midiNodeOut;
  267. #endif
  268. PluginInstanceInfo* pluginInstance;
  269. int32_t* isPrepared;
  270. float* const* meterTapBuffers;
  271. int32_t* sideChainBuffers;
  272. };
  273. struct JUCEAlgorithmIDs
  274. {
  275. enum
  276. {
  277. inputChannels = AAX_FIELD_INDEX (JUCEAlgorithmContext, inputChannels),
  278. outputChannels = AAX_FIELD_INDEX (JUCEAlgorithmContext, outputChannels),
  279. bufferSize = AAX_FIELD_INDEX (JUCEAlgorithmContext, bufferSize),
  280. bypass = AAX_FIELD_INDEX (JUCEAlgorithmContext, bypass),
  281. #if JucePlugin_WantsMidiInput || JucePlugin_IsMidiEffect
  282. midiNodeIn = AAX_FIELD_INDEX (JUCEAlgorithmContext, midiNodeIn),
  283. #endif
  284. #if JucePlugin_ProducesMidiOutput || JucePlugin_IsSynth || JucePlugin_IsMidiEffect
  285. midiNodeOut = AAX_FIELD_INDEX (JUCEAlgorithmContext, midiNodeOut),
  286. #endif
  287. pluginInstance = AAX_FIELD_INDEX (JUCEAlgorithmContext, pluginInstance),
  288. preparedFlag = AAX_FIELD_INDEX (JUCEAlgorithmContext, isPrepared),
  289. meterTapBuffers = AAX_FIELD_INDEX (JUCEAlgorithmContext, meterTapBuffers),
  290. sideChainBuffers = AAX_FIELD_INDEX (JUCEAlgorithmContext, sideChainBuffers)
  291. };
  292. };
  293. #if JucePlugin_WantsMidiInput || JucePlugin_IsMidiEffect
  294. static AAX_IMIDINode* getMidiNodeIn (const JUCEAlgorithmContext& c) noexcept { return c.midiNodeIn; }
  295. #else
  296. static AAX_IMIDINode* getMidiNodeIn (const JUCEAlgorithmContext&) noexcept { return nullptr; }
  297. #endif
  298. #if JucePlugin_ProducesMidiOutput || JucePlugin_IsSynth || JucePlugin_IsMidiEffect
  299. AAX_IMIDINode* midiNodeOut;
  300. static AAX_IMIDINode* getMidiNodeOut (const JUCEAlgorithmContext& c) noexcept { return c.midiNodeOut; }
  301. #else
  302. static AAX_IMIDINode* getMidiNodeOut (const JUCEAlgorithmContext&) noexcept { return nullptr; }
  303. #endif
  304. //==============================================================================
  305. class JuceAAX_Processor;
  306. class JuceAAX_GUI : public AAX_CEffectGUI, public ModifierKeyProvider
  307. {
  308. public:
  309. JuceAAX_GUI() {}
  310. ~JuceAAX_GUI() { DeleteViewContainer(); }
  311. static AAX_IEffectGUI* AAX_CALLBACK Create() { return new JuceAAX_GUI(); }
  312. void CreateViewContents() override;
  313. void CreateViewContainer() override
  314. {
  315. CreateViewContents();
  316. if (void* nativeViewToAttachTo = GetViewContainerPtr())
  317. {
  318. #if JUCE_MAC
  319. if (GetViewContainerType() == AAX_eViewContainer_Type_NSView)
  320. #else
  321. if (GetViewContainerType() == AAX_eViewContainer_Type_HWND)
  322. #endif
  323. {
  324. component->setVisible (true);
  325. component->addToDesktop (0, nativeViewToAttachTo);
  326. if (ModifierKeyReceiver* modReceiver = dynamic_cast<ModifierKeyReceiver*> (component->getPeer()))
  327. modReceiver->setModifierKeyProvider (this);
  328. }
  329. }
  330. }
  331. void DeleteViewContainer() override
  332. {
  333. if (component != nullptr)
  334. {
  335. JUCE_AUTORELEASEPOOL
  336. {
  337. if (ModifierKeyReceiver* modReceiver = dynamic_cast<ModifierKeyReceiver*> (component->getPeer()))
  338. modReceiver->removeModifierKeyProvider();
  339. component->removeFromDesktop();
  340. component = nullptr;
  341. }
  342. }
  343. }
  344. AAX_Result GetViewSize (AAX_Point* viewSize) const override
  345. {
  346. if (component != nullptr)
  347. {
  348. viewSize->horz = (float) component->getWidth();
  349. viewSize->vert = (float) component->getHeight();
  350. return AAX_SUCCESS;
  351. }
  352. return AAX_ERROR_NULL_OBJECT;
  353. }
  354. AAX_Result ParameterUpdated (AAX_CParamID) override
  355. {
  356. return AAX_SUCCESS;
  357. }
  358. AAX_Result SetControlHighlightInfo (AAX_CParamID paramID, AAX_CBoolean isHighlighted, AAX_EHighlightColor colour) override
  359. {
  360. if (component != nullptr && component->pluginEditor != nullptr)
  361. {
  362. if (! isBypassParam (paramID))
  363. {
  364. AudioProcessorEditor::ParameterControlHighlightInfo info;
  365. info.parameterIndex = getParamIndexFromID (paramID);
  366. info.isHighlighted = (isHighlighted != 0);
  367. info.suggestedColour = getColourFromHighlightEnum (colour);
  368. component->pluginEditor->setControlHighlight (info);
  369. }
  370. return AAX_SUCCESS;
  371. }
  372. return AAX_ERROR_NULL_OBJECT;
  373. }
  374. int getWin32Modifiers() const override
  375. {
  376. int modifierFlags = 0;
  377. if (const AAX_IViewContainer* viewContainer = GetViewContainer())
  378. {
  379. uint32 aaxViewMods = 0;
  380. const_cast<AAX_IViewContainer*>(viewContainer)->GetModifiers (&aaxViewMods);
  381. if ((aaxViewMods & AAX_eModifiers_Shift) != 0) modifierFlags |= ModifierKeys::shiftModifier;
  382. if ((aaxViewMods & AAX_eModifiers_Alt ) != 0) modifierFlags |= ModifierKeys::altModifier;
  383. }
  384. return modifierFlags;
  385. }
  386. private:
  387. //==============================================================================
  388. int getParamIndexFromID (AAX_CParamID paramID) const noexcept;
  389. AAX_CParamID getAAXParamIDFromJuceIndex (int index) const noexcept;
  390. //==============================================================================
  391. struct ContentWrapperComponent : public Component
  392. {
  393. ContentWrapperComponent (JuceAAX_GUI& gui, AudioProcessor& plugin)
  394. : owner (gui)
  395. {
  396. setOpaque (true);
  397. setBroughtToFrontOnMouseClick (true);
  398. addAndMakeVisible (pluginEditor = plugin.createEditorIfNeeded());
  399. if (pluginEditor != nullptr)
  400. {
  401. setBounds (pluginEditor->getLocalBounds());
  402. pluginEditor->addMouseListener (this, true);
  403. }
  404. ignoreUnused (fakeMouseGenerator);
  405. }
  406. ~ContentWrapperComponent()
  407. {
  408. if (pluginEditor != nullptr)
  409. {
  410. PopupMenu::dismissAllActiveMenus();
  411. pluginEditor->removeMouseListener (this);
  412. pluginEditor->processor.editorBeingDeleted (pluginEditor);
  413. }
  414. }
  415. void paint (Graphics& g) override
  416. {
  417. g.fillAll (Colours::black);
  418. }
  419. template <typename MethodType>
  420. void callMouseMethod (const MouseEvent& e, MethodType method)
  421. {
  422. if (AAX_IViewContainer* vc = owner.GetViewContainer())
  423. {
  424. const int parameterIndex = pluginEditor->getControlParameterIndex (*e.eventComponent);
  425. if (AAX_CParamID aaxParamID = owner.getAAXParamIDFromJuceIndex (parameterIndex))
  426. {
  427. uint32_t mods = 0;
  428. vc->GetModifiers (&mods);
  429. (vc->*method) (aaxParamID, mods);
  430. }
  431. }
  432. }
  433. void mouseDown (const MouseEvent& e) override { callMouseMethod (e, &AAX_IViewContainer::HandleParameterMouseDown); }
  434. void mouseUp (const MouseEvent& e) override { callMouseMethod (e, &AAX_IViewContainer::HandleParameterMouseUp); }
  435. void mouseDrag (const MouseEvent& e) override { callMouseMethod (e, &AAX_IViewContainer::HandleParameterMouseDrag); }
  436. void childBoundsChanged (Component*) override
  437. {
  438. if (pluginEditor != nullptr)
  439. {
  440. const int w = pluginEditor->getWidth();
  441. const int h = pluginEditor->getHeight();
  442. setSize (w, h);
  443. AAX_Point newSize ((float) h, (float) w);
  444. owner.GetViewContainer()->SetViewSize (newSize);
  445. }
  446. }
  447. ScopedPointer<AudioProcessorEditor> pluginEditor;
  448. JuceAAX_GUI& owner;
  449. #if JUCE_WINDOWS
  450. WindowsHooks hooks;
  451. #endif
  452. FakeMouseMoveGenerator fakeMouseGenerator;
  453. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ContentWrapperComponent)
  454. };
  455. ScopedPointer<ContentWrapperComponent> component;
  456. ScopedJuceInitialiser_GUI libraryInitialiser;
  457. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (JuceAAX_GUI)
  458. };
  459. static void AAX_CALLBACK algorithmProcessCallback (JUCEAlgorithmContext* const instancesBegin[], const void* const instancesEnd);
  460. //==============================================================================
  461. class JuceAAX_Processor : public AAX_CEffectParameters,
  462. public juce::AudioPlayHead,
  463. public AudioProcessorListener,
  464. private AsyncUpdater
  465. {
  466. public:
  467. JuceAAX_Processor()
  468. : pluginInstance (createPluginFilterOfType (AudioProcessor::wrapperType_AAX)),
  469. isPrepared (false),
  470. sampleRate (0), lastBufferSize (1024), maxBufferSize (1024),
  471. hasSidechain (false), canDisableSidechain (false)
  472. {
  473. pluginInstance->setPlayHead (this);
  474. pluginInstance->addListener (this);
  475. rebuildChannelMapArrays();
  476. AAX_CEffectParameters::GetNumberOfChunks (&juceChunkIndex);
  477. }
  478. static AAX_CEffectParameters* AAX_CALLBACK Create()
  479. {
  480. PluginHostType::jucePlugInClientCurrentWrapperType = AudioProcessor::wrapperType_AAX;
  481. return new JuceAAX_Processor();
  482. }
  483. AAX_Result Uninitialize() override
  484. {
  485. cancelPendingUpdate();
  486. if (isPrepared && pluginInstance != nullptr)
  487. {
  488. isPrepared = false;
  489. processingSidechainChange.set (0);
  490. pluginInstance->releaseResources();
  491. }
  492. return AAX_CEffectParameters::Uninitialize();
  493. }
  494. AAX_Result EffectInit() override
  495. {
  496. cancelPendingUpdate();
  497. AAX_Result err;
  498. check (Controller()->GetSampleRate (&sampleRate));
  499. processingSidechainChange.set (0);
  500. if ((err = preparePlugin()) != AAX_SUCCESS)
  501. return err;
  502. addBypassParameter();
  503. addAudioProcessorParameters();
  504. return AAX_SUCCESS;
  505. }
  506. AAX_Result GetNumberOfChunks (int32_t* numChunks) const override
  507. {
  508. // The juceChunk is the last chunk.
  509. *numChunks = juceChunkIndex + 1;
  510. return AAX_SUCCESS;
  511. }
  512. AAX_Result GetChunkIDFromIndex (int32_t index, AAX_CTypeID* chunkID) const override
  513. {
  514. if (index != juceChunkIndex)
  515. return AAX_CEffectParameters::GetChunkIDFromIndex (index, chunkID);
  516. *chunkID = juceChunkType;
  517. return AAX_SUCCESS;
  518. }
  519. AAX_Result GetChunkSize (AAX_CTypeID chunkID, uint32_t* oSize) const override
  520. {
  521. if (chunkID != juceChunkType)
  522. return AAX_CEffectParameters::GetChunkSize (chunkID, oSize);
  523. auto& chunkMemoryBlock = perThreadFilterData.get();
  524. chunkMemoryBlock.data.reset();
  525. pluginInstance->getStateInformation (chunkMemoryBlock.data);
  526. chunkMemoryBlock.isValid = true;
  527. *oSize = (uint32_t) chunkMemoryBlock.data.getSize();
  528. return AAX_SUCCESS;
  529. }
  530. AAX_Result GetChunk (AAX_CTypeID chunkID, AAX_SPlugInChunk* oChunk) const override
  531. {
  532. if (chunkID != juceChunkType)
  533. return AAX_CEffectParameters::GetChunk (chunkID, oChunk);
  534. auto& chunkMemoryBlock = perThreadFilterData.get();
  535. if (! chunkMemoryBlock.isValid)
  536. return 20700; // AAX_ERROR_PLUGIN_API_INVALID_THREAD
  537. oChunk->fSize = (int32_t) chunkMemoryBlock.data.getSize();
  538. chunkMemoryBlock.data.copyTo (oChunk->fData, 0, chunkMemoryBlock.data.getSize());
  539. chunkMemoryBlock.isValid = false;
  540. return AAX_SUCCESS;
  541. }
  542. AAX_Result SetChunk (AAX_CTypeID chunkID, const AAX_SPlugInChunk* chunk) override
  543. {
  544. if (chunkID != juceChunkType)
  545. return AAX_CEffectParameters::SetChunk (chunkID, chunk);
  546. pluginInstance->setStateInformation ((void*) chunk->fData, chunk->fSize);
  547. // Notify Pro Tools that the parameters were updated.
  548. // Without it a bug happens in these circumstances:
  549. // * A preset is saved with the RTAS version of the plugin (".tfx" preset format).
  550. // * The preset is loaded in PT 10 using the AAX version.
  551. // * The session is then saved, and closed.
  552. // * The saved session is loaded, but acting as if the preset was never loaded.
  553. const int numParameters = pluginInstance->getNumParameters();
  554. for (int i = 0; i < numParameters; ++i)
  555. if (AAX_CParamID paramID = getAAXParamIDFromJuceIndex(i))
  556. SetParameterNormalizedValue (paramID, (double) pluginInstance->getParameter(i));
  557. return AAX_SUCCESS;
  558. }
  559. AAX_Result ResetFieldData (AAX_CFieldIndex fieldIndex, void* data, uint32_t dataSize) const override
  560. {
  561. switch (fieldIndex)
  562. {
  563. case JUCEAlgorithmIDs::pluginInstance:
  564. {
  565. const size_t numObjects = dataSize / sizeof (PluginInstanceInfo);
  566. PluginInstanceInfo* const objects = static_cast<PluginInstanceInfo*> (data);
  567. jassert (numObjects == 1); // not sure how to handle more than one..
  568. for (size_t i = 0; i < numObjects; ++i)
  569. new (objects + i) PluginInstanceInfo (const_cast<JuceAAX_Processor&> (*this));
  570. break;
  571. }
  572. case JUCEAlgorithmIDs::preparedFlag:
  573. {
  574. const_cast<JuceAAX_Processor*>(this)->preparePlugin();
  575. const size_t numObjects = dataSize / sizeof (uint32_t);
  576. uint32_t* const objects = static_cast<uint32_t*> (data);
  577. for (size_t i = 0; i < numObjects; ++i)
  578. new (objects + i) uint32_t (1);
  579. break;
  580. }
  581. case JUCEAlgorithmIDs::meterTapBuffers:
  582. {
  583. // this is a dummy field only when there are no aaxMeters
  584. jassert (aaxMeters.size() == 0);
  585. {
  586. const size_t numObjects = dataSize / sizeof (float*);
  587. float** const objects = static_cast<float**> (data);
  588. for (size_t i = 0; i < numObjects; ++i)
  589. new (objects + i) (float*) (nullptr);
  590. }
  591. break;
  592. }
  593. }
  594. return AAX_SUCCESS;
  595. }
  596. AAX_Result UpdateParameterNormalizedValue (AAX_CParamID paramID, double value, AAX_EUpdateSource source) override
  597. {
  598. AAX_Result result = AAX_CEffectParameters::UpdateParameterNormalizedValue (paramID, value, source);
  599. if (! isBypassParam (paramID))
  600. pluginInstance->setParameter (getParamIndexFromID (paramID), (float) value);
  601. return result;
  602. }
  603. AAX_Result GetParameterValueFromString (AAX_CParamID paramID, double* result, const AAX_IString& text) const override
  604. {
  605. if (isBypassParam (paramID))
  606. {
  607. *result = (text.Get()[0] == 'B') ? 1 : 0;
  608. return AAX_SUCCESS;
  609. }
  610. if (AudioProcessorParameter* param = pluginInstance->getParameters() [getParamIndexFromID (paramID)])
  611. {
  612. *result = param->getValueForText (text.Get());
  613. return AAX_SUCCESS;
  614. }
  615. return AAX_CEffectParameters::GetParameterValueFromString (paramID, result, text);
  616. }
  617. AAX_Result GetParameterStringFromValue (AAX_CParamID paramID, double value, AAX_IString* result, int32_t maxLen) const override
  618. {
  619. if (isBypassParam (paramID))
  620. {
  621. result->Set (value == 0 ? "Off" : (maxLen >= 8 ? "Bypassed" : "Byp"));
  622. }
  623. else
  624. {
  625. const int paramIndex = getParamIndexFromID (paramID);
  626. juce::String text;
  627. if (AudioProcessorParameter* param = pluginInstance->getParameters() [paramIndex])
  628. text = param->getText ((float) value, maxLen);
  629. else
  630. text = pluginInstance->getParameterText (paramIndex, maxLen);
  631. result->Set (text.toRawUTF8());
  632. }
  633. return AAX_SUCCESS;
  634. }
  635. AAX_Result GetParameterNumberofSteps (AAX_CParamID paramID, int32_t* result) const
  636. {
  637. if (isBypassParam (paramID))
  638. *result = 2;
  639. else
  640. *result = pluginInstance->getParameterNumSteps (getParamIndexFromID (paramID));
  641. return AAX_SUCCESS;
  642. }
  643. AAX_Result GetParameterNormalizedValue (AAX_CParamID paramID, double* result) const override
  644. {
  645. if (isBypassParam (paramID))
  646. return AAX_CEffectParameters::GetParameterNormalizedValue (paramID, result);
  647. *result = pluginInstance->getParameter (getParamIndexFromID (paramID));
  648. return AAX_SUCCESS;
  649. }
  650. AAX_Result SetParameterNormalizedValue (AAX_CParamID paramID, double newValue) override
  651. {
  652. if (isBypassParam (paramID))
  653. return AAX_CEffectParameters::SetParameterNormalizedValue (paramID, newValue);
  654. if (AAX_IParameter* p = const_cast<AAX_IParameter*> (mParameterManager.GetParameterByID (paramID)))
  655. p->SetValueWithFloat ((float) newValue);
  656. pluginInstance->setParameter (getParamIndexFromID (paramID), (float) newValue);
  657. return AAX_SUCCESS;
  658. }
  659. AAX_Result SetParameterNormalizedRelative (AAX_CParamID paramID, double newDeltaValue) override
  660. {
  661. if (isBypassParam (paramID))
  662. return AAX_CEffectParameters::SetParameterNormalizedRelative (paramID, newDeltaValue);
  663. const int paramIndex = getParamIndexFromID (paramID);
  664. const float newValue = pluginInstance->getParameter (paramIndex) + (float) newDeltaValue;
  665. pluginInstance->setParameter (paramIndex, jlimit (0.0f, 1.0f, newValue));
  666. if (AAX_IParameter* p = const_cast<AAX_IParameter*> (mParameterManager.GetParameterByID (paramID)))
  667. p->SetValueWithFloat (newValue);
  668. return AAX_SUCCESS;
  669. }
  670. AAX_Result GetParameterNameOfLength (AAX_CParamID paramID, AAX_IString* result, int32_t maxLen) const override
  671. {
  672. if (isBypassParam (paramID))
  673. result->Set (maxLen >= 13 ? "Master Bypass"
  674. : (maxLen >= 8 ? "Mast Byp"
  675. : (maxLen >= 6 ? "MstByp" : "MByp")));
  676. else
  677. result->Set (pluginInstance->getParameterName (getParamIndexFromID (paramID), maxLen).toRawUTF8());
  678. return AAX_SUCCESS;
  679. }
  680. AAX_Result GetParameterName (AAX_CParamID paramID, AAX_IString* result) const override
  681. {
  682. if (isBypassParam (paramID))
  683. result->Set ("Master Bypass");
  684. else
  685. result->Set (pluginInstance->getParameterName (getParamIndexFromID (paramID), 31).toRawUTF8());
  686. return AAX_SUCCESS;
  687. }
  688. AAX_Result GetParameterDefaultNormalizedValue (AAX_CParamID paramID, double* result) const override
  689. {
  690. if (! isBypassParam (paramID))
  691. {
  692. *result = (double) pluginInstance->getParameterDefaultValue (getParamIndexFromID (paramID));
  693. jassert (*result >= 0 && *result <= 1.0f);
  694. }
  695. return AAX_SUCCESS;
  696. }
  697. AudioProcessor& getPluginInstance() const noexcept { return *pluginInstance; }
  698. bool getCurrentPosition (juce::AudioPlayHead::CurrentPositionInfo& info) override
  699. {
  700. const AAX_ITransport& transport = *Transport();
  701. info.bpm = 0.0;
  702. check (transport.GetCurrentTempo (&info.bpm));
  703. int32_t num = 4, den = 4;
  704. transport.GetCurrentMeter (&num, &den);
  705. info.timeSigNumerator = (int) num;
  706. info.timeSigDenominator = (int) den;
  707. info.timeInSamples = 0;
  708. if (transport.IsTransportPlaying (&info.isPlaying) != AAX_SUCCESS)
  709. info.isPlaying = false;
  710. if (info.isPlaying
  711. || transport.GetTimelineSelectionStartPosition (&info.timeInSamples) != AAX_SUCCESS)
  712. check (transport.GetCurrentNativeSampleLocation (&info.timeInSamples));
  713. info.timeInSeconds = info.timeInSamples / sampleRate;
  714. int64_t ticks = 0;
  715. check (transport.GetCurrentTickPosition (&ticks));
  716. info.ppqPosition = ticks / 960000.0;
  717. info.isLooping = false;
  718. int64_t loopStartTick = 0, loopEndTick = 0;
  719. check (transport.GetCurrentLoopPosition (&info.isLooping, &loopStartTick, &loopEndTick));
  720. info.ppqLoopStart = loopStartTick / 960000.0;
  721. info.ppqLoopEnd = loopEndTick / 960000.0;
  722. info.editOriginTime = 0;
  723. info.frameRate = AudioPlayHead::fpsUnknown;
  724. AAX_EFrameRate frameRate;
  725. int32_t offset;
  726. if (transport.GetTimeCodeInfo (&frameRate, &offset) == AAX_SUCCESS)
  727. {
  728. double framesPerSec = 24.0;
  729. switch (frameRate)
  730. {
  731. case AAX_eFrameRate_Undeclared: break;
  732. case AAX_eFrameRate_24Frame: info.frameRate = AudioPlayHead::fps24; break;
  733. case AAX_eFrameRate_25Frame: info.frameRate = AudioPlayHead::fps25; framesPerSec = 25.0; break;
  734. case AAX_eFrameRate_2997NonDrop: info.frameRate = AudioPlayHead::fps2997; framesPerSec = 30.0 * 1000.0 / 1001.0; break;
  735. case AAX_eFrameRate_2997DropFrame: info.frameRate = AudioPlayHead::fps2997drop; framesPerSec = 30.0 * 1000.0 / 1001.0; break;
  736. case AAX_eFrameRate_30NonDrop: info.frameRate = AudioPlayHead::fps30; framesPerSec = 30.0; break;
  737. case AAX_eFrameRate_30DropFrame: info.frameRate = AudioPlayHead::fps30drop; framesPerSec = 30.0; break;
  738. case AAX_eFrameRate_23976: info.frameRate = AudioPlayHead::fps23976; framesPerSec = 24.0 * 1000.0 / 1001.0; break;
  739. default: break;
  740. }
  741. info.editOriginTime = offset / framesPerSec;
  742. }
  743. // No way to get these: (?)
  744. info.isRecording = false;
  745. info.ppqPositionOfLastBarStart = 0;
  746. return true;
  747. }
  748. void audioProcessorParameterChanged (AudioProcessor* /*processor*/, int parameterIndex, float newValue) override
  749. {
  750. if (AAX_CParamID paramID = getAAXParamIDFromJuceIndex (parameterIndex))
  751. SetParameterNormalizedValue (paramID, (double) newValue);
  752. }
  753. void audioProcessorChanged (AudioProcessor* processor) override
  754. {
  755. ++mNumPlugInChanges;
  756. check (Controller()->SetSignalLatency (processor->getLatencySamples()));
  757. }
  758. void audioProcessorParameterChangeGestureBegin (AudioProcessor* /*processor*/, int parameterIndex) override
  759. {
  760. if (AAX_CParamID paramID = getAAXParamIDFromJuceIndex (parameterIndex))
  761. TouchParameter (paramID);
  762. }
  763. void audioProcessorParameterChangeGestureEnd (AudioProcessor* /*processor*/, int parameterIndex) override
  764. {
  765. if (AAX_CParamID paramID = getAAXParamIDFromJuceIndex (parameterIndex))
  766. ReleaseParameter (paramID);
  767. }
  768. AAX_Result NotificationReceived (AAX_CTypeID type, const void* data, uint32_t size) override
  769. {
  770. if (type == AAX_eNotificationEvent_EnteringOfflineMode) pluginInstance->setNonRealtime (true);
  771. if (type == AAX_eNotificationEvent_ExitingOfflineMode) pluginInstance->setNonRealtime (false);
  772. if (type == AAX_eNotificationEvent_TrackNameChanged && data != nullptr)
  773. {
  774. AudioProcessor::TrackProperties props;
  775. props.name = static_cast<const AAX_IString*> (data)->Get();
  776. pluginInstance->updateTrackProperties (props);
  777. }
  778. return AAX_CEffectParameters::NotificationReceived (type, data, size);
  779. }
  780. const float* getAudioBufferForInput (const float* const* inputs, const int sidechain, const int mainNumIns, int idx) const noexcept
  781. {
  782. jassert (idx < (mainNumIns + 1));
  783. if (idx < mainNumIns)
  784. return inputs[inputLayoutMap[idx]];
  785. return (sidechain != -1 ? inputs[sidechain] : sideChainBuffer.getData());
  786. }
  787. void process (const float* const* inputs, float* const* outputs, const int sideChainBufferIdx,
  788. const int bufferSize, const bool bypass,
  789. AAX_IMIDINode* midiNodeIn, AAX_IMIDINode* midiNodesOut,
  790. float* const meterBuffers)
  791. {
  792. const int numIns = pluginInstance->getTotalNumInputChannels();
  793. const int numOuts = pluginInstance->getTotalNumOutputChannels();
  794. const int numMeters = aaxMeters.size();
  795. const bool processWantsSidechain = (sideChainBufferIdx != -1);
  796. bool isSuspended = pluginInstance->isSuspended();
  797. if (processingSidechainChange.get() == 0)
  798. {
  799. if (hasSidechain && canDisableSidechain
  800. && (sidechainDesired.get() != 0) != processWantsSidechain)
  801. {
  802. isSuspended = true;
  803. sidechainDesired.set (processWantsSidechain ? 1 : 0);
  804. processingSidechainChange.set (1);
  805. triggerAsyncUpdate();
  806. }
  807. }
  808. else
  809. isSuspended = true;
  810. if (isSuspended)
  811. {
  812. for (int i = 0; i < numOuts; ++i)
  813. FloatVectorOperations::clear (outputs[i], bufferSize);
  814. if (meterBuffers != nullptr)
  815. FloatVectorOperations::clear (meterBuffers, numMeters);
  816. }
  817. else
  818. {
  819. const int mainNumIns = pluginInstance->getMainBusNumInputChannels();
  820. const int sidechain = (pluginInstance->getChannelCountOfBus (true, 1) > 0 ? sideChainBufferIdx : -1);
  821. const int numChans = jmax (numIns, numOuts);
  822. if (numChans == 0) return;
  823. if (channelList.size() <= numChans)
  824. channelList.insertMultiple (-1, nullptr, 1 + numChans - channelList.size());
  825. float** channels = channelList.getRawDataPointer();
  826. if (numOuts >= numIns)
  827. {
  828. for (int i = 0; i < numOuts; ++i)
  829. channels[i] = outputs[outputLayoutMap[i]];
  830. for (int i = 0; i < numIns; ++i)
  831. memcpy (channels[i], getAudioBufferForInput (inputs, sidechain, mainNumIns, i), (size_t) bufferSize * sizeof (float));
  832. for (int i = numIns; i < numOuts; ++i)
  833. zeromem (channels[i], (size_t) bufferSize * sizeof (float));
  834. process (channels, numOuts, bufferSize, bypass, midiNodeIn, midiNodesOut);
  835. }
  836. else
  837. {
  838. for (int i = 0; i < numOuts; ++i)
  839. channels[i] = outputs[outputLayoutMap[i]];
  840. for (int i = 0; i < numOuts; ++i)
  841. memcpy (channels[i], getAudioBufferForInput (inputs, sidechain, mainNumIns, i), (size_t) bufferSize * sizeof (float));
  842. for (int i = numOuts; i < numIns; ++i)
  843. channels[i] = const_cast<float*> (getAudioBufferForInput (inputs, sidechain, mainNumIns, i));
  844. process (channels, numIns, bufferSize, bypass, midiNodeIn, midiNodesOut);
  845. }
  846. if (meterBuffers != nullptr)
  847. {
  848. for (int i = 0; i < numMeters; ++i)
  849. meterBuffers[i] = pluginInstance->getParameter (aaxMeters[i]);
  850. }
  851. }
  852. }
  853. //==============================================================================
  854. // In aax, the format of the aux and sidechain buses need to be fully determined
  855. // by the format on the main buses. This function tried to provide such a mapping.
  856. // Returns false if the in/out main layout is not supported
  857. static bool fullBusesLayoutFromMainLayout (const AudioProcessor& p,
  858. const AudioChannelSet& mainInput, const AudioChannelSet& mainOutput,
  859. AudioProcessor::BusesLayout& fullLayout)
  860. {
  861. AudioProcessor::BusesLayout currentLayout = getDefaultLayout (p, true);
  862. bool success = p.checkBusesLayoutSupported (currentLayout);
  863. jassert (success);
  864. ignoreUnused (success);
  865. const int numInputBuses = p.getBusCount (true);
  866. const int numOutputBuses = p.getBusCount (false);
  867. if (const AudioProcessor::Bus* bus = p.getBus (true, 0))
  868. if (! bus->isLayoutSupported (mainInput, &currentLayout))
  869. return false;
  870. if (const AudioProcessor::Bus* bus = p.getBus (false, 0))
  871. if (! bus->isLayoutSupported (mainOutput, &currentLayout))
  872. return false;
  873. // did this change the input again
  874. if (numInputBuses > 0 && currentLayout.inputBuses.getReference (0) != mainInput)
  875. return false;
  876. #ifdef JucePlugin_PreferredChannelConfigurations
  877. short configs[][2] = {JucePlugin_PreferredChannelConfigurations};
  878. if (! AudioProcessor::containsLayout (currentLayout, configs))
  879. return false;
  880. #endif
  881. bool foundValid = false;
  882. {
  883. AudioProcessor::BusesLayout onlyMains = currentLayout;
  884. for (int i = 1; i < numInputBuses; ++i)
  885. onlyMains.inputBuses.getReference (i) = AudioChannelSet::disabled();
  886. for (int i = 1; i < numOutputBuses; ++i)
  887. onlyMains.outputBuses.getReference (i) = AudioChannelSet::disabled();
  888. if (p.checkBusesLayoutSupported (onlyMains))
  889. {
  890. foundValid = true;
  891. fullLayout = onlyMains;
  892. }
  893. }
  894. if (numInputBuses > 1)
  895. {
  896. // can the first bus be a sidechain or disabled, if not then we can't use this layout combination
  897. if (const AudioProcessor::Bus* bus = p.getBus (true, 1))
  898. if (! bus->isLayoutSupported (AudioChannelSet::mono(), &currentLayout) && ! bus->isLayoutSupported (AudioChannelSet::disabled(), &currentLayout))
  899. return foundValid;
  900. // can all the other inputs be disabled, if not then we can't use this layout combination
  901. for (int i = 2; i < numInputBuses; ++i)
  902. if (const AudioProcessor::Bus* bus = p.getBus (true, i))
  903. if (! bus->isLayoutSupported (AudioChannelSet::disabled(), &currentLayout))
  904. return foundValid;
  905. if (const AudioProcessor::Bus* bus = p.getBus (true, 0))
  906. if (! bus->isLayoutSupported (mainInput, &currentLayout))
  907. return foundValid;
  908. if (const AudioProcessor::Bus* bus = p.getBus (false, 0))
  909. if (! bus->isLayoutSupported (mainOutput, &currentLayout))
  910. return foundValid;
  911. // recheck if the format is correct
  912. if ((numInputBuses > 0 && currentLayout.inputBuses .getReference (0) != mainInput)
  913. || (numOutputBuses > 0 && currentLayout.outputBuses.getReference (0) != mainOutput))
  914. return foundValid;
  915. const AudioChannelSet& sidechainBus = currentLayout.inputBuses.getReference (1);
  916. if (sidechainBus != AudioChannelSet::mono() && sidechainBus != AudioChannelSet::disabled())
  917. return foundValid;
  918. for (int i = 2; i < numInputBuses; ++i)
  919. if (currentLayout.outputBuses.getReference (i) != AudioChannelSet::disabled())
  920. return foundValid;
  921. }
  922. const bool hasSidechain = (numInputBuses > 1 && currentLayout.inputBuses.getReference (1) == AudioChannelSet::mono());
  923. if (hasSidechain)
  924. {
  925. AudioProcessor::BusesLayout onlyMainsAndSidechain = currentLayout;
  926. for (int i = 1; i < numOutputBuses; ++i)
  927. onlyMainsAndSidechain.outputBuses.getReference (i) = AudioChannelSet::disabled();
  928. if (p.checkBusesLayoutSupported (onlyMainsAndSidechain))
  929. {
  930. foundValid = true;
  931. fullLayout = onlyMainsAndSidechain;
  932. }
  933. }
  934. if (numOutputBuses > 1)
  935. {
  936. AudioProcessor::BusesLayout copy = currentLayout;
  937. int maxAuxBuses = jmin (16, numOutputBuses);
  938. for (int i = 1; i < maxAuxBuses; ++i)
  939. copy.outputBuses.getReference (i) = mainOutput;
  940. for (int i = maxAuxBuses; i < numOutputBuses; ++i)
  941. copy.outputBuses.getReference (i) = AudioChannelSet::disabled();
  942. if (p.checkBusesLayoutSupported (copy))
  943. {
  944. fullLayout = copy;
  945. foundValid = true;
  946. }
  947. else
  948. {
  949. for (int i = 1; i < maxAuxBuses; ++i)
  950. if (currentLayout.outputBuses.getReference (i).isDisabled())
  951. return foundValid;
  952. for (int i = maxAuxBuses; i < numOutputBuses; ++i)
  953. if (const AudioProcessor::Bus* bus = p.getBus (false, i))
  954. if (! bus->isLayoutSupported (AudioChannelSet::disabled(), &currentLayout))
  955. return foundValid;
  956. if (const AudioProcessor::Bus* bus = p.getBus (true, 0))
  957. if (! bus->isLayoutSupported (mainInput, &currentLayout))
  958. return foundValid;
  959. if (const AudioProcessor::Bus* bus = p.getBus (false, 0))
  960. if (! bus->isLayoutSupported (mainOutput, &currentLayout))
  961. return foundValid;
  962. if ((numInputBuses > 0 && currentLayout.inputBuses .getReference (0) != mainInput)
  963. || (numOutputBuses > 0 && currentLayout.outputBuses.getReference (0) != mainOutput))
  964. return foundValid;
  965. if (numInputBuses > 1)
  966. {
  967. const AudioChannelSet& sidechainBus = currentLayout.inputBuses.getReference (1);
  968. if (sidechainBus != AudioChannelSet::mono() && sidechainBus != AudioChannelSet::disabled())
  969. return foundValid;
  970. }
  971. for (int i = maxAuxBuses; i < numOutputBuses; ++i)
  972. if (! currentLayout.outputBuses.getReference (i).isDisabled())
  973. return foundValid;
  974. fullLayout = currentLayout;
  975. foundValid = true;
  976. }
  977. }
  978. return foundValid;
  979. }
  980. private:
  981. friend class JuceAAX_GUI;
  982. friend void AAX_CALLBACK AAXClasses::algorithmProcessCallback (JUCEAlgorithmContext* const instancesBegin[], const void* const instancesEnd);
  983. void process (float* const* channels, const int numChans, const int bufferSize,
  984. const bool bypass, AAX_IMIDINode* midiNodeIn, AAX_IMIDINode* midiNodesOut)
  985. {
  986. AudioBuffer<float> buffer (channels, numChans, bufferSize);
  987. midiBuffer.clear();
  988. ignoreUnused (midiNodeIn, midiNodesOut);
  989. #if JucePlugin_WantsMidiInput || JucePlugin_IsMidiEffect
  990. {
  991. AAX_CMidiStream* const midiStream = midiNodeIn->GetNodeBuffer();
  992. const uint32_t numMidiEvents = midiStream->mBufferSize;
  993. for (uint32_t i = 0; i < numMidiEvents; ++i)
  994. {
  995. const AAX_CMidiPacket& m = midiStream->mBuffer[i];
  996. jassert ((int) m.mTimestamp < bufferSize);
  997. midiBuffer.addEvent (m.mData, (int) m.mLength,
  998. jlimit (0, (int) bufferSize - 1, (int) m.mTimestamp));
  999. }
  1000. }
  1001. #endif
  1002. {
  1003. if (lastBufferSize != bufferSize)
  1004. {
  1005. lastBufferSize = bufferSize;
  1006. pluginInstance->setRateAndBufferSizeDetails (sampleRate, bufferSize);
  1007. if (bufferSize > maxBufferSize)
  1008. {
  1009. // we only call prepareToPlay here if the new buffer size is larger than
  1010. // the one used last time prepareToPlay was called.
  1011. // currently, this should never actually happen, because as of Pro Tools 12,
  1012. // the maximum possible value is 1024, and we call prepareToPlay with that
  1013. // value during initialisation.
  1014. pluginInstance->prepareToPlay (sampleRate, bufferSize);
  1015. maxBufferSize = bufferSize;
  1016. sideChainBuffer.calloc (static_cast<size_t> (maxBufferSize));
  1017. }
  1018. }
  1019. const ScopedLock sl (pluginInstance->getCallbackLock());
  1020. if (bypass)
  1021. pluginInstance->processBlockBypassed (buffer, midiBuffer);
  1022. else
  1023. pluginInstance->processBlock (buffer, midiBuffer);
  1024. }
  1025. #if JucePlugin_ProducesMidiOutput || JucePlugin_IsMidiEffect
  1026. {
  1027. const juce::uint8* midiEventData;
  1028. int midiEventSize, midiEventPosition;
  1029. MidiBuffer::Iterator i (midiBuffer);
  1030. AAX_CMidiPacket packet;
  1031. packet.mIsImmediate = false;
  1032. while (i.getNextEvent (midiEventData, midiEventSize, midiEventPosition))
  1033. {
  1034. jassert (isPositiveAndBelow (midiEventPosition, bufferSize));
  1035. if (midiEventSize <= 4)
  1036. {
  1037. packet.mTimestamp = (uint32_t) midiEventPosition;
  1038. packet.mLength = (uint32_t) midiEventSize;
  1039. memcpy (packet.mData, midiEventData, (size_t) midiEventSize);
  1040. check (midiNodesOut->PostMIDIPacket (&packet));
  1041. }
  1042. }
  1043. }
  1044. #endif
  1045. }
  1046. void addBypassParameter()
  1047. {
  1048. AAX_IParameter* masterBypass = new AAX_CParameter<bool> (cDefaultMasterBypassID,
  1049. AAX_CString ("Master Bypass"),
  1050. false,
  1051. AAX_CBinaryTaperDelegate<bool>(),
  1052. AAX_CBinaryDisplayDelegate<bool> ("bypass", "on"),
  1053. true);
  1054. masterBypass->SetNumberOfSteps (2);
  1055. masterBypass->SetType (AAX_eParameterType_Discrete);
  1056. mParameterManager.AddParameter (masterBypass);
  1057. mPacketDispatcher.RegisterPacket (cDefaultMasterBypassID, JUCEAlgorithmIDs::bypass);
  1058. }
  1059. void addAudioProcessorParameters()
  1060. {
  1061. AudioProcessor& audioProcessor = getPluginInstance();
  1062. const int numParameters = audioProcessor.getNumParameters();
  1063. #if JUCE_FORCE_USE_LEGACY_PARAM_IDS
  1064. const bool usingManagedParameters = false;
  1065. #else
  1066. const bool usingManagedParameters = (audioProcessor.getParameters().size() == numParameters);
  1067. #endif
  1068. for (int parameterIndex = 0; parameterIndex < numParameters; ++parameterIndex)
  1069. {
  1070. const AudioProcessorParameter::Category category = audioProcessor.getParameterCategory (parameterIndex);
  1071. aaxParamIDs.add (usingManagedParameters ? audioProcessor.getParameterID (parameterIndex)
  1072. : String (parameterIndex));
  1073. AAX_CString paramName (audioProcessor.getParameterName (parameterIndex, 31).toRawUTF8());
  1074. AAX_CParamID paramID = aaxParamIDs.getReference (parameterIndex).getCharPointer();
  1075. paramMap.set (AAXClasses::getAAXParamHash (paramID), parameterIndex);
  1076. // is this a meter?
  1077. if (((category & 0xffff0000) >> 16) == 2)
  1078. {
  1079. aaxMeters.add (parameterIndex);
  1080. continue;
  1081. }
  1082. AAX_IParameter* parameter
  1083. = new AAX_CParameter<float> (paramID,
  1084. paramName,
  1085. audioProcessor.getParameterDefaultValue (parameterIndex),
  1086. AAX_CLinearTaperDelegate<float, 0>(),
  1087. AAX_CNumberDisplayDelegate<float, 3>(),
  1088. audioProcessor.isParameterAutomatable (parameterIndex));
  1089. parameter->AddShortenedName (audioProcessor.getParameterName (parameterIndex, 4).toRawUTF8());
  1090. const int parameterNumSteps = audioProcessor.getParameterNumSteps (parameterIndex);
  1091. parameter->SetNumberOfSteps ((uint32_t) parameterNumSteps);
  1092. #if JUCE_FORCE_LEGACY_PARAMETER_AUTOMATION_TYPE
  1093. parameter->SetType (parameterNumSteps > 1000 ? AAX_eParameterType_Continuous
  1094. : AAX_eParameterType_Discrete);
  1095. #else
  1096. parameter->SetType (audioProcessor.isParameterDiscrete (parameterIndex) ? AAX_eParameterType_Discrete
  1097. : AAX_eParameterType_Continuous);
  1098. #endif
  1099. parameter->SetOrientation (audioProcessor.isParameterOrientationInverted (parameterIndex)
  1100. ? (AAX_eParameterOrientation_RightMinLeftMax | AAX_eParameterOrientation_TopMinBottomMax
  1101. | AAX_eParameterOrientation_RotarySingleDotMode | AAX_eParameterOrientation_RotaryRightMinLeftMax)
  1102. : (AAX_eParameterOrientation_LeftMinRightMax | AAX_eParameterOrientation_BottomMinTopMax
  1103. | AAX_eParameterOrientation_RotarySingleDotMode | AAX_eParameterOrientation_RotaryLeftMinRightMax));
  1104. mParameterManager.AddParameter (parameter);
  1105. }
  1106. }
  1107. bool getMainBusFormats (AudioChannelSet& inputSet, AudioChannelSet& outputSet)
  1108. {
  1109. AudioProcessor& audioProcessor = getPluginInstance();
  1110. #if ! JucePlugin_IsMidiEffect
  1111. const int inputBuses = audioProcessor.getBusCount (true);
  1112. const int outputBuses = audioProcessor.getBusCount (false);
  1113. #endif
  1114. #if JucePlugin_IsMidiEffect
  1115. // MIDI effect plug-ins do not support any audio channels
  1116. jassert (audioProcessor.getTotalNumInputChannels() == 0
  1117. && audioProcessor.getTotalNumOutputChannels() == 0);
  1118. inputSet = outputSet = AudioChannelSet();
  1119. return true;
  1120. #else
  1121. AAX_EStemFormat inputStemFormat = AAX_eStemFormat_None;
  1122. check (Controller()->GetInputStemFormat (&inputStemFormat));
  1123. AAX_EStemFormat outputStemFormat = AAX_eStemFormat_None;
  1124. check (Controller()->GetOutputStemFormat (&outputStemFormat));
  1125. #if JucePlugin_IsSynth
  1126. if (inputBuses == 0) inputStemFormat = AAX_eStemFormat_None;
  1127. #endif
  1128. inputSet = (inputBuses > 0 ? channelSetFromStemFormat (inputStemFormat, false) : AudioChannelSet());
  1129. outputSet = (outputBuses > 0 ? channelSetFromStemFormat (outputStemFormat, false) : AudioChannelSet());
  1130. if ( (inputSet == AudioChannelSet::disabled() && inputStemFormat != AAX_eStemFormat_None)
  1131. || (outputSet == AudioChannelSet::disabled() && outputStemFormat != AAX_eStemFormat_None)
  1132. || (inputSet != AudioChannelSet::disabled() && inputBuses == 0)
  1133. || (outputSet != AudioChannelSet::disabled() && outputBuses == 0))
  1134. return false;
  1135. return true;
  1136. #endif
  1137. }
  1138. AAX_Result preparePlugin()
  1139. {
  1140. AudioProcessor& audioProcessor = getPluginInstance();
  1141. AudioProcessor::BusesLayout oldLayout = audioProcessor.getBusesLayout();
  1142. AudioChannelSet inputSet, outputSet;
  1143. if (! getMainBusFormats (inputSet, outputSet))
  1144. {
  1145. if (isPrepared)
  1146. {
  1147. isPrepared = false;
  1148. audioProcessor.releaseResources();
  1149. }
  1150. return AAX_ERROR_UNIMPLEMENTED;
  1151. }
  1152. AudioProcessor::BusesLayout newLayout;
  1153. if (! fullBusesLayoutFromMainLayout (audioProcessor, inputSet, outputSet, newLayout))
  1154. {
  1155. if (isPrepared)
  1156. {
  1157. isPrepared = false;
  1158. audioProcessor.releaseResources();
  1159. }
  1160. return AAX_ERROR_UNIMPLEMENTED;
  1161. }
  1162. hasSidechain = (newLayout.getNumChannels (true, 1) == 1);
  1163. if (hasSidechain)
  1164. {
  1165. sidechainDesired.set (1);
  1166. AudioProcessor::BusesLayout disabledSidechainLayout (newLayout);
  1167. disabledSidechainLayout.inputBuses.getReference (1) = AudioChannelSet::disabled();
  1168. canDisableSidechain = audioProcessor.checkBusesLayoutSupported (disabledSidechainLayout);
  1169. if (canDisableSidechain)
  1170. {
  1171. sidechainDesired.set (0);
  1172. newLayout = disabledSidechainLayout;
  1173. }
  1174. }
  1175. const bool layoutChanged = (oldLayout != newLayout);
  1176. if (layoutChanged)
  1177. {
  1178. if (! audioProcessor.setBusesLayout (newLayout))
  1179. {
  1180. if (isPrepared)
  1181. {
  1182. isPrepared = false;
  1183. audioProcessor.releaseResources();
  1184. }
  1185. return AAX_ERROR_UNIMPLEMENTED;
  1186. }
  1187. rebuildChannelMapArrays();
  1188. }
  1189. if (layoutChanged || (! isPrepared))
  1190. {
  1191. if (isPrepared)
  1192. {
  1193. isPrepared = false;
  1194. audioProcessor.releaseResources();
  1195. }
  1196. audioProcessor.setRateAndBufferSizeDetails (sampleRate, lastBufferSize);
  1197. audioProcessor.prepareToPlay (sampleRate, lastBufferSize);
  1198. maxBufferSize = lastBufferSize;
  1199. sideChainBuffer.calloc (static_cast<size_t> (maxBufferSize));
  1200. }
  1201. check (Controller()->SetSignalLatency (audioProcessor.getLatencySamples()));
  1202. isPrepared = true;
  1203. return AAX_SUCCESS;
  1204. }
  1205. void rebuildChannelMapArrays()
  1206. {
  1207. AudioProcessor& audioProcessor = getPluginInstance();
  1208. for (int dir = 0; dir < 2; ++dir)
  1209. {
  1210. const bool isInput = (dir == 0);
  1211. Array<int>& layoutMap = isInput ? inputLayoutMap : outputLayoutMap;
  1212. layoutMap.clear();
  1213. const int n = audioProcessor.getBusCount (isInput);
  1214. int chOffset = 0;
  1215. for (int busIdx = 0; busIdx < n; ++busIdx)
  1216. {
  1217. const AudioChannelSet channelFormat = audioProcessor.getChannelLayoutOfBus (isInput, busIdx);
  1218. if (channelFormat != AudioChannelSet::disabled())
  1219. {
  1220. const int numChannels = channelFormat.size();
  1221. for (int ch = 0; ch < numChannels; ++ch)
  1222. layoutMap.add (juceChannelIndexToAax (ch, channelFormat) + chOffset);
  1223. chOffset += numChannels;
  1224. }
  1225. }
  1226. }
  1227. }
  1228. static void algorithmCallback (JUCEAlgorithmContext* const instancesBegin[], const void* const instancesEnd)
  1229. {
  1230. for (JUCEAlgorithmContext* const* iter = instancesBegin; iter < instancesEnd; ++iter)
  1231. {
  1232. const JUCEAlgorithmContext& i = **iter;
  1233. int sideChainBufferIdx = i.pluginInstance->parameters.hasSidechain && i.sideChainBuffers != nullptr
  1234. ? static_cast<int> (*i.sideChainBuffers) : -1;
  1235. // sidechain index of zero is an invalid index
  1236. if (sideChainBufferIdx <= 0)
  1237. sideChainBufferIdx = -1;
  1238. const int numMeters = i.pluginInstance->parameters.aaxMeters.size();
  1239. float* const meterTapBuffers = (i.meterTapBuffers != nullptr && numMeters > 0 ? *i.meterTapBuffers : nullptr);
  1240. i.pluginInstance->parameters.process (i.inputChannels, i.outputChannels, sideChainBufferIdx,
  1241. *(i.bufferSize), *(i.bypass) != 0,
  1242. getMidiNodeIn(i), getMidiNodeOut(i),
  1243. meterTapBuffers);
  1244. }
  1245. }
  1246. //==============================================================================
  1247. void handleAsyncUpdate() override
  1248. {
  1249. if (processingSidechainChange.get() == 0)
  1250. return;
  1251. AudioProcessor& audioProcessor = getPluginInstance();
  1252. const bool sidechainActual = (audioProcessor.getChannelCountOfBus (true, 1) > 0);
  1253. if (hasSidechain && canDisableSidechain && (sidechainDesired.get() != 0) != sidechainActual)
  1254. {
  1255. if (isPrepared)
  1256. {
  1257. isPrepared = false;
  1258. audioProcessor.releaseResources();
  1259. }
  1260. if (AudioProcessor::Bus* bus = audioProcessor.getBus (true, 1))
  1261. bus->setCurrentLayout (sidechainDesired.get() != 0 ? AudioChannelSet::mono() : AudioChannelSet::disabled());
  1262. audioProcessor.prepareToPlay (audioProcessor.getSampleRate(), audioProcessor.getBlockSize());
  1263. isPrepared = true;
  1264. }
  1265. processingSidechainChange.set (0);
  1266. }
  1267. //==============================================================================
  1268. inline int getParamIndexFromID (AAX_CParamID paramID) const noexcept
  1269. {
  1270. return paramMap [AAXClasses::getAAXParamHash (paramID)];
  1271. }
  1272. inline AAX_CParamID getAAXParamIDFromJuceIndex (int index) const noexcept
  1273. {
  1274. if (! isPositiveAndBelow (index, aaxParamIDs.size())) return nullptr;
  1275. return aaxParamIDs.getReference (index).getCharPointer();
  1276. }
  1277. //==============================================================================
  1278. static AudioProcessor::BusesLayout getDefaultLayout (const AudioProcessor& p, bool enableAll)
  1279. {
  1280. AudioProcessor::BusesLayout defaultLayout;
  1281. for (int dir = 0; dir < 2; ++dir)
  1282. {
  1283. const bool isInput = (dir == 0);
  1284. const int n = p.getBusCount (isInput);
  1285. Array<AudioChannelSet>& layouts = (isInput ? defaultLayout.inputBuses : defaultLayout.outputBuses);
  1286. for (int i = 0; i < n; ++i)
  1287. if (const AudioProcessor::Bus* bus = p.getBus (isInput, i))
  1288. layouts.add (enableAll || bus->isEnabledByDefault() ? bus->getDefaultLayout() : AudioChannelSet());
  1289. }
  1290. return defaultLayout;
  1291. }
  1292. static AudioProcessor::BusesLayout getDefaultLayout (AudioProcessor& p)
  1293. {
  1294. AudioProcessor::BusesLayout defaultLayout;
  1295. defaultLayout = getDefaultLayout (p, true);
  1296. if (! p.checkBusesLayoutSupported (defaultLayout))
  1297. defaultLayout = getDefaultLayout (p, false);
  1298. // Your processor must support the default layout
  1299. jassert (p.checkBusesLayoutSupported (defaultLayout));
  1300. return defaultLayout;
  1301. }
  1302. //==============================================================================
  1303. ScopedJuceInitialiser_GUI libraryInitialiser;
  1304. ScopedPointer<AudioProcessor> pluginInstance;
  1305. bool isPrepared;
  1306. MidiBuffer midiBuffer;
  1307. Array<float*> channelList;
  1308. int32_t juceChunkIndex;
  1309. AAX_CSampleRate sampleRate;
  1310. int lastBufferSize, maxBufferSize;
  1311. bool hasSidechain, canDisableSidechain;
  1312. Atomic<int> processingSidechainChange, sidechainDesired;
  1313. HeapBlock<float> sideChainBuffer;
  1314. Array<int> inputLayoutMap, outputLayoutMap;
  1315. Array<String> aaxParamIDs;
  1316. HashMap<int32, int> paramMap;
  1317. Array<int> aaxMeters;
  1318. struct ChunkMemoryBlock
  1319. {
  1320. juce::MemoryBlock data;
  1321. bool isValid;
  1322. };
  1323. // temporary filter data is generated in GetChunkSize
  1324. // and the size of the data returned. To avoid generating
  1325. // it again in GetChunk, we need to store it somewhere.
  1326. // However, as GetChunkSize and GetChunk can be called
  1327. // on different threads, we store it in thread dependant storage
  1328. // in a hash map with the thread id as a key.
  1329. mutable ThreadLocalValue<ChunkMemoryBlock> perThreadFilterData;
  1330. CriticalSection perThreadDataLock;
  1331. JUCE_DECLARE_NON_COPYABLE (JuceAAX_Processor)
  1332. };
  1333. //==============================================================================
  1334. void JuceAAX_GUI::CreateViewContents()
  1335. {
  1336. if (component == nullptr)
  1337. {
  1338. if (JuceAAX_Processor* params = dynamic_cast<JuceAAX_Processor*> (GetEffectParameters()))
  1339. component = new ContentWrapperComponent (*this, params->getPluginInstance());
  1340. else
  1341. jassertfalse;
  1342. }
  1343. }
  1344. int JuceAAX_GUI::getParamIndexFromID (AAX_CParamID paramID) const noexcept
  1345. {
  1346. if (const JuceAAX_Processor* params = dynamic_cast<const JuceAAX_Processor*> (GetEffectParameters()))
  1347. return params->getParamIndexFromID (paramID);
  1348. return -1;
  1349. }
  1350. AAX_CParamID JuceAAX_GUI::getAAXParamIDFromJuceIndex (int index) const noexcept
  1351. {
  1352. if (const JuceAAX_Processor* params = dynamic_cast<const JuceAAX_Processor*> (GetEffectParameters()))
  1353. return params->getAAXParamIDFromJuceIndex (index);
  1354. return nullptr;
  1355. }
  1356. //==============================================================================
  1357. struct AAXFormatConfiguration
  1358. {
  1359. AAXFormatConfiguration() noexcept
  1360. : inputFormat (AAX_eStemFormat_None), outputFormat (AAX_eStemFormat_None) {}
  1361. AAXFormatConfiguration (AAX_EStemFormat inFormat, AAX_EStemFormat outFormat) noexcept
  1362. : inputFormat (inFormat), outputFormat (outFormat) {}
  1363. AAX_EStemFormat inputFormat, outputFormat;
  1364. bool operator== (const AAXFormatConfiguration other) const noexcept { return (inputFormat == other.inputFormat) && (outputFormat == other.outputFormat); }
  1365. bool operator< (const AAXFormatConfiguration other) const noexcept
  1366. {
  1367. return (inputFormat == other.inputFormat) ? (outputFormat < other.outputFormat) : (inputFormat < other.inputFormat);
  1368. }
  1369. };
  1370. //==============================================================================
  1371. static int addAAXMeters (AudioProcessor& p, AAX_IEffectDescriptor& descriptor)
  1372. {
  1373. const int n = p.getNumParameters();
  1374. int meterIdx = 0;
  1375. for (int i = 0; i < n; ++i)
  1376. {
  1377. const AudioProcessorParameter::Category category = p.getParameterCategory (i);
  1378. // is this a meter?
  1379. if (((category & 0xffff0000) >> 16) == 2)
  1380. {
  1381. if (AAX_IPropertyMap* meterProperties = descriptor.NewPropertyMap())
  1382. {
  1383. AAX_EMeterType aaxMeterType;
  1384. switch (category)
  1385. {
  1386. case AudioProcessorParameter::inputMeter:
  1387. aaxMeterType = AAX_eMeterType_Input;
  1388. break;
  1389. case AudioProcessorParameter::outputMeter:
  1390. aaxMeterType = AAX_eMeterType_Output;
  1391. break;
  1392. case AudioProcessorParameter::compressorLimiterGainReductionMeter:
  1393. aaxMeterType = AAX_eMeterType_CLGain;
  1394. break;
  1395. case AudioProcessorParameter::expanderGateGainReductionMeter:
  1396. aaxMeterType = AAX_eMeterType_EGGain;
  1397. break;
  1398. case AudioProcessorParameter::analysisMeter:
  1399. aaxMeterType = AAX_eMeterType_Analysis;
  1400. break;
  1401. default:
  1402. aaxMeterType = AAX_eMeterType_Other;
  1403. }
  1404. meterProperties->AddProperty (AAX_eProperty_Meter_Type, aaxMeterType);
  1405. meterProperties->AddProperty (AAX_eProperty_Meter_Orientation, AAX_eMeterOrientation_TopRight);
  1406. descriptor.AddMeterDescription ('Metr' + static_cast<AAX_CTypeID> (meterIdx++),
  1407. p.getParameterName (i).toRawUTF8(), meterProperties);
  1408. }
  1409. }
  1410. }
  1411. return meterIdx;
  1412. }
  1413. static void createDescriptor (AAX_IComponentDescriptor& desc,
  1414. const AudioProcessor::BusesLayout& fullLayout,
  1415. AudioProcessor& processor,
  1416. Array<int32>& pluginIds,
  1417. const int numMeters)
  1418. {
  1419. AAX_EStemFormat aaxInputFormat = getFormatForAudioChannelSet (fullLayout.getMainInputChannelSet(), false);
  1420. AAX_EStemFormat aaxOutputFormat = getFormatForAudioChannelSet (fullLayout.getMainOutputChannelSet(), false);
  1421. #if JucePlugin_IsSynth
  1422. if (aaxInputFormat == AAX_eStemFormat_None)
  1423. aaxInputFormat = aaxOutputFormat;
  1424. #endif
  1425. #if JucePlugin_IsMidiEffect
  1426. aaxInputFormat = aaxOutputFormat = AAX_eStemFormat_Mono;
  1427. #endif
  1428. check (desc.AddAudioIn (JUCEAlgorithmIDs::inputChannels));
  1429. check (desc.AddAudioOut (JUCEAlgorithmIDs::outputChannels));
  1430. check (desc.AddAudioBufferLength (JUCEAlgorithmIDs::bufferSize));
  1431. check (desc.AddDataInPort (JUCEAlgorithmIDs::bypass, sizeof (int32_t)));
  1432. #if JucePlugin_WantsMidiInput || JucePlugin_IsMidiEffect
  1433. check (desc.AddMIDINode (JUCEAlgorithmIDs::midiNodeIn, AAX_eMIDINodeType_LocalInput,
  1434. JucePlugin_Name, 0xffff));
  1435. #endif
  1436. #if JucePlugin_ProducesMidiOutput || JucePlugin_IsSynth || JucePlugin_IsMidiEffect
  1437. check (desc.AddMIDINode (JUCEAlgorithmIDs::midiNodeOut, AAX_eMIDINodeType_LocalOutput,
  1438. JucePlugin_Name " Out", 0xffff));
  1439. #endif
  1440. check (desc.AddPrivateData (JUCEAlgorithmIDs::pluginInstance, sizeof (PluginInstanceInfo)));
  1441. check (desc.AddPrivateData (JUCEAlgorithmIDs::preparedFlag, sizeof (int32_t)));
  1442. if (numMeters > 0)
  1443. {
  1444. HeapBlock<AAX_CTypeID> meterIDs (static_cast<size_t> (numMeters));
  1445. for (int i = 0; i < numMeters; ++i)
  1446. meterIDs[i] = 'Metr' + static_cast<AAX_CTypeID> (i);
  1447. check (desc.AddMeters (JUCEAlgorithmIDs::meterTapBuffers, meterIDs.getData(), static_cast<uint32_t> (numMeters)));
  1448. }
  1449. else
  1450. {
  1451. // AAX does not allow there to be any gaps in the fields of the algorithm context structure
  1452. // so just add a dummy one here if there aren't any meters
  1453. check (desc.AddPrivateData (JUCEAlgorithmIDs::meterTapBuffers, sizeof (uintptr_t)));
  1454. }
  1455. // Create a property map
  1456. AAX_IPropertyMap* const properties = desc.NewPropertyMap();
  1457. jassert (properties != nullptr);
  1458. properties->AddProperty (AAX_eProperty_ManufacturerID, JucePlugin_AAXManufacturerCode);
  1459. properties->AddProperty (AAX_eProperty_ProductID, JucePlugin_AAXProductId);
  1460. #if JucePlugin_AAXDisableBypass
  1461. properties->AddProperty (AAX_eProperty_CanBypass, false);
  1462. #else
  1463. properties->AddProperty (AAX_eProperty_CanBypass, true);
  1464. #endif
  1465. properties->AddProperty (AAX_eProperty_InputStemFormat, static_cast<AAX_CPropertyValue> (aaxInputFormat));
  1466. properties->AddProperty (AAX_eProperty_OutputStemFormat, static_cast<AAX_CPropertyValue> (aaxOutputFormat));
  1467. // This value needs to match the RTAS wrapper's Type ID, so that
  1468. // the host knows that the RTAS/AAX plugins are equivalent.
  1469. const int32 pluginID = processor.getAAXPluginIDForMainBusConfig (fullLayout.getMainInputChannelSet(),
  1470. fullLayout.getMainOutputChannelSet(),
  1471. false);
  1472. // The plugin id generated from your AudioProcessor's getAAXPluginIDForMainBusConfig callback
  1473. // it not unique. Please fix your implementation!
  1474. jassert (! pluginIds.contains (pluginID));
  1475. pluginIds.add (pluginID);
  1476. properties->AddProperty (AAX_eProperty_PlugInID_Native, pluginID);
  1477. #if ! JucePlugin_AAXDisableAudioSuite
  1478. properties->AddProperty (AAX_eProperty_PlugInID_AudioSuite,
  1479. processor.getAAXPluginIDForMainBusConfig (fullLayout.getMainInputChannelSet(),
  1480. fullLayout.getMainOutputChannelSet(),
  1481. true));
  1482. #endif
  1483. #if JucePlugin_AAXDisableMultiMono
  1484. properties->AddProperty (AAX_eProperty_Constraint_MultiMonoSupport, false);
  1485. #else
  1486. properties->AddProperty (AAX_eProperty_Constraint_MultiMonoSupport, true);
  1487. #endif
  1488. #if JucePlugin_AAXDisableDynamicProcessing
  1489. properties->AddProperty (AAX_eProperty_Constraint_AlwaysProcess, true);
  1490. #endif
  1491. #if JucePlugin_AAXDisableSaveRestore
  1492. properties->AddProperty (AAX_eProperty_SupportsSaveRestore, false);
  1493. #endif
  1494. if (fullLayout.getChannelSet (true, 1) == AudioChannelSet::mono())
  1495. {
  1496. check (desc.AddSideChainIn (JUCEAlgorithmIDs::sideChainBuffers));
  1497. properties->AddProperty (AAX_eProperty_SupportsSideChainInput, true);
  1498. }
  1499. const int maxAuxBuses = jmax (0, jmin (15, fullLayout.outputBuses.size() - 1));
  1500. // add the output buses
  1501. // This is incrdibly dumb: the output bus format must be well defined
  1502. // for every main bus in/out format pair. This means that there cannot
  1503. // be two configurations with different aux formats but
  1504. // identical main bus in/out formats.
  1505. for (int busIdx = 1; busIdx < maxAuxBuses + 1; ++busIdx)
  1506. {
  1507. const AudioChannelSet& set = fullLayout.getChannelSet (false, busIdx);
  1508. if (set.isDisabled()) break;
  1509. AAX_EStemFormat auxFormat = getFormatForAudioChannelSet (set, true);
  1510. if (auxFormat != AAX_eStemFormat_INT32_MAX && auxFormat != AAX_eStemFormat_None)
  1511. {
  1512. const String& name = processor.getBus (false, busIdx)->getName();
  1513. check (desc.AddAuxOutputStem (0, static_cast<int32_t> (auxFormat), name.toRawUTF8()));
  1514. }
  1515. }
  1516. check (desc.AddProcessProc_Native (algorithmProcessCallback, properties));
  1517. }
  1518. static bool hostSupportsStemFormat (AAX_EStemFormat stemFormat, const AAX_IFeatureInfo* featureInfo)
  1519. {
  1520. if (featureInfo != nullptr)
  1521. {
  1522. AAX_ESupportLevel supportLevel;
  1523. if (featureInfo->SupportLevel (supportLevel) == AAX_SUCCESS && supportLevel == AAX_eSupportLevel_ByProperty)
  1524. {
  1525. ScopedPointer<const AAX_IPropertyMap> props (featureInfo->AcquireProperties());
  1526. if (props != nullptr && props->GetProperty ((AAX_EProperty) stemFormat, (AAX_CPropertyValue*) &supportLevel) != 0)
  1527. return (supportLevel == AAX_eSupportLevel_Supported);
  1528. }
  1529. }
  1530. return (AAX_STEM_FORMAT_INDEX (stemFormat) <= 12);
  1531. }
  1532. static void getPlugInDescription (AAX_IEffectDescriptor& descriptor, const AAX_IFeatureInfo* featureInfo)
  1533. {
  1534. PluginHostType::jucePlugInClientCurrentWrapperType = AudioProcessor::wrapperType_AAX;
  1535. ScopedPointer<AudioProcessor> plugin = createPluginFilterOfType (AudioProcessor::wrapperType_AAX);
  1536. const int numInputBuses = plugin->getBusCount (true);
  1537. const int numOutputBuses = plugin->getBusCount (false);
  1538. auto pluginNames = plugin->getAlternateDisplayNames();
  1539. pluginNames.insert (0, JucePlugin_Desc);
  1540. pluginNames.insert (0, JucePlugin_Name);
  1541. pluginNames.removeDuplicates (false);
  1542. for (auto name : pluginNames)
  1543. descriptor.AddName (name.toRawUTF8());
  1544. descriptor.AddCategory (JucePlugin_AAXCategory);
  1545. const int numMeters = addAAXMeters (*plugin, descriptor);
  1546. #ifdef JucePlugin_AAXPageTableFile
  1547. // optional page table setting - define this macro in your project if you want
  1548. // to set this value - see Avid documentation for details about its format.
  1549. descriptor.AddResourceInfo (AAX_eResourceType_PageTable, JucePlugin_AAXPageTableFile);
  1550. #endif
  1551. check (descriptor.AddProcPtr ((void*) JuceAAX_GUI::Create, kAAX_ProcPtrID_Create_EffectGUI));
  1552. check (descriptor.AddProcPtr ((void*) JuceAAX_Processor::Create, kAAX_ProcPtrID_Create_EffectParameters));
  1553. #if JucePlugin_IsMidiEffect
  1554. // MIDI effect plug-ins do not support any audio channels
  1555. jassert (numInputBuses == 0 && numOutputBuses == 0);
  1556. if (AAX_IComponentDescriptor* const desc = descriptor.NewComponentDescriptor())
  1557. {
  1558. createDescriptor (*desc, 0, plugin->getBusesLayout(), *plugin, numMeters);
  1559. check (descriptor.AddComponent (desc));
  1560. }
  1561. #else
  1562. Array<int32> pluginIds;
  1563. const int numIns = numInputBuses > 0 ? numElementsInArray (aaxFormats) : 0;
  1564. const int numOuts = numOutputBuses > 0 ? numElementsInArray (aaxFormats) : 0;
  1565. for (int inIdx = 0; inIdx < jmax (numIns, 1); ++inIdx)
  1566. {
  1567. AAX_EStemFormat aaxInFormat = numIns > 0 ? aaxFormats[inIdx] : AAX_eStemFormat_None;
  1568. AudioChannelSet inLayout = channelSetFromStemFormat (aaxInFormat, false);
  1569. for (int outIdx = 0; outIdx < jmax (numOuts, 1); ++outIdx)
  1570. {
  1571. AAX_EStemFormat aaxOutFormat = numOuts > 0 ? aaxFormats[outIdx] : AAX_eStemFormat_None;
  1572. AudioChannelSet outLayout = channelSetFromStemFormat (aaxOutFormat, false);
  1573. if (hostSupportsStemFormat (aaxInFormat, featureInfo) && hostSupportsStemFormat (aaxOutFormat, featureInfo))
  1574. {
  1575. AudioProcessor::BusesLayout fullLayout;
  1576. if (! JuceAAX_Processor::fullBusesLayoutFromMainLayout (*plugin, inLayout, outLayout, fullLayout))
  1577. continue;
  1578. if (AAX_IComponentDescriptor* const desc = descriptor.NewComponentDescriptor())
  1579. {
  1580. createDescriptor (*desc, fullLayout, *plugin, pluginIds, numMeters);
  1581. check (descriptor.AddComponent (desc));
  1582. }
  1583. }
  1584. }
  1585. }
  1586. // You don't have any supported layouts
  1587. jassert (pluginIds.size() > 0);
  1588. #endif
  1589. }
  1590. }
  1591. void AAX_CALLBACK AAXClasses::algorithmProcessCallback (JUCEAlgorithmContext* const instancesBegin[], const void* const instancesEnd)
  1592. {
  1593. AAXClasses::JuceAAX_Processor::algorithmCallback (instancesBegin, instancesEnd);
  1594. }
  1595. //==============================================================================
  1596. //==============================================================================
  1597. AAX_Result JUCE_CDECL GetEffectDescriptions (AAX_ICollection*);
  1598. AAX_Result JUCE_CDECL GetEffectDescriptions (AAX_ICollection* collection)
  1599. {
  1600. ScopedJuceInitialiser_GUI libraryInitialiser;
  1601. ScopedPointer<const AAX_IFeatureInfo> stemFormatFeatureInfo;
  1602. if (const auto* hostDescription = collection->DescriptionHost())
  1603. stemFormatFeatureInfo = hostDescription->AcquireFeatureProperties (AAXATTR_ClientFeature_StemFormat);
  1604. if (AAX_IEffectDescriptor* const descriptor = collection->NewDescriptor())
  1605. {
  1606. AAXClasses::getPlugInDescription (*descriptor, stemFormatFeatureInfo);
  1607. collection->AddEffect (JUCE_STRINGIFY (JucePlugin_AAXIdentifier), descriptor);
  1608. collection->SetManufacturerName (JucePlugin_Manufacturer);
  1609. collection->AddPackageName (JucePlugin_Desc);
  1610. collection->AddPackageName (JucePlugin_Name);
  1611. collection->SetPackageVersion (JucePlugin_VersionCode);
  1612. return AAX_SUCCESS;
  1613. }
  1614. return AAX_ERROR_NULL_OBJECT;
  1615. }
  1616. //==============================================================================
  1617. #if _MSC_VER || JUCE_MINGW
  1618. extern "C" BOOL WINAPI DllMain (HINSTANCE instance, DWORD reason, LPVOID) { if (reason == DLL_PROCESS_ATTACH) Process::setCurrentModuleInstanceHandle (instance); return true; }
  1619. #endif
  1620. #endif