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.

885 lines
29KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-7 by Raw Material Software ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the
  7. GNU General Public License, as published by the Free Software Foundation;
  8. either version 2 of the License, or (at your option) any later version.
  9. JUCE is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with JUCE; if not, visit www.gnu.org/licenses or write to the
  15. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. Boston, MA 02111-1307 USA
  17. ------------------------------------------------------------------------------
  18. If you'd like to release a closed-source product which uses JUCE, commercial
  19. licenses are also available: visit www.rawmaterialsoftware.com/juce for
  20. more information.
  21. ==============================================================================
  22. */
  23. #include <AudioUnit/AudioUnit.h>
  24. #include "AUMIDIEffectBase.h"
  25. #include "AUCarbonViewBase.h"
  26. #include "../../juce_AudioFilterBase.h"
  27. #include "../../juce_IncludeCharacteristics.h"
  28. //==============================================================================
  29. #define juceFilterObjectPropertyID 0x1a45ffe9
  30. static VoidArray activePlugins;
  31. static const short channelConfigs[][2] = { JucePlugin_PreferredChannelConfigurations };
  32. static const int numChannelConfigs = numElementsInArray (channelConfigs);
  33. BEGIN_JUCE_NAMESPACE
  34. extern void juce_setCurrentExecutableFileNameFromBundleId (const String& bundleId) throw();
  35. END_JUCE_NAMESPACE
  36. //==============================================================================
  37. class JuceAU : public AUMIDIEffectBase,
  38. public AudioFilterBase::HostCallbacks
  39. {
  40. public:
  41. //==============================================================================
  42. JuceAU (AudioUnit component)
  43. : AUMIDIEffectBase (component),
  44. juceFilter (0),
  45. bufferSpace (2, 16),
  46. channels (0),
  47. prepared (false)
  48. {
  49. CreateElements();
  50. if (activePlugins.size() == 0)
  51. {
  52. initialiseJuce_GUI();
  53. #ifdef JucePlugin_CFBundleIdentifier
  54. juce_setCurrentExecutableFileNameFromBundleId (JucePlugin_CFBundleIdentifier);
  55. #endif
  56. MessageManager::getInstance()->setTimeBeforeShowingWaitCursor (0);
  57. }
  58. juceFilter = createPluginFilter();
  59. juceFilter->setHostCallbacks (this);
  60. jassert (juceFilter != 0);
  61. Globals()->UseIndexedParameters (juceFilter->getNumParameters());
  62. activePlugins.add (this);
  63. zerostruct (auEvent);
  64. auEvent.mArgument.mParameter.mAudioUnit = GetComponentInstance();
  65. auEvent.mArgument.mParameter.mScope = kAudioUnitScope_Global;
  66. auEvent.mArgument.mParameter.mElement = 0;
  67. }
  68. ~JuceAU()
  69. {
  70. delete juceFilter;
  71. juceFilter = 0;
  72. juce_free (channels);
  73. channels = 0;
  74. jassert (activePlugins.contains (this));
  75. activePlugins.removeValue (this);
  76. if (activePlugins.size() == 0)
  77. shutdownJuce_GUI();
  78. }
  79. //==============================================================================
  80. ComponentResult GetPropertyInfo (AudioUnitPropertyID inID,
  81. AudioUnitScope inScope,
  82. AudioUnitElement inElement,
  83. UInt32& outDataSize,
  84. Boolean& outWritable)
  85. {
  86. if (inScope == kAudioUnitScope_Global)
  87. {
  88. if (inID == juceFilterObjectPropertyID)
  89. {
  90. outWritable = false;
  91. outDataSize = sizeof (void*);
  92. return noErr;
  93. }
  94. }
  95. return AUMIDIEffectBase::GetPropertyInfo (inID, inScope, inElement, outDataSize, outWritable);
  96. }
  97. ComponentResult GetProperty (AudioUnitPropertyID inID,
  98. AudioUnitScope inScope,
  99. AudioUnitElement inElement,
  100. void* outData)
  101. {
  102. if (inScope == kAudioUnitScope_Global)
  103. {
  104. if (inID == juceFilterObjectPropertyID)
  105. {
  106. *((void**) outData) = (void*) juceFilter;
  107. return noErr;
  108. }
  109. }
  110. return AUMIDIEffectBase::GetProperty (inID, inScope, inElement, outData);
  111. }
  112. ComponentResult SaveState (CFPropertyListRef* outData)
  113. {
  114. ComponentResult err = AUMIDIEffectBase::SaveState (outData);
  115. if (err != noErr)
  116. return err;
  117. jassert (CFGetTypeID (*outData) == CFDictionaryGetTypeID());
  118. CFMutableDictionaryRef dict = (CFMutableDictionaryRef) *outData;
  119. if (juceFilter != 0)
  120. {
  121. JUCE_NAMESPACE::MemoryBlock state;
  122. juceFilter->getStateInformation (state);
  123. if (state.getSize() > 0)
  124. {
  125. CFDataRef ourState = CFDataCreate (kCFAllocatorDefault, (const uint8*) state, state.getSize());
  126. CFDictionarySetValue (dict, CFSTR("jucePluginState"), ourState);
  127. CFRelease (ourState);
  128. }
  129. }
  130. return noErr;
  131. }
  132. ComponentResult RestoreState (CFPropertyListRef inData)
  133. {
  134. ComponentResult err = AUMIDIEffectBase::RestoreState (inData);
  135. if (err != noErr)
  136. return err;
  137. if (juceFilter != 0)
  138. {
  139. CFDictionaryRef dict = (CFDictionaryRef) inData;
  140. CFDataRef data = 0;
  141. if (CFDictionaryGetValueIfPresent (dict, CFSTR("jucePluginState"),
  142. (const void**) &data))
  143. {
  144. if (data != 0)
  145. {
  146. const int numBytes = (int) CFDataGetLength (data);
  147. const uint8* const rawBytes = CFDataGetBytePtr (data);
  148. if (numBytes > 0)
  149. juceFilter->setStateInformation (rawBytes, numBytes);
  150. }
  151. }
  152. }
  153. return noErr;
  154. }
  155. UInt32 SupportedNumChannels (const AUChannelInfo** outInfo)
  156. {
  157. if (juceFilter == 0)
  158. return 0;
  159. // You need to actually add some configurations to the JucePlugin_PreferredChannelConfigurations
  160. // value in your JucePluginCharacteristics.h file..
  161. jassert (numChannelConfigs > 0);
  162. if (outInfo != 0)
  163. {
  164. for (int i = 0; i < numChannelConfigs; ++i)
  165. {
  166. channelInfo[i].inChannels = channelConfigs[i][0];
  167. channelInfo[i].outChannels = channelConfigs[i][1];
  168. outInfo[i] = channelInfo + i;
  169. }
  170. }
  171. return numChannelConfigs;
  172. }
  173. //==============================================================================
  174. ComponentResult GetParameterInfo (AudioUnitScope inScope,
  175. AudioUnitParameterID inParameterID,
  176. AudioUnitParameterInfo& outParameterInfo)
  177. {
  178. const int index = (int) inParameterID;
  179. if (inScope == kAudioUnitScope_Global
  180. && juceFilter != 0
  181. && index < juceFilter->getNumParameters())
  182. {
  183. outParameterInfo.flags = kAudioUnitParameterFlag_IsWritable
  184. | kAudioUnitParameterFlag_IsReadable
  185. | kAudioUnitParameterFlag_HasCFNameString;
  186. const String name (juceFilter->getParameterName (index));
  187. CharacterFunctions::copy ((char*) outParameterInfo.name,
  188. (const char*) name.toUTF8(),
  189. sizeof (outParameterInfo.name) - 1);
  190. // set whether the param is automatable (unnamed parameters aren't allowed to be automated)
  191. if (name.isEmpty() || ! juceFilter->isParameterAutomatable (index))
  192. outParameterInfo.flags |= kAudioUnitParameterFlag_NonRealTime;
  193. outParameterInfo.cfNameString = PlatformUtilities::juceStringToCFString (name);
  194. outParameterInfo.minValue = 0.0f;
  195. outParameterInfo.maxValue = 1.0f;
  196. outParameterInfo.defaultValue = 0.0f;
  197. outParameterInfo.unit = kAudioUnitParameterUnit_Generic;
  198. return noErr;
  199. }
  200. else
  201. {
  202. return kAudioUnitErr_InvalidParameter;
  203. }
  204. }
  205. ComponentResult GetParameter (AudioUnitParameterID inID,
  206. AudioUnitScope inScope,
  207. AudioUnitElement inElement,
  208. Float32& outValue)
  209. {
  210. if (inScope == kAudioUnitScope_Global && juceFilter != 0)
  211. {
  212. outValue = juceFilter->getParameter ((int) inID);
  213. return noErr;
  214. }
  215. return AUBase::GetParameter (inID, inScope, inElement, outValue);
  216. }
  217. ComponentResult SetParameter (AudioUnitParameterID inID,
  218. AudioUnitScope inScope,
  219. AudioUnitElement inElement,
  220. Float32 inValue,
  221. UInt32 inBufferOffsetInFrames)
  222. {
  223. if (inScope == kAudioUnitScope_Global && juceFilter != 0)
  224. {
  225. juceFilter->setParameter ((int) inID, inValue);
  226. return noErr;
  227. }
  228. return AUBase::SetParameter (inID, inScope, inElement, inValue, inBufferOffsetInFrames);
  229. }
  230. //==============================================================================
  231. ComponentResult Version() { return JucePlugin_VersionCode; }
  232. bool SupportsTail() { return true; }
  233. Float64 GetTailTime() { return 0; }
  234. Float64 GetLatency()
  235. {
  236. jassert (GetSampleRate() > 0);
  237. if (GetSampleRate() <= 0)
  238. return 0.0;
  239. return (JucePlugin_Latency) / GetSampleRate();
  240. }
  241. //==============================================================================
  242. int GetNumCustomUIComponents() { return 1; }
  243. void GetUIComponentDescs (ComponentDescription* inDescArray)
  244. {
  245. inDescArray[0].componentType = kAudioUnitCarbonViewComponentType;
  246. inDescArray[0].componentSubType = JucePlugin_AUSubType;
  247. inDescArray[0].componentManufacturer = JucePlugin_AUManufacturerCode;
  248. inDescArray[0].componentFlags = 0;
  249. inDescArray[0].componentFlagsMask = 0;
  250. }
  251. //==============================================================================
  252. bool getCurrentPositionInfo (AudioFilterBase::CurrentPositionInfo& info)
  253. {
  254. info.timeSigNumerator = 0;
  255. info.timeSigDenominator = 0;
  256. info.timeInSeconds = 0;
  257. info.editOriginTime = 0;
  258. info.ppqPositionOfLastBarStart = 0;
  259. info.isPlaying = false;
  260. info.isRecording = false;
  261. switch (lastSMPTETime.mType)
  262. {
  263. case kSMPTETimeType24:
  264. info.frameRate = AudioFilterBase::CurrentPositionInfo::fps24;
  265. break;
  266. case kSMPTETimeType25:
  267. info.frameRate = AudioFilterBase::CurrentPositionInfo::fps25;
  268. break;
  269. case kSMPTETimeType30Drop:
  270. info.frameRate = AudioFilterBase::CurrentPositionInfo::fps30drop;
  271. break;
  272. case kSMPTETimeType30:
  273. info.frameRate = AudioFilterBase::CurrentPositionInfo::fps30;
  274. break;
  275. case kSMPTETimeType2997:
  276. info.frameRate = AudioFilterBase::CurrentPositionInfo::fps2997;
  277. break;
  278. case kSMPTETimeType2997Drop:
  279. info.frameRate = AudioFilterBase::CurrentPositionInfo::fps2997drop;
  280. break;
  281. //case kSMPTETimeType60:
  282. //case kSMPTETimeType5994:
  283. default:
  284. info.frameRate = AudioFilterBase::CurrentPositionInfo::fpsUnknown;
  285. break;
  286. }
  287. if (CallHostBeatAndTempo (&info.ppqPosition, &info.bpm) != noErr)
  288. {
  289. info.ppqPosition = 0;
  290. info.bpm = 0;
  291. }
  292. UInt32 outDeltaSampleOffsetToNextBeat;
  293. double outCurrentMeasureDownBeat;
  294. float num;
  295. UInt32 den;
  296. if (CallHostMusicalTimeLocation (&outDeltaSampleOffsetToNextBeat, &num, &den,
  297. &outCurrentMeasureDownBeat) == noErr)
  298. {
  299. info.timeSigNumerator = (int) num;
  300. info.timeSigDenominator = den;
  301. info.ppqPositionOfLastBarStart = outCurrentMeasureDownBeat;
  302. }
  303. double outCurrentSampleInTimeLine, outCycleStartBeat, outCycleEndBeat;
  304. Boolean playing, playchanged, looping;
  305. if (CallHostTransportState (&playing,
  306. &playchanged,
  307. &outCurrentSampleInTimeLine,
  308. &looping,
  309. &outCycleStartBeat,
  310. &outCycleEndBeat) == noErr)
  311. {
  312. info.isPlaying = playing;
  313. info.timeInSeconds = outCurrentSampleInTimeLine / GetSampleRate();
  314. }
  315. return true;
  316. }
  317. void sendAUEvent (const AudioUnitEventType type, const int index) throw()
  318. {
  319. if (AUEventListenerNotify != 0)
  320. {
  321. auEvent.mEventType = type;
  322. auEvent.mArgument.mParameter.mParameterID = (AudioUnitParameterID) index;
  323. AUEventListenerNotify (0, 0, &auEvent);
  324. }
  325. }
  326. void informHostOfParameterChange (int index, float newValue)
  327. {
  328. if (juceFilter != 0)
  329. {
  330. juceFilter->setParameter (index, newValue);
  331. sendAUEvent (kAudioUnitEvent_ParameterValueChange, index);
  332. }
  333. }
  334. void informHostOfParameterGestureBegin (int index)
  335. {
  336. if (juceFilter != 0)
  337. sendAUEvent (kAudioUnitEvent_BeginParameterChangeGesture, index);
  338. }
  339. void informHostOfParameterGestureEnd (int index)
  340. {
  341. if (juceFilter != 0)
  342. sendAUEvent (kAudioUnitEvent_EndParameterChangeGesture, index);
  343. }
  344. void informHostOfStateChange()
  345. {
  346. // xxx is there an AU equivalent?
  347. }
  348. //==============================================================================
  349. ComponentResult Initialize()
  350. {
  351. AUMIDIEffectBase::Initialize();
  352. prepareToPlay();
  353. return noErr;
  354. }
  355. void Cleanup()
  356. {
  357. AUMIDIEffectBase::Cleanup();
  358. if (juceFilter != 0)
  359. juceFilter->releaseResources();
  360. bufferSpace.setSize (2, 16);
  361. midiEvents.clear();
  362. prepared = false;
  363. }
  364. ComponentResult Reset (AudioUnitScope inScope, AudioUnitElement inElement)
  365. {
  366. if (! prepared)
  367. prepareToPlay();
  368. return AUMIDIEffectBase::Reset (inScope, inElement);
  369. }
  370. void prepareToPlay()
  371. {
  372. if (juceFilter != 0)
  373. {
  374. juceFilter->setPlayConfigDetails (GetInput(0)->GetStreamFormat().mChannelsPerFrame,
  375. GetOutput(0)->GetStreamFormat().mChannelsPerFrame,
  376. GetSampleRate(),
  377. GetMaxFramesPerSlice());
  378. bufferSpace.setSize (juceFilter->getNumInputChannels() + juceFilter->getNumOutputChannels(),
  379. GetMaxFramesPerSlice() + 32);
  380. juceFilter->prepareToPlay (GetSampleRate(),
  381. GetMaxFramesPerSlice());
  382. midiEvents.clear();
  383. juce_free (channels);
  384. channels = (float**) juce_calloc (sizeof (float*) * jmax (juceFilter->getNumInputChannels(),
  385. juceFilter->getNumOutputChannels()) + 4);
  386. prepared = true;
  387. }
  388. }
  389. ComponentResult Render (AudioUnitRenderActionFlags &ioActionFlags,
  390. const AudioTimeStamp& inTimeStamp,
  391. UInt32 nFrames)
  392. {
  393. lastSMPTETime = inTimeStamp.mSMPTETime;
  394. return AUMIDIEffectBase::Render (ioActionFlags, inTimeStamp, nFrames);
  395. }
  396. OSStatus ProcessBufferLists (AudioUnitRenderActionFlags& ioActionFlags,
  397. const AudioBufferList& inBuffer,
  398. AudioBufferList& outBuffer,
  399. UInt32 numSamples)
  400. {
  401. if (juceFilter != 0)
  402. {
  403. jassert (prepared);
  404. int numOutChans = 0;
  405. int nextSpareBufferChan = 0;
  406. bool needToReinterleave = false;
  407. const int numIn = juceFilter->getNumInputChannels();
  408. const int numOut = juceFilter->getNumOutputChannels();
  409. unsigned int i;
  410. for (i = 0; i < outBuffer.mNumberBuffers; ++i)
  411. {
  412. AudioBuffer& buf = outBuffer.mBuffers[i];
  413. if (buf.mNumberChannels == 1)
  414. {
  415. channels [numOutChans++] = (float*) buf.mData;
  416. }
  417. else
  418. {
  419. needToReinterleave = true;
  420. for (unsigned int subChan = 0; subChan < buf.mNumberChannels && numOutChans < numOut; ++subChan)
  421. channels [numOutChans++] = bufferSpace.getSampleData (nextSpareBufferChan++);
  422. }
  423. if (numOutChans >= numOut)
  424. break;
  425. }
  426. int numInChans = 0;
  427. for (i = 0; i < inBuffer.mNumberBuffers; ++i)
  428. {
  429. const AudioBuffer& buf = inBuffer.mBuffers[i];
  430. if (buf.mNumberChannels == 1)
  431. {
  432. if (numInChans < numOut)
  433. memcpy (channels [numInChans], (const float*) buf.mData, sizeof (float) * numSamples);
  434. else
  435. channels [numInChans] = (float*) buf.mData;
  436. }
  437. else
  438. {
  439. // need to de-interleave..
  440. for (unsigned int subChan = 0; subChan < buf.mNumberChannels && numInChans < numIn; ++subChan)
  441. {
  442. float* dest;
  443. if (numInChans >= numOut)
  444. {
  445. dest = bufferSpace.getSampleData (nextSpareBufferChan++);
  446. channels [numInChans++] = dest;
  447. }
  448. else
  449. {
  450. dest = channels [numInChans++];
  451. }
  452. const float* src = ((const float*) buf.mData) + subChan;
  453. for (int j = numSamples; --j >= 0;)
  454. {
  455. *dest++ = *src;
  456. src += buf.mNumberChannels;
  457. }
  458. }
  459. }
  460. if (numInChans >= numIn)
  461. break;
  462. }
  463. {
  464. AudioSampleBuffer buffer (channels, jmax (numIn, numOut), numSamples);
  465. const ScopedLock sl (juceFilter->getCallbackLock());
  466. if (juceFilter->isSuspended())
  467. {
  468. for (int i = 0; i < numOut; ++i)
  469. zeromem (channels [i], sizeof (float) * numSamples);
  470. }
  471. else
  472. {
  473. juceFilter->processBlock (buffer, midiEvents);
  474. }
  475. }
  476. if (! midiEvents.isEmpty())
  477. {
  478. #if JucePlugin_ProducesMidiOutput
  479. const uint8* midiEventData;
  480. int midiEventSize, midiEventPosition;
  481. MidiBuffer::Iterator i (midiEvents);
  482. while (i.getNextEvent (midiEventData, midiEventSize, midiEventPosition))
  483. {
  484. jassert (midiEventPosition >= 0 && midiEventPosition < (int) numSamples);
  485. //xxx
  486. }
  487. #else
  488. // if your plugin creates midi messages, you'll need to set
  489. // the JucePlugin_ProducesMidiOutput macro to 1 in your
  490. // JucePluginCharacteristics.h file
  491. //jassert (midiEvents.getNumEvents() <= numMidiEventsComingIn);
  492. #endif
  493. midiEvents.clear();
  494. }
  495. if (needToReinterleave)
  496. {
  497. nextSpareBufferChan = 0;
  498. for (i = 0; i < outBuffer.mNumberBuffers; ++i)
  499. {
  500. AudioBuffer& buf = outBuffer.mBuffers[i];
  501. if (buf.mNumberChannels > 1)
  502. {
  503. for (unsigned int subChan = 0; subChan < buf.mNumberChannels; ++subChan)
  504. {
  505. const float* src = bufferSpace.getSampleData (nextSpareBufferChan++);
  506. float* dest = ((float*) buf.mData) + subChan;
  507. for (int j = numSamples; --j >= 0;)
  508. {
  509. *dest = *src++;
  510. dest += buf.mNumberChannels;
  511. }
  512. }
  513. }
  514. }
  515. }
  516. #if ! JucePlugin_SilenceInProducesSilenceOut
  517. ioActionFlags &= ~kAudioUnitRenderAction_OutputIsSilence;
  518. #endif
  519. }
  520. return noErr;
  521. }
  522. protected:
  523. OSStatus HandleMidiEvent (UInt8 nStatus,
  524. UInt8 inChannel,
  525. UInt8 inData1,
  526. UInt8 inData2,
  527. long inStartFrame)
  528. {
  529. #if JucePlugin_WantsMidiInput
  530. uint8 data [4];
  531. data[0] = nStatus | inChannel;
  532. data[1] = inData1;
  533. data[2] = inData2;
  534. midiEvents.addEvent (data, 3, inStartFrame);
  535. #endif
  536. return noErr;
  537. }
  538. //==============================================================================
  539. private:
  540. AudioFilterBase* juceFilter;
  541. AudioSampleBuffer bufferSpace;
  542. float** channels;
  543. MidiBuffer midiEvents;
  544. bool prepared;
  545. SMPTETime lastSMPTETime;
  546. AUChannelInfo channelInfo [numChannelConfigs];
  547. AudioUnitEvent auEvent;
  548. };
  549. //==============================================================================
  550. class JuceAUComponentHolder : public Component
  551. {
  552. public:
  553. JuceAUComponentHolder (Component* const editorComp)
  554. {
  555. addAndMakeVisible (editorComp);
  556. setOpaque (true);
  557. setVisible (true);
  558. setBroughtToFrontOnMouseClick (true);
  559. #if ! JucePlugin_EditorRequiresKeyboardFocus
  560. setWantsKeyboardFocus (false);
  561. #endif
  562. }
  563. ~JuceAUComponentHolder()
  564. {
  565. }
  566. void resized()
  567. {
  568. if (getNumChildComponents() > 0)
  569. getChildComponent (0)->setBounds (0, 0, getWidth(), getHeight());
  570. }
  571. void paint (Graphics& g)
  572. {
  573. }
  574. };
  575. //==============================================================================
  576. class JuceAUView : public AUCarbonViewBase,
  577. public ComponentListener,
  578. public MouseListener,
  579. public Timer
  580. {
  581. AudioFilterBase* juceFilter;
  582. AudioFilterEditor* editorComp;
  583. Component* windowComp;
  584. bool recursive;
  585. int mx, my;
  586. public:
  587. JuceAUView (AudioUnitCarbonView auview)
  588. : AUCarbonViewBase (auview),
  589. juceFilter (0),
  590. editorComp (0),
  591. windowComp (0),
  592. recursive (false),
  593. mx (0),
  594. my (0)
  595. {
  596. }
  597. ~JuceAUView()
  598. {
  599. deleteUI();
  600. }
  601. ComponentResult CreateUI (Float32 inXOffset, Float32 inYOffset)
  602. {
  603. if (juceFilter == 0)
  604. {
  605. UInt32 propertySize = sizeof (&juceFilter);
  606. AudioUnitGetProperty (GetEditAudioUnit(),
  607. juceFilterObjectPropertyID,
  608. kAudioUnitScope_Global,
  609. 0,
  610. &juceFilter,
  611. &propertySize);
  612. }
  613. if (juceFilter != 0)
  614. {
  615. deleteUI();
  616. editorComp = juceFilter->createEditorIfNeeded();
  617. const int w = editorComp->getWidth();
  618. const int h = editorComp->getHeight();
  619. editorComp->setOpaque (true);
  620. editorComp->setVisible (true);
  621. windowComp = new JuceAUComponentHolder (editorComp);
  622. windowComp->setBounds ((int) inXOffset, (int) inYOffset, w, h);
  623. windowComp->addToDesktop (0, (void*) mCarbonPane);
  624. SizeControl (mCarbonPane, w, h);
  625. editorComp->addComponentListener (this);
  626. windowComp->addMouseListener (this, true);
  627. startTimer (20);
  628. }
  629. else
  630. {
  631. jassertfalse // can't get a pointer to our effect
  632. }
  633. return noErr;
  634. }
  635. void componentMovedOrResized (Component& component,
  636. bool wasMoved,
  637. bool wasResized)
  638. {
  639. if (! recursive)
  640. {
  641. recursive = true;
  642. if (editorComp != 0 && wasResized)
  643. {
  644. const int w = jmax (32, editorComp->getWidth());
  645. const int h = jmax (32, editorComp->getHeight());
  646. SizeControl (mCarbonPane, w, h);
  647. if (windowComp->getWidth() != w
  648. || windowComp->getHeight() != h)
  649. {
  650. windowComp->setSize (w, h);
  651. }
  652. editorComp->repaint();
  653. }
  654. recursive = false;
  655. }
  656. }
  657. void timerCallback()
  658. {
  659. // for some stupid Apple-related reason, mouse move events just don't seem to get sent
  660. // to the windows in an AU, so we have to bodge it here and simulate them with a
  661. // timer..
  662. if (editorComp != 0)
  663. {
  664. int x, y;
  665. Desktop::getInstance().getMousePosition (x, y);
  666. if (x != mx || y != my)
  667. {
  668. mx = x;
  669. my = y;
  670. if (! ModifierKeys::getCurrentModifiers().isAnyMouseButtonDown())
  671. {
  672. for (int i = ComponentPeer::getNumPeers(); --i >= 0;)
  673. {
  674. ComponentPeer* const peer = ComponentPeer::getPeer (i);
  675. const int rx = x - peer->getComponent()->getX();
  676. const int ry = y - peer->getComponent()->getY();
  677. if (peer->contains (rx, ry, false) && peer->getComponent()->isShowing())
  678. {
  679. peer->handleMouseMove (rx, ry, Time::currentTimeMillis());
  680. break;
  681. }
  682. }
  683. }
  684. }
  685. }
  686. }
  687. void mouseMove (const MouseEvent& e)
  688. {
  689. Desktop::getInstance().getMousePosition (mx, my);
  690. startTimer (20);
  691. }
  692. private:
  693. void deleteUI()
  694. {
  695. PopupMenu::dismissAllActiveMenus();
  696. // there's some kind of component currently modal, but the host
  697. // is trying to delete our plugin..
  698. jassert (Component::getCurrentlyModalComponent() == 0);
  699. if (editorComp != 0)
  700. juceFilter->editorBeingDeleted (editorComp);
  701. deleteAndZero (editorComp);
  702. deleteAndZero (windowComp);
  703. }
  704. };
  705. //==============================================================================
  706. #define JUCE_COMPONENT_ENTRYX(Class, Name, Suffix) \
  707. extern "C" __attribute__((visibility("default"))) ComponentResult Name ## Suffix (ComponentParameters* params, Class* obj); \
  708. extern "C" __attribute__((visibility("default"))) ComponentResult Name ## Suffix (ComponentParameters* params, Class* obj) \
  709. { \
  710. return ComponentEntryPoint<Class>::Dispatch(params, obj); \
  711. }
  712. #define JUCE_COMPONENT_ENTRY(Class, Name, Suffix) JUCE_COMPONENT_ENTRYX(Class, Name, Suffix)
  713. JUCE_COMPONENT_ENTRY (JuceAU, JucePlugin_AUExportPrefix, Entry)
  714. JUCE_COMPONENT_ENTRY (JuceAUView, JucePlugin_AUExportPrefix, ViewEntry)