2 Copyright (C) 2006 Paul Davis
3 Written by Taybin Rutkin
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.
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.
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.
21 #include <pbd/transmitter.h>
22 #include <pbd/xml++.h>
24 #include <ardour/audioengine.h>
25 #include <ardour/audio_unit.h>
26 #include <ardour/session.h>
27 #include <ardour/utils.h>
29 #include <appleutility/CAAudioUnit.h>
31 #include <CoreServices/CoreServices.h>
32 #include <AudioUnit/AudioUnit.h>
38 using namespace ARDOUR;
40 AUPlugin::AUPlugin (AudioEngine& engine, Session& session, CAComponent* _comp)
42 Plugin (engine, session),
44 unit (new CAAudioUnit)
46 OSErr err = CAAudioUnit::Open (*comp, *unit);
48 error << _("AudioUnit: Could not convert CAComponent to CAAudioUnit") << endmsg;
51 throw failed_constructor ();
57 AUPlugin::~AUPlugin ()
60 unit->Uninitialize ();
77 AUPluginInfo::~AUPluginInfo ()
85 AUPlugin::unique_id () const
91 AUPlugin::label () const
93 return "AUPlugin label";
97 AUPlugin::maker () const
99 return "AUplugin maker";
103 AUPlugin::parameter_count () const
109 AUPlugin::default_value (uint32_t port)
111 // AudioUnits don't have default values. Maybe presets though?
116 AUPlugin::latency () const
118 return unit->Latency ();
122 AUPlugin::set_parameter (uint32_t which, float val)
124 unit->SetParameter (parameter_map[which].first, parameter_map[which].second, 0, val);
128 AUPlugin::get_parameter (uint32_t which) const
130 float outValue = 0.0;
132 unit->GetParameter(parameter_map[which].first, parameter_map[which].second, 0, outValue);
138 AUPlugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor&) const
144 AUPlugin::nth_parameter (uint32_t which, bool& ok) const
150 AUPlugin::activate ()
152 unit->GlobalReset ();
156 AUPlugin::deactivate ()
158 // not needed. GlobalReset () takes care of it.
162 AUPlugin::set_block_size (nframes_t nframes)
168 AUPlugin::connect_and_run (vector<Sample*>& bufs, uint32_t maxbuf, int32_t& in, int32_t& out, nframes_t nframes, nframes_t offset)
170 AudioUnitRenderActionFlags flags = 0;
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];
180 unit->Render (&flags, &ts, 0, 0, &abl);
186 AUPlugin::automatable() const
188 set<uint32_t> automates;
194 AUPlugin::describe_parameter (uint32_t)
200 AUPlugin::print_parameter (uint32_t, char*, uint32_t len) const
206 AUPlugin::parameter_is_audio (uint32_t) const
212 AUPlugin::parameter_is_control (uint32_t) const
218 AUPlugin::parameter_is_input (uint32_t) const
224 AUPlugin::parameter_is_output (uint32_t) const
230 AUPlugin::get_state()
232 XMLNode* root = new XMLNode (state_node_name());
238 AUPlugin::set_state(const XMLNode& node)
244 AUPlugin::save_preset (string name)
250 AUPlugin::load_preset (const string preset_label)
256 AUPlugin::get_presets ()
258 vector<string> presets;
264 AUPlugin::has_editor () const
270 AUPluginInfo::load (Session& session)
275 CAComponent* comp = new CAComponent(*desc);
277 if (!comp->IsValid()) {
278 error << ("AudioUnit: not a valid Component") << endmsg;
280 plugin.reset (new AUPlugin (session.engine(), session, comp));
283 plugin->set_info(PluginInfoPtr(new AUPluginInfo(*this)));
287 catch (failed_constructor &err) {
288 return PluginPtr ((Plugin*) 0);
293 AUPluginInfo::discover ()
295 PluginInfoList plugs;
297 CAComponentDescription desc;
298 desc.componentFlags = 0;
299 desc.componentFlagsMask = 0;
300 desc.componentSubType = 0;
301 desc.componentManufacturer = 0;
302 desc.componentType = kAudioUnitType_Effect;
306 comp = FindNextComponent (NULL, &desc);
307 while (comp != NULL) {
308 CAComponentDescription temp;
309 GetComponentInfo (comp, &temp, NULL, NULL, NULL);
311 AUPluginInfoPtr plug(new AUPluginInfo);
312 plug->name = AUPluginInfo::get_name (temp);
313 plug->type = ARDOUR::AudioUnit;
316 // plug->setup_nchannels (temp);
317 plug->category = "AudioUnit";
318 plug->desc = new CAComponentDescription(temp);
320 plugs.push_back(plug);
322 comp = FindNextComponent (comp, &desc);
329 AUPluginInfo::get_name (CAComponentDescription& comp_desc)
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);
340 ConstStr255Param nameString = (ConstStr255Param) (*nameHandle);
341 if (nameString != NULL) {
342 itemName = CFStringCreateWithPascalString(kCFAllocatorDefault, nameString, CFStringGetSystemEncoding());
345 DisposeHandle(nameHandle);
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);
355 itemName = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%@ - %@ - %@"),
356 compTypeString, compManufacturerString, compSubTypeString);
358 if (compTypeString != NULL)
359 CFRelease(compTypeString);
360 if (compSubTypeString != NULL)
361 CFRelease(compSubTypeString);
362 if (compManufacturerString != NULL)
363 CFRelease(compManufacturerString);
366 return CFStringRefToStdString(itemName);
370 AUPluginInfo::setup_nchannels (CAComponentDescription& comp_desc)
374 CAAudioUnit::Open (comp_desc, unit);
376 if (unit.SupportsNumChannels()) {
377 n_inputs = n_outputs = 0;
380 size_t info_size = sizeof(cinfo);
381 OSStatus err = AudioUnitGetProperty (unit.AU(), kAudioUnitProperty_SupportedNumChannels, kAudioUnitScope_Global,
382 0, &cinfo, &info_size);