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.

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