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.

886 lines
33KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. // Your project must contain an AppConfig.h file with your project-specific settings in it,
  19. // and your header search path must make it accessible to the module's files.
  20. #include "AppConfig.h"
  21. #include "../utility/juce_CheckSettingMacros.h"
  22. #if JucePlugin_Build_AAX && (JUCE_INCLUDED_AAX_IN_MM || defined (_WIN32) || defined (_WIN64))
  23. #ifdef _MSC_VER
  24. #include <windows.h>
  25. #else
  26. #include <Cocoa/Cocoa.h>
  27. #endif
  28. #include "../utility/juce_IncludeModuleHeaders.h"
  29. #undef Component
  30. #ifdef __clang__
  31. #pragma clang diagnostic push
  32. #pragma clang diagnostic ignored "-Wnon-virtual-dtor"
  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 AAX_EStemFormat getFormatForChans (const int numChans) noexcept
  82. {
  83. switch (numChans)
  84. {
  85. case 0: return AAX_eStemFormat_None;
  86. case 1: return AAX_eStemFormat_Mono;
  87. case 2: return AAX_eStemFormat_Stereo;
  88. case 3: return AAX_eStemFormat_LCR;
  89. case 4: return AAX_eStemFormat_Quad;
  90. case 5: return AAX_eStemFormat_5_0;
  91. case 6: return AAX_eStemFormat_5_1;
  92. case 7: return AAX_eStemFormat_6_1;
  93. case 8: return AAX_eStemFormat_7_1_DTS;
  94. default: jassertfalse; break; // hmm - not a valid number of chans..
  95. }
  96. return AAX_eStemFormat_None;
  97. }
  98. static int getNumChannelsForStemFormat (AAX_EStemFormat format) noexcept
  99. {
  100. switch (format)
  101. {
  102. case AAX_eStemFormat_None: return 0;
  103. case AAX_eStemFormat_Mono: return 1;
  104. case AAX_eStemFormat_Stereo: return 2;
  105. case AAX_eStemFormat_LCR: return 3;
  106. case AAX_eStemFormat_Quad: return 4;
  107. case AAX_eStemFormat_5_0: return 5;
  108. case AAX_eStemFormat_5_1: return 6;
  109. case AAX_eStemFormat_6_1: return 7;
  110. case AAX_eStemFormat_7_1_DTS: return 8;
  111. default: jassertfalse; break; // hmm - not a valid number of chans..
  112. }
  113. return 0;
  114. }
  115. //==============================================================================
  116. struct JUCELibraryRefCount
  117. {
  118. JUCELibraryRefCount() { if (getCount()++ == 0) initialise(); }
  119. ~JUCELibraryRefCount() { if (--getCount() == 0) shutdown(); }
  120. private:
  121. static void initialise()
  122. {
  123. initialiseJuce_GUI();
  124. }
  125. static void shutdown()
  126. {
  127. shutdownJuce_GUI();
  128. }
  129. int& getCount() noexcept
  130. {
  131. static int count = 0;
  132. return count;
  133. }
  134. };
  135. //==============================================================================
  136. class JuceAAX_Processor;
  137. struct PluginInstanceInfo
  138. {
  139. PluginInstanceInfo (JuceAAX_Processor& p) : parameters (p) {}
  140. JuceAAX_Processor& parameters;
  141. JUCE_DECLARE_NON_COPYABLE (PluginInstanceInfo)
  142. };
  143. //==============================================================================
  144. struct JUCEAlgorithmContext
  145. {
  146. float** inputChannels;
  147. float** outputChannels;
  148. int32_t* bufferSize;
  149. int32_t* bypass;
  150. #if JucePlugin_WantsMidiInput
  151. AAX_IMIDINode* midiNodeIn;
  152. #endif
  153. #if JucePlugin_ProducesMidiOutput
  154. AAX_IMIDINode* midiNodeOut;
  155. #endif
  156. PluginInstanceInfo* pluginInstance;
  157. int32_t* isPrepared;
  158. };
  159. struct JUCEAlgorithmIDs
  160. {
  161. enum
  162. {
  163. inputChannels = AAX_FIELD_INDEX (JUCEAlgorithmContext, inputChannels),
  164. outputChannels = AAX_FIELD_INDEX (JUCEAlgorithmContext, outputChannels),
  165. bufferSize = AAX_FIELD_INDEX (JUCEAlgorithmContext, bufferSize),
  166. bypass = AAX_FIELD_INDEX (JUCEAlgorithmContext, bypass),
  167. #if JucePlugin_WantsMidiInput
  168. midiNodeIn = AAX_FIELD_INDEX (JUCEAlgorithmContext, midiNodeIn),
  169. #endif
  170. #if JucePlugin_ProducesMidiOutput
  171. midiNodeOut = AAX_FIELD_INDEX (JUCEAlgorithmContext, midiNodeOut),
  172. #endif
  173. pluginInstance = AAX_FIELD_INDEX (JUCEAlgorithmContext, pluginInstance),
  174. preparedFlag = AAX_FIELD_INDEX (JUCEAlgorithmContext, isPrepared)
  175. };
  176. };
  177. #if JucePlugin_WantsMidiInput
  178. static AAX_IMIDINode* getMidiNodeIn (const JUCEAlgorithmContext& c) noexcept { return c.midiNodeIn; }
  179. #else
  180. static AAX_IMIDINode* getMidiNodeIn (const JUCEAlgorithmContext&) noexcept { return nullptr; }
  181. #endif
  182. #if JucePlugin_ProducesMidiOutput
  183. AAX_IMIDINode* midiNodeOut;
  184. static AAX_IMIDINode* getMidiNodeOut (const JUCEAlgorithmContext& c) noexcept { return c.midiNodeOut; }
  185. #else
  186. static AAX_IMIDINode* getMidiNodeOut (const JUCEAlgorithmContext&) noexcept { return nullptr; }
  187. #endif
  188. //==============================================================================
  189. class JuceAAX_GUI : public AAX_CEffectGUI
  190. {
  191. public:
  192. JuceAAX_GUI() {}
  193. virtual ~JuceAAX_GUI() { DeleteViewContainer(); }
  194. static AAX_IEffectGUI* AAX_CALLBACK Create() { return new JuceAAX_GUI(); }
  195. void CreateViewContents()
  196. {
  197. if (component == nullptr)
  198. {
  199. if (JuceAAX_Processor* params = dynamic_cast <JuceAAX_Processor*> (GetEffectParameters()))
  200. component = new ContentWrapperComponent (*this, params->getPluginInstance());
  201. else
  202. jassertfalse;
  203. }
  204. }
  205. void CreateViewContainer()
  206. {
  207. CreateViewContents();
  208. if (void* nativeViewToAttachTo = GetViewContainerPtr())
  209. {
  210. #if JUCE_MAC
  211. if (GetViewContainerType() == AAX_eViewContainer_Type_NSView)
  212. #else
  213. if (GetViewContainerType() == AAX_eViewContainer_Type_HWND)
  214. #endif
  215. {
  216. component->setVisible (true);
  217. component->addToDesktop (0, nativeViewToAttachTo);
  218. }
  219. }
  220. }
  221. void DeleteViewContainer()
  222. {
  223. if (component != nullptr)
  224. {
  225. JUCE_AUTORELEASEPOOL
  226. {
  227. component->removeFromDesktop();
  228. component = nullptr;
  229. }
  230. }
  231. }
  232. virtual AAX_Result GetViewSize (AAX_Point* viewSize) const
  233. {
  234. if (component != nullptr)
  235. {
  236. viewSize->horz = (float) component->getWidth();
  237. viewSize->vert = (float) component->getHeight();
  238. return AAX_SUCCESS;
  239. }
  240. return AAX_ERROR_NULL_OBJECT;
  241. }
  242. AAX_Result ParameterUpdated (AAX_CParamID /*paramID*/)
  243. {
  244. return AAX_SUCCESS;
  245. }
  246. AAX_Result SetControlHighlightInfo (AAX_CParamID /*paramID*/, AAX_CBoolean /*isHighlighted*/, AAX_EHighlightColor)
  247. {
  248. return AAX_SUCCESS;
  249. }
  250. private:
  251. class ContentWrapperComponent : public juce::Component
  252. {
  253. public:
  254. ContentWrapperComponent (JuceAAX_GUI& gui, AudioProcessor& plugin)
  255. : owner (gui)
  256. {
  257. setOpaque (true);
  258. addAndMakeVisible (pluginEditor = plugin.createEditorIfNeeded());
  259. setBounds (pluginEditor->getLocalBounds());
  260. setBroughtToFrontOnMouseClick (true);
  261. }
  262. ~ContentWrapperComponent()
  263. {
  264. if (pluginEditor != nullptr)
  265. {
  266. PopupMenu::dismissAllActiveMenus();
  267. pluginEditor->getAudioProcessor()->editorBeingDeleted (pluginEditor);
  268. }
  269. }
  270. void paint (Graphics& g)
  271. {
  272. g.fillAll (Colours::black);
  273. }
  274. void childBoundsChanged (Component*)
  275. {
  276. if (pluginEditor != nullptr)
  277. {
  278. const int w = pluginEditor->getWidth();
  279. const int h = pluginEditor->getHeight();
  280. setSize (w, h);
  281. AAX_Point newSize ((float) h, (float) w);
  282. owner.GetViewContainer()->SetViewSize (newSize);
  283. }
  284. }
  285. private:
  286. ScopedPointer<AudioProcessorEditor> pluginEditor;
  287. JuceAAX_GUI& owner;
  288. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ContentWrapperComponent)
  289. };
  290. ScopedPointer<ContentWrapperComponent> component;
  291. JUCELibraryRefCount juceCount;
  292. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (JuceAAX_GUI)
  293. };
  294. //==============================================================================
  295. class JuceAAX_Processor : public AAX_CEffectParameters,
  296. public juce::AudioPlayHead,
  297. public AudioProcessorListener
  298. {
  299. public:
  300. JuceAAX_Processor()
  301. {
  302. pluginInstance = createPluginFilterOfType (AudioProcessor::wrapperType_AAX);
  303. pluginInstance->setPlayHead (this);
  304. pluginInstance->addListener (this);
  305. AAX_CEffectParameters::GetNumberOfChunks (&juceChunkIndex);
  306. }
  307. static AAX_CEffectParameters* AAX_CALLBACK Create() { return new JuceAAX_Processor(); }
  308. AAX_Result EffectInit()
  309. {
  310. addBypassParameter();
  311. addAudioProcessorParameters();
  312. preparePlugin();
  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. check (transport.GetTimelineSelectionStartPosition (&info.timeInSamples));
  406. else
  407. check (transport.GetCurrentNativeSampleLocation (&info.timeInSamples));
  408. info.timeInSeconds = info.timeInSamples / getSampleRate();
  409. int64_t ticks = 0;
  410. check (transport.GetCurrentTickPosition (&ticks));
  411. info.ppqPosition = ticks / 960000.0;
  412. info.isLooping = false;
  413. int64_t loopStartTick = 0, loopEndTick = 0;
  414. check (transport.GetCurrentLoopPosition (&info.isLooping, &loopStartTick, &loopEndTick));
  415. info.ppqLoopStart = loopStartTick / 960000.0;
  416. info.ppqLoopEnd = loopEndTick / 960000.0;
  417. info.editOriginTime = 0;
  418. info.frameRate = AudioPlayHead::fpsUnknown;
  419. AAX_EFrameRate frameRate;
  420. int32_t offset;
  421. if (transport.GetTimeCodeInfo (&frameRate, &offset) == AAX_SUCCESS)
  422. {
  423. double framesPerSec = 24.0;
  424. switch (frameRate)
  425. {
  426. case AAX_eFrameRate_Undeclared: break;
  427. case AAX_eFrameRate_24Frame: info.frameRate = AudioPlayHead::fps24; break;
  428. case AAX_eFrameRate_25Frame: info.frameRate = AudioPlayHead::fps25; framesPerSec = 25.0; break;
  429. case AAX_eFrameRate_2997NonDrop: info.frameRate = AudioPlayHead::fps2997; framesPerSec = 29.97002997; break;
  430. case AAX_eFrameRate_2997DropFrame: info.frameRate = AudioPlayHead::fps2997drop; framesPerSec = 29.97002997; break;
  431. case AAX_eFrameRate_30NonDrop: info.frameRate = AudioPlayHead::fps30; framesPerSec = 30.0; break;
  432. case AAX_eFrameRate_30DropFrame: info.frameRate = AudioPlayHead::fps30drop; framesPerSec = 30.0; break;
  433. case AAX_eFrameRate_23976: info.frameRate = AudioPlayHead::fps24; framesPerSec = 23.976; break;
  434. default: break;
  435. }
  436. info.editOriginTime = offset / framesPerSec;
  437. }
  438. // No way to get these: (?)
  439. info.isRecording = false;
  440. info.ppqPositionOfLastBarStart = 0;
  441. return true;
  442. }
  443. void audioProcessorParameterChanged (AudioProcessor* /*processor*/, int parameterIndex, float newValue)
  444. {
  445. SetParameterNormalizedValue (IndexAsParamID (parameterIndex), (double) newValue);
  446. }
  447. void audioProcessorChanged (AudioProcessor* processor)
  448. {
  449. check (Controller()->SetSignalLatency (processor->getLatencySamples()));
  450. }
  451. void audioProcessorParameterChangeGestureBegin (AudioProcessor* /*processor*/, int parameterIndex)
  452. {
  453. TouchParameter (IndexAsParamID (parameterIndex));
  454. }
  455. void audioProcessorParameterChangeGestureEnd (AudioProcessor* /*processor*/, int parameterIndex)
  456. {
  457. ReleaseParameter (IndexAsParamID (parameterIndex));
  458. }
  459. void process (const float* const* inputs, float* const* outputs, const int bufferSize,
  460. const bool bypass, AAX_IMIDINode* midiNodeIn, AAX_IMIDINode* midiNodeOut)
  461. {
  462. const int numIns = pluginInstance->getNumInputChannels();
  463. const int numOuts = pluginInstance->getNumOutputChannels();
  464. if (numOuts >= numIns)
  465. {
  466. for (int i = 0; i < numIns; ++i)
  467. memcpy (outputs[i], inputs[i], bufferSize * sizeof (float));
  468. process (outputs, numOuts, bufferSize, bypass, midiNodeIn, midiNodeOut);
  469. }
  470. else
  471. {
  472. if (channelList.size() <= numIns)
  473. channelList.insertMultiple (-1, nullptr, 1 + numIns - channelList.size());
  474. float** channels = channelList.getRawDataPointer();
  475. for (int i = 0; i < numOuts; ++i)
  476. {
  477. memcpy (outputs[i], inputs[i], bufferSize * sizeof (float));
  478. channels[i] = outputs[i];
  479. }
  480. for (int i = numOuts; i < numIns; ++i)
  481. channels[i] = const_cast <float*> (inputs[i]);
  482. process (channels, numIns, bufferSize, bypass, midiNodeIn, midiNodeOut);
  483. }
  484. }
  485. private:
  486. struct IndexAsParamID
  487. {
  488. inline explicit IndexAsParamID (int i) noexcept : index (i) {}
  489. operator AAX_CParamID() noexcept
  490. {
  491. jassert (index >= 0);
  492. char* t = name + sizeof (name);
  493. *--t = 0;
  494. int v = index;
  495. do
  496. {
  497. *--t = (char) ('0' + (v % 10));
  498. v /= 10;
  499. } while (v > 0);
  500. return static_cast <AAX_CParamID> (t);
  501. }
  502. private:
  503. int index;
  504. char name[32];
  505. JUCE_DECLARE_NON_COPYABLE (IndexAsParamID)
  506. };
  507. void process (float* const* channels, const int numChans, const int bufferSize,
  508. const bool bypass, AAX_IMIDINode* midiNodeIn, AAX_IMIDINode* midiNodeOut)
  509. {
  510. AudioSampleBuffer buffer (channels, numChans, bufferSize);
  511. midiBuffer.clear();
  512. #if JucePlugin_WantsMidiInput
  513. {
  514. AAX_CMidiStream* const midiStream = midiNodeIn->GetNodeBuffer();
  515. const uint32_t numMidiEvents = midiStream->mBufferSize;
  516. for (uint32_t i = 0; i < numMidiEvents; ++i)
  517. {
  518. // (This 8-byte alignment is a workaround to a bug in the AAX SDK. Hopefully can be
  519. // removed in future when the packet structure size is fixed)
  520. const AAX_CMidiPacket& m = *addBytesToPointer (midiStream->mBuffer,
  521. i * ((sizeof (AAX_CMidiPacket) + 7) & ~7));
  522. jassert ((int) m.mTimestamp < bufferSize);
  523. midiBuffer.addEvent (m.mData, (int) m.mLength,
  524. jlimit (0, (int) bufferSize - 1, (int) m.mTimestamp));
  525. }
  526. }
  527. #endif
  528. {
  529. const ScopedLock sl (pluginInstance->getCallbackLock());
  530. if (bypass)
  531. pluginInstance->processBlockBypassed (buffer, midiBuffer);
  532. else
  533. pluginInstance->processBlock (buffer, midiBuffer);
  534. }
  535. #if JucePlugin_ProducesMidiOutput
  536. {
  537. const juce::uint8* midiEventData;
  538. int midiEventSize, midiEventPosition;
  539. MidiBuffer::Iterator i (midiBuffer);
  540. AAX_CMidiPacket packet;
  541. packet.mIsImmediate = false;
  542. while (i.getNextEvent (midiEventData, midiEventSize, midiEventPosition))
  543. {
  544. jassert (isPositiveAndBelow (midiEventPosition, bufferSize));
  545. if (midiEventSize <= 4)
  546. {
  547. packet.mTimestamp = (uint32_t) midiEventPosition;
  548. packet.mLength = (uint32_t) midiEventSize;
  549. memcpy (packet.mData, midiEventData, midiEventSize);
  550. check (midiNodeOut->PostMIDIPacket (&packet));
  551. }
  552. }
  553. }
  554. #endif
  555. }
  556. void addBypassParameter()
  557. {
  558. AAX_IParameter* masterBypass = new AAX_CParameter<bool> (cDefaultMasterBypassID,
  559. AAX_CString ("Master Bypass"),
  560. false,
  561. AAX_CBinaryTaperDelegate<bool>(),
  562. AAX_CBinaryDisplayDelegate<bool> ("bypass", "on"),
  563. true);
  564. masterBypass->SetNumberOfSteps (2);
  565. masterBypass->SetType (AAX_eParameterType_Discrete);
  566. mParameterManager.AddParameter (masterBypass);
  567. mPacketDispatcher.RegisterPacket (cDefaultMasterBypassID, JUCEAlgorithmIDs::bypass);
  568. }
  569. void addAudioProcessorParameters()
  570. {
  571. AudioProcessor& audioProcessor = getPluginInstance();
  572. const int numParameters = audioProcessor.getNumParameters();
  573. for (int parameterIndex = 0; parameterIndex < numParameters; ++parameterIndex)
  574. {
  575. if (audioProcessor.isParameterAutomatable (parameterIndex))
  576. {
  577. AAX_IParameter* parameter
  578. = new AAX_CParameter<float> (IndexAsParamID (parameterIndex),
  579. audioProcessor.getParameterName (parameterIndex).toRawUTF8(),
  580. audioProcessor.getParameter (parameterIndex),
  581. AAX_CLinearTaperDelegate<float, 0>(),
  582. AAX_CNumberDisplayDelegate<float, 3>(),
  583. true);
  584. parameter->SetNumberOfSteps (0x7fffffff);
  585. parameter->SetType (AAX_eParameterType_Continuous);
  586. mParameterManager.AddParameter (parameter);
  587. }
  588. }
  589. }
  590. void preparePlugin()
  591. {
  592. AAX_EStemFormat inputStemFormat = AAX_eStemFormat_None;
  593. check (Controller()->GetInputStemFormat (&inputStemFormat));
  594. const int numberOfInputChannels = getNumChannelsForStemFormat (inputStemFormat);
  595. AAX_EStemFormat outputStemFormat = AAX_eStemFormat_None;
  596. check (Controller()->GetOutputStemFormat (&outputStemFormat));
  597. const int numberOfOutputChannels = getNumChannelsForStemFormat (outputStemFormat);
  598. AudioProcessor& audioProcessor = getPluginInstance();
  599. const AAX_CSampleRate sampleRate = getSampleRate();
  600. const int bufferSize = 0; // how to get this?
  601. audioProcessor.setPlayConfigDetails (numberOfInputChannels, numberOfOutputChannels, sampleRate, bufferSize);
  602. audioProcessor.prepareToPlay (sampleRate, bufferSize);
  603. check (Controller()->SetSignalLatency (audioProcessor.getLatencySamples()));
  604. }
  605. AAX_CSampleRate getSampleRate() const
  606. {
  607. AAX_CSampleRate sampleRate;
  608. check (Controller()->GetSampleRate (&sampleRate));
  609. return sampleRate;
  610. }
  611. JUCELibraryRefCount juceCount;
  612. ScopedPointer<AudioProcessor> pluginInstance;
  613. MidiBuffer midiBuffer;
  614. Array<float*> channelList;
  615. int32_t juceChunkIndex;
  616. // tempFilterData is initialized in GetChunkSize.
  617. // To avoid generating it again in GetChunk, we keep it as a member.
  618. mutable juce::MemoryBlock tempFilterData;
  619. JUCE_DECLARE_NON_COPYABLE (JuceAAX_Processor)
  620. };
  621. //==============================================================================
  622. static void AAX_CALLBACK algorithmProcessCallback (JUCEAlgorithmContext* const instancesBegin[],
  623. const void* const instancesEnd)
  624. {
  625. for (JUCEAlgorithmContext* const* iter = instancesBegin; iter < instancesEnd; ++iter)
  626. {
  627. const JUCEAlgorithmContext& i = **iter;
  628. i.pluginInstance->parameters.process (i.inputChannels, i.outputChannels,
  629. *(i.bufferSize), *(i.bypass) != 0,
  630. getMidiNodeIn(i), getMidiNodeOut(i));
  631. }
  632. }
  633. //==============================================================================
  634. static void createDescriptor (AAX_IComponentDescriptor& desc, int channelConfigIndex,
  635. int numInputs, int numOutputs)
  636. {
  637. check (desc.AddAudioIn (JUCEAlgorithmIDs::inputChannels));
  638. check (desc.AddAudioOut (JUCEAlgorithmIDs::outputChannels));
  639. check (desc.AddAudioBufferLength (JUCEAlgorithmIDs::bufferSize));
  640. check (desc.AddDataInPort (JUCEAlgorithmIDs::bypass, sizeof (int32_t)));
  641. #if JucePlugin_WantsMidiInput
  642. check (desc.AddMIDINode (JUCEAlgorithmIDs::midiNodeIn, AAX_eMIDINodeType_LocalInput,
  643. JucePlugin_Name, 0xffff));
  644. #endif
  645. #if JucePlugin_ProducesMidiOutput
  646. check (desc.AddMIDINode (JUCEAlgorithmIDs::midiNodeOut, AAX_eMIDINodeType_LocalOutput,
  647. JucePlugin_Name " Out", 0xffff));
  648. #endif
  649. check (desc.AddPrivateData (JUCEAlgorithmIDs::pluginInstance, sizeof (PluginInstanceInfo)));
  650. // Create a property map
  651. AAX_IPropertyMap* const properties = desc.NewPropertyMap();
  652. jassert (properties != nullptr);
  653. properties->AddProperty (AAX_eProperty_ManufacturerID, JucePlugin_AAXManufacturerCode);
  654. properties->AddProperty (AAX_eProperty_ProductID, JucePlugin_AAXProductId);
  655. #if JucePlugin_AAXDisableBypass
  656. properties->AddProperty (AAX_eProperty_CanBypass, false);
  657. #else
  658. properties->AddProperty (AAX_eProperty_CanBypass, true);
  659. #endif
  660. properties->AddProperty (AAX_eProperty_InputStemFormat, getFormatForChans (numInputs));
  661. properties->AddProperty (AAX_eProperty_OutputStemFormat, getFormatForChans (numOutputs));
  662. // This value needs to match the RTAS wrapper's Type ID, so that
  663. // the host knows that the RTAS/AAX plugins are equivalent.
  664. properties->AddProperty (AAX_eProperty_PlugInID_Native, 'jcaa' + channelConfigIndex);
  665. check (desc.AddProcessProc_Native (algorithmProcessCallback, properties));
  666. }
  667. static void getPlugInDescription (AAX_IEffectDescriptor& descriptor)
  668. {
  669. descriptor.AddName (JucePlugin_Desc);
  670. descriptor.AddName (JucePlugin_Name);
  671. descriptor.AddCategory (JucePlugin_AAXCategory);
  672. check (descriptor.AddProcPtr ((void*) JuceAAX_GUI::Create, kAAX_ProcPtrID_Create_EffectGUI));
  673. check (descriptor.AddProcPtr ((void*) JuceAAX_Processor::Create, kAAX_ProcPtrID_Create_EffectParameters));
  674. const short channelConfigs[][2] = { JucePlugin_PreferredChannelConfigurations };
  675. const int numConfigs = numElementsInArray (channelConfigs);
  676. // You need to actually add some configurations to the JucePlugin_PreferredChannelConfigurations
  677. // value in your JucePluginCharacteristics.h file..
  678. jassert (numConfigs > 0);
  679. for (int i = 0; i < numConfigs; ++i)
  680. {
  681. if (AAX_IComponentDescriptor* const desc = descriptor.NewComponentDescriptor())
  682. {
  683. createDescriptor (*desc, i,
  684. channelConfigs [i][0],
  685. channelConfigs [i][1]);
  686. check (descriptor.AddComponent (desc));
  687. }
  688. }
  689. }
  690. };
  691. //==============================================================================
  692. AAX_Result JUCE_CDECL GetEffectDescriptions (AAX_ICollection*);
  693. AAX_Result JUCE_CDECL GetEffectDescriptions (AAX_ICollection* collection)
  694. {
  695. AAXClasses::JUCELibraryRefCount libraryRefCount;
  696. if (AAX_IEffectDescriptor* const descriptor = collection->NewDescriptor())
  697. {
  698. AAXClasses::getPlugInDescription (*descriptor);
  699. collection->AddEffect (JUCE_STRINGIFY (JucePlugin_AAXIdentifier), descriptor);
  700. collection->SetManufacturerName (JucePlugin_Manufacturer);
  701. collection->AddPackageName (JucePlugin_Desc);
  702. collection->AddPackageName (JucePlugin_Name);
  703. collection->SetPackageVersion (JucePlugin_VersionCode);
  704. return AAX_SUCCESS;
  705. }
  706. return AAX_ERROR_NULL_OBJECT;
  707. }
  708. #endif