Most PluginManager refactoring is out of the way. Time to begin on AudioUnit support...
[ardour.git] / libs / ardour / audio_unit.cc
1 /*
2     Copyright (C) 2006 Paul Davis 
3         Written by Taybin Rutkin
4         
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #include <ardour/audio_unit.h>
22 #include <ardour/utils.h>
23
24 #include <CoreServices/CoreServices.h>
25 #include <AudioUnit/AudioUnit.h>
26
27 using namespace ARDOUR;
28
29 PluginPtr
30 AUPluginInfo::load (Session& session)
31 {
32         
33         return PluginPtr((AUPlugin*)0);
34 }
35
36
37 PluginInfoList
38 AUPluginInfo::discover ()
39 {
40         PluginInfoList plugs;
41
42         int numTypes = 2;    // this magic number was retrieved from the apple AUHost example.
43
44         ComponentDescription desc;
45         desc.componentFlags = 0;
46         desc.componentFlagsMask = 0;
47         desc.componentSubType = 0;
48         desc.componentManufacturer = 0;
49
50         for (int i = 0; i < numTypes; ++i) {
51                 if (i == 1) {
52                         desc.componentType = kAudioUnitType_MusicEffect;
53                 } else {
54                         desc.componentType = kAudioUnitType_Effect;
55                 }
56
57                 Component comp = 0;
58
59                 comp = FindNextComponent (NULL, &desc);
60                 while (comp != NULL) {
61                         ComponentDescription temp;
62                         GetComponentInfo (comp, &temp, NULL, NULL, NULL);
63                         
64                         AUPluginInfoPtr plug(new AUPluginInfo);
65                         plug->name = AUPluginInfo::get_name (temp);
66                         plug->type = PluginInfo::AudioUnit;
67                         plug->n_inputs = 0;
68                         plug->n_outputs = 0;
69                         plug->category = "AudioUnit";
70                         plug->desc = CompDescPtr(new ComponentDescription(temp));
71
72                         plugs.push_back(plug);
73                         
74                         comp = FindNextComponent (comp, &desc);
75                 }
76         }
77
78         return plugs;
79 }
80
81 std::string
82 AUPluginInfo::get_name (ComponentDescription& comp_desc)
83 {
84                 CFStringRef itemName = NULL;
85                 // Marc Poirier -style item name
86                 Component auComponent = FindNextComponent (0, &comp_desc);
87                 if (auComponent != NULL) {
88                         ComponentDescription dummydesc;
89                         Handle nameHandle = NewHandle(sizeof(void*));
90                         if (nameHandle != NULL) {
91                                 OSErr err = GetComponentInfo(auComponent, &dummydesc, nameHandle, NULL, NULL);
92                                 if (err == noErr) {
93                                         ConstStr255Param nameString = (ConstStr255Param) (*nameHandle);
94                                         if (nameString != NULL) {
95                                                 itemName = CFStringCreateWithPascalString(kCFAllocatorDefault, nameString, CFStringGetSystemEncoding());
96                                         }
97                                 }
98                                 DisposeHandle(nameHandle);
99                         }
100                 }
101
102                 // if Marc-style fails, do the original way
103                 if (itemName == NULL) {
104                         CFStringRef compTypeString = UTCreateStringForOSType(comp_desc.componentType);
105                         CFStringRef compSubTypeString = UTCreateStringForOSType(comp_desc.componentSubType);
106                         CFStringRef compManufacturerString = UTCreateStringForOSType(comp_desc.componentManufacturer);
107
108                         itemName = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%@ - %@ - %@"), 
109                                 compTypeString, compManufacturerString, compSubTypeString);
110
111                         if (compTypeString != NULL)
112                                 CFRelease(compTypeString);
113                         if (compSubTypeString != NULL)
114                                 CFRelease(compSubTypeString);
115                         if (compManufacturerString != NULL)
116                                 CFRelease(compManufacturerString);
117                 }
118                 
119                 return CFStringRefToStdString(itemName);
120 }