Use sys::path and ARDOUR::user_config_directory in ARDOUR::read/write_recent_sessions
[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 void
194 AUPlugin::store_state (ARDOUR::PluginState&)
195 {
196         
197 }
198
199 void
200 AUPlugin::restore_state (ARDOUR::PluginState&)
201 {
202         
203 }
204
205 string
206 AUPlugin::describe_parameter (uint32_t)
207 {
208         return "";
209 }
210
211 void
212 AUPlugin::print_parameter (uint32_t, char*, uint32_t len) const
213 {
214         
215 }
216
217 bool
218 AUPlugin::parameter_is_audio (uint32_t) const
219 {
220         return false;
221 }
222
223 bool
224 AUPlugin::parameter_is_control (uint32_t) const
225 {
226         return false;
227 }
228
229 bool
230 AUPlugin::parameter_is_input (uint32_t) const
231 {
232         return false;
233 }
234
235 bool
236 AUPlugin::parameter_is_output (uint32_t) const
237 {
238         return false;
239 }
240
241 XMLNode&
242 AUPlugin::get_state()
243 {
244         XMLNode* root = new XMLNode (state_node_name());
245         
246         return *root;
247 }
248
249 int
250 AUPlugin::set_state(const XMLNode& node)
251 {
252         return -1;
253 }
254
255 bool
256 AUPlugin::save_preset (string name)
257 {
258         return false;
259 }
260
261 bool
262 AUPlugin::load_preset (const string preset_label)
263 {
264         return false;
265 }
266
267 vector<string>
268 AUPlugin::get_presets ()
269 {
270         vector<string> presets;
271         
272         return presets;
273 }
274
275 bool
276 AUPlugin::has_editor () const
277 {
278         return false;
279 }
280
281 PluginPtr
282 AUPluginInfo::load (Session& session)
283 {
284         try {
285                 PluginPtr plugin;
286
287                 CAComponent* comp = new CAComponent(*desc);
288                 
289                 if (!comp->IsValid()) {
290                         error << ("AudioUnit: not a valid Component") << endmsg;
291                 } else {
292                         plugin.reset (new AUPlugin (session.engine(), session, comp));
293                 }
294                 
295                 plugin->set_info(PluginInfoPtr(new AUPluginInfo(*this)));
296                 return plugin;
297         }
298
299         catch (failed_constructor &err) {
300                 return PluginPtr ((Plugin*) 0);
301         }
302 }
303
304 PluginInfoList
305 AUPluginInfo::discover ()
306 {
307         PluginInfoList plugs;
308
309         CAComponentDescription desc;
310         desc.componentFlags = 0;
311         desc.componentFlagsMask = 0;
312         desc.componentSubType = 0;
313         desc.componentManufacturer = 0;
314         desc.componentType = kAudioUnitType_Effect;
315
316         Component comp = 0;
317
318         comp = FindNextComponent (NULL, &desc);
319         while (comp != NULL) {
320                 CAComponentDescription temp;
321                 GetComponentInfo (comp, &temp, NULL, NULL, NULL);
322                 
323                 AUPluginInfoPtr plug(new AUPluginInfo);
324                 plug->name = AUPluginInfo::get_name (temp);
325                 plug->type = ARDOUR::AudioUnit;
326                 plug->n_inputs = 0;
327                 plug->n_outputs = 0;
328                 // plug->setup_nchannels (temp);
329                 plug->category = "AudioUnit";
330                 plug->desc = new CAComponentDescription(temp);
331
332                 plugs.push_back(plug);
333                 
334                 comp = FindNextComponent (comp, &desc);
335         }
336
337         return plugs;
338 }
339
340 string
341 AUPluginInfo::get_name (CAComponentDescription& comp_desc)
342 {
343         CFStringRef itemName = NULL;
344         // Marc Poirier -style item name
345         CAComponent auComponent (comp_desc);
346         if (auComponent.IsValid()) {
347                 CAComponentDescription dummydesc;
348                 Handle nameHandle = NewHandle(sizeof(void*));
349                 if (nameHandle != NULL) {
350                         OSErr err = GetComponentInfo(auComponent.Comp(), &dummydesc, nameHandle, NULL, NULL);
351                         if (err == noErr) {
352                                 ConstStr255Param nameString = (ConstStr255Param) (*nameHandle);
353                                 if (nameString != NULL) {
354                                         itemName = CFStringCreateWithPascalString(kCFAllocatorDefault, nameString, CFStringGetSystemEncoding());
355                                 }
356                         }
357                         DisposeHandle(nameHandle);
358                 }
359         }
360     
361         // if Marc-style fails, do the original way
362         if (itemName == NULL) {
363                 CFStringRef compTypeString = UTCreateStringForOSType(comp_desc.componentType);
364                 CFStringRef compSubTypeString = UTCreateStringForOSType(comp_desc.componentSubType);
365                 CFStringRef compManufacturerString = UTCreateStringForOSType(comp_desc.componentManufacturer);
366     
367                 itemName = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%@ - %@ - %@"), 
368                         compTypeString, compManufacturerString, compSubTypeString);
369     
370                 if (compTypeString != NULL)
371                         CFRelease(compTypeString);
372                 if (compSubTypeString != NULL)
373                         CFRelease(compSubTypeString);
374                 if (compManufacturerString != NULL)
375                         CFRelease(compManufacturerString);
376         }
377         
378         return CFStringRefToStdString(itemName);
379 }
380
381 void
382 AUPluginInfo::setup_nchannels (CAComponentDescription& comp_desc)
383 {
384         CAAudioUnit unit;
385         
386         CAAudioUnit::Open (comp_desc, unit);
387         
388         if (unit.SupportsNumChannels()) {
389                 n_inputs = n_outputs = 0;
390         } else {
391                 AUChannelInfo cinfo;
392                 size_t info_size = sizeof(cinfo);
393                 OSStatus err = AudioUnitGetProperty (unit.AU(), kAudioUnitProperty_SupportedNumChannels, kAudioUnitScope_Global,
394                                              0, &cinfo, &info_size);
395         }
396 }
397