2 Copyright (C) 2006 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <pbd/transmitter.h>
23 #include <pbd/xml++.h>
24 #include <pbd/whitespace.h>
26 #include <glibmm/thread.h>
28 #include <ardour/audioengine.h>
29 #include <ardour/io.h>
30 #include <ardour/audio_unit.h>
31 #include <ardour/session.h>
32 #include <ardour/utils.h>
34 #include <appleutility/CAAudioUnit.h>
36 #include <CoreServices/CoreServices.h>
37 #include <AudioUnit/AudioUnit.h>
43 using namespace ARDOUR;
46 _render_callback(void *userData,
47 AudioUnitRenderActionFlags *ioActionFlags,
48 const AudioTimeStamp *inTimeStamp,
50 UInt32 inNumberFrames,
51 AudioBufferList* ioData)
53 return ((AUPlugin*)userData)->render_callback (ioActionFlags, inTimeStamp, inBusNumber, inNumberFrames, ioData);
56 AUPlugin::AUPlugin (AudioEngine& engine, Session& session, boost::shared_ptr<CAComponent> _comp)
58 Plugin (engine, session),
60 unit (new CAAudioUnit),
68 OSErr err = CAAudioUnit::Open (*(comp.get()), *unit);
71 error << _("AudioUnit: Could not convert CAComponent to CAAudioUnit") << endmsg;
72 throw failed_constructor ();
75 AURenderCallbackStruct renderCallbackInfo;
77 renderCallbackInfo.inputProc = _render_callback;
78 renderCallbackInfo.inputProcRefCon = this;
80 if ((err = unit->SetProperty (kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input,
81 0, (void*) &renderCallbackInfo, sizeof(renderCallbackInfo))) != 0) {
82 cerr << "cannot install render callback (err = " << err << ')' << endl;
83 throw failed_constructor();
86 unit->GetElementCount (kAudioUnitScope_Input, input_elements);
87 unit->GetElementCount (kAudioUnitScope_Output, output_elements);
89 // set up the basic stream format. these fields do not change
91 streamFormat.mSampleRate = session.frame_rate();
92 streamFormat.mFormatID = kAudioFormatLinearPCM;
93 streamFormat.mFormatFlags = kAudioFormatFlagIsFloat|kAudioFormatFlagIsPacked|kAudioFormatFlagIsNonInterleaved;
94 streamFormat.mBitsPerChannel = 32;
95 streamFormat.mFramesPerPacket = 1;
97 // subject to later modification as we discover channel counts
99 streamFormat.mBytesPerPacket = 4;
100 streamFormat.mBytesPerFrame = 4;
101 streamFormat.mChannelsPerFrame = 1;
105 if (_set_block_size (_session.get_block_size())) {
106 error << _("AUPlugin: cannot set processing block size") << endmsg;
107 throw failed_constructor();
111 AUPlugin::~AUPlugin ()
114 unit->Uninitialize ();
123 AUPlugin::unique_id () const
125 return AUPluginInfo::stringify_descriptor (comp->Desc());
129 AUPlugin::label () const
131 return _info->name.c_str();
135 AUPlugin::parameter_count () const
141 AUPlugin::default_value (uint32_t port)
143 // AudioUnits don't have default values. Maybe presets though?
148 AUPlugin::signal_latency () const
151 return _user_latency;
154 return unit->Latency ();
158 AUPlugin::set_parameter (uint32_t which, float val)
160 // unit->SetParameter (parameter_map[which].first, parameter_map[which].second, 0, val);
164 AUPlugin::get_parameter (uint32_t which) const
166 float outValue = 0.0;
168 // unit->GetParameter(parameter_map[which].first, parameter_map[which].second, 0, outValue);
174 AUPlugin::get_parameter_descriptor (uint32_t which, ParameterDescriptor&) const
180 AUPlugin::nth_parameter (uint32_t which, bool& ok) const
186 AUPlugin::activate ()
190 if ((err = unit->Initialize()) != noErr) {
191 error << string_compose (_("AUPlugin: cannot initialize plugin (err = %1)"), err) << endmsg;
193 frames_processed = 0;
200 AUPlugin::deactivate ()
202 unit->GlobalReset ();
206 AUPlugin::set_block_size (nframes_t nframes)
208 _set_block_size (nframes);
212 AUPlugin::_set_block_size (nframes_t nframes)
214 bool was_initialized = initialized;
215 UInt32 numFrames = nframes;
219 unit->Uninitialize ();
222 if ((err = unit->SetProperty (kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global,
223 0, &numFrames, sizeof (numFrames))) != noErr) {
224 cerr << "cannot set max frames (err = " << err << ')' << endl;
228 if (was_initialized) {
236 AUPlugin::can_support_input_configuration (int32_t in)
238 streamFormat.mChannelsPerFrame = in;
239 /* apple says that for non-interleaved data, these
240 values always refer to a single channel.
242 streamFormat.mBytesPerPacket = 4;
243 streamFormat.mBytesPerFrame = 4;
245 if (set_input_format () == 0) {
253 AUPlugin::set_input_format ()
255 return set_stream_format (kAudioUnitScope_Input, input_elements);
259 AUPlugin::set_output_format ()
261 return set_stream_format (kAudioUnitScope_Output, output_elements);
265 AUPlugin::set_stream_format (int scope, uint32_t cnt)
269 for (uint32_t i = 0; i < cnt; ++i) {
270 if ((result = unit->SetFormat (scope, i, streamFormat)) != 0) {
271 error << string_compose (_("AUPlugin: could not set stream format for %1/%2 (err = %3)"),
272 (scope == kAudioUnitScope_Input ? "input" : "output"), i, result) << endmsg;
277 if (scope == kAudioUnitScope_Input) {
287 AUPlugin::compute_output_streams (int32_t nplugins)
289 /* we will never replicate AU plugins - either they can do the I/O we need
290 or not. thus, we can ignore nplugins entirely.
293 if (set_output_format() == 0) {
300 buffers = (AudioBufferList *) malloc (offsetof(AudioBufferList, mBuffers) +
301 streamFormat.mChannelsPerFrame * sizeof(AudioBuffer));
303 Glib::Mutex::Lock em (_session.engine().process_lock());
304 IO::MoreOutputs (streamFormat.mChannelsPerFrame);
306 return streamFormat.mChannelsPerFrame;
313 AUPlugin::output_streams() const
315 if (!(format_set & 0x2)) {
316 warning << _("AUPlugin: output_streams() called without any format set!") << endmsg;
319 return streamFormat.mChannelsPerFrame;
324 AUPlugin::input_streams() const
326 if (!(format_set & 0x1)) {
327 warning << _("AUPlugin: input_streams() called without any format set!") << endmsg;
330 return streamFormat.mChannelsPerFrame;
334 AUPlugin::render_callback(AudioUnitRenderActionFlags *ioActionFlags,
335 const AudioTimeStamp *inTimeStamp,
337 UInt32 inNumberFrames,
338 AudioBufferList* ioData)
340 /* not much to do - the data is already in the buffers given to us in connect_and_run() */
342 if (current_maxbuf == 0) {
343 error << _("AUPlugin: render callback called illegally!") << endmsg;
344 return kAudioUnitErr_CannotDoInCurrentContext;
347 for (uint32_t i = 0; i < current_maxbuf; ++i) {
348 ioData->mBuffers[i].mNumberChannels = 1;
349 ioData->mBuffers[i].mDataByteSize = sizeof (Sample) * inNumberFrames;
350 ioData->mBuffers[i].mData = (*current_buffers)[i] + cb_offset + current_offset;
353 cb_offset += inNumberFrames;
359 AUPlugin::connect_and_run (vector<Sample*>& bufs, uint32_t maxbuf, int32_t& in, int32_t& out, nframes_t nframes, nframes_t offset)
361 AudioUnitRenderActionFlags flags = 0;
364 current_buffers = &bufs;
365 current_maxbuf = maxbuf;
366 current_offset = offset;
369 buffers->mNumberBuffers = maxbuf;
371 for (uint32_t i = 0; i < maxbuf; ++i) {
372 buffers->mBuffers[i].mNumberChannels = 1;
373 buffers->mBuffers[i].mDataByteSize = nframes * sizeof (Sample);
374 buffers->mBuffers[i].mData = 0;
377 ts.mSampleTime = frames_processed;
378 ts.mFlags = kAudioTimeStampSampleTimeValid;
380 if (unit->Render (&flags, &ts, 0, nframes, buffers) == noErr) {
383 frames_processed += nframes;
385 for (uint32_t i = 0; i < maxbuf; ++i) {
386 if (bufs[i] + offset != buffers->mBuffers[i].mData) {
387 memcpy (bufs[i]+offset, buffers->mBuffers[i].mData, nframes * sizeof (Sample));
397 AUPlugin::automatable() const
399 set<uint32_t> automates;
405 AUPlugin::describe_parameter (uint32_t)
411 AUPlugin::print_parameter (uint32_t, char*, uint32_t len) const
417 AUPlugin::parameter_is_audio (uint32_t) const
423 AUPlugin::parameter_is_control (uint32_t) const
429 AUPlugin::parameter_is_input (uint32_t) const
435 AUPlugin::parameter_is_output (uint32_t) const
441 AUPlugin::get_state()
443 XMLNode *root = new XMLNode (state_node_name());
444 LocaleGuard lg (X_("POSIX"));
449 AUPlugin::set_state(const XMLNode& node)
455 AUPlugin::save_preset (string name)
461 AUPlugin::load_preset (const string preset_label)
467 AUPlugin::get_presets ()
469 vector<string> presets;
475 AUPlugin::has_editor () const
477 // even if the plugin doesn't have its own editor, the AU API can be used
478 // to create one that looks native.
482 AUPluginInfo::AUPluginInfo (boost::shared_ptr<CAComponentDescription> d)
488 AUPluginInfo::~AUPluginInfo ()
493 AUPluginInfo::load (Session& session)
498 boost::shared_ptr<CAComponent> comp (new CAComponent(*descriptor));
500 if (!comp->IsValid()) {
501 error << ("AudioUnit: not a valid Component") << endmsg;
503 plugin.reset (new AUPlugin (session.engine(), session, comp));
506 plugin->set_info (PluginInfoPtr (new AUPluginInfo (*this)));
510 catch (failed_constructor &err) {
511 return PluginPtr ((Plugin*) 0);
516 AUPluginInfo::discover ()
518 PluginInfoList plugs;
521 discover_music (plugs);
527 AUPluginInfo::discover_music (PluginInfoList& plugs)
529 CAComponentDescription desc;
530 desc.componentFlags = 0;
531 desc.componentFlagsMask = 0;
532 desc.componentSubType = 0;
533 desc.componentManufacturer = 0;
534 desc.componentType = kAudioUnitType_MusicEffect;
536 discover_by_description (plugs, desc);
540 AUPluginInfo::discover_fx (PluginInfoList& plugs)
542 CAComponentDescription desc;
543 desc.componentFlags = 0;
544 desc.componentFlagsMask = 0;
545 desc.componentSubType = 0;
546 desc.componentManufacturer = 0;
547 desc.componentType = kAudioUnitType_Effect;
549 discover_by_description (plugs, desc);
553 AUPluginInfo::discover_by_description (PluginInfoList& plugs, CAComponentDescription& desc)
557 comp = FindNextComponent (NULL, &desc);
559 while (comp != NULL) {
560 CAComponentDescription temp;
561 GetComponentInfo (comp, &temp, NULL, NULL, NULL);
563 AUPluginInfoPtr info (new AUPluginInfo
564 (boost::shared_ptr<CAComponentDescription> (new CAComponentDescription(temp))));
566 /* no panners, format converters or i/o AU's for our purposes
569 switch (info->descriptor->Type()) {
570 case kAudioUnitType_Panner:
571 case kAudioUnitType_OfflineEffect:
572 case kAudioUnitType_FormatConverter:
578 switch (info->descriptor->SubType()) {
579 case kAudioUnitSubType_DefaultOutput:
580 case kAudioUnitSubType_SystemOutput:
581 case kAudioUnitSubType_GenericOutput:
582 case kAudioUnitSubType_AUConverter:
586 case kAudioUnitSubType_DLSSynth:
587 info->category = "DLSSynth";
590 case kAudioUnitType_MusicEffect:
591 info->category = "MusicEffect";
594 case kAudioUnitSubType_Varispeed:
595 info->category = "Varispeed";
598 case kAudioUnitSubType_Delay:
599 info->category = "Delay";
602 case kAudioUnitSubType_LowPassFilter:
603 info->category = "LowPassFilter";
606 case kAudioUnitSubType_HighPassFilter:
607 info->category = "HighPassFilter";
610 case kAudioUnitSubType_BandPassFilter:
611 info->category = "BandPassFilter";
614 case kAudioUnitSubType_HighShelfFilter:
615 info->category = "HighShelfFilter";
618 case kAudioUnitSubType_LowShelfFilter:
619 info->category = "LowShelfFilter";
622 case kAudioUnitSubType_ParametricEQ:
623 info->category = "ParametricEQ";
626 case kAudioUnitSubType_GraphicEQ:
627 info->category = "GraphicEQ";
630 case kAudioUnitSubType_PeakLimiter:
631 info->category = "PeakLimiter";
634 case kAudioUnitSubType_DynamicsProcessor:
635 info->category = "DynamicsProcessor";
638 case kAudioUnitSubType_MultiBandCompressor:
639 info->category = "MultiBandCompressor";
642 case kAudioUnitSubType_MatrixReverb:
643 info->category = "MatrixReverb";
646 case kAudioUnitType_Mixer:
647 info->category = "Mixer";
650 case kAudioUnitSubType_StereoMixer:
651 info->category = "StereoMixer";
654 case kAudioUnitSubType_3DMixer:
655 info->category = "3DMixer";
658 case kAudioUnitSubType_MatrixMixer:
659 info->category = "MatrixMixer";
666 AUPluginInfo::get_names (temp, info->name, info->creator);
668 info->type = ARDOUR::AudioUnit;
669 info->unique_id = stringify_descriptor (*info->descriptor);
671 /* mark the plugin as having flexible i/o */
674 info->n_outputs = -1;
677 plugs.push_back (info);
679 comp = FindNextComponent (comp, &desc);
684 AUPluginInfo::get_names (CAComponentDescription& comp_desc, std::string& name, Glib::ustring& maker)
686 CFStringRef itemName = NULL;
688 // Marc Poirier-style item name
689 CAComponent auComponent (comp_desc);
690 if (auComponent.IsValid()) {
691 CAComponentDescription dummydesc;
692 Handle nameHandle = NewHandle(sizeof(void*));
693 if (nameHandle != NULL) {
694 OSErr err = GetComponentInfo(auComponent.Comp(), &dummydesc, nameHandle, NULL, NULL);
696 ConstStr255Param nameString = (ConstStr255Param) (*nameHandle);
697 if (nameString != NULL) {
698 itemName = CFStringCreateWithPascalString(kCFAllocatorDefault, nameString, CFStringGetSystemEncoding());
701 DisposeHandle(nameHandle);
705 // if Marc-style fails, do the original way
706 if (itemName == NULL) {
707 CFStringRef compTypeString = UTCreateStringForOSType(comp_desc.componentType);
708 CFStringRef compSubTypeString = UTCreateStringForOSType(comp_desc.componentSubType);
709 CFStringRef compManufacturerString = UTCreateStringForOSType(comp_desc.componentManufacturer);
711 itemName = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%@ - %@ - %@"),
712 compTypeString, compManufacturerString, compSubTypeString);
714 if (compTypeString != NULL)
715 CFRelease(compTypeString);
716 if (compSubTypeString != NULL)
717 CFRelease(compSubTypeString);
718 if (compManufacturerString != NULL)
719 CFRelease(compManufacturerString);
722 string str = CFStringRefToStdString(itemName);
723 string::size_type colon = str.find (':');
726 name = str.substr (colon+1);
727 maker = str.substr (0, colon);
728 // strip_whitespace_edges (maker);
729 // strip_whitespace_edges (name);
736 // from CAComponentDescription.cpp (in libs/appleutility in ardour source)
737 extern char *StringForOSType (OSType t, char *writeLocation);
740 AUPluginInfo::stringify_descriptor (const CAComponentDescription& desc)
745 s << StringForOSType (desc.Type(), str);
748 s << StringForOSType (desc.SubType(), str);
751 s << StringForOSType (desc.Manu(), str);