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 ();
69 AUPluginInfo::~AUPluginInfo ()
77 AUPlugin::unique_id () const
83 AUPlugin::label () const
85 return "AUPlugin label";
89 AUPlugin::maker () const
91 return "AUplugin maker";
95 AUPlugin::parameter_count () const
101 AUPlugin::default_value (uint32_t port)
103 // AudioUnits don't have default values. Maybe presets though?
108 AUPlugin::latency () const
110 return unit->Latency ();
114 AUPlugin::set_parameter (uint32_t which, float val)
116 unit->SetParameter (parameter_map[which].first, parameter_map[which].second, 0, val);
120 AUPlugin::get_parameter (uint32_t which) const
122 float outValue = 0.0;
124 unit->GetParameter(parameter_map[which].first, parameter_map[which].second, 0, outValue);
130 AUPlugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor&) const
136 AUPlugin::nth_parameter (uint32_t which, bool& ok) const
142 AUPlugin::activate ()
144 unit->GlobalReset ();
148 AUPlugin::deactivate ()
150 // not needed. GlobalReset () takes care of it.
154 AUPlugin::set_block_size (jack_nframes_t nframes)
160 AUPlugin::connect_and_run (vector<Sample*>& bufs, uint32_t maxbuf, int32_t& in, int32_t& out, jack_nframes_t nframes, jack_nframes_t offset)
162 AudioUnitRenderActionFlags flags = 0;
166 abl.mNumberBuffers = 1;
167 abl.mBuffers[0].mNumberChannels = 1;
168 abl.mBuffers[0].mDataByteSize = nframes * sizeof(Sample);
169 abl.mBuffers[0].mData = &bufs[0];
172 unit->Render (&flags, &ts, 0, 0, &abl);
178 AUPlugin::automatable() const
180 set<uint32_t> automates;
186 AUPlugin::store_state (ARDOUR::PluginState&)
192 AUPlugin::restore_state (ARDOUR::PluginState&)
198 AUPlugin::describe_parameter (uint32_t)
204 AUPlugin::print_parameter (uint32_t, char*, uint32_t len) const
210 AUPlugin::parameter_is_audio (uint32_t) const
216 AUPlugin::parameter_is_control (uint32_t) const
222 AUPlugin::parameter_is_input (uint32_t) const
228 AUPlugin::parameter_is_output (uint32_t) const
234 AUPlugin::get_state()
236 XMLNode* root = new XMLNode (state_node_name());
242 AUPlugin::set_state(const XMLNode& node)
248 AUPlugin::save_preset (string name)
254 AUPlugin::load_preset (const string preset_label)
260 AUPlugin::get_presets ()
262 vector<string> presets;
268 AUPlugin::has_editor () const
274 AUPluginInfo::load (Session& session)
279 CAComponent* comp = new CAComponent(*desc);
281 if (!comp->IsValid()) {
282 error << ("AudioUnit: not a valid Component") << endmsg;
284 plugin.reset (new AUPlugin (session.engine(), session, comp));
287 plugin->set_info(PluginInfoPtr(new AUPluginInfo(*this)));
291 catch (failed_constructor &err) {
292 return PluginPtr ((Plugin*) 0);
297 AUPluginInfo::discover ()
299 PluginInfoList plugs;
301 CAComponentDescription desc;
302 desc.componentFlags = 0;
303 desc.componentFlagsMask = 0;
304 desc.componentSubType = 0;
305 desc.componentManufacturer = 0;
306 desc.componentType = kAudioUnitType_Effect;
310 comp = FindNextComponent (NULL, &desc);
311 while (comp != NULL) {
312 CAComponentDescription temp;
313 GetComponentInfo (comp, &temp, NULL, NULL, NULL);
315 AUPluginInfoPtr plug(new AUPluginInfo);
316 plug->name = AUPluginInfo::get_name (temp);
317 plug->type = PluginInfo::AudioUnit;
320 plug->category = "AudioUnit";
321 plug->desc = new CAComponentDescription(temp);
323 plugs.push_back(plug);
325 comp = FindNextComponent (comp, &desc);
332 AUPluginInfo::get_name (CAComponentDescription& comp_desc)
334 CFStringRef itemName = NULL;
335 // Marc Poirier -style item name
336 CAComponent auComponent (comp_desc);
337 if (auComponent.IsValid()) {
338 CAComponentDescription dummydesc;
339 Handle nameHandle = NewHandle(sizeof(void*));
340 if (nameHandle != NULL) {
341 OSErr err = GetComponentInfo(auComponent.Comp(), &dummydesc, nameHandle, NULL, NULL);
343 ConstStr255Param nameString = (ConstStr255Param) (*nameHandle);
344 if (nameString != NULL) {
345 itemName = CFStringCreateWithPascalString(kCFAllocatorDefault, nameString, CFStringGetSystemEncoding());
348 DisposeHandle(nameHandle);
352 // if Marc-style fails, do the original way
353 if (itemName == NULL) {
354 CFStringRef compTypeString = UTCreateStringForOSType(comp_desc.componentType);
355 CFStringRef compSubTypeString = UTCreateStringForOSType(comp_desc.componentSubType);
356 CFStringRef compManufacturerString = UTCreateStringForOSType(comp_desc.componentManufacturer);
358 itemName = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%@ - %@ - %@"),
359 compTypeString, compManufacturerString, compSubTypeString);
361 if (compTypeString != NULL)
362 CFRelease(compTypeString);
363 if (compSubTypeString != NULL)
364 CFRelease(compSubTypeString);
365 if (compManufacturerString != NULL)
366 CFRelease(compManufacturerString);
369 return CFStringRefToStdString(itemName);