remove StateManager code entirely and more debugging output cruft
[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 <pbd/transmitter.h>
22 #include <pbd/xml++.h>
23
24 #include <ardour/audioengine.h>
25 #include <ardour/audio_unit.h>
26 #include <ardour/session.h>
27 #include <ardour/utils.h>
28
29 #include <appleutility/CAAudioUnit.h>
30
31 #include <CoreServices/CoreServices.h>
32 #include <AudioUnit/AudioUnit.h>
33
34 #include "i18n.h"
35
36 using namespace std;
37 using namespace PBD;
38 using namespace ARDOUR;
39
40 AUPlugin::AUPlugin (AudioEngine& engine, Session& session, CAComponent* _comp)
41         :
42         Plugin (engine, session),
43         comp (_comp),
44         unit (new CAAudioUnit)
45 {                       
46         OSErr err = CAAudioUnit::Open (*comp, *unit);
47         if (err != noErr) {
48                 error << _("AudioUnit: Could not convert CAComponent to CAAudioUnit") << endmsg;
49                 delete unit;
50                 delete comp;
51                 throw failed_constructor ();
52         }
53         
54         unit->Initialize ();
55 }
56
57 AUPlugin::~AUPlugin ()
58 {
59         if (unit) {
60                 unit->Uninitialize ();
61                 delete unit;
62         }
63         
64         if (comp) {
65                 delete comp;
66         }
67         
68         if (in_list) {
69                 delete in_list;
70         }
71         
72         if (out_list) {
73                 delete out_list;
74         }
75 }
76
77 AUPluginInfo::~AUPluginInfo ()
78 {
79         if (desc) {
80                 delete desc;
81         }
82 }
83
84 uint32_t
85 AUPlugin::unique_id () const
86 {
87         return 0;
88 }
89
90 const char *
91 AUPlugin::label () const
92 {
93         return "AUPlugin label";
94 }
95
96 const char *
97 AUPlugin::maker () const
98 {
99         return "AUplugin maker";
100 }
101
102 uint32_t
103 AUPlugin::parameter_count () const
104 {
105         return 0;
106 }
107
108 float
109 AUPlugin::default_value (uint32_t port)
110 {
111         // AudioUnits don't have default values.  Maybe presets though?
112         return 0;
113 }
114
115 nframes_t
116 AUPlugin::latency () const
117 {
118         return unit->Latency ();
119 }
120
121 void
122 AUPlugin::set_parameter (uint32_t which, float val)
123 {
124         unit->SetParameter (parameter_map[which].first, parameter_map[which].second, 0, val);
125 }
126
127 float
128 AUPlugin::get_parameter (uint32_t which) const
129 {
130         float outValue = 0.0;
131         
132         unit->GetParameter(parameter_map[which].first, parameter_map[which].second, 0, outValue);
133         
134         return outValue;
135 }
136
137 int
138 AUPlugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor&) const
139 {
140         return 0;
141 }
142
143 uint32_t
144 AUPlugin::nth_parameter (uint32_t which, bool& ok) const
145 {
146         return 0;
147 }
148
149 void
150 AUPlugin::activate ()
151 {
152         unit->GlobalReset ();
153 }
154
155 void
156 AUPlugin::deactivate ()
157 {
158         // not needed.  GlobalReset () takes care of it.
159 }
160
161 void
162 AUPlugin::set_block_size (nframes_t nframes)
163 {
164         
165 }
166
167 int
168 AUPlugin::connect_and_run (vector<Sample*>& bufs, uint32_t maxbuf, int32_t& in, int32_t& out, nframes_t nframes, nframes_t offset)
169 {
170         AudioUnitRenderActionFlags flags = 0;
171         AudioTimeStamp ts;
172         
173         AudioBufferList abl;
174         abl.mNumberBuffers = 1;
175         abl.mBuffers[0].mNumberChannels = 1;
176         abl.mBuffers[0].mDataByteSize = nframes * sizeof(Sample);
177         abl.mBuffers[0].mData = &bufs[0];
178         
179         
180         unit->Render (&flags, &ts, 0, 0, &abl);
181         
182         return 0;
183 }
184
185 set<uint32_t>
186 AUPlugin::automatable() const
187 {
188         set<uint32_t> automates;
189         
190         return automates;
191 }
192
193 string
194 AUPlugin::describe_parameter (uint32_t)
195 {
196         return "";
197 }
198
199 void
200 AUPlugin::print_parameter (uint32_t, char*, uint32_t len) const
201 {
202         
203 }
204
205 bool
206 AUPlugin::parameter_is_audio (uint32_t) const
207 {
208         return false;
209 }
210
211 bool
212 AUPlugin::parameter_is_control (uint32_t) const
213 {
214         return false;
215 }
216
217 bool
218 AUPlugin::parameter_is_input (uint32_t) const
219 {
220         return false;
221 }
222
223 bool
224 AUPlugin::parameter_is_output (uint32_t) const
225 {
226         return false;
227 }
228
229 XMLNode&
230 AUPlugin::get_state()
231 {
232         XMLNode* root = new XMLNode (state_node_name());
233         
234         return *root;
235 }
236
237 int
238 AUPlugin::set_state(const XMLNode& node)
239 {
240         return -1;
241 }
242
243 bool
244 AUPlugin::save_preset (string name)
245 {
246         return false;
247 }
248
249 bool
250 AUPlugin::load_preset (const string preset_label)
251 {
252         return false;
253 }
254
255 vector<string>
256 AUPlugin::get_presets ()
257 {
258         vector<string> presets;
259         
260         return presets;
261 }
262
263 bool
264 AUPlugin::has_editor () const
265 {
266         return false;
267 }
268
269 PluginPtr
270 AUPluginInfo::load (Session& session)
271 {
272         try {
273                 PluginPtr plugin;
274
275                 CAComponent* comp = new CAComponent(*desc);
276                 
277                 if (!comp->IsValid()) {
278                         error << ("AudioUnit: not a valid Component") << endmsg;
279                 } else {
280                         plugin.reset (new AUPlugin (session.engine(), session, comp));
281                 }
282                 
283                 plugin->set_info(PluginInfoPtr(new AUPluginInfo(*this)));
284                 return plugin;
285         }
286
287         catch (failed_constructor &err) {
288                 return PluginPtr ((Plugin*) 0);
289         }
290 }
291
292 PluginInfoList
293 AUPluginInfo::discover ()
294 {
295         PluginInfoList plugs;
296
297         CAComponentDescription desc;
298         desc.componentFlags = 0;
299         desc.componentFlagsMask = 0;
300         desc.componentSubType = 0;
301         desc.componentManufacturer = 0;
302         desc.componentType = kAudioUnitType_Effect;
303
304         Component comp = 0;
305
306         comp = FindNextComponent (NULL, &desc);
307         while (comp != NULL) {
308                 CAComponentDescription temp;
309                 GetComponentInfo (comp, &temp, NULL, NULL, NULL);
310                 
311                 AUPluginInfoPtr plug(new AUPluginInfo);
312                 plug->name = AUPluginInfo::get_name (temp);
313                 plug->type = ARDOUR::AudioUnit;
314                 plug->n_inputs = 0;
315                 plug->n_outputs = 0;
316                 // plug->setup_nchannels (temp);
317                 plug->category = "AudioUnit";
318                 plug->desc = new CAComponentDescription(temp);
319
320                 plugs.push_back(plug);
321                 
322                 comp = FindNextComponent (comp, &desc);
323         }
324
325         return plugs;
326 }
327
328 string
329 AUPluginInfo::get_name (CAComponentDescription& comp_desc)
330 {
331         CFStringRef itemName = NULL;
332         // Marc Poirier -style item name
333         CAComponent auComponent (comp_desc);
334         if (auComponent.IsValid()) {
335                 CAComponentDescription dummydesc;
336                 Handle nameHandle = NewHandle(sizeof(void*));
337                 if (nameHandle != NULL) {
338                         OSErr err = GetComponentInfo(auComponent.Comp(), &dummydesc, nameHandle, NULL, NULL);
339                         if (err == noErr) {
340                                 ConstStr255Param nameString = (ConstStr255Param) (*nameHandle);
341                                 if (nameString != NULL) {
342                                         itemName = CFStringCreateWithPascalString(kCFAllocatorDefault, nameString, CFStringGetSystemEncoding());
343                                 }
344                         }
345                         DisposeHandle(nameHandle);
346                 }
347         }
348     
349         // if Marc-style fails, do the original way
350         if (itemName == NULL) {
351                 CFStringRef compTypeString = UTCreateStringForOSType(comp_desc.componentType);
352                 CFStringRef compSubTypeString = UTCreateStringForOSType(comp_desc.componentSubType);
353                 CFStringRef compManufacturerString = UTCreateStringForOSType(comp_desc.componentManufacturer);
354     
355                 itemName = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%@ - %@ - %@"), 
356                         compTypeString, compManufacturerString, compSubTypeString);
357     
358                 if (compTypeString != NULL)
359                         CFRelease(compTypeString);
360                 if (compSubTypeString != NULL)
361                         CFRelease(compSubTypeString);
362                 if (compManufacturerString != NULL)
363                         CFRelease(compManufacturerString);
364         }
365         
366         return CFStringRefToStdString(itemName);
367 }
368
369 void
370 AUPluginInfo::setup_nchannels (CAComponentDescription& comp_desc)
371 {
372         CAAudioUnit unit;
373         
374         CAAudioUnit::Open (comp_desc, unit);
375         
376         if (unit.SupportsNumChannels()) {
377                 n_inputs = n_outputs = 0;
378         } else {
379                 AUChannelInfo cinfo;
380                 size_t info_size = sizeof(cinfo);
381                 OSStatus err = AudioUnitGetProperty (unit.AU(), kAudioUnitProperty_SupportedNumChannels, kAudioUnitScope_Global,
382                                              0, &cinfo, &info_size);
383         }
384 }
385