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.

1050 lines
41KB

  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. setBounds (pluginEditor->getLocalBounds());
  296. }
  297. ~ContentWrapperComponent()
  298. {
  299. if (pluginEditor != nullptr)
  300. {
  301. PopupMenu::dismissAllActiveMenus();
  302. pluginEditor->processor.editorBeingDeleted (pluginEditor);
  303. }
  304. }
  305. void paint (Graphics& g) override
  306. {
  307. g.fillAll (Colours::black);
  308. }
  309. void childBoundsChanged (Component*) override
  310. {
  311. if (pluginEditor != nullptr)
  312. {
  313. const int w = pluginEditor->getWidth();
  314. const int h = pluginEditor->getHeight();
  315. setSize (w, h);
  316. AAX_Point newSize ((float) h, (float) w);
  317. owner.GetViewContainer()->SetViewSize (newSize);
  318. }
  319. }
  320. ScopedPointer<AudioProcessorEditor> pluginEditor;
  321. JuceAAX_GUI& owner;
  322. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ContentWrapperComponent)
  323. };
  324. ScopedPointer<ContentWrapperComponent> component;
  325. ScopedJuceInitialiser_GUI libraryInitialiser;
  326. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (JuceAAX_GUI)
  327. };
  328. //==============================================================================
  329. class JuceAAX_Processor : public AAX_CEffectParameters,
  330. public juce::AudioPlayHead,
  331. public AudioProcessorListener
  332. {
  333. public:
  334. JuceAAX_Processor() : sampleRate (0), lastBufferSize (1024)
  335. {
  336. pluginInstance = createPluginFilterOfType (AudioProcessor::wrapperType_AAX);
  337. pluginInstance->setPlayHead (this);
  338. pluginInstance->addListener (this);
  339. AAX_CEffectParameters::GetNumberOfChunks (&juceChunkIndex);
  340. }
  341. static AAX_CEffectParameters* AAX_CALLBACK Create() { return new JuceAAX_Processor(); }
  342. AAX_Result EffectInit() override
  343. {
  344. check (Controller()->GetSampleRate (&sampleRate));
  345. preparePlugin();
  346. addBypassParameter();
  347. addAudioProcessorParameters();
  348. return AAX_SUCCESS;
  349. }
  350. AAX_Result GetNumberOfChunks (int32_t* numChunks) const override
  351. {
  352. // The juceChunk is the last chunk.
  353. *numChunks = juceChunkIndex + 1;
  354. return AAX_SUCCESS;
  355. }
  356. AAX_Result GetChunkIDFromIndex (int32_t index, AAX_CTypeID* chunkID) const override
  357. {
  358. if (index != juceChunkIndex)
  359. return AAX_CEffectParameters::GetChunkIDFromIndex (index, chunkID);
  360. *chunkID = juceChunkType;
  361. return AAX_SUCCESS;
  362. }
  363. AAX_Result GetChunkSize (AAX_CTypeID chunkID, uint32_t* oSize) const override
  364. {
  365. if (chunkID != juceChunkType)
  366. return AAX_CEffectParameters::GetChunkSize (chunkID, oSize);
  367. tempFilterData.reset();
  368. pluginInstance->getStateInformation (tempFilterData);
  369. *oSize = (uint32_t) tempFilterData.getSize();
  370. return AAX_SUCCESS;
  371. }
  372. AAX_Result GetChunk (AAX_CTypeID chunkID, AAX_SPlugInChunk* oChunk) const override
  373. {
  374. if (chunkID != juceChunkType)
  375. return AAX_CEffectParameters::GetChunk (chunkID, oChunk);
  376. if (tempFilterData.getSize() == 0)
  377. pluginInstance->getStateInformation (tempFilterData);
  378. oChunk->fSize = (int32_t) tempFilterData.getSize();
  379. tempFilterData.copyTo (oChunk->fData, 0, tempFilterData.getSize());
  380. tempFilterData.reset();
  381. return AAX_SUCCESS;
  382. }
  383. AAX_Result SetChunk (AAX_CTypeID chunkID, const AAX_SPlugInChunk* chunk) override
  384. {
  385. if (chunkID != juceChunkType)
  386. return AAX_CEffectParameters::SetChunk (chunkID, chunk);
  387. pluginInstance->setStateInformation ((void*) chunk->fData, chunk->fSize);
  388. return AAX_SUCCESS;
  389. }
  390. AAX_Result ResetFieldData (AAX_CFieldIndex fieldIndex, void* data, uint32_t dataSize) const override
  391. {
  392. switch (fieldIndex)
  393. {
  394. case JUCEAlgorithmIDs::pluginInstance:
  395. {
  396. const size_t numObjects = dataSize / sizeof (PluginInstanceInfo);
  397. PluginInstanceInfo* const objects = static_cast<PluginInstanceInfo*> (data);
  398. jassert (numObjects == 1); // not sure how to handle more than one..
  399. for (size_t i = 0; i < numObjects; ++i)
  400. new (objects + i) PluginInstanceInfo (const_cast<JuceAAX_Processor&> (*this));
  401. break;
  402. }
  403. case JUCEAlgorithmIDs::preparedFlag:
  404. {
  405. const_cast<JuceAAX_Processor*>(this)->preparePlugin();
  406. const size_t numObjects = dataSize / sizeof (uint32_t);
  407. uint32_t* const objects = static_cast<uint32_t*> (data);
  408. for (size_t i = 0; i < numObjects; ++i)
  409. new (objects + i) uint32_t (1);
  410. break;
  411. }
  412. }
  413. return AAX_SUCCESS;
  414. }
  415. AAX_Result UpdateParameterNormalizedValue (AAX_CParamID paramID, double value, AAX_EUpdateSource source) override
  416. {
  417. AAX_Result result = AAX_CEffectParameters::UpdateParameterNormalizedValue (paramID, value, source);
  418. if (! isBypassParam (paramID))
  419. pluginInstance->setParameter (getParamIndexFromID (paramID), (float) value);
  420. return result;
  421. }
  422. AAX_Result GetParameterStringFromValue (AAX_CParamID paramID, double value, AAX_IString* result, int32_t maxLen) const override
  423. {
  424. if (isBypassParam (paramID))
  425. result->Set (value == 0 ? "Off"
  426. : (maxLen >= 8 ? "Bypassed" : "Byp"));
  427. else
  428. result->Set (pluginInstance->getParameterText (getParamIndexFromID (paramID), maxLen).toRawUTF8());
  429. return AAX_SUCCESS;
  430. }
  431. AAX_Result GetParameterNumberofSteps (AAX_CParamID paramID, int32_t* result) const
  432. {
  433. if (isBypassParam (paramID))
  434. *result = 2;
  435. else
  436. *result = pluginInstance->getParameterNumSteps (getParamIndexFromID (paramID));
  437. return AAX_SUCCESS;
  438. }
  439. AAX_Result GetParameterNormalizedValue (AAX_CParamID paramID, double* result) const override
  440. {
  441. if (isBypassParam (paramID))
  442. return AAX_CEffectParameters::GetParameterNormalizedValue (paramID, result);
  443. *result = pluginInstance->getParameter (getParamIndexFromID (paramID));
  444. return AAX_SUCCESS;
  445. }
  446. AAX_Result SetParameterNormalizedValue (AAX_CParamID paramID, double newValue) override
  447. {
  448. if (! isBypassParam (paramID))
  449. {
  450. if (AAX_IParameter* p = const_cast<AAX_IParameter*> (mParameterManager.GetParameterByID (paramID)))
  451. p->SetValueWithFloat ((float) newValue);
  452. pluginInstance->setParameter (getParamIndexFromID (paramID), (float) newValue);
  453. }
  454. return AAX_SUCCESS;
  455. }
  456. AAX_Result SetParameterNormalizedRelative (AAX_CParamID paramID, double newValue) override
  457. {
  458. if (! isBypassParam (paramID))
  459. {
  460. const int paramIndex = getParamIndexFromID (paramID);
  461. const float oldValue = pluginInstance->getParameter (paramIndex);
  462. pluginInstance->setParameter (paramIndex, jlimit (0.0f, 1.0f, (float) (oldValue + newValue)));
  463. if (AAX_IParameter* p = const_cast<AAX_IParameter*> (mParameterManager.GetParameterByID (paramID)))
  464. p->SetValueWithFloat ((float) newValue);
  465. }
  466. return AAX_SUCCESS;
  467. }
  468. AAX_Result GetParameterNameOfLength (AAX_CParamID paramID, AAX_IString* result, int32_t maxLen) const override
  469. {
  470. if (isBypassParam (paramID))
  471. result->Set (maxLen >= 13 ? "Master Bypass"
  472. : (maxLen >= 8 ? "Mast Byp"
  473. : (maxLen >= 6 ? "MstByp" : "MByp")));
  474. else
  475. result->Set (pluginInstance->getParameterName (getParamIndexFromID (paramID), maxLen).toRawUTF8());
  476. return AAX_SUCCESS;
  477. }
  478. AAX_Result GetParameterName (AAX_CParamID paramID, AAX_IString* result) const override
  479. {
  480. if (isBypassParam (paramID))
  481. result->Set ("Master Bypass");
  482. else
  483. result->Set (pluginInstance->getParameterName (getParamIndexFromID (paramID), 31).toRawUTF8());
  484. return AAX_SUCCESS;
  485. }
  486. AAX_Result GetParameterDefaultNormalizedValue (AAX_CParamID paramID, double* result) const override
  487. {
  488. if (! isBypassParam (paramID))
  489. *result = (double) pluginInstance->getParameterDefaultValue (getParamIndexFromID (paramID));
  490. return AAX_SUCCESS;
  491. }
  492. AudioProcessor& getPluginInstance() const noexcept { return *pluginInstance; }
  493. bool getCurrentPosition (juce::AudioPlayHead::CurrentPositionInfo& info) override
  494. {
  495. const AAX_ITransport& transport = *Transport();
  496. info.bpm = 0.0;
  497. check (transport.GetCurrentTempo (&info.bpm));
  498. int32_t num = 4, den = 4;
  499. transport.GetCurrentMeter (&num, &den);
  500. info.timeSigNumerator = (int) num;
  501. info.timeSigDenominator = (int) den;
  502. info.timeInSamples = 0;
  503. if (transport.IsTransportPlaying (&info.isPlaying) != AAX_SUCCESS)
  504. info.isPlaying = false;
  505. if (info.isPlaying
  506. || transport.GetTimelineSelectionStartPosition (&info.timeInSamples) != AAX_SUCCESS)
  507. check (transport.GetCurrentNativeSampleLocation (&info.timeInSamples));
  508. info.timeInSeconds = info.timeInSamples / sampleRate;
  509. int64_t ticks = 0;
  510. check (transport.GetCurrentTickPosition (&ticks));
  511. info.ppqPosition = ticks / 960000.0;
  512. info.isLooping = false;
  513. int64_t loopStartTick = 0, loopEndTick = 0;
  514. check (transport.GetCurrentLoopPosition (&info.isLooping, &loopStartTick, &loopEndTick));
  515. info.ppqLoopStart = loopStartTick / 960000.0;
  516. info.ppqLoopEnd = loopEndTick / 960000.0;
  517. info.editOriginTime = 0;
  518. info.frameRate = AudioPlayHead::fpsUnknown;
  519. AAX_EFrameRate frameRate;
  520. int32_t offset;
  521. if (transport.GetTimeCodeInfo (&frameRate, &offset) == AAX_SUCCESS)
  522. {
  523. double framesPerSec = 24.0;
  524. switch (frameRate)
  525. {
  526. case AAX_eFrameRate_Undeclared: break;
  527. case AAX_eFrameRate_24Frame: info.frameRate = AudioPlayHead::fps24; break;
  528. case AAX_eFrameRate_25Frame: info.frameRate = AudioPlayHead::fps25; framesPerSec = 25.0; break;
  529. case AAX_eFrameRate_2997NonDrop: info.frameRate = AudioPlayHead::fps2997; framesPerSec = 29.97002997; break;
  530. case AAX_eFrameRate_2997DropFrame: info.frameRate = AudioPlayHead::fps2997drop; framesPerSec = 29.97002997; break;
  531. case AAX_eFrameRate_30NonDrop: info.frameRate = AudioPlayHead::fps30; framesPerSec = 30.0; break;
  532. case AAX_eFrameRate_30DropFrame: info.frameRate = AudioPlayHead::fps30drop; framesPerSec = 30.0; break;
  533. case AAX_eFrameRate_23976: info.frameRate = AudioPlayHead::fps24; framesPerSec = 23.976; break;
  534. default: break;
  535. }
  536. info.editOriginTime = offset / framesPerSec;
  537. }
  538. // No way to get these: (?)
  539. info.isRecording = false;
  540. info.ppqPositionOfLastBarStart = 0;
  541. return true;
  542. }
  543. void audioProcessorParameterChanged (AudioProcessor* /*processor*/, int parameterIndex, float newValue) override
  544. {
  545. SetParameterNormalizedValue (IndexAsParamID (parameterIndex), (double) newValue);
  546. }
  547. void audioProcessorChanged (AudioProcessor* processor) override
  548. {
  549. ++mNumPlugInChanges;
  550. check (Controller()->SetSignalLatency (processor->getLatencySamples()));
  551. }
  552. void audioProcessorParameterChangeGestureBegin (AudioProcessor* /*processor*/, int parameterIndex) override
  553. {
  554. TouchParameter (IndexAsParamID (parameterIndex));
  555. }
  556. void audioProcessorParameterChangeGestureEnd (AudioProcessor* /*processor*/, int parameterIndex) override
  557. {
  558. ReleaseParameter (IndexAsParamID (parameterIndex));
  559. }
  560. AAX_Result NotificationReceived (AAX_CTypeID type, const void* data, uint32_t size) override
  561. {
  562. if (type == AAX_eNotificationEvent_EnteringOfflineMode) pluginInstance->setNonRealtime (true);
  563. if (type == AAX_eNotificationEvent_ExitingOfflineMode) pluginInstance->setNonRealtime (false);
  564. return AAX_CEffectParameters::NotificationReceived (type, data, size);
  565. }
  566. void process (const float* const* inputs, float* const* outputs, const int bufferSize,
  567. const bool bypass, AAX_IMIDINode* midiNodeIn, AAX_IMIDINode* midiNodesOut)
  568. {
  569. const int numIns = pluginInstance->getNumInputChannels();
  570. const int numOuts = pluginInstance->getNumOutputChannels();
  571. if (numOuts >= numIns)
  572. {
  573. for (int i = 0; i < numIns; ++i)
  574. memcpy (outputs[i], inputs[i], (size_t) bufferSize * sizeof (float));
  575. process (outputs, numOuts, bufferSize, bypass, midiNodeIn, midiNodesOut);
  576. }
  577. else
  578. {
  579. if (channelList.size() <= numIns)
  580. channelList.insertMultiple (-1, nullptr, 1 + numIns - channelList.size());
  581. float** channels = channelList.getRawDataPointer();
  582. for (int i = 0; i < numOuts; ++i)
  583. {
  584. memcpy (outputs[i], inputs[i], (size_t) bufferSize * sizeof (float));
  585. channels[i] = outputs[i];
  586. }
  587. for (int i = numOuts; i < numIns; ++i)
  588. channels[i] = const_cast<float*> (inputs[i]);
  589. process (channels, numIns, bufferSize, bypass, midiNodeIn, midiNodesOut);
  590. }
  591. }
  592. private:
  593. struct IndexAsParamID
  594. {
  595. inline explicit IndexAsParamID (int i) noexcept : index (i) {}
  596. operator AAX_CParamID() noexcept
  597. {
  598. jassert (index >= 0);
  599. char* t = name + sizeof (name);
  600. *--t = 0;
  601. int v = index;
  602. do
  603. {
  604. *--t = (char) ('0' + (v % 10));
  605. v /= 10;
  606. } while (v > 0);
  607. return static_cast<AAX_CParamID> (t);
  608. }
  609. private:
  610. int index;
  611. char name[32];
  612. JUCE_DECLARE_NON_COPYABLE (IndexAsParamID)
  613. };
  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. static void AAX_CALLBACK algorithmProcessCallback (JUCEAlgorithmContext* const instancesBegin[],
  740. const void* const instancesEnd)
  741. {
  742. for (JUCEAlgorithmContext* const* iter = instancesBegin; iter < instancesEnd; ++iter)
  743. {
  744. const JUCEAlgorithmContext& i = **iter;
  745. i.pluginInstance->parameters.process (i.inputChannels, i.outputChannels,
  746. *(i.bufferSize), *(i.bypass) != 0,
  747. getMidiNodeIn(i), getMidiNodeOut(i));
  748. }
  749. }
  750. //==============================================================================
  751. static void createDescriptor (AAX_IComponentDescriptor& desc, int channelConfigIndex,
  752. int numInputs, int numOutputs)
  753. {
  754. check (desc.AddAudioIn (JUCEAlgorithmIDs::inputChannels));
  755. check (desc.AddAudioOut (JUCEAlgorithmIDs::outputChannels));
  756. check (desc.AddAudioBufferLength (JUCEAlgorithmIDs::bufferSize));
  757. check (desc.AddDataInPort (JUCEAlgorithmIDs::bypass, sizeof (int32_t)));
  758. #if JucePlugin_WantsMidiInput
  759. check (desc.AddMIDINode (JUCEAlgorithmIDs::midiNodeIn, AAX_eMIDINodeType_LocalInput,
  760. JucePlugin_Name, 0xffff));
  761. #endif
  762. #if JucePlugin_ProducesMidiOutput
  763. check (desc.AddMIDINode (JUCEAlgorithmIDs::midiNodeOut, AAX_eMIDINodeType_LocalOutput,
  764. JucePlugin_Name " Out", 0xffff));
  765. #endif
  766. check (desc.AddPrivateData (JUCEAlgorithmIDs::pluginInstance, sizeof (PluginInstanceInfo)));
  767. // Create a property map
  768. AAX_IPropertyMap* const properties = desc.NewPropertyMap();
  769. jassert (properties != nullptr);
  770. properties->AddProperty (AAX_eProperty_ManufacturerID, JucePlugin_AAXManufacturerCode);
  771. properties->AddProperty (AAX_eProperty_ProductID, JucePlugin_AAXProductId);
  772. #if JucePlugin_AAXDisableBypass
  773. properties->AddProperty (AAX_eProperty_CanBypass, false);
  774. #else
  775. properties->AddProperty (AAX_eProperty_CanBypass, true);
  776. #endif
  777. properties->AddProperty (AAX_eProperty_InputStemFormat, getFormatForChans (numInputs));
  778. properties->AddProperty (AAX_eProperty_OutputStemFormat, getFormatForChans (numOutputs));
  779. // This value needs to match the RTAS wrapper's Type ID, so that
  780. // the host knows that the RTAS/AAX plugins are equivalent.
  781. properties->AddProperty (AAX_eProperty_PlugInID_Native, 'jcaa' + channelConfigIndex);
  782. properties->AddProperty (AAX_eProperty_PlugInID_AudioSuite, 'jyaa' + channelConfigIndex);
  783. #if JucePlugin_AAXDisableMultiMono
  784. properties->AddProperty (AAX_eProperty_Constraint_MultiMonoSupport, false);
  785. #else
  786. properties->AddProperty (AAX_eProperty_Constraint_MultiMonoSupport, true);
  787. #endif
  788. check (desc.AddProcessProc_Native (algorithmProcessCallback, properties));
  789. }
  790. static void getPlugInDescription (AAX_IEffectDescriptor& descriptor)
  791. {
  792. descriptor.AddName (JucePlugin_Desc);
  793. descriptor.AddName (JucePlugin_Name);
  794. descriptor.AddCategory (JucePlugin_AAXCategory);
  795. #ifdef JucePlugin_AAXPageTableFile
  796. // optional page table setting - define this macro in your AppConfig.h if you
  797. // want to set this value - see Avid documentation for details about its format.
  798. descriptor.AddResourceInfo (AAX_eResourceType_PageTable, JucePlugin_AAXPageTableFile);
  799. #endif
  800. check (descriptor.AddProcPtr ((void*) JuceAAX_GUI::Create, kAAX_ProcPtrID_Create_EffectGUI));
  801. check (descriptor.AddProcPtr ((void*) JuceAAX_Processor::Create, kAAX_ProcPtrID_Create_EffectParameters));
  802. const short channelConfigs[][2] = { JucePlugin_PreferredChannelConfigurations };
  803. const int numConfigs = numElementsInArray (channelConfigs);
  804. // You need to actually add some configurations to the JucePlugin_PreferredChannelConfigurations
  805. // value in your JucePluginCharacteristics.h file..
  806. jassert (numConfigs > 0);
  807. for (int i = 0; i < numConfigs; ++i)
  808. {
  809. if (AAX_IComponentDescriptor* const desc = descriptor.NewComponentDescriptor())
  810. {
  811. const int numIns = channelConfigs [i][0];
  812. const int numOuts = channelConfigs [i][1];
  813. if (numIns <= 8 && numOuts <= 8) // AAX doesn't seem to handle more than 8 chans
  814. {
  815. createDescriptor (*desc, i, numIns, numOuts);
  816. check (descriptor.AddComponent (desc));
  817. }
  818. }
  819. }
  820. }
  821. };
  822. //==============================================================================
  823. AAX_Result JUCE_CDECL GetEffectDescriptions (AAX_ICollection*);
  824. AAX_Result JUCE_CDECL GetEffectDescriptions (AAX_ICollection* collection)
  825. {
  826. ScopedJuceInitialiser_GUI libraryInitialiser;
  827. if (AAX_IEffectDescriptor* const descriptor = collection->NewDescriptor())
  828. {
  829. AAXClasses::getPlugInDescription (*descriptor);
  830. collection->AddEffect (JUCE_STRINGIFY (JucePlugin_AAXIdentifier), descriptor);
  831. collection->SetManufacturerName (JucePlugin_Manufacturer);
  832. collection->AddPackageName (JucePlugin_Desc);
  833. collection->AddPackageName (JucePlugin_Name);
  834. collection->SetPackageVersion (JucePlugin_VersionCode);
  835. return AAX_SUCCESS;
  836. }
  837. return AAX_ERROR_NULL_OBJECT;
  838. }
  839. #endif