abort if configuration fails
[ardour.git] / libs / ardour / audio_unit.cc
index 0a15e922853905753a3f370916f38b13f088bfaf..bcc8c2c31044e70db8a2a16dc794303ad7855139 100644 (file)
@@ -25,6 +25,7 @@
 #include <math.h>
 #include <ctype.h>
 
+#include "pbd/gstdio_compat.h"
 #include "pbd/transmitter.h"
 #include "pbd/xml++.h"
 #include "pbd/convert.h"
@@ -35,7 +36,6 @@
 #include <glibmm/threads.h>
 #include <glibmm/fileutils.h>
 #include <glibmm/miscutils.h>
-#include <pbd/gstdio_compat.h>
 
 #include "ardour/ardour.h"
 #include "ardour/audioengine.h"
@@ -50,8 +50,8 @@
 #include "ardour/tempo.h"
 #include "ardour/utils.h"
 
-#include "appleutility/CAAudioUnit.h"
-#include "appleutility/CAAUParameter.h"
+#include "CAAudioUnit.h"
+#include "CAAUParameter.h"
 
 #include <CoreFoundation/CoreFoundation.h>
 #include <CoreServices/CoreServices.h>
 #include <Carbon/Carbon.h>
 #endif
 
+#ifdef COREAUDIO105
+#define ArdourComponent Component
+#define ArdourDescription ComponentDescription
+#define ArdourFindNext FindNextComponent
+#else
+#define ArdourComponent AudioComponent
+#define ArdourDescription AudioComponentDescription
+#define ArdourFindNext AudioComponentFindNext
+#endif
+
 #include "i18n.h"
 
 using namespace std;
@@ -339,8 +349,8 @@ get_preset_name_in_plist (CFPropertyListRef plist)
 //--------------------------------------------------------------------------
 // general implementation for ComponentDescriptionsMatch() and ComponentDescriptionsMatch_Loosely()
 // if inIgnoreType is true, then the type code is ignored in the ComponentDescriptions
