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.

1075 lines
42KB

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