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.

893 lines
33KB

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