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.

994 lines
38KB

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