From e1a599596501350218d79d17a0aedf8f2fbcc58b Mon Sep 17 00:00:00 2001 From: Julian Storer Date: Thu, 1 Oct 2009 20:08:42 +0100 Subject: [PATCH] Minor whitespace tidying-up --- .../demo/src/JucePluginCharacteristics.h | 2 +- .../wrapper/AU/juce_AU_Wrapper.mm | 2 +- .../wrapper/RTAS/juce_RTAS_MacUtilities.mm | 6 +- .../wrapper/RTAS/juce_RTAS_Wrapper.cpp | 2006 ++++++------ .../src/demos/AudioDemoLatencyPage.cpp | 4 +- src/audio/devices/juce_AudioDeviceManager.h | 2 +- src/audio/midi/juce_MidiMessage.h | 4 +- .../special/juce_WebBrowserComponent.h | 4 +- src/juce_app_includes.h | 26 +- .../mac/juce_mac_CarbonViewWrapperComponent.h | 2 +- src/native/mac/juce_mac_CoreAudio.cpp | 2748 ++++++++--------- src/native/windows/juce_win32_SystemStats.cpp | 2 +- 12 files changed, 2404 insertions(+), 2404 deletions(-) diff --git a/extras/audio plugins/demo/src/JucePluginCharacteristics.h b/extras/audio plugins/demo/src/JucePluginCharacteristics.h index 362c42afd7..1086291603 100644 --- a/extras/audio plugins/demo/src/JucePluginCharacteristics.h +++ b/extras/audio plugins/demo/src/JucePluginCharacteristics.h @@ -154,7 +154,7 @@ */ #define JucePlugin_SilenceInProducesSilenceOut 0 -/** If your plugin has a tail, you can set the length here and this information +/** If your plugin has a tail, you can set the length here and this information will be passed on to the host. (Not all formats/hosts might actually use this, though) */ diff --git a/extras/audio plugins/wrapper/AU/juce_AU_Wrapper.mm b/extras/audio plugins/wrapper/AU/juce_AU_Wrapper.mm index 5eba19a845..5577752fcb 100644 --- a/extras/audio plugins/wrapper/AU/juce_AU_Wrapper.mm +++ b/extras/audio plugins/wrapper/AU/juce_AU_Wrapper.mm @@ -1337,7 +1337,7 @@ private: recursive = false; } } - + bool keyPressed (const KeyPress& kp) { if (! kp.getModifiers().isCommandDown()) diff --git a/extras/audio plugins/wrapper/RTAS/juce_RTAS_MacUtilities.mm b/extras/audio plugins/wrapper/RTAS/juce_RTAS_MacUtilities.mm index 88ae5ba051..47282f09d5 100644 --- a/extras/audio plugins/wrapper/RTAS/juce_RTAS_MacUtilities.mm +++ b/extras/audio plugins/wrapper/RTAS/juce_RTAS_MacUtilities.mm @@ -116,11 +116,11 @@ static bool isJuceWindow (WindowRef w) throw() { ComponentPeer* peer = ComponentPeer::getPeer(i); NSView* view = (NSView*) peer->getNativeHandle(); - + if ([[view window] windowRef] == w) return true; } - + return false; } @@ -132,7 +132,7 @@ void forwardCurrentKeyEventToHostWindow() while (IsValidWindowPtr (w) && isJuceWindow (w)) { w = GetNextWindowOfClass (w, kDocumentWindowClass, true); - + if (w == original) break; } diff --git a/extras/audio plugins/wrapper/RTAS/juce_RTAS_Wrapper.cpp b/extras/audio plugins/wrapper/RTAS/juce_RTAS_Wrapper.cpp index 116634edf3..d8e1a4cb2a 100644 --- a/extras/audio plugins/wrapper/RTAS/juce_RTAS_Wrapper.cpp +++ b/extras/audio plugins/wrapper/RTAS/juce_RTAS_Wrapper.cpp @@ -1,1003 +1,1003 @@ -/* - ============================================================================== - - This file is part of the JUCE library - "Jules' Utility Class Extensions" - Copyright 2004-9 by Raw Material Software Ltd. - - ------------------------------------------------------------------------------ - - JUCE can be redistributed and/or modified under the terms of the GNU General - Public License (Version 2), as published by the Free Software Foundation. - A copy of the license is included in the JUCE distribution, or can be found - online at www.gnu.org/licenses. - - JUCE is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU General Public License for more details. - - ------------------------------------------------------------------------------ - - To release a closed-source product which uses JUCE, commercial licenses are - available: visit www.rawmaterialsoftware.com/juce for more information. - - ============================================================================== -*/ - -#ifdef _MSC_VER - // (this is a workaround for a build problem in VC9) - #define _DO_NOT_DECLARE_INTERLOCKED_INTRINSICS_IN_MEMORY - #include -#endif - -#include "juce_RTAS_DigiCode_Header.h" - -#if JucePlugin_Build_RTAS - -#ifdef _MSC_VER - #include "Mac2Win.H" -#endif - -/* Note about include paths - ------------------------ - - To be able to include all the Digidesign headers correctly, you'll need to add this - lot to your include path: - - c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\EffectClasses - c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\ProcessClasses - c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\ProcessClasses\Interfaces - c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\Utilities - c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\RTASP_Adapt - c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\CoreClasses - c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\Controls - c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\Meters - c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\ViewClasses - c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\DSPClasses - c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\Interfaces - c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\common - c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\common\Platform - c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\SignalProcessing\Public - C:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugIns\DSPManager\Interfaces - c:\yourdirectory\PT_80_SDK\AlturaPorts\SADriver\Interfaces - c:\yourdirectory\PT_80_SDK\AlturaPorts\DigiPublic\Interfaces - c:\yourdirectory\PT_80_SDK\AlturaPorts\Fic\Interfaces\DAEClient - c:\yourdirectory\PT_80_SDK\AlturaPorts\NewFileLibs\Cmn - c:\yourdirectory\PT_80_SDK\AlturaPorts\NewFileLibs\DOA - c:\yourdirectory\PT_80_SDK\AlturaPorts\AlturaSource\PPC_H - c:\yourdirectory\PT_80_SDK\AlturaPorts\AlturaSource\AppSupport - c:\yourdirectory\PT_80_SDK\AvidCode\AVX2sdk\AVX\avx2\avx2sdk\inc - C:\yourdirectory\PT_80_SDK\xplat\AVX\avx2\avx2sdk\inc - - NB. If you hit a huge pile of bugs around here, make sure that you've not got the - Apple QuickTime headers before the PT headers in your path, because there are - some filename clashes between them. - -*/ -#include "CEffectGroupMIDI.h" -#include "CEffectProcessMIDI.h" -#include "CEffectProcessRTAS.h" -#include "CCustomView.h" -#include "CEffectTypeRTAS.h" -#include "CPluginControl.h" -#include "CPluginControl_OnOff.h" -#include "FicProcessTokens.h" - -//============================================================================== -#ifdef _MSC_VER - #pragma pack (push, 8) -#endif - -#include "../juce_PluginHeaders.h" - -#ifdef _MSC_VER - #pragma pack (pop) - - #if JUCE_DEBUG - #define PT_LIB_PATH JucePlugin_WinBag_path "\\Debug\\lib\\" - #else - #define PT_LIB_PATH JucePlugin_WinBag_path "\\Release\\lib\\" - #endif - - #pragma comment(lib, PT_LIB_PATH "DAE.lib") - #pragma comment(lib, PT_LIB_PATH "DigiExt.lib") - #pragma comment(lib, PT_LIB_PATH "DSI.lib") - #pragma comment(lib, PT_LIB_PATH "PluginLib.lib") - -#endif - -#undef Component -#undef MemoryBlock - -//============================================================================== -#if JUCE_WIN32 - extern void JUCE_CALLTYPE attachSubWindow (void* hostWindow, int& titleW, int& titleH, JUCE_NAMESPACE::Component* comp); - extern void JUCE_CALLTYPE resizeHostWindow (void* hostWindow, int& titleW, int& titleH, JUCE_NAMESPACE::Component* comp); - #if ! JucePlugin_EditorRequiresKeyboardFocus - extern void JUCE_CALLTYPE passFocusToHostWindow (void* hostWindow); - #endif -#else - extern void* attachSubWindow (void* hostWindowRef, JUCE_NAMESPACE::Component* comp); - extern void removeSubWindow (void* nsWindow, JUCE_NAMESPACE::Component* comp); - extern void forwardCurrentKeyEventToHostWindow(); -#endif - -const int midiBufferSize = 1024; -const OSType juceChunkType = 'juce'; -static const int bypassControlIndex = 1; - -//============================================================================== -/** Somewhere in the codebase of your plugin, you need to implement this function - and make it return a new instance of the filter subclass that you're building. -*/ -extern AudioProcessor* JUCE_CALLTYPE createPluginFilter(); - - -//============================================================================== -static float longToFloat (const long n) throw() -{ - return (float) ((((double) n) + (double) 0x80000000) / (double) 0xffffffff); -} - -static long floatToLong (const float n) throw() -{ - return roundDoubleToInt (jlimit (-(double) 0x80000000, - (double) 0x7fffffff, - n * (double) 0xffffffff - (double) 0x80000000)); -} - -static int numInstances = 0; - -//============================================================================== -class JucePlugInProcess : public CEffectProcessMIDI, - public CEffectProcessRTAS, - public AudioProcessorListener, - public AudioPlayHead, - public AsyncUpdater -{ -public: - //============================================================================== - JucePlugInProcess() - : midiBufferNode (0), - midiTransport (0), - channels (0), - prepared (false), - sampleRate (44100.0) - { - juceFilter = createPluginFilter(); - jassert (juceFilter != 0); - - AddChunk (juceChunkType, "Juce Audio Plugin Data"); - - ++numInstances; - } - - ~JucePlugInProcess() - { - if (mLoggedIn) - MIDILogOut(); - - deleteAndZero (midiBufferNode); - deleteAndZero (midiTransport); - - if (prepared) - juceFilter->releaseResources(); - - delete juceFilter; - juce_free (channels); - - if (--numInstances == 0) - shutdownJuce_GUI(); - } - - //============================================================================== - class JuceCustomUIView : public CCustomView, - public Timer - { - public: - //============================================================================== - JuceCustomUIView (AudioProcessor* const filter_, - JucePlugInProcess* const process_) - : filter (filter_), - process (process_), - wrapper (0), - editorComp (0) - { - // setting the size in here crashes PT for some reason, so keep it simple.. - } - - ~JuceCustomUIView() - { - deleteEditorComp(); - } - - //============================================================================== - void updateSize() - { - if (editorComp == 0) - { - editorComp = filter->createEditorIfNeeded(); - jassert (editorComp != 0); - } - - Rect oldRect; - GetRect (&oldRect); - - Rect r; - r.left = 0; - r.top = 0; - r.right = editorComp->getWidth(); - r.bottom = editorComp->getHeight(); - SetRect (&r); - - if ((oldRect.right != r.right) || (oldRect.bottom != r.bottom)) - startTimer (50); - } - - void timerCallback() - { - if (! JUCE_NAMESPACE::Component::isMouseButtonDownAnywhere()) - { - stopTimer(); - - // Send a token to the host to tell it about the resize - SSetProcessWindowResizeToken token (process->fRootNameId, process->fRootNameId); - FicSDSDispatchToken (&token); - } - } - - void attachToWindow (GrafPtr port) - { - if (port != 0) - { - updateSize(); - -#if JUCE_WIN32 - void* const hostWindow = (void*) ASI_GethWnd ((WindowPtr) port); -#else - void* const hostWindow = (void*) GetWindowFromPort (port); -#endif - deleteAndZero (wrapper); - - wrapper = new EditorCompWrapper (hostWindow, editorComp, this); - } - else - { - deleteEditorComp(); - } - } - - void DrawContents (Rect*) - { -#if JUCE_WIN32 - if (wrapper != 0) - { - ComponentPeer* const peer = wrapper->getPeer(); - - if (peer != 0) - { - // (seems to be required in PT6.4, but not in 7.x) - peer->repaint (0, 0, wrapper->getWidth(), wrapper->getHeight()); - } - } -#endif - } - - void DrawBackground (Rect*) {} - - //============================================================================== - private: - AudioProcessor* const filter; - JucePlugInProcess* const process; - JUCE_NAMESPACE::Component* wrapper; - AudioProcessorEditor* editorComp; - - void deleteEditorComp() - { - if (editorComp != 0 || wrapper != 0) - { -#if JUCE_MAC - const ScopedAutoReleasePool pool; -#endif - PopupMenu::dismissAllActiveMenus(); - - JUCE_NAMESPACE::Component* const modalComponent = JUCE_NAMESPACE::Component::getCurrentlyModalComponent(); - if (modalComponent != 0) - modalComponent->exitModalState (0); - - filter->editorBeingDeleted (editorComp); - - deleteAndZero (editorComp); - deleteAndZero (wrapper); - } - } - - //============================================================================== - // A component to hold the AudioProcessorEditor, and cope with some housekeeping - // chores when it changes or repaints. - class EditorCompWrapper : public JUCE_NAMESPACE::Component -#if ! JUCE_MAC - , public FocusChangeListener -#endif - { - public: - EditorCompWrapper (void* const hostWindow_, - Component* const editorComp, - JuceCustomUIView* const owner_) - : hostWindow (hostWindow_), - owner (owner_), - titleW (0), - titleH (0) - { -#if ! JucePlugin_EditorRequiresKeyboardFocus - setMouseClickGrabsKeyboardFocus (false); - setWantsKeyboardFocus (false); -#endif - setOpaque (true); - setBroughtToFrontOnMouseClick (true); - setBounds (editorComp->getBounds()); - editorComp->setTopLeftPosition (0, 0); - addAndMakeVisible (editorComp); - -#if JUCE_WIN32 - attachSubWindow (hostWindow, titleW, titleH, this); -#else - nsWindow = attachSubWindow (hostWindow, this); -#endif - setVisible (true); - -#if JUCE_WIN32 && ! JucePlugin_EditorRequiresKeyboardFocus - Desktop::getInstance().addFocusChangeListener (this); -#endif - } - - ~EditorCompWrapper() - { -#if JUCE_WIN32 && ! JucePlugin_EditorRequiresKeyboardFocus - Desktop::getInstance().removeFocusChangeListener (this); -#endif - -#if JUCE_MAC - removeSubWindow (nsWindow, this); -#endif - } - - void paint (Graphics&) - { - } - - void resized() - { - JUCE_NAMESPACE::Component* const c = getChildComponent (0); - - if (c != 0) - c->setBounds (0, 0, getWidth(), getHeight()); - - repaint(); - } - -#if JUCE_WIN32 - void globalFocusChanged (JUCE_NAMESPACE::Component*) - { - #if ! JucePlugin_EditorRequiresKeyboardFocus - if (hasKeyboardFocus (true)) - passFocusToHostWindow (hostWindow); - #endif - } -#endif - - void childBoundsChanged (JUCE_NAMESPACE::Component* child) - { - setSize (child->getWidth(), child->getHeight()); - child->setTopLeftPosition (0, 0); - -#if JUCE_WIN32 - resizeHostWindow (hostWindow, titleW, titleH, this); -#endif - owner->updateSize(); - } - - void userTriedToCloseWindow() - { - } - -#if JUCE_MAC && JucePlugin_EditorRequiresKeyboardFocus - bool keyPressed (const KeyPress& kp) - { - owner->updateSize(); - forwardCurrentKeyEventToHostWindow(); - return true; - } -#endif - //============================================================================== - juce_UseDebuggingNewOperator - - private: - void* const hostWindow; - void* nsWindow; - JuceCustomUIView* const owner; - int titleW, titleH; - }; - }; - - JuceCustomUIView* getView() const - { - return dynamic_cast (fOurPlugInView); - } - - void GetViewRect (Rect* size) - { - if (getView() != 0) - getView()->updateSize(); - - CEffectProcessRTAS::GetViewRect (size); - } - - CPlugInView* CreateCPlugInView() - { - return new JuceCustomUIView (juceFilter, this); - } - - void SetViewPort (GrafPtr port) - { - CEffectProcessRTAS::SetViewPort (port); - - if (getView() != 0) - getView()->attachToWindow (port); - } - - //============================================================================== -protected: - ComponentResult GetDelaySamplesLong (long* aNumSamples) - { - if (aNumSamples != 0) - *aNumSamples = juceFilter != 0 ? juceFilter->getLatencySamples() : 0; - - return noErr; - } - - //============================================================================== - void EffectInit() - { - SFicPlugInStemFormats stems; - GetProcessType()->GetStemFormats (&stems); - - juceFilter->setPlayConfigDetails (fNumInputs, fNumOutputs, - juceFilter->getSampleRate(), juceFilter->getBlockSize()); - - AddControl (new CPluginControl_OnOff ('bypa', "Master Bypass\nMastrByp\nMByp\nByp", false, true)); - DefineMasterBypassControlIndex (bypassControlIndex); - - for (int i = 0; i < juceFilter->getNumParameters(); ++i) - AddControl (new JucePluginControl (juceFilter, i)); - - // we need to do this midi log-in to get timecode, regardless of whether - // the plugin actually uses midi... - if (MIDILogIn() == noErr) - { -#if JucePlugin_WantsMidiInput - CEffectType* const type = dynamic_cast (this->GetProcessType()); - - if (type != 0) - { - char nodeName [64]; - type->GetProcessTypeName (63, nodeName); - p2cstrcpy (nodeName, reinterpret_cast (nodeName)); - - midiBufferNode = new CEffectMIDIOtherBufferedNode (&mMIDIWorld, - 8192, - eLocalNode, - nodeName, - midiBuffer); - - midiBufferNode->Initialize (1, true); - } -#endif - } - - midiTransport = new CEffectMIDITransport (&mMIDIWorld); - - juceFilter->setPlayHead (this); - juceFilter->addListener (this); - } - - void handleAsyncUpdate() - { - if (! prepared) - { - sampleRate = gProcessGroup->GetSampleRate(); - jassert (sampleRate > 0); - - juce_free (channels); - channels = (float**) juce_calloc (sizeof (float*) * jmax (juceFilter->getNumInputChannels(), - juceFilter->getNumOutputChannels())); - - juceFilter->setPlayConfigDetails (fNumInputs, fNumOutputs, - sampleRate, mRTGlobals->mHWBufferSizeInSamples); - - juceFilter->prepareToPlay (sampleRate, - mRTGlobals->mHWBufferSizeInSamples); - - prepared = true; - } - } - - void RenderAudio (float** inputs, float** outputs, long numSamples) - { - if (! prepared) - { - triggerAsyncUpdate(); - bypassBuffers (inputs, outputs, numSamples); - return; - } - - if (mBypassed) - { - bypassBuffers (inputs, outputs, numSamples); - return; - } - -#if JucePlugin_WantsMidiInput - midiEvents.clear(); - - const Cmn_UInt32 bufferSize = mRTGlobals->mHWBufferSizeInSamples; - - if (midiBufferNode->GetAdvanceScheduleTime() != bufferSize) - midiBufferNode->SetAdvanceScheduleTime (bufferSize); - - if (midiBufferNode->FillMIDIBuffer (mRTGlobals->mRunningTime, numSamples) == noErr) - { - jassert (midiBufferNode->GetBufferPtr() != 0); - const int numMidiEvents = midiBufferNode->GetBufferSize(); - - for (int i = 0; i < numMidiEvents; ++i) - { - const DirectMidiPacket& m = midiBuffer[i]; - - jassert ((int) m.mTimestamp < numSamples); - - midiEvents.addEvent (m.mData, m.mLength, - jlimit (0, (int) numSamples - 1, (int) m.mTimestamp)); - } - } -#endif - -#if defined (JUCE_DEBUG) || JUCE_LOG_ASSERTIONS - const int numMidiEventsComingIn = midiEvents.getNumEvents(); - (void) numMidiEventsComingIn; -#endif - - { - const ScopedLock sl (juceFilter->getCallbackLock()); - - const int numIn = juceFilter->getNumInputChannels(); - const int numOut = juceFilter->getNumOutputChannels(); - const int totalChans = jmax (numIn, numOut); - - if (juceFilter->isSuspended()) - { - for (int i = 0; i < numOut; ++i) - zeromem (outputs [i], sizeof (float) * numSamples); - } - else - { - { - int i; - for (i = 0; i < numOut; ++i) - { - channels[i] = outputs [i]; - - if (i < numIn && inputs != outputs) - memcpy (outputs [i], inputs[i], sizeof (float) * numSamples); - } - - for (; i < numIn; ++i) - channels [i] = inputs [i]; - } - - AudioSampleBuffer chans (channels, totalChans, numSamples); - - juceFilter->processBlock (chans, midiEvents); - } - } - - if (! midiEvents.isEmpty()) - { -#if JucePlugin_ProducesMidiOutput - const juce::uint8* midiEventData; - int midiEventSize, midiEventPosition; - MidiBuffer::Iterator i (midiEvents); - - while (i.getNextEvent (midiEventData, midiEventSize, midiEventPosition)) - { -// jassert (midiEventPosition >= 0 && midiEventPosition < (int) numSamples); - - //xxx - } -#else - // if your plugin creates midi messages, you'll need to set - // the JucePlugin_ProducesMidiOutput macro to 1 in your - // JucePluginCharacteristics.h file - jassert (midiEvents.getNumEvents() <= numMidiEventsComingIn); -#endif - - midiEvents.clear(); - } - } - - //============================================================================== - ComponentResult GetChunkSize (OSType chunkID, long* size) - { - if (chunkID == juceChunkType) - { - tempFilterData.setSize (0); - juceFilter->getStateInformation (tempFilterData); - - *size = sizeof (SFicPlugInChunkHeader) + tempFilterData.getSize(); - return noErr; - } - - return CEffectProcessMIDI::GetChunkSize (chunkID, size); - } - - ComponentResult GetChunk (OSType chunkID, SFicPlugInChunk* chunk) - { - if (chunkID == juceChunkType) - { - if (tempFilterData.getSize() == 0) - juceFilter->getStateInformation (tempFilterData); - - chunk->fSize = sizeof (SFicPlugInChunkHeader) + tempFilterData.getSize(); - tempFilterData.copyTo ((void*) chunk->fData, 0, tempFilterData.getSize()); - - tempFilterData.setSize (0); - - return noErr; - } - - return CEffectProcessMIDI::GetChunk (chunkID, chunk); - } - - ComponentResult SetChunk (OSType chunkID, SFicPlugInChunk* chunk) - { - if (chunkID == juceChunkType) - { - tempFilterData.setSize (0); - - if (chunk->fSize - sizeof (SFicPlugInChunkHeader) > 0) - { - juceFilter->setStateInformation ((void*) chunk->fData, - chunk->fSize - sizeof (SFicPlugInChunkHeader)); - } - - return noErr; - } - - return CEffectProcessMIDI::SetChunk (chunkID, chunk); - } - - //============================================================================== - ComponentResult UpdateControlValue (long controlIndex, long value) - { - if (controlIndex != bypassControlIndex) - juceFilter->setParameter (controlIndex - 2, longToFloat (value)); - else - mBypassed = (value > 0); - - return CProcess::UpdateControlValue (controlIndex, value); - } - - //============================================================================== - bool getCurrentPosition (AudioPlayHead::CurrentPositionInfo& info) - { - // this method can only be called while the plugin is running - jassert (prepared); - - Cmn_Float64 bpm = 120.0; - Cmn_Int32 num = 4, denom = 4; - Cmn_Int64 ticks = 0; - Cmn_Bool isPlaying = false; - - if (midiTransport != 0) - { - midiTransport->GetCurrentTempo (&bpm); - midiTransport->IsTransportPlaying (&isPlaying); - midiTransport->GetCurrentMeter (&num, &denom); - midiTransport->GetCurrentTickPosition (&ticks); - } - - info.bpm = bpm; - info.timeSigNumerator = num; - info.timeSigDenominator = denom; - info.isPlaying = isPlaying; - info.isRecording = false; - info.ppqPosition = ticks / 960000.0; - info.ppqPositionOfLastBarStart = 0; //xxx no idea how to get this correctly.. - - // xxx incorrect if there are tempo changes, but there's no - // other way of getting this info.. - info.timeInSeconds = ticks * (60.0 / 960000.0) / bpm; - - double framesPerSec = 24.0; - - switch (fTimeCodeInfo.mFrameRate) - { - case ficFrameRate_24Frame: - info.frameRate = AudioPlayHead::fps24; - break; - - case ficFrameRate_25Frame: - info.frameRate = AudioPlayHead::fps25; - framesPerSec = 25.0; - break; - - case ficFrameRate_2997NonDrop: - info.frameRate = AudioPlayHead::fps2997; - framesPerSec = 29.97002997; - break; - - case ficFrameRate_2997DropFrame: - info.frameRate = AudioPlayHead::fps2997drop; - framesPerSec = 29.97002997; - break; - - case ficFrameRate_30NonDrop: - info.frameRate = AudioPlayHead::fps30; - framesPerSec = 30.0; - break; - - case ficFrameRate_30DropFrame: - info.frameRate = AudioPlayHead::fps30drop; - framesPerSec = 30.0; - break; - - case ficFrameRate_23976: - // xxx not strictly true.. - info.frameRate = AudioPlayHead::fps24; - framesPerSec = 23.976; - break; - - default: - info.frameRate = AudioPlayHead::fpsUnknown; - break; - } - - info.editOriginTime = fTimeCodeInfo.mFrameOffset / framesPerSec; - - return true; - } - - void audioProcessorParameterChanged (AudioProcessor*, int index, float newValue) - { - SetControlValue (index + 2, floatToLong (newValue)); - } - - void audioProcessorParameterChangeGestureBegin (AudioProcessor*, int index) - { - TouchControl (index + 2); - } - - void audioProcessorParameterChangeGestureEnd (AudioProcessor*, int index) - { - ReleaseControl (index + 2); - } - - void audioProcessorChanged (AudioProcessor*) - { - // xxx is there an RTAS equivalent? - } - - //============================================================================== -private: - AudioProcessor* juceFilter; - MidiBuffer midiEvents; - CEffectMIDIOtherBufferedNode* midiBufferNode; - CEffectMIDITransport* midiTransport; - DirectMidiPacket midiBuffer [midiBufferSize]; - - JUCE_NAMESPACE::MemoryBlock tempFilterData; - float** channels; - bool prepared; - double sampleRate; - - void bypassBuffers (float** const inputs, float** const outputs, const long numSamples) const - { - for (int i = fNumOutputs; --i >= 0;) - { - if (i < fNumInputs) - memcpy (outputs[i], inputs[i], numSamples * sizeof (float)); - else - zeromem (outputs[i], numSamples * sizeof (float)); - } - } - - //============================================================================== - class JucePluginControl : public CPluginControl - { - public: - //============================================================================== - JucePluginControl (AudioProcessor* const juceFilter_, const int index_) - : juceFilter (juceFilter_), - index (index_) - { - } - - ~JucePluginControl() - { - } - - //============================================================================== - OSType GetID() const { return index + 1; } - long GetDefaultValue() const { return floatToLong (0); } - void SetDefaultValue (long) {} - long GetNumSteps() const { return 0xffffffff; } - - long ConvertStringToValue (const char* valueString) const - { - return floatToLong (String (valueString).getFloatValue()); - } - - Cmn_Bool IsKeyValid (long key) const { return true; } - - void GetNameOfLength (char* name, int maxLength, OSType inControllerType) const - { - juceFilter->getParameterName (index).copyToBuffer (name, maxLength); - } - - long GetPriority() const { return kFicCooperativeTaskPriority; } - - long GetOrientation() const - { - return kDAE_LeftMinRightMax | kDAE_BottomMinTopMax - | kDAE_RotarySingleDotMode | kDAE_RotaryLeftMinRightMax; - } - - long GetControlType() const { return kDAE_ContinuousValues; } - - void GetValueString (char* valueString, int maxLength, long value) const - { - juceFilter->getParameterText (index).copyToBuffer (valueString, maxLength); - } - - Cmn_Bool IsAutomatable() const - { - return juceFilter->isParameterAutomatable (index); - } - - private: - //============================================================================== - AudioProcessor* const juceFilter; - const int index; - - JucePluginControl (const JucePluginControl&); - const JucePluginControl& operator= (const JucePluginControl&); - }; -}; - -//============================================================================== -class JucePlugInGroup : public CEffectGroupMIDI -{ -public: - //============================================================================== - JucePlugInGroup() - { - DefineManufacturerNamesAndID (JucePlugin_Manufacturer, JucePlugin_RTASManufacturerCode); - DefinePlugInNamesAndVersion (createRTASName(), JucePlugin_VersionCode); - -#ifndef JUCE_DEBUG - AddGestalt (pluginGestalt_IsCacheable); -#endif - } - - ~JucePlugInGroup() - { - shutdownJuce_GUI(); - shutdownJuce_NonGUI(); - } - - //============================================================================== - void CreateEffectTypes() - { - const short channelConfigs[][2] = { JucePlugin_PreferredChannelConfigurations }; - const int numConfigs = numElementsInArray (channelConfigs); - - // You need to actually add some configurations to the JucePlugin_PreferredChannelConfigurations - // value in your JucePluginCharacteristics.h file.. - jassert (numConfigs > 0); - - for (int i = 0; i < numConfigs; ++i) - { - CEffectType* const type - = new CEffectTypeRTAS ('jcaa' + i, - JucePlugin_RTASProductId, - JucePlugin_RTASCategory); - - type->DefineTypeNames (createRTASName()); - type->DefineSampleRateSupport (eSupports48kAnd96kAnd192k); - - type->DefineStemFormats (getFormatForChans (channelConfigs [i][0] != 0 ? channelConfigs [i][0] : channelConfigs [i][1]), - getFormatForChans (channelConfigs [i][1] != 0 ? channelConfigs [i][1] : channelConfigs [i][0])); - - type->AddGestalt (pluginGestalt_CanBypass); - type->AddGestalt (pluginGestalt_SupportsVariableQuanta); - type->AttachEffectProcessCreator (createNewProcess); - - AddEffectType (type); - } - } - - void Initialize() - { - CEffectGroupMIDI::Initialize(); - } - - //============================================================================== -private: - static CEffectProcess* createNewProcess() - { - // Juce setup -#if JUCE_WIN32 - PlatformUtilities::setCurrentModuleInstanceHandle (gThisModule); -#endif - initialiseJuce_GUI(); - - return new JucePlugInProcess(); - } - - static const String createRTASName() - { - return String (JucePlugin_Name) + T("\n") - + String (JucePlugin_Name).substring (0, 4); - } - - static EPlugIn_StemFormat getFormatForChans (const int numChans) throw() - { - switch (numChans) - { - case 0: - return ePlugIn_StemFormat_Generic; - - case 1: - return ePlugIn_StemFormat_Mono; - - case 2: - return ePlugIn_StemFormat_Stereo; - - case 3: - return ePlugIn_StemFormat_LCR; - - case 4: - return ePlugIn_StemFormat_Quad; - - case 5: - return ePlugIn_StemFormat_5dot0; - - case 6: - return ePlugIn_StemFormat_5dot1; - - case 7: - return ePlugIn_StemFormat_6dot1; - - case 8: - return ePlugIn_StemFormat_7dot1; - - default: - jassertfalse // hmm - not a valid number of chans for RTAS.. - break; - } - - return ePlugIn_StemFormat_Generic; - } -}; - -void initialiseMacRTAS(); - -CProcessGroupInterface* CProcessGroup::CreateProcessGroup() -{ -#if JUCE_MAC - initialiseMacRTAS(); -#endif - initialiseJuce_NonGUI(); - return new JucePlugInGroup(); -} - -#endif +/* + ============================================================================== + + This file is part of the JUCE library - "Jules' Utility Class Extensions" + Copyright 2004-9 by Raw Material Software Ltd. + + ------------------------------------------------------------------------------ + + JUCE can be redistributed and/or modified under the terms of the GNU General + Public License (Version 2), as published by the Free Software Foundation. + A copy of the license is included in the JUCE distribution, or can be found + online at www.gnu.org/licenses. + + JUCE is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + ------------------------------------------------------------------------------ + + To release a closed-source product which uses JUCE, commercial licenses are + available: visit www.rawmaterialsoftware.com/juce for more information. + + ============================================================================== +*/ + +#ifdef _MSC_VER + // (this is a workaround for a build problem in VC9) + #define _DO_NOT_DECLARE_INTERLOCKED_INTRINSICS_IN_MEMORY + #include +#endif + +#include "juce_RTAS_DigiCode_Header.h" + +#if JucePlugin_Build_RTAS + +#ifdef _MSC_VER + #include "Mac2Win.H" +#endif + +/* Note about include paths + ------------------------ + + To be able to include all the Digidesign headers correctly, you'll need to add this + lot to your include path: + + c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\EffectClasses + c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\ProcessClasses + c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\ProcessClasses\Interfaces + c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\Utilities + c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\RTASP_Adapt + c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\CoreClasses + c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\Controls + c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\Meters + c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\ViewClasses + c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\DSPClasses + c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\PluginLibrary\Interfaces + c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\common + c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\common\Platform + c:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugins\SignalProcessing\Public + C:\yourdirectory\PT_80_SDK\AlturaPorts\TDMPlugIns\DSPManager\Interfaces + c:\yourdirectory\PT_80_SDK\AlturaPorts\SADriver\Interfaces + c:\yourdirectory\PT_80_SDK\AlturaPorts\DigiPublic\Interfaces + c:\yourdirectory\PT_80_SDK\AlturaPorts\Fic\Interfaces\DAEClient + c:\yourdirectory\PT_80_SDK\AlturaPorts\NewFileLibs\Cmn + c:\yourdirectory\PT_80_SDK\AlturaPorts\NewFileLibs\DOA + c:\yourdirectory\PT_80_SDK\AlturaPorts\AlturaSource\PPC_H + c:\yourdirectory\PT_80_SDK\AlturaPorts\AlturaSource\AppSupport + c:\yourdirectory\PT_80_SDK\AvidCode\AVX2sdk\AVX\avx2\avx2sdk\inc + C:\yourdirectory\PT_80_SDK\xplat\AVX\avx2\avx2sdk\inc + + NB. If you hit a huge pile of bugs around here, make sure that you've not got the + Apple QuickTime headers before the PT headers in your path, because there are + some filename clashes between them. + +*/ +#include "CEffectGroupMIDI.h" +#include "CEffectProcessMIDI.h" +#include "CEffectProcessRTAS.h" +#include "CCustomView.h" +#include "CEffectTypeRTAS.h" +#include "CPluginControl.h" +#include "CPluginControl_OnOff.h" +#include "FicProcessTokens.h" + +//============================================================================== +#ifdef _MSC_VER + #pragma pack (push, 8) +#endif + +#include "../juce_PluginHeaders.h" + +#ifdef _MSC_VER + #pragma pack (pop) + + #if JUCE_DEBUG + #define PT_LIB_PATH JucePlugin_WinBag_path "\\Debug\\lib\\" + #else + #define PT_LIB_PATH JucePlugin_WinBag_path "\\Release\\lib\\" + #endif + + #pragma comment(lib, PT_LIB_PATH "DAE.lib") + #pragma comment(lib, PT_LIB_PATH "DigiExt.lib") + #pragma comment(lib, PT_LIB_PATH "DSI.lib") + #pragma comment(lib, PT_LIB_PATH "PluginLib.lib") + +#endif + +#undef Component +#undef MemoryBlock + +//============================================================================== +#if JUCE_WIN32 + extern void JUCE_CALLTYPE attachSubWindow (void* hostWindow, int& titleW, int& titleH, JUCE_NAMESPACE::Component* comp); + extern void JUCE_CALLTYPE resizeHostWindow (void* hostWindow, int& titleW, int& titleH, JUCE_NAMESPACE::Component* comp); + #if ! JucePlugin_EditorRequiresKeyboardFocus + extern void JUCE_CALLTYPE passFocusToHostWindow (void* hostWindow); + #endif +#else + extern void* attachSubWindow (void* hostWindowRef, JUCE_NAMESPACE::Component* comp); + extern void removeSubWindow (void* nsWindow, JUCE_NAMESPACE::Component* comp); + extern void forwardCurrentKeyEventToHostWindow(); +#endif + +const int midiBufferSize = 1024; +const OSType juceChunkType = 'juce'; +static const int bypassControlIndex = 1; + +//============================================================================== +/** Somewhere in the codebase of your plugin, you need to implement this function + and make it return a new instance of the filter subclass that you're building. +*/ +extern AudioProcessor* JUCE_CALLTYPE createPluginFilter(); + + +//============================================================================== +static float longToFloat (const long n) throw() +{ + return (float) ((((double) n) + (double) 0x80000000) / (double) 0xffffffff); +} + +static long floatToLong (const float n) throw() +{ + return roundDoubleToInt (jlimit (-(double) 0x80000000, + (double) 0x7fffffff, + n * (double) 0xffffffff - (double) 0x80000000)); +} + +static int numInstances = 0; + +//============================================================================== +class JucePlugInProcess : public CEffectProcessMIDI, + public CEffectProcessRTAS, + public AudioProcessorListener, + public AudioPlayHead, + public AsyncUpdater +{ +public: + //============================================================================== + JucePlugInProcess() + : midiBufferNode (0), + midiTransport (0), + channels (0), + prepared (false), + sampleRate (44100.0) + { + juceFilter = createPluginFilter(); + jassert (juceFilter != 0); + + AddChunk (juceChunkType, "Juce Audio Plugin Data"); + + ++numInstances; + } + + ~JucePlugInProcess() + { + if (mLoggedIn) + MIDILogOut(); + + deleteAndZero (midiBufferNode); + deleteAndZero (midiTransport); + + if (prepared) + juceFilter->releaseResources(); + + delete juceFilter; + juce_free (channels); + + if (--numInstances == 0) + shutdownJuce_GUI(); + } + + //============================================================================== + class JuceCustomUIView : public CCustomView, + public Timer + { + public: + //============================================================================== + JuceCustomUIView (AudioProcessor* const filter_, + JucePlugInProcess* const process_) + : filter (filter_), + process (process_), + wrapper (0), + editorComp (0) + { + // setting the size in here crashes PT for some reason, so keep it simple.. + } + + ~JuceCustomUIView() + { + deleteEditorComp(); + } + + //============================================================================== + void updateSize() + { + if (editorComp == 0) + { + editorComp = filter->createEditorIfNeeded(); + jassert (editorComp != 0); + } + + Rect oldRect; + GetRect (&oldRect); + + Rect r; + r.left = 0; + r.top = 0; + r.right = editorComp->getWidth(); + r.bottom = editorComp->getHeight(); + SetRect (&r); + + if ((oldRect.right != r.right) || (oldRect.bottom != r.bottom)) + startTimer (50); + } + + void timerCallback() + { + if (! JUCE_NAMESPACE::Component::isMouseButtonDownAnywhere()) + { + stopTimer(); + + // Send a token to the host to tell it about the resize + SSetProcessWindowResizeToken token (process->fRootNameId, process->fRootNameId); + FicSDSDispatchToken (&token); + } + } + + void attachToWindow (GrafPtr port) + { + if (port != 0) + { + updateSize(); + +#if JUCE_WIN32 + void* const hostWindow = (void*) ASI_GethWnd ((WindowPtr) port); +#else + void* const hostWindow = (void*) GetWindowFromPort (port); +#endif + deleteAndZero (wrapper); + + wrapper = new EditorCompWrapper (hostWindow, editorComp, this); + } + else + { + deleteEditorComp(); + } + } + + void DrawContents (Rect*) + { +#if JUCE_WIN32 + if (wrapper != 0) + { + ComponentPeer* const peer = wrapper->getPeer(); + + if (peer != 0) + { + // (seems to be required in PT6.4, but not in 7.x) + peer->repaint (0, 0, wrapper->getWidth(), wrapper->getHeight()); + } + } +#endif + } + + void DrawBackground (Rect*) {} + + //============================================================================== + private: + AudioProcessor* const filter; + JucePlugInProcess* const process; + JUCE_NAMESPACE::Component* wrapper; + AudioProcessorEditor* editorComp; + + void deleteEditorComp() + { + if (editorComp != 0 || wrapper != 0) + { +#if JUCE_MAC + const ScopedAutoReleasePool pool; +#endif + PopupMenu::dismissAllActiveMenus(); + + JUCE_NAMESPACE::Component* const modalComponent = JUCE_NAMESPACE::Component::getCurrentlyModalComponent(); + if (modalComponent != 0) + modalComponent->exitModalState (0); + + filter->editorBeingDeleted (editorComp); + + deleteAndZero (editorComp); + deleteAndZero (wrapper); + } + } + + //============================================================================== + // A component to hold the AudioProcessorEditor, and cope with some housekeeping + // chores when it changes or repaints. + class EditorCompWrapper : public JUCE_NAMESPACE::Component +#if ! JUCE_MAC + , public FocusChangeListener +#endif + { + public: + EditorCompWrapper (void* const hostWindow_, + Component* const editorComp, + JuceCustomUIView* const owner_) + : hostWindow (hostWindow_), + owner (owner_), + titleW (0), + titleH (0) + { +#if ! JucePlugin_EditorRequiresKeyboardFocus + setMouseClickGrabsKeyboardFocus (false); + setWantsKeyboardFocus (false); +#endif + setOpaque (true); + setBroughtToFrontOnMouseClick (true); + setBounds (editorComp->getBounds()); + editorComp->setTopLeftPosition (0, 0); + addAndMakeVisible (editorComp); + +#if JUCE_WIN32 + attachSubWindow (hostWindow, titleW, titleH, this); +#else + nsWindow = attachSubWindow (hostWindow, this); +#endif + setVisible (true); + +#if JUCE_WIN32 && ! JucePlugin_EditorRequiresKeyboardFocus + Desktop::getInstance().addFocusChangeListener (this); +#endif + } + + ~EditorCompWrapper() + { +#if JUCE_WIN32 && ! JucePlugin_EditorRequiresKeyboardFocus + Desktop::getInstance().removeFocusChangeListener (this); +#endif + +#if JUCE_MAC + removeSubWindow (nsWindow, this); +#endif + } + + void paint (Graphics&) + { + } + + void resized() + { + JUCE_NAMESPACE::Component* const c = getChildComponent (0); + + if (c != 0) + c->setBounds (0, 0, getWidth(), getHeight()); + + repaint(); + } + +#if JUCE_WIN32 + void globalFocusChanged (JUCE_NAMESPACE::Component*) + { + #if ! JucePlugin_EditorRequiresKeyboardFocus + if (hasKeyboardFocus (true)) + passFocusToHostWindow (hostWindow); + #endif + } +#endif + + void childBoundsChanged (JUCE_NAMESPACE::Component* child) + { + setSize (child->getWidth(), child->getHeight()); + child->setTopLeftPosition (0, 0); + +#if JUCE_WIN32 + resizeHostWindow (hostWindow, titleW, titleH, this); +#endif + owner->updateSize(); + } + + void userTriedToCloseWindow() + { + } + +#if JUCE_MAC && JucePlugin_EditorRequiresKeyboardFocus + bool keyPressed (const KeyPress& kp) + { + owner->updateSize(); + forwardCurrentKeyEventToHostWindow(); + return true; + } +#endif + //============================================================================== + juce_UseDebuggingNewOperator + + private: + void* const hostWindow; + void* nsWindow; + JuceCustomUIView* const owner; + int titleW, titleH; + }; + }; + + JuceCustomUIView* getView() const + { + return dynamic_cast (fOurPlugInView); + } + + void GetViewRect (Rect* size) + { + if (getView() != 0) + getView()->updateSize(); + + CEffectProcessRTAS::GetViewRect (size); + } + + CPlugInView* CreateCPlugInView() + { + return new JuceCustomUIView (juceFilter, this); + } + + void SetViewPort (GrafPtr port) + { + CEffectProcessRTAS::SetViewPort (port); + + if (getView() != 0) + getView()->attachToWindow (port); + } + + //============================================================================== +protected: + ComponentResult GetDelaySamplesLong (long* aNumSamples) + { + if (aNumSamples != 0) + *aNumSamples = juceFilter != 0 ? juceFilter->getLatencySamples() : 0; + + return noErr; + } + + //============================================================================== + void EffectInit() + { + SFicPlugInStemFormats stems; + GetProcessType()->GetStemFormats (&stems); + + juceFilter->setPlayConfigDetails (fNumInputs, fNumOutputs, + juceFilter->getSampleRate(), juceFilter->getBlockSize()); + + AddControl (new CPluginControl_OnOff ('bypa', "Master Bypass\nMastrByp\nMByp\nByp", false, true)); + DefineMasterBypassControlIndex (bypassControlIndex); + + for (int i = 0; i < juceFilter->getNumParameters(); ++i) + AddControl (new JucePluginControl (juceFilter, i)); + + // we need to do this midi log-in to get timecode, regardless of whether + // the plugin actually uses midi... + if (MIDILogIn() == noErr) + { +#if JucePlugin_WantsMidiInput + CEffectType* const type = dynamic_cast (this->GetProcessType()); + + if (type != 0) + { + char nodeName [64]; + type->GetProcessTypeName (63, nodeName); + p2cstrcpy (nodeName, reinterpret_cast (nodeName)); + + midiBufferNode = new CEffectMIDIOtherBufferedNode (&mMIDIWorld, + 8192, + eLocalNode, + nodeName, + midiBuffer); + + midiBufferNode->Initialize (1, true); + } +#endif + } + + midiTransport = new CEffectMIDITransport (&mMIDIWorld); + + juceFilter->setPlayHead (this); + juceFilter->addListener (this); + } + + void handleAsyncUpdate() + { + if (! prepared) + { + sampleRate = gProcessGroup->GetSampleRate(); + jassert (sampleRate > 0); + + juce_free (channels); + channels = (float**) juce_calloc (sizeof (float*) * jmax (juceFilter->getNumInputChannels(), + juceFilter->getNumOutputChannels())); + + juceFilter->setPlayConfigDetails (fNumInputs, fNumOutputs, + sampleRate, mRTGlobals->mHWBufferSizeInSamples); + + juceFilter->prepareToPlay (sampleRate, + mRTGlobals->mHWBufferSizeInSamples); + + prepared = true; + } + } + + void RenderAudio (float** inputs, float** outputs, long numSamples) + { + if (! prepared) + { + triggerAsyncUpdate(); + bypassBuffers (inputs, outputs, numSamples); + return; + } + + if (mBypassed) + { + bypassBuffers (inputs, outputs, numSamples); + return; + } + +#if JucePlugin_WantsMidiInput + midiEvents.clear(); + + const Cmn_UInt32 bufferSize = mRTGlobals->mHWBufferSizeInSamples; + + if (midiBufferNode->GetAdvanceScheduleTime() != bufferSize) + midiBufferNode->SetAdvanceScheduleTime (bufferSize); + + if (midiBufferNode->FillMIDIBuffer (mRTGlobals->mRunningTime, numSamples) == noErr) + { + jassert (midiBufferNode->GetBufferPtr() != 0); + const int numMidiEvents = midiBufferNode->GetBufferSize(); + + for (int i = 0; i < numMidiEvents; ++i) + { + const DirectMidiPacket& m = midiBuffer[i]; + + jassert ((int) m.mTimestamp < numSamples); + + midiEvents.addEvent (m.mData, m.mLength, + jlimit (0, (int) numSamples - 1, (int) m.mTimestamp)); + } + } +#endif + +#if defined (JUCE_DEBUG) || JUCE_LOG_ASSERTIONS + const int numMidiEventsComingIn = midiEvents.getNumEvents(); + (void) numMidiEventsComingIn; +#endif + + { + const ScopedLock sl (juceFilter->getCallbackLock()); + + const int numIn = juceFilter->getNumInputChannels(); + const int numOut = juceFilter->getNumOutputChannels(); + const int totalChans = jmax (numIn, numOut); + + if (juceFilter->isSuspended()) + { + for (int i = 0; i < numOut; ++i) + zeromem (outputs [i], sizeof (float) * numSamples); + } + else + { + { + int i; + for (i = 0; i < numOut; ++i) + { + channels[i] = outputs [i]; + + if (i < numIn && inputs != outputs) + memcpy (outputs [i], inputs[i], sizeof (float) * numSamples); + } + + for (; i < numIn; ++i) + channels [i] = inputs [i]; + } + + AudioSampleBuffer chans (channels, totalChans, numSamples); + + juceFilter->processBlock (chans, midiEvents); + } + } + + if (! midiEvents.isEmpty()) + { +#if JucePlugin_ProducesMidiOutput + const juce::uint8* midiEventData; + int midiEventSize, midiEventPosition; + MidiBuffer::Iterator i (midiEvents); + + while (i.getNextEvent (midiEventData, midiEventSize, midiEventPosition)) + { +// jassert (midiEventPosition >= 0 && midiEventPosition < (int) numSamples); + + //xxx + } +#else + // if your plugin creates midi messages, you'll need to set + // the JucePlugin_ProducesMidiOutput macro to 1 in your + // JucePluginCharacteristics.h file + jassert (midiEvents.getNumEvents() <= numMidiEventsComingIn); +#endif + + midiEvents.clear(); + } + } + + //============================================================================== + ComponentResult GetChunkSize (OSType chunkID, long* size) + { + if (chunkID == juceChunkType) + { + tempFilterData.setSize (0); + juceFilter->getStateInformation (tempFilterData); + + *size = sizeof (SFicPlugInChunkHeader) + tempFilterData.getSize(); + return noErr; + } + + return CEffectProcessMIDI::GetChunkSize (chunkID, size); + } + + ComponentResult GetChunk (OSType chunkID, SFicPlugInChunk* chunk) + { + if (chunkID == juceChunkType) + { + if (tempFilterData.getSize() == 0) + juceFilter->getStateInformation (tempFilterData); + + chunk->fSize = sizeof (SFicPlugInChunkHeader) + tempFilterData.getSize(); + tempFilterData.copyTo ((void*) chunk->fData, 0, tempFilterData.getSize()); + + tempFilterData.setSize (0); + + return noErr; + } + + return CEffectProcessMIDI::GetChunk (chunkID, chunk); + } + + ComponentResult SetChunk (OSType chunkID, SFicPlugInChunk* chunk) + { + if (chunkID == juceChunkType) + { + tempFilterData.setSize (0); + + if (chunk->fSize - sizeof (SFicPlugInChunkHeader) > 0) + { + juceFilter->setStateInformation ((void*) chunk->fData, + chunk->fSize - sizeof (SFicPlugInChunkHeader)); + } + + return noErr; + } + + return CEffectProcessMIDI::SetChunk (chunkID, chunk); + } + + //============================================================================== + ComponentResult UpdateControlValue (long controlIndex, long value) + { + if (controlIndex != bypassControlIndex) + juceFilter->setParameter (controlIndex - 2, longToFloat (value)); + else + mBypassed = (value > 0); + + return CProcess::UpdateControlValue (controlIndex, value); + } + + //============================================================================== + bool getCurrentPosition (AudioPlayHead::CurrentPositionInfo& info) + { + // this method can only be called while the plugin is running + jassert (prepared); + + Cmn_Float64 bpm = 120.0; + Cmn_Int32 num = 4, denom = 4; + Cmn_Int64 ticks = 0; + Cmn_Bool isPlaying = false; + + if (midiTransport != 0) + { + midiTransport->GetCurrentTempo (&bpm); + midiTransport->IsTransportPlaying (&isPlaying); + midiTransport->GetCurrentMeter (&num, &denom); + midiTransport->GetCurrentTickPosition (&ticks); + } + + info.bpm = bpm; + info.timeSigNumerator = num; + info.timeSigDenominator = denom; + info.isPlaying = isPlaying; + info.isRecording = false; + info.ppqPosition = ticks / 960000.0; + info.ppqPositionOfLastBarStart = 0; //xxx no idea how to get this correctly.. + + // xxx incorrect if there are tempo changes, but there's no + // other way of getting this info.. + info.timeInSeconds = ticks * (60.0 / 960000.0) / bpm; + + double framesPerSec = 24.0; + + switch (fTimeCodeInfo.mFrameRate) + { + case ficFrameRate_24Frame: + info.frameRate = AudioPlayHead::fps24; + break; + + case ficFrameRate_25Frame: + info.frameRate = AudioPlayHead::fps25; + framesPerSec = 25.0; + break; + + case ficFrameRate_2997NonDrop: + info.frameRate = AudioPlayHead::fps2997; + framesPerSec = 29.97002997; + break; + + case ficFrameRate_2997DropFrame: + info.frameRate = AudioPlayHead::fps2997drop; + framesPerSec = 29.97002997; + break; + + case ficFrameRate_30NonDrop: + info.frameRate = AudioPlayHead::fps30; + framesPerSec = 30.0; + break; + + case ficFrameRate_30DropFrame: + info.frameRate = AudioPlayHead::fps30drop; + framesPerSec = 30.0; + break; + + case ficFrameRate_23976: + // xxx not strictly true.. + info.frameRate = AudioPlayHead::fps24; + framesPerSec = 23.976; + break; + + default: + info.frameRate = AudioPlayHead::fpsUnknown; + break; + } + + info.editOriginTime = fTimeCodeInfo.mFrameOffset / framesPerSec; + + return true; + } + + void audioProcessorParameterChanged (AudioProcessor*, int index, float newValue) + { + SetControlValue (index + 2, floatToLong (newValue)); + } + + void audioProcessorParameterChangeGestureBegin (AudioProcessor*, int index) + { + TouchControl (index + 2); + } + + void audioProcessorParameterChangeGestureEnd (AudioProcessor*, int index) + { + ReleaseControl (index + 2); + } + + void audioProcessorChanged (AudioProcessor*) + { + // xxx is there an RTAS equivalent? + } + + //============================================================================== +private: + AudioProcessor* juceFilter; + MidiBuffer midiEvents; + CEffectMIDIOtherBufferedNode* midiBufferNode; + CEffectMIDITransport* midiTransport; + DirectMidiPacket midiBuffer [midiBufferSize]; + + JUCE_NAMESPACE::MemoryBlock tempFilterData; + float** channels; + bool prepared; + double sampleRate; + + void bypassBuffers (float** const inputs, float** const outputs, const long numSamples) const + { + for (int i = fNumOutputs; --i >= 0;) + { + if (i < fNumInputs) + memcpy (outputs[i], inputs[i], numSamples * sizeof (float)); + else + zeromem (outputs[i], numSamples * sizeof (float)); + } + } + + //============================================================================== + class JucePluginControl : public CPluginControl + { + public: + //============================================================================== + JucePluginControl (AudioProcessor* const juceFilter_, const int index_) + : juceFilter (juceFilter_), + index (index_) + { + } + + ~JucePluginControl() + { + } + + //============================================================================== + OSType GetID() const { return index + 1; } + long GetDefaultValue() const { return floatToLong (0); } + void SetDefaultValue (long) {} + long GetNumSteps() const { return 0xffffffff; } + + long ConvertStringToValue (const char* valueString) const + { + return floatToLong (String (valueString).getFloatValue()); + } + + Cmn_Bool IsKeyValid (long key) const { return true; } + + void GetNameOfLength (char* name, int maxLength, OSType inControllerType) const + { + juceFilter->getParameterName (index).copyToBuffer (name, maxLength); + } + + long GetPriority() const { return kFicCooperativeTaskPriority; } + + long GetOrientation() const + { + return kDAE_LeftMinRightMax | kDAE_BottomMinTopMax + | kDAE_RotarySingleDotMode | kDAE_RotaryLeftMinRightMax; + } + + long GetControlType() const { return kDAE_ContinuousValues; } + + void GetValueString (char* valueString, int maxLength, long value) const + { + juceFilter->getParameterText (index).copyToBuffer (valueString, maxLength); + } + + Cmn_Bool IsAutomatable() const + { + return juceFilter->isParameterAutomatable (index); + } + + private: + //============================================================================== + AudioProcessor* const juceFilter; + const int index; + + JucePluginControl (const JucePluginControl&); + const JucePluginControl& operator= (const JucePluginControl&); + }; +}; + +//============================================================================== +class JucePlugInGroup : public CEffectGroupMIDI +{ +public: + //============================================================================== + JucePlugInGroup() + { + DefineManufacturerNamesAndID (JucePlugin_Manufacturer, JucePlugin_RTASManufacturerCode); + DefinePlugInNamesAndVersion (createRTASName(), JucePlugin_VersionCode); + +#ifndef JUCE_DEBUG + AddGestalt (pluginGestalt_IsCacheable); +#endif + } + + ~JucePlugInGroup() + { + shutdownJuce_GUI(); + shutdownJuce_NonGUI(); + } + + //============================================================================== + void CreateEffectTypes() + { + const short channelConfigs[][2] = { JucePlugin_PreferredChannelConfigurations }; + const int numConfigs = numElementsInArray (channelConfigs); + + // You need to actually add some configurations to the JucePlugin_PreferredChannelConfigurations + // value in your JucePluginCharacteristics.h file.. + jassert (numConfigs > 0); + + for (int i = 0; i < numConfigs; ++i) + { + CEffectType* const type + = new CEffectTypeRTAS ('jcaa' + i, + JucePlugin_RTASProductId, + JucePlugin_RTASCategory); + + type->DefineTypeNames (createRTASName()); + type->DefineSampleRateSupport (eSupports48kAnd96kAnd192k); + + type->DefineStemFormats (getFormatForChans (channelConfigs [i][0] != 0 ? channelConfigs [i][0] : channelConfigs [i][1]), + getFormatForChans (channelConfigs [i][1] != 0 ? channelConfigs [i][1] : channelConfigs [i][0])); + + type->AddGestalt (pluginGestalt_CanBypass); + type->AddGestalt (pluginGestalt_SupportsVariableQuanta); + type->AttachEffectProcessCreator (createNewProcess); + + AddEffectType (type); + } + } + + void Initialize() + { + CEffectGroupMIDI::Initialize(); + } + + //============================================================================== +private: + static CEffectProcess* createNewProcess() + { + // Juce setup +#if JUCE_WIN32 + PlatformUtilities::setCurrentModuleInstanceHandle (gThisModule); +#endif + initialiseJuce_GUI(); + + return new JucePlugInProcess(); + } + + static const String createRTASName() + { + return String (JucePlugin_Name) + T("\n") + + String (JucePlugin_Name).substring (0, 4); + } + + static EPlugIn_StemFormat getFormatForChans (const int numChans) throw() + { + switch (numChans) + { + case 0: + return ePlugIn_StemFormat_Generic; + + case 1: + return ePlugIn_StemFormat_Mono; + + case 2: + return ePlugIn_StemFormat_Stereo; + + case 3: + return ePlugIn_StemFormat_LCR; + + case 4: + return ePlugIn_StemFormat_Quad; + + case 5: + return ePlugIn_StemFormat_5dot0; + + case 6: + return ePlugIn_StemFormat_5dot1; + + case 7: + return ePlugIn_StemFormat_6dot1; + + case 8: + return ePlugIn_StemFormat_7dot1; + + default: + jassertfalse // hmm - not a valid number of chans for RTAS.. + break; + } + + return ePlugIn_StemFormat_Generic; + } +}; + +void initialiseMacRTAS(); + +CProcessGroupInterface* CProcessGroup::CreateProcessGroup() +{ +#if JUCE_MAC + initialiseMacRTAS(); +#endif + initialiseJuce_NonGUI(); + return new JucePlugInGroup(); +} + +#endif diff --git a/extras/juce demo/src/demos/AudioDemoLatencyPage.cpp b/extras/juce demo/src/demos/AudioDemoLatencyPage.cpp index c69b77bebf..3bd0dd4374 100644 --- a/extras/juce demo/src/demos/AudioDemoLatencyPage.cpp +++ b/extras/juce demo/src/demos/AudioDemoLatencyPage.cpp @@ -165,7 +165,7 @@ private: int deviceInputLatency, deviceOutputLatency; Array spikes; - + void createTestSound() { const int length = ((int) sampleRate) / 4; @@ -186,7 +186,7 @@ private: } } - // Searches a buffer for a set of spikes that matches those in the test sound + // Searches a buffer for a set of spikes that matches those in the test sound int findOffsetOfSpikes (const AudioSampleBuffer& buffer) const { const float minSpikeLevel = 5.0f; diff --git a/src/audio/devices/juce_AudioDeviceManager.h b/src/audio/devices/juce_AudioDeviceManager.h index 062aaa0960..30dc79c57f 100644 --- a/src/audio/devices/juce_AudioDeviceManager.h +++ b/src/audio/devices/juce_AudioDeviceManager.h @@ -242,7 +242,7 @@ public: void setCurrentAudioDeviceType (const String& type, const bool treatAsChosenDevice); - + /** Closes the currently-open device. You can call restartLastAudioDevice() later to reopen it in the same state diff --git a/src/audio/midi/juce_MidiMessage.h b/src/audio/midi/juce_MidiMessage.h index 8053834a90..ac484e184c 100644 --- a/src/audio/midi/juce_MidiMessage.h +++ b/src/audio/midi/juce_MidiMessage.h @@ -131,10 +131,10 @@ public: The exact meaning of this time and its units will vary, as messages are used in a variety of different contexts. - + If you're getting the message from a midi file, this could be a time in seconds, or a number of ticks - see MidiFile::convertTimestampTicksToSeconds(). - + If the message is being used in a MidiBuffer, it might indicate the number of audio samples from the start of the buffer. diff --git a/src/gui/components/special/juce_WebBrowserComponent.h b/src/gui/components/special/juce_WebBrowserComponent.h index 0595817db1..7f1bf06b3a 100644 --- a/src/gui/components/special/juce_WebBrowserComponent.h +++ b/src/gui/components/special/juce_WebBrowserComponent.h @@ -49,8 +49,8 @@ public: /** Creates a WebBrowserComponent. Once it's created and visible, send the browser to a URL using goToURL(). - - @param unloadPageWhenBrowserIsHidden if this is true, then when the browser + + @param unloadPageWhenBrowserIsHidden if this is true, then when the browser component is taken offscreen, it'll clear the current page and replace it with a blank page - this can be handy to stop the browser using resources in the background when it's not diff --git a/src/juce_app_includes.h b/src/juce_app_includes.h index d444811e0f..c342c6027d 100644 --- a/src/juce_app_includes.h +++ b/src/juce_app_includes.h @@ -53,14 +53,14 @@ #ifndef __JUCE_MIDIKEYBOARDSTATE_JUCEHEADER__ #include "audio/midi/juce_MidiKeyboardState.h" #endif -#ifndef __JUCE_MIDIMESSAGE_JUCEHEADER__ - #include "audio/midi/juce_MidiMessage.h" +#ifndef __JUCE_MIDIMESSAGESEQUENCE_JUCEHEADER__ + #include "audio/midi/juce_MidiMessageSequence.h" #endif #ifndef __JUCE_MIDIMESSAGECOLLECTOR_JUCEHEADER__ #include "audio/midi/juce_MidiMessageCollector.h" #endif -#ifndef __JUCE_MIDIMESSAGESEQUENCE_JUCEHEADER__ - #include "audio/midi/juce_MidiMessageSequence.h" +#ifndef __JUCE_MIDIMESSAGE_JUCEHEADER__ + #include "audio/midi/juce_MidiMessage.h" #endif #ifndef __JUCE_AUDIODATACONVERTERS_JUCEHEADER__ #include "audio/dsp/juce_AudioDataConverters.h" @@ -125,9 +125,6 @@ #ifndef __JUCE_RESAMPLINGAUDIOSOURCE_JUCEHEADER__ #include "audio/audio_sources/juce_ResamplingAudioSource.h" #endif -#ifndef __JUCE_AUDIODEVICEMANAGER_JUCEHEADER__ - #include "audio/devices/juce_AudioDeviceManager.h" -#endif #ifndef __JUCE_AUDIOIODEVICE_JUCEHEADER__ #include "audio/devices/juce_AudioIODevice.h" #endif @@ -140,6 +137,9 @@ #ifndef __JUCE_MIDIOUTPUT_JUCEHEADER__ #include "audio/devices/juce_MidiOutput.h" #endif +#ifndef __JUCE_AUDIODEVICEMANAGER_JUCEHEADER__ + #include "audio/devices/juce_AudioDeviceManager.h" +#endif #ifndef __JUCE_SAMPLER_JUCEHEADER__ #include "audio/synthesisers/juce_Sampler.h" #endif @@ -527,12 +527,12 @@ #ifndef __JUCE_TOOLBAR_JUCEHEADER__ #include "gui/components/controls/juce_Toolbar.h" #endif -#ifndef __JUCE_TOOLBARITEMCOMPONENT_JUCEHEADER__ - #include "gui/components/controls/juce_ToolbarItemComponent.h" -#endif #ifndef __JUCE_TOOLBARITEMFACTORY_JUCEHEADER__ #include "gui/components/controls/juce_ToolbarItemFactory.h" #endif +#ifndef __JUCE_TOOLBARITEMCOMPONENT_JUCEHEADER__ + #include "gui/components/controls/juce_ToolbarItemComponent.h" +#endif #ifndef __JUCE_TOOLBARITEMPALETTE_JUCEHEADER__ #include "gui/components/controls/juce_ToolbarItemPalette.h" #endif @@ -689,6 +689,9 @@ #ifndef __JUCE_DROPSHADOWER_JUCEHEADER__ #include "gui/components/special/juce_DropShadower.h" #endif +#ifndef __JUCE_WEBBROWSERCOMPONENT_JUCEHEADER__ + #include "gui/components/special/juce_WebBrowserComponent.h" +#endif #ifndef __JUCE_MAGNIFIERCOMPONENT_JUCEHEADER__ #include "gui/components/special/juce_MagnifierComponent.h" #endif @@ -707,9 +710,6 @@ #ifndef __JUCE_QUICKTIMEMOVIECOMPONENT_JUCEHEADER__ #include "gui/components/special/juce_QuickTimeMovieComponent.h" #endif -#ifndef __JUCE_WEBBROWSERCOMPONENT_JUCEHEADER__ - #include "gui/components/special/juce_WebBrowserComponent.h" -#endif #ifndef __JUCE_SYSTEMTRAYICONCOMPONENT_JUCEHEADER__ #include "gui/components/special/juce_SystemTrayIconComponent.h" #endif diff --git a/src/native/mac/juce_mac_CarbonViewWrapperComponent.h b/src/native/mac/juce_mac_CarbonViewWrapperComponent.h index 8e863035e7..dcabbc24c2 100644 --- a/src/native/mac/juce_mac_CarbonViewWrapperComponent.h +++ b/src/native/mac/juce_mac_CarbonViewWrapperComponent.h @@ -213,7 +213,7 @@ public: child = HIViewGetNextView (child); } } - + void timerCallback() { setOurSizeToEmbeddedViewSize(); diff --git a/src/native/mac/juce_mac_CoreAudio.cpp b/src/native/mac/juce_mac_CoreAudio.cpp index 8de877f0e3..7dc0b9b04e 100644 --- a/src/native/mac/juce_mac_CoreAudio.cpp +++ b/src/native/mac/juce_mac_CoreAudio.cpp @@ -1,1374 +1,1374 @@ -/* - ============================================================================== - - This file is part of the JUCE library - "Jules' Utility Class Extensions" - Copyright 2004-9 by Raw Material Software Ltd. - - ------------------------------------------------------------------------------ - - JUCE can be redistributed and/or modified under the terms of the GNU General - Public License (Version 2), as published by the Free Software Foundation. - A copy of the license is included in the JUCE distribution, or can be found - online at www.gnu.org/licenses. - - JUCE is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU General Public License for more details. - - ------------------------------------------------------------------------------ - - To release a closed-source product which uses JUCE, commercial licenses are - available: visit www.rawmaterialsoftware.com/juce for more information. - - ============================================================================== -*/ - -// (This file gets included by juce_mac_NativeCode.mm, rather than being -// compiled on its own). -#ifdef JUCE_INCLUDED_FILE - - -//============================================================================== -#ifndef JUCE_COREAUDIO_ERROR_LOGGING_ENABLED - #define JUCE_COREAUDIO_ERROR_LOGGING_ENABLED 1 -#endif - -//============================================================================== -#undef log -#if JUCE_COREAUDIO_LOGGING_ENABLED - #define log(a) Logger::writeToLog (a) -#else - #define log(a) -#endif - -#undef OK -#if JUCE_COREAUDIO_ERROR_LOGGING_ENABLED - static bool logAnyErrors_CoreAudio (const OSStatus err, const int lineNum) - { - if (err == noErr) - return true; - - Logger::writeToLog (T("CoreAudio error: ") + String (lineNum) + T(" - ") + String::toHexString ((int)err)); - jassertfalse - return false; - } - - #define OK(a) logAnyErrors_CoreAudio (a, __LINE__) -#else - #define OK(a) (a == noErr) -#endif - - -//============================================================================== -class CoreAudioInternal : public Timer -{ -public: - //============================================================================== - CoreAudioInternal (AudioDeviceID id) - : inputLatency (0), - outputLatency (0), - callback (0), -#if ! MACOS_10_4_OR_EARLIER - audioProcID (0), -#endif - inputDevice (0), - isSlaveDevice (false), - deviceID (id), - started (false), - sampleRate (0), - bufferSize (512), - audioBuffer (0), - numInputChans (0), - numOutputChans (0), - callbacksAllowed (true), - numInputChannelInfos (0), - numOutputChannelInfos (0), - tempInputBuffers (0), - tempOutputBuffers (0), - inputChannelInfo (0), - outputChannelInfo (0) - { - jassert (deviceID != 0); - - updateDetailsFromDevice(); - - AudioObjectPropertyAddress pa; - pa.mSelector = kAudioObjectPropertySelectorWildcard; - pa.mScope = kAudioObjectPropertyScopeWildcard; - pa.mElement = kAudioObjectPropertyElementWildcard; - - AudioObjectAddPropertyListener (deviceID, &pa, deviceListenerProc, this); - } - - ~CoreAudioInternal() - { - AudioObjectPropertyAddress pa; - pa.mSelector = kAudioObjectPropertySelectorWildcard; - pa.mScope = kAudioObjectPropertyScopeWildcard; - pa.mElement = kAudioObjectPropertyElementWildcard; - - AudioObjectRemovePropertyListener (deviceID, &pa, deviceListenerProc, this); - - stop (false); - delete inputDevice; - - juce_free (audioBuffer); - juce_free (tempInputBuffers); - juce_free (tempOutputBuffers); - juce_free (inputChannelInfo); - juce_free (outputChannelInfo); - } - - void allocateTempBuffers() - { - const int tempBufSize = bufferSize + 4; - juce_free (audioBuffer); - audioBuffer = (float*) juce_calloc ((numInputChans + numOutputChans) * tempBufSize * sizeof (float)); - - juce_free (tempInputBuffers); - tempInputBuffers = (float**) juce_calloc (sizeof (float*) * (numInputChans + 2)); - juce_free (tempOutputBuffers); - tempOutputBuffers = (float**) juce_calloc (sizeof (float*) * (numOutputChans + 2)); - - int i, count = 0; - for (i = 0; i < numInputChans; ++i) - tempInputBuffers[i] = audioBuffer + count++ * tempBufSize; - - for (i = 0; i < numOutputChans; ++i) - tempOutputBuffers[i] = audioBuffer + count++ * tempBufSize; - } - - // returns the number of actual available channels - void fillInChannelInfo (const bool input) - { - int chanNum = 0; - UInt32 size; - - AudioObjectPropertyAddress pa; - pa.mSelector = kAudioDevicePropertyStreamConfiguration; - pa.mScope = input ? kAudioDevicePropertyScopeInput : kAudioDevicePropertyScopeOutput; - pa.mElement = kAudioObjectPropertyElementMaster; - - if (OK (AudioObjectGetPropertyDataSize (deviceID, &pa, 0, 0, &size))) - { - AudioBufferList* const bufList = (AudioBufferList*) juce_calloc (size); - - if (OK (AudioObjectGetPropertyData (deviceID, &pa, 0, 0, &size, bufList))) - { - const int numStreams = bufList->mNumberBuffers; - - for (int i = 0; i < numStreams; ++i) - { - const AudioBuffer& b = bufList->mBuffers[i]; - - for (unsigned int j = 0; j < b.mNumberChannels; ++j) - { - String name; - - { - uint8 channelName [256]; - zerostruct (channelName); - UInt32 nameSize = sizeof (channelName); - UInt32 channelNum = chanNum + 1; - pa.mSelector = kAudioDevicePropertyChannelName; - - if (AudioObjectGetPropertyData (deviceID, &pa, sizeof (channelNum), &channelNum, &nameSize, channelName) == noErr) - name = String::fromUTF8 (channelName, nameSize); - } - - if (input) - { - if (activeInputChans[chanNum]) - { - inputChannelInfo [numInputChannelInfos].streamNum = i; - inputChannelInfo [numInputChannelInfos].dataOffsetSamples = j; - inputChannelInfo [numInputChannelInfos].dataStrideSamples = b.mNumberChannels; - ++numInputChannelInfos; - } - - if (name.isEmpty()) - name << "Input " << (chanNum + 1); - - inChanNames.add (name); - } - else - { - if (activeOutputChans[chanNum]) - { - outputChannelInfo [numOutputChannelInfos].streamNum = i; - outputChannelInfo [numOutputChannelInfos].dataOffsetSamples = j; - outputChannelInfo [numOutputChannelInfos].dataStrideSamples = b.mNumberChannels; - ++numOutputChannelInfos; - } - - if (name.isEmpty()) - name << "Output " << (chanNum + 1); - - outChanNames.add (name); - } - - ++chanNum; - } - } - } - - juce_free (bufList); - } - } - - void updateDetailsFromDevice() - { - stopTimer(); - - if (deviceID == 0) - return; - - const ScopedLock sl (callbackLock); - - Float64 sr; - UInt32 size = sizeof (Float64); - - AudioObjectPropertyAddress pa; - pa.mSelector = kAudioDevicePropertyNominalSampleRate; - pa.mScope = kAudioObjectPropertyScopeWildcard; - pa.mElement = kAudioObjectPropertyElementMaster; - - if (OK (AudioObjectGetPropertyData (deviceID, &pa, 0, 0, &size, &sr))) - sampleRate = sr; - - UInt32 framesPerBuf; - size = sizeof (framesPerBuf); - - pa.mSelector = kAudioDevicePropertyBufferFrameSize; - if (OK (AudioObjectGetPropertyData (deviceID, &pa, 0, 0, &size, &framesPerBuf))) - { - bufferSize = framesPerBuf; - allocateTempBuffers(); - } - - bufferSizes.clear(); - - pa.mSelector = kAudioDevicePropertyBufferFrameSizeRange; - - if (OK (AudioObjectGetPropertyDataSize (deviceID, &pa, 0, 0, &size))) - { - AudioValueRange* ranges = (AudioValueRange*) juce_calloc (size); - - if (OK (AudioObjectGetPropertyData (deviceID, &pa, 0, 0, &size, ranges))) - { - bufferSizes.add ((int) ranges[0].mMinimum); - - for (int i = 32; i < 8192; i += 32) - { - for (int j = size / sizeof (AudioValueRange); --j >= 0;) - { - if (i >= ranges[j].mMinimum && i <= ranges[j].mMaximum) - { - bufferSizes.addIfNotAlreadyThere (i); - break; - } - } - } - - if (bufferSize > 0) - bufferSizes.addIfNotAlreadyThere (bufferSize); - } - - juce_free (ranges); - } - - if (bufferSizes.size() == 0 && bufferSize > 0) - bufferSizes.add (bufferSize); - - sampleRates.clear(); - const double possibleRates[] = { 44100.0, 48000.0, 88200.0, 96000.0, 176400.0, 192000.0 }; - String rates; - - pa.mSelector = kAudioDevicePropertyAvailableNominalSampleRates; - - if (OK (AudioObjectGetPropertyDataSize (deviceID, &pa, 0, 0, &size))) - { - AudioValueRange* ranges = (AudioValueRange*) juce_calloc (size); - - if (OK (AudioObjectGetPropertyData (deviceID, &pa, 0, 0, &size, ranges))) - { - for (int i = 0; i < numElementsInArray (possibleRates); ++i) - { - bool ok = false; - - for (int j = size / sizeof (AudioValueRange); --j >= 0;) - if (possibleRates[i] >= ranges[j].mMinimum - 2 && possibleRates[i] <= ranges[j].mMaximum + 2) - ok = true; - - if (ok) - { - sampleRates.add (possibleRates[i]); - rates << possibleRates[i] << T(" "); - } - } - } - - juce_free (ranges); - } - - if (sampleRates.size() == 0 && sampleRate > 0) - { - sampleRates.add (sampleRate); - rates << sampleRate; - } - - log (T("sr: ") + rates); - - inputLatency = 0; - outputLatency = 0; - UInt32 lat; - size = sizeof (lat); - pa.mSelector = kAudioDevicePropertyLatency; - pa.mScope = kAudioDevicePropertyScopeInput; - //if (AudioDeviceGetProperty (deviceID, 0, true, kAudioDevicePropertyLatency, &size, &lat) == noErr) - if (AudioObjectGetPropertyData (deviceID, &pa, 0, 0, &size, &lat) == noErr) - inputLatency = (int) lat; - - pa.mScope = kAudioDevicePropertyScopeOutput; - size = sizeof (lat); - - if (AudioObjectGetPropertyData (deviceID, &pa, 0, 0, &size, &lat) == noErr) - outputLatency = (int) lat; - - log (T("lat: ") + String (inputLatency) + T(" ") + String (outputLatency)); - - inChanNames.clear(); - outChanNames.clear(); - - juce_free (inputChannelInfo); - inputChannelInfo = (CallbackDetailsForChannel*) juce_calloc (sizeof (CallbackDetailsForChannel) * (numInputChans + 2)); - numInputChannelInfos = 0; - - juce_free (outputChannelInfo); - outputChannelInfo = (CallbackDetailsForChannel*) juce_calloc (sizeof (CallbackDetailsForChannel) * (numOutputChans + 2)); - numOutputChannelInfos = 0; - - fillInChannelInfo (true); - fillInChannelInfo (false); - } - - //============================================================================== - const StringArray getSources (bool input) - { - StringArray s; - int num = 0; - OSType* types = getAllDataSourcesForDevice (deviceID, input, num); - - if (types != 0) - { - for (int i = 0; i < num; ++i) - { - AudioValueTranslation avt; - char buffer[256]; - - avt.mInputData = (void*) &(types[i]); - avt.mInputDataSize = sizeof (UInt32); - avt.mOutputData = buffer; - avt.mOutputDataSize = 256; - - UInt32 transSize = sizeof (avt); - - AudioObjectPropertyAddress pa; - pa.mSelector = kAudioDevicePropertyDataSourceNameForID; - pa.mScope = input ? kAudioDevicePropertyScopeInput : kAudioDevicePropertyScopeOutput; - pa.mElement = kAudioObjectPropertyElementMaster; - - if (OK (AudioObjectGetPropertyData (deviceID, &pa, 0, 0, &transSize, &avt))) - { - DBG (buffer); - s.add (buffer); - } - } - - juce_free (types); - } - - return s; - } - - int getCurrentSourceIndex (bool input) const - { - OSType currentSourceID = 0; - UInt32 size = sizeof (currentSourceID); - int result = -1; - - AudioObjectPropertyAddress pa; - pa.mSelector = kAudioDevicePropertyDataSource; - pa.mScope = input ? kAudioDevicePropertyScopeInput : kAudioDevicePropertyScopeOutput; - pa.mElement = kAudioObjectPropertyElementMaster; - - if (deviceID != 0) - { - if (OK (AudioObjectGetPropertyData (deviceID, &pa, 0, 0, &size, ¤tSourceID))) - { - int num = 0; - OSType* const types = getAllDataSourcesForDevice (deviceID, input, num); - - if (types != 0) - { - for (int i = 0; i < num; ++i) - { - if (types[num] == currentSourceID) - { - result = i; - break; - } - } - - juce_free (types); - } - } - } - - return result; - } - - void setCurrentSourceIndex (int index, bool input) - { - if (deviceID != 0) - { - int num = 0; - OSType* types = getAllDataSourcesForDevice (deviceID, input, num); - - if (types != 0) - { - if (((unsigned int) index) < (unsigned int) num) - { - AudioObjectPropertyAddress pa; - pa.mSelector = kAudioDevicePropertyDataSource; - pa.mScope = input ? kAudioDevicePropertyScopeInput : kAudioDevicePropertyScopeOutput; - pa.mElement = kAudioObjectPropertyElementMaster; - - OSType typeId = types[index]; - - OK (AudioObjectSetPropertyData (deviceID, &pa, 0, 0, sizeof (typeId), &typeId)); - } - - juce_free (types); - } - } - } - - //============================================================================== - const String reopen (const BitArray& inputChannels, - const BitArray& outputChannels, - double newSampleRate, - int bufferSizeSamples) - { - String error; - log ("CoreAudio reopen"); - callbacksAllowed = false; - stopTimer(); - - stop (false); - - activeInputChans = inputChannels; - activeInputChans.setRange (inChanNames.size(), - activeInputChans.getHighestBit() + 1 - inChanNames.size(), - false); - - activeOutputChans = outputChannels; - activeOutputChans.setRange (outChanNames.size(), - activeOutputChans.getHighestBit() + 1 - outChanNames.size(), - false); - - numInputChans = activeInputChans.countNumberOfSetBits(); - numOutputChans = activeOutputChans.countNumberOfSetBits(); - - // set sample rate - AudioObjectPropertyAddress pa; - pa.mSelector = kAudioDevicePropertyNominalSampleRate; - pa.mScope = kAudioObjectPropertyScopeWildcard; - pa.mElement = kAudioObjectPropertyElementMaster; - Float64 sr = newSampleRate; - - if (! OK (AudioObjectSetPropertyData (deviceID, &pa, 0, 0, sizeof (sr), &sr))) - { - error = "Couldn't change sample rate"; - } - else - { - // change buffer size - UInt32 framesPerBuf = bufferSizeSamples; - pa.mSelector = kAudioDevicePropertyBufferFrameSize; - - if (! OK (AudioObjectSetPropertyData (deviceID, &pa, 0, 0, sizeof (framesPerBuf), &framesPerBuf))) - { - error = "Couldn't change buffer size"; - } - else - { - // Annoyingly, after changing the rate and buffer size, some devices fail to - // correctly report their new settings until some random time in the future, so - // after calling updateDetailsFromDevice, we need to manually bodge these values - // to make sure we're using the correct numbers.. - updateDetailsFromDevice(); - sampleRate = newSampleRate; - bufferSize = bufferSizeSamples; - - if (sampleRates.size() == 0) - error = "Device has no available sample-rates"; - else if (bufferSizes.size() == 0) - error = "Device has no available buffer-sizes"; - else if (inputDevice != 0) - error = inputDevice->reopen (inputChannels, - outputChannels, - newSampleRate, - bufferSizeSamples); - } - } - - callbacksAllowed = true; - return error; - } - - bool start (AudioIODeviceCallback* cb) - { - if (! started) - { - callback = 0; - - if (deviceID != 0) - { -#if MACOS_10_4_OR_EARLIER - if (OK (AudioDeviceAddIOProc (deviceID, audioIOProc, (void*) this))) -#else - if (OK (AudioDeviceCreateIOProcID (deviceID, audioIOProc, (void*) this, &audioProcID))) -#endif - { - if (OK (AudioDeviceStart (deviceID, audioIOProc))) - { - started = true; - } - else - { -#if MACOS_10_4_OR_EARLIER - OK (AudioDeviceRemoveIOProc (deviceID, audioIOProc)); -#else - OK (AudioDeviceDestroyIOProcID (deviceID, audioProcID)); - audioProcID = 0; -#endif - } - } - } - } - - if (started) - { - const ScopedLock sl (callbackLock); - callback = cb; - } - - if (inputDevice != 0) - return started && inputDevice->start (cb); - else - return started; - } - - void stop (bool leaveInterruptRunning) - { - callbackLock.enter(); - callback = 0; - callbackLock.exit(); - - if (started - && (deviceID != 0) - && ! leaveInterruptRunning) - { - OK (AudioDeviceStop (deviceID, audioIOProc)); - -#if MACOS_10_4_OR_EARLIER - OK (AudioDeviceRemoveIOProc (deviceID, audioIOProc)); -#else - OK (AudioDeviceDestroyIOProcID (deviceID, audioProcID)); - audioProcID = 0; -#endif - started = false; - - callbackLock.enter(); - callbackLock.exit(); - - // wait until it's definately stopped calling back.. - for (int i = 40; --i >= 0;) - { - Thread::sleep (50); - - UInt32 running = 0; - UInt32 size = sizeof (running); - - AudioObjectPropertyAddress pa; - pa.mSelector = kAudioDevicePropertyDeviceIsRunning; - pa.mScope = kAudioObjectPropertyScopeWildcard; - pa.mElement = kAudioObjectPropertyElementMaster; - - OK (AudioObjectGetPropertyData (deviceID, &pa, 0, 0, &size, &running)); - - if (running == 0) - break; - } - - callbackLock.enter(); - callbackLock.exit(); - } - - if (inputDevice != 0) - inputDevice->stop (leaveInterruptRunning); - } - - double getSampleRate() const - { - return sampleRate; - } - - int getBufferSize() const - { - return bufferSize; - } - - void audioCallback (const AudioBufferList* inInputData, - AudioBufferList* outOutputData) - { - int i; - const ScopedLock sl (callbackLock); - - if (callback != 0) - { - if (inputDevice == 0) - { - for (i = numInputChans; --i >= 0;) - { - const CallbackDetailsForChannel& info = inputChannelInfo[i]; - float* dest = tempInputBuffers [i]; - const float* src = ((const float*) inInputData->mBuffers[info.streamNum].mData) - + info.dataOffsetSamples; - const int stride = info.dataStrideSamples; - - if (stride != 0) // if this is zero, info is invalid - { - for (int j = bufferSize; --j >= 0;) - { - *dest++ = *src; - src += stride; - } - } - } - } - - if (! isSlaveDevice) - { - if (inputDevice == 0) - { - callback->audioDeviceIOCallback ((const float**) tempInputBuffers, - numInputChans, - tempOutputBuffers, - numOutputChans, - bufferSize); - } - else - { - jassert (inputDevice->bufferSize == bufferSize); - - // Sometimes the two linked devices seem to get their callbacks in - // parallel, so we need to lock both devices to stop the input data being - // changed while inside our callback.. - const ScopedLock sl (inputDevice->callbackLock); - - callback->audioDeviceIOCallback ((const float**) inputDevice->tempInputBuffers, - inputDevice->numInputChans, - tempOutputBuffers, - numOutputChans, - bufferSize); - } - - for (i = numOutputChans; --i >= 0;) - { - const CallbackDetailsForChannel& info = outputChannelInfo[i]; - const float* src = tempOutputBuffers [i]; - float* dest = ((float*) outOutputData->mBuffers[info.streamNum].mData) - + info.dataOffsetSamples; - const int stride = info.dataStrideSamples; - - if (stride != 0) // if this is zero, info is invalid - { - for (int j = bufferSize; --j >= 0;) - { - *dest = *src++; - dest += stride; - } - } - } - } - } - else - { - for (i = jmin (numOutputChans, numOutputChannelInfos); --i >= 0;) - { - const CallbackDetailsForChannel& info = outputChannelInfo[i]; - float* dest = ((float*) outOutputData->mBuffers[info.streamNum].mData) - + info.dataOffsetSamples; - const int stride = info.dataStrideSamples; - - if (stride != 0) // if this is zero, info is invalid - { - for (int j = bufferSize; --j >= 0;) - { - *dest = 0.0f; - dest += stride; - } - } - } - } - } - - // called by callbacks - void deviceDetailsChanged() - { - if (callbacksAllowed) - startTimer (100); - } - - void timerCallback() - { - stopTimer(); - log ("CoreAudio device changed callback"); - - const double oldSampleRate = sampleRate; - const int oldBufferSize = bufferSize; - updateDetailsFromDevice(); - - if (oldBufferSize != bufferSize || oldSampleRate != sampleRate) - { - callbacksAllowed = false; - stop (false); - updateDetailsFromDevice(); - callbacksAllowed = true; - } - } - - CoreAudioInternal* getRelatedDevice() const - { - UInt32 size = 0; - CoreAudioInternal* result = 0; - - AudioObjectPropertyAddress pa; - pa.mSelector = kAudioDevicePropertyRelatedDevices; - pa.mScope = kAudioObjectPropertyScopeWildcard; - pa.mElement = kAudioObjectPropertyElementMaster; - - if (deviceID != 0 - && AudioObjectGetPropertyDataSize (deviceID, &pa, 0, 0, &size) == noErr - && size > 0) - { - AudioDeviceID* devs = (AudioDeviceID*) juce_calloc (size); - - if (OK (AudioObjectGetPropertyData (deviceID, &pa, 0, 0, &size, devs))) - { - for (unsigned int i = 0; i < size / sizeof (AudioDeviceID); ++i) - { - if (devs[i] != deviceID && devs[i] != 0) - { - result = new CoreAudioInternal (devs[i]); - - const bool thisIsInput = inChanNames.size() > 0 && outChanNames.size() == 0; - const bool otherIsInput = result->inChanNames.size() > 0 && result->outChanNames.size() == 0; - - if (thisIsInput != otherIsInput - || (inChanNames.size() + outChanNames.size() == 0) - || (result->inChanNames.size() + result->outChanNames.size()) == 0) - break; - - deleteAndZero (result); - } - } - } - - juce_free (devs); - } - - return result; - } - - //============================================================================== - juce_UseDebuggingNewOperator - - int inputLatency, outputLatency; - BitArray activeInputChans, activeOutputChans; - StringArray inChanNames, outChanNames; - Array sampleRates; - Array bufferSizes; - AudioIODeviceCallback* callback; -#if ! MACOS_10_4_OR_EARLIER - AudioDeviceIOProcID audioProcID; -#endif - - CoreAudioInternal* inputDevice; - bool isSlaveDevice; - -private: - CriticalSection callbackLock; - AudioDeviceID deviceID; - bool started; - double sampleRate; - int bufferSize; - float* audioBuffer; - int numInputChans, numOutputChans; - bool callbacksAllowed; - - struct CallbackDetailsForChannel - { - int streamNum; - int dataOffsetSamples; - int dataStrideSamples; - }; - - int numInputChannelInfos, numOutputChannelInfos; - CallbackDetailsForChannel* inputChannelInfo; - CallbackDetailsForChannel* outputChannelInfo; - float** tempInputBuffers; - float** tempOutputBuffers; - - CoreAudioInternal (const CoreAudioInternal&); - const CoreAudioInternal& operator= (const CoreAudioInternal&); - - //============================================================================== - static OSStatus audioIOProc (AudioDeviceID inDevice, - const AudioTimeStamp* inNow, - const AudioBufferList* inInputData, - const AudioTimeStamp* inInputTime, - AudioBufferList* outOutputData, - const AudioTimeStamp* inOutputTime, - void* device) - { - ((CoreAudioInternal*) device)->audioCallback (inInputData, outOutputData); - return noErr; - } - - static OSStatus deviceListenerProc (AudioDeviceID /*inDevice*/, UInt32 /*inLine*/, const AudioObjectPropertyAddress* pa, void* inClientData) - { - CoreAudioInternal* const intern = (CoreAudioInternal*) inClientData; - - switch (pa->mSelector) - { - case kAudioDevicePropertyBufferSize: - case kAudioDevicePropertyBufferFrameSize: - case kAudioDevicePropertyNominalSampleRate: - case kAudioDevicePropertyStreamFormat: - case kAudioDevicePropertyDeviceIsAlive: - intern->deviceDetailsChanged(); - break; - - case kAudioDevicePropertyBufferSizeRange: - case kAudioDevicePropertyVolumeScalar: - case kAudioDevicePropertyMute: - case kAudioDevicePropertyPlayThru: - case kAudioDevicePropertyDataSource: - case kAudioDevicePropertyDeviceIsRunning: - break; - } - - return noErr; - } - - //============================================================================== - static OSType* getAllDataSourcesForDevice (AudioDeviceID deviceID, const bool input, int& num) - { - OSType* types = 0; - UInt32 size = 0; - num = 0; - - AudioObjectPropertyAddress pa; - pa.mSelector = kAudioDevicePropertyDataSources; - pa.mScope = kAudioObjectPropertyScopeWildcard; - pa.mElement = kAudioObjectPropertyElementMaster; - - if (deviceID != 0 - && OK (AudioObjectGetPropertyDataSize (deviceID, &pa, 0, 0, &size))) - { - types = (OSType*) juce_calloc (size); - - if (OK (AudioObjectGetPropertyData (deviceID, &pa, 0, 0, &size, types))) - { - num = size / sizeof (OSType); - } - else - { - juce_free (types); - types = 0; - } - } - - return types; - } -}; - - -//============================================================================== -class CoreAudioIODevice : public AudioIODevice -{ -public: - CoreAudioIODevice (const String& deviceName, - AudioDeviceID inputDeviceId, - const int inputIndex_, - AudioDeviceID outputDeviceId, - const int outputIndex_) - : AudioIODevice (deviceName, "CoreAudio"), - inputIndex (inputIndex_), - outputIndex (outputIndex_), - isOpen_ (false), - isStarted (false) - { - internal = 0; - CoreAudioInternal* device = 0; - - if (outputDeviceId == 0 || outputDeviceId == inputDeviceId) - { - jassert (inputDeviceId != 0); - - device = new CoreAudioInternal (inputDeviceId); - } - else - { - device = new CoreAudioInternal (outputDeviceId); - - if (inputDeviceId != 0) - { - CoreAudioInternal* secondDevice = new CoreAudioInternal (inputDeviceId); - - device->inputDevice = secondDevice; - secondDevice->isSlaveDevice = true; - } - } - - internal = device; - - AudioObjectPropertyAddress pa; - pa.mSelector = kAudioObjectPropertySelectorWildcard; - pa.mScope = kAudioObjectPropertyScopeWildcard; - pa.mElement = kAudioObjectPropertyElementWildcard; - - AudioObjectAddPropertyListener (kAudioObjectSystemObject, &pa, hardwareListenerProc, internal); - } - - ~CoreAudioIODevice() - { - AudioObjectPropertyAddress pa; - pa.mSelector = kAudioObjectPropertySelectorWildcard; - pa.mScope = kAudioObjectPropertyScopeWildcard; - pa.mElement = kAudioObjectPropertyElementWildcard; - - AudioObjectRemovePropertyListener (kAudioObjectSystemObject, &pa, hardwareListenerProc, internal); - - delete internal; - } - - const StringArray getOutputChannelNames() - { - return internal->outChanNames; - } - - const StringArray getInputChannelNames() - { - if (internal->inputDevice != 0) - return internal->inputDevice->inChanNames; - else - return internal->inChanNames; - } - - int getNumSampleRates() - { - return internal->sampleRates.size(); - } - - double getSampleRate (int index) - { - return internal->sampleRates [index]; - } - - int getNumBufferSizesAvailable() - { - return internal->bufferSizes.size(); - } - - int getBufferSizeSamples (int index) - { - return internal->bufferSizes [index]; - } - - int getDefaultBufferSize() - { - for (int i = 0; i < getNumBufferSizesAvailable(); ++i) - if (getBufferSizeSamples(i) >= 512) - return getBufferSizeSamples(i); - - return 512; - } - - const String open (const BitArray& inputChannels, - const BitArray& outputChannels, - double sampleRate, - int bufferSizeSamples) - { - isOpen_ = true; - - if (bufferSizeSamples <= 0) - bufferSizeSamples = getDefaultBufferSize(); - - lastError = internal->reopen (inputChannels, outputChannels, sampleRate, bufferSizeSamples); - isOpen_ = lastError.isEmpty(); - return lastError; - } - - void close() - { - isOpen_ = false; - internal->stop (false); - } - - bool isOpen() - { - return isOpen_; - } - - int getCurrentBufferSizeSamples() - { - return internal != 0 ? internal->getBufferSize() : 512; - } - - double getCurrentSampleRate() - { - return internal != 0 ? internal->getSampleRate() : 0; - } - - int getCurrentBitDepth() - { - return 32; // no way to find out, so just assume it's high.. - } - - const BitArray getActiveOutputChannels() const - { - return internal != 0 ? internal->activeOutputChans : BitArray(); - } - - const BitArray getActiveInputChannels() const - { - BitArray chans; - - if (internal != 0) - { - chans = internal->activeInputChans; - - if (internal->inputDevice != 0) - chans.orWith (internal->inputDevice->activeInputChans); - } - - return chans; - } - - int getOutputLatencyInSamples() - { - if (internal == 0) - return 0; - - // this seems like a good guess at getting the latency right - comparing - // this with a round-trip measurement, it gets it to within a few millisecs - // for the built-in mac soundcard - return internal->outputLatency + internal->getBufferSize() * 2; - } - - int getInputLatencyInSamples() - { - if (internal == 0) - return 0; - - return internal->inputLatency + internal->getBufferSize() * 2; - } - - void start (AudioIODeviceCallback* callback) - { - if (internal != 0 && ! isStarted) - { - if (callback != 0) - callback->audioDeviceAboutToStart (this); - - isStarted = true; - internal->start (callback); - } - } - - void stop() - { - if (isStarted && internal != 0) - { - AudioIODeviceCallback* const lastCallback = internal->callback; - - isStarted = false; - internal->stop (true); - - if (lastCallback != 0) - lastCallback->audioDeviceStopped(); - } - } - - bool isPlaying() - { - if (internal->callback == 0) - isStarted = false; - - return isStarted; - } - - const String getLastError() - { - return lastError; - } - - int inputIndex, outputIndex; - - juce_UseDebuggingNewOperator - -private: - CoreAudioInternal* internal; - bool isOpen_, isStarted; - String lastError; - - static OSStatus hardwareListenerProc (AudioDeviceID /*inDevice*/, UInt32 /*inLine*/, const AudioObjectPropertyAddress* pa, void* inClientData) - { - CoreAudioInternal* const intern = (CoreAudioInternal*) inClientData; - - switch (pa->mSelector) - { - case kAudioHardwarePropertyDevices: - intern->deviceDetailsChanged(); - break; - - case kAudioHardwarePropertyDefaultOutputDevice: - case kAudioHardwarePropertyDefaultInputDevice: - case kAudioHardwarePropertyDefaultSystemOutputDevice: - break; - } - - return noErr; - } - - CoreAudioIODevice (const CoreAudioIODevice&); - const CoreAudioIODevice& operator= (const CoreAudioIODevice&); -}; - -//============================================================================== -class CoreAudioIODeviceType : public AudioIODeviceType -{ -public: - //============================================================================== - CoreAudioIODeviceType() - : AudioIODeviceType (T("CoreAudio")), - hasScanned (false) - { - } - - ~CoreAudioIODeviceType() - { - } - - //============================================================================== - void scanForDevices() - { - hasScanned = true; - - inputDeviceNames.clear(); - outputDeviceNames.clear(); - inputIds.clear(); - outputIds.clear(); - - UInt32 size; - - AudioObjectPropertyAddress pa; - pa.mSelector = kAudioHardwarePropertyDevices; - pa.mScope = kAudioObjectPropertyScopeWildcard; - pa.mElement = kAudioObjectPropertyElementMaster; - - if (OK (AudioObjectGetPropertyDataSize (kAudioObjectSystemObject, &pa, 0, 0, &size))) - { - AudioDeviceID* const devs = (AudioDeviceID*) juce_calloc (size); - - if (OK (AudioObjectGetPropertyData (kAudioObjectSystemObject, &pa, 0, 0, &size, devs))) - { - static bool alreadyLogged = false; - const int num = size / sizeof (AudioDeviceID); - for (int i = 0; i < num; ++i) - { - char name [1024]; - size = sizeof (name); - pa.mSelector = kAudioDevicePropertyDeviceName; - - if (OK (AudioObjectGetPropertyData (devs[i], &pa, 0, 0, &size, name))) - { - const String nameString (String::fromUTF8 ((const uint8*) name, strlen (name))); - - if (! alreadyLogged) - log (T("CoreAudio device: ") + nameString); - - const int numIns = getNumChannels (devs[i], true); - const int numOuts = getNumChannels (devs[i], false); - - if (numIns > 0) - { - inputDeviceNames.add (nameString); - inputIds.add (devs[i]); - } - - if (numOuts > 0) - { - outputDeviceNames.add (nameString); - outputIds.add (devs[i]); - } - } - } - - alreadyLogged = true; - } - - juce_free (devs); - } - - inputDeviceNames.appendNumbersToDuplicates (false, true); - outputDeviceNames.appendNumbersToDuplicates (false, true); - } - - const StringArray getDeviceNames (const bool wantInputNames) const - { - jassert (hasScanned); // need to call scanForDevices() before doing this - - if (wantInputNames) - return inputDeviceNames; - else - return outputDeviceNames; - } - - int getDefaultDeviceIndex (const bool forInput) const - { - jassert (hasScanned); // need to call scanForDevices() before doing this - - AudioDeviceID deviceID; - UInt32 size = sizeof (deviceID); - - // if they're asking for any input channels at all, use the default input, so we - // get the built-in mic rather than the built-in output with no inputs.. - - AudioObjectPropertyAddress pa; - pa.mSelector = forInput ? kAudioHardwarePropertyDefaultInputDevice : kAudioHardwarePropertyDefaultOutputDevice; - pa.mScope = kAudioObjectPropertyScopeWildcard; - pa.mElement = kAudioObjectPropertyElementMaster; - - if (AudioObjectGetPropertyData (kAudioObjectSystemObject, &pa, 0, 0, &size, &deviceID) == noErr) - { - if (forInput) - { - for (int i = inputIds.size(); --i >= 0;) - if (inputIds[i] == deviceID) - return i; - } - else - { - for (int i = outputIds.size(); --i >= 0;) - if (outputIds[i] == deviceID) - return i; - } - } - - return 0; - } - - int getIndexOfDevice (AudioIODevice* device, const bool asInput) const - { - jassert (hasScanned); // need to call scanForDevices() before doing this - - CoreAudioIODevice* const d = dynamic_cast (device); - if (d == 0) - return -1; - - return asInput ? d->inputIndex - : d->outputIndex; - } - - bool hasSeparateInputsAndOutputs() const { return true; } - - AudioIODevice* createDevice (const String& outputDeviceName, - const String& inputDeviceName) - { - jassert (hasScanned); // need to call scanForDevices() before doing this - - const int inputIndex = inputDeviceNames.indexOf (inputDeviceName); - const int outputIndex = outputDeviceNames.indexOf (outputDeviceName); - - String deviceName (outputDeviceName); - if (deviceName.isEmpty()) - deviceName = inputDeviceName; - - if (index >= 0) - return new CoreAudioIODevice (deviceName, - inputIds [inputIndex], - inputIndex, - outputIds [outputIndex], - outputIndex); - - return 0; - } - - //============================================================================== - juce_UseDebuggingNewOperator - -private: - StringArray inputDeviceNames, outputDeviceNames; - Array inputIds, outputIds; - - bool hasScanned; - - static int getNumChannels (AudioDeviceID deviceID, bool input) - { - int total = 0; - UInt32 size; - - AudioObjectPropertyAddress pa; - pa.mSelector = kAudioDevicePropertyStreamConfiguration; - pa.mScope = input ? kAudioDevicePropertyScopeInput : kAudioDevicePropertyScopeOutput; - pa.mElement = kAudioObjectPropertyElementMaster; - - if (OK (AudioObjectGetPropertyDataSize (deviceID, &pa, 0, 0, &size))) - { - AudioBufferList* const bufList = (AudioBufferList*) juce_calloc (size); - - if (OK (AudioObjectGetPropertyData (deviceID, &pa, 0, 0, &size, bufList))) - { - const int numStreams = bufList->mNumberBuffers; - - for (int i = 0; i < numStreams; ++i) - { - const AudioBuffer& b = bufList->mBuffers[i]; - total += b.mNumberChannels; - } - } - - juce_free (bufList); - } - - return total; - } - - CoreAudioIODeviceType (const CoreAudioIODeviceType&); - const CoreAudioIODeviceType& operator= (const CoreAudioIODeviceType&); -}; - -//============================================================================== -AudioIODeviceType* juce_createAudioIODeviceType_CoreAudio() -{ - return new CoreAudioIODeviceType(); -} - -#undef log - -#endif +/* + ============================================================================== + + This file is part of the JUCE library - "Jules' Utility Class Extensions" + Copyright 2004-9 by Raw Material Software Ltd. + + ------------------------------------------------------------------------------ + + JUCE can be redistributed and/or modified under the terms of the GNU General + Public License (Version 2), as published by the Free Software Foundation. + A copy of the license is included in the JUCE distribution, or can be found + online at www.gnu.org/licenses. + + JUCE is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + ------------------------------------------------------------------------------ + + To release a closed-source product which uses JUCE, commercial licenses are + available: visit www.rawmaterialsoftware.com/juce for more information. + + ============================================================================== +*/ + +// (This file gets included by juce_mac_NativeCode.mm, rather than being +// compiled on its own). +#ifdef JUCE_INCLUDED_FILE + + +//============================================================================== +#ifndef JUCE_COREAUDIO_ERROR_LOGGING_ENABLED + #define JUCE_COREAUDIO_ERROR_LOGGING_ENABLED 1 +#endif + +//============================================================================== +#undef log +#if JUCE_COREAUDIO_LOGGING_ENABLED + #define log(a) Logger::writeToLog (a) +#else + #define log(a) +#endif + +#undef OK +#if JUCE_COREAUDIO_ERROR_LOGGING_ENABLED + static bool logAnyErrors_CoreAudio (const OSStatus err, const int lineNum) + { + if (err == noErr) + return true; + + Logger::writeToLog (T("CoreAudio error: ") + String (lineNum) + T(" - ") + String::toHexString ((int)err)); + jassertfalse + return false; + } + + #define OK(a) logAnyErrors_CoreAudio (a, __LINE__) +#else + #define OK(a) (a == noErr) +#endif + + +//============================================================================== +class CoreAudioInternal : public Timer +{ +public: + //============================================================================== + CoreAudioInternal (AudioDeviceID id) + : inputLatency (0), + outputLatency (0), + callback (0), +#if ! MACOS_10_4_OR_EARLIER + audioProcID (0), +#endif + inputDevice (0), + isSlaveDevice (false), + deviceID (id), + started (false), + sampleRate (0), + bufferSize (512), + audioBuffer (0), + numInputChans (0), + numOutputChans (0), + callbacksAllowed (true), + numInputChannelInfos (0), + numOutputChannelInfos (0), + tempInputBuffers (0), + tempOutputBuffers (0), + inputChannelInfo (0), + outputChannelInfo (0) + { + jassert (deviceID != 0); + + updateDetailsFromDevice(); + + AudioObjectPropertyAddress pa; + pa.mSelector = kAudioObjectPropertySelectorWildcard; + pa.mScope = kAudioObjectPropertyScopeWildcard; + pa.mElement = kAudioObjectPropertyElementWildcard; + + AudioObjectAddPropertyListener (deviceID, &pa, deviceListenerProc, this); + } + + ~CoreAudioInternal() + { + AudioObjectPropertyAddress pa; + pa.mSelector = kAudioObjectPropertySelectorWildcard; + pa.mScope = kAudioObjectPropertyScopeWildcard; + pa.mElement = kAudioObjectPropertyElementWildcard; + + AudioObjectRemovePropertyListener (deviceID, &pa, deviceListenerProc, this); + + stop (false); + delete inputDevice; + + juce_free (audioBuffer); + juce_free (tempInputBuffers); + juce_free (tempOutputBuffers); + juce_free (inputChannelInfo); + juce_free (outputChannelInfo); + } + + void allocateTempBuffers() + { + const int tempBufSize = bufferSize + 4; + juce_free (audioBuffer); + audioBuffer = (float*) juce_calloc ((numInputChans + numOutputChans) * tempBufSize * sizeof (float)); + + juce_free (tempInputBuffers); + tempInputBuffers = (float**) juce_calloc (sizeof (float*) * (numInputChans + 2)); + juce_free (tempOutputBuffers); + tempOutputBuffers = (float**) juce_calloc (sizeof (float*) * (numOutputChans + 2)); + + int i, count = 0; + for (i = 0; i < numInputChans; ++i) + tempInputBuffers[i] = audioBuffer + count++ * tempBufSize; + + for (i = 0; i < numOutputChans; ++i) + tempOutputBuffers[i] = audioBuffer + count++ * tempBufSize; + } + + // returns the number of actual available channels + void fillInChannelInfo (const bool input) + { + int chanNum = 0; + UInt32 size; + + AudioObjectPropertyAddress pa; + pa.mSelector = kAudioDevicePropertyStreamConfiguration; + pa.mScope = input ? kAudioDevicePropertyScopeInput : kAudioDevicePropertyScopeOutput; + pa.mElement = kAudioObjectPropertyElementMaster; + + if (OK (AudioObjectGetPropertyDataSize (deviceID, &pa, 0, 0, &size))) + { + AudioBufferList* const bufList = (AudioBufferList*) juce_calloc (size); + + if (OK (AudioObjectGetPropertyData (deviceID, &pa, 0, 0, &size, bufList))) + { + const int numStreams = bufList->mNumberBuffers; + + for (int i = 0; i < numStreams; ++i) + { + const AudioBuffer& b = bufList->mBuffers[i]; + + for (unsigned int j = 0; j < b.mNumberChannels; ++j) + { + String name; + + { + uint8 channelName [256]; + zerostruct (channelName); + UInt32 nameSize = sizeof (channelName); + UInt32 channelNum = chanNum + 1; + pa.mSelector = kAudioDevicePropertyChannelName; + + if (AudioObjectGetPropertyData (deviceID, &pa, sizeof (channelNum), &channelNum, &nameSize, channelName) == noErr) + name = String::fromUTF8 (channelName, nameSize); + } + + if (input) + { + if (activeInputChans[chanNum]) + { + inputChannelInfo [numInputChannelInfos].streamNum = i; + inputChannelInfo [numInputChannelInfos].dataOffsetSamples = j; + inputChannelInfo [numInputChannelInfos].dataStrideSamples = b.mNumberChannels; + ++numInputChannelInfos; + } + + if (name.isEmpty()) + name << "Input " << (chanNum + 1); + + inChanNames.add (name); + } + else + { + if (activeOutputChans[chanNum]) + { + outputChannelInfo [numOutputChannelInfos].streamNum = i; + outputChannelInfo [numOutputChannelInfos].dataOffsetSamples = j; + outputChannelInfo [numOutputChannelInfos].dataStrideSamples = b.mNumberChannels; + ++numOutputChannelInfos; + } + + if (name.isEmpty()) + name << "Output " << (chanNum + 1); + + outChanNames.add (name); + } + + ++chanNum; + } + } + } + + juce_free (bufList); + } + } + + void updateDetailsFromDevice() + { + stopTimer(); + + if (deviceID == 0) + return; + + const ScopedLock sl (callbackLock); + + Float64 sr; + UInt32 size = sizeof (Float64); + + AudioObjectPropertyAddress pa; + pa.mSelector = kAudioDevicePropertyNominalSampleRate; + pa.mScope = kAudioObjectPropertyScopeWildcard; + pa.mElement = kAudioObjectPropertyElementMaster; + + if (OK (AudioObjectGetPropertyData (deviceID, &pa, 0, 0, &size, &sr))) + sampleRate = sr; + + UInt32 framesPerBuf; + size = sizeof (framesPerBuf); + + pa.mSelector = kAudioDevicePropertyBufferFrameSize; + if (OK (AudioObjectGetPropertyData (deviceID, &pa, 0, 0, &size, &framesPerBuf))) + { + bufferSize = framesPerBuf; + allocateTempBuffers(); + } + + bufferSizes.clear(); + + pa.mSelector = kAudioDevicePropertyBufferFrameSizeRange; + + if (OK (AudioObjectGetPropertyDataSize (deviceID, &pa, 0, 0, &size))) + { + AudioValueRange* ranges = (AudioValueRange*) juce_calloc (size); + + if (OK (AudioObjectGetPropertyData (deviceID, &pa, 0, 0, &size, ranges))) + { + bufferSizes.add ((int) ranges[0].mMinimum); + + for (int i = 32; i < 8192; i += 32) + { + for (int j = size / sizeof (AudioValueRange); --j >= 0;) + { + if (i >= ranges[j].mMinimum && i <= ranges[j].mMaximum) + { + bufferSizes.addIfNotAlreadyThere (i); + break; + } + } + } + + if (bufferSize > 0) + bufferSizes.addIfNotAlreadyThere (bufferSize); + } + + juce_free (ranges); + } + + if (bufferSizes.size() == 0 && bufferSize > 0) + bufferSizes.add (bufferSize); + + sampleRates.clear(); + const double possibleRates[] = { 44100.0, 48000.0, 88200.0, 96000.0, 176400.0, 192000.0 }; + String rates; + + pa.mSelector = kAudioDevicePropertyAvailableNominalSampleRates; + + if (OK (AudioObjectGetPropertyDataSize (deviceID, &pa, 0, 0, &size))) + { + AudioValueRange* ranges = (AudioValueRange*) juce_calloc (size); + + if (OK (AudioObjectGetPropertyData (deviceID, &pa, 0, 0, &size, ranges))) + { + for (int i = 0; i < numElementsInArray (possibleRates); ++i) + { + bool ok = false; + + for (int j = size / sizeof (AudioValueRange); --j >= 0;) + if (possibleRates[i] >= ranges[j].mMinimum - 2 && possibleRates[i] <= ranges[j].mMaximum + 2) + ok = true; + + if (ok) + { + sampleRates.add (possibleRates[i]); + rates << possibleRates[i] << T(" "); + } + } + } + + juce_free (ranges); + } + + if (sampleRates.size() == 0 && sampleRate > 0) + { + sampleRates.add (sampleRate); + rates << sampleRate; + } + + log (T("sr: ") + rates); + + inputLatency = 0; + outputLatency = 0; + UInt32 lat; + size = sizeof (lat); + pa.mSelector = kAudioDevicePropertyLatency; + pa.mScope = kAudioDevicePropertyScopeInput; + //if (AudioDeviceGetProperty (deviceID, 0, true, kAudioDevicePropertyLatency, &size, &lat) == noErr) + if (AudioObjectGetPropertyData (deviceID, &pa, 0, 0, &size, &lat) == noErr) + inputLatency = (int) lat; + + pa.mScope = kAudioDevicePropertyScopeOutput; + size = sizeof (lat); + + if (AudioObjectGetPropertyData (deviceID, &pa, 0, 0, &size, &lat) == noErr) + outputLatency = (int) lat; + + log (T("lat: ") + String (inputLatency) + T(" ") + String (outputLatency)); + + inChanNames.clear(); + outChanNames.clear(); + + juce_free (inputChannelInfo); + inputChannelInfo = (CallbackDetailsForChannel*) juce_calloc (sizeof (CallbackDetailsForChannel) * (numInputChans + 2)); + numInputChannelInfos = 0; + + juce_free (outputChannelInfo); + outputChannelInfo = (CallbackDetailsForChannel*) juce_calloc (sizeof (CallbackDetailsForChannel) * (numOutputChans + 2)); + numOutputChannelInfos = 0; + + fillInChannelInfo (true); + fillInChannelInfo (false); + } + + //============================================================================== + const StringArray getSources (bool input) + { + StringArray s; + int num = 0; + OSType* types = getAllDataSourcesForDevice (deviceID, input, num); + + if (types != 0) + { + for (int i = 0; i < num; ++i) + { + AudioValueTranslation avt; + char buffer[256]; + + avt.mInputData = (void*) &(types[i]); + avt.mInputDataSize = sizeof (UInt32); + avt.mOutputData = buffer; + avt.mOutputDataSize = 256; + + UInt32 transSize = sizeof (avt); + + AudioObjectPropertyAddress pa; + pa.mSelector = kAudioDevicePropertyDataSourceNameForID; + pa.mScope = input ? kAudioDevicePropertyScopeInput : kAudioDevicePropertyScopeOutput; + pa.mElement = kAudioObjectPropertyElementMaster; + + if (OK (AudioObjectGetPropertyData (deviceID, &pa, 0, 0, &transSize, &avt))) + { + DBG (buffer); + s.add (buffer); + } + } + + juce_free (types); + } + + return s; + } + + int getCurrentSourceIndex (bool input) const + { + OSType currentSourceID = 0; + UInt32 size = sizeof (currentSourceID); + int result = -1; + + AudioObjectPropertyAddress pa; + pa.mSelector = kAudioDevicePropertyDataSource; + pa.mScope = input ? kAudioDevicePropertyScopeInput : kAudioDevicePropertyScopeOutput; + pa.mElement = kAudioObjectPropertyElementMaster; + + if (deviceID != 0) + { + if (OK (AudioObjectGetPropertyData (deviceID, &pa, 0, 0, &size, ¤tSourceID))) + { + int num = 0; + OSType* const types = getAllDataSourcesForDevice (deviceID, input, num); + + if (types != 0) + { + for (int i = 0; i < num; ++i) + { + if (types[num] == currentSourceID) + { + result = i; + break; + } + } + + juce_free (types); + } + } + } + + return result; + } + + void setCurrentSourceIndex (int index, bool input) + { + if (deviceID != 0) + { + int num = 0; + OSType* types = getAllDataSourcesForDevice (deviceID, input, num); + + if (types != 0) + { + if (((unsigned int) index) < (unsigned int) num) + { + AudioObjectPropertyAddress pa; + pa.mSelector = kAudioDevicePropertyDataSource; + pa.mScope = input ? kAudioDevicePropertyScopeInput : kAudioDevicePropertyScopeOutput; + pa.mElement = kAudioObjectPropertyElementMaster; + + OSType typeId = types[index]; + + OK (AudioObjectSetPropertyData (deviceID, &pa, 0, 0, sizeof (typeId), &typeId)); + } + + juce_free (types); + } + } + } + + //============================================================================== + const String reopen (const BitArray& inputChannels, + const BitArray& outputChannels, + double newSampleRate, + int bufferSizeSamples) + { + String error; + log ("CoreAudio reopen"); + callbacksAllowed = false; + stopTimer(); + + stop (false); + + activeInputChans = inputChannels; + activeInputChans.setRange (inChanNames.size(), + activeInputChans.getHighestBit() + 1 - inChanNames.size(), + false); + + activeOutputChans = outputChannels; + activeOutputChans.setRange (outChanNames.size(), + activeOutputChans.getHighestBit() + 1 - outChanNames.size(), + false); + + numInputChans = activeInputChans.countNumberOfSetBits(); + numOutputChans = activeOutputChans.countNumberOfSetBits(); + + // set sample rate + AudioObjectPropertyAddress pa; + pa.mSelector = kAudioDevicePropertyNominalSampleRate; + pa.mScope = kAudioObjectPropertyScopeWildcard; + pa.mElement = kAudioObjectPropertyElementMaster; + Float64 sr = newSampleRate; + + if (! OK (AudioObjectSetPropertyData (deviceID, &pa, 0, 0, sizeof (sr), &sr))) + { + error = "Couldn't change sample rate"; + } + else + { + // change buffer size + UInt32 framesPerBuf = bufferSizeSamples; + pa.mSelector = kAudioDevicePropertyBufferFrameSize; + + if (! OK (AudioObjectSetPropertyData (deviceID, &pa, 0, 0, sizeof (framesPerBuf), &framesPerBuf))) + { + error = "Couldn't change buffer size"; + } + else + { + // Annoyingly, after changing the rate and buffer size, some devices fail to + // correctly report their new settings until some random time in the future, so + // after calling updateDetailsFromDevice, we need to manually bodge these values + // to make sure we're using the correct numbers.. + updateDetailsFromDevice(); + sampleRate = newSampleRate; + bufferSize = bufferSizeSamples; + + if (sampleRates.size() == 0) + error = "Device has no available sample-rates"; + else if (bufferSizes.size() == 0) + error = "Device has no available buffer-sizes"; + else if (inputDevice != 0) + error = inputDevice->reopen (inputChannels, + outputChannels, + newSampleRate, + bufferSizeSamples); + } + } + + callbacksAllowed = true; + return error; + } + + bool start (AudioIODeviceCallback* cb) + { + if (! started) + { + callback = 0; + + if (deviceID != 0) + { +#if MACOS_10_4_OR_EARLIER + if (OK (AudioDeviceAddIOProc (deviceID, audioIOProc, (void*) this))) +#else + if (OK (AudioDeviceCreateIOProcID (deviceID, audioIOProc, (void*) this, &audioProcID))) +#endif + { + if (OK (AudioDeviceStart (deviceID, audioIOProc))) + { + started = true; + } + else + { +#if MACOS_10_4_OR_EARLIER + OK (AudioDeviceRemoveIOProc (deviceID, audioIOProc)); +#else + OK (AudioDeviceDestroyIOProcID (deviceID, audioProcID)); + audioProcID = 0; +#endif + } + } + } + } + + if (started) + { + const ScopedLock sl (callbackLock); + callback = cb; + } + + if (inputDevice != 0) + return started && inputDevice->start (cb); + else + return started; + } + + void stop (bool leaveInterruptRunning) + { + callbackLock.enter(); + callback = 0; + callbackLock.exit(); + + if (started + && (deviceID != 0) + && ! leaveInterruptRunning) + { + OK (AudioDeviceStop (deviceID, audioIOProc)); + +#if MACOS_10_4_OR_EARLIER + OK (AudioDeviceRemoveIOProc (deviceID, audioIOProc)); +#else + OK (AudioDeviceDestroyIOProcID (deviceID, audioProcID)); + audioProcID = 0; +#endif + started = false; + + callbackLock.enter(); + callbackLock.exit(); + + // wait until it's definately stopped calling back.. + for (int i = 40; --i >= 0;) + { + Thread::sleep (50); + + UInt32 running = 0; + UInt32 size = sizeof (running); + + AudioObjectPropertyAddress pa; + pa.mSelector = kAudioDevicePropertyDeviceIsRunning; + pa.mScope = kAudioObjectPropertyScopeWildcard; + pa.mElement = kAudioObjectPropertyElementMaster; + + OK (AudioObjectGetPropertyData (deviceID, &pa, 0, 0, &size, &running)); + + if (running == 0) + break; + } + + callbackLock.enter(); + callbackLock.exit(); + } + + if (inputDevice != 0) + inputDevice->stop (leaveInterruptRunning); + } + + double getSampleRate() const + { + return sampleRate; + } + + int getBufferSize() const + { + return bufferSize; + } + + void audioCallback (const AudioBufferList* inInputData, + AudioBufferList* outOutputData) + { + int i; + const ScopedLock sl (callbackLock); + + if (callback != 0) + { + if (inputDevice == 0) + { + for (i = numInputChans; --i >= 0;) + { + const CallbackDetailsForChannel& info = inputChannelInfo[i]; + float* dest = tempInputBuffers [i]; + const float* src = ((const float*) inInputData->mBuffers[info.streamNum].mData) + + info.dataOffsetSamples; + const int stride = info.dataStrideSamples; + + if (stride != 0) // if this is zero, info is invalid + { + for (int j = bufferSize; --j >= 0;) + { + *dest++ = *src; + src += stride; + } + } + } + } + + if (! isSlaveDevice) + { + if (inputDevice == 0) + { + callback->audioDeviceIOCallback ((const float**) tempInputBuffers, + numInputChans, + tempOutputBuffers, + numOutputChans, + bufferSize); + } + else + { + jassert (inputDevice->bufferSize == bufferSize); + + // Sometimes the two linked devices seem to get their callbacks in + // parallel, so we need to lock both devices to stop the input data being + // changed while inside our callback.. + const ScopedLock sl (inputDevice->callbackLock); + + callback->audioDeviceIOCallback ((const float**) inputDevice->tempInputBuffers, + inputDevice->numInputChans, + tempOutputBuffers, + numOutputChans, + bufferSize); + } + + for (i = numOutputChans; --i >= 0;) + { + const CallbackDetailsForChannel& info = outputChannelInfo[i]; + const float* src = tempOutputBuffers [i]; + float* dest = ((float*) outOutputData->mBuffers[info.streamNum].mData) + + info.dataOffsetSamples; + const int stride = info.dataStrideSamples; + + if (stride != 0) // if this is zero, info is invalid + { + for (int j = bufferSize; --j >= 0;) + { + *dest = *src++; + dest += stride; + } + } + } + } + } + else + { + for (i = jmin (numOutputChans, numOutputChannelInfos); --i >= 0;) + { + const CallbackDetailsForChannel& info = outputChannelInfo[i]; + float* dest = ((float*) outOutputData->mBuffers[info.streamNum].mData) + + info.dataOffsetSamples; + const int stride = info.dataStrideSamples; + + if (stride != 0) // if this is zero, info is invalid + { + for (int j = bufferSize; --j >= 0;) + { + *dest = 0.0f; + dest += stride; + } + } + } + } + } + + // called by callbacks + void deviceDetailsChanged() + { + if (callbacksAllowed) + startTimer (100); + } + + void timerCallback() + { + stopTimer(); + log ("CoreAudio device changed callback"); + + const double oldSampleRate = sampleRate; + const int oldBufferSize = bufferSize; + updateDetailsFromDevice(); + + if (oldBufferSize != bufferSize || oldSampleRate != sampleRate) + { + callbacksAllowed = false; + stop (false); + updateDetailsFromDevice(); + callbacksAllowed = true; + } + } + + CoreAudioInternal* getRelatedDevice() const + { + UInt32 size = 0; + CoreAudioInternal* result = 0; + + AudioObjectPropertyAddress pa; + pa.mSelector = kAudioDevicePropertyRelatedDevices; + pa.mScope = kAudioObjectPropertyScopeWildcard; + pa.mElement = kAudioObjectPropertyElementMaster; + + if (deviceID != 0 + && AudioObjectGetPropertyDataSize (deviceID, &pa, 0, 0, &size) == noErr + && size > 0) + { + AudioDeviceID* devs = (AudioDeviceID*) juce_calloc (size); + + if (OK (AudioObjectGetPropertyData (deviceID, &pa, 0, 0, &size, devs))) + { + for (unsigned int i = 0; i < size / sizeof (AudioDeviceID); ++i) + { + if (devs[i] != deviceID && devs[i] != 0) + { + result = new CoreAudioInternal (devs[i]); + + const bool thisIsInput = inChanNames.size() > 0 && outChanNames.size() == 0; + const bool otherIsInput = result->inChanNames.size() > 0 && result->outChanNames.size() == 0; + + if (thisIsInput != otherIsInput + || (inChanNames.size() + outChanNames.size() == 0) + || (result->inChanNames.size() + result->outChanNames.size()) == 0) + break; + + deleteAndZero (result); + } + } + } + + juce_free (devs); + } + + return result; + } + + //============================================================================== + juce_UseDebuggingNewOperator + + int inputLatency, outputLatency; + BitArray activeInputChans, activeOutputChans; + StringArray inChanNames, outChanNames; + Array sampleRates; + Array bufferSizes; + AudioIODeviceCallback* callback; +#if ! MACOS_10_4_OR_EARLIER + AudioDeviceIOProcID audioProcID; +#endif + + CoreAudioInternal* inputDevice; + bool isSlaveDevice; + +private: + CriticalSection callbackLock; + AudioDeviceID deviceID; + bool started; + double sampleRate; + int bufferSize; + float* audioBuffer; + int numInputChans, numOutputChans; + bool callbacksAllowed; + + struct CallbackDetailsForChannel + { + int streamNum; + int dataOffsetSamples; + int dataStrideSamples; + }; + + int numInputChannelInfos, numOutputChannelInfos; + CallbackDetailsForChannel* inputChannelInfo; + CallbackDetailsForChannel* outputChannelInfo; + float** tempInputBuffers; + float** tempOutputBuffers; + + CoreAudioInternal (const CoreAudioInternal&); + const CoreAudioInternal& operator= (const CoreAudioInternal&); + + //============================================================================== + static OSStatus audioIOProc (AudioDeviceID inDevice, + const AudioTimeStamp* inNow, + const AudioBufferList* inInputData, + const AudioTimeStamp* inInputTime, + AudioBufferList* outOutputData, + const AudioTimeStamp* inOutputTime, + void* device) + { + ((CoreAudioInternal*) device)->audioCallback (inInputData, outOutputData); + return noErr; + } + + static OSStatus deviceListenerProc (AudioDeviceID /*inDevice*/, UInt32 /*inLine*/, const AudioObjectPropertyAddress* pa, void* inClientData) + { + CoreAudioInternal* const intern = (CoreAudioInternal*) inClientData; + + switch (pa->mSelector) + { + case kAudioDevicePropertyBufferSize: + case kAudioDevicePropertyBufferFrameSize: + case kAudioDevicePropertyNominalSampleRate: + case kAudioDevicePropertyStreamFormat: + case kAudioDevicePropertyDeviceIsAlive: + intern->deviceDetailsChanged(); + break; + + case kAudioDevicePropertyBufferSizeRange: + case kAudioDevicePropertyVolumeScalar: + case kAudioDevicePropertyMute: + case kAudioDevicePropertyPlayThru: + case kAudioDevicePropertyDataSource: + case kAudioDevicePropertyDeviceIsRunning: + break; + } + + return noErr; + } + + //============================================================================== + static OSType* getAllDataSourcesForDevice (AudioDeviceID deviceID, const bool input, int& num) + { + OSType* types = 0; + UInt32 size = 0; + num = 0; + + AudioObjectPropertyAddress pa; + pa.mSelector = kAudioDevicePropertyDataSources; + pa.mScope = kAudioObjectPropertyScopeWildcard; + pa.mElement = kAudioObjectPropertyElementMaster; + + if (deviceID != 0 + && OK (AudioObjectGetPropertyDataSize (deviceID, &pa, 0, 0, &size))) + { + types = (OSType*) juce_calloc (size); + + if (OK (AudioObjectGetPropertyData (deviceID, &pa, 0, 0, &size, types))) + { + num = size / sizeof (OSType); + } + else + { + juce_free (types); + types = 0; + } + } + + return types; + } +}; + + +//============================================================================== +class CoreAudioIODevice : public AudioIODevice +{ +public: + CoreAudioIODevice (const String& deviceName, + AudioDeviceID inputDeviceId, + const int inputIndex_, + AudioDeviceID outputDeviceId, + const int outputIndex_) + : AudioIODevice (deviceName, "CoreAudio"), + inputIndex (inputIndex_), + outputIndex (outputIndex_), + isOpen_ (false), + isStarted (false) + { + internal = 0; + CoreAudioInternal* device = 0; + + if (outputDeviceId == 0 || outputDeviceId == inputDeviceId) + { + jassert (inputDeviceId != 0); + + device = new CoreAudioInternal (inputDeviceId); + } + else + { + device = new CoreAudioInternal (outputDeviceId); + + if (inputDeviceId != 0) + { + CoreAudioInternal* secondDevice = new CoreAudioInternal (inputDeviceId); + + device->inputDevice = secondDevice; + secondDevice->isSlaveDevice = true; + } + } + + internal = device; + + AudioObjectPropertyAddress pa; + pa.mSelector = kAudioObjectPropertySelectorWildcard; + pa.mScope = kAudioObjectPropertyScopeWildcard; + pa.mElement = kAudioObjectPropertyElementWildcard; + + AudioObjectAddPropertyListener (kAudioObjectSystemObject, &pa, hardwareListenerProc, internal); + } + + ~CoreAudioIODevice() + { + AudioObjectPropertyAddress pa; + pa.mSelector = kAudioObjectPropertySelectorWildcard; + pa.mScope = kAudioObjectPropertyScopeWildcard; + pa.mElement = kAudioObjectPropertyElementWildcard; + + AudioObjectRemovePropertyListener (kAudioObjectSystemObject, &pa, hardwareListenerProc, internal); + + delete internal; + } + + const StringArray getOutputChannelNames() + { + return internal->outChanNames; + } + + const StringArray getInputChannelNames() + { + if (internal->inputDevice != 0) + return internal->inputDevice->inChanNames; + else + return internal->inChanNames; + } + + int getNumSampleRates() + { + return internal->sampleRates.size(); + } + + double getSampleRate (int index) + { + return internal->sampleRates [index]; + } + + int getNumBufferSizesAvailable() + { + return internal->bufferSizes.size(); + } + + int getBufferSizeSamples (int index) + { + return internal->bufferSizes [index]; + } + + int getDefaultBufferSize() + { + for (int i = 0; i < getNumBufferSizesAvailable(); ++i) + if (getBufferSizeSamples(i) >= 512) + return getBufferSizeSamples(i); + + return 512; + } + + const String open (const BitArray& inputChannels, + const BitArray& outputChannels, + double sampleRate, + int bufferSizeSamples) + { + isOpen_ = true; + + if (bufferSizeSamples <= 0) + bufferSizeSamples = getDefaultBufferSize(); + + lastError = internal->reopen (inputChannels, outputChannels, sampleRate, bufferSizeSamples); + isOpen_ = lastError.isEmpty(); + return lastError; + } + + void close() + { + isOpen_ = false; + internal->stop (false); + } + + bool isOpen() + { + return isOpen_; + } + + int getCurrentBufferSizeSamples() + { + return internal != 0 ? internal->getBufferSize() : 512; + } + + double getCurrentSampleRate() + { + return internal != 0 ? internal->getSampleRate() : 0; + } + + int getCurrentBitDepth() + { + return 32; // no way to find out, so just assume it's high.. + } + + const BitArray getActiveOutputChannels() const + { + return internal != 0 ? internal->activeOutputChans : BitArray(); + } + + const BitArray getActiveInputChannels() const + { + BitArray chans; + + if (internal != 0) + { + chans = internal->activeInputChans; + + if (internal->inputDevice != 0) + chans.orWith (internal->inputDevice->activeInputChans); + } + + return chans; + } + + int getOutputLatencyInSamples() + { + if (internal == 0) + return 0; + + // this seems like a good guess at getting the latency right - comparing + // this with a round-trip measurement, it gets it to within a few millisecs + // for the built-in mac soundcard + return internal->outputLatency + internal->getBufferSize() * 2; + } + + int getInputLatencyInSamples() + { + if (internal == 0) + return 0; + + return internal->inputLatency + internal->getBufferSize() * 2; + } + + void start (AudioIODeviceCallback* callback) + { + if (internal != 0 && ! isStarted) + { + if (callback != 0) + callback->audioDeviceAboutToStart (this); + + isStarted = true; + internal->start (callback); + } + } + + void stop() + { + if (isStarted && internal != 0) + { + AudioIODeviceCallback* const lastCallback = internal->callback; + + isStarted = false; + internal->stop (true); + + if (lastCallback != 0) + lastCallback->audioDeviceStopped(); + } + } + + bool isPlaying() + { + if (internal->callback == 0) + isStarted = false; + + return isStarted; + } + + const String getLastError() + { + return lastError; + } + + int inputIndex, outputIndex; + + juce_UseDebuggingNewOperator + +private: + CoreAudioInternal* internal; + bool isOpen_, isStarted; + String lastError; + + static OSStatus hardwareListenerProc (AudioDeviceID /*inDevice*/, UInt32 /*inLine*/, const AudioObjectPropertyAddress* pa, void* inClientData) + { + CoreAudioInternal* const intern = (CoreAudioInternal*) inClientData; + + switch (pa->mSelector) + { + case kAudioHardwarePropertyDevices: + intern->deviceDetailsChanged(); + break; + + case kAudioHardwarePropertyDefaultOutputDevice: + case kAudioHardwarePropertyDefaultInputDevice: + case kAudioHardwarePropertyDefaultSystemOutputDevice: + break; + } + + return noErr; + } + + CoreAudioIODevice (const CoreAudioIODevice&); + const CoreAudioIODevice& operator= (const CoreAudioIODevice&); +}; + +//============================================================================== +class CoreAudioIODeviceType : public AudioIODeviceType +{ +public: + //============================================================================== + CoreAudioIODeviceType() + : AudioIODeviceType (T("CoreAudio")), + hasScanned (false) + { + } + + ~CoreAudioIODeviceType() + { + } + + //============================================================================== + void scanForDevices() + { + hasScanned = true; + + inputDeviceNames.clear(); + outputDeviceNames.clear(); + inputIds.clear(); + outputIds.clear(); + + UInt32 size; + + AudioObjectPropertyAddress pa; + pa.mSelector = kAudioHardwarePropertyDevices; + pa.mScope = kAudioObjectPropertyScopeWildcard; + pa.mElement = kAudioObjectPropertyElementMaster; + + if (OK (AudioObjectGetPropertyDataSize (kAudioObjectSystemObject, &pa, 0, 0, &size))) + { + AudioDeviceID* const devs = (AudioDeviceID*) juce_calloc (size); + + if (OK (AudioObjectGetPropertyData (kAudioObjectSystemObject, &pa, 0, 0, &size, devs))) + { + static bool alreadyLogged = false; + const int num = size / sizeof (AudioDeviceID); + for (int i = 0; i < num; ++i) + { + char name [1024]; + size = sizeof (name); + pa.mSelector = kAudioDevicePropertyDeviceName; + + if (OK (AudioObjectGetPropertyData (devs[i], &pa, 0, 0, &size, name))) + { + const String nameString (String::fromUTF8 ((const uint8*) name, strlen (name))); + + if (! alreadyLogged) + log (T("CoreAudio device: ") + nameString); + + const int numIns = getNumChannels (devs[i], true); + const int numOuts = getNumChannels (devs[i], false); + + if (numIns > 0) + { + inputDeviceNames.add (nameString); + inputIds.add (devs[i]); + } + + if (numOuts > 0) + { + outputDeviceNames.add (nameString); + outputIds.add (devs[i]); + } + } + } + + alreadyLogged = true; + } + + juce_free (devs); + } + + inputDeviceNames.appendNumbersToDuplicates (false, true); + outputDeviceNames.appendNumbersToDuplicates (false, true); + } + + const StringArray getDeviceNames (const bool wantInputNames) const + { + jassert (hasScanned); // need to call scanForDevices() before doing this + + if (wantInputNames) + return inputDeviceNames; + else + return outputDeviceNames; + } + + int getDefaultDeviceIndex (const bool forInput) const + { + jassert (hasScanned); // need to call scanForDevices() before doing this + + AudioDeviceID deviceID; + UInt32 size = sizeof (deviceID); + + // if they're asking for any input channels at all, use the default input, so we + // get the built-in mic rather than the built-in output with no inputs.. + + AudioObjectPropertyAddress pa; + pa.mSelector = forInput ? kAudioHardwarePropertyDefaultInputDevice : kAudioHardwarePropertyDefaultOutputDevice; + pa.mScope = kAudioObjectPropertyScopeWildcard; + pa.mElement = kAudioObjectPropertyElementMaster; + + if (AudioObjectGetPropertyData (kAudioObjectSystemObject, &pa, 0, 0, &size, &deviceID) == noErr) + { + if (forInput) + { + for (int i = inputIds.size(); --i >= 0;) + if (inputIds[i] == deviceID) + return i; + } + else + { + for (int i = outputIds.size(); --i >= 0;) + if (outputIds[i] == deviceID) + return i; + } + } + + return 0; + } + + int getIndexOfDevice (AudioIODevice* device, const bool asInput) const + { + jassert (hasScanned); // need to call scanForDevices() before doing this + + CoreAudioIODevice* const d = dynamic_cast (device); + if (d == 0) + return -1; + + return asInput ? d->inputIndex + : d->outputIndex; + } + + bool hasSeparateInputsAndOutputs() const { return true; } + + AudioIODevice* createDevice (const String& outputDeviceName, + const String& inputDeviceName) + { + jassert (hasScanned); // need to call scanForDevices() before doing this + + const int inputIndex = inputDeviceNames.indexOf (inputDeviceName); + const int outputIndex = outputDeviceNames.indexOf (outputDeviceName); + + String deviceName (outputDeviceName); + if (deviceName.isEmpty()) + deviceName = inputDeviceName; + + if (index >= 0) + return new CoreAudioIODevice (deviceName, + inputIds [inputIndex], + inputIndex, + outputIds [outputIndex], + outputIndex); + + return 0; + } + + //============================================================================== + juce_UseDebuggingNewOperator + +private: + StringArray inputDeviceNames, outputDeviceNames; + Array inputIds, outputIds; + + bool hasScanned; + + static int getNumChannels (AudioDeviceID deviceID, bool input) + { + int total = 0; + UInt32 size; + + AudioObjectPropertyAddress pa; + pa.mSelector = kAudioDevicePropertyStreamConfiguration; + pa.mScope = input ? kAudioDevicePropertyScopeInput : kAudioDevicePropertyScopeOutput; + pa.mElement = kAudioObjectPropertyElementMaster; + + if (OK (AudioObjectGetPropertyDataSize (deviceID, &pa, 0, 0, &size))) + { + AudioBufferList* const bufList = (AudioBufferList*) juce_calloc (size); + + if (OK (AudioObjectGetPropertyData (deviceID, &pa, 0, 0, &size, bufList))) + { + const int numStreams = bufList->mNumberBuffers; + + for (int i = 0; i < numStreams; ++i) + { + const AudioBuffer& b = bufList->mBuffers[i]; + total += b.mNumberChannels; + } + } + + juce_free (bufList); + } + + return total; + } + + CoreAudioIODeviceType (const CoreAudioIODeviceType&); + const CoreAudioIODeviceType& operator= (const CoreAudioIODeviceType&); +}; + +//============================================================================== +AudioIODeviceType* juce_createAudioIODeviceType_CoreAudio() +{ + return new CoreAudioIODeviceType(); +} + +#undef log + +#endif diff --git a/src/native/windows/juce_win32_SystemStats.cpp b/src/native/windows/juce_win32_SystemStats.cpp index 6e249aa97e..7c51f81cfc 100644 --- a/src/native/windows/juce_win32_SystemStats.cpp +++ b/src/native/windows/juce_win32_SystemStats.cpp @@ -307,7 +307,7 @@ bool SystemStats::isOperatingSystem64Bit() throw() int SystemStats::getMemorySizeInMegabytes() throw() { MEMORYSTATUSEX mem; - mem.dwLength = sizeof (mem); + mem.dwLength = sizeof (mem); GlobalMemoryStatusEx (&mem); return (int) (mem.ullTotalPhys / (1024 * 1024)) + 1; }