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.

1042 lines
40KB

  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. //==============================================================================
  150. struct JUCELibraryRefCount
  151. {
  152. JUCELibraryRefCount() { if (getCount()++ == 0) initialise(); }
  153. ~JUCELibraryRefCount() { if (--getCount() == 0) shutdown(); }
  154. private:
  155. static void initialise()
  156. {
  157. initialiseJuce_GUI();
  158. }
  159. static void shutdown()
  160. {
  161. shutdownJuce_GUI();
  162. }
  163. int& getCount() noexcept
  164. {
  165. static int count = 0;
  166. return count;
  167. }
  168. };
  169. //==============================================================================
  170. class JuceAAX_Processor;
  171. struct PluginInstanceInfo
  172. {
  173. PluginInstanceInfo (JuceAAX_Processor& p) : parameters (p) {}
  174. JuceAAX_Processor& parameters;
  175. JUCE_DECLARE_NON_COPYABLE (PluginInstanceInfo)
  176. };
  177. //==============================================================================
  178. struct JUCEAlgorithmContext
  179. {
  180. float** inputChannels;
  181. float** outputChannels;
  182. int32_t* bufferSize;
  183. int32_t* bypass;
  184. #if JucePlugin_WantsMidiInput
  185. AAX_IMIDINode* midiNodeIn;
  186. #endif
  187. #if JucePlugin_ProducesMidiOutput
  188. AAX_IMIDINode* midiNodeOut;
  189. #endif
  190. PluginInstanceInfo* pluginInstance;
  191. int32_t* isPrepared;
  192. };
  193. struct JUCEAlgorithmIDs
  194. {
  195. enum
  196. {
  197. inputChannels = AAX_FIELD_INDEX (JUCEAlgorithmContext, inputChannels),
  198. outputChannels = AAX_FIELD_INDEX (JUCEAlgorithmContext, outputChannels),
  199. bufferSize = AAX_FIELD_INDEX (JUCEAlgorithmContext, bufferSize),
  200. bypass = AAX_FIELD_INDEX (JUCEAlgorithmContext, bypass),
  201. #if JucePlugin_WantsMidiInput
  202. midiNodeIn = AAX_FIELD_INDEX (JUCEAlgorithmContext, midiNodeIn),
  203. #endif
  204. #if JucePlugin_ProducesMidiOutput
  205. midiNodeOut = AAX_FIELD_INDEX (JUCEAlgorithmContext, midiNodeOut),
  206. #endif
  207. pluginInstance = AAX_FIELD_INDEX (JUCEAlgorithmContext, pluginInstance),
  208. preparedFlag = AAX_FIELD_INDEX (JUCEAlgorithmContext, isPrepared)
  209. };
  210. };
  211. #if JucePlugin_WantsMidiInput
  212. static AAX_IMIDINode* getMidiNodeIn (const JUCEAlgorithmContext& c) noexcept { return c.midiNodeIn; }
  213. #else
  214. static AAX_IMIDINode* getMidiNodeIn (const JUCEAlgorithmContext&) noexcept { return nullptr; }
  215. #endif
  216. #if JucePlugin_ProducesMidiOutput
  217. AAX_IMIDINode* midiNodeOut;
  218. static AAX_IMIDINode* getMidiNodeOut (const JUCEAlgorithmContext& c) noexcept { return c.midiNodeOut; }
  219. #else
  220. static AAX_IMIDINode* getMidiNodeOut (const JUCEAlgorithmContext&) noexcept { return nullptr; }
  221. #endif
  222. //==============================================================================
  223. class JuceAAX_GUI : public AAX_CEffectGUI
  224. {
  225. public:
  226. JuceAAX_GUI() {}
  227. ~JuceAAX_GUI() { DeleteViewContainer(); }
  228. static AAX_IEffectGUI* AAX_CALLBACK Create() { return new JuceAAX_GUI(); }
  229. void CreateViewContents() override
  230. {
  231. if (component == nullptr)
  232. {
  233. if (JuceAAX_Processor* params = dynamic_cast <JuceAAX_Processor*> (GetEffectParameters()))
  234. component = new ContentWrapperComponent (*this, params->getPluginInstance());
  235. else
  236. jassertfalse;
  237. }
  238. }
  239. void CreateViewContainer() override
  240. {
  241. CreateViewContents();
  242. if (void* nativeViewToAttachTo = GetViewContainerPtr())
  243. {
  244. #if JUCE_MAC
  245. if (GetViewContainerType() == AAX_eViewContainer_Type_NSView)
  246. #else
  247. if (GetViewContainerType() == AAX_eViewContainer_Type_HWND)
  248. #endif
  249. {
  250. component->setVisible (true);
  251. component->addToDesktop (0, nativeViewToAttachTo);
  252. }
  253. }
  254. }
  255. void DeleteViewContainer() override
  256. {
  257. if (component != nullptr)
  258. {
  259. JUCE_AUTORELEASEPOOL
  260. {
  261. component->removeFromDesktop();
  262. component = nullptr;
  263. }
  264. }
  265. }
  266. virtual AAX_Result GetViewSize (AAX_Point* viewSize) const override
  267. {
  268. if (component != nullptr)
  269. {
  270. viewSize->horz = (float) component->getWidth();
  271. viewSize->vert = (float) component->getHeight();
  272. return AAX_SUCCESS;
  273. }
  274. return AAX_ERROR_NULL_OBJECT;
  275. }
  276. AAX_Result ParameterUpdated (AAX_CParamID /*paramID*/) override
  277. {
  278. return AAX_SUCCESS;
  279. }
  280. AAX_Result SetControlHighlightInfo (AAX_CParamID /*paramID*/, AAX_CBoolean /*isHighlighted*/, AAX_EHighlightColor) override
  281. {
  282. return AAX_SUCCESS;
  283. }
  284. private:
  285. class ContentWrapperComponent : public juce::Component
  286. {
  287. public:
  288. ContentWrapperComponent (JuceAAX_GUI& gui, AudioProcessor& plugin)
  289. : owner (gui)
  290. {
  291. setOpaque (true);
  292. addAndMakeVisible (pluginEditor = plugin.createEditorIfNeeded());
  293. setBounds (pluginEditor->getLocalBounds());
  294. setBroughtToFrontOnMouseClick (true);
  295. }
  296. ~ContentWrapperComponent()
  297. {
  298. if (pluginEditor != nullptr)
  299. {
  300. PopupMenu::dismissAllActiveMenus();
  301. pluginEditor->getAudioProcessor()->editorBeingDeleted (pluginEditor);
  302. }
  303. }
  304. void paint (Graphics& g) override
  305. {
  306. g.fillAll (Colours::black);
  307. }
  308. void childBoundsChanged (Component*) override
  309. {
  310. if (pluginEditor != nullptr)
  311. {
  312. const int w = pluginEditor->getWidth();
  313. const int h = pluginEditor->getHeight();
  314. setSize (w, h);
  315. AAX_Point newSize ((float) h, (float) w);
  316. owner.GetViewContainer()->SetViewSize (newSize);
  317. }
  318. }
  319. private:
  320. ScopedPointer<AudioProcessorEditor> pluginEditor;
  321. JuceAAX_GUI& owner;
  322. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ContentWrapperComponent)
  323. };
  324. ScopedPointer<ContentWrapperComponent> component;
  325. JUCELibraryRefCount juceCount;
  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.getParameter (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. mParameterManager.AddParameter (parameter);
  704. }
  705. }
  706. void preparePlugin()
  707. {
  708. AAX_EStemFormat inputStemFormat = AAX_eStemFormat_None;
  709. check (Controller()->GetInputStemFormat (&inputStemFormat));
  710. const int numberOfInputChannels = getNumChannelsForStemFormat (inputStemFormat);
  711. AAX_EStemFormat outputStemFormat = AAX_eStemFormat_None;
  712. check (Controller()->GetOutputStemFormat (&outputStemFormat));
  713. const int numberOfOutputChannels = getNumChannelsForStemFormat (outputStemFormat);
  714. AudioProcessor& audioProcessor = getPluginInstance();
  715. audioProcessor.setSpeakerArrangement (getSpeakerArrangementString (inputStemFormat),
  716. getSpeakerArrangementString (outputStemFormat));
  717. audioProcessor.setPlayConfigDetails (numberOfInputChannels, numberOfOutputChannels, sampleRate, lastBufferSize);
  718. audioProcessor.prepareToPlay (sampleRate, lastBufferSize);
  719. check (Controller()->SetSignalLatency (audioProcessor.getLatencySamples()));
  720. }
  721. JUCELibraryRefCount juceCount;
  722. ScopedPointer<AudioProcessor> pluginInstance;
  723. MidiBuffer midiBuffer;
  724. Array<float*> channelList;
  725. int32_t juceChunkIndex;
  726. AAX_CSampleRate sampleRate;
  727. int lastBufferSize;
  728. // tempFilterData is initialized in GetChunkSize.
  729. // To avoid generating it again in GetChunk, we keep it as a member.
  730. mutable juce::MemoryBlock tempFilterData;
  731. JUCE_DECLARE_NON_COPYABLE (JuceAAX_Processor)
  732. };
  733. //==============================================================================
  734. static void AAX_CALLBACK algorithmProcessCallback (JUCEAlgorithmContext* const instancesBegin[],
  735. const void* const instancesEnd)
  736. {
  737. for (JUCEAlgorithmContext* const* iter = instancesBegin; iter < instancesEnd; ++iter)
  738. {
  739. const JUCEAlgorithmContext& i = **iter;
  740. i.pluginInstance->parameters.process (i.inputChannels, i.outputChannels,
  741. *(i.bufferSize), *(i.bypass) != 0,
  742. getMidiNodeIn(i), getMidiNodeOut(i));
  743. }
  744. }
  745. //==============================================================================
  746. static void createDescriptor (AAX_IComponentDescriptor& desc, int channelConfigIndex,
  747. int numInputs, int numOutputs)
  748. {
  749. check (desc.AddAudioIn (JUCEAlgorithmIDs::inputChannels));
  750. check (desc.AddAudioOut (JUCEAlgorithmIDs::outputChannels));
  751. check (desc.AddAudioBufferLength (JUCEAlgorithmIDs::bufferSize));
  752. check (desc.AddDataInPort (JUCEAlgorithmIDs::bypass, sizeof (int32_t)));
  753. #if JucePlugin_WantsMidiInput
  754. check (desc.AddMIDINode (JUCEAlgorithmIDs::midiNodeIn, AAX_eMIDINodeType_LocalInput,
  755. JucePlugin_Name, 0xffff));
  756. #endif
  757. #if JucePlugin_ProducesMidiOutput
  758. check (desc.AddMIDINode (JUCEAlgorithmIDs::midiNodeOut, AAX_eMIDINodeType_LocalOutput,
  759. JucePlugin_Name " Out", 0xffff));
  760. #endif
  761. check (desc.AddPrivateData (JUCEAlgorithmIDs::pluginInstance, sizeof (PluginInstanceInfo)));
  762. // Create a property map
  763. AAX_IPropertyMap* const properties = desc.NewPropertyMap();
  764. jassert (properties != nullptr);
  765. properties->AddProperty (AAX_eProperty_ManufacturerID, JucePlugin_AAXManufacturerCode);
  766. properties->AddProperty (AAX_eProperty_ProductID, JucePlugin_AAXProductId);
  767. #if JucePlugin_AAXDisableBypass
  768. properties->AddProperty (AAX_eProperty_CanBypass, false);
  769. #else
  770. properties->AddProperty (AAX_eProperty_CanBypass, true);
  771. #endif
  772. properties->AddProperty (AAX_eProperty_InputStemFormat, getFormatForChans (numInputs));
  773. properties->AddProperty (AAX_eProperty_OutputStemFormat, getFormatForChans (numOutputs));
  774. // This value needs to match the RTAS wrapper's Type ID, so that
  775. // the host knows that the RTAS/AAX plugins are equivalent.
  776. properties->AddProperty (AAX_eProperty_PlugInID_Native, 'jcaa' + channelConfigIndex);
  777. properties->AddProperty (AAX_eProperty_PlugInID_AudioSuite, 'jyaa' + channelConfigIndex);
  778. #if JucePlugin_AAXDisableMultiMono
  779. properties->AddProperty (AAX_eProperty_Constraint_MultiMonoSupport, false);
  780. #else
  781. properties->AddProperty (AAX_eProperty_Constraint_MultiMonoSupport, true);
  782. #endif
  783. check (desc.AddProcessProc_Native (algorithmProcessCallback, properties));
  784. }
  785. static void getPlugInDescription (AAX_IEffectDescriptor& descriptor)
  786. {
  787. descriptor.AddName (JucePlugin_Desc);
  788. descriptor.AddName (JucePlugin_Name);
  789. descriptor.AddCategory (JucePlugin_AAXCategory);
  790. #ifdef JucePlugin_AAXPageTableFile
  791. // optional page table setting - define this macro in your AppConfig.h if you
  792. // want to set this value - see Avid documentation for details about its format.
  793. descriptor.AddResourceInfo (AAX_eResourceType_PageTable, JucePlugin_AAXPageTableFile);
  794. #endif
  795. check (descriptor.AddProcPtr ((void*) JuceAAX_GUI::Create, kAAX_ProcPtrID_Create_EffectGUI));
  796. check (descriptor.AddProcPtr ((void*) JuceAAX_Processor::Create, kAAX_ProcPtrID_Create_EffectParameters));
  797. const short channelConfigs[][2] = { JucePlugin_PreferredChannelConfigurations };
  798. const int numConfigs = numElementsInArray (channelConfigs);
  799. // You need to actually add some configurations to the JucePlugin_PreferredChannelConfigurations
  800. // value in your JucePluginCharacteristics.h file..
  801. jassert (numConfigs > 0);
  802. for (int i = 0; i < numConfigs; ++i)
  803. {
  804. if (AAX_IComponentDescriptor* const desc = descriptor.NewComponentDescriptor())
  805. {
  806. const int numIns = channelConfigs [i][0];
  807. const int numOuts = channelConfigs [i][1];
  808. if (numIns <= 8 && numOuts <= 8) // AAX doesn't seem to handle more than 8 chans
  809. {
  810. createDescriptor (*desc, i, numIns, numOuts);
  811. check (descriptor.AddComponent (desc));
  812. }
  813. }
  814. }
  815. }
  816. };
  817. //==============================================================================
  818. AAX_Result JUCE_CDECL GetEffectDescriptions (AAX_ICollection*);
  819. AAX_Result JUCE_CDECL GetEffectDescriptions (AAX_ICollection* collection)
  820. {
  821. AAXClasses::JUCELibraryRefCount libraryRefCount;
  822. if (AAX_IEffectDescriptor* const descriptor = collection->NewDescriptor())
  823. {
  824. AAXClasses::getPlugInDescription (*descriptor);
  825. collection->AddEffect (JUCE_STRINGIFY (JucePlugin_AAXIdentifier), descriptor);
  826. collection->SetManufacturerName (JucePlugin_Manufacturer);
  827. collection->AddPackageName (JucePlugin_Desc);
  828. collection->AddPackageName (JucePlugin_Name);
  829. collection->SetPackageVersion (JucePlugin_VersionCode);
  830. return AAX_SUCCESS;
  831. }
  832. return AAX_ERROR_NULL_OBJECT;
  833. }
  834. #endif