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.

894 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. AAX_Result NotificationReceived (AAX_CTypeID type, const void* data, uint32_t size)
  460. {
  461. if (type == AAX_eNotificationEvent_EnteringOfflineMode) pluginInstance->setNonRealtime (true);
  462. if (type == AAX_eNotificationEvent_ExitingOfflineMode) pluginInstance->setNonRealtime (false);
  463. return AAX_CEffectParameters::NotificationReceived (type, data, size);
  464. }
  465. void process (const float* const* inputs, float* const* outputs, const int bufferSize,
  466. const bool bypass, AAX_IMIDINode* midiNodeIn, AAX_IMIDINode* midiNodeOut)
  467. {
  468. const int numIns = pluginInstance->getNumInputChannels();
  469. const int numOuts = pluginInstance->getNumOutputChannels();
  470. if (numOuts >= numIns)
  471. {
  472. for (int i = 0; i < numIns; ++i)
  473. memcpy (outputs[i], inputs[i], bufferSize * sizeof (float));
  474. process (outputs, numOuts, bufferSize, bypass, midiNodeIn, midiNodeOut);
  475. }
  476. else
  477. {
  478. if (channelList.size() <= numIns)
  479. channelList.insertMultiple (-1, nullptr, 1 + numIns - channelList.size());
  480. float** channels = channelList.getRawDataPointer();
  481. for (int i = 0; i < numOuts; ++i)
  482. {
  483. memcpy (outputs[i], inputs[i], bufferSize * sizeof (float));
  484. channels[i] = outputs[i];
  485. }
  486. for (int i = numOuts; i < numIns; ++i)
  487. channels[i] = const_cast <float*> (inputs[i]);
  488. process (channels, numIns, bufferSize, bypass, midiNodeIn, midiNodeOut);
  489. }
  490. }
  491. private:
  492. struct IndexAsParamID
  493. {
  494. inline explicit IndexAsParamID (int i) noexcept : index (i) {}
  495. operator AAX_CParamID() noexcept
  496. {
  497. jassert (index >= 0);
  498. char* t = name + sizeof (name);
  499. *--t = 0;
  500. int v = index;
  501. do
  502. {
  503. *--t = (char) ('0' + (v % 10));
  504. v /= 10;
  505. } while (v > 0);
  506. return static_cast <AAX_CParamID> (t);
  507. }
  508. private:
  509. int index;
  510. char name[32];
  511. JUCE_DECLARE_NON_COPYABLE (IndexAsParamID)
  512. };
  513. void process (float* const* channels, const int numChans, const int bufferSize,
  514. const bool bypass, AAX_IMIDINode* midiNodeIn, AAX_IMIDINode* midiNodeOut)
  515. {
  516. AudioSampleBuffer buffer (channels, numChans, bufferSize);
  517. midiBuffer.clear();
  518. #if JucePlugin_WantsMidiInput
  519. {
  520. AAX_CMidiStream* const midiStream = midiNodeIn->GetNodeBuffer();
  521. const uint32_t numMidiEvents = midiStream->mBufferSize;
  522. for (uint32_t i = 0; i < numMidiEvents; ++i)
  523. {
  524. // (This 8-byte alignment is a workaround to a bug in the AAX SDK. Hopefully can be
  525. // removed in future when the packet structure size is fixed)
  526. const AAX_CMidiPacket& m = *addBytesToPointer (midiStream->mBuffer,
  527. i * ((sizeof (AAX_CMidiPacket) + 7) & ~7));
  528. jassert ((int) m.mTimestamp < bufferSize);
  529. midiBuffer.addEvent (m.mData, (int) m.mLength,
  530. jlimit (0, (int) bufferSize - 1, (int) m.mTimestamp));
  531. }
  532. }
  533. #endif
  534. {
  535. const ScopedLock sl (pluginInstance->getCallbackLock());
  536. if (bypass)
  537. pluginInstance->processBlockBypassed (buffer, midiBuffer);
  538. else
  539. pluginInstance->processBlock (buffer, midiBuffer);
  540. }
  541. #if JucePlugin_ProducesMidiOutput
  542. {
  543. const juce::uint8* midiEventData;
  544. int midiEventSize, midiEventPosition;
  545. MidiBuffer::Iterator i (midiBuffer);
  546. AAX_CMidiPacket packet;
  547. packet.mIsImmediate = false;
  548. while (i.getNextEvent (midiEventData, midiEventSize, midiEventPosition))
  549. {
  550. jassert (isPositiveAndBelow (midiEventPosition, bufferSize));
  551. if (midiEventSize <= 4)
  552. {
  553. packet.mTimestamp = (uint32_t) midiEventPosition;
  554. packet.mLength = (uint32_t) midiEventSize;
  555. memcpy (packet.mData, midiEventData, midiEventSize);
  556. check (midiNodeOut->PostMIDIPacket (&packet));
  557. }
  558. }
  559. }
  560. #endif
  561. }
  562. void addBypassParameter()
  563. {
  564. AAX_IParameter* masterBypass = new AAX_CParameter<bool> (cDefaultMasterBypassID,
  565. AAX_CString ("Master Bypass"),
  566. false,
  567. AAX_CBinaryTaperDelegate<bool>(),
  568. AAX_CBinaryDisplayDelegate<bool> ("bypass", "on"),
  569. true);
  570. masterBypass->SetNumberOfSteps (2);
  571. masterBypass->SetType (AAX_eParameterType_Discrete);
  572. mParameterManager.AddParameter (masterBypass);
  573. mPacketDispatcher.RegisterPacket (cDefaultMasterBypassID, JUCEAlgorithmIDs::bypass);
  574. }
  575. void addAudioProcessorParameters()
  576. {
  577. AudioProcessor& audioProcessor = getPluginInstance();
  578. const int numParameters = audioProcessor.getNumParameters();
  579. for (int parameterIndex = 0; parameterIndex < numParameters; ++parameterIndex)
  580. {
  581. if (audioProcessor.isParameterAutomatable (parameterIndex))
  582. {
  583. AAX_IParameter* parameter
  584. = new AAX_CParameter<float> (IndexAsParamID (parameterIndex),
  585. audioProcessor.getParameterName (parameterIndex).toRawUTF8(),
  586. audioProcessor.getParameter (parameterIndex),
  587. AAX_CLinearTaperDelegate<float, 0>(),
  588. AAX_CNumberDisplayDelegate<float, 3>(),
  589. true);
  590. parameter->SetNumberOfSteps (0x7fffffff);
  591. parameter->SetType (AAX_eParameterType_Continuous);
  592. mParameterManager.AddParameter (parameter);
  593. }
  594. }
  595. }
  596. void preparePlugin()
  597. {
  598. AAX_EStemFormat inputStemFormat = AAX_eStemFormat_None;
  599. check (Controller()->GetInputStemFormat (&inputStemFormat));
  600. const int numberOfInputChannels = getNumChannelsForStemFormat (inputStemFormat);
  601. AAX_EStemFormat outputStemFormat = AAX_eStemFormat_None;
  602. check (Controller()->GetOutputStemFormat (&outputStemFormat));
  603. const int numberOfOutputChannels = getNumChannelsForStemFormat (outputStemFormat);
  604. AudioProcessor& audioProcessor = getPluginInstance();
  605. const AAX_CSampleRate sampleRate = getSampleRate();
  606. const int bufferSize = 0; // how to get this?
  607. audioProcessor.setPlayConfigDetails (numberOfInputChannels, numberOfOutputChannels, sampleRate, bufferSize);
  608. audioProcessor.prepareToPlay (sampleRate, bufferSize);
  609. check (Controller()->SetSignalLatency (audioProcessor.getLatencySamples()));
  610. }
  611. AAX_CSampleRate getSampleRate() const
  612. {
  613. AAX_CSampleRate sampleRate;
  614. check (Controller()->GetSampleRate (&sampleRate));
  615. return sampleRate;
  616. }
  617. JUCELibraryRefCount juceCount;
  618. ScopedPointer<AudioProcessor> pluginInstance;
  619. MidiBuffer midiBuffer;
  620. Array<float*> channelList;
  621. int32_t juceChunkIndex;
  622. // tempFilterData is initialized in GetChunkSize.
  623. // To avoid generating it again in GetChunk, we keep it as a member.
  624. mutable juce::MemoryBlock tempFilterData;
  625. JUCE_DECLARE_NON_COPYABLE (JuceAAX_Processor)
  626. };
  627. //==============================================================================
  628. static void AAX_CALLBACK algorithmProcessCallback (JUCEAlgorithmContext* const instancesBegin[],
  629. const void* const instancesEnd)
  630. {
  631. for (JUCEAlgorithmContext* const* iter = instancesBegin; iter < instancesEnd; ++iter)
  632. {
  633. const JUCEAlgorithmContext& i = **iter;
  634. i.pluginInstance->parameters.process (i.inputChannels, i.outputChannels,
  635. *(i.bufferSize), *(i.bypass) != 0,
  636. getMidiNodeIn(i), getMidiNodeOut(i));
  637. }
  638. }
  639. //==============================================================================
  640. static void createDescriptor (AAX_IComponentDescriptor& desc, int channelConfigIndex,
  641. int numInputs, int numOutputs)
  642. {
  643. check (desc.AddAudioIn (JUCEAlgorithmIDs::inputChannels));
  644. check (desc.AddAudioOut (JUCEAlgorithmIDs::outputChannels));
  645. check (desc.AddAudioBufferLength (JUCEAlgorithmIDs::bufferSize));
  646. check (desc.AddDataInPort (JUCEAlgorithmIDs::bypass, sizeof (int32_t)));
  647. #if JucePlugin_WantsMidiInput
  648. check (desc.AddMIDINode (JUCEAlgorithmIDs::midiNodeIn, AAX_eMIDINodeType_LocalInput,
  649. JucePlugin_Name, 0xffff));
  650. #endif
  651. #if JucePlugin_ProducesMidiOutput
  652. check (desc.AddMIDINode (JUCEAlgorithmIDs::midiNodeOut, AAX_eMIDINodeType_LocalOutput,
  653. JucePlugin_Name " Out", 0xffff));
  654. #endif
  655. check (desc.AddPrivateData (JUCEAlgorithmIDs::pluginInstance, sizeof (PluginInstanceInfo)));
  656. // Create a property map
  657. AAX_IPropertyMap* const properties = desc.NewPropertyMap();
  658. jassert (properties != nullptr);
  659. properties->AddProperty (AAX_eProperty_ManufacturerID, JucePlugin_AAXManufacturerCode);
  660. properties->AddProperty (AAX_eProperty_ProductID, JucePlugin_AAXProductId);
  661. #if JucePlugin_AAXDisableBypass
  662. properties->AddProperty (AAX_eProperty_CanBypass, false);
  663. #else
  664. properties->AddProperty (AAX_eProperty_CanBypass, true);
  665. #endif
  666. properties->AddProperty (AAX_eProperty_InputStemFormat, getFormatForChans (numInputs));
  667. properties->AddProperty (AAX_eProperty_OutputStemFormat, getFormatForChans (numOutputs));
  668. // This value needs to match the RTAS wrapper's Type ID, so that
  669. // the host knows that the RTAS/AAX plugins are equivalent.
  670. properties->AddProperty (AAX_eProperty_PlugInID_Native, 'jcaa' + channelConfigIndex);
  671. check (desc.AddProcessProc_Native (algorithmProcessCallback, properties));
  672. }
  673. static void getPlugInDescription (AAX_IEffectDescriptor& descriptor)
  674. {
  675. descriptor.AddName (JucePlugin_Desc);
  676. descriptor.AddName (JucePlugin_Name);
  677. descriptor.AddCategory (JucePlugin_AAXCategory);
  678. check (descriptor.AddProcPtr ((void*) JuceAAX_GUI::Create, kAAX_ProcPtrID_Create_EffectGUI));
  679. check (descriptor.AddProcPtr ((void*) JuceAAX_Processor::Create, kAAX_ProcPtrID_Create_EffectParameters));
  680. const short channelConfigs[][2] = { JucePlugin_PreferredChannelConfigurations };
  681. const int numConfigs = numElementsInArray (channelConfigs);
  682. // You need to actually add some configurations to the JucePlugin_PreferredChannelConfigurations
  683. // value in your JucePluginCharacteristics.h file..
  684. jassert (numConfigs > 0);
  685. for (int i = 0; i < numConfigs; ++i)
  686. {
  687. if (AAX_IComponentDescriptor* const desc = descriptor.NewComponentDescriptor())
  688. {
  689. createDescriptor (*desc, i,
  690. channelConfigs [i][0],
  691. channelConfigs [i][1]);
  692. check (descriptor.AddComponent (desc));
  693. }
  694. }
  695. }
  696. };
  697. //==============================================================================
  698. AAX_Result JUCE_CDECL GetEffectDescriptions (AAX_ICollection*);
  699. AAX_Result JUCE_CDECL GetEffectDescriptions (AAX_ICollection* collection)
  700. {
  701. AAXClasses::JUCELibraryRefCount libraryRefCount;
  702. if (AAX_IEffectDescriptor* const descriptor = collection->NewDescriptor())
  703. {
  704. AAXClasses::getPlugInDescription (*descriptor);
  705. collection->AddEffect (JUCE_STRINGIFY (JucePlugin_AAXIdentifier), descriptor);
  706. collection->SetManufacturerName (JucePlugin_Manufacturer);
  707. collection->AddPackageName (JucePlugin_Desc);
  708. collection->AddPackageName (JucePlugin_Name);
  709. collection->SetPackageVersion (JucePlugin_VersionCode);
  710. return AAX_SUCCESS;
  711. }
  712. return AAX_ERROR_NULL_OBJECT;
  713. }
  714. #endif