enable icon-start-from-.ardour-file to work on OS X; properly install apple event...
[ardour.git] / libs / ardour / audio_unit.cc
1 /*
2     Copyright (C) 2006 Paul Davis 
3         
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <sstream>
21
22 #include <pbd/transmitter.h>
23 #include <pbd/xml++.h>
24 #include <pbd/whitespace.h>
25
26 #include <glibmm/thread.h>
27
28 #include <ardour/audioengine.h>
29 #include <ardour/io.h>
30 #include <ardour/audio_unit.h>
31 #include <ardour/session.h>
32 #include <ardour/utils.h>
33
34 #include <appleutility/CAAudioUnit.h>
35 #include <appleutility/CAAUParameter.h>
36
37 #include <CoreServices/CoreServices.h>
38 #include <AudioUnit/AudioUnit.h>
39
40 #include "i18n.h"
41
42 using namespace std;
43 using namespace PBD;
44 using namespace ARDOUR;
45
46 static OSStatus 
47 _render_callback(void *userData,
48                  AudioUnitRenderActionFlags *ioActionFlags,
49                  const AudioTimeStamp    *inTimeStamp,
50                  UInt32       inBusNumber,
51                  UInt32       inNumberFrames,
52                  AudioBufferList*       ioData)
53 {
54         return ((AUPlugin*)userData)->render_callback (ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, ioData);
55 }
56
57 AUPlugin::AUPlugin (AudioEngine& engine, Session& session, boost::shared_ptr<CAComponent> _comp)
58         :
59         Plugin (engine, session),
60         comp (_comp),
61         unit (new CAAudioUnit),
62         initialized (false),
63         buffers (0),
64         current_maxbuf (0),
65         current_offset (0),
66         current_buffers (0),
67         frames_processed (0)
68 {                       
69         OSErr err = CAAudioUnit::Open (*(comp.get()), *unit);
70
71         if (err != noErr) {
72                 error << _("AudioUnit: Could not convert CAComponent to CAAudioUnit") << endmsg;
73                 throw failed_constructor ();
74         }
75         
76         AURenderCallbackStruct renderCallbackInfo;
77
78         renderCallbackInfo.inputProc = _render_callback;
79         renderCallbackInfo.inputProcRefCon = this;
80
81         if ((err = unit->SetProperty (kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input, 
82                                          0, (void*) &renderCallbackInfo, sizeof(renderCallbackInfo))) != 0) {
83                 cerr << "cannot install render callback (err = " << err << ')' << endl;
84                 throw failed_constructor();
85         }
86
87         unit->GetElementCount (kAudioUnitScope_Global, global_elements);
88         unit->GetElementCount (kAudioUnitScope_Input, input_elements);
89         unit->GetElementCount (kAudioUnitScope_Output, output_elements);
90
91         // set up the basic stream format. these fields do not change
92                             
93         streamFormat.mSampleRate = session.frame_rate();
94         streamFormat.mFormatID = kAudioFormatLinearPCM;
95         streamFormat.mFormatFlags = kAudioFormatFlagIsFloat|kAudioFormatFlagIsPacked|kAudioFormatFlagIsNonInterleaved;
96
97 #ifdef __LITTLE_ENDIAN__
98         /* relax */
99 #else
100         streamFormat.mFormatFlags |= kAudioFormatFlagIsBigEndian;
101 #endif
102
103         streamFormat.mBitsPerChannel = 32;
104         streamFormat.mFramesPerPacket = 1;
105
106         // subject to later modification as we discover channel counts
107
108         streamFormat.mBytesPerPacket = 4;
109         streamFormat.mBytesPerFrame = 4;
110         streamFormat.mChannelsPerFrame = 1;
111
112         format_set = 0;
113
114         if (_set_block_size (_session.get_block_size())) {
115                 error << _("AUPlugin: cannot set processing block size") << endmsg;
116                 throw failed_constructor();
117         }
118
119         discover_parameters ();
120
121         Plugin::setup_controls ();
122 }
123
124 AUPlugin::~AUPlugin ()
125 {
126         if (unit) {
127                 unit->Uninitialize ();
128         }
129
130         if (buffers) {
131                 free (buffers);
132         }
133 }
134
135 void
136 AUPlugin::discover_parameters ()
137 {
138         /* discover writable parameters */
139         
140         AudioUnitScope scopes[] = { 
141                 kAudioUnitScope_Global,
142                 kAudioUnitScope_Output,
143                 kAudioUnitScope_Input
144         };
145
146         descriptors.clear ();
147
148         for (uint32_t i = 0; i < sizeof (scopes) / sizeof (scopes[0]); ++i) {
149
150                 AUParamInfo param_info (unit->AU(), false, false, scopes[i]);
151                 
152                 for (uint32_t i = 0; i < param_info.NumParams(); ++i) {
153
154                         AUParameterDescriptor d;
155
156                         d.id = param_info.ParamID (i);
157
158                         const CAAUParameter* param = param_info.GetParamInfo (d.id);
159                         const AudioUnitParameterInfo& info (param->ParamInfo());
160
161                         const int len = CFStringGetLength (param->GetName());;
162                         char local_buffer[len*2];
163                         Boolean good = CFStringGetCString(param->GetName(),local_buffer,len*2,kCFStringEncodingMacRoman);
164                         if (!good) {
165                                 d.label = "???";
166                         } else {
167                                 d.label = local_buffer;
168                         }
169
170                         d.scope = param_info.GetScope ();
171                         d.element = param_info.GetElement ();
172
173                         /* info.units to consider */
174                         /*
175                           kAudioUnitParameterUnit_Generic             = 0
176                           kAudioUnitParameterUnit_Indexed             = 1
177                           kAudioUnitParameterUnit_Boolean             = 2
178                           kAudioUnitParameterUnit_Percent             = 3
179                           kAudioUnitParameterUnit_Seconds             = 4
180                           kAudioUnitParameterUnit_SampleFrames        = 5
181                           kAudioUnitParameterUnit_Phase               = 6
182                           kAudioUnitParameterUnit_Rate                = 7
183                           kAudioUnitParameterUnit_Hertz               = 8
184                           kAudioUnitParameterUnit_Cents               = 9
185                           kAudioUnitParameterUnit_RelativeSemiTones   = 10
186                           kAudioUnitParameterUnit_MIDINoteNumber      = 11
187                           kAudioUnitParameterUnit_MIDIController      = 12
188                           kAudioUnitParameterUnit_Decibels            = 13
189                           kAudioUnitParameterUnit_LinearGain          = 14
190                           kAudioUnitParameterUnit_Degrees             = 15
191                           kAudioUnitParameterUnit_EqualPowerCrossfade = 16
192                           kAudioUnitParameterUnit_MixerFaderCurve1    = 17
193                           kAudioUnitParameterUnit_Pan                 = 18
194                           kAudioUnitParameterUnit_Meters              = 19
195                           kAudioUnitParameterUnit_AbsoluteCents       = 20
196                           kAudioUnitParameterUnit_Octaves             = 21
197                           kAudioUnitParameterUnit_BPM                 = 22
198                           kAudioUnitParameterUnit_Beats               = 23
199                           kAudioUnitParameterUnit_Milliseconds        = 24
200                           kAudioUnitParameterUnit_Ratio               = 25
201                         */
202
203                         /* info.flags to consider */
204
205                         /*
206
207                           kAudioUnitParameterFlag_CFNameRelease       = (1L << 4)
208                           kAudioUnitParameterFlag_HasClump            = (1L << 20)
209                           kAudioUnitParameterFlag_HasName             = (1L << 21)
210                           kAudioUnitParameterFlag_DisplayLogarithmic  = (1L << 22)
211                           kAudioUnitParameterFlag_IsHighResolution    = (1L << 23)
212                           kAudioUnitParameterFlag_NonRealTime         = (1L << 24)
213                           kAudioUnitParameterFlag_CanRamp             = (1L << 25)
214                           kAudioUnitParameterFlag_ExpertMode          = (1L << 26)
215                           kAudioUnitParameterFlag_HasCFNameString     = (1L << 27)
216                           kAudioUnitParameterFlag_IsGlobalMeta        = (1L << 28)
217                           kAudioUnitParameterFlag_IsElementMeta       = (1L << 29)
218                           kAudioUnitParameterFlag_IsReadable          = (1L << 30)
219                           kAudioUnitParameterFlag_IsWritable          = (1L << 31)
220                         */
221
222                         d.lower = info.minValue;
223                         d.upper = info.maxValue;
224                         d.default_value = info.defaultValue;
225
226                         d.integer_step = (info.unit & kAudioUnitParameterUnit_Indexed);
227                         d.toggled = (info.unit & kAudioUnitParameterUnit_Boolean) ||
228                                 (d.integer_step && ((d.upper - d.lower) == 1.0));
229                         d.sr_dependent = (info.unit & kAudioUnitParameterUnit_SampleFrames);
230                         d.automatable = !d.toggled && 
231                                 !(info.flags & kAudioUnitParameterFlag_NonRealTime) &&
232                                 (info.flags & kAudioUnitParameterFlag_IsWritable);
233                         
234                         d.logarithmic = (info.flags & kAudioUnitParameterFlag_DisplayLogarithmic);
235                         d.unit = info.unit;
236
237                         d.step = 1.0;
238                         d.smallstep = 0.1;
239                         d.largestep = 10.0;
240                         d.min_unbound = 0; // lower is bound
241                         d.max_unbound = 0; // upper is bound
242
243                         descriptors.push_back (d);
244                 }
245         }
246 }
247
248
249 string
250 AUPlugin::unique_id () const
251 {
252         return AUPluginInfo::stringify_descriptor (comp->Desc());
253 }
254
255 const char *
256 AUPlugin::label () const
257 {
258         return _info->name.c_str();
259 }
260
261 uint32_t
262 AUPlugin::parameter_count () const
263 {
264         return descriptors.size();
265 }
266
267 float
268 AUPlugin::default_value (uint32_t port)
269 {
270         if (port < descriptors.size()) {
271                 return descriptors[port].default_value;
272         }
273
274         return 0;
275 }
276
277 nframes_t
278 AUPlugin::latency () const
279 {
280         return unit->Latency ();
281 }
282
283 void
284 AUPlugin::set_parameter (uint32_t which, float val)
285 {
286         if (which < descriptors.size()) {
287                 const AUParameterDescriptor& d (descriptors[which]);
288                 unit->SetParameter (d.id, d.scope, d.element, val);
289         }
290 }
291
292 float
293 AUPlugin::get_parameter (uint32_t which) const
294 {
295         float val = 0.0;
296         if (which < descriptors.size()) {
297                 const AUParameterDescriptor& d (descriptors[which]);
298                 unit->GetParameter(d.id, d.scope, d.element, val);
299         }
300         return val;
301 }
302
303 int
304 AUPlugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor& pd) const
305 {
306         if (which < descriptors.size()) {
307                 pd = descriptors[which];
308                 return 0;
309         } 
310         return -1;
311 }
312
313 uint32_t
314 AUPlugin::nth_parameter (uint32_t which, bool& ok) const
315 {
316         if (which < descriptors.size()) {
317                 ok = true;
318                 return which;
319         }
320         ok = false;
321         return 0;
322 }
323
324 void
325 AUPlugin::activate ()
326 {
327         if (!initialized) {
328                 OSErr err;
329                 if ((err = unit->Initialize()) != noErr) {
330                         error << string_compose (_("AUPlugin: %1 cannot initialize plugin (err = %2)"), name(), err) << endmsg;
331                 } else {
332                         frames_processed = 0;
333                         initialized = true;
334                 }
335         }
336 }
337
338 void
339 AUPlugin::deactivate ()
340 {
341         unit->GlobalReset ();
342 }
343
344 void
345 AUPlugin::set_block_size (nframes_t nframes)
346 {
347         _set_block_size (nframes);
348 }
349
350 int
351 AUPlugin::_set_block_size (nframes_t nframes)
352 {
353         bool was_initialized = initialized;
354         UInt32 numFrames = nframes;
355         OSErr err;
356
357         if (initialized) {
358                 unit->Uninitialize ();
359         }
360
361         if ((err = unit->SetProperty (kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 
362                                       0, &numFrames, sizeof (numFrames))) != noErr) {
363                 cerr << "cannot set max frames (err = " << err << ')' << endl;
364                 return -1;
365         }
366
367         if (was_initialized) {
368                 activate ();
369         }
370
371         return 0;
372 }
373
374 int32_t 
375 AUPlugin::can_support_input_configuration (int32_t in)
376 {       
377         streamFormat.mChannelsPerFrame = in;
378         /* apple says that for non-interleaved data, these
379            values always refer to a single channel.
380         */
381         streamFormat.mBytesPerPacket = 4;
382         streamFormat.mBytesPerFrame = 4;
383
384         if (set_input_format () == 0) {
385                 return 1;
386         } else {
387                 return -1;
388         }
389 }
390
391 int
392 AUPlugin::set_input_format ()
393 {
394         return set_stream_format (kAudioUnitScope_Input, input_elements);
395 }
396
397 int
398 AUPlugin::set_output_format ()
399 {
400         return set_stream_format (kAudioUnitScope_Output, output_elements);
401 }
402
403 int
404 AUPlugin::set_stream_format (int scope, uint32_t cnt)
405 {
406         OSErr result;
407
408         for (uint32_t i = 0; i < cnt; ++i) {
409                 if ((result = unit->SetFormat (scope, i, streamFormat)) != 0) {
410                         error << string_compose (_("AUPlugin: could not set stream format for %1/%2 (err = %3)"),
411                                                  (scope == kAudioUnitScope_Input ? "input" : "output"), i, result) << endmsg;
412                         return -1;
413                 }
414         }
415
416         if (scope == kAudioUnitScope_Input) {
417                 format_set |= 0x1;
418         } else {
419                 format_set |= 0x2;
420         }
421
422         return 0;
423 }
424
425 int32_t 
426 AUPlugin::compute_output_streams (int32_t nplugins)
427 {
428         /* we will never replicate AU plugins - either they can do the I/O we need
429            or not. thus, we can ignore nplugins entirely.
430         */
431         
432         if (set_output_format() == 0) {
433
434                 if (buffers) {
435                         free (buffers);
436                         buffers = 0;
437                 }
438
439                 buffers = (AudioBufferList *) malloc (offsetof(AudioBufferList, mBuffers) + 
440                                                       streamFormat.mChannelsPerFrame * sizeof(AudioBuffer));
441
442                 Glib::Mutex::Lock em (_session.engine().process_lock());
443                 IO::MoreOutputs (streamFormat.mChannelsPerFrame);
444
445                 return streamFormat.mChannelsPerFrame;
446         } else {
447                 return -1;
448         }
449 }
450
451 uint32_t
452 AUPlugin::output_streams() const
453 {
454         if (!(format_set & 0x2)) {
455                 warning << string_compose (_("AUPlugin: %1 output_streams() called without any format set!"), name()) << endmsg;
456                 return 1;
457         }
458
459         return streamFormat.mChannelsPerFrame;
460 }
461
462
463 uint32_t
464 AUPlugin::input_streams() const
465 {
466         if (!(format_set & 0x1)) {
467                 warning << _("AUPlugin: input_streams() called without any format set!") << endmsg;
468                 return 1;
469         }
470         return streamFormat.mChannelsPerFrame;
471 }
472
473 OSStatus 
474 AUPlugin::render_callback(AudioUnitRenderActionFlags *ioActionFlags,
475                           const AudioTimeStamp    *inTimeStamp,
476                           UInt32       inBusNumber,
477                           UInt32       inNumberFrames,
478                           AudioBufferList*       ioData)
479 {
480         /* not much to do - the data is already in the buffers given to us in connect_and_run() */
481
482         if (current_maxbuf == 0) {
483                 error << _("AUPlugin: render callback called illegally!") << endmsg;
484                 return kAudioUnitErr_CannotDoInCurrentContext;
485         }
486
487         for (uint32_t i = 0; i < current_maxbuf; ++i) {
488                 ioData->mBuffers[i].mNumberChannels = 1;
489                 ioData->mBuffers[i].mDataByteSize = sizeof (Sample) * inNumberFrames;
490                 ioData->mBuffers[i].mData = (*current_buffers)[i] + cb_offset + current_offset;
491         }
492
493         cb_offset += inNumberFrames;
494
495         return noErr;
496 }
497
498 int
499 AUPlugin::connect_and_run (vector<Sample*>& bufs, uint32_t maxbuf, int32_t& in, int32_t& out, nframes_t nframes, nframes_t offset)
500 {
501         AudioUnitRenderActionFlags flags = 0;
502         AudioTimeStamp ts;
503
504         current_buffers = &bufs;
505         current_maxbuf = maxbuf;
506         current_offset = offset;
507         cb_offset = 0;
508
509         buffers->mNumberBuffers = maxbuf;
510
511         for (uint32_t i = 0; i < maxbuf; ++i) {
512                 buffers->mBuffers[i].mNumberChannels = 1;
513                 buffers->mBuffers[i].mDataByteSize = nframes * sizeof (Sample);
514                 buffers->mBuffers[i].mData = 0;
515         }
516
517         ts.mSampleTime = frames_processed;
518         ts.mFlags = kAudioTimeStampSampleTimeValid;
519
520         if (unit->Render (&flags, &ts, 0, nframes, buffers) == noErr) {
521
522                 current_maxbuf = 0;
523                 frames_processed += nframes;
524                 
525                 for (uint32_t i = 0; i < maxbuf; ++i) {
526                         if (bufs[i] + offset != buffers->mBuffers[i].mData) {
527                                 memcpy (bufs[i]+offset, buffers->mBuffers[i].mData, nframes * sizeof (Sample));
528                         }
529                 }
530                 return 0;
531         }
532
533         return -1;
534 }
535
536 set<uint32_t>
537 AUPlugin::automatable() const
538 {
539         set<uint32_t> automates;
540
541         for (uint32_t i = 0; i < descriptors.size(); ++i) {
542                 if (descriptors[i].automatable) {
543                         automates.insert (i);
544                 }
545         }
546
547         return automates;
548 }
549
550 string
551 AUPlugin::describe_parameter (uint32_t param)
552 {
553         return descriptors[param].label;
554 }
555
556 void
557 AUPlugin::print_parameter (uint32_t param, char* buf, uint32_t len) const
558 {
559         // NameValue stuff here
560 }
561
562 bool
563 AUPlugin::parameter_is_audio (uint32_t) const
564 {
565         return false;
566 }
567
568 bool
569 AUPlugin::parameter_is_control (uint32_t) const
570 {
571         return true;
572 }
573
574 bool
575 AUPlugin::parameter_is_input (uint32_t) const
576 {
577         return false;
578 }
579
580 bool
581 AUPlugin::parameter_is_output (uint32_t) const
582 {
583         return false;
584 }
585
586 XMLNode&
587 AUPlugin::get_state()
588 {
589         XMLNode *root = new XMLNode (state_node_name());
590         LocaleGuard lg (X_("POSIX"));
591         return *root;
592 }
593
594 int
595 AUPlugin::set_state(const XMLNode& node)
596 {
597         return -1;
598 }
599
600 bool
601 AUPlugin::save_preset (string name)
602 {
603         return false;
604 }
605
606 bool
607 AUPlugin::load_preset (const string preset_label)
608 {
609         return false;
610 }
611
612 vector<string>
613 AUPlugin::get_presets ()
614 {
615         vector<string> presets;
616         
617         return presets;
618 }
619
620 bool
621 AUPlugin::has_editor () const
622 {
623         // even if the plugin doesn't have its own editor, the AU API can be used
624         // to create one that looks native.
625         return true;
626 }
627
628 AUPluginInfo::AUPluginInfo (boost::shared_ptr<CAComponentDescription> d)
629         : descriptor (d)
630 {
631
632 }
633
634 AUPluginInfo::~AUPluginInfo ()
635 {
636 }
637
638 PluginPtr
639 AUPluginInfo::load (Session& session)
640 {
641         try {
642                 PluginPtr plugin;
643
644                 boost::shared_ptr<CAComponent> comp (new CAComponent(*descriptor));
645                 
646                 if (!comp->IsValid()) {
647                         error << ("AudioUnit: not a valid Component") << endmsg;
648                 } else {
649                         plugin.reset (new AUPlugin (session.engine(), session, comp));
650                 }
651                 
652                 plugin->set_info (PluginInfoPtr (new AUPluginInfo (*this)));
653                 return plugin;
654         }
655
656         catch (failed_constructor &err) {
657                 return PluginPtr ((Plugin*) 0);
658         }
659 }
660
661 PluginInfoList
662 AUPluginInfo::discover ()
663 {
664         PluginInfoList plugs;
665         
666         discover_fx (plugs);
667         discover_music (plugs);
668
669         return plugs;
670 }
671
672 void
673 AUPluginInfo::discover_music (PluginInfoList& plugs)
674 {
675         CAComponentDescription desc;
676         desc.componentFlags = 0;
677         desc.componentFlagsMask = 0;
678         desc.componentSubType = 0;
679         desc.componentManufacturer = 0;
680         desc.componentType = kAudioUnitType_MusicEffect;
681
682         discover_by_description (plugs, desc);
683 }
684
685 void
686 AUPluginInfo::discover_fx (PluginInfoList& plugs)
687 {
688         CAComponentDescription desc;
689         desc.componentFlags = 0;
690         desc.componentFlagsMask = 0;
691         desc.componentSubType = 0;
692         desc.componentManufacturer = 0;
693         desc.componentType = kAudioUnitType_Effect;
694
695         discover_by_description (plugs, desc);
696 }
697
698 void
699 AUPluginInfo::discover_by_description (PluginInfoList& plugs, CAComponentDescription& desc)
700 {
701         Component comp = 0;
702
703         comp = FindNextComponent (NULL, &desc);
704
705         while (comp != NULL) {
706                 CAComponentDescription temp;
707                 GetComponentInfo (comp, &temp, NULL, NULL, NULL);
708
709                 AUPluginInfoPtr info (new AUPluginInfo 
710                                       (boost::shared_ptr<CAComponentDescription> (new CAComponentDescription(temp))));
711
712                 /* no panners, format converters or i/o AU's for our purposes
713                  */
714
715                 switch (info->descriptor->Type()) {
716                 case kAudioUnitType_Panner:
717                 case kAudioUnitType_OfflineEffect:
718                 case kAudioUnitType_FormatConverter:
719                         continue;
720                 default:
721                         break;
722                 }
723
724                 switch (info->descriptor->SubType()) {
725                 case kAudioUnitSubType_DefaultOutput:
726                 case kAudioUnitSubType_SystemOutput:
727                 case kAudioUnitSubType_GenericOutput:
728                 case kAudioUnitSubType_AUConverter:
729                         continue;
730                         break;
731
732                 case kAudioUnitSubType_DLSSynth:
733                         info->category = "DLSSynth";
734                         break;
735
736                 case kAudioUnitType_MusicEffect:
737                         info->category = "MusicEffect";
738                         break;
739
740                 case kAudioUnitSubType_Varispeed:
741                         info->category = "Varispeed";
742                         break;
743
744                 case kAudioUnitSubType_Delay:
745                         info->category = "Delay";
746                         break;
747
748                 case kAudioUnitSubType_LowPassFilter:
749                         info->category = "LowPassFilter";
750                         break;
751
752                 case kAudioUnitSubType_HighPassFilter:
753                         info->category = "HighPassFilter";
754                         break;
755
756                 case kAudioUnitSubType_BandPassFilter:
757                         info->category = "BandPassFilter";
758                         break;
759
760                 case kAudioUnitSubType_HighShelfFilter:
761                         info->category = "HighShelfFilter";
762                         break;
763
764                 case kAudioUnitSubType_LowShelfFilter:
765                         info->category = "LowShelfFilter";
766                         break;
767
768                 case kAudioUnitSubType_ParametricEQ:
769                         info->category = "ParametricEQ";
770                         break;
771
772                 case kAudioUnitSubType_GraphicEQ:
773                         info->category = "GraphicEQ";
774                         break;
775
776                 case kAudioUnitSubType_PeakLimiter:
777                         info->category = "PeakLimiter";
778                         break;
779
780                 case kAudioUnitSubType_DynamicsProcessor:
781                         info->category = "DynamicsProcessor";
782                         break;
783
784                 case kAudioUnitSubType_MultiBandCompressor:
785                         info->category = "MultiBandCompressor";
786                         break;
787
788                 case kAudioUnitSubType_MatrixReverb:
789                         info->category = "MatrixReverb";
790                         break;
791
792                 case kAudioUnitType_Mixer:
793                         info->category = "Mixer";
794                         break;
795
796                 case kAudioUnitSubType_StereoMixer:
797                         info->category = "StereoMixer";
798                         break;
799
800                 case kAudioUnitSubType_3DMixer:
801                         info->category = "3DMixer";
802                         break;
803
804                 case kAudioUnitSubType_MatrixMixer:
805                         info->category = "MatrixMixer";
806                         break;
807
808                 default:
809                         info->category = "";
810                 }
811
812                 AUPluginInfo::get_names (temp, info->name, info->creator);
813
814                 info->type = ARDOUR::AudioUnit;
815                 info->unique_id = stringify_descriptor (*info->descriptor);
816
817                 /* mark the plugin as having flexible i/o */
818                 
819                 info->n_inputs = -1;
820                 info->n_outputs = -1;
821
822                 plugs.push_back (info);
823                 
824                 comp = FindNextComponent (comp, &desc);
825         }
826 }
827
828 void
829 AUPluginInfo::get_names (CAComponentDescription& comp_desc, std::string& name, Glib::ustring& maker)
830 {
831         CFStringRef itemName = NULL;
832
833         // Marc Poirier-style item name
834         CAComponent auComponent (comp_desc);
835         if (auComponent.IsValid()) {
836                 CAComponentDescription dummydesc;
837                 Handle nameHandle = NewHandle(sizeof(void*));
838                 if (nameHandle != NULL) {
839                         OSErr err = GetComponentInfo(auComponent.Comp(), &dummydesc, nameHandle, NULL, NULL);
840                         if (err == noErr) {
841                                 ConstStr255Param nameString = (ConstStr255Param) (*nameHandle);
842                                 if (nameString != NULL) {
843                                         itemName = CFStringCreateWithPascalString(kCFAllocatorDefault, nameString, CFStringGetSystemEncoding());
844                                 }
845                         }
846                         DisposeHandle(nameHandle);
847                 }
848         }
849     
850         // if Marc-style fails, do the original way
851         if (itemName == NULL) {
852                 CFStringRef compTypeString = UTCreateStringForOSType(comp_desc.componentType);
853                 CFStringRef compSubTypeString = UTCreateStringForOSType(comp_desc.componentSubType);
854                 CFStringRef compManufacturerString = UTCreateStringForOSType(comp_desc.componentManufacturer);
855     
856                 itemName = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%@ - %@ - %@"), 
857                         compTypeString, compManufacturerString, compSubTypeString);
858     
859                 if (compTypeString != NULL)
860                         CFRelease(compTypeString);
861                 if (compSubTypeString != NULL)
862                         CFRelease(compSubTypeString);
863                 if (compManufacturerString != NULL)
864                         CFRelease(compManufacturerString);
865         }
866         
867         string str = CFStringRefToStdString(itemName);
868         string::size_type colon = str.find (':');
869
870         if (colon) {
871                 name = str.substr (colon+1);
872                 maker = str.substr (0, colon);
873                 // strip_whitespace_edges (maker);
874                 // strip_whitespace_edges (name);
875         } else {
876                 name = str;
877                 maker = "unknown";
878         }
879 }
880
881 // from CAComponentDescription.cpp (in libs/appleutility in ardour source)
882 extern char *StringForOSType (OSType t, char *writeLocation);
883
884 std::string
885 AUPluginInfo::stringify_descriptor (const CAComponentDescription& desc)
886 {
887         char str[24];
888         stringstream s;
889
890         s << StringForOSType (desc.Type(), str);
891         s << " - ";
892                 
893         s << StringForOSType (desc.SubType(), str);
894         s << " - ";
895                 
896         s << StringForOSType (desc.Manu(), str);
897
898         return s.str();
899 }