+ // XXX as of May 13th 2008, AU plugin support returns a count of either 1 or -1. We never
+ // attempt to multiply-instantiate plugins to meet io configurations.
+
+ int32_t plugcnt = -1;
+ AUPluginInfoPtr pinfo = boost::dynamic_pointer_cast<AUPluginInfo>(get_info());
+
+ out = -1;
+
+ vector<pair<int,int> >& io_configs = pinfo->cache.io_configs;
+
+ for (vector<pair<int,int> >::iterator i = io_configs.begin(); i != io_configs.end(); ++i) {
+
+ int32_t possible_in = i->first;
+ int32_t possible_out = i->second;
+
+ if (possible_out == 0) {
+ warning << string_compose (_("AU %1 has zero outputs - configuration ignored"), name()) << endmsg;
+ continue;
+ }
+
+ if (possible_in == 0) {
+
+ /* instrument plugin, always legal but throws away inputs ...
+ */
+
+ if (possible_out == -1) {
+ /* out much match in (UNLIKELY!!) */
+ out = in;
+ plugcnt = 1;
+ } else if (possible_out == -2) {
+ /* any configuration possible, pick matching */
+ out = in;
+ plugcnt = 1;
+ } else if (possible_out < -2) {
+ /* explicit variable number of outputs, pick maximum */
+ out = -possible_out;
+ plugcnt = 1;
+ } else {
+ /* exact number of outputs */
+ out = possible_out;
+ plugcnt = 1;
+ }
+ }
+
+ if (possible_in == -1) {
+
+ /* wildcard for input */
+
+ if (possible_out == -1) {
+ /* out much match in */
+ out = in;
+ plugcnt = 1;
+ } else if (possible_out == -2) {
+ /* any configuration possible, pick matching */
+ out = in;
+ plugcnt = 1;
+ } else if (possible_out < -2) {
+ /* explicit variable number of outputs, pick maximum */
+ out = -possible_out;
+ plugcnt = 1;
+ } else {
+ /* exact number of outputs */
+ out = possible_out;
+ plugcnt = 1;
+ }
+ }
+
+ if (possible_in == -2) {
+
+ if (possible_out == -1) {
+ /* any configuration possible, pick matching */
+ out = in;
+ plugcnt = 1;
+ } else if (possible_out == -2) {
+ error << string_compose (_("AU plugin %1 has illegal IO configuration (-2,-2)"), name())
+ << endmsg;
+ plugcnt = -1;
+ } else if (possible_out < -2) {
+ /* explicit variable number of outputs, pick maximum */
+ out = -possible_out;
+ plugcnt = 1;
+ } else {
+ /* exact number of outputs */
+ out = possible_out;
+ plugcnt = 1;
+ }
+ }
+
+ if (possible_in < -2) {
+
+ /* explicit variable number of inputs */
+
+ if (in > -possible_in) {
+ /* request is too large */
+ plugcnt = -1;
+ }
+
+ if (possible_out == -1) {
+ /* out must match in */
+ out = in;
+ plugcnt = 1;
+ } else if (possible_out == -2) {
+ error << string_compose (_("AU plugin %1 has illegal IO configuration (-2,-2)"), name())
+ << endmsg;
+ plugcnt = -1;
+ } else if (possible_out < -2) {
+ /* explicit variable number of outputs, pick maximum */
+ out = -possible_out;
+ plugcnt = 1;
+ } else {
+ /* exact number of outputs */
+ out = possible_out;
+ plugcnt = 1;
+ }
+ }
+
+ if (possible_in == in) {
+
+ /* exact number of inputs ... must match obviously */
+
+ if (possible_out == -1) {
+ /* out must match in */
+ out = in;
+ plugcnt = 1;
+ } else if (possible_out == -2) {
+ /* any output configuration, pick matching */
+ out = in;
+ plugcnt = -1;
+ } else if (possible_out < -2) {
+ /* explicit variable number of outputs, pick maximum */
+ out = -possible_out;
+ plugcnt = 1;
+ } else {
+ /* exact number of outputs */
+ out = possible_out;
+ plugcnt = 1;
+ }
+ }
+
+ if (plugcnt == 1) {
+ break;
+ }
+ }
+
+ /* no fit */
+ return plugcnt;
+}
+
+int
+AUPlugin::set_input_format (AudioStreamBasicDescription& fmt)
+{
+ return set_stream_format (kAudioUnitScope_Input, input_elements, fmt);
+}
+
+int
+AUPlugin::set_output_format (AudioStreamBasicDescription& fmt)
+{
+ if (set_stream_format (kAudioUnitScope_Output, output_elements, fmt) != 0) {
+ return -1;
+ }
+
+ if (buffers) {
+ free (buffers);
+ buffers = 0;
+ }
+
+ buffers = (AudioBufferList *) malloc (offsetof(AudioBufferList, mBuffers) +
+ fmt.mChannelsPerFrame * sizeof(AudioBuffer));
+
+ Glib::Mutex::Lock em (_session.engine().process_lock());
+ IO::MoreOutputs (fmt.mChannelsPerFrame);
+
+ return 0;
+}
+
+int
+AUPlugin::set_stream_format (int scope, uint32_t cnt, AudioStreamBasicDescription& fmt)
+{
+ OSErr result;
+
+ for (uint32_t i = 0; i < cnt; ++i) {
+ if ((result = unit->SetFormat (scope, i, fmt)) != 0) {
+ error << string_compose (_("AUPlugin: could not set stream format for %1/%2 (err = %3)"),
+ (scope == kAudioUnitScope_Input ? "input" : "output"), i, result) << endmsg;
+ return -1;
+ }
+ }
+
+ if (scope == kAudioUnitScope_Input) {
+ input_channels.setAudio( fmt.mChannelsPerFrame );
+ } else {
+ output_channels.setAudio( fmt.mChannelsPerFrame );
+ }
+
+ return 0;
+}
+
+ChanCount
+AUPlugin::input_streams() const
+{
+ if (input_channels < 0) {
+ warning << string_compose (_("AUPlugin: %1 input_streams() called without any format set!"), name()) << endmsg;
+ return 1;
+ }
+ return input_channels;
+}
+
+
+ChanCount
+AUPlugin::output_streams() const
+{
+ if (output_channels < 0) {
+ warning << string_compose (_("AUPlugin: %1 output_streams() called without any format set!"), name()) << endmsg;
+ return 1;
+ }
+ return output_channels;
+}
+
+OSStatus
+AUPlugin::render_callback(AudioUnitRenderActionFlags *ioActionFlags,
+ const AudioTimeStamp *inTimeStamp,
+ UInt32 inBusNumber,
+ UInt32 inNumberFrames,
+ AudioBufferList* ioData)
+{
+ /* not much to do - the data is already in the buffers given to us in connect_and_run() */
+
+ if (current_maxbuf == 0) {
+ error << _("AUPlugin: render callback called illegally!") << endmsg;
+ return kAudioUnitErr_CannotDoInCurrentContext;
+ }
+
+ for (uint32_t i = 0; i < current_maxbuf; ++i) {
+ ioData->mBuffers[i].mNumberChannels = 1;
+ ioData->mBuffers[i].mDataByteSize = sizeof (Sample) * inNumberFrames;
+ ioData->mBuffers[i].mData = (*current_buffers)[i] + cb_offset + current_offset;
+ }
+
+ cb_offset += inNumberFrames;
+
+ return noErr;
+}
+
+int
+AUPlugin::connect_and_run (vector<Sample*>& bufs, uint32_t maxbuf, int32_t& in, int32_t& out, nframes_t nframes, nframes_t offset)
+{
+ AudioUnitRenderActionFlags flags = 0;
+ AudioTimeStamp ts;
+
+ current_buffers = &bufs;
+ current_maxbuf = maxbuf;
+ current_offset = offset;
+ cb_offset = 0;
+
+ buffers->mNumberBuffers = maxbuf;
+
+ for (uint32_t i = 0; i < maxbuf; ++i) {
+ buffers->mBuffers[i].mNumberChannels = 1;
+ buffers->mBuffers[i].mDataByteSize = nframes * sizeof (Sample);
+ buffers->mBuffers[i].mData = 0;
+ }
+
+ ts.mSampleTime = frames_processed;
+ ts.mFlags = kAudioTimeStampSampleTimeValid;
+
+ if (unit->Render (&flags, &ts, 0, nframes, buffers) == noErr) {
+
+ current_maxbuf = 0;
+ frames_processed += nframes;
+
+ for (uint32_t i = 0; i < maxbuf; ++i) {
+ if (bufs[i] + offset != buffers->mBuffers[i].mData) {
+ memcpy (bufs[i]+offset, buffers->mBuffers[i].mData, nframes * sizeof (Sample));
+ }
+ }
+ return 0;
+ }
+
+ return -1;
+}
+
+set<uint32_t>
+AUPlugin::automatable() const
+{
+ set<uint32_t> automates;
+
+ for (uint32_t i = 0; i < descriptors.size(); ++i) {
+ if (descriptors[i].automatable) {
+ automates.insert (i);
+ }
+ }
+
+ return automates;
+}
+
+string
+AUPlugin::describe_parameter (uint32_t param)
+{
+ return descriptors[param].label;
+}
+
+void
+AUPlugin::print_parameter (uint32_t param, char* buf, uint32_t len) const
+{
+ // NameValue stuff here
+}
+
+bool
+AUPlugin::parameter_is_audio (uint32_t) const
+{
+ return false;
+}
+
+bool
+AUPlugin::parameter_is_control (uint32_t) const
+{
+ return true;
+}
+
+bool
+AUPlugin::parameter_is_input (uint32_t) const
+{
+ return false;
+}
+
+bool
+AUPlugin::parameter_is_output (uint32_t) const
+{
+ return false;
+}
+
+XMLNode&
+AUPlugin::get_state()
+{
+ LocaleGuard lg (X_("POSIX"));
+ XMLNode *root = new XMLNode (state_node_name());
+
+#ifdef AU_STATE_SUPPORT
+ CFDataRef xmlData;
+ CFPropertyListRef propertyList;
+
+ if (unit->GetAUPreset (propertyList) != noErr) {
+ return *root;
+ }
+
+ // Convert the property list into XML data.
+
+ xmlData = CFPropertyListCreateXMLData( kCFAllocatorDefault, propertyList);
+
+ if (!xmlData) {
+ error << _("Could not create XML version of property list") << endmsg;
+ return *root;
+ }
+
+ /* re-parse XML bytes to create a libxml++ XMLTree that we can merge into
+ our state node. GACK!
+ */
+
+ XMLTree t;
+
+ if (t.read_buffer (string ((const char*) CFDataGetBytePtr (xmlData), CFDataGetLength (xmlData)))) {
+ if (t.root()) {
+ root->add_child_copy (*t.root());
+ }
+ }
+
+ CFRelease (xmlData);
+ CFRelease (propertyList);
+#else
+ if (!seen_get_state_message) {
+ info << string_compose (_("Saving AudioUnit settings is not supported in this build of %1. Consider paying for a newer version"), PROGRAM_NAME)
+ << endmsg;
+ seen_get_state_message = true;
+ }
+#endif
+
+ return *root;
+}
+
+int
+AUPlugin::set_state(const XMLNode& node)
+{
+#ifdef AU_STATE_SUPPORT
+ int ret = -1;
+ CFPropertyListRef propertyList;
+ LocaleGuard lg (X_("POSIX"));
+
+ if (node.name() != state_node_name()) {
+ error << _("Bad node sent to AUPlugin::set_state") << endmsg;
+ return -1;
+ }
+
+ if (node.children().empty()) {
+ return -1;
+ }
+
+ XMLNode* top = node.children().front();
+ XMLNode* copy = new XMLNode (*top);
+
+ XMLTree t;
+ t.set_root (copy);
+
+ const string& xml = t.write_buffer ();
+ CFDataRef xmlData = CFDataCreateWithBytesNoCopy (kCFAllocatorDefault, (UInt8*) xml.data(), xml.length(), kCFAllocatorNull);
+ CFStringRef errorString;
+
+ propertyList = CFPropertyListCreateFromXMLData( kCFAllocatorDefault,
+ xmlData,
+ kCFPropertyListImmutable,
+ &errorString);
+
+ CFRelease (xmlData);
+
+ if (propertyList) {
+ if (unit->SetAUPreset (propertyList) == noErr) {
+ ret = 0;
+ }
+ CFRelease (propertyList);
+ }
+
+ return ret;
+#else
+ if (!seen_set_state_message) {
+ info << string_compose (_("Restoring AudioUnit settings is not supported in this build of %1. Consider paying for a newer version", PROGRAM_NAME)
+ << endmsg;
+ }
+ return 0;
+#endif
+}
+
+bool
+AUPlugin::load_preset (const string& preset_label)
+{
+#ifdef AU_STATE_SUPPORT
+ bool ret = false;
+ CFPropertyListRef propertyList;
+ Glib::ustring path;
+ PresetMap::iterator x = preset_map.find (preset_label);
+
+ if (x == preset_map.end()) {
+ return false;
+ }
+
+ if ((propertyList = load_property_list (x->second)) != 0) {
+ if (unit->SetAUPreset (propertyList) == noErr) {
+ ret = true;
+ }
+ CFRelease(propertyList);
+ }
+
+ return ret;
+#else
+ if (!seen_loading_message) {
+ info << string_compose (_("Loading AudioUnit presets is not supported in this build of %1. Consider paying for a newer version", PROGRAM_NAME)
+ << endmsg;
+ seen_loading_message = true;
+ }
+ return true;
+#endif
+}
+
+bool
+AUPlugin::save_preset (string preset_name)
+{
+#ifdef AU_STATE_SUPPORT
+ CFPropertyListRef propertyList;
+ vector<Glib::ustring> v;
+ Glib::ustring user_preset_path;
+ bool ret = true;
+
+ std::string m = maker();
+ std::string n = name();
+
+ strip_whitespace_edges (m);
+ strip_whitespace_edges (n);
+
+ v.push_back (Glib::get_home_dir());
+ v.push_back ("Library");
+ v.push_back ("Audio");
+ v.push_back ("Presets");
+ v.push_back (m);
+ v.push_back (n);
+
+ user_preset_path = Glib::build_filename (v);
+
+ if (g_mkdir_with_parents (user_preset_path.c_str(), 0775) < 0) {
+ error << string_compose (_("Cannot create user plugin presets folder (%1)"), user_preset_path) << endmsg;
+ return false;
+ }
+
+ if (unit->GetAUPreset (propertyList) != noErr) {
+ return false;
+ }
+
+ // add the actual preset name */
+
+ v.push_back (preset_name + preset_suffix);
+
+ // rebuild
+
+ user_preset_path = Glib::build_filename (v);
+
+ set_preset_name_in_plist (propertyList, preset_name);
+
+ if (save_property_list (propertyList, user_preset_path)) {
+ error << string_compose (_("Saving plugin state to %1 failed"), user_preset_path) << endmsg;
+ ret = false;
+ }
+
+ CFRelease(propertyList);
+
+ return ret;
+#else
+ if (!seen_saving_message) {
+ info << string_compose (_("Saving AudioUnit presets is not supported in this build of %1. Consider paying for a newer version"), PROGRAM_NAME)
+ << endmsg;
+ seen_saving_message = true;
+ }
+ return false;
+#endif
+}
+
+//-----------------------------------------------------------------------------
+// this is just a little helper function used by GetAUComponentDescriptionFromPresetFile()
+static SInt32
+GetDictionarySInt32Value(CFDictionaryRef inAUStateDictionary, CFStringRef inDictionaryKey, Boolean * outSuccess)
+{
+ CFNumberRef cfNumber;
+ SInt32 numberValue = 0;
+ Boolean dummySuccess;
+
+ if (outSuccess == NULL)
+ outSuccess = &dummySuccess;
+ if ( (inAUStateDictionary == NULL) || (inDictionaryKey == NULL) )
+ {
+ *outSuccess = FALSE;
+ return 0;
+ }
+
+ cfNumber = (CFNumberRef) CFDictionaryGetValue(inAUStateDictionary, inDictionaryKey);
+ if (cfNumber == NULL)
+ {
+ *outSuccess = FALSE;
+ return 0;
+ }
+ *outSuccess = CFNumberGetValue(cfNumber, kCFNumberSInt32Type, &numberValue);
+ if (*outSuccess)
+ return numberValue;
+ else
+ return 0;
+}
+
+static OSStatus
+GetAUComponentDescriptionFromStateData(CFPropertyListRef inAUStateData, ComponentDescription * outComponentDescription)
+{
+ CFDictionaryRef auStateDictionary;
+ ComponentDescription tempDesc = {0};
+ SInt32 versionValue;
+ Boolean gotValue;
+
+ if ( (inAUStateData == NULL) || (outComponentDescription == NULL) )
+ return paramErr;
+
+ // the property list for AU state data must be of the dictionary type
+ if (CFGetTypeID(inAUStateData) != CFDictionaryGetTypeID()) {
+ return kAudioUnitErr_InvalidPropertyValue;
+ }
+
+ auStateDictionary = (CFDictionaryRef)inAUStateData;
+
+ // first check to make sure that the version of the AU state data is one that we know understand
+ // XXX should I really do this? later versions would probably still hold these ID keys, right?
+ versionValue = GetDictionarySInt32Value(auStateDictionary, CFSTR(kAUPresetVersionKey), &gotValue);
+
+ if (!gotValue) {
+ return kAudioUnitErr_InvalidPropertyValue;
+ }
+#define kCurrentSavedStateVersion 0
+ if (versionValue != kCurrentSavedStateVersion) {
+ return kAudioUnitErr_InvalidPropertyValue;
+ }
+
+ // grab the ComponentDescription values from the AU state data
+ tempDesc.componentType = (OSType) GetDictionarySInt32Value(auStateDictionary, CFSTR(kAUPresetTypeKey), NULL);
+ tempDesc.componentSubType = (OSType) GetDictionarySInt32Value(auStateDictionary, CFSTR(kAUPresetSubtypeKey), NULL);
+ tempDesc.componentManufacturer = (OSType) GetDictionarySInt32Value(auStateDictionary, CFSTR(kAUPresetManufacturerKey), NULL);
+ // zero values are illegit for specific ComponentDescriptions, so zero for any value means that there was an error
+ if ( (tempDesc.componentType == 0) || (tempDesc.componentSubType == 0) || (tempDesc.componentManufacturer == 0) )
+ return kAudioUnitErr_InvalidPropertyValue;
+
+ *outComponentDescription = tempDesc;
+ return noErr;
+}
+
+
+static bool au_preset_filter (const string& str, void* arg)
+{
+ /* Not a dotfile, has a prefix before a period, suffix is aupreset */
+
+ bool ret;
+
+ ret = (str[0] != '.' && str.length() > 9 && str.find (preset_suffix) == (str.length() - preset_suffix.length()));
+
+ if (ret && arg) {
+
+ /* check the preset file path name against this plugin
+ ID. The idea is that all preset files for this plugin
+ include "<manufacturer>/<plugin-name>" in their path.
+ */
+
+ Plugin* p = (Plugin *) arg;
+ string match = p->maker();
+ match += '/';
+ match += p->name();
+
+ ret = str.find (match) != string::npos;
+
+ if (ret == false) {
+ string m = p->maker ();
+ string n = p->name ();
+ strip_whitespace_edges (m);
+ strip_whitespace_edges (n);
+ match = m;
+ match += '/';
+ match += n;
+
+ ret = str.find (match) != string::npos;
+ }
+ }
+
+ return ret;
+}
+
+bool
+check_and_get_preset_name (Component component, const string& pathstr, string& preset_name)
+{
+ OSStatus status;
+ CFPropertyListRef plist;
+ ComponentDescription presetDesc;
+ bool ret = false;
+
+ plist = load_property_list (pathstr);
+
+ if (!plist) {
+ return ret;
+ }
+
+ // get the ComponentDescription from the AU preset file
+
+ status = GetAUComponentDescriptionFromStateData(plist, &presetDesc);
+
+ if (status == noErr) {
+ if (ComponentAndDescriptionMatch_Loosely(component, &presetDesc)) {
+
+ /* try to get the preset name from the property list */
+
+ if (CFGetTypeID(plist) == CFDictionaryGetTypeID()) {
+
+ const void* psk = CFDictionaryGetValue ((CFMutableDictionaryRef)plist, CFSTR(kAUPresetNameKey));
+
+ if (psk) {
+
+ const char* p = CFStringGetCStringPtr ((CFStringRef) psk, kCFStringEncodingUTF8);
+
+ if (!p) {
+ char buf[PATH_MAX+1];
+
+ if (CFStringGetCString ((CFStringRef)psk, buf, sizeof (buf), kCFStringEncodingUTF8)) {
+ preset_name = buf;
+ }
+ }
+ }
+ }
+ }
+ }
+
+ CFRelease (plist);
+
+ return true;
+}
+
+std::string
+AUPlugin::current_preset() const
+{
+ string preset_name;
+
+#ifdef AU_STATE_SUPPORT
+ CFPropertyListRef propertyList;
+
+ if (unit->GetAUPreset (propertyList) == noErr) {
+ preset_name = get_preset_name_in_plist (propertyList);
+ CFRelease(propertyList);
+ }
+#endif
+ return preset_name;
+}
+
+vector<string>
+AUPlugin::get_presets ()
+{
+ vector<string*>* preset_files;
+ vector<string> presets;
+ PathScanner scanner;
+
+ preset_files = scanner (preset_search_path, au_preset_filter, this, true, true, -1, true);
+
+ if (!preset_files) {
+ return presets;
+ }
+
+ for (vector<string*>::iterator x = preset_files->begin(); x != preset_files->end(); ++x) {
+
+ string path = *(*x);
+ string preset_name;
+
+ /* make an initial guess at the preset name using the path */
+
+ preset_name = Glib::path_get_basename (path);
+ preset_name = preset_name.substr (0, preset_name.find_last_of ('.'));
+
+ /* check that this preset file really matches this plugin
+ and potentially get the "real" preset name from
+ within the file.
+ */
+
+ if (check_and_get_preset_name (get_comp()->Comp(), path, preset_name)) {
+ presets.push_back (preset_name);
+ preset_map[preset_name] = path;
+ }
+
+ delete *x;
+ }
+
+ delete preset_files;
+
+ return presets;
+}
+
+bool
+AUPlugin::has_editor () const
+{
+ // even if the plugin doesn't have its own editor, the AU API can be used
+ // to create one that looks native.
+ return true;
+}
+
+AUPluginInfo::AUPluginInfo (boost::shared_ptr<CAComponentDescription> d)
+ : descriptor (d)
+{
+
+}
+
+AUPluginInfo::~AUPluginInfo ()
+{
+}
+
+PluginPtr
+AUPluginInfo::load (Session& session)
+{
+ try {
+ PluginPtr plugin;
+
+ boost::shared_ptr<CAComponent> comp (new CAComponent(*descriptor));
+
+ if (!comp->IsValid()) {
+ error << ("AudioUnit: not a valid Component") << endmsg;
+ } else {
+ plugin.reset (new AUPlugin (session.engine(), session, comp));
+ }
+
+ plugin->set_info (PluginInfoPtr (new AUPluginInfo (*this)));
+ return plugin;
+ }
+
+ catch (failed_constructor &err) {
+ return PluginPtr ();
+ }
+}
+
+Glib::ustring
+AUPluginInfo::au_cache_path ()
+{
+ return Glib::build_filename (ARDOUR::get_user_ardour_path(), "au_cache");
+}
+
+PluginInfoList*
+AUPluginInfo::discover ()
+{
+ XMLTree tree;
+
+ if (!Glib::file_test (au_cache_path(), Glib::FILE_TEST_EXISTS)) {
+ ARDOUR::BootMessage (_("Discovering AudioUnit plugins (could take some time ...)"));
+ }
+
+ PluginInfoList* plugs = new PluginInfoList ();
+
+ discover_fx (plugs);
+ discover_music (plugs);
+ discover_generators (plugs);
+
+ return plugs;
+}
+
+void
+AUPluginInfo::discover_music (PluginInfoList& plugs)
+{
+ CAComponentDescription desc;
+ desc.componentFlags = 0;
+ desc.componentFlagsMask = 0;
+ desc.componentSubType = 0;
+ desc.componentManufacturer = 0;
+ desc.componentType = kAudioUnitType_MusicEffect;
+
+ discover_by_description (plugs, desc);
+}
+
+void
+AUPluginInfo::discover_fx (PluginInfoList& plugs)
+{
+ CAComponentDescription desc;
+ desc.componentFlags = 0;
+ desc.componentFlagsMask = 0;
+ desc.componentSubType = 0;
+ desc.componentManufacturer = 0;
+ desc.componentType = kAudioUnitType_Effect;
+
+ discover_by_description (plugs, desc);
+}
+
+void
+AUPluginInfo::discover_generators (PluginInfoList& plugs)
+{
+ CAComponentDescription desc;
+ desc.componentFlags = 0;
+ desc.componentFlagsMask = 0;
+ desc.componentSubType = 0;
+ desc.componentManufacturer = 0;
+ desc.componentType = kAudioUnitType_Generator;
+
+ discover_by_description (plugs, desc);
+}
+
+void
+AUPluginInfo::discover_by_description (PluginInfoList& plugs, CAComponentDescription& desc)
+{
+ Component comp = 0;
+
+ comp = FindNextComponent (NULL, &desc);
+
+ while (comp != NULL) {
+ CAComponentDescription temp;
+ GetComponentInfo (comp, &temp, NULL, NULL, NULL);
+
+ AUPluginInfoPtr info (new AUPluginInfo
+ (boost::shared_ptr<CAComponentDescription> (new CAComponentDescription(temp))));
+
+ /* no panners, format converters or i/o AU's for our purposes
+ */
+
+ switch (info->descriptor->Type()) {
+ case kAudioUnitType_Panner:
+ case kAudioUnitType_OfflineEffect:
+ case kAudioUnitType_FormatConverter:
+ continue;
+ case kAudioUnitType_Output:
+ case kAudioUnitType_MusicDevice:
+ case kAudioUnitType_MusicEffect:
+ case kAudioUnitType_Effect:
+ case kAudioUnitType_Mixer:
+ case kAudioUnitType_Generator:
+ break;
+ default:
+ break;
+ }
+
+ switch (info->descriptor->SubType()) {
+ case kAudioUnitSubType_DefaultOutput:
+ case kAudioUnitSubType_SystemOutput:
+ case kAudioUnitSubType_GenericOutput:
+ case kAudioUnitSubType_AUConverter:
+ /* we don't want output units here */
+ continue;
+ break;
+
+ case kAudioUnitSubType_DLSSynth:
+ info->category = "DLS Synth";
+ break;
+
+ case kAudioUnitSubType_Varispeed:
+ info->category = "Varispeed";
+ break;
+
+ case kAudioUnitSubType_Delay:
+ info->category = "Delay";
+ break;
+
+ case kAudioUnitSubType_LowPassFilter:
+ info->category = "Low-pass Filter";
+ break;
+
+ case kAudioUnitSubType_HighPassFilter:
+ info->category = "High-pass Filter";
+ break;
+
+ case kAudioUnitSubType_BandPassFilter:
+ info->category = "Band-pass Filter";
+ break;
+
+ case kAudioUnitSubType_HighShelfFilter:
+ info->category = "High-shelf Filter";
+ break;
+
+ case kAudioUnitSubType_LowShelfFilter:
+ info->category = "Low-shelf Filter";
+ break;
+
+ case kAudioUnitSubType_ParametricEQ:
+ info->category = "Parametric EQ";
+ break;
+
+ case kAudioUnitSubType_GraphicEQ:
+ info->category = "Graphic EQ";
+ break;
+
+ case kAudioUnitSubType_PeakLimiter:
+ info->category = "Peak Limiter";
+ break;
+
+ case kAudioUnitSubType_DynamicsProcessor:
+ info->category = "Dynamics Processor";
+ break;
+
+ case kAudioUnitSubType_MultiBandCompressor:
+ info->category = "Multiband Compressor";
+ break;
+
+ case kAudioUnitSubType_MatrixReverb:
+ info->category = "Matrix Reverb";
+ break;
+
+ case kAudioUnitSubType_SampleDelay:
+ info->category = "Sample Delay";
+ break;
+
+ case kAudioUnitSubType_Pitch:
+ info->category = "Pitch";
+ break;
+
+ case kAudioUnitSubType_NetSend:
+ info->category = "Net Sender";
+ break;
+
+ case kAudioUnitSubType_3DMixer:
+ info->category = "3DMixer";
+ break;
+
+ case kAudioUnitSubType_MatrixMixer:
+ info->category = "MatrixMixer";
+ break;
+
+ case kAudioUnitSubType_ScheduledSoundPlayer:
+ info->category = "Scheduled Sound Player";
+ break;
+
+
+ case kAudioUnitSubType_AudioFilePlayer:
+ info->category = "Audio File Player";
+ break;
+
+ case kAudioUnitSubType_NetReceive:
+ info->category = "Net Receiver";
+ break;
+
+ default:
+ info->category = "";
+ }
+
+ AUPluginInfo::get_names (temp, info->name, info->creator);
+
+ info->type = ARDOUR::AudioUnit;
+ info->unique_id = stringify_descriptor (*info->descriptor);
+
+ /* XXX not sure of the best way to handle plugin versioning yet
+ */
+
+ CAComponent cacomp (*info->descriptor);
+
+ if (cacomp.GetResourceVersion (info->version) != noErr) {
+ info->version = 0;
+ }
+
+ if (cached_io_configuration (info->unique_id, info->version, cacomp, info->cache, info->name)) {
+
+ /* here we have to map apple's wildcard system to a simple pair
+ of values. in ::can_do() we use the whole system, but here
+ we need a single pair of values. XXX probably means we should
+ remove any use of these values.
+ */
+
+ info->n_inputs = info->cache.io_configs.front().first;
+ info->n_outputs = info->cache.io_configs.front().second;
+
+ if (info->cache.io_configs.size() > 1) {
+ cerr << "ODD: variable IO config for " << info->unique_id << endl;
+ }
+
+ plugs.push_back (info);
+
+ } else {
+ error << string_compose (_("Cannot get I/O configuration info for AU %1"), info->name) << endmsg;
+ }
+
+ comp = FindNextComponent (comp, &desc);
+ }
+}
+
+bool
+AUPluginInfo::cached_io_configuration (const std::string& unique_id,
+ UInt32 version,
+ CAComponent& comp,
+ AUPluginCachedInfo& cinfo,
+ const std::string& name)
+{
+ std::string id;
+ char buf[32];
+
+ /* concatenate unique ID with version to provide a key for cached info lookup.
+ this ensures we don't get stale information, or should if plugin developers
+ follow Apple "guidelines".
+ */
+
+ snprintf (buf, sizeof (buf), "%u", version);
+ id = unique_id;
+ id += '/';
+ id += buf;
+
+ CachedInfoMap::iterator cim = cached_info.find (id);
+
+ if (cim != cached_info.end()) {
+ cinfo = cim->second;
+ return true;
+ }
+
+ CAAudioUnit unit;
+ AUChannelInfo* channel_info;
+ UInt32 cnt;
+ int ret;
+
+ ARDOUR::BootMessage (string_compose (_("Checking AudioUnit: %1"), name));
+
+ try {
+
+ if (CAAudioUnit::Open (comp, unit) != noErr) {
+ return false;
+ }
+
+ } catch (...) {
+
+ warning << string_compose (_("Could not load AU plugin %1 - ignored"), name) << endmsg;
+ cerr << string_compose (_("Could not load AU plugin %1 - ignored"), name) << endl;
+ return false;
+
+ }
+
+ if ((ret = unit.GetChannelInfo (&channel_info, cnt)) < 0) {
+ return false;
+ }
+
+ if (ret > 0) {
+ /* no explicit info available */
+
+ cinfo.io_configs.push_back (pair<int,int> (-1, -1));
+
+ } else {
+
+ /* store each configuration */
+
+ for (uint32_t n = 0; n < cnt; ++n) {
+ cinfo.io_configs.push_back (pair<int,int> (channel_info[n].inChannels,
+ channel_info[n].outChannels));
+ }
+
+ free (channel_info);
+ }
+
+ add_cached_info (id, cinfo);
+ save_cached_info ();
+
+ return true;
+}
+
+void
+AUPluginInfo::add_cached_info (const std::string& id, AUPluginCachedInfo& cinfo)
+{
+ cached_info[id] = cinfo;
+}
+
+void
+AUPluginInfo::save_cached_info ()
+{
+ XMLNode* node;
+
+ node = new XMLNode (X_("AudioUnitPluginCache"));
+
+ for (map<string,AUPluginCachedInfo>::iterator i = cached_info.begin(); i != cached_info.end(); ++i) {
+ XMLNode* parent = new XMLNode (X_("plugin"));
+ parent->add_property ("id", i->first);
+ node->add_child_nocopy (*parent);
+
+ for (vector<pair<int, int> >::iterator j = i->second.io_configs.begin(); j != i->second.io_configs.end(); ++j) {
+
+ XMLNode* child = new XMLNode (X_("io"));
+ char buf[32];
+
+ snprintf (buf, sizeof (buf), "%d", j->first);
+ child->add_property (X_("in"), buf);
+ snprintf (buf, sizeof (buf), "%d", j->second);
+ child->add_property (X_("out"), buf);
+ parent->add_child_nocopy (*child);
+ }
+
+ }
+
+ Glib::ustring path = au_cache_path ();
+ XMLTree tree;
+
+ tree.set_root (node);
+
+ if (!tree.write (path)) {
+ error << string_compose (_("could not save AU cache to %1"), path) << endmsg;
+ unlink (path.c_str());
+ }
+}
+
+int
+AUPluginInfo::load_cached_info ()
+{
+ Glib::ustring path = au_cache_path ();
+ XMLTree tree;
+
+ if (!Glib::file_test (path, Glib::FILE_TEST_EXISTS)) {
+ return 0;
+ }
+
+ tree.read (path);
+ const XMLNode* root (tree.root());
+
+ if (root->name() != X_("AudioUnitPluginCache")) {
+ return -1;
+ }
+
+ cached_info.clear ();
+
+ const XMLNodeList children = root->children();
+
+ for (XMLNodeConstIterator iter = children.begin(); iter != children.end(); ++iter) {
+
+ const XMLNode* child = *iter;
+
+ if (child->name() == X_("plugin")) {
+
+ const XMLNode* gchild;
+ const XMLNodeList gchildren = child->children();
+ const XMLProperty* prop = child->property (X_("id"));
+
+ if (!prop) {
+ continue;
+ }
+
+ std::string id = prop->value();
+ AUPluginCachedInfo cinfo;
+
+ for (XMLNodeConstIterator giter = gchildren.begin(); giter != gchildren.end(); giter++) {
+
+ gchild = *giter;
+
+ if (gchild->name() == X_("io")) {
+
+ const XMLProperty* iprop;
+ const XMLProperty* oprop;
+
+ if (((iprop = gchild->property (X_("in"))) != 0) &&
+ ((oprop = gchild->property (X_("out"))) != 0)) {
+ const int in = atoi (iprop->value());
+ const int out = atoi (iprop->value());
+
+ cinfo.io_configs.push_back (pair<int,int> (in, out));
+ }
+ }
+ }
+
+ if (cinfo.io_configs.size()) {
+ add_cached_info (id, cinfo);
+ }
+ }
+ }
+
+ return 0;
+}
+
+void
+AUPluginInfo::get_names (CAComponentDescription& comp_desc, std::string& name, Glib::ustring& 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) {