Merge with 2.0-ongoing R3071.
[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         streamFormat.mBitsPerChannel = 32;
97         streamFormat.mFramesPerPacket = 1;
98
99         // subject to later modification as we discover channel counts
100
101         streamFormat.mBytesPerPacket = 4;
102         streamFormat.mBytesPerFrame = 4;
103         streamFormat.mChannelsPerFrame = 1;
104
105         format_set = 0;
106
107         if (_set_block_size (_session.get_block_size())) {
108                 error << _("AUPlugin: cannot set processing block size") << endmsg;
109                 throw failed_constructor();
110         }
111
112         discover_parameters ();
113
114         Plugin::setup_controls ();
115 }
116
117 AUPlugin::~AUPlugin ()
118 {
119         if (unit) {
120                 unit->Uninitialize ();
121         }
122
123         if (buffers) {
124                 free (buffers);
125         }
126 }
127
128 void
129 AUPlugin::discover_parameters ()
130 {
131         /* discover writable parameters */
132         
133         cerr << "get param info, there are " << global_elements << " global elements\n";
134
135         AudioUnitScope scopes[] = { 
136                 kAudioUnitScope_Global,
137                 kAudioUnitScope_Output,
138                 kAudioUnitScope_Input
139         };
140
141         descriptors.clear ();
142
143         for (uint32_t i = 0; i < sizeof (scopes) / sizeof (scopes[0]); ++i) {
144
145                 AUParamInfo param_info (unit->AU(), false, false, scopes[i]);
146                 
147                 cerr << "discovered " << param_info.NumParams() << " parameters in scope " << i << endl;
148                 
149                 for (uint32_t i = 0; i < param_info.NumParams(); ++i) {
150
151                         AUParameterDescriptor d;
152
153                         d.id = param_info.ParamID (i);
154
155                         const CAAUParameter* param = param_info.GetParamInfo (d.id);
156                         const AudioUnitParameterInfo& info (param->ParamInfo());
157
158                         const int len = CFStringGetLength (param->GetName());;
159                         char local_buffer[len*2];
160                         Boolean good = CFStringGetCString(param->GetName(),local_buffer,len*2,kCFStringEncodingMacRoman);
161                         if (!good) {
162                                 d.label = "???";
163                         } else {
164                                 d.label = local_buffer;
165                         }
166
167                         d.scope = param_info.GetScope ();
168                         d.element = param_info.GetElement ();
169
170                         /* info.units to consider */
171                         /*
172                           kAudioUnitParameterUnit_Generic             = 0
173                           kAudioUnitParameterUnit_Indexed             = 1
174                           kAudioUnitParameterUnit_Boolean             = 2
175                           kAudioUnitParameterUnit_Percent             = 3
176                           kAudioUnitParameterUnit_Seconds             = 4
177                           kAudioUnitParameterUnit_SampleFrames        = 5
178                           kAudioUnitParameterUnit_Phase               = 6
179                           kAudioUnitParameterUnit_Rate                = 7
180                           kAudioUnitParameterUnit_Hertz               = 8
181                           kAudioUnitParameterUnit_Cents               = 9
182                           kAudioUnitParameterUnit_RelativeSemiTones   = 10
183                           kAudioUnitParameterUnit_MIDINoteNumber      = 11
184                           kAudioUnitParameterUnit_MIDIController      = 12
185                           kAudioUnitParameterUnit_Decibels            = 13
186                           kAudioUnitParameterUnit_LinearGain          = 14
187                           kAudioUnitParameterUnit_Degrees             = 15
188                           kAudioUnitParameterUnit_EqualPowerCrossfade = 16
189                           kAudioUnitParameterUnit_MixerFaderCurve1    = 17
190                           kAudioUnitParameterUnit_Pan                 = 18
191                           kAudioUnitParameterUnit_Meters              = 19
192                           kAudioUnitParameterUnit_AbsoluteCents       = 20
193                           kAudioUnitParameterUnit_Octaves             = 21
194                           kAudioUnitParameterUnit_BPM                 = 22
195                           kAudioUnitParameterUnit_Beats               = 23
196                           kAudioUnitParameterUnit_Milliseconds        = 24
197                           kAudioUnitParameterUnit_Ratio               = 25
198                         */
199
200                         /* info.flags to consider */
201
202                         /*
203
204                           kAudioUnitParameterFlag_CFNameRelease       = (1L << 4)
205                           kAudioUnitParameterFlag_HasClump            = (1L << 20)
206                           kAudioUnitParameterFlag_HasName             = (1L << 21)
207                           kAudioUnitParameterFlag_DisplayLogarithmic  = (1L << 22)
208                           kAudioUnitParameterFlag_IsHighResolution    = (1L << 23)
209                           kAudioUnitParameterFlag_NonRealTime         = (1L << 24)
210                           kAudioUnitParameterFlag_CanRamp             = (1L << 25)
211                           kAudioUnitParameterFlag_ExpertMode          = (1L << 26)
212                           kAudioUnitParameterFlag_HasCFNameString     = (1L << 27)
213                           kAudioUnitParameterFlag_IsGlobalMeta        = (1L << 28)
214                           kAudioUnitParameterFlag_IsElementMeta       = (1L << 29)
215                           kAudioUnitParameterFlag_IsReadable          = (1L << 30)
216                           kAudioUnitParameterFlag_IsWritable          = (1L << 31)
217                         */
218
219                         d.lower = info.minValue;
220                         d.upper = info.maxValue;
221                         d.default_value = info.defaultValue;
222
223                         d.integer_step = (info.unit & kAudioUnitParameterUnit_Indexed);
224                         d.toggled = (info.unit & kAudioUnitParameterUnit_Boolean) ||
225                                 (d.integer_step && ((d.upper - d.lower) == 1.0));
226                         d.sr_dependent = (info.unit & kAudioUnitParameterUnit_SampleFrames);
227                         d.automatable = !d.toggled && 
228                                 !(info.flags & kAudioUnitParameterFlag_NonRealTime) &&
229                                 (info.flags & kAudioUnitParameterFlag_IsWritable);
230                         
231                         d.logarithmic = (info.flags & kAudioUnitParameterFlag_DisplayLogarithmic);
232                         d.unit = info.unit;
233
234                         d.step = 1.0;
235                         d.smallstep = 0.1;
236                         d.largestep = 10.0;
237                         d.min_unbound = 0; // lower is bound
238                         d.max_unbound = 0; // upper is bound
239
240                         descriptors.push_back (d);
241                 }
242         }
243 }
244
245
246 string
247 AUPlugin::unique_id () const
248 {
249         return AUPluginInfo::stringify_descriptor (comp->Desc());
250 }
251
252 const char *
253 AUPlugin::label () const
254 {
255         return _info->name.c_str();
256 }
257
258 uint32_t
259 AUPlugin::parameter_count () const
260 {
261         return descriptors.size();
262 }
263
264 float
265 AUPlugin::default_value (uint32_t port)
266 {
267         if (port < descriptors.size()) {
268                 return descriptors[port].default_value;
269         }
270
271         return 0;
272 }
273
274 nframes_t
275 AUPlugin::signal_latency () const
276 {
277         if (_user_latency) {
278                 return _user_latency;
279         }
280
281         return unit->Latency ();
282 }
283
284 void
285 AUPlugin::set_parameter (uint32_t which, float val)
286 {
287         if (which < descriptors.size()) {
288                 const AUParameterDescriptor& d (descriptors[which]);
289                 unit->SetParameter (d.id, d.scope, d.element, val);
290         }
291 }
292
293 float
294 AUPlugin::get_parameter (uint32_t which) const
295 {
296         float val = 0.0;
297         if (which < descriptors.size()) {
298                 const AUParameterDescriptor& d (descriptors[which]);
299                 unit->GetParameter(d.id, d.scope, d.element, val);
300         }
301         return val;
302 }
303
304 int
305 AUPlugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor& pd) const
306 {
307         if (which < descriptors.size()) {
308                 pd = descriptors[which];
309                 return 0;
310         } 
311         return -1;
312 }
313
314 uint32_t
315 AUPlugin::nth_parameter (uint32_t which, bool& ok) const
316 {
317         if (which < descriptors.size()) {
318                 ok = true;
319                 return which;
320         }
321         ok = false;
322         return 0;
323 }
324
325 void
326 AUPlugin::activate ()
327 {
328         if (!initialized) {
329                 OSErr err;
330                 if ((err = unit->Initialize()) != noErr) {
331                         error << string_compose (_("AUPlugin: cannot initialize plugin (err = %1)"), err) << endmsg;
332                 } else {
333                         frames_processed = 0;
334                         initialized = true;
335                 }
336         }
337 }
338
339 void
340 AUPlugin::deactivate ()
341 {
342         unit->GlobalReset ();
343 }
344
345 void
346 AUPlugin::set_block_size (nframes_t nframes)
347 {
348         _set_block_size (nframes);
349 }
350
351 int
352 AUPlugin::_set_block_size (nframes_t nframes)
353 {
354         bool was_initialized = initialized;
355         UInt32 numFrames = nframes;
356         OSErr err;
357
358         if (initialized) {
359                 unit->Uninitialize ();
360         }
361
362         if ((err = unit->SetProperty (kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global, 
363                                       0, &numFrames, sizeof (numFrames))) != noErr) {
364                 cerr << "cannot set max frames (err = " << err << ')' << endl;
365                 return -1;
366         }
367
368         if (was_initialized) {
369                 activate ();
370         }
371
372         return 0;
373 }
374
375 int32_t 
376 AUPlugin::can_support_input_configuration (int32_t in)
377 {       
378         streamFormat.mChannelsPerFrame = in;
379         /* apple says that for non-interleaved data, these
380            values always refer to a single channel.
381         */
382         streamFormat.mBytesPerPacket = 4;
383         streamFormat.mBytesPerFrame = 4;
384
385         if (set_input_format () == 0) {
386                 return 1;
387         } else {
388                 return -1;
389         }
390 }
391
392 int
393 AUPlugin::set_input_format ()
394 {
395         return set_stream_format (kAudioUnitScope_Input, input_elements);
396 }
397
398 int
399 AUPlugin::set_output_format ()
400 {
401         return set_stream_format (kAudioUnitScope_Output, output_elements);
402 }
403
404 int
405 AUPlugin::set_stream_format (int scope, uint32_t cnt)
406 {
407         OSErr result;
408
409         for (uint32_t i = 0; i < cnt; ++i) {
410                 if ((result = unit->SetFormat (scope, i, streamFormat)) != 0) {
411                         error << string_compose (_("AUPlugin: could not set stream format for %1/%2 (err = %3)"),
412                                                  (scope == kAudioUnitScope_Input ? "input" : "output"), i, result) << endmsg;
413                         return -1;
414                 }
415         }
416
417         if (scope == kAudioUnitScope_Input) {
418                 format_set |= 0x1;
419         } else {
420                 format_set |= 0x2;
421         }
422
423         return 0;
424 }
425
426 int32_t 
427 AUPlugin::compute_output_streams (int32_t nplugins)
428 {
429         /* we will never replicate AU plugins - either they can do the I/O we need
430            or not. thus, we can ignore nplugins entirely.
431         */
432         
433         if (set_output_format() == 0) {
434
435                 if (buffers) {
436                         free (buffers);
437                         buffers = 0;
438                 }
439
440                 buffers = (AudioBufferList *) malloc (offsetof(AudioBufferList, mBuffers) + 
441                                                       streamFormat.mChannelsPerFrame * sizeof(AudioBuffer));
442
443                 Glib::Mutex::Lock em (_session.engine().process_lock());
444                 IO::MoreOutputs (streamFormat.mChannelsPerFrame);
445
446                 return streamFormat.mChannelsPerFrame;
447         } else {
448                 return -1;
449         }
450 }
451
452 uint32_t
453 AUPlugin::output_streams() const
454 {
455         if (!(format_set & 0x2)) {
456                 warning << _("AUPlugin: output_streams() called without any format set!") << endmsg;
457                 return 1;
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
823                 plugs.push_back (info);
824                 
825                 comp = FindNextComponent (comp, &desc);
826         }
827 }
828
829 void
830 AUPluginInfo::get_names (CAComponentDescription& comp_desc, std::string& name, Glib::ustring& maker)
831 {
832         CFStringRef itemName = NULL;
833
834         // Marc Poirier-style item name
835         CAComponent auComponent (comp_desc);
836         if (auComponent.IsValid()) {
837                 CAComponentDescription dummydesc;
838                 Handle nameHandle = NewHandle(sizeof(void*));
839                 if (nameHandle != NULL) {
840                         OSErr err = GetComponentInfo(auComponent.Comp(), &dummydesc, nameHandle, NULL, NULL);
841                         if (err == noErr) {
842                                 ConstStr255Param nameString = (ConstStr255Param) (*nameHandle);
843                                 if (nameString != NULL) {
844                                         itemName = CFStringCreateWithPascalString(kCFAllocatorDefault, nameString, CFStringGetSystemEncoding());
845                                 }
846                         }
847                         DisposeHandle(nameHandle);
848                 }
849         }
850     
851         // if Marc-style fails, do the original way
852         if (itemName == NULL) {
853                 CFStringRef compTypeString = UTCreateStringForOSType(comp_desc.componentType);
854                 CFStringRef compSubTypeString = UTCreateStringForOSType(comp_desc.componentSubType);
855                 CFStringRef compManufacturerString = UTCreateStringForOSType(comp_desc.componentManufacturer);
856     
857                 itemName = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%@ - %@ - %@"), 
858                         compTypeString, compManufacturerString, compSubTypeString);
859     
860                 if (compTypeString != NULL)
861                         CFRelease(compTypeString);
862                 if (compSubTypeString != NULL)
863                         CFRelease(compSubTypeString);
864                 if (compManufacturerString != NULL)
865                         CFRelease(compManufacturerString);
866         }
867         
868         string str = CFStringRefToStdString(itemName);
869         string::size_type colon = str.find (':');
870
871         if (colon) {
872                 name = str.substr (colon+1);
873                 maker = str.substr (0, colon);
874                 // strip_whitespace_edges (maker);
875                 // strip_whitespace_edges (name);
876         } else {
877                 name = str;
878                 maker = "unknown";
879         }
880 }
881
882 // from CAComponentDescription.cpp (in libs/appleutility in ardour source)
883 extern char *StringForOSType (OSType t, char *writeLocation);
884
885 std::string
886 AUPluginInfo::stringify_descriptor (const CAComponentDescription& desc)
887 {
888         char str[24];
889         stringstream s;
890
891         s << StringForOSType (desc.Type(), str);
892         s << " - ";
893                 
894         s << StringForOSType (desc.SubType(), str);
895         s << " - ";
896                 
897         s << StringForOSType (desc.Manu(), str);
898
899         return s.str();
900 }