1 /*
2  * Copyright (C) 2006-2016 David Robillard <d@drobilla.net>
3  * Copyright (C) 2007-2017 Paul Davis <paul@linuxaudiosystems.com>
4  * Copyright (C) 2010 Carl Hetherington <carl@carlh.net>
5  * Copyright (C) 2013-2019 Robin Gareus <robin@gareus.org>
6  * Copyright (C) 2014-2017 Tim Mayberry <mojofunk@gmail.com>
7  * Copyright (C) 2015-2016 Nick Mainsbridge <mainsbridge@gmail.com>
8  * Copyright (C) 2018 Julien "_FrnchFrgg_" RIVAUD <frnchfrgg@free.fr>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License along
21  * with this program; if not, write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23  */
24 
25 #include <sstream>
26 #include <fstream>
27 #include <errno.h>
28 #include <string.h>
29 #include <math.h>
30 #include <ctype.h>
31 
32 #include "pbd/gstdio_compat.h"
33 #include "pbd/transmitter.h"
34 #include "pbd/xml++.h"
35 #include "pbd/convert.h"
36 #include "pbd/whitespace.h"
37 #include "pbd/file_utils.h"
38 #include "pbd/locale_guard.h"
39 
40 #include <glibmm/threads.h>
41 #include <glibmm/fileutils.h>
42 #include <glibmm/miscutils.h>
43 
44 #include "ardour/ardour.h"
45 #include "ardour/audio_unit.h"
46 #include "ardour/audioengine.h"
47 #include "ardour/audio_buffer.h"
48 #include "ardour/debug.h"
49 #include "ardour/filesystem_paths.h"
50 #include "ardour/io.h"
51 #include "ardour/midi_buffer.h"
52 #include "ardour/route.h"
53 #include "ardour/session.h"
54 #include "ardour/tempo.h"
55 #include "ardour/utils.h"
56 
57 #include "CAAudioUnit.h"
58 #include "CAAUParameter.h"
59 
60 #include <CoreFoundation/CoreFoundation.h>
61 #include <CoreServices/CoreServices.h>
62 #include <AudioUnit/AudioUnit.h>
63 #include <AudioToolbox/AudioUnitUtilities.h>
64 #ifdef WITH_CARBON
65 #include <Carbon/Carbon.h>
66 #endif
67 
68 #ifdef COREAUDIO105
69 #define ArdourComponent Component
70 #define ArdourDescription ComponentDescription
71 #define ArdourFindNext FindNextComponent
72 #else
73 #define ArdourComponent AudioComponent
74 #define ArdourDescription AudioComponentDescription
75 #define ArdourFindNext AudioComponentFindNext
76 #endif
77 
78 #include "pbd/i18n.h"
79 
80 using namespace std;
81 using namespace PBD;
82 using namespace ARDOUR;
83 
84 AUPluginInfo::CachedInfoMap AUPluginInfo::cached_info;
85 
86 static string preset_search_path = "/Library/Audio/Presets:/Network/Library/Audio/Presets";
87 static string preset_suffix = ".aupreset";
88 static bool preset_search_path_initialized = false;
89 FILE * AUPluginInfo::_crashlog_fd = NULL;
90 bool AUPluginInfo::_scan_only = true;
91 
92 
au_blacklist(std::string id)93 static void au_blacklist (std::string id)
94 {
95 	string fn = Glib::build_filename (ARDOUR::user_cache_directory(), "au_blacklist.txt");
96 	FILE * blacklist_fd = NULL;
97 	if (! (blacklist_fd = fopen(fn.c_str(), "a"))) {
98 		PBD::error << "Cannot append to AU blacklist for '"<< id <<"'\n";
99 		return;
100 	}
101 	assert(id.find("\n") == string::npos);
102 	fprintf(blacklist_fd, "%s\n", id.c_str());
103 	::fclose(blacklist_fd);
104 }
105 
au_unblacklist(std::string id)106 static void au_unblacklist (std::string id)
107 {
108 	string fn = Glib::build_filename (ARDOUR::user_cache_directory(), "au_blacklist.txt");
109 	if (!Glib::file_test (fn, Glib::FILE_TEST_EXISTS)) {
110 		PBD::warning << "Expected Blacklist file does not exist.\n";
111 		return;
112 	}
113 
114 	std::string bl;
115 	{
116 		std::ifstream ifs(fn.c_str());
117 		bl.assign ((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));
118 	}
119 
120 	::g_unlink (fn.c_str());
121 
122 	assert (!Glib::file_test (fn, Glib::FILE_TEST_EXISTS));
123 	assert(id.find("\n") == string::npos);
124 
125 	id += "\n"; // add separator
126 	const size_t rpl = bl.find(id);
127 	if (rpl != string::npos) {
128 		bl.replace(rpl, id.size(), "");
129 	}
130 	if (bl.empty()) {
131 		return;
132 	}
133 
134 	FILE * blacklist_fd = NULL;
135 	if (! (blacklist_fd = fopen(fn.c_str(), "w"))) {
136 		PBD::error << "Cannot open AU blacklist.\n";
137 		return;
138 	}
139 	fprintf(blacklist_fd, "%s", bl.c_str());
140 	::fclose(blacklist_fd);
141 }
142 
is_blacklisted(std::string id)143 static bool is_blacklisted (std::string id)
144 {
145 	string fn = Glib::build_filename (ARDOUR::user_cache_directory(), "au_blacklist.txt");
146 	if (!Glib::file_test (fn, Glib::FILE_TEST_EXISTS)) {
147 		return false;
148 	}
149 	std::string bl;
150 	std::ifstream ifs(fn.c_str());
151 	bl.assign ((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));
152 
153 	assert(id.find("\n") == string::npos);
154 
155 	id += "\n"; // add separator
156 	const size_t rpl = bl.find(id);
157 	if (rpl != string::npos) {
158 		return true;
159 	}
160 	return false;
161 }
162 
163 
164 
165 static OSStatus
_render_callback(void * userData,AudioUnitRenderActionFlags * ioActionFlags,const AudioTimeStamp * inTimeStamp,UInt32 inBusNumber,UInt32 inNumberSamples,AudioBufferList * ioData)166 _render_callback(void *userData,
167 		 AudioUnitRenderActionFlags *ioActionFlags,
168 		 const AudioTimeStamp    *inTimeStamp,
169 		 UInt32       inBusNumber,
170 		 UInt32       inNumberSamples,
171 		 AudioBufferList*       ioData)
172 {
173 	if (userData) {
174 		return ((AUPlugin*)userData)->render_callback (ioActionFlags, inTimeStamp, inBusNumber, inNumberSamples, ioData);
175 	}
176 	return paramErr;
177 }
178 
179 static OSStatus
_get_beat_and_tempo_callback(void * userData,Float64 * outCurrentBeat,Float64 * outCurrentTempo)180 _get_beat_and_tempo_callback (void*    userData,
181                               Float64* outCurrentBeat,
182                               Float64* outCurrentTempo)
183 {
184 	if (userData) {
185 		return ((AUPlugin*)userData)->get_beat_and_tempo_callback (outCurrentBeat, outCurrentTempo);
186 	}
187 
188 	return paramErr;
189 }
190 
191 static OSStatus
_get_musical_time_location_callback(void * userData,UInt32 * outDeltaSampleOffsetToNextBeat,Float32 * outTimeSig_Numerator,UInt32 * outTimeSig_Denominator,Float64 * outCurrentMeasureDownBeat)192 _get_musical_time_location_callback (void *     userData,
193                                      UInt32 *   outDeltaSampleOffsetToNextBeat,
194                                      Float32 *  outTimeSig_Numerator,
195                                      UInt32 *   outTimeSig_Denominator,
196                                      Float64 *  outCurrentMeasureDownBeat)
197 {
198 	if (userData) {
199 		return ((AUPlugin*)userData)->get_musical_time_location_callback (outDeltaSampleOffsetToNextBeat,
200 										  outTimeSig_Numerator,
201 										  outTimeSig_Denominator,
202 										  outCurrentMeasureDownBeat);
203 	}
204 	return paramErr;
205 }
206 
207 static OSStatus
_get_transport_state_callback(void * userData,Boolean * outIsPlaying,Boolean * outTransportStateChanged,Float64 * outCurrentSampleInTimeLine,Boolean * outIsCycling,Float64 * outCycleStartBeat,Float64 * outCycleEndBeat)208 _get_transport_state_callback (void*     userData,
209 			       Boolean*  outIsPlaying,
210 			       Boolean*  outTransportStateChanged,
211 			       Float64*  outCurrentSampleInTimeLine,
212 			       Boolean*  outIsCycling,
213 			       Float64*  outCycleStartBeat,
214 			       Float64*  outCycleEndBeat)
215 {
216 	if (userData) {
217 		return ((AUPlugin*)userData)->get_transport_state_callback (
218 			outIsPlaying, outTransportStateChanged,
219 			outCurrentSampleInTimeLine, outIsCycling,
220 			outCycleStartBeat, outCycleEndBeat);
221 	}
222 	return paramErr;
223 }
224 
225 
226 static int
save_property_list(CFPropertyListRef propertyList,Glib::ustring path)227 save_property_list (CFPropertyListRef propertyList, Glib::ustring path)
228 
229 {
230 	CFDataRef xmlData;
231 	int fd;
232 
233 	// Convert the property list into XML data.
234 
235 	xmlData = CFPropertyListCreateXMLData( kCFAllocatorDefault, propertyList);
236 
237 	if (!xmlData) {
238 		error << _("Could not create XML version of property list") << endmsg;
239 		return -1;
240 	}
241 
242 	// Write the XML data to the file.
243 
244 	fd = open (path.c_str(), O_WRONLY|O_CREAT|O_EXCL, 0664);
245 	while (fd < 0) {
246 		if (errno == EEXIST) {
247 			error << string_compose (_("Preset file %1 exists; not overwriting"),
248 			                         path) << endmsg;
249 		} else {
250 			error << string_compose (_("Cannot open preset file %1 (%2)"),
251 			                         path, strerror (errno)) << endmsg;
252 		}
253 		CFRelease (xmlData);
254 		return -1;
255 	}
256 
257 	size_t cnt = CFDataGetLength (xmlData);
258 
259 	if (write (fd, CFDataGetBytePtr (xmlData), cnt) != (ssize_t) cnt) {
260 		CFRelease (xmlData);
261 		close (fd);
262 		return -1;
263 	}
264 
265 	close (fd);
266 	return 0;
267 }
268 
269 
270 static CFPropertyListRef
load_property_list(Glib::ustring path)271 load_property_list (Glib::ustring path)
272 {
273 	int fd;
274 	CFPropertyListRef propertyList = 0;
275 	CFDataRef         xmlData;
276 	CFStringRef       errorString;
277 
278 	// Read the XML file.
279 
280 	if ((fd = open (path.c_str(), O_RDONLY)) < 0) {
281 		return propertyList;
282 
283 	}
284 
285 	off_t len = lseek (fd, 0, SEEK_END);
286 	char* buf = new char[len];
287 	lseek (fd, 0, SEEK_SET);
288 
289 	if (read (fd, buf, len) != len) {
290 		delete [] buf;
291 		close (fd);
292 		return propertyList;
293 	}
294 
295 	close (fd);
296 
297 	xmlData = CFDataCreateWithBytesNoCopy (kCFAllocatorDefault, (UInt8*) buf, len, kCFAllocatorNull);
298 
299 	// Reconstitute the dictionary using the XML data.
300 
301 	propertyList = CFPropertyListCreateFromXMLData( kCFAllocatorDefault,
302 							xmlData,
303 							kCFPropertyListImmutable,
304 							&errorString);
305 
306 	CFRelease (xmlData);
307 	delete [] buf;
308 
309 	return propertyList;
310 }
311 
312 //-----------------------------------------------------------------------------
313 static void
set_preset_name_in_plist(CFPropertyListRef plist,string preset_name)314 set_preset_name_in_plist (CFPropertyListRef plist, string preset_name)
315 {
316 	if (!plist) {
317 		return;
318 	}
319 	CFStringRef pn = CFStringCreateWithCString (kCFAllocatorDefault, preset_name.c_str(), kCFStringEncodingUTF8);
320 
321 	if (CFGetTypeID (plist) == CFDictionaryGetTypeID()) {
322 		CFDictionarySetValue ((CFMutableDictionaryRef)plist, CFSTR(kAUPresetNameKey), pn);
323 	}
324 
325 	CFRelease (pn);
326 }
327 
328 //-----------------------------------------------------------------------------
329 static std::string
get_preset_name_in_plist(CFPropertyListRef plist)330 get_preset_name_in_plist (CFPropertyListRef plist)
331 {
332 	std::string ret;
333 
334 	if (!plist) {
335 		return ret;
336 	}
337 
338 	if (CFGetTypeID (plist) == CFDictionaryGetTypeID()) {
339 		const void *p = CFDictionaryGetValue ((CFMutableDictionaryRef)plist, CFSTR(kAUPresetNameKey));
340 		if (p) {
341 			CFStringRef str = (CFStringRef) p;
342 			int len = CFStringGetLength(str);
343 			len =  (len * 2) + 1;
344 			char local_buffer[len];
345 			if (CFStringGetCString (str, local_buffer, len, kCFStringEncodingUTF8)) {
346 				ret = local_buffer;
347 			}
348 		}
349 	}
350 	return ret;
351 }
352 
353 //--------------------------------------------------------------------------
354 // general implementation for ComponentDescriptionsMatch() and ComponentDescriptionsMatch_Loosely()
355 // if inIgnoreType is true, then the type code is ignored in the ComponentDescriptions
356 Boolean ComponentDescriptionsMatch_General(const ArdourDescription * inComponentDescription1, const ArdourDescription * inComponentDescription2, Boolean inIgnoreType);
ComponentDescriptionsMatch_General(const ArdourDescription * inComponentDescription1,const ArdourDescription * inComponentDescription2,Boolean inIgnoreType)357 Boolean ComponentDescriptionsMatch_General(const ArdourDescription * inComponentDescription1, const ArdourDescription * inComponentDescription2, Boolean inIgnoreType)
358 {
359 	if ( (inComponentDescription1 == NULL) || (inComponentDescription2 == NULL) )
360 		return FALSE;
361 
362 	if ( (inComponentDescription1->componentSubType == inComponentDescription2->componentSubType)
363 			&& (inComponentDescription1->componentManufacturer == inComponentDescription2->componentManufacturer) )
364 	{
365 		// only sub-type and manufacturer IDs need to be equal
366 		if (inIgnoreType)
367 			return TRUE;
368 		// type, sub-type, and manufacturer IDs all need to be equal in order to call this a match
369 		else if (inComponentDescription1->componentType == inComponentDescription2->componentType)
370 			return TRUE;
371 	}
372 
373 	return FALSE;
374 }
375 
376 //--------------------------------------------------------------------------
377 // general implementation for ComponentAndDescriptionMatch() and ComponentAndDescriptionMatch_Loosely()
378 // if inIgnoreType is true, then the type code is ignored in the ComponentDescriptions
379 Boolean ComponentAndDescriptionMatch_General(ArdourComponent inComponent, const ArdourDescription * inComponentDescription, Boolean inIgnoreType);
ComponentAndDescriptionMatch_General(ArdourComponent inComponent,const ArdourDescription * inComponentDescription,Boolean inIgnoreType)380 Boolean ComponentAndDescriptionMatch_General(ArdourComponent inComponent, const ArdourDescription * inComponentDescription, Boolean inIgnoreType)
381 {
382 	OSErr status;
383 	ArdourDescription desc;
384 
385 	if ( (inComponent == NULL) || (inComponentDescription == NULL) )
386 		return FALSE;
387 
388 	// get the ComponentDescription of the input Component
389 #ifdef COREAUDIO105
390 	status = GetComponentInfo(inComponent, &desc, NULL, NULL, NULL);
391 #else
392 	status = AudioComponentGetDescription (inComponent, &desc);
393 #endif
394 	if (status != noErr)
395 		return FALSE;
396 
397 	// check if the Component's ComponentDescription matches the input ComponentDescription
398 	return ComponentDescriptionsMatch_General(&desc, inComponentDescription, inIgnoreType);
399 }
400 
401 //--------------------------------------------------------------------------
402 // determine if 2 ComponentDescriptions are basically equal
403 // (by that, I mean that the important identifying values are compared,
404 // but not the ComponentDescription flags)
ComponentDescriptionsMatch(const ArdourDescription * inComponentDescription1,const ArdourDescription * inComponentDescription2)405 Boolean ComponentDescriptionsMatch(const ArdourDescription * inComponentDescription1, const ArdourDescription * inComponentDescription2)
406 {
407 	return ComponentDescriptionsMatch_General(inComponentDescription1, inComponentDescription2, FALSE);
408 }
409 
410 //--------------------------------------------------------------------------
411 // determine if 2 ComponentDescriptions have matching sub-type and manufacturer codes
ComponentDescriptionsMatch_Loose(const ArdourDescription * inComponentDescription1,const ArdourDescription * inComponentDescription2)412 Boolean ComponentDescriptionsMatch_Loose(const ArdourDescription * inComponentDescription1, const ArdourDescription * inComponentDescription2)
413 {
414 	return ComponentDescriptionsMatch_General(inComponentDescription1, inComponentDescription2, TRUE);
415 }
416 
417 //--------------------------------------------------------------------------
418 // determine if a ComponentDescription basically matches that of a particular Component
ComponentAndDescriptionMatch(ArdourComponent inComponent,const ArdourDescription * inComponentDescription)419 Boolean ComponentAndDescriptionMatch(ArdourComponent inComponent, const ArdourDescription * inComponentDescription)
420 {
421 	return ComponentAndDescriptionMatch_General(inComponent, inComponentDescription, FALSE);
422 }
423 
424 //--------------------------------------------------------------------------
425 // determine if a ComponentDescription matches only the sub-type and manufacturer codes of a particular Component
ComponentAndDescriptionMatch_Loosely(ArdourComponent inComponent,const ArdourDescription * inComponentDescription)426 Boolean ComponentAndDescriptionMatch_Loosely(ArdourComponent inComponent, const ArdourDescription * inComponentDescription)
427 {
428 	return ComponentAndDescriptionMatch_General(inComponent, inComponentDescription, TRUE);
429 }
430 
431 
AUPlugin(AudioEngine & engine,Session & session,boost::shared_ptr<CAComponent> _comp)432 AUPlugin::AUPlugin (AudioEngine& engine, Session& session, boost::shared_ptr<CAComponent> _comp)
433 	: Plugin (engine, session)
434 	, comp (_comp)
435 	, unit (new CAAudioUnit)
436 	, initialized (false)
437 	, _last_nframes (0)
438 	, _requires_fixed_size_buffers (false)
439 	, buffers (0)
440 	, variable_inputs (false)
441 	, variable_outputs (false)
442 	, configured_input_busses (0)
443 	, configured_output_busses (0)
444 	, bus_inputs (0)
445 	, bus_inused (0)
446 	, bus_outputs (0)
447 	, input_maxbuf (0)
448 	, input_offset (0)
449 	, cb_offsets (0)
450 	, input_buffers (0)
451 	, input_map (0)
452 	, samples_processed (0)
453 	, _parameter_listener (0)
454 	, _parameter_listener_arg (0)
455 	, transport_sample (0)
456 	, transport_speed (0)
457 	, last_transport_speed (0.0)
458 	, preset_holdoff (0)
459 {
460 	if (!preset_search_path_initialized) {
461 		Glib::ustring p = Glib::get_home_dir();
462 		p += "/Library/Audio/Presets:";
463 		p += preset_search_path;
464 		preset_search_path = p;
465 		preset_search_path_initialized = true;
466 		DEBUG_TRACE (DEBUG::AudioUnitConfig, string_compose("AU Preset Path: %1\n", preset_search_path));
467 	}
468 
469 	init ();
470 }
471 
472 
AUPlugin(const AUPlugin & other)473 AUPlugin::AUPlugin (const AUPlugin& other)
474 	: Plugin (other)
475 	, comp (other.get_comp())
476 	, unit (new CAAudioUnit)
477 	, initialized (false)
478 	, _last_nframes (0)
479 	, _requires_fixed_size_buffers (false)
480 	, buffers (0)
481 	, variable_inputs (false)
482 	, variable_outputs (false)
483 	, configured_input_busses (0)
484 	, configured_output_busses (0)
485 	, bus_inputs (0)
486 	, bus_inused (0)
487 	, bus_outputs (0)
488 	, input_maxbuf (0)
489 	, input_offset (0)
490 	, cb_offsets (0)
491 	, input_buffers (0)
492 	, input_map (0)
493 	, samples_processed (0)
494 	, _parameter_listener (0)
495 	, _parameter_listener_arg (0)
496 	, transport_sample (0)
497 	, transport_speed (0)
498 	, last_transport_speed (0.0)
499 	, preset_holdoff (0)
500 
501 {
502 	init ();
503 
504 	XMLNode root (other.state_node_name ());
505 	other.add_state (&root);
506 	set_state (root, Stateful::loading_state_version);
507 
508 	for (size_t i = 0; i < descriptors.size(); ++i) {
509 		set_parameter (i, other.get_parameter (i), 0);
510 	}
511 }
512 
~AUPlugin()513 AUPlugin::~AUPlugin ()
514 {
515 	if (_parameter_listener) {
516 		AUListenerDispose (_parameter_listener);
517 		_parameter_listener = 0;
518 	}
519 
520 	if (unit) {
521 		DEBUG_TRACE (DEBUG::AudioUnitConfig, "about to call uninitialize in plugin destructor\n");
522 		unit->Uninitialize ();
523 	}
524 
525 	free (buffers);
526 	free (bus_inputs);
527 	free (bus_inused);
528 	free (bus_outputs);
529 	free (cb_offsets);
530 }
531 
532 void
discover_factory_presets()533 AUPlugin::discover_factory_presets ()
534 {
535 	CFArrayRef presets;
536 	UInt32 dataSize;
537 	Boolean isWritable;
538 	OSStatus err;
539 
540 	if ((err = unit->GetPropertyInfo (kAudioUnitProperty_FactoryPresets, kAudioUnitScope_Global, 0, &dataSize, &isWritable)) != 0) {
541 		DEBUG_TRACE (DEBUG::AudioUnitConfig, "no factory presets for AU\n");
542 		return;
543 	}
544 
545 	assert (dataSize == sizeof (presets));
546 
547 	if ((err = unit->GetProperty (kAudioUnitProperty_FactoryPresets, kAudioUnitScope_Global, 0, (void*) &presets, &dataSize)) != 0) {
548 		error << string_compose (_("cannot get factory preset info: errcode %1"), err) << endmsg;
549 		return;
550 	}
551 
552 	if (!presets) {
553 		return;
554 	}
555 
556 	CFIndex cnt = CFArrayGetCount (presets);
557 
558 	for (CFIndex i = 0; i < cnt; ++i) {
559 		AUPreset* preset = (AUPreset*) CFArrayGetValueAtIndex (presets, i);
560 
561 		string name = CFStringRefToStdString (preset->presetName);
562 		factory_preset_map[name] = preset->presetNumber;
563 		DEBUG_TRACE (DEBUG::AudioUnitConfig, string_compose("AU Factory Preset: %1 > %2\n", name, preset->presetNumber));
564 	}
565 
566 	CFRelease (presets);
567 }
568 
569 void
init()570 AUPlugin::init ()
571 {
572 	g_atomic_int_set (&_current_latency, UINT_MAX);
573 
574 	OSErr err;
575 	CFStringRef itemName;
576 
577 	/* these keep track of *configured* channel set up,
578 	   not potential set ups.
579 	*/
580 
581 	input_channels = -1;
582 	output_channels = -1;
583 	{
584 		CAComponentDescription temp;
585 #ifdef COREAUDIO105
586 		GetComponentInfo (comp.get()->Comp(), &temp, NULL, NULL, NULL);
587 #else
588 		AudioComponentGetDescription (comp.get()->Comp(), &temp);
589 #endif
590 		CFStringRef compTypeString = UTCreateStringForOSType(temp.componentType);
591 		CFStringRef compSubTypeString = UTCreateStringForOSType(temp.componentSubType);
592 		CFStringRef compManufacturerString = UTCreateStringForOSType(temp.componentManufacturer);
593 		itemName = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%@ - %@ - %@"),
594 				compTypeString, compManufacturerString, compSubTypeString);
595 		if (compTypeString != NULL) CFRelease(compTypeString);
596 		if (compSubTypeString != NULL) CFRelease(compSubTypeString);
597 		if (compManufacturerString != NULL) CFRelease(compManufacturerString);
598 	}
599 
600 	au_blacklist(CFStringRefToStdString(itemName));
601 
602 	try {
603 		DEBUG_TRACE (DEBUG::AudioUnitConfig, "opening AudioUnit\n");
604 		err = CAAudioUnit::Open (*(comp.get()), *unit);
605 	} catch (...) {
606 		error << _("Exception thrown during AudioUnit plugin loading - plugin ignored") << endmsg;
607 		throw failed_constructor();
608 	}
609 
610 	if (err != noErr) {
611 		error << _("AudioUnit: Could not convert CAComponent to CAAudioUnit") << endmsg;
612 		throw failed_constructor ();
613 	}
614 
615 	DEBUG_TRACE (DEBUG::AudioUnitConfig, "count global elements\n");
616 	unit->GetElementCount (kAudioUnitScope_Global, global_elements);
617 	DEBUG_TRACE (DEBUG::AudioUnitConfig, "count input elements\n");
618 	unit->GetElementCount (kAudioUnitScope_Input, input_elements);
619 	DEBUG_TRACE (DEBUG::AudioUnitConfig, "count output elements\n");
620 	unit->GetElementCount (kAudioUnitScope_Output, output_elements);
621 
622 
623 	if (input_elements > 0) {
624 		cb_offsets = (samplecnt_t*) calloc (input_elements, sizeof(samplecnt_t));
625 		bus_inputs = (uint32_t*) calloc (input_elements, sizeof(uint32_t));
626 		bus_inused = (uint32_t*) calloc (input_elements, sizeof(uint32_t));
627 	}
628 	if (output_elements > 0) {
629 		bus_outputs = (uint32_t*) calloc (output_elements, sizeof(uint32_t));
630 	}
631 
632 	for (size_t i = 0; i < output_elements; ++i) {
633 		unit->Reset (kAudioUnitScope_Output, i);
634 		AudioStreamBasicDescription fmt;
635 		err = unit->GetFormat (kAudioUnitScope_Output, i, fmt);
636 		if (err == noErr) {
637 			bus_outputs[i] = fmt.mChannelsPerFrame;
638 		}
639 		CFStringRef name;
640 		UInt32 sz = sizeof (CFStringRef);
641 		if (AudioUnitGetProperty (unit->AU(), kAudioUnitProperty_ElementName, kAudioUnitScope_Output,
642 					i, &name, &sz) == noErr
643 				&& sz > 0) {
644 			_bus_name_out.push_back (CFStringRefToStdString (name));
645 			CFRelease(name);
646 		} else {
647 			_bus_name_out.push_back (string_compose ("Audio-Bus %1", i));
648 		}
649 	}
650 
651 	for (size_t i = 0; i < input_elements; ++i) {
652 		unit->Reset (kAudioUnitScope_Input, i);
653 		AudioStreamBasicDescription fmt;
654 		err = unit->GetFormat (kAudioUnitScope_Input, i, fmt);
655 		if (err == noErr) {
656 			bus_inputs[i] = fmt.mChannelsPerFrame;
657 			bus_inused[i] = bus_inputs[i];
658 		}
659 		CFStringRef name;
660 		UInt32 sz = sizeof (CFStringRef);
661 		if (AudioUnitGetProperty (unit->AU(), kAudioUnitProperty_ElementName, kAudioUnitScope_Input,
662 					i, &name, &sz) == noErr
663 				&& sz > 0) {
664 			_bus_name_in.push_back (CFStringRefToStdString (name));
665 			CFRelease(name);
666 		} else {
667 			_bus_name_in.push_back (string_compose ("Audio-Bus %1", i));
668 		}
669 	}
670 
671 	/* tell the plugin about tempo/meter/transport callbacks in case it wants them */
672 
673 	HostCallbackInfo info;
674 	memset (&info, 0, sizeof (HostCallbackInfo));
675 	info.hostUserData = this;
676 	info.beatAndTempoProc = _get_beat_and_tempo_callback;
677 	info.musicalTimeLocationProc = _get_musical_time_location_callback;
678 	info.transportStateProc = _get_transport_state_callback;
679 
680 	//ignore result of this - don't care if the property isn't supported
681 	DEBUG_TRACE (DEBUG::AudioUnitConfig, "set host callbacks in global scope\n");
682 	unit->SetProperty (kAudioUnitProperty_HostCallbacks,
683 			   kAudioUnitScope_Global,
684 			   0, //elementID
685 			   &info,
686 			   sizeof (HostCallbackInfo));
687 
688 	if (set_block_size (_session.get_block_size())) {
689 		error << _("AUPlugin: cannot set processing block size") << endmsg;
690 		throw failed_constructor();
691 	}
692 
693 	create_parameter_listener (AUPlugin::_parameter_change_listener, this, 0.05);
694 	discover_parameters ();
695 	discover_factory_presets ();
696 
697 	// Plugin::setup_controls ();
698 
699 	au_unblacklist(CFStringRefToStdString(itemName));
700 	if (itemName != NULL) CFRelease(itemName);
701 }
702 
703 void
discover_parameters()704 AUPlugin::discover_parameters ()
705 {
706 	/* discover writable parameters */
707 
708 	AudioUnitScope scopes[] = {
709 		kAudioUnitScope_Global,
710 		kAudioUnitScope_Output,
711 		kAudioUnitScope_Input
712 	};
713 
714 	descriptors.clear ();
715 
716 	for (uint32_t i = 0; i < sizeof (scopes) / sizeof (scopes[0]); ++i) {
717 
718 		AUParamInfo param_info (unit->AU(), false, /* include read only */ true, scopes[i]);
719 
720 		for (uint32_t i = 0; i < param_info.NumParams(); ++i) {
721 
722 			AUParameterDescriptor d;
723 
724 			d.id = param_info.ParamID (i);
725 
726 			const CAAUParameter* param = param_info.GetParamInfo (d.id);
727 			const AudioUnitParameterInfo& info (param->ParamInfo());
728 
729 			const int len = CFStringGetLength (param->GetName());
730 			char local_buffer[len*2];
731 			Boolean good = CFStringGetCString (param->GetName(), local_buffer ,len*2 , kCFStringEncodingUTF8);
732 			if (!good) {
733 				d.label = "???";
734 			} else {
735 				d.label = local_buffer;
736 			}
737 
738 			d.scope = param_info.GetScope ();
739 			d.element = param_info.GetElement ();
740 
741 			/* info.units to consider */
742 			/*
743 			  kAudioUnitParameterUnit_Generic             = 0
744 			  kAudioUnitParameterUnit_Indexed             = 1
745 			  kAudioUnitParameterUnit_Boolean             = 2
746 			  kAudioUnitParameterUnit_Percent             = 3
747 			  kAudioUnitParameterUnit_Seconds             = 4
748 			  kAudioUnitParameterUnit_SampleFrames        = 5
749 			  kAudioUnitParameterUnit_Phase               = 6
750 			  kAudioUnitParameterUnit_Rate                = 7
751 			  kAudioUnitParameterUnit_Hertz               = 8
752 			  kAudioUnitParameterUnit_Cents               = 9
753 			  kAudioUnitParameterUnit_RelativeSemiTones   = 10
754 			  kAudioUnitParameterUnit_MIDINoteNumber      = 11
755 			  kAudioUnitParameterUnit_MIDIController      = 12
756 			  kAudioUnitParameterUnit_Decibels            = 13
757 			  kAudioUnitParameterUnit_LinearGain          = 14
758 			  kAudioUnitParameterUnit_Degrees             = 15
759 			  kAudioUnitParameterUnit_EqualPowerCrossfade = 16
760 			  kAudioUnitParameterUnit_MixerFaderCurve1    = 17
761 			  kAudioUnitParameterUnit_Pan                 = 18
762 			  kAudioUnitParameterUnit_Meters              = 19
763 			  kAudioUnitParameterUnit_AbsoluteCents       = 20
764 			  kAudioUnitParameterUnit_Octaves             = 21
765 			  kAudioUnitParameterUnit_BPM                 = 22
766 			  kAudioUnitParameterUnit_Beats               = 23
767 			  kAudioUnitParameterUnit_Milliseconds        = 24
768 			  kAudioUnitParameterUnit_Ratio               = 25
769 			*/
770 
771 			/* info.flags to consider */
772 
773 			/*
774 
775 			  kAudioUnitParameterFlag_CFNameRelease       = (1L << 4)
776 			  kAudioUnitParameterFlag_HasClump            = (1L << 20)
777 			  kAudioUnitParameterFlag_HasName             = (1L << 21)
778 			  kAudioUnitParameterFlag_DisplayLogarithmic  = (1L << 22)
779 			  kAudioUnitParameterFlag_IsHighResolution    = (1L << 23)
780 			  kAudioUnitParameterFlag_NonRealTime         = (1L << 24)
781 			  kAudioUnitParameterFlag_CanRamp             = (1L << 25)
782 			  kAudioUnitParameterFlag_ExpertMode          = (1L << 26)
783 			  kAudioUnitParameterFlag_HasCFNameString     = (1L << 27)
784 			  kAudioUnitParameterFlag_IsGlobalMeta        = (1L << 28)
785 			  kAudioUnitParameterFlag_IsElementMeta       = (1L << 29)
786 			  kAudioUnitParameterFlag_IsReadable          = (1L << 30)
787 			  kAudioUnitParameterFlag_IsWritable          = (1L << 31)
788 			*/
789 
790 			d.lower = info.minValue;
791 			d.upper = info.maxValue;
792 			d.normal = info.defaultValue;
793 
794 			d.integer_step = (info.unit == kAudioUnitParameterUnit_Indexed);
795 			d.toggled = (info.unit == kAudioUnitParameterUnit_Boolean) ||
796 				(d.integer_step && ((d.upper - d.lower) == 1.0));
797 			d.sr_dependent = (info.unit == kAudioUnitParameterUnit_SampleFrames);
798 			d.automatable = /* !d.toggled && -- ardour can automate toggles, can AU ? */
799 				!(info.flags & kAudioUnitParameterFlag_NonRealTime) &&
800 				(info.flags & kAudioUnitParameterFlag_IsWritable);
801 
802 			d.logarithmic = (info.flags & kAudioUnitParameterFlag_DisplayLogarithmic);
803 			d.au_unit = info.unit;
804 			switch (info.unit) {
805 			case kAudioUnitParameterUnit_Decibels:
806 				d.unit = ParameterDescriptor::DB;
807 				break;
808 			case kAudioUnitParameterUnit_MIDINoteNumber:
809 				d.unit = ParameterDescriptor::MIDI_NOTE;
810 				break;
811 			case kAudioUnitParameterUnit_Hertz:
812 				d.unit = ParameterDescriptor::HZ;
813 				break;
814 			}
815 
816 			d.update_steps();
817 
818 			descriptors.push_back (d);
819 
820 			uint32_t last_param = descriptors.size() - 1;
821 			parameter_map.insert (pair<uint32_t,uint32_t> (d.id, last_param));
822 			listen_to_parameter (last_param);
823 		}
824 	}
825 }
826 
827 
828 static unsigned int
four_ints_to_four_byte_literal(unsigned char n[4])829 four_ints_to_four_byte_literal (unsigned char n[4])
830 {
831 	/* this is actually implementation dependent. sigh. this is what gcc
832 	   and quite a few others do.
833 	 */
834 	return ((n[0] << 24) + (n[1] << 16) + (n[2] << 8) + n[3]);
835 }
836 
837 std::string
maybe_fix_broken_au_id(const std::string & id)838 AUPlugin::maybe_fix_broken_au_id (const std::string& id)
839 {
840 	if (isdigit (id[0])) {
841 		return id;
842 	}
843 
844 	/* ID format is xxxx-xxxx-xxxx
845 	   where x maybe \xNN or a printable character.
846 
847 	   Split at the '-' and and process each part into an integer.
848 	   Then put it back together.
849 	*/
850 
851 
852 	unsigned char nascent[4];
853 	const char* cstr = id.c_str();
854 	const char* estr = cstr + id.size();
855 	uint32_t n[3];
856 	int in;
857 	int next_int;
858 	char short_buf[3];
859 	stringstream s;
860 
861 	in = 0;
862 	next_int = 0;
863 	short_buf[2] = '\0';
864 
865 	while (*cstr && next_int < 4) {
866 
867 		if (*cstr == '\\') {
868 
869 			if (estr - cstr < 3) {
870 
871 				/* too close to the end for \xNN parsing: treat as literal characters */
872 
873 				nascent[in] = *cstr;
874 				++cstr;
875 				++in;
876 
877 			} else {
878 
879 				if (cstr[1] == 'x' && isxdigit (cstr[2]) && isxdigit (cstr[3])) {
880 
881 					/* parse \xNN */
882 
883 					memcpy (short_buf, &cstr[2], 2);
884 					nascent[in] = strtol (short_buf, NULL, 16);
885 					cstr += 4;
886 					++in;
887 
888 				} else {
889 
890 					/* treat as literal characters */
891 					nascent[in] = *cstr;
892 					++cstr;
893 					++in;
894 				}
895 			}
896 
897 		} else {
898 
899 			nascent[in] = *cstr;
900 			++cstr;
901 			++in;
902 		}
903 
904 		if (in && (in % 4 == 0)) {
905 			/* nascent is ready */
906 			n[next_int] = four_ints_to_four_byte_literal (nascent);
907 			in = 0;
908 			next_int++;
909 
910 			/* swallow space-hyphen-space */
911 
912 			if (next_int < 3) {
913 				++cstr;
914 				++cstr;
915 				++cstr;
916 			}
917 		}
918 	}
919 
920 	if (next_int != 3) {
921 		goto err;
922 	}
923 
924 	s << n[0] << '-' << n[1] << '-' << n[2];
925 
926 	return s.str();
927 
928 err:
929 	return string();
930 }
931 
932 string
unique_id() const933 AUPlugin::unique_id () const
934 {
935 	return AUPluginInfo::stringify_descriptor (comp->Desc());
936 }
937 
938 const char *
label() const939 AUPlugin::label () const
940 {
941 	return _info->name.c_str();
942 }
943 
944 uint32_t
parameter_count() const945 AUPlugin::parameter_count () const
946 {
947 	return descriptors.size();
948 }
949 
950 float
default_value(uint32_t port)951 AUPlugin::default_value (uint32_t port)
952 {
953 	if (port < descriptors.size()) {
954 		return descriptors[port].normal;
955 	}
956 
957 	return 0;
958 }
959 
960 samplecnt_t
plugin_latency() const961 AUPlugin::plugin_latency () const
962 {
963 	guint lat = g_atomic_int_get (&_current_latency);;
964 	if (lat == UINT_MAX) {
965 		lat = unit->Latency() * _session.sample_rate();
966 		g_atomic_int_set (&_current_latency, lat);
967 	}
968 	return lat;
969 }
970 
971 void
set_parameter(uint32_t which,float val,sampleoffset_t when)972 AUPlugin::set_parameter (uint32_t which, float val, sampleoffset_t when)
973 {
974 	if (which >= descriptors.size()) {
975 		return;
976 	}
977 
978 	if (get_parameter(which) == val) {
979 		return;
980 	}
981 
982 	const AUParameterDescriptor& d (descriptors[which]);
983 	DEBUG_TRACE (DEBUG::AudioUnitProcess, string_compose ("set parameter %1 in scope %2 element %3 to %4\n", d.id, d.scope, d.element, val));
984 	unit->SetParameter (d.id, d.scope, d.element, val);
985 
986 	/* tell the world what we did */
987 
988 	AudioUnitEvent theEvent;
989 
990 	theEvent.mEventType = kAudioUnitEvent_ParameterValueChange;
991 	theEvent.mArgument.mParameter.mAudioUnit = unit->AU();
992 	theEvent.mArgument.mParameter.mParameterID = d.id;
993 	theEvent.mArgument.mParameter.mScope = d.scope;
994 	theEvent.mArgument.mParameter.mElement = d.element;
995 
996 	DEBUG_TRACE (DEBUG::AudioUnitProcess, "notify about parameter change\n");
997         /* Note the 1st argument, which means "Don't notify us about a change we made ourselves" */
998         AUEventListenerNotify (_parameter_listener, NULL, &theEvent);
999 
1000 	Plugin::set_parameter (which, val, when);
1001 }
1002 
1003 float
get_parameter(uint32_t which) const1004 AUPlugin::get_parameter (uint32_t which) const
1005 {
1006 	float val = 0.0;
1007 	if (which < descriptors.size()) {
1008 		const AUParameterDescriptor& d (descriptors[which]);
1009 		DEBUG_TRACE (DEBUG::AudioUnitProcess, string_compose ("get value of parameter %1 in scope %2 element %3\n", d.id, d.scope, d.element));
1010 		unit->GetParameter(d.id, d.scope, d.element, val);
1011 	}
1012 	return val;
1013 }
1014 
1015 int
get_parameter_descriptor(uint32_t which,ParameterDescriptor & pd) const1016 AUPlugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor& pd) const
1017 {
1018 	if (which < descriptors.size()) {
1019 		pd = descriptors[which];
1020 		return 0;
1021 	}
1022 	return -1;
1023 }
1024 
1025 uint32_t
nth_parameter(uint32_t which,bool & ok) const1026 AUPlugin::nth_parameter (uint32_t which, bool& ok) const
1027 {
1028 	if (which < descriptors.size()) {
1029 		ok = true;
1030 		return which;
1031 	}
1032 	ok = false;
1033 	return 0;
1034 }
1035 
1036 void
activate()1037 AUPlugin::activate ()
1038 {
1039 	if (!initialized) {
1040 		OSErr err;
1041 		DEBUG_TRACE (DEBUG::AudioUnitConfig, "call Initialize in activate()\n");
1042 		if ((err = unit->Initialize()) != noErr) {
1043 			error << string_compose (_("AUPlugin: %1 cannot initialize plugin (err = %2)"), name(), err) << endmsg;
1044 		} else {
1045 			samples_processed = 0;
1046 			initialized = true;
1047 		}
1048 	}
1049 }
1050 
1051 void
deactivate()1052 AUPlugin::deactivate ()
1053 {
1054 	DEBUG_TRACE (DEBUG::AudioUnitConfig, "call Uninitialize in deactivate()\n");
1055 	unit->Uninitialize ();
1056 	initialized = false;
1057 }
1058 
1059 void
flush()1060 AUPlugin::flush ()
1061 {
1062 	DEBUG_TRACE (DEBUG::AudioUnitConfig, "call Reset in flush()\n");
1063 	unit->GlobalReset ();
1064 }
1065 
1066 bool
requires_fixed_size_buffers() const1067 AUPlugin::requires_fixed_size_buffers() const
1068 {
1069 	return _requires_fixed_size_buffers;
1070 }
1071 
1072 
1073 int
set_block_size(pframes_t nframes)1074 AUPlugin::set_block_size (pframes_t nframes)
1075 {
1076 	bool was_initialized = initialized;
1077 	UInt32 numSamples = nframes;
1078 	OSErr err;
1079 
1080 	if (initialized) {
1081 		deactivate ();
1082 	}
1083 
1084 	DEBUG_TRACE (DEBUG::AudioUnitConfig, string_compose ("set MaximumFramesPerSlice in global scope to %1\n", numSamples));
1085 	if ((err = unit->SetProperty (kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global,
1086 				      0, &numSamples, sizeof (numSamples))) != noErr) {
1087 		error << string_compose (_("AU: cannot set max samples (err = %1)"), err) << endmsg;
1088 		return -1;
1089 	}
1090 
1091 	if (was_initialized) {
1092 		activate ();
1093 	}
1094 
1095 	return 0;
1096 }
1097 
1098 bool
reconfigure_io(ChanCount in,ChanCount aux_in,ChanCount out)1099 AUPlugin::reconfigure_io (ChanCount in, ChanCount aux_in, ChanCount out)
1100 {
1101 	AudioStreamBasicDescription streamFormat;
1102 	bool was_initialized = initialized;
1103 
1104 	DEBUG_TRACE (DEBUG::AudioUnitConfig, string_compose ("AUPlugin::reconfigure_io %1 for in: %2 aux-in %3 out: %4 out\n", name(), in, aux_in, out));
1105 
1106 	//TODO handle cases of no-input, only sidechain
1107 	// (needs special-casing of configured_input_busses)
1108 	if (input_elements == 1 || in.n_audio () == 0) {
1109 		in += aux_in;
1110 		aux_in.reset ();
1111 	}
1112 
1113 	const int32_t audio_in = in.n_audio();
1114 	const int32_t audio_out = out.n_audio();
1115 
1116 	if (initialized) {
1117 		/* if we are already running with the requested i/o config, bail out here */
1118 		if ((audio_in + aux_in.n_audio () == input_channels) && (audio_out == output_channels)) {
1119 			return true;
1120 		} else {
1121 			deactivate ();
1122 		}
1123 	}
1124 
1125 	streamFormat.mSampleRate  = _session.sample_rate();
1126 	streamFormat.mFormatID    = kAudioFormatLinearPCM;
1127 	streamFormat.mFormatFlags = kAudioFormatFlagIsFloat|kAudioFormatFlagIsPacked|kAudioFormatFlagIsNonInterleaved;
1128 
1129 #ifdef __LITTLE_ENDIAN__
1130 	/* relax */
1131 #else
1132 	streamFormat.mFormatFlags |= kAudioFormatFlagIsBigEndian;
1133 #endif
1134 
1135 	streamFormat.mBitsPerChannel = 32;
1136 	streamFormat.mFramesPerPacket = 1;
1137 
1138 	/* apple says that for non-interleaved data, these
1139 	 * values always refer to a single channel.
1140 	 */
1141 	streamFormat.mBytesPerPacket = 4;
1142 	streamFormat.mBytesPerFrame = 4;
1143 
1144 	configured_input_busses  = 0;
1145 	configured_output_busses = 0;
1146 
1147 	/* reset busses */
1148 	for (size_t i = 0; i < output_elements; ++i) {
1149 		unit->Reset (kAudioUnitScope_Output, i);
1150 	}
1151 	for (size_t i = 0; i < input_elements; ++i) {
1152 		bus_inused[i] = 0;
1153 		unit->Reset (kAudioUnitScope_Input, i);
1154 		/* remove any input callbacks */
1155 		AURenderCallbackStruct renderCallbackInfo;
1156 		renderCallbackInfo.inputProc = 0;
1157 		renderCallbackInfo.inputProcRefCon = 0;
1158 		unit->SetProperty (kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, i, (void*) &renderCallbackInfo, sizeof(renderCallbackInfo));
1159 	}
1160 
1161 	/* now assign the channels to available busses */
1162 	uint32_t used_in = 0;
1163 	uint32_t used_out = 0;
1164 
1165 	if (input_elements == 0 || audio_in == 0) {
1166 		configured_input_busses = 0;
1167 	} else if (variable_inputs || input_elements == 1 || audio_in < bus_inputs[0]) {
1168 		/* we only ever use the first bus and configure it to match */
1169 		if (variable_inputs && input_elements > 1) {
1170 			info << string_compose (_("AU %1 has multiple input busses and variable port count."), name()) << endmsg;
1171 		}
1172 		streamFormat.mChannelsPerFrame = audio_in;
1173 		if (set_stream_format (kAudioUnitScope_Input, 0, streamFormat) != 0) {
1174 			warning << string_compose (_("AU %1 failed to reconfigure input: %2"), name(), audio_in) << endmsg;
1175 			return false;
1176 		}
1177 		bus_inused[0] = audio_in;
1178 		configured_input_busses = 1;
1179 		used_in = audio_in;
1180 	} else {
1181 		/* more inputs than the first bus' channel-count: distribute sequentially */
1182 		configured_input_busses = 0;
1183 		uint32_t remain = audio_in + aux_in.n_audio ();
1184 		aux_in.reset (); /* now taken care of */
1185 		for (uint32_t bus = 0; remain > 0 && bus < input_elements; ++bus) {
1186 			uint32_t cnt = std::min (remain, bus_inputs[bus]);
1187 			DEBUG_TRACE (DEBUG::AudioUnitConfig, string_compose ("%1 configure input bus: %2 chn: %3", name(), bus, cnt));
1188 
1189 			streamFormat.mChannelsPerFrame = cnt;
1190 			if (set_stream_format (kAudioUnitScope_Input, bus, streamFormat) != 0) {
1191 				if (cnt > 0) {
1192 					return false;
1193 				}
1194 			}
1195 			bus_inused[bus] = cnt;
1196 			used_in += cnt;
1197 			remain -= cnt;
1198 			if (cnt == 0) { continue; }
1199 			++configured_input_busses;
1200 		}
1201 	}
1202 
1203 	/* add additional busses, connect aux-inputs */
1204 	if (configured_input_busses == 1 && aux_in.n_audio () > 0 && input_elements > 1) {
1205 		uint32_t remain = aux_in.n_audio ();
1206 		for (uint32_t bus = 1; remain > 0 && bus < input_elements; ++bus) {
1207 			uint32_t cnt = std::min (remain, bus_inputs[bus]);
1208 			DEBUG_TRACE (DEBUG::AudioUnitConfig, string_compose ("%1 configure aux input bus: %2 chn: %3", name(), bus, cnt));
1209 
1210 			streamFormat.mChannelsPerFrame = cnt;
1211 			if (set_stream_format (kAudioUnitScope_Input, bus, streamFormat) != 0) {
1212 				if (cnt > 0) {
1213 					return false;
1214 				}
1215 			}
1216 			bus_inused[bus] = cnt;
1217 			used_in += cnt;
1218 			remain -= cnt;
1219 			if (cnt == 0) { continue; }
1220 			++configured_input_busses;
1221 		}
1222 	}
1223 
1224 	if (variable_outputs || output_elements == 1) {
1225 		if (output_elements > 1) {
1226 			warning << string_compose (_("AU %1 has multiple output busses and variable port count."), name()) << endmsg;
1227 		}
1228 
1229 		streamFormat.mChannelsPerFrame = audio_out;
1230 		if (set_stream_format (kAudioUnitScope_Output, 0, streamFormat) != 0) {
1231 			warning << string_compose (_("AU %1 failed to reconfigure output: %2"), name(), audio_out) << endmsg;
1232 			return false;
1233 		}
1234 		configured_output_busses = 1;
1235 		used_out = audio_out;
1236 	} else {
1237 		uint32_t remain = audio_out;
1238 		configured_output_busses = 0;
1239 		for (uint32_t bus = 0; remain > 0 && bus < output_elements; ++bus) {
1240 			uint32_t cnt = std::min (remain, bus_outputs[bus]);
1241 			if (cnt == 0) { continue; }
1242 			DEBUG_TRACE (DEBUG::AudioUnitConfig, string_compose ("%1 configure output bus: %2 chn: %3", name(), bus, cnt));
1243 			streamFormat.mChannelsPerFrame = cnt;
1244 			if (set_stream_format (kAudioUnitScope_Output, bus, streamFormat) != 0) {
1245 				return false;
1246 			}
1247 			used_out += cnt;
1248 			remain -= cnt;
1249 			++configured_output_busses;
1250 		}
1251 	}
1252 
1253 	for (size_t i = 0; used_in > 0 && i < configured_input_busses; ++i) {
1254 		/* setup render callback: the plugin calls this to get input data */
1255 		AURenderCallbackStruct renderCallbackInfo;
1256 		renderCallbackInfo.inputProc = _render_callback;
1257 		renderCallbackInfo.inputProcRefCon = this;
1258 		DEBUG_TRACE (DEBUG::AudioUnitConfig, "set render callback in input scope\n");
1259 		OSErr err;
1260 		if ((err = unit->SetProperty (kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input,
1261 						i, (void*) &renderCallbackInfo, sizeof(renderCallbackInfo))) != 0) {
1262 			error << string_compose (_("AU: %1 cannot install render callback (err = %2)"), name(), err) << endmsg;
1263 		}
1264 	}
1265 
1266 	free (buffers);
1267 	buffers = (AudioBufferList *) malloc (offsetof(AudioBufferList, mBuffers) + used_out * sizeof(::AudioBuffer));
1268 
1269 	input_channels = used_in;
1270 	output_channels = used_out;
1271 
1272 	/* reset plugin info to show currently configured state */
1273 	_info->n_inputs = ChanCount (DataType::AUDIO, used_in) + ChanCount (DataType::MIDI, _has_midi_input ? 1 : 0);
1274 	_info->n_outputs = ChanCount (DataType::AUDIO, used_out);
1275 
1276 	DEBUG_TRACE (DEBUG::AudioUnitConfig, string_compose ("AUPlugin::configured %1 used-in: %2 used-out %3, in-bus: %4 out-bus: %5, I/O %6 %7\n",
1277 	             name(), used_in, used_out, configured_input_busses, configured_output_busses, _info->n_inputs, _info->n_outputs));
1278 
1279 	if (was_initialized) {
1280 		activate ();
1281 	}
1282 
1283 	return true;
1284 }
1285 
1286 ChanCount
input_streams() const1287 AUPlugin::input_streams() const
1288 {
1289 	ChanCount c;
1290 	if (input_channels < 0) {
1291 		// force PluginIoReConfigure -- see also commit msg e38eb06
1292 		c.set (DataType::AUDIO, 0);
1293 		c.set (DataType::MIDI, 0);
1294 	} else {
1295 		c.set (DataType::AUDIO, input_channels);
1296 		c.set (DataType::MIDI, _has_midi_input ? 1 : 0);
1297 	}
1298 	return c;
1299 }
1300 
1301 
1302 ChanCount
output_streams() const1303 AUPlugin::output_streams() const
1304 {
1305 	ChanCount c;
1306 	if (output_channels < 0) {
1307 		// force PluginIoReConfigure - see also commit msg e38eb06
1308 		c.set (DataType::AUDIO, 0);
1309 		c.set (DataType::MIDI, 0);
1310 	} else {
1311 		c.set (DataType::AUDIO, output_channels);
1312 		c.set (DataType::MIDI, _has_midi_output ? 1 : 0);
1313 	}
1314 	return c;
1315 }
1316 
1317 bool
match_variable_io(ChanCount & in,ChanCount & aux_in,ChanCount & out)1318 AUPlugin::match_variable_io (ChanCount& in, ChanCount& aux_in, ChanCount& out)
1319 {
1320 	_output_configs.clear ();
1321 
1322 	/* if the plugin has no input busses, treat side-chain as normal input */
1323 	const int32_t audio_in = in.n_audio() + ((input_elements == 1) ? aux_in.n_audio() : 0);
1324 	/* preferred setting (provided by plugin_insert) */
1325 	const int32_t preferred_out = out.n_audio ();
1326 
1327 	AUPluginInfoPtr pinfo = boost::dynamic_pointer_cast<AUPluginInfo>(get_info());
1328 	vector<pair<int,int> > io_configs = pinfo->cache.io_configs;
1329 
1330 #ifndef NDEBUG
1331 	if (DEBUG_ENABLED(DEBUG::AudioUnitConfig)) {
1332 		DEBUG_STR_DECL(a);
1333 		DEBUG_STR_APPEND(a, string_compose ("AU Initial I/O Config list for %1 n_cfg: %2, in-bus %4 out-bus: %5\n", name(), io_configs.size(), input_elements, output_elements));
1334 		for (vector<pair<int,int> >::iterator i = io_configs.begin(); i != io_configs.end(); ++i) {
1335 			DEBUG_STR_APPEND(a, string_compose (" - I/O  %1 / %2\n", i->first, i->second));
1336 		}
1337 		DEBUG_TRACE (DEBUG::AudioUnitConfig, DEBUG_STR(a).str());
1338 	}
1339 #endif
1340 
1341 	/* add output busses as sum to possible outputs */
1342 #ifndef NDEBUG
1343 	bool outs_added = false;
1344 #endif
1345 	if (output_elements > 1) {
1346 		const vector<pair<int,int> >& ioc (pinfo->cache.io_configs);
1347 		for (vector<pair<int,int> >::const_iterator i = ioc.begin(); i != ioc.end(); ++i) {
1348 			int32_t possible_in = i->first;
1349 			int32_t possible_out = i->second;
1350 			if (possible_out < 0) {
1351 				continue;
1352 			}
1353 			for (uint32_t i = 1; i < output_elements; ++i) {
1354 				int32_t c = bus_outputs[i];
1355 				for (uint32_t j = 1; j < i; ++j) {
1356 					c += bus_outputs [j];
1357 				}
1358 				io_configs.push_back (pair<int,int> (possible_in, possible_out + c));
1359 			}
1360 #ifndef NDEBUG
1361 			outs_added = true;
1362 #endif
1363 			/* only add additional, optional busses to first available config.
1364 			 * AUPluginInfo::cached_io_configuration () already incrementally
1365 			 * adds busses (for instruments w/ multiple configurations)
1366 			 */
1367 			break;
1368 		}
1369 	}
1370 
1371 	DEBUG_TRACE (DEBUG::AudioUnitConfig, string_compose ("%1 has %2 IO configurations, looking for in: %3 aux: %4 out: %5\n", name(), io_configs.size(), in, aux_in, out));
1372 
1373 #ifndef NDEBUG
1374 	if (DEBUG_ENABLED(DEBUG::AudioUnitConfig) && outs_added) {
1375 		DEBUG_STR_DECL(a);
1376 		DEBUG_STR_APPEND(a, string_compose ("AU Final I/O Config list for %1 n_cfg: %2\n", name(), io_configs.size()));
1377 		for (vector<pair<int,int> >::iterator i = io_configs.begin(); i != io_configs.end(); ++i) {
1378 			DEBUG_STR_APPEND(a, string_compose (" - I/O  %1 / %2\n", i->first, i->second));
1379 		}
1380 		DEBUG_TRACE (DEBUG::AudioUnitConfig, DEBUG_STR(a).str());
1381 	}
1382 #endif
1383 
1384 	/* kAudioUnitProperty_SupportedNumChannels
1385 	 * https://developer.apple.com/library/mac/documentation/MusicAudio/Conceptual/AudioUnitProgrammingGuide/TheAudioUnit/TheAudioUnit.html#//apple_ref/doc/uid/TP40003278-CH12-SW20
1386 	 *
1387 	 * - both fields are -1
1388 	 *   e.g. inChannels = -1 outChannels = -1
1389 	 *    This is the default case. Any number of input and output channels, as long as the numbers match
1390 	 *
1391 	 * - one field is -1, the other field is positive
1392 	 *   e.g. inChannels = -1 outChannels = 2
1393 	 *    Any number of input channels, exactly two output channels
1394 	 *
1395 	 * - one field is -1, the other field is -2
1396 	 *   e.g. inChannels = -1 outChannels = -2
1397 	 *    Any number of input channels, any number of output channels
1398 	 *
1399 	 * - both fields have non-negative values
1400 	 *   e.g. inChannels = 2 outChannels = 6
1401 	 *    Exactly two input channels, exactly six output channels
1402 	 *   e.g. inChannels = 0 outChannels = 2
1403 	 *    No input channels, exactly two output channels (such as for an instrument unit with stereo output)
1404 	 *
1405 	 * - both fields have negative values, neither of which is –1 or –2
1406 	 *   e.g. inChannels = -4 outChannels = -8
1407 	 *    Up to four input channels and up to eight output channels
1408 	 */
1409 
1410 	int32_t audio_out = -1;
1411 	float penalty = 9999;
1412 	int32_t used_possible_in = 0;
1413 	bool found = false;
1414 
1415 #if defined (__clang__)
1416 # pragma clang diagnostic push
1417 # pragma clang diagnostic ignored "-Wtautological-compare"
1418 #endif
1419 
1420 #define FOUNDCFG_PENALTY(n_in, n_out, p) { \
1421   _output_configs.insert (n_out);          \
1422   if (p < penalty) {                       \
1423     used_possible_in = possible_in;        \
1424     audio_out = (n_out);                   \
1425     in.set (DataType::AUDIO, (n_in));      \
1426     penalty = p;                           \
1427     found = true;                          \
1428     variable_inputs = possible_in < 0;     \
1429     variable_outputs = possible_out < 0;   \
1430   }                                        \
1431 }
1432 
1433 #define FOUNDCFG_IMPRECISE(n_in, n_out) {                   \
1434   const float p = fabsf ((float)(n_out) - preferred_out) *  \
1435                       (((n_out) > preferred_out) ? 1.1 : 1) \
1436                 + fabsf ((float)(n_in) - audio_in) *        \
1437                       (((n_in) > audio_in) ? 275 : 250);    \
1438   FOUNDCFG_PENALTY(n_in, n_out, p);                         \
1439 }
1440 
1441 #define FOUNDCFG(n_out)              \
1442   FOUNDCFG_IMPRECISE(audio_in, n_out)
1443 
1444 #define ANYTHINGGOES         \
1445   _output_configs.insert (0);
1446 
1447 #define UPTO(nch) {                    \
1448   for (int32_t n = 1; n <= nch; ++n) { \
1449     _output_configs.insert (n);        \
1450   }                                    \
1451 }
1452 
1453 	for (vector<pair<int,int> >::iterator i = io_configs.begin(); i != io_configs.end(); ++i) {
1454 
1455 		int32_t possible_in = i->first;
1456 		int32_t possible_out = i->second;
1457 
1458 		DEBUG_TRACE (DEBUG::AudioUnitConfig, string_compose ("\tpossible in %1 possible out %2\n", possible_in, possible_out));
1459 
1460 		/* exact match */
1461 		if ((possible_in == audio_in) && (possible_out == preferred_out)) {
1462 			DEBUG_TRACE (DEBUG::AudioUnitConfig, string_compose ("\tCHOSEN: %1 in %2 out to match in %3 out %4\n",
1463 						possible_in, possible_out,
1464 						in, out));
1465 			/* Set penalty so low that this output configuration
1466 			 * will trump any other one */
1467 			FOUNDCFG_PENALTY(audio_in, preferred_out, -1);
1468 			break;
1469 		}
1470 
1471 		if (possible_out == 0) {
1472 			warning << string_compose (_("AU %1 has zero outputs - configuration ignored"), name()) << endmsg;
1473 			/* XXX surely this is just a send? (e.g. AUNetSend) */
1474 			continue;
1475 		}
1476 
1477 		/* now allow potentially "imprecise" matches */
1478 		if (possible_in == -1 || possible_in == -2) {
1479 			/* wildcard for input */
1480 			if (possible_out == possible_in) {
1481 				/* either both -1 or both -2 (invalid and
1482 				 * interpreted as both -1): out must match in */
1483 				FOUNDCFG (audio_in);
1484 			} else if (possible_out == -3 - possible_in) {
1485 				/* one is -1, the other is -2: any output configuration
1486 				 * possible, pick what the insert prefers */
1487 				FOUNDCFG (preferred_out);
1488 				ANYTHINGGOES;
1489 			} else if (possible_out < -2) {
1490 				/* variable number of outputs up to -N,
1491 				 * invalid if in == -2 but we accept it anyway */
1492 				FOUNDCFG (min (-possible_out, preferred_out));
1493 				UPTO (-possible_out)
1494 			} else {
1495 				/* exact number of outputs */
1496 				FOUNDCFG (possible_out);
1497 			}
1498 		}
1499 
1500 		if (possible_in < -2 || possible_in >= 0) {
1501 			/* specified number, exact or up to */
1502 			int32_t desired_in;
1503 			if (possible_in >= 0) {
1504 				/* configuration can only match possible_in */
1505 				desired_in = possible_in;
1506 			} else {
1507 				/* configuration can match up to -possible_in */
1508 				desired_in = min (-possible_in, audio_in);
1509 			}
1510 			 if (possible_out == -1 || possible_out == -2) {
1511 				/* any output configuration possible
1512 				 * out == -2 is invalid, interpreted as out == -1
1513 				 * Really imprecise only if desired_in != audio_in */
1514 				FOUNDCFG_IMPRECISE (desired_in, preferred_out);
1515 				ANYTHINGGOES;
1516 			} else if (possible_out < -2) {
1517 				/* variable number of outputs up to -N
1518 				 * not specified if in > 0, but we accept it anyway
1519 				 * Really imprecise only if desired_in != audio_in */
1520 				FOUNDCFG_IMPRECISE (desired_in, min (-possible_out, preferred_out));
1521 				UPTO (-possible_out)
1522 			} else {
1523 				/* exact number of outputs
1524 				 * Really imprecise only if desired_in != audio_in */
1525 				FOUNDCFG_IMPRECISE (desired_in, possible_out);
1526 			}
1527 		}
1528 
1529 	}
1530 
1531 	if (!found) {
1532 		DEBUG_TRACE (DEBUG::AudioUnitConfig, string_compose ("\tFAIL: no io configs match %1\n", in));
1533 		return false;
1534 	}
1535 
1536 	if (used_possible_in < -2 && audio_in == 0 && aux_in.n_audio () == 0) {
1537 		/* input-port count cannot be zero, use as many ports
1538 		 * as outputs, but at most abs(possible_in) */
1539 		uint32_t n_in = max (1, min (audio_out, -used_possible_in));
1540 		in.set (DataType::AUDIO, n_in);
1541 	}
1542 
1543 #if 0
1544 	if (aux_in.n_audio () > 0 && input_elements > 1) {
1545 		in.set (DataType::AUDIO, in.n_audio() + aux_in.n_audio());
1546 	}
1547 #endif
1548 
1549 	out.set (DataType::MIDI, 0); /// XXX currently always zero
1550 	out.set (DataType::AUDIO, audio_out);
1551 
1552 	if (input_elements == 1) {
1553 		/* subtract aux-ins that were treated as default inputs */
1554 		in.set (DataType::AUDIO, in.n_audio() - aux_in.n_audio());
1555 	}
1556 
1557 	DEBUG_TRACE (DEBUG::AudioUnitConfig, string_compose ("\tCHOSEN: in: %1 aux-in: %2 out: %3\n", in, aux_in, out));
1558 
1559 #if defined (__clang__)
1560 # pragma clang diagnostic pop
1561 #endif
1562 	return true;
1563 }
1564 
1565 int
set_stream_format(int scope,uint32_t bus,AudioStreamBasicDescription & fmt)1566 AUPlugin::set_stream_format (int scope, uint32_t bus, AudioStreamBasicDescription& fmt)
1567 {
1568 	OSErr result;
1569 
1570 	DEBUG_TRACE (DEBUG::AudioUnitConfig, string_compose ("set stream format for %1, scope = %2 element %3\n",
1571 				(scope == kAudioUnitScope_Input ? "input" : "output"),
1572 				scope, bus));
1573 	if ((result = unit->SetFormat (scope, bus, fmt)) != 0) {
1574 		error << string_compose (_("AUPlugin: could not set stream format for %1/%2 (err = %3)"),
1575 				(scope == kAudioUnitScope_Input ? "input" : "output"), bus, result) << endmsg;
1576 		return -1;
1577 	}
1578 	return 0;
1579 }
1580 
1581 OSStatus
render_callback(AudioUnitRenderActionFlags *,const AudioTimeStamp *,UInt32 bus,UInt32 inNumberSamples,AudioBufferList * ioData)1582 AUPlugin::render_callback(AudioUnitRenderActionFlags*,
1583 			  const AudioTimeStamp*,
1584 			  UInt32 bus,
1585 			  UInt32 inNumberSamples,
1586 			  AudioBufferList* ioData)
1587 {
1588 	/* not much to do with audio - the data is already in the buffers given to us in connect_and_run() */
1589 
1590 	DEBUG_TRACE (DEBUG::AudioUnitProcess, string_compose ("%1: render callback, samples %2 bus %3 bufs %4\n", name(), inNumberSamples, bus, ioData->mNumberBuffers));
1591 
1592 	if (input_maxbuf == 0) {
1593 		DEBUG_TRACE (DEBUG::AudioUnitProcess, "AUPlugin: render callback called illegally!");
1594 		error << _("AUPlugin: render callback called illegally!") << endmsg;
1595 		return kAudioUnitErr_CannotDoInCurrentContext;
1596 	}
1597 
1598 	assert (bus < input_elements);
1599 	uint32_t busoff = 0;
1600 	for (uint32_t i = 0; i < bus; ++i) {
1601 		busoff += bus_inused[i];
1602 	}
1603 
1604 	uint32_t limit = min ((uint32_t) ioData->mNumberBuffers, input_maxbuf);
1605 
1606 	ChanCount bufs_count (DataType::AUDIO, 1);
1607 	BufferSet& silent_bufs = _session.get_silent_buffers(bufs_count);
1608 
1609 	/* apply bus offsets */
1610 
1611 	for (uint32_t i = 0; i < limit; ++i) {
1612 		ioData->mBuffers[i].mNumberChannels = 1;
1613 		ioData->mBuffers[i].mDataByteSize = sizeof (Sample) * inNumberSamples;
1614 
1615 		bool valid = false;
1616 		uint32_t idx = input_map->get (DataType::AUDIO, i + busoff, &valid);
1617 		if (valid) {
1618 			ioData->mBuffers[i].mData = input_buffers->get_audio (idx).data (cb_offsets[bus] + input_offset);
1619 		} else {
1620 			ioData->mBuffers[i].mData = silent_bufs.get_audio(0).data (cb_offsets[bus] + input_offset);
1621 		}
1622 	}
1623 	cb_offsets[bus] += inNumberSamples;
1624 	return noErr;
1625 }
1626 
1627 int
connect_and_run(BufferSet & bufs,samplepos_t start,samplepos_t end,double speed,ChanMapping const & in_map,ChanMapping const & out_map,pframes_t nframes,samplecnt_t offset)1628 AUPlugin::connect_and_run (BufferSet& bufs,
1629 		samplepos_t start, samplepos_t end, double speed,
1630 		ChanMapping const& in_map, ChanMapping const& out_map,
1631 		pframes_t nframes, samplecnt_t offset)
1632 {
1633 	Plugin::connect_and_run(bufs, start, end, speed, in_map, out_map, nframes, offset);
1634 
1635 	/* remain at zero during pre-roll at zero */
1636 	transport_speed = end > 0 ? speed : 0;
1637 	transport_sample = std::max (start, samplepos_t (0));
1638 
1639 	AudioUnitRenderActionFlags flags = 0;
1640 	AudioTimeStamp ts;
1641 	OSErr err;
1642 
1643 	if (preset_holdoff > 0) {
1644 		preset_holdoff -= std::min (nframes, preset_holdoff);
1645 	}
1646 
1647 	if (requires_fixed_size_buffers() && (nframes != _last_nframes)) {
1648 		unit->GlobalReset();
1649 		_last_nframes = nframes;
1650 	}
1651 
1652 	/* test if we can run in-place; only compare audio buffers */
1653 	bool inplace = true; // TODO check plugin-insert in-place ?
1654 	ChanMapping::Mappings inmap (in_map.mappings ());
1655 	ChanMapping::Mappings outmap (out_map.mappings ());
1656 	if (outmap[DataType::AUDIO].size () == 0 || inmap[DataType::AUDIO].size() == 0) {
1657 		inplace = false;
1658 	}
1659 	if (inmap[DataType::AUDIO].size() > 0 && inmap != outmap) {
1660 		inplace = false;
1661 	}
1662 
1663 	DEBUG_TRACE (DEBUG::AudioUnitProcess, string_compose ("%1 in %2 out %3 MIDI %4 bufs %5 (available %6) InBus %7 OutBus %8 Inplace: %9 var-i/o %10 %11\n",
1664 				name(), input_channels, output_channels, _has_midi_input,
1665 				bufs.count(), bufs.available(),
1666 				configured_input_busses, configured_output_busses, inplace, variable_inputs, variable_outputs));
1667 
1668 	/* the apparent number of buffers matches our input configuration, but we know that the bufferset
1669 	 * has the capacity to handle our outputs.
1670 	 */
1671 
1672 	assert (bufs.available() >= ChanCount (DataType::AUDIO, output_channels));
1673 
1674 	input_buffers = &bufs;
1675 	input_map = &in_map;
1676 	input_maxbuf = bufs.count().n_audio(); // number of input audio buffers
1677 	input_offset = offset;
1678 	for (size_t i = 0; i < input_elements; ++i) {
1679 		cb_offsets[i] = 0;
1680 	}
1681 
1682 	ChanCount bufs_count (DataType::AUDIO, 1);
1683 	BufferSet& scratch_bufs = _session.get_scratch_buffers(bufs_count);
1684 
1685 	if (_has_midi_input) {
1686 		uint32_t nmidi = bufs.count().n_midi();
1687 		for (uint32_t i = 0; i < nmidi; ++i) {
1688 			/* one MIDI port/buffer only */
1689 			MidiBuffer& m = bufs.get_midi (i);
1690 			for (MidiBuffer::iterator i = m.begin(); i != m.end(); ++i) {
1691 				Evoral::Event<samplepos_t> ev (*i);
1692 				if (ev.is_channel_event()) {
1693 					const uint8_t* b = ev.buffer();
1694 					DEBUG_TRACE (DEBUG::AudioUnitProcess, string_compose ("%1: MIDI event %2\n", name(), ev));
1695 					unit->MIDIEvent (b[0], b[1], b[2], ev.time());
1696 				}
1697 				/* XXX need to handle sysex and other message types */
1698 			}
1699 		}
1700 	}
1701 
1702 	assert (input_maxbuf < 512);
1703 	std::bitset<512> used_outputs;
1704 
1705 	bool ok = true;
1706 	uint32_t busoff = 0;
1707 	uint32_t remain = output_channels;
1708 	for (uint32_t bus = 0; remain > 0 && bus < configured_output_busses; ++bus) {
1709 		uint32_t cnt;
1710 		if (variable_outputs || (output_elements == configured_output_busses && configured_output_busses == 1)) {
1711 			cnt = output_channels;
1712 		} else {
1713 			cnt = std::min (remain, bus_outputs[bus]);
1714 		}
1715 		assert (cnt > 0);
1716 
1717 		buffers->mNumberBuffers = cnt;
1718 
1719 		for (uint32_t i = 0; i < cnt; ++i) {
1720 			buffers->mBuffers[i].mNumberChannels = 1;
1721 			/* setting this to 0 indicates to the AU that it *can* provide buffers here
1722 			 * if necessary. if it can process in-place, it will use the buffers provided
1723 			 * as input by ::render_callback() above.
1724 			 *
1725 			 * a non-null values tells the plugin to render into the buffer pointed
1726 			 * at by the value.
1727 			 * https://developer.apple.com/documentation/audiotoolbox/1438430-audiounitrender?language=objc
1728 			 */
1729 			if (inplace) {
1730 				buffers->mBuffers[i].mDataByteSize = 0;
1731 				buffers->mBuffers[i].mData = 0;
1732 			} else {
1733 				buffers->mBuffers[i].mDataByteSize = nframes * sizeof (Sample);
1734 				bool valid = false;
1735 				uint32_t idx = out_map.get (DataType::AUDIO, i + busoff, &valid);
1736 				if (valid) {
1737 					buffers->mBuffers[i].mData = bufs.get_audio (idx).data (offset);
1738 				} else {
1739 					buffers->mBuffers[i].mData = scratch_bufs.get_audio(0).data(offset);
1740 				}
1741 			}
1742 		}
1743 
1744 		/* does this really mean anything ?  */
1745 		ts.mSampleTime = samples_processed;
1746 		ts.mFlags = kAudioTimeStampSampleTimeValid;
1747 
1748 		DEBUG_TRACE (DEBUG::AudioUnitProcess, string_compose ("%1 render flags=%2 time=%3 nframes=%4 bus=%5 buffers=%6\n",
1749 					name(), flags, samples_processed, nframes, bus, buffers->mNumberBuffers));
1750 
1751 		if ((err = unit->Render (&flags, &ts, bus, nframes, buffers)) == noErr) {
1752 
1753 			DEBUG_TRACE (DEBUG::AudioUnitProcess, string_compose ("%1 rendered %2 buffers of %3\n",
1754 						name(), buffers->mNumberBuffers, output_channels));
1755 
1756 			uint32_t limit = std::min ((uint32_t) buffers->mNumberBuffers, cnt);
1757 			for (uint32_t i = 0; i < limit; ++i) {
1758 				bool valid = false;
1759 				uint32_t idx = out_map.get (DataType::AUDIO, i + busoff, &valid);
1760 				if (!valid) {
1761 					continue;
1762 				}
1763 				if (buffers->mBuffers[i].mData == 0 || buffers->mBuffers[i].mNumberChannels != 1) {
1764 					continue;
1765 				}
1766 				used_outputs.set (i + busoff);
1767 				Sample* expected_buffer_address = bufs.get_audio (idx).data (offset);
1768 				if (expected_buffer_address != buffers->mBuffers[i].mData) {
1769 					/* plugin provided its own buffer for output so copy it back to where we want it */
1770 					memcpy (expected_buffer_address, buffers->mBuffers[i].mData, nframes * sizeof (Sample));
1771 				}
1772 			}
1773 		} else {
1774 			DEBUG_TRACE (DEBUG::AudioUnitProcess, string_compose (_("AU: render error for %1, bus %2 status = %3\n"), name(), bus, err));
1775 			error << string_compose (_("AU: render error for %1, bus %2 status = %3"), name(), bus, err) << endmsg;
1776 			ok = false;
1777 			break;
1778 		}
1779 
1780 		remain -= cnt;
1781 		busoff += bus_outputs[bus];
1782 	}
1783 
1784 	/* now silence any buffers that were passed in but the that the plugin
1785 	 * did not fill/touch/use.
1786 	 *
1787 	 * TODO: optimize, when plugin-insert is processing in-place
1788 	 * unconnected buffers are (also) cleared there.
1789 	 */
1790 	for (uint32_t i = 0; i < input_maxbuf; ++i) {
1791 		if (used_outputs.test (i)) { continue; }
1792 		bool valid = false;
1793 		uint32_t idx = out_map.get (DataType::AUDIO, i, &valid);
1794 		if (!valid) continue;
1795 		memset (bufs.get_audio (idx).data (offset), 0, nframes * sizeof (Sample));
1796 	}
1797 
1798 	input_maxbuf = 0;
1799 
1800 	if (ok) {
1801 		samples_processed += nframes;
1802 		return 0;
1803 	}
1804 	return -1;
1805 }
1806 
1807 OSStatus
get_beat_and_tempo_callback(Float64 * outCurrentBeat,Float64 * outCurrentTempo)1808 AUPlugin::get_beat_and_tempo_callback (Float64* outCurrentBeat,
1809 				       Float64* outCurrentTempo)
1810 {
1811 	TempoMap& tmap (_session.tempo_map());
1812 
1813 	DEBUG_TRACE (DEBUG::AudioUnitProcess, "AU calls ardour beat&tempo callback\n");
1814 
1815 	if (outCurrentBeat) {
1816 		*outCurrentBeat = tmap.quarter_note_at_sample (transport_sample);
1817 	}
1818 
1819 	if (outCurrentTempo) {
1820 		*outCurrentTempo = tmap.tempo_at_sample (transport_sample).quarter_notes_per_minute();
1821 	}
1822 
1823 	return noErr;
1824 
1825 }
1826 
1827 OSStatus
get_musical_time_location_callback(UInt32 * outDeltaSampleOffsetToNextBeat,Float32 * outTimeSig_Numerator,UInt32 * outTimeSig_Denominator,Float64 * outCurrentMeasureDownBeat)1828 AUPlugin::get_musical_time_location_callback (UInt32*   outDeltaSampleOffsetToNextBeat,
1829 					      Float32*  outTimeSig_Numerator,
1830 					      UInt32*   outTimeSig_Denominator,
1831 					      Float64*  outCurrentMeasureDownBeat)
1832 {
1833 	TempoMap& tmap (_session.tempo_map());
1834 
1835 	DEBUG_TRACE (DEBUG::AudioUnitProcess, "AU calls ardour music time location callback\n");
1836 
1837 	TempoMetric metric = tmap.metric_at (transport_sample);
1838 	Timecode::BBT_Time bbt = _session.tempo_map().bbt_at_sample (transport_sample);
1839 
1840 	if (outDeltaSampleOffsetToNextBeat) {
1841 		if (bbt.ticks == 0) {
1842 			/* on the beat */
1843 			*outDeltaSampleOffsetToNextBeat = 0;
1844 		} else {
1845 			double const next_beat = ceil (tmap.quarter_note_at_sample (transport_sample));
1846 			samplepos_t const next_beat_sample = tmap.sample_at_quarter_note (next_beat);
1847 
1848 			*outDeltaSampleOffsetToNextBeat = next_beat_sample - transport_sample;
1849 		}
1850 	}
1851 
1852 	if (outTimeSig_Numerator) {
1853 		*outTimeSig_Numerator = (UInt32) lrintf (metric.meter().divisions_per_bar());
1854 	}
1855 	if (outTimeSig_Denominator) {
1856 		*outTimeSig_Denominator = (UInt32) lrintf (metric.meter().note_divisor());
1857 	}
1858 
1859 	if (outCurrentMeasureDownBeat) {
1860 
1861 		/* beat for the start of the bar.
1862 		   1|1|0 -> 1
1863 		   2|1|0 -> 1 + divisions_per_bar
1864 		   3|1|0 -> 1 + (2 * divisions_per_bar)
1865 		   etc.
1866 		*/
1867 		bbt.beats = 1;
1868 		bbt.ticks = 0;
1869 
1870 		*outCurrentMeasureDownBeat = tmap.quarter_note_at_bbt (bbt);
1871 	}
1872 
1873 	return noErr;
1874 }
1875 
1876 OSStatus
get_transport_state_callback(Boolean * outIsPlaying,Boolean * outTransportStateChanged,Float64 * outCurrentSampleInTimeLine,Boolean * outIsCycling,Float64 * outCycleStartBeat,Float64 * outCycleEndBeat)1877 AUPlugin::get_transport_state_callback (Boolean*  outIsPlaying,
1878 					Boolean*  outTransportStateChanged,
1879 					Float64*  outCurrentSampleInTimeLine,
1880 					Boolean*  outIsCycling,
1881 					Float64*  outCycleStartBeat,
1882 					Float64*  outCycleEndBeat)
1883 {
1884 	const bool rolling = (transport_speed != 0);
1885 	const bool last_transport_rolling = (last_transport_speed != 0);
1886 
1887 	DEBUG_TRACE (DEBUG::AudioUnitProcess, "AU calls ardour transport state callback\n");
1888 
1889 
1890 	if (outIsPlaying) {
1891 		*outIsPlaying = rolling;
1892 	}
1893 
1894 	if (outTransportStateChanged) {
1895 		if (rolling != last_transport_rolling) {
1896 			*outTransportStateChanged = true;
1897 		} else if (transport_speed != last_transport_speed) {
1898 			*outTransportStateChanged = true;
1899 		} else {
1900 			*outTransportStateChanged = false;
1901 		}
1902 	}
1903 
1904 	if (outCurrentSampleInTimeLine) {
1905 		/* this assumes that the AU can only call this host callback from render context,
1906 		   where input_offset is valid.
1907 		*/
1908 		*outCurrentSampleInTimeLine = transport_sample;
1909 	}
1910 
1911 	if (outIsCycling) {
1912 		// TODO check bounce-processing
1913 		Location* loc = _session.locations()->auto_loop_location();
1914 
1915 		*outIsCycling = (loc && rolling && _session.get_play_loop());
1916 
1917 		if (*outIsCycling) {
1918 
1919 			if (outCycleStartBeat || outCycleEndBeat) {
1920 
1921 				TempoMap& tmap (_session.tempo_map());
1922 
1923 				Timecode::BBT_Time bbt;
1924 
1925 				if (outCycleStartBeat) {
1926 					*outCycleStartBeat = tmap.quarter_note_at_sample (loc->start());
1927 				}
1928 
1929 				if (outCycleEndBeat) {
1930 					*outCycleEndBeat = tmap.quarter_note_at_sample (loc->end());
1931 				}
1932 			}
1933 		}
1934 	}
1935 
1936 	last_transport_speed = transport_speed;
1937 
1938 	return noErr;
1939 }
1940 
1941 set<Evoral::Parameter>
automatable() const1942 AUPlugin::automatable() const
1943 {
1944 	set<Evoral::Parameter> automates;
1945 
1946 	for (uint32_t i = 0; i < descriptors.size(); ++i) {
1947 		if (descriptors[i].automatable) {
1948 			automates.insert (automates.end(), Evoral::Parameter (PluginAutomation, 0, i));
1949 		}
1950 	}
1951 
1952 	return automates;
1953 }
1954 
1955 Plugin::IOPortDescription
describe_io_port(ARDOUR::DataType dt,bool input,uint32_t id) const1956 AUPlugin::describe_io_port (ARDOUR::DataType dt, bool input, uint32_t id) const
1957 {
1958 	std::stringstream ss;
1959 	switch (dt) {
1960 		case DataType::AUDIO:
1961 			break;
1962 		case DataType::MIDI:
1963 			ss << _("Midi");
1964 			break;
1965 		default:
1966 			ss << _("?");
1967 			break;
1968 	}
1969 
1970 	std::string busname;
1971 	bool is_sidechain = false;
1972 
1973 	if (dt == DataType::AUDIO) {
1974 		if (input) {
1975 			uint32_t pid = id;
1976 			for (uint32_t bus = 0; bus < input_elements; ++bus) {
1977 				if (pid < bus_inused[bus]) {
1978 					id = pid;
1979 					ss << _bus_name_in[bus];
1980 					ss << " / Bus " << (1 + bus);
1981 					busname = _bus_name_in[bus];
1982 					is_sidechain = bus > 0;
1983 					busname = _bus_name_in[bus];
1984 					break;
1985 				}
1986 				pid -= bus_inused[bus];
1987 			}
1988 		}
1989 		else {
1990 			uint32_t pid = id;
1991 			for (uint32_t bus = 0; bus < output_elements; ++bus) {
1992 				if (pid < bus_outputs[bus]) {
1993 					id = pid;
1994 					ss << _bus_name_out[bus];
1995 					ss << " / Bus " << (1 + bus);
1996 					busname = _bus_name_out[bus];
1997 					break;
1998 				}
1999 				pid -= bus_outputs[bus];
2000 			}
2001 		}
2002 	}
2003 
2004 	if (input) {
2005 		ss << " " << _("In") << " ";
2006 	} else {
2007 		ss << " " << _("Out") << " ";
2008 	}
2009 
2010 	ss << (id + 1);
2011 
2012 	Plugin::IOPortDescription iod (ss.str());
2013 	iod.is_sidechain = is_sidechain;
2014 	if (!busname.empty()) {
2015 		iod.group_name = busname;
2016 		iod.group_channel = id;
2017 	}
2018 	return iod;
2019 }
2020 
2021 string
describe_parameter(Evoral::Parameter param)2022 AUPlugin::describe_parameter (Evoral::Parameter param)
2023 {
2024 	if (param.type() == PluginAutomation && param.id() < parameter_count()) {
2025 		return descriptors[param.id()].label;
2026 	} else {
2027 		return "??";
2028 	}
2029 }
2030 
2031 bool
parameter_is_audio(uint32_t) const2032 AUPlugin::parameter_is_audio (uint32_t) const
2033 {
2034 	return false;
2035 }
2036 
2037 bool
parameter_is_control(uint32_t param) const2038 AUPlugin::parameter_is_control (uint32_t param) const
2039 {
2040 	assert(param < descriptors.size());
2041 	if (descriptors[param].automatable) {
2042 		/* corrently ardour expects all controls to be automatable
2043 		 * IOW ardour GUI elements mandate an Evoral::Parameter
2044 		 * for all input+control ports.
2045 		 */
2046 		return true;
2047 	}
2048 	return false;
2049 }
2050 
2051 bool
parameter_is_input(uint32_t param) const2052 AUPlugin::parameter_is_input (uint32_t param) const
2053 {
2054 	/* AU params that are both readable and writeable,
2055 	 * are listed in kAudioUnitScope_Global
2056 	 */
2057 	return (descriptors[param].scope == kAudioUnitScope_Input || descriptors[param].scope == kAudioUnitScope_Global);
2058 }
2059 
2060 bool
parameter_is_output(uint32_t param) const2061 AUPlugin::parameter_is_output (uint32_t param) const
2062 {
2063 	assert(param < descriptors.size());
2064 	// TODO check if ardour properly handles ports
2065 	// that report is_input + is_output == true
2066 	// -> add || descriptors[param].scope == kAudioUnitScope_Global
2067 	return (descriptors[param].scope == kAudioUnitScope_Output);
2068 }
2069 
2070 void
add_state(XMLNode * root) const2071 AUPlugin::add_state (XMLNode* root) const
2072 {
2073 	LocaleGuard lg;
2074 	CFDataRef xmlData;
2075 	CFPropertyListRef propertyList;
2076 
2077 	DEBUG_TRACE (DEBUG::AudioUnitConfig, "get preset state\n");
2078 	if (unit->GetAUPreset (propertyList) != noErr) {
2079 		return;
2080 	}
2081 
2082 	// Convert the property list into XML data.
2083 
2084 	xmlData = CFPropertyListCreateXMLData( kCFAllocatorDefault, propertyList);
2085 
2086 	if (!xmlData) {
2087 		error << _("Could not create XML version of property list") << endmsg;
2088 		return;
2089 	}
2090 
2091 	/* re-parse XML bytes to create a libxml++ XMLTree that we can merge into
2092 	   our state node. GACK!
2093 	*/
2094 
2095 	XMLTree t;
2096 
2097 	if (t.read_buffer (string ((const char*) CFDataGetBytePtr (xmlData), CFDataGetLength (xmlData)).c_str())) {
2098 		if (t.root()) {
2099 			root->add_child_copy (*t.root());
2100 		}
2101 	}
2102 
2103 	CFRelease (xmlData);
2104 	CFRelease (propertyList);
2105 }
2106 
2107 int
set_state(const XMLNode & node,int version)2108 AUPlugin::set_state(const XMLNode& node, int version)
2109 {
2110 	int ret = -1;
2111 	CFPropertyListRef propertyList;
2112 	LocaleGuard lg;
2113 
2114 	if (node.name() != state_node_name()) {
2115 		error << _("Bad node sent to AUPlugin::set_state") << endmsg;
2116 		return -1;
2117 	}
2118 
2119 	if (node.children().empty()) {
2120 		return -1;
2121 	}
2122 
2123 	XMLNode* top = node.children().front();
2124 	XMLNode* copy = new XMLNode (*top);
2125 
2126 	XMLTree t;
2127 	t.set_root (copy);
2128 
2129 	const string& xml = t.write_buffer ();
2130 	CFDataRef xmlData = CFDataCreateWithBytesNoCopy (kCFAllocatorDefault, (UInt8*) xml.data(), xml.length(), kCFAllocatorNull);
2131 	CFStringRef errorString;
2132 
2133 	propertyList = CFPropertyListCreateFromXMLData( kCFAllocatorDefault,
2134 							xmlData,
2135 							kCFPropertyListImmutable,
2136 							&errorString);
2137 
2138 	CFRelease (xmlData);
2139 
2140 	if (propertyList) {
2141 		DEBUG_TRACE (DEBUG::AudioUnitConfig, "set preset\n");
2142 		if (unit->SetAUPreset (propertyList) == noErr) {
2143 			ret = 0;
2144 
2145 			/* tell the world */
2146 
2147 			AudioUnitParameter changedUnit;
2148 			changedUnit.mAudioUnit = unit->AU();
2149 			changedUnit.mParameterID = kAUParameterListener_AnyParameter;
2150 			AUParameterListenerNotify (NULL, NULL, &changedUnit);
2151 		}
2152 		CFRelease (propertyList);
2153 	}
2154 
2155 	Plugin::set_state (node, version);
2156 	return ret;
2157 }
2158 
2159 bool
load_preset(PresetRecord r)2160 AUPlugin::load_preset (PresetRecord r)
2161 {
2162 	bool ret = false;
2163 	CFPropertyListRef propertyList;
2164 	Glib::ustring path;
2165 	UserPresetMap::iterator ux;
2166 	FactoryPresetMap::iterator fx;
2167 
2168 	/* look first in "user" presets */
2169 
2170 	if ((ux = user_preset_map.find (r.label)) != user_preset_map.end()) {
2171 
2172 		if ((propertyList = load_property_list (ux->second)) != 0) {
2173 			DEBUG_TRACE (DEBUG::AudioUnitConfig, "set preset from user presets\n");
2174 			if (unit->SetAUPreset (propertyList) == noErr) {
2175 				ret = true;
2176 
2177 				/* tell the world */
2178 
2179 				AudioUnitParameter changedUnit;
2180 				changedUnit.mAudioUnit = unit->AU();
2181 				changedUnit.mParameterID = kAUParameterListener_AnyParameter;
2182 				AUParameterListenerNotify (NULL, NULL, &changedUnit);
2183 			}
2184 			CFRelease(propertyList);
2185 		}
2186 
2187 	} else if ((fx = factory_preset_map.find (r.label)) != factory_preset_map.end()) {
2188 
2189 		AUPreset preset;
2190 
2191 		preset.presetNumber = fx->second;
2192 		preset.presetName = CFStringCreateWithCString (kCFAllocatorDefault, fx->first.c_str(), kCFStringEncodingUTF8);
2193 
2194 		DEBUG_TRACE (DEBUG::AudioUnitConfig, "set preset from factory presets\n");
2195 
2196 		if (unit->SetPresentPreset (preset) == 0) {
2197 			ret = true;
2198 
2199 			/* tell the world */
2200 
2201 			AudioUnitParameter changedUnit;
2202 			changedUnit.mAudioUnit = unit->AU();
2203 			changedUnit.mParameterID = kAUParameterListener_AnyParameter;
2204 			AUParameterListenerNotify (NULL, NULL, &changedUnit);
2205 		}
2206 	}
2207 	if (ret) {
2208 		preset_holdoff = std::max (_session.get_block_size() * 2.0, _session.sample_rate() * .2);
2209 	}
2210 
2211 	return ret && Plugin::load_preset (r);
2212 }
2213 
2214 void
do_remove_preset(std::string preset_name)2215 AUPlugin::do_remove_preset (std::string preset_name)
2216 {
2217 	vector<Glib::ustring> v;
2218 
2219 	std::string m = maker();
2220 	std::string n = name();
2221 
2222 	strip_whitespace_edges (m);
2223 	strip_whitespace_edges (n);
2224 
2225 	v.push_back (Glib::get_home_dir());
2226 	v.push_back ("Library");
2227 	v.push_back ("Audio");
2228 	v.push_back ("Presets");
2229 	v.push_back (m);
2230 	v.push_back (n);
2231 	v.push_back (preset_name + preset_suffix);
2232 
2233 	Glib::ustring user_preset_path = Glib::build_filename (v);
2234 
2235 	DEBUG_TRACE (DEBUG::AudioUnitConfig, string_compose("AU Deleting Preset file %1\n", user_preset_path));
2236 
2237 	if (g_unlink (user_preset_path.c_str())) {
2238 		error << string_compose (X_("Could not delete preset at \"%1\": %2"), user_preset_path, strerror (errno)) << endmsg;
2239 	}
2240 }
2241 
2242 string
do_save_preset(string preset_name)2243 AUPlugin::do_save_preset (string preset_name)
2244 {
2245 	CFPropertyListRef propertyList;
2246 	vector<Glib::ustring> v;
2247 	Glib::ustring user_preset_path;
2248 
2249 	std::string m = maker();
2250 	std::string n = name();
2251 
2252 	strip_whitespace_edges (m);
2253 	strip_whitespace_edges (n);
2254 
2255 	v.push_back (Glib::get_home_dir());
2256 	v.push_back ("Library");
2257 	v.push_back ("Audio");
2258 	v.push_back ("Presets");
2259 	v.push_back (m);
2260 	v.push_back (n);
2261 
2262 	user_preset_path = Glib::build_filename (v);
2263 
2264 	if (g_mkdir_with_parents (user_preset_path.c_str(), 0775) < 0) {
2265 		error << string_compose (_("Cannot create user plugin presets folder (%1)"), user_preset_path) << endmsg;
2266 		return string();
2267 	}
2268 
2269 	DEBUG_TRACE (DEBUG::AudioUnitConfig, "get current preset\n");
2270 	if (unit->GetAUPreset (propertyList) != noErr) {
2271 		return string();
2272 	}
2273 
2274 	// add the actual preset name */
2275 
2276 	v.push_back (preset_name + preset_suffix);
2277 
2278 	// rebuild
2279 
2280 	user_preset_path = Glib::build_filename (v);
2281 
2282 	/* delete old preset if it exists */
2283 	g_unlink (user_preset_path.c_str());
2284 
2285 	set_preset_name_in_plist (propertyList, preset_name);
2286 
2287 	if (save_property_list (propertyList, user_preset_path)) {
2288 		error << string_compose (_("Saving plugin state to %1 failed"), user_preset_path) << endmsg;
2289 		return string();
2290 	}
2291 
2292 	CFRelease(propertyList);
2293 
2294 	user_preset_map[preset_name] = user_preset_path;;
2295 
2296 	DEBUG_TRACE (DEBUG::AudioUnitConfig, string_compose("AU Saving Preset to %1\n", user_preset_path));
2297 
2298 	return user_preset_path;
2299 }
2300 
2301 //-----------------------------------------------------------------------------
2302 // this is just a little helper function used by GetAUComponentDescriptionFromPresetFile()
2303 static SInt32
GetDictionarySInt32Value(CFDictionaryRef inAUStateDictionary,CFStringRef inDictionaryKey,Boolean * outSuccess)2304 GetDictionarySInt32Value(CFDictionaryRef inAUStateDictionary, CFStringRef inDictionaryKey, Boolean * outSuccess)
2305 {
2306 	CFNumberRef cfNumber;
2307 	SInt32 numberValue = 0;
2308 	Boolean dummySuccess;
2309 
2310 	if (outSuccess == NULL)
2311 		outSuccess = &dummySuccess;
2312 	if ( (inAUStateDictionary == NULL) || (inDictionaryKey == NULL) )
2313 	{
2314 		*outSuccess = FALSE;
2315 		return 0;
2316 	}
2317 
2318 	cfNumber = (CFNumberRef) CFDictionaryGetValue(inAUStateDictionary, inDictionaryKey);
2319 	if (cfNumber == NULL)
2320 	{
2321 		*outSuccess = FALSE;
2322 		return 0;
2323 	}
2324 	*outSuccess = CFNumberGetValue(cfNumber, kCFNumberSInt32Type, &numberValue);
2325 	if (*outSuccess)
2326 		return numberValue;
2327 	else
2328 		return 0;
2329 }
2330 
2331 static OSStatus
GetAUComponentDescriptionFromStateData(CFPropertyListRef inAUStateData,ArdourDescription * outComponentDescription)2332 GetAUComponentDescriptionFromStateData(CFPropertyListRef inAUStateData, ArdourDescription * outComponentDescription)
2333 {
2334 	CFDictionaryRef auStateDictionary;
2335 	ArdourDescription tempDesc = {0,0,0,0,0};
2336 	SInt32 versionValue;
2337 	Boolean gotValue;
2338 
2339 	if ( (inAUStateData == NULL) || (outComponentDescription == NULL) )
2340 		return paramErr;
2341 
2342 	// the property list for AU state data must be of the dictionary type
2343 	if (CFGetTypeID(inAUStateData) != CFDictionaryGetTypeID()) {
2344 		return kAudioUnitErr_InvalidPropertyValue;
2345 	}
2346 
2347 	auStateDictionary = (CFDictionaryRef)inAUStateData;
2348 
2349 	// first check to make sure that the version of the AU state data is one that we know understand
2350 	// XXX should I really do this?  later versions would probably still hold these ID keys, right?
2351 	versionValue = GetDictionarySInt32Value(auStateDictionary, CFSTR(kAUPresetVersionKey), &gotValue);
2352 
2353 	if (!gotValue) {
2354 		return kAudioUnitErr_InvalidPropertyValue;
2355 	}
2356 #define kCurrentSavedStateVersion 0
2357 	if (versionValue != kCurrentSavedStateVersion) {
2358 		return kAudioUnitErr_InvalidPropertyValue;
2359 	}
2360 
2361 	// grab the ComponentDescription values from the AU state data
2362 	tempDesc.componentType = (OSType) GetDictionarySInt32Value(auStateDictionary, CFSTR(kAUPresetTypeKey), NULL);
2363 	tempDesc.componentSubType = (OSType) GetDictionarySInt32Value(auStateDictionary, CFSTR(kAUPresetSubtypeKey), NULL);
2364 	tempDesc.componentManufacturer = (OSType) GetDictionarySInt32Value(auStateDictionary, CFSTR(kAUPresetManufacturerKey), NULL);
2365 	// zero values are illegit for specific ComponentDescriptions, so zero for any value means that there was an error
2366 	if ( (tempDesc.componentType == 0) || (tempDesc.componentSubType == 0) || (tempDesc.componentManufacturer == 0) )
2367 		return kAudioUnitErr_InvalidPropertyValue;
2368 
2369 	*outComponentDescription = tempDesc;
2370 	return noErr;
2371 }
2372 
2373 
au_preset_filter(const string & str,void * arg)2374 static bool au_preset_filter (const string& str, void* arg)
2375 {
2376 	/* Not a dotfile, has a prefix before a period, suffix is aupreset */
2377 
2378 	bool ret;
2379 
2380 	ret = (str[0] != '.' && str.length() > 9 && str.find (preset_suffix) == (str.length() - preset_suffix.length()));
2381 
2382 	if (ret && arg) {
2383 
2384 		/* check the preset file path name against this plugin
2385 		   ID. The idea is that all preset files for this plugin
2386 		   include "<manufacturer>/<plugin-name>" in their path.
2387 		*/
2388 
2389 		AUPluginInfo* p = (AUPluginInfo *) arg;
2390 		string match = p->creator;
2391 		match += '/';
2392 		match += p->name;
2393 
2394 		ret = str.find (match) != string::npos;
2395 
2396 		if (ret == false) {
2397 			string m = p->creator;
2398 			string n = p->name;
2399 			strip_whitespace_edges (m);
2400 			strip_whitespace_edges (n);
2401 			match = m;
2402 			match += '/';
2403 			match += n;
2404 
2405 			ret = str.find (match) != string::npos;
2406 		}
2407 	}
2408 
2409 	return ret;
2410 }
2411 
2412 static bool
check_and_get_preset_name(ArdourComponent component,const string & pathstr,string & preset_name)2413 check_and_get_preset_name (ArdourComponent component, const string& pathstr, string& preset_name)
2414 {
2415 	OSStatus status;
2416 	CFPropertyListRef plist;
2417 	ArdourDescription presetDesc;
2418 	bool ret = false;
2419 
2420 	plist = load_property_list (pathstr);
2421 
2422 	if (!plist) {
2423 		return ret;
2424 	}
2425 
2426 	// get the ComponentDescription from the AU preset file
2427 
2428 	status = GetAUComponentDescriptionFromStateData(plist, &presetDesc);
2429 
2430 	if (status == noErr) {
2431 		if (ComponentAndDescriptionMatch_Loosely(component, &presetDesc)) {
2432 
2433 			/* try to get the preset name from the property list */
2434 
2435 			if (CFGetTypeID(plist) == CFDictionaryGetTypeID()) {
2436 
2437 				const void* psk = CFDictionaryGetValue ((CFMutableDictionaryRef)plist, CFSTR(kAUPresetNameKey));
2438 
2439 				if (psk) {
2440 
2441 					const char* p = CFStringGetCStringPtr ((CFStringRef) psk, kCFStringEncodingUTF8);
2442 
2443 					if (!p) {
2444 						char buf[PATH_MAX+1];
2445 
2446 						if (CFStringGetCString ((CFStringRef)psk, buf, sizeof (buf), kCFStringEncodingUTF8)) {
2447 							preset_name = buf;
2448 						}
2449 					}
2450 				}
2451 			}
2452 		}
2453 	}
2454 
2455 	CFRelease (plist);
2456 
2457 	return true;
2458 }
2459 
2460 
2461 static void
2462 #ifdef COREAUDIO105
get_names(CAComponentDescription & comp_desc,std::string & name,std::string & maker)2463 get_names (CAComponentDescription& comp_desc, std::string& name, std::string& maker)
2464 #else
2465 get_names (ArdourComponent& comp, std::string& name, std::string& maker)
2466 #endif
2467 {
2468 	CFStringRef itemName = NULL;
2469 	// Marc Poirier-style item name
2470 #ifdef COREAUDIO105
2471 	CAComponent auComponent (comp_desc);
2472 	if (auComponent.IsValid()) {
2473 		CAComponentDescription dummydesc;
2474 		Handle nameHandle = NewHandle(sizeof(void*));
2475 		if (nameHandle != NULL) {
2476 			OSErr err = GetComponentInfo(auComponent.Comp(), &dummydesc, nameHandle, NULL, NULL);
2477 			if (err == noErr) {
2478 				ConstStr255Param nameString = (ConstStr255Param) (*nameHandle);
2479 				if (nameString != NULL) {
2480 					itemName = CFStringCreateWithPascalString(kCFAllocatorDefault, nameString, CFStringGetSystemEncoding());
2481 				}
2482 			}
2483 			DisposeHandle(nameHandle);
2484 		}
2485 	}
2486 #else
2487 	assert (comp);
2488 	AudioComponentCopyName (comp, &itemName);
2489 #endif
2490 
2491 	// if Marc-style fails, do the original way
2492 	if (itemName == NULL) {
2493 #ifndef COREAUDIO105
2494 		CAComponentDescription comp_desc;
2495 		AudioComponentGetDescription (comp, &comp_desc);
2496 #endif
2497 		CFStringRef compTypeString = UTCreateStringForOSType(comp_desc.componentType);
2498 		CFStringRef compSubTypeString = UTCreateStringForOSType(comp_desc.componentSubType);
2499 		CFStringRef compManufacturerString = UTCreateStringForOSType(comp_desc.componentManufacturer);
2500 
2501 		itemName = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%@ - %@ - %@"),
2502 				compTypeString, compManufacturerString, compSubTypeString);
2503 
2504 		if (compTypeString != NULL)
2505 			CFRelease(compTypeString);
2506 		if (compSubTypeString != NULL)
2507 			CFRelease(compSubTypeString);
2508 		if (compManufacturerString != NULL)
2509 			CFRelease(compManufacturerString);
2510 	}
2511 
2512 	string str = CFStringRefToStdString(itemName);
2513 	string::size_type colon = str.find (':');
2514 
2515 	if (colon) {
2516 		name = str.substr (colon+1);
2517 		maker = str.substr (0, colon);
2518 		strip_whitespace_edges (maker);
2519 		strip_whitespace_edges (name);
2520 	} else {
2521 		name = str;
2522 		maker = "unknown";
2523 		strip_whitespace_edges (name);
2524 	}
2525 }
2526 
2527 std::string
current_preset() const2528 AUPlugin::current_preset() const
2529 {
2530 	string preset_name;
2531 
2532 	CFPropertyListRef propertyList;
2533 
2534 	DEBUG_TRACE (DEBUG::AudioUnitConfig, "get current preset for current_preset()\n");
2535 	if (unit->GetAUPreset (propertyList) == noErr) {
2536 		preset_name = get_preset_name_in_plist (propertyList);
2537 		CFRelease(propertyList);
2538 	}
2539 
2540 	return preset_name;
2541 }
2542 
2543 void
find_presets()2544 AUPlugin::find_presets ()
2545 {
2546 	vector<string> preset_files;
2547 
2548 	user_preset_map.clear ();
2549 
2550 	PluginInfoPtr nfo = get_info();
2551 	find_files_matching_filter (preset_files, preset_search_path, au_preset_filter,
2552 			boost::dynamic_pointer_cast<AUPluginInfo> (nfo).get(),
2553 			true, true, true);
2554 
2555 	if (preset_files.empty()) {
2556 		DEBUG_TRACE (DEBUG::AudioUnitConfig, "AU No Preset Files found for given plugin.\n");
2557 	}
2558 
2559 	for (vector<string>::iterator x = preset_files.begin(); x != preset_files.end(); ++x) {
2560 
2561 		string path = *x;
2562 		string preset_name;
2563 
2564 		/* make an initial guess at the preset name using the path */
2565 
2566 		preset_name = Glib::path_get_basename (path);
2567 		preset_name = preset_name.substr (0, preset_name.find_last_of ('.'));
2568 
2569 		/* check that this preset file really matches this plugin
2570 		   and potentially get the "real" preset name from
2571 		   within the file.
2572 		*/
2573 
2574 		if (check_and_get_preset_name (get_comp()->Comp(), path, preset_name)) {
2575 			user_preset_map[preset_name] = path;
2576 			DEBUG_TRACE (DEBUG::AudioUnitConfig, string_compose("AU Preset File: %1 > %2\n", preset_name, path));
2577 		} else {
2578 			DEBUG_TRACE (DEBUG::AudioUnitConfig, string_compose("AU INVALID Preset: %1 > %2\n", preset_name, path));
2579 		}
2580 
2581 	}
2582 
2583 	/* now fill the vector<string> with the names we have */
2584 
2585 	for (UserPresetMap::iterator i = user_preset_map.begin(); i != user_preset_map.end(); ++i) {
2586 		_presets.insert (make_pair (i->second, Plugin::PresetRecord (i->second, i->first)));
2587 		DEBUG_TRACE (DEBUG::AudioUnitConfig, string_compose("AU Adding User Preset: %1 > %2\n", i->first, i->second));
2588 	}
2589 
2590 	/* add factory presets */
2591 
2592 	for (FactoryPresetMap::iterator i = factory_preset_map.begin(); i != factory_preset_map.end(); ++i) {
2593 		string const uri = string_compose ("AU2:%1", std::setw(4), std::setfill('0'), i->second);
2594 		_presets.insert (make_pair (uri, Plugin::PresetRecord (uri, i->first, false)));
2595 		DEBUG_TRACE (DEBUG::AudioUnitConfig, string_compose("AU Adding Factory Preset: %1 > %2\n", i->first, i->second));
2596 	}
2597 }
2598 
2599 bool
has_editor() const2600 AUPlugin::has_editor () const
2601 {
2602 	// even if the plugin doesn't have its own editor, the AU API can be used
2603 	// to create one that looks native.
2604 	return true;
2605 }
2606 
AUPluginInfo(boost::shared_ptr<CAComponentDescription> d)2607 AUPluginInfo::AUPluginInfo (boost::shared_ptr<CAComponentDescription> d)
2608 	: descriptor (d)
2609 	, version (0)
2610 	, max_outputs (0)
2611 {
2612 	type = ARDOUR::AudioUnit;
2613 }
2614 
2615 PluginPtr
load(Session & session)2616 AUPluginInfo::load (Session& session)
2617 {
2618 	try {
2619 		PluginPtr plugin;
2620 
2621 		DEBUG_TRACE (DEBUG::AudioUnitConfig, "load AU as a component\n");
2622 		boost::shared_ptr<CAComponent> comp (new CAComponent(*descriptor));
2623 
2624 		if (!comp->IsValid()) {
2625 			error << ("AudioUnit: not a valid Component") << endmsg;
2626 			return PluginPtr ();
2627 		} else {
2628 			plugin.reset (new AUPlugin (session.engine(), session, comp));
2629 		}
2630 
2631 		AUPluginInfo *aup = new AUPluginInfo (*this);
2632 		DEBUG_TRACE (DEBUG::AudioUnitConfig, string_compose ("plugin info for %1 = %2\n", this, aup));
2633 		plugin->set_info (PluginInfoPtr (aup));
2634 		boost::dynamic_pointer_cast<AUPlugin> (plugin)->set_fixed_size_buffers (aup->creator == "Universal Audio");
2635 		return plugin;
2636 	}
2637 
2638 	catch (failed_constructor &err) {
2639 		DEBUG_TRACE (DEBUG::AudioUnitConfig, "failed to load component/plugin\n");
2640 		return PluginPtr ();
2641 	}
2642 }
2643 
2644 std::vector<Plugin::PresetRecord>
get_presets(bool user_only) const2645 AUPluginInfo::get_presets (bool user_only) const
2646 {
2647 	std::vector<Plugin::PresetRecord> p;
2648 	boost::shared_ptr<CAComponent> comp;
2649 
2650 	try {
2651 		comp = boost::shared_ptr<CAComponent>(new CAComponent(*descriptor));
2652 		if (!comp->IsValid()) {
2653 			throw failed_constructor();
2654 		}
2655 	} catch (failed_constructor& err) {
2656 		return p;
2657 	}
2658 
2659 	// user presets
2660 
2661 	if (!preset_search_path_initialized) {
2662 		Glib::ustring p = Glib::get_home_dir();
2663 		p += "/Library/Audio/Presets:";
2664 		p += preset_search_path;
2665 		preset_search_path = p;
2666 		preset_search_path_initialized = true;
2667 		DEBUG_TRACE (DEBUG::AudioUnitConfig, string_compose("AU Preset Path: %1\n", preset_search_path));
2668 	}
2669 
2670 	vector<string> preset_files;
2671 	find_files_matching_filter (preset_files, preset_search_path, au_preset_filter, const_cast<AUPluginInfo*>(this), true, true, true);
2672 
2673 	for (vector<string>::iterator x = preset_files.begin(); x != preset_files.end(); ++x) {
2674 		string path = *x;
2675 		string preset_name;
2676 		preset_name = Glib::path_get_basename (path);
2677 		preset_name = preset_name.substr (0, preset_name.find_last_of ('.'));
2678 		if (check_and_get_preset_name (comp.get()->Comp(), path, preset_name)) {
2679 			p.push_back (Plugin::PresetRecord (path, preset_name));
2680 		}
2681 	}
2682 
2683 	if (user_only) {
2684 		return p;
2685 	}
2686 
2687 	// factory presets
2688 
2689 	CFArrayRef presets;
2690 	UInt32 dataSize;
2691 	Boolean isWritable;
2692 
2693 	boost::shared_ptr<CAAudioUnit> unit (new CAAudioUnit);
2694 	if (noErr != CAAudioUnit::Open (*(comp.get()), *unit)) {
2695 		return p;
2696 	}
2697 	if (noErr != unit->GetPropertyInfo (kAudioUnitProperty_FactoryPresets, kAudioUnitScope_Global, 0, &dataSize, &isWritable)) {
2698 		unit->Uninitialize ();
2699 		return p;
2700 	}
2701 	if (noErr != unit->GetProperty (kAudioUnitProperty_FactoryPresets, kAudioUnitScope_Global, 0, (void*) &presets, &dataSize)) {
2702 		unit->Uninitialize ();
2703 		return p;
2704 	}
2705 	if (!presets) {
2706 		unit->Uninitialize ();
2707 		return p;
2708 	}
2709 
2710 	CFIndex cnt = CFArrayGetCount (presets);
2711 	for (CFIndex i = 0; i < cnt; ++i) {
2712 		AUPreset* preset = (AUPreset*) CFArrayGetValueAtIndex (presets, i);
2713 		string const uri = string_compose ("%1", i);
2714 		string name = CFStringRefToStdString (preset->presetName);
2715 		p.push_back (Plugin::PresetRecord (uri, name, false));
2716 	}
2717 	CFRelease (presets);
2718 	unit->Uninitialize ();
2719 
2720 	return p;
2721 }
2722 
2723 Glib::ustring
au_cache_path()2724 AUPluginInfo::au_cache_path ()
2725 {
2726 	return Glib::build_filename (ARDOUR::user_cache_directory(), "au_cache");
2727 }
2728 
2729 PluginInfoList*
discover(bool scan_only)2730 AUPluginInfo::discover (bool scan_only)
2731 {
2732 	XMLTree tree;
2733 
2734 	/* AU require a CAComponentDescription pointer provided by the OS.
2735 	 * Ardour only caches port and i/o config. It can't just 'scan' without
2736 	 * 'discovering' (like we do for VST).
2737 	 *
2738 	 * "Scan Only" means
2739 	 * "Iterate over all plugins. skip the ones where there's no io-cache".
2740 	 */
2741 	_scan_only = scan_only;
2742 
2743 	if (!Glib::file_test (au_cache_path(), Glib::FILE_TEST_EXISTS)) {
2744 		ARDOUR::BootMessage (_("Discovering AudioUnit plugins (could take some time ...)"));
2745 		// flush RAM cache -- after clear_cache()
2746 		cached_info.clear();
2747 	}
2748 	// create crash log file
2749 	au_start_crashlog ();
2750 
2751 	PluginInfoList* plugs = new PluginInfoList;
2752 
2753 	discover_fx (*plugs);
2754 	discover_music (*plugs);
2755 	discover_generators (*plugs);
2756 	discover_instruments (*plugs);
2757 
2758 	// all fine if we get here
2759 	au_remove_crashlog ();
2760 
2761 	DEBUG_TRACE (DEBUG::PluginManager, string_compose ("AU: discovered %1 plugins\n", plugs->size()));
2762 
2763 	return plugs;
2764 }
2765 
2766 void
discover_music(PluginInfoList & plugs)2767 AUPluginInfo::discover_music (PluginInfoList& plugs)
2768 {
2769 	CAComponentDescription desc;
2770 	desc.componentFlags = 0;
2771 	desc.componentFlagsMask = 0;
2772 	desc.componentSubType = 0;
2773 	desc.componentManufacturer = 0;
2774 	desc.componentType = kAudioUnitType_MusicEffect;
2775 
2776 	discover_by_description (plugs, desc);
2777 }
2778 
2779 void
discover_fx(PluginInfoList & plugs)2780 AUPluginInfo::discover_fx (PluginInfoList& plugs)
2781 {
2782 	CAComponentDescription desc;
2783 	desc.componentFlags = 0;
2784 	desc.componentFlagsMask = 0;
2785 	desc.componentSubType = 0;
2786 	desc.componentManufacturer = 0;
2787 	desc.componentType = kAudioUnitType_Effect;
2788 
2789 	discover_by_description (plugs, desc);
2790 }
2791 
2792 void
discover_generators(PluginInfoList & plugs)2793 AUPluginInfo::discover_generators (PluginInfoList& plugs)
2794 {
2795 	CAComponentDescription desc;
2796 	desc.componentFlags = 0;
2797 	desc.componentFlagsMask = 0;
2798 	desc.componentSubType = 0;
2799 	desc.componentManufacturer = 0;
2800 	desc.componentType = kAudioUnitType_Generator;
2801 
2802 	discover_by_description (plugs, desc);
2803 }
2804 
2805 void
discover_instruments(PluginInfoList & plugs)2806 AUPluginInfo::discover_instruments (PluginInfoList& plugs)
2807 {
2808 	CAComponentDescription desc;
2809 	desc.componentFlags = 0;
2810 	desc.componentFlagsMask = 0;
2811 	desc.componentSubType = 0;
2812 	desc.componentManufacturer = 0;
2813 	desc.componentType = kAudioUnitType_MusicDevice;
2814 
2815 	discover_by_description (plugs, desc);
2816 }
2817 
2818 
2819 bool
au_get_crashlog(std::string & msg)2820 AUPluginInfo::au_get_crashlog (std::string &msg)
2821 {
2822 	string fn = Glib::build_filename (ARDOUR::user_cache_directory(), "au_crashlog.txt");
2823 	if (!Glib::file_test (fn, Glib::FILE_TEST_EXISTS)) {
2824 		return false;
2825 	}
2826 	std::ifstream ifs(fn.c_str());
2827 	msg.assign ((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));
2828 	au_remove_crashlog ();
2829 	return true;
2830 }
2831 
2832 void
au_start_crashlog()2833 AUPluginInfo::au_start_crashlog ()
2834 {
2835 	string fn = Glib::build_filename (ARDOUR::user_cache_directory(), "au_crashlog.txt");
2836 	assert(!_crashlog_fd);
2837 	DEBUG_TRACE (DEBUG::AudioUnitConfig, string_compose ("Creating AU Log: %1\n", fn));
2838 	if (!(_crashlog_fd = fopen(fn.c_str(), "w"))) {
2839 		PBD::error << "Cannot create AU error-log" << fn << "\n";
2840 		cerr << "Cannot create AU error-log" << fn << "\n";
2841 	}
2842 }
2843 
2844 void
au_remove_crashlog()2845 AUPluginInfo::au_remove_crashlog ()
2846 {
2847 	if (_crashlog_fd) {
2848 		::fclose(_crashlog_fd);
2849 		_crashlog_fd = NULL;
2850 	}
2851 	string fn = Glib::build_filename (ARDOUR::user_cache_directory(), "au_crashlog.txt");
2852 	::g_unlink(fn.c_str());
2853 	DEBUG_TRACE (DEBUG::AudioUnitConfig, string_compose ("Remove AU Log: %1\n", fn));
2854 }
2855 
2856 
2857 void
au_crashlog(std::string msg)2858 AUPluginInfo::au_crashlog (std::string msg)
2859 {
2860 	if (!_crashlog_fd) {
2861 		fprintf(stderr, "AU: %s\n", msg.c_str());
2862 	} else {
2863 		fprintf(_crashlog_fd, "AU: %s\n", msg.c_str());
2864 		::fflush(_crashlog_fd);
2865 	}
2866 }
2867 
2868 void
discover_by_description(PluginInfoList & plugs,CAComponentDescription & desc)2869 AUPluginInfo::discover_by_description (PluginInfoList& plugs, CAComponentDescription& desc)
2870 {
2871 	ArdourComponent comp = 0;
2872 	au_crashlog(string_compose("Start AU discovery for Type: %1", (int)desc.componentType));
2873 
2874 	comp = ArdourFindNext (NULL, &desc);
2875 
2876 	while (comp != NULL) {
2877 		CAComponentDescription temp;
2878 #ifdef COREAUDIO105
2879 		GetComponentInfo (comp, &temp, NULL, NULL, NULL);
2880 #else
2881 		AudioComponentGetDescription (comp, &temp);
2882 #endif
2883 		CFStringRef itemName = NULL;
2884 
2885 		{
2886 			if (itemName != NULL) CFRelease(itemName);
2887 			CFStringRef compTypeString = UTCreateStringForOSType(temp.componentType);
2888 			CFStringRef compSubTypeString = UTCreateStringForOSType(temp.componentSubType);
2889 			CFStringRef compManufacturerString = UTCreateStringForOSType(temp.componentManufacturer);
2890 			itemName = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%@ - %@ - %@"),
2891 					compTypeString, compManufacturerString, compSubTypeString);
2892 			au_crashlog(string_compose("Scanning ID: %1", CFStringRefToStdString(itemName)));
2893 			if (compTypeString != NULL)
2894 				CFRelease(compTypeString);
2895 			if (compSubTypeString != NULL)
2896 				CFRelease(compSubTypeString);
2897 			if (compManufacturerString != NULL)
2898 				CFRelease(compManufacturerString);
2899 		}
2900 
2901 		if (is_blacklisted(CFStringRefToStdString(itemName))) {
2902 			info << string_compose (_("Skipped blacklisted AU plugin %1 "), CFStringRefToStdString(itemName)) << endmsg;
2903 			if (itemName != NULL) {
2904 				CFRelease(itemName);
2905 				itemName = NULL;
2906 			}
2907 			comp = ArdourFindNext (comp, &desc);
2908 			continue;
2909 		}
2910 
2911 		bool has_midi_in = false;
2912 
2913 		AUPluginInfoPtr info (new AUPluginInfo
2914 				      (boost::shared_ptr<CAComponentDescription> (new CAComponentDescription(temp))));
2915 
2916 		/* although apple designed the subtype field to be a "category" indicator,
2917 		   its really turned into a plugin ID field for a given manufacturer. Hence
2918 		   there are no categories for AudioUnits. However, to keep the plugins
2919 		   showing up under "categories", we'll use the "type" as a high level
2920 		   selector.
2921 
2922 		   NOTE: no panners, format converters or i/o AU's for our purposes
2923 		 */
2924 
2925 		switch (info->descriptor->Type()) {
2926 		case kAudioUnitType_Panner:
2927 		case kAudioUnitType_OfflineEffect:
2928 		case kAudioUnitType_FormatConverter:
2929 			comp = ArdourFindNext (comp, &desc);
2930 			continue;
2931 
2932 		case kAudioUnitType_Output:
2933 			info->category = _("Output");
2934 			break;
2935 		case kAudioUnitType_MusicDevice:
2936 			info->category = _("Instrument");
2937 			has_midi_in = true;
2938 			break;
2939 		case kAudioUnitType_MusicEffect:
2940 			info->category = _("Effect");
2941 			has_midi_in = true;
2942 			break;
2943 		case kAudioUnitType_Effect:
2944 			info->category = _("Effect");
2945 			break;
2946 		case kAudioUnitType_Mixer:
2947 			info->category = _("Mixer");
2948 			break;
2949 		case kAudioUnitType_Generator:
2950 			info->category = _("Generator");
2951 			break;
2952 		default:
2953 			info->category = _("(Unknown)");
2954 			break;
2955 		}
2956 
2957 		au_blacklist(CFStringRefToStdString(itemName));
2958 #ifdef COREAUDIO105
2959 		get_names (temp, info->name, info->creator);
2960 #else
2961 		get_names (comp, info->name, info->creator);
2962 #endif
2963 		ARDOUR::PluginScanMessage(_("AU"), info->name, false);
2964 		au_crashlog(string_compose("Plugin: %1", info->name));
2965 
2966 		info->type = ARDOUR::AudioUnit;
2967 		info->unique_id = stringify_descriptor (*info->descriptor);
2968 
2969 		/* XXX not sure of the best way to handle plugin versioning yet */
2970 
2971 		CAComponent cacomp (*info->descriptor);
2972 
2973 #ifdef COREAUDIO105
2974 		if (cacomp.GetResourceVersion (info->version) != noErr)
2975 #else
2976 		if (cacomp.GetVersion (info->version) != noErr)
2977 #endif
2978 		{
2979 			info->version = 0;
2980 		}
2981 
2982 		const int rv = cached_io_configuration (info->unique_id, info->version, cacomp, info->cache, info->name);
2983 
2984 		info->max_outputs = 0;
2985 
2986 		if (rv == 0) {
2987 			/* here we have to map apple's wildcard system to a simple pair
2988 			   of values. in ::can_do() we use the whole system, but here
2989 			   we need a single pair of values. XXX probably means we should
2990 			   remove any use of these values.
2991 
2992 			   for now, if the plugin provides a wildcard, treat it as 1. we really
2993 			   don't care much, because whether we can handle an i/o configuration
2994 			   depends upon ::configure_variable_io(), not these counts.
2995 
2996 			   they exist because other parts of ardour try to present i/o configuration
2997 			   info to the user, which should perhaps be revisited.
2998 			*/
2999 
3000 			const vector<pair<int,int> >& ioc (info->cache.io_configs);
3001 			for (vector<pair<int,int> >::const_iterator i = ioc.begin(); i != ioc.end(); ++i) {
3002 				int32_t possible_out = i->second;
3003 				if (possible_out < 0) {
3004 					continue;
3005 				} else if (possible_out > info->max_outputs) {
3006 					info->max_outputs = possible_out;
3007 				}
3008 			}
3009 
3010 			int32_t possible_in = ioc.front().first;
3011 			int32_t possible_out = ioc.front().second;
3012 
3013 			if (possible_in > 0) {
3014 				info->n_inputs.set (DataType::AUDIO, possible_in);
3015 			} else {
3016 				info->n_inputs.set (DataType::AUDIO, 1);
3017 			}
3018 
3019 			info->n_inputs.set (DataType::MIDI, has_midi_in ? 1 : 0);
3020 
3021 			if (possible_out > 0) {
3022 				info->n_outputs.set (DataType::AUDIO, possible_out);
3023 			} else {
3024 				info->n_outputs.set (DataType::AUDIO, 1);
3025 			}
3026 
3027 			DEBUG_TRACE (DEBUG::AudioUnitConfig, string_compose ("detected AU %1 with %2 i/o configurations - %3\n",
3028 									info->name.c_str(), info->cache.io_configs.size(), info->unique_id));
3029 
3030 			plugs.push_back (info);
3031 
3032 		}
3033 		else if (rv == -1) {
3034 			error << string_compose (_("Cannot get I/O configuration info for AU %1"), info->name) << endmsg;
3035 		}
3036 
3037 		au_unblacklist(CFStringRefToStdString(itemName));
3038 		au_crashlog("Success.");
3039 		comp = ArdourFindNext (comp, &desc);
3040 		if (itemName != NULL) CFRelease(itemName); itemName = NULL;
3041 	}
3042 	au_crashlog(string_compose("End AU discovery for Type: %1", (int)desc.componentType));
3043 }
3044 
3045 int
cached_io_configuration(const std::string & unique_id,UInt32 version,CAComponent & comp,AUPluginCachedInfo & cinfo,const std::string & name)3046 AUPluginInfo::cached_io_configuration (const std::string& unique_id,
3047 				       UInt32 version,
3048 				       CAComponent& comp,
3049 				       AUPluginCachedInfo& cinfo,
3050 				       const std::string& name)
3051 {
3052 	std::string id;
3053 	char buf[32];
3054 
3055 	/* concatenate unique ID with version to provide a key for cached info lookup.
3056 	   this ensures we don't get stale information, or should if plugin developers
3057 	   follow Apple "guidelines".
3058 	 */
3059 
3060 	snprintf (buf, sizeof (buf), "%u", (uint32_t) version);
3061 	id = unique_id;
3062 	id += '/';
3063 	id += buf;
3064 
3065 	CachedInfoMap::iterator cim = cached_info.find (id);
3066 
3067 	if (cim != cached_info.end()) {
3068 		cinfo = cim->second;
3069 		return 0;
3070 	}
3071 
3072 	if (_scan_only) {
3073 		PBD::info << string_compose (_("Skipping AU %1 (not indexed. Discover new plugins to add)"), name) << endmsg;
3074 		return 1;
3075 	}
3076 
3077 	CAAudioUnit unit;
3078 	AUChannelInfo* channel_info;
3079 	UInt32 cnt;
3080 	int ret;
3081 
3082 	ARDOUR::BootMessage (string_compose (_("Checking AudioUnit: %1"), name));
3083 
3084 	try {
3085 
3086 		if (CAAudioUnit::Open (comp, unit) != noErr) {
3087 			return -1;
3088 		}
3089 
3090 	} catch (...) {
3091 
3092 		warning << string_compose (_("Could not load AU plugin %1 - ignored"), name) << endmsg;
3093 		return -1;
3094 
3095 	}
3096 
3097 	DEBUG_TRACE (DEBUG::AudioUnitConfig, "get AU channel info\n");
3098 	if ((ret = unit.GetChannelInfo (&channel_info, cnt)) < 0) {
3099 		return -1;
3100 	}
3101 
3102 	if (ret > 0) {
3103 		/* AU is expected to deal with same channel valance in and out */
3104 		cinfo.io_configs.push_back (pair<int,int> (-1, -1));
3105 	} else {
3106 		/* CAAudioUnit::GetChannelInfo silently merges bus formats
3107 		 * check if this was the case and if so, add
3108 		 * bus configs as incremental options.
3109 		 */
3110 		Boolean* isWritable = 0;
3111 		UInt32   dataSize   = 0;
3112 		OSStatus result = AudioUnitGetPropertyInfo (unit.AU(),
3113 				kAudioUnitProperty_SupportedNumChannels,
3114 				kAudioUnitScope_Global, 0,
3115 				&dataSize, isWritable);
3116 		if (result != noErr && (comp.Desc().IsGenerator() || comp.Desc().IsMusicDevice())) {
3117 			/* incrementally add busses */
3118 			int in = 0;
3119 			int out = 0;
3120 			for (uint32_t n = 0; n < cnt; ++n) {
3121 				in += channel_info[n].inChannels;
3122 				out += channel_info[n].outChannels;
3123 				cinfo.io_configs.push_back (pair<int,int> (in, out));
3124 			}
3125 		} else {
3126 			/* store each configuration */
3127 			for (uint32_t n = 0; n < cnt; ++n) {
3128 				cinfo.io_configs.push_back (pair<int,int> (channel_info[n].inChannels,
3129 							channel_info[n].outChannels));
3130 			}
3131 		}
3132 
3133 		free (channel_info);
3134 	}
3135 
3136 	add_cached_info (id, cinfo);
3137 	save_cached_info ();
3138 
3139 	return 0;
3140 }
3141 
3142 void
clear_cache()3143 AUPluginInfo::clear_cache ()
3144 {
3145 	const string& fn = au_cache_path();
3146 	if (Glib::file_test (fn, Glib::FILE_TEST_EXISTS)) {
3147 		::g_unlink(fn.c_str());
3148 	}
3149 	// keep cached_info in RAM until restart or re-scan
3150 	cached_info.clear();
3151 }
3152 
3153 void
add_cached_info(const std::string & id,AUPluginCachedInfo & cinfo)3154 AUPluginInfo::add_cached_info (const std::string& id, AUPluginCachedInfo& cinfo)
3155 {
3156 	cached_info[id] = cinfo;
3157 }
3158 
3159 #define AU_CACHE_VERSION "2.0"
3160 
3161 void
save_cached_info()3162 AUPluginInfo::save_cached_info ()
3163 {
3164 	XMLNode* node;
3165 
3166 	node = new XMLNode (X_("AudioUnitPluginCache"));
3167 	node->set_property( "version", AU_CACHE_VERSION );
3168 
3169 	for (map<string,AUPluginCachedInfo>::iterator i = cached_info.begin(); i != cached_info.end(); ++i) {
3170 		XMLNode* parent = new XMLNode (X_("plugin"));
3171 		parent->set_property ("id", i->first);
3172 		node->add_child_nocopy (*parent);
3173 
3174 		for (vector<pair<int, int> >::iterator j = i->second.io_configs.begin(); j != i->second.io_configs.end(); ++j) {
3175 
3176 			XMLNode* child = new XMLNode (X_("io"));
3177 
3178 			child->set_property (X_("in"), j->first);
3179 			child->set_property (X_("out"), j->second);
3180 			parent->add_child_nocopy (*child);
3181 		}
3182 
3183 	}
3184 
3185 	Glib::ustring path = au_cache_path ();
3186 	XMLTree tree;
3187 
3188 	tree.set_root (node);
3189 
3190 	if (!tree.write (path)) {
3191 		error << string_compose (_("could not save AU cache to %1"), path) << endmsg;
3192 		g_unlink (path.c_str());
3193 	}
3194 }
3195 
3196 int
load_cached_info()3197 AUPluginInfo::load_cached_info ()
3198 {
3199 	Glib::ustring path = au_cache_path ();
3200 	XMLTree tree;
3201 
3202 	if (!Glib::file_test (path, Glib::FILE_TEST_EXISTS)) {
3203 		return 0;
3204 	}
3205 
3206 	if ( !tree.read (path) ) {
3207 		error << "au_cache is not a valid XML file.  AU plugins will be re-scanned" << endmsg;
3208 		return -1;
3209 	}
3210 
3211 	const XMLNode* root (tree.root());
3212 
3213 	if (root->name() != X_("AudioUnitPluginCache")) {
3214 		return -1;
3215 	}
3216 
3217 	//initial version has incorrectly stored i/o info, and/or garbage chars.
3218 	XMLProperty const * version = root->property(X_("version"));
3219 	if (! ((version != NULL) && (version->value() == X_(AU_CACHE_VERSION)))) {
3220 		error << "au_cache is not correct version.  AU plugins will be re-scanned" << endmsg;
3221 		return -1;
3222 	}
3223 
3224 	cached_info.clear ();
3225 
3226 	const XMLNodeList children = root->children();
3227 
3228 	for (XMLNodeConstIterator iter = children.begin(); iter != children.end(); ++iter) {
3229 
3230 		const XMLNode* child = *iter;
3231 
3232 		if (child->name() == X_("plugin")) {
3233 
3234 			const XMLNode* gchild;
3235 			const XMLNodeList gchildren = child->children();
3236 
3237 			string id;
3238 			if (!child->get_property (X_("id"), id)) {
3239 				continue;
3240 			}
3241 
3242 			string fixed;
3243 			string version;
3244 
3245 			string::size_type slash = id.find_last_of ('/');
3246 
3247 			if (slash == string::npos) {
3248 				continue;
3249 			}
3250 
3251 			version = id.substr (slash);
3252 			id = id.substr (0, slash);
3253 			fixed = AUPlugin::maybe_fix_broken_au_id (id);
3254 
3255 			if (fixed.empty()) {
3256 				error << string_compose (_("Your AudioUnit configuration cache contains an AU plugin whose ID cannot be understood - ignored (%1)"), id) << endmsg;
3257 				continue;
3258 			}
3259 
3260 			id = fixed;
3261 			id += version;
3262 
3263 			AUPluginCachedInfo cinfo;
3264 
3265 			for (XMLNodeConstIterator giter = gchildren.begin(); giter != gchildren.end(); giter++) {
3266 
3267 				gchild = *giter;
3268 
3269 				if (gchild->name() == X_("io")) {
3270 
3271 					int32_t in;
3272 					int32_t out;
3273 
3274 					if (gchild->get_property (X_("in"), in) && gchild->get_property (X_("out"), out)) {
3275 						cinfo.io_configs.push_back (pair<int,int> (in, out));
3276 					}
3277 				}
3278 			}
3279 
3280 			if (cinfo.io_configs.size()) {
3281 				add_cached_info (id, cinfo);
3282 			}
3283 		}
3284 	}
3285 
3286 	return 0;
3287 }
3288 
3289 
3290 std::string
stringify_descriptor(const CAComponentDescription & desc)3291 AUPluginInfo::stringify_descriptor (const CAComponentDescription& desc)
3292 {
3293 	stringstream s;
3294 
3295 	/* note: OSType is a compiler-implemenation-defined value,
3296 	   historically a 32 bit integer created with a multi-character
3297 	   constant such as 'abcd'. It is, fundamentally, an abomination.
3298 	*/
3299 
3300 	s << desc.Type();
3301 	s << '-';
3302 	s << desc.SubType();
3303 	s << '-';
3304 	s << desc.Manu();
3305 
3306 	return s.str();
3307 }
3308 
3309 bool
needs_midi_input() const3310 AUPluginInfo::needs_midi_input () const
3311 {
3312 	return is_effect_with_midi_input () || is_instrument ();
3313 }
3314 
3315 bool
is_effect() const3316 AUPluginInfo::is_effect () const
3317 {
3318 	return is_effect_without_midi_input() || is_effect_with_midi_input();
3319 }
3320 
3321 bool
is_effect_without_midi_input() const3322 AUPluginInfo::is_effect_without_midi_input () const
3323 {
3324 	return descriptor->IsAUFX();
3325 }
3326 
3327 bool
is_effect_with_midi_input() const3328 AUPluginInfo::is_effect_with_midi_input () const
3329 {
3330 	return descriptor->IsAUFM();
3331 }
3332 
3333 bool
is_instrument() const3334 AUPluginInfo::is_instrument () const
3335 {
3336 	return descriptor->IsMusicDevice();
3337 }
3338 
3339 bool
is_utility() const3340 AUPluginInfo::is_utility () const
3341 {
3342 	return (descriptor->IsGenerator() || descriptor->componentType == 'aumi');
3343 	// kAudioUnitType_MidiProcessor  ..looks like we aren't even scanning for these yet?
3344 }
3345 
3346 void
set_info(PluginInfoPtr info)3347 AUPlugin::set_info (PluginInfoPtr info)
3348 {
3349 	Plugin::set_info (info);
3350 
3351 	AUPluginInfoPtr pinfo = boost::dynamic_pointer_cast<AUPluginInfo>(get_info());
3352 	_has_midi_input = pinfo->needs_midi_input ();
3353 	_has_midi_output = false;
3354 }
3355 
3356 int
create_parameter_listener(AUEventListenerProc cb,void * arg,float interval_secs)3357 AUPlugin::create_parameter_listener (AUEventListenerProc cb, void* arg, float interval_secs)
3358 {
3359 #ifdef WITH_CARBON
3360 	CFRunLoopRef run_loop = (CFRunLoopRef) GetCFRunLoopFromEventLoop(GetCurrentEventLoop());
3361 #else
3362 	CFRunLoopRef run_loop = CFRunLoopGetCurrent();
3363 #endif
3364 	CFStringRef  loop_mode = kCFRunLoopDefaultMode;
3365 
3366 	if (AUEventListenerCreate (cb, arg, run_loop, loop_mode, interval_secs, interval_secs, &_parameter_listener) != noErr) {
3367 		return -1;
3368 	}
3369 
3370 	_parameter_listener_arg = arg;
3371 
3372 	// listen for latency changes
3373 	AudioUnitEvent event;
3374 	event.mEventType = kAudioUnitEvent_PropertyChange;
3375 	event.mArgument.mProperty.mAudioUnit = unit->AU();
3376 	event.mArgument.mProperty.mPropertyID = kAudioUnitProperty_Latency;
3377 	event.mArgument.mProperty.mScope = kAudioUnitScope_Global;
3378 	event.mArgument.mProperty.mElement = 0;
3379 
3380 	if (AUEventListenerAddEventType (_parameter_listener, _parameter_listener_arg, &event) != noErr) {
3381 		PBD::error << "Failed to create latency event listener\n";
3382 		// TODO don't cache _current_latency
3383 	}
3384 
3385 	return 0;
3386 }
3387 
3388 int
listen_to_parameter(uint32_t param_id)3389 AUPlugin::listen_to_parameter (uint32_t param_id)
3390 {
3391 	AudioUnitEvent      event;
3392 
3393 	if (!_parameter_listener || param_id >= descriptors.size()) {
3394 		return -2;
3395 	}
3396 
3397 	event.mEventType = kAudioUnitEvent_ParameterValueChange;
3398 	event.mArgument.mParameter.mAudioUnit = unit->AU();
3399 	event.mArgument.mParameter.mParameterID = descriptors[param_id].id;
3400 	event.mArgument.mParameter.mScope = descriptors[param_id].scope;
3401 	event.mArgument.mParameter.mElement = descriptors[param_id].element;
3402 
3403 	if (AUEventListenerAddEventType (_parameter_listener, _parameter_listener_arg, &event) != noErr) {
3404 		return -1;
3405 	}
3406 
3407 	event.mEventType = kAudioUnitEvent_BeginParameterChangeGesture;
3408 	event.mArgument.mParameter.mAudioUnit = unit->AU();
3409 	event.mArgument.mParameter.mParameterID = descriptors[param_id].id;
3410 	event.mArgument.mParameter.mScope = descriptors[param_id].scope;
3411 	event.mArgument.mParameter.mElement = descriptors[param_id].element;
3412 
3413 	if (AUEventListenerAddEventType (_parameter_listener, _parameter_listener_arg, &event) != noErr) {
3414 		return -1;
3415 	}
3416 
3417 	event.mEventType = kAudioUnitEvent_EndParameterChangeGesture;
3418 	event.mArgument.mParameter.mAudioUnit = unit->AU();
3419 	event.mArgument.mParameter.mParameterID = descriptors[param_id].id;
3420 	event.mArgument.mParameter.mScope = descriptors[param_id].scope;
3421 	event.mArgument.mParameter.mElement = descriptors[param_id].element;
3422 
3423 	if (AUEventListenerAddEventType (_parameter_listener, _parameter_listener_arg, &event) != noErr) {
3424 		return -1;
3425 	}
3426 
3427 	return 0;
3428 }
3429 
3430 int
end_listen_to_parameter(uint32_t param_id)3431 AUPlugin::end_listen_to_parameter (uint32_t param_id)
3432 {
3433 	AudioUnitEvent      event;
3434 
3435 	if (!_parameter_listener || param_id >= descriptors.size()) {
3436 		return -2;
3437 	}
3438 
3439 	event.mEventType = kAudioUnitEvent_ParameterValueChange;
3440 	event.mArgument.mParameter.mAudioUnit = unit->AU();
3441 	event.mArgument.mParameter.mParameterID = descriptors[param_id].id;
3442 	event.mArgument.mParameter.mScope = descriptors[param_id].scope;
3443 	event.mArgument.mParameter.mElement = descriptors[param_id].element;
3444 
3445 	if (AUEventListenerRemoveEventType (_parameter_listener, _parameter_listener_arg, &event) != noErr) {
3446 		return -1;
3447 	}
3448 
3449 	event.mEventType = kAudioUnitEvent_BeginParameterChangeGesture;
3450 	event.mArgument.mParameter.mAudioUnit = unit->AU();
3451 	event.mArgument.mParameter.mParameterID = descriptors[param_id].id;
3452 	event.mArgument.mParameter.mScope = descriptors[param_id].scope;
3453 	event.mArgument.mParameter.mElement = descriptors[param_id].element;
3454 
3455 	if (AUEventListenerRemoveEventType (_parameter_listener, _parameter_listener_arg, &event) != noErr) {
3456 		return -1;
3457 	}
3458 
3459 	event.mEventType = kAudioUnitEvent_EndParameterChangeGesture;
3460 	event.mArgument.mParameter.mAudioUnit = unit->AU();
3461 	event.mArgument.mParameter.mParameterID = descriptors[param_id].id;
3462 	event.mArgument.mParameter.mScope = descriptors[param_id].scope;
3463 	event.mArgument.mParameter.mElement = descriptors[param_id].element;
3464 
3465 	if (AUEventListenerRemoveEventType (_parameter_listener, _parameter_listener_arg, &event) != noErr) {
3466 		return -1;
3467 	}
3468 
3469 	return 0;
3470 }
3471 
3472 void
_parameter_change_listener(void * arg,void * src,const AudioUnitEvent * event,UInt64 host_time,Float32 new_value)3473 AUPlugin::_parameter_change_listener (void* arg, void* src, const AudioUnitEvent* event, UInt64 host_time, Float32 new_value)
3474 {
3475 	((AUPlugin*) arg)->parameter_change_listener (arg, src, event, host_time, new_value);
3476 }
3477 
3478 void
parameter_change_listener(void *,void * src,const AudioUnitEvent * event,UInt64,Float32 new_value)3479 AUPlugin::parameter_change_listener (void* /*arg*/, void* src, const AudioUnitEvent* event, UInt64 /*host_time*/, Float32 new_value)
3480 {
3481 	if (event->mEventType == kAudioUnitEvent_PropertyChange) {
3482 		if (event->mArgument.mProperty.mPropertyID == kAudioUnitProperty_Latency) {
3483 			DEBUG_TRACE (DEBUG::AudioUnitConfig, string_compose("AU Latency Change Event %1 <> %2\n", new_value, unit->Latency()));
3484 			guint lat = unit->Latency() * _session.sample_rate();
3485 			g_atomic_int_set (&_current_latency, lat);
3486 		}
3487 		return;
3488 	}
3489 
3490         ParameterMap::iterator i;
3491 
3492         if ((i = parameter_map.find (event->mArgument.mParameter.mParameterID)) == parameter_map.end()) {
3493                 return;
3494         }
3495 
3496         switch (event->mEventType) {
3497         case kAudioUnitEvent_BeginParameterChangeGesture:
3498                 StartTouch (i->second);
3499                 break;
3500         case kAudioUnitEvent_EndParameterChangeGesture:
3501                 EndTouch (i->second);
3502                 break;
3503         case kAudioUnitEvent_ParameterValueChange:
3504                 /* whenever we change a parameter, we request that we are NOT notified of the change, so anytime we arrive here, it
3505                    means that something else (i.e. the plugin GUI) made the change.
3506                 */
3507                 if (preset_holdoff > 0) {
3508 	                ParameterChangedExternally (i->second, new_value);
3509                 } else {
3510                         Plugin::parameter_changed_externally (i->second, new_value);
3511 		}
3512                 break;
3513         default:
3514                 break;
3515         }
3516 }
3517