-Boolean ComponentDescriptionsMatch_General(const ComponentDescription * inComponentDescription1, const ComponentDescription * inComponentDescription2, Boolean inIgnoreType);
-Boolean ComponentDescriptionsMatch_General(const ComponentDescription * inComponentDescription1, const ComponentDescription * inComponentDescription2, Boolean inIgnoreType)
+Boolean ComponentDescriptionsMatch_General(const ArdourDescription * inComponentDescription1, const ArdourDescription * inComponentDescription2, Boolean inIgnoreType);
+Boolean ComponentDescriptionsMatch_General(const ArdourDescription * inComponentDescription1, const ArdourDescription * inComponentDescription2, Boolean inIgnoreType)
 {
        if ( (inComponentDescription1 == NULL) || (inComponentDescription2 == NULL) )
                return FALSE;
@@ -362,17 +372,21 @@ Boolean ComponentDescriptionsMatch_General(const ComponentDescription * inCompon
 //--------------------------------------------------------------------------
 // general implementation for ComponentAndDescriptionMatch() and ComponentAndDescriptionMatch_Loosely()
 // if inIgnoreType is true, then the type code is ignored in the ComponentDescriptions
-Boolean ComponentAndDescriptionMatch_General(Component inComponent, const ComponentDescription * inComponentDescription, Boolean inIgnoreType);
-Boolean ComponentAndDescriptionMatch_General(Component inComponent, const ComponentDescription * inComponentDescription, Boolean inIgnoreType)
+Boolean ComponentAndDescriptionMatch_General(ArdourComponent inComponent, const ArdourDescription * inComponentDescription, Boolean inIgnoreType);
+Boolean ComponentAndDescriptionMatch_General(ArdourComponent inComponent, const ArdourDescription * inComponentDescription, Boolean inIgnoreType)
 {
        OSErr status;
-       ComponentDescription desc;
+       ArdourDescription desc;
 
        if ( (inComponent == NULL) || (inComponentDescription == NULL) )
                return FALSE;
 
        // get the ComponentDescription of the input Component
+#ifdef COREAUDIO105
        status = GetComponentInfo(inComponent, &desc, NULL, NULL, NULL);
+#else
+       status = AudioComponentGetDescription (inComponent, &desc);
+#endif
        if (status != noErr)
                return FALSE;
 
@@ -384,28 +398,28 @@ Boolean ComponentAndDescriptionMatch_General(Component inComponent, const Compon
 // determine if 2 ComponentDescriptions are basically equal
 // (by that, I mean that the important identifying values are compared,
 // but not the ComponentDescription flags)
-Boolean ComponentDescriptionsMatch(const ComponentDescription * inComponentDescription1, const ComponentDescription * inComponentDescription2)
+Boolean ComponentDescriptionsMatch(const ArdourDescription * inComponentDescription1, const ArdourDescription * inComponentDescription2)
 {
        return ComponentDescriptionsMatch_General(inComponentDescription1, inComponentDescription2, FALSE);
 }
 
 //--------------------------------------------------------------------------
 // determine if 2 ComponentDescriptions have matching sub-type and manufacturer codes
-Boolean ComponentDescriptionsMatch_Loose(const ComponentDescription * inComponentDescription1, const ComponentDescription * inComponentDescription2)
+Boolean ComponentDescriptionsMatch_Loose(const ArdourDescription * inComponentDescription1, const ArdourDescription * inComponentDescription2)
 {
        return ComponentDescriptionsMatch_General(inComponentDescription1, inComponentDescription2, TRUE);
 }
 
 //--------------------------------------------------------------------------
 // determine if a ComponentDescription basically matches that of a particular Component
-Boolean ComponentAndDescriptionMatch(Component inComponent, const ComponentDescription * inComponentDescription)
+Boolean ComponentAndDescriptionMatch(ArdourComponent inComponent, const ArdourDescription * inComponentDescription)
 {
        return ComponentAndDescriptionMatch_General(inComponent, inComponentDescription, FALSE);
 }
 
 //--------------------------------------------------------------------------
 // determine if a ComponentDescription matches only the sub-type and manufacturer codes of a particular Component
-Boolean ComponentAndDescriptionMatch_Loosely(Component inComponent, const ComponentDescription * inComponentDescription)
+Boolean ComponentAndDescriptionMatch_Loosely(ArdourComponent inComponent, const ArdourDescription * inComponentDescription)
 {
        return ComponentAndDescriptionMatch_General(inComponent, inComponentDescription, TRUE);
 }
@@ -471,7 +485,7 @@ AUPlugin::~AUPlugin ()
                AUListenerDispose (_parameter_listener);
                _parameter_listener = 0;
        }
-       
+
        if (unit) {
                DEBUG_TRACE (DEBUG::AudioUnits, "about to call uninitialize in plugin destructor\n");
                unit->Uninitialize ();
@@ -533,7 +547,11 @@ AUPlugin::init ()
        output_channels = -1;
        {
                CAComponentDescription temp;
+#ifdef COREAUDIO105
                GetComponentInfo (comp.get()->Comp(), &temp, NULL, NULL, NULL);
+#else
+               AudioComponentGetDescription (comp.get()->Comp(), &temp);
+#endif
                CFStringRef compTypeString = UTCreateStringForOSType(temp.componentType);
                CFStringRef compSubTypeString = UTCreateStringForOSType(temp.componentSubType);
                CFStringRef compManufacturerString = UTCreateStringForOSType(temp.componentManufacturer);
@@ -567,14 +585,14 @@ AUPlugin::init ()
        unit->GetElementCount (kAudioUnitScope_Output, output_elements);
 
        if (input_elements > 0) {
-               /* setup render callback: the plugin calls this to get input data 
+               /* setup render callback: the plugin calls this to get input data
                 */
-               
+
                AURenderCallbackStruct renderCallbackInfo;
-               
+
                renderCallbackInfo.inputProc = _render_callback;
                renderCallbackInfo.inputProcRefCon = this;
-               
+
                DEBUG_TRACE (DEBUG::AudioUnits, "set render callback in input scope\n");
                if ((err = unit->SetProperty (kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input,
                                              0, (void*) &renderCallbackInfo, sizeof(renderCallbackInfo))) != 0) {
@@ -641,9 +659,9 @@ AUPlugin::discover_parameters ()
                        const CAAUParameter* param = param_info.GetParamInfo (d.id);
                        const AudioUnitParameterInfo& info (param->ParamInfo());
 
-                       const int len = CFStringGetLength (param->GetName());;
+                       const int len = CFStringGetLength (param->GetName());
                        char local_buffer[len*2];
-                       Boolean good = CFStringGetCString(param->GetName(),local_buffer,len*2,kCFStringEncodingMacRoman);
+                       Boolean good = CFStringGetCString (param->GetName(), local_buffer ,len*2 , kCFStringEncodingUTF8);
                        if (!good) {
                                d.label = "???";
                        } else {
@@ -842,7 +860,7 @@ AUPlugin::maybe_fix_broken_au_id (const std::string& id)
 
        return s.str();
 
-  err:
+err:
        return string();
 }
 
@@ -906,7 +924,8 @@ AUPlugin::set_parameter (uint32_t which, float val)
        theEvent.mArgument.mParameter.mElement = d.element;
 
        DEBUG_TRACE (DEBUG::AudioUnits, "notify about parameter change\n");
-       AUEventListenerNotify (NULL, NULL, &theEvent);
+        /* Note the 1st argument, which means "Don't notify us about a change we made ourselves" */
+        AUEventListenerNotify (_parameter_listener, NULL, &theEvent);
 
        Plugin::set_parameter (which, val);
 }
@@ -1024,7 +1043,7 @@ AUPlugin::configure_io (ChanCount in, ChanCount out)
        if (initialized) {
                //if we are already running with the requested i/o config, bail out here
                if ( (audio_in==input_channels) && (audio_out==output_channels) ) {
-                       return 0;
+                       return true;
                } else {
                        deactivate ();
                }
@@ -1052,17 +1071,17 @@ AUPlugin::configure_io (ChanCount in, ChanCount out)
        streamFormat.mChannelsPerFrame = audio_in;
 
        if (set_input_format (streamFormat) != 0) {
-               return -1;
+               return false;
        }
 
        streamFormat.mChannelsPerFrame = audio_out;
 
        if (set_output_format (streamFormat) != 0) {
-               return -1;
+               return false;
        }
 
        /* reset plugin info to show currently configured state */
-       
+
        _info->n_inputs = in;
        _info->n_outputs = out;
 
@@ -1070,7 +1089,7 @@ AUPlugin::configure_io (ChanCount in, ChanCount out)
                activate ();
        }
 
-       return 0;
+       return true;
 }
 
 ChanCount
@@ -1129,7 +1148,7 @@ AUPlugin::can_support_io_configuration (const ChanCount& in, ChanCount& out)
 
        vector<pair<int,int> >& io_configs = pinfo->cache.io_configs;
 
-       DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("%1 has %2 IO configurations, looking for %3 in, %4 out\n", 
+       DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("%1 has %2 IO configurations, looking for %3 in, %4 out\n",
                                                        name(), io_configs.size(), in, out));
 
        //Ardour expects the plugin to tell it the output
@@ -1175,7 +1194,7 @@ AUPlugin::can_support_io_configuration (const ChanCount& in, ChanCount& out)
                int32_t possible_out = i->second;
 
                if ((possible_in == audio_in) && (possible_out == audio_out)) {
-                       DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("\tCHOSEN: %1 in %2 out to match in %3 out %4\n", 
+                       DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("\tCHOSEN: %1 in %2 out to match in %3 out %4\n",
                                                                        possible_in, possible_out,
                                                                        in, out));
 
@@ -1213,8 +1232,8 @@ AUPlugin::can_support_io_configuration (const ChanCount& in, ChanCount& out)
                                audio_out = 2;
                                found = true;
                        } else if (possible_out == -2) {
-                               /* plugins shouldn't really use (0,-2) but might. 
-                                  any configuration possible, provide stereo output 
+                               /* plugins shouldn't really use (0,-2) but might.
+                                  any configuration possible, provide stereo output
                                */
                                audio_out = 2;
                                found = true;
@@ -1263,7 +1282,7 @@ AUPlugin::can_support_io_configuration (const ChanCount& in, ChanCount& out)
                                audio_out = audio_in;
                                found = true;
                        } else if (possible_out == -2) {
-                               /* plugins shouldn't really use (-2,-2) but might. 
+                               /* plugins shouldn't really use (-2,-2) but might.
                                   interpret as (-1,-1).
                                */
                                audio_out = audio_in;
@@ -1293,8 +1312,8 @@ AUPlugin::can_support_io_configuration (const ChanCount& in, ChanCount& out)
                                audio_out = 2;
                                found = true;
                        } else if (possible_out == -2) {
-                               /* plugins shouldn't really use (<-2,-2) but might. 
-                                  interpret as (<-2,-1): any configuration possible, provide stereo output 
+                               /* plugins shouldn't really use (<-2,-2) but might.
+                                  interpret as (<-2,-1): any configuration possible, provide stereo output
                                */
                                audio_out = 2;
                                found = true;
@@ -1322,8 +1341,8 @@ AUPlugin::can_support_io_configuration (const ChanCount& in, ChanCount& out)
                                audio_out = 2;
                                found = true;
                        } else if (possible_out == -2) {
-                               /* plugins shouldn't really use (>0,-2) but might. 
-                                  interpret as (>0,-1): 
+                               /* plugins shouldn't really use (>0,-2) but might.
+                                  interpret as (>0,-1):
                                   any output configuration possible, provide stereo output
                                */
                                audio_out = 2;
@@ -1421,7 +1440,7 @@ AUPlugin::render_callback(AudioUnitRenderActionFlags*,
        /* not much to do with audio - the data is already in the buffers given to us in connect_and_run() */
 
        // DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("%1: render callback, frames %2 bufs %3\n",
-        // name(), inNumberFrames, ioData->mNumberBuffers));
+       // name(), inNumberFrames, ioData->mNumberBuffers));
 
        if (input_maxbuf == 0) {
                error << _("AUPlugin: render callback called illegally!") << endmsg;
@@ -1434,9 +1453,9 @@ AUPlugin::render_callback(AudioUnitRenderActionFlags*,
                ioData->mBuffers[i].mDataByteSize = sizeof (Sample) * inNumberFrames;
 
                /* we don't use the channel mapping because audiounits are
-                  never replicated. one plugin instance uses all channels/buffers
-                  passed to PluginInsert::connect_and_run()
-               */
+                * never replicated. one plugin instance uses all channels/buffers
+                * passed to PluginInsert::connect_and_run()
+                */
 
                ioData->mBuffers[i].mData = input_buffers->get_audio (i).data (cb_offset + input_offset);
        }
@@ -1461,12 +1480,12 @@ AUPlugin::connect_and_run (BufferSet& bufs, ChanMapping in_map, ChanMapping out_
        }
 
        DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("%1 in %2 out %3 MIDI %4 bufs %5 (available %6)\n",
-                                                       name(), input_channels, output_channels, _has_midi_input,
-                                                       bufs.count(), bufs.available()));
+                               name(), input_channels, output_channels, _has_midi_input,
+                               bufs.count(), bufs.available()));
 
        /* the apparent number of buffers matches our input configuration, but we know that the bufferset
           has the capacity to handle our outputs.
-       */
+          */
 
        assert (bufs.available() >= ChanCount (DataType::AUDIO, output_channels));
 
@@ -1481,12 +1500,12 @@ AUPlugin::connect_and_run (BufferSet& bufs, ChanMapping in_map, ChanMapping out_
                buffers->mBuffers[i].mNumberChannels = 1;
                buffers->mBuffers[i].mDataByteSize = nframes * sizeof (Sample);
                /* setting this to 0 indicates to the AU that it can provide buffers here
-                  if necessary. if it can process in-place, it will use the buffers provided
-                  as input by ::render_callback() above. 
-                   
-                   a non-null values tells the plugin to render into the buffer pointed
-                   at by the value.
-               */
+                * if necessary. if it can process in-place, it will use the buffers provided
+                * as input by ::render_callback() above.
+                *
+                * a non-null values tells the plugin to render into the buffer pointed
+                * at by the value.
+                */
                buffers->mBuffers[i].mData = 0;
        }
 
@@ -1495,14 +1514,14 @@ AUPlugin::connect_and_run (BufferSet& bufs, ChanMapping in_map, ChanMapping out_
                uint32_t nmidi = bufs.count().n_midi();
 
                for (uint32_t i = 0; i < nmidi; ++i) {
-                       
+
                        /* one MIDI port/buffer only */
-                       
+
                        MidiBuffer& m = bufs.get_midi (i);
-                       
+
                        for (MidiBuffer::iterator i = m.begin(); i != m.end(); ++i) {
                                Evoral::MIDIEvent<framepos_t> ev (*i);
-                               
+
                                if (ev.is_channel_event()) {
                                        const uint8_t* b = ev.buffer();
                                        DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("%1: MIDI event %2\n", name(), ev));
@@ -1514,14 +1533,14 @@ AUPlugin::connect_and_run (BufferSet& bufs, ChanMapping in_map, ChanMapping out_
                }
        }
 
-       /* does this really mean anything ? 
-        */
+       /* does this really mean anything ?
+       */
 
        ts.mSampleTime = frames_processed;
        ts.mFlags = kAudioTimeStampSampleTimeValid;
 
        DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("%1 render flags=%2 time=%3 nframes=%4 buffers=%5\n",
-                                                        name(), flags, frames_processed, nframes, buffers->mNumberBuffers));
+                               name(), flags, frames_processed, nframes, buffers->mNumberBuffers));
 
        if ((err = unit->Render (&flags, &ts, 0, nframes, buffers)) == noErr) {
 
@@ -1529,7 +1548,7 @@ AUPlugin::connect_and_run (BufferSet& bufs, ChanMapping in_map, ChanMapping out_
                frames_processed += nframes;
 
                DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("%1 rendered %2 buffers of %3\n",
-                                                               name(), buffers->mNumberBuffers, output_channels));
+                                       name(), buffers->mNumberBuffers, output_channels));
 
                int32_t limit = min ((int32_t) buffers->mNumberBuffers, output_channels);
                int32_t i;
@@ -1538,14 +1557,14 @@ AUPlugin::connect_and_run (BufferSet& bufs, ChanMapping in_map, ChanMapping out_
                        Sample* expected_buffer_address= bufs.get_audio (i).data (offset);
                        if (expected_buffer_address != buffers->mBuffers[i].mData) {
                                /* plugin provided its own buffer for output so copy it back to where we want it
-                                */
+                               */
                                memcpy (expected_buffer_address, buffers->mBuffers[i].mData, nframes * sizeof (Sample));
                        }
                }
 
                /* now silence any buffers that were passed in but the that the plugin
-                  did not fill/touch/use.
-               */
+                * did not fill/touch/use.
+                */
 
                for (;i < output_channels; ++i) {
                        memset (bufs.get_audio (i).data (offset), 0, nframes * sizeof (Sample));
@@ -1623,7 +1642,7 @@ AUPlugin::get_musical_time_location_callback (UInt32*   outDeltaSampleOffsetToNe
                        /* on the beat */
                        *outDeltaSampleOffsetToNextBeat = 0;
                } else {
-                       *outDeltaSampleOffsetToNextBeat = (UInt32) 
+                       *outDeltaSampleOffsetToNextBeat = (UInt32)
                                floor (((Timecode::BBT_Time::ticks_per_beat - bbt.ticks)/Timecode::BBT_Time::ticks_per_beat) * // fraction of a beat to next beat
                                       metric.tempo().frames_per_beat (_session.frame_rate())); // frames per beat
                }
@@ -1957,7 +1976,7 @@ AUPlugin::load_preset (PresetRecord r)
 }
 
 void
-AUPlugin::do_remove_preset (std::string) 
+AUPlugin::do_remove_preset (std::string)
 {
 }
 
@@ -2048,10 +2067,10 @@ GetDictionarySInt32Value(CFDictionaryRef inAUStateDictionary, CFStringRef inDict
 }
 
 static OSStatus
-GetAUComponentDescriptionFromStateData(CFPropertyListRef inAUStateData, ComponentDescription * outComponentDescription)
+GetAUComponentDescriptionFromStateData(CFPropertyListRef inAUStateData, ArdourDescription * outComponentDescription)
 {
        CFDictionaryRef auStateDictionary;
-       ComponentDescription tempDesc = {0,0,0,0,0};
+       ArdourDescription tempDesc = {0,0,0,0,0};
        SInt32 versionValue;
        Boolean gotValue;
 
@@ -2105,16 +2124,16 @@ static bool au_preset_filter (const string& str, void* arg)
                   include "<manufacturer>/<plugin-name>" in their path.
                */
 
-               Plugin* p = (Plugin *) arg;
-               string match = p->maker();
+               AUPluginInfo* p = (AUPluginInfo *) arg;
+               string match = p->creator;
                match += '/';
-               match += p->name();
+               match += p->name;
 
                ret = str.find (match) != string::npos;
 
                if (ret == false) {
-                       string m = p->maker ();
-                       string n = p->name ();
+                       string m = p->creator;
+                       string n = p->name;
                        strip_whitespace_edges (m);
                        strip_whitespace_edges (n);
                        match = m;
@@ -2128,12 +2147,12 @@ static bool au_preset_filter (const string& str, void* arg)
        return ret;
 }
 
-bool
-check_and_get_preset_name (Component component, const string& pathstr, string& preset_name)
+static bool
+check_and_get_preset_name (ArdourComponent component, const string& pathstr, string& preset_name)
 {
        OSStatus status;
        CFPropertyListRef plist;
-       ComponentDescription presetDesc;
+       ArdourDescription presetDesc;
        bool ret = false;
 
        plist = load_property_list (pathstr);
@@ -2176,6 +2195,73 @@ check_and_get_preset_name (Component component, const string& pathstr, string& p
        return true;
 }
 
+
+static void
+#ifdef COREAUDIO105
+get_names (CAComponentDescription& comp_desc, std::string& name, std::string& maker)
+#else
+get_names (ArdourComponent& comp, std::string& name, std::string& maker)
+#endif
+{
+       CFStringRef itemName = NULL;
+       // Marc Poirier-style item name
+#ifdef COREAUDIO105
+       CAComponent auComponent (comp_desc);
+       if (auComponent.IsValid()) {
+               CAComponentDescription dummydesc;
+               Handle nameHandle = NewHandle(sizeof(void*));
+               if (nameHandle != NULL) {
+                       OSErr err = GetComponentInfo(auComponent.Comp(), &dummydesc, nameHandle, NULL, NULL);
+                       if (err == noErr) {
+                               ConstStr255Param nameString = (ConstStr255Param) (*nameHandle);
+                               if (nameString != NULL) {
+                                       itemName = CFStringCreateWithPascalString(kCFAllocatorDefault, nameString, CFStringGetSystemEncoding());
+                               }
+                       }
+                       DisposeHandle(nameHandle);
+               }
+       }
+#else
+       assert (comp);
+       AudioComponentCopyName (comp, &itemName);
+#endif
+
+       // if Marc-style fails, do the original way
+       if (itemName == NULL) {
+#ifndef COREAUDIO105
+               CAComponentDescription comp_desc;
+               AudioComponentGetDescription (comp, &comp_desc);
+#endif
+               CFStringRef compTypeString = UTCreateStringForOSType(comp_desc.componentType);
+               CFStringRef compSubTypeString = UTCreateStringForOSType(comp_desc.componentSubType);
+               CFStringRef compManufacturerString = UTCreateStringForOSType(comp_desc.componentManufacturer);
+
+               itemName = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%@ - %@ - %@"),
+                               compTypeString, compManufacturerString, compSubTypeString);
+
+               if (compTypeString != NULL)
+                       CFRelease(compTypeString);
+               if (compSubTypeString != NULL)
+                       CFRelease(compSubTypeString);
+               if (compManufacturerString != NULL)
+                       CFRelease(compManufacturerString);
+       }
+
+       string str = CFStringRefToStdString(itemName);
+       string::size_type colon = str.find (':');
+
+       if (colon) {
+               name = str.substr (colon+1);
+               maker = str.substr (0, colon);
+               strip_whitespace_edges (maker);
+               strip_whitespace_edges (name);
+       } else {
+               name = str;
+               maker = "unknown";
+               strip_whitespace_edges (name);
+       }
+}
+
 std::string
 AUPlugin::current_preset() const
 {
@@ -2199,11 +2285,13 @@ AUPlugin::find_presets ()
 
        user_preset_map.clear ();
 
-       find_files_matching_filter (preset_files, preset_search_path, au_preset_filter, this, true, true, true);
+       PluginInfoPtr nfo = get_info();
+       find_files_matching_filter (preset_files, preset_search_path, au_preset_filter,
+                       boost::dynamic_pointer_cast<AUPluginInfo> (nfo).get(),
+                       true, true, true);
 
        if (preset_files.empty()) {
                DEBUG_TRACE (DEBUG::AudioUnits, "AU No Preset Files found for given plugin.\n");
-               return;
        }
 
        for (vector<string>::iterator x = preset_files.begin(); x != preset_files.end(); ++x) {
@@ -2242,7 +2330,7 @@ AUPlugin::find_presets ()
        for (FactoryPresetMap::iterator i = factory_preset_map.begin(); i != factory_preset_map.end(); ++i) {
                /* XXX: dubious */
                string const uri = string_compose ("%1", _presets.size ());
-               _presets.insert (make_pair (uri, Plugin::PresetRecord (uri, i->first, i->second)));
+               _presets.insert (make_pair (uri, Plugin::PresetRecord (uri, i->first, false)));
                DEBUG_TRACE (DEBUG::AudioUnits, string_compose("AU Adding Factory Preset: %1 > %2\n", i->first, i->second));
        }
 }
@@ -2295,10 +2383,90 @@ AUPluginInfo::load (Session& session)
        }
 }
 
+std::vector<Plugin::PresetRecord>
+AUPluginInfo::get_presets (bool user_only) const
+{
+       std::vector<Plugin::PresetRecord> p;
+       boost::shared_ptr<CAComponent> comp;
+#ifndef NO_PLUGIN_STATE
+       try {
+               comp = boost::shared_ptr<CAComponent>(new CAComponent(*descriptor));
+               if (!comp->IsValid()) {
+                       throw failed_constructor();
+               }
+       } catch (failed_constructor& err) {
+               return p;
+       }
+
+       // user presets
+
+       if (!preset_search_path_initialized) {
+               Glib::ustring p = Glib::get_home_dir();
+               p += "/Library/Audio/Presets:";
+               p += preset_search_path;
+               preset_search_path = p;
+               preset_search_path_initialized = true;
+               DEBUG_TRACE (DEBUG::AudioUnits, string_compose("AU Preset Path: %1\n", preset_search_path));
+       }
+
+       vector<string> preset_files;
+       find_files_matching_filter (preset_files, preset_search_path, au_preset_filter, const_cast<AUPluginInfo*>(this), true, true, true);
+
+       for (vector<string>::iterator x = preset_files.begin(); x != preset_files.end(); ++x) {
+               string path = *x;
+               string preset_name;
+               preset_name = Glib::path_get_basename (path);
+               preset_name = preset_name.substr (0, preset_name.find_last_of ('.'));
+               if (check_and_get_preset_name (comp.get()->Comp(), path, preset_name)) {
+                       p.push_back (Plugin::PresetRecord (path, preset_name));
+               }
+       }
+
+       if (user_only) {
+               return p;
+       }
+
+       // factory presets
+
+       CFArrayRef presets;
+       UInt32 dataSize;
+       Boolean isWritable;
+
+       boost::shared_ptr<CAAudioUnit> unit (new CAAudioUnit);
+       if (noErr != CAAudioUnit::Open (*(comp.get()), *unit)) {
+               return p;
+       }
+       if (noErr != unit->GetPropertyInfo (kAudioUnitProperty_FactoryPresets, kAudioUnitScope_Global, 0, &dataSize, &isWritable)) {
+               unit->Uninitialize ();
+               return p;
+       }
+       if (noErr != unit->GetProperty (kAudioUnitProperty_FactoryPresets, kAudioUnitScope_Global, 0, (void*) &presets, &dataSize)) {
+               unit->Uninitialize ();
+               return p;
+       }
+       if (!presets) {
+               unit->Uninitialize ();
+               return p;
+       }
+
+       CFIndex cnt = CFArrayGetCount (presets);
+       for (CFIndex i = 0; i < cnt; ++i) {
+               AUPreset* preset = (AUPreset*) CFArrayGetValueAtIndex (presets, i);
+               string const uri = string_compose ("%1", i);
+               string name = CFStringRefToStdString (preset->presetName);
+               p.push_back (Plugin::PresetRecord (uri, name, false));
+       }
+       CFRelease (presets);
+       unit->Uninitialize ();
+
+#endif // NO_PLUGIN_STATE
+       return p;
+}
+
 Glib::ustring
 AUPluginInfo::au_cache_path ()
 {
-       return Glib::build_filename (ARDOUR::user_config_directory(), "au_cache");
+       return Glib::build_filename (ARDOUR::user_cache_directory(), "au_cache");
 }
 
 PluginInfoList*
@@ -2309,7 +2477,7 @@ AUPluginInfo::discover (bool scan_only)
        /* AU require a CAComponentDescription pointer provided by the OS.
         * Ardour only caches port and i/o config. It can't just 'scan' without
         * 'discovering' (like we do for VST).
-        * 
+        *
         * "Scan Only" means
         * "Iterate over all plugins. skip the ones where there's no io-cache".
         */
@@ -2441,14 +2609,18 @@ AUPluginInfo::au_crashlog (std::string msg)
 void
 AUPluginInfo::discover_by_description (PluginInfoList& plugs, CAComponentDescription& desc)
 {
-       Component comp = 0;
+       ArdourComponent comp = 0;
        au_crashlog(string_compose("Start AU discovery for Type: %1", (int)desc.componentType));
 
-       comp = FindNextComponent (NULL, &desc);
+       comp = ArdourFindNext (NULL, &desc);
 
        while (comp != NULL) {
                CAComponentDescription temp;
+#ifdef COREAUDIO105
                GetComponentInfo (comp, &temp, NULL, NULL, NULL);
+#else
+               AudioComponentGetDescription (comp, &temp);
+#endif
                CFStringRef itemName = NULL;
 
                {
@@ -2469,7 +2641,7 @@ AUPluginInfo::discover_by_description (PluginInfoList& plugs, CAComponentDescrip
 
                if (is_blacklisted(CFStringRefToStdString(itemName))) {
                        info << string_compose (_("Skipped blacklisted AU plugin %1 "), CFStringRefToStdString(itemName)) << endmsg;
-                       comp = FindNextComponent (comp, &desc);
+                       comp = ArdourFindNext (comp, &desc);
                        continue;
                }
 
@@ -2489,7 +2661,7 @@ AUPluginInfo::discover_by_description (PluginInfoList& plugs, CAComponentDescrip
                case kAudioUnitType_Panner:
                case kAudioUnitType_OfflineEffect:
                case kAudioUnitType_FormatConverter:
-                       comp = FindNextComponent (comp, &desc);
+                       comp = ArdourFindNext (comp, &desc);
                        continue;
 
                case kAudioUnitType_Output:
@@ -2516,7 +2688,11 @@ AUPluginInfo::discover_by_description (PluginInfoList& plugs, CAComponentDescrip
                }
 
                au_blacklist(CFStringRefToStdString(itemName));
-               AUPluginInfo::get_names (temp, info->name, info->creator);
+#ifdef COREAUDIO105
+               get_names (temp, info->name, info->creator);
+#else
+               get_names (comp, info->name, info->creator);
+#endif
                ARDOUR::PluginScanMessage(_("AU"), info->name, false);
                au_crashlog(string_compose("Plugin: %1", info->name));
 
@@ -2528,7 +2704,12 @@ AUPluginInfo::discover_by_description (PluginInfoList& plugs, CAComponentDescrip
 
                CAComponent cacomp (*info->descriptor);
 
-               if (cacomp.GetResourceVersion (info->version) != noErr) {
+#ifdef COREAUDIO105
+               if (cacomp.GetResourceVersion (info->version) != noErr)
+#else
+               if (cacomp.GetVersion (info->version) != noErr)
+#endif
+               {
                        info->version = 0;
                }
 
@@ -2550,7 +2731,7 @@ AUPluginInfo::discover_by_description (PluginInfoList& plugs, CAComponentDescrip
 
                        int32_t possible_in = info->cache.io_configs.front().first;
                        int32_t possible_out = info->cache.io_configs.front().second;
-                       
+
                        if (possible_in > 0) {
                                info->n_inputs.set (DataType::AUDIO, possible_in);
                        } else {
@@ -2575,7 +2756,7 @@ AUPluginInfo::discover_by_description (PluginInfoList& plugs, CAComponentDescrip
 
                au_unblacklist(CFStringRefToStdString(itemName));
                au_crashlog("Success.");
-               comp = FindNextComponent (comp, &desc);
+               comp = ArdourFindNext (comp, &desc);
                if (itemName != NULL) CFRelease(itemName); itemName = NULL;
        }
        au_crashlog(string_compose("End AU discovery for Type: %1", (int)desc.componentType));
@@ -2810,59 +2991,6 @@ AUPluginInfo::load_cached_info ()
        return 0;
 }
 
-void
-AUPluginInfo::get_names (CAComponentDescription& comp_desc, std::string& name, std::string& maker)
-{
-       CFStringRef itemName = NULL;
-
-       // Marc Poirier-style item name
-       CAComponent auComponent (comp_desc);
-       if (auComponent.IsValid()) {
-               CAComponentDescription dummydesc;
-               Handle nameHandle = NewHandle(sizeof(void*));
-               if (nameHandle != NULL) {
-                       OSErr err = GetComponentInfo(auComponent.Comp(), &dummydesc, nameHandle, NULL, NULL);
-                       if (err == noErr) {
-                               ConstStr255Param nameString = (ConstStr255Param) (*nameHandle);
-                               if (nameString != NULL) {
-                                       itemName = CFStringCreateWithPascalString(kCFAllocatorDefault, nameString, CFStringGetSystemEncoding());
-                               }
-                       }
-                       DisposeHandle(nameHandle);
-               }
-       }
-
-       // if Marc-style fails, do the original way
-       if (itemName == NULL) {
-               CFStringRef compTypeString = UTCreateStringForOSType(comp_desc.componentType);
-               CFStringRef compSubTypeString = UTCreateStringForOSType(comp_desc.componentSubType);
-               CFStringRef compManufacturerString = UTCreateStringForOSType(comp_desc.componentManufacturer);
-
-               itemName = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%@ - %@ - %@"),
-                       compTypeString, compManufacturerString, compSubTypeString);
-
-               if (compTypeString != NULL)
-                       CFRelease(compTypeString);
-               if (compSubTypeString != NULL)
-                       CFRelease(compSubTypeString);
-               if (compManufacturerString != NULL)
-                       CFRelease(compManufacturerString);
-       }
-
-       string str = CFStringRefToStdString(itemName);
-       string::size_type colon = str.find (':');
-
-       if (colon) {
-               name = str.substr (colon+1);
-               maker = str.substr (0, colon);
-               strip_whitespace_edges (maker);
-               strip_whitespace_edges (name);
-       } else {
-               name = str;
-               maker = "unknown";
-               strip_whitespace_edges (name);
-       }
-}
 
 std::string
 AUPluginInfo::stringify_descriptor (const CAComponentDescription& desc)
@@ -2917,7 +3045,7 @@ void
 AUPlugin::set_info (PluginInfoPtr info)
 {
        Plugin::set_info (info);
-       
+
        AUPluginInfoPtr pinfo = boost::dynamic_pointer_cast<AUPluginInfo>(get_info());
        _has_midi_input = pinfo->needs_midi_input ();
        _has_midi_output = false;
@@ -2927,7 +3055,7 @@ int
 AUPlugin::create_parameter_listener (AUEventListenerProc cb, void* arg, float interval_secs)
 {
 #ifdef WITH_CARBON
-       CFRunLoopRef run_loop = (CFRunLoopRef) GetCFRunLoopFromEventLoop(GetCurrentEventLoop()); 
+       CFRunLoopRef run_loop = (CFRunLoopRef) GetCFRunLoopFromEventLoop(GetCurrentEventLoop());
 #else
        CFRunLoopRef run_loop = CFRunLoopGetCurrent();
 #endif
@@ -2959,7 +3087,7 @@ AUPlugin::listen_to_parameter (uint32_t param_id)
 
        if (AUEventListenerAddEventType (_parameter_listener, _parameter_listener_arg, &event) != noErr) {
                return -1;
-       } 
+       }
 
        event.mEventType = kAudioUnitEvent_BeginParameterChangeGesture;
        event.mArgument.mParameter.mAudioUnit = unit->AU();
@@ -2969,7 +3097,7 @@ AUPlugin::listen_to_parameter (uint32_t param_id)
 
        if (AUEventListenerAddEventType (_parameter_listener, _parameter_listener_arg, &event) != noErr) {
                return -1;
-       } 
+       }
 
        event.mEventType = kAudioUnitEvent_EndParameterChangeGesture;
        event.mArgument.mParameter.mAudioUnit = unit->AU();
@@ -2979,7 +3107,7 @@ AUPlugin::listen_to_parameter (uint32_t param_id)
 
        if (AUEventListenerAddEventType (_parameter_listener, _parameter_listener_arg, &event) != noErr) {
                return -1;
-       } 
+       }
 
        return 0;
 }
@@ -3001,7 +3129,7 @@ AUPlugin::end_listen_to_parameter (uint32_t param_id)
 
        if (AUEventListenerRemoveEventType (_parameter_listener, _parameter_listener_arg, &event) != noErr) {
                return -1;
-       } 
+       }
 
        event.mEventType = kAudioUnitEvent_BeginParameterChangeGesture;
        event.mArgument.mParameter.mAudioUnit = unit->AU();
@@ -3011,7 +3139,7 @@ AUPlugin::end_listen_to_parameter (uint32_t param_id)
 
        if (AUEventListenerRemoveEventType (_parameter_listener, _parameter_listener_arg, &event) != noErr) {
                return -1;
-       } 
+       }
 
        event.mEventType = kAudioUnitEvent_EndParameterChangeGesture;
        event.mArgument.mParameter.mAudioUnit = unit->AU();
@@ -3021,7 +3149,7 @@ AUPlugin::end_listen_to_parameter (uint32_t param_id)
 
        if (AUEventListenerRemoveEventType (_parameter_listener, _parameter_listener_arg, &event) != noErr) {
                return -1;
-       } 
+       }
 
        return 0;
 }
@@ -3033,14 +3161,14 @@ AUPlugin::_parameter_change_listener (void* arg, void* src, const AudioUnitEvent
 }
 
 void
-AUPlugin::parameter_change_listener (void* /*arg*/, void* /*src*/, const AudioUnitEvent* event, UInt64 /*host_time*/, Float32 new_value)
+AUPlugin::parameter_change_listener (void* /*arg*/, void* src, const AudioUnitEvent* event, UInt64 /*host_time*/, Float32 new_value)
 {
         ParameterMap::iterator i;
 
         if ((i = parameter_map.find (event->mArgument.mParameter.mParameterID)) == parameter_map.end()) {
                 return;
         }
-        
+
         switch (event->mEventType) {
         case kAudioUnitEvent_BeginParameterChangeGesture:
                 StartTouch (i->second);
@@ -3049,7 +3177,10 @@ AUPlugin::parameter_change_listener (void* /*arg*/, void* /*src*/, const AudioUn
                 EndTouch (i->second);
                 break;
         case kAudioUnitEvent_ParameterValueChange:
-                ParameterChanged (i->second, new_value);
+                /* whenever we change a parameter, we request that we are NOT notified of the change, so anytime we arrive here, it
+                   means that something else (i.e. the plugin GUI) made the change.
+                */
+                ParameterChangedExternally (i->second, new_value);
                 break;
         default:
                 break;