add StartTouch and EndTouch signals to Plugin class; make PluginInsert handle these...
[ardour.git] / libs / ardour / audio_unit.cc
index 76a9f1d45e97b897cc3dcc6ddbac760437868ad6..431fb99a481630ed5c843d5cccd7a1bbefcf0a4c 100644 (file)
@@ -43,6 +43,7 @@
 #include "ardour/filesystem_paths.h"
 #include "ardour/io.h"
 #include "ardour/audio_unit.h"
+#include "ardour/route.h"
 #include "ardour/session.h"
 #include "ardour/tempo.h"
 #include "ardour/utils.h"
@@ -54,6 +55,9 @@
 #include <CoreServices/CoreServices.h>
 #include <AudioUnit/AudioUnit.h>
 #include <AudioToolbox/AudioUnitUtilities.h>
+#ifdef WITH_CARBON
+#include <Carbon/Carbon.h>
+#endif
 
 #include "i18n.h"
 
@@ -345,10 +349,12 @@ AUPlugin::AUPlugin (AudioEngine& engine, Session& session, boost::shared_ptr<CAC
        , _current_block_size (0)
        , _requires_fixed_size_buffers (false)
        , buffers (0)
-       , current_maxbuf (0)
-       , current_offset (0)
-       , current_buffers (0)
+       , input_maxbuf (0)
+       , input_offset (0)
+       , input_buffers (0)
        , frames_processed (0)
+       , _parameter_listener (0)
+       , _parameter_listener_arg (0)
        , last_transport_rolling (false)
        , last_transport_speed (0.0)
 {
@@ -373,10 +379,12 @@ AUPlugin::AUPlugin (const AUPlugin& other)
        , _last_nframes (0)
        , _requires_fixed_size_buffers (false)
        , buffers (0)
-       , current_maxbuf (0)
-       , current_offset (0)
-       , current_buffers (0)
+       , input_maxbuf (0)
+       , input_offset (0)
+       , input_buffers (0)
        , frames_processed (0)
+       , _parameter_listener (0)
+       , _parameter_listener_arg (0)
 
 {
        init ();
@@ -384,6 +392,11 @@ AUPlugin::AUPlugin (const AUPlugin& other)
 
 AUPlugin::~AUPlugin ()
 {
+       if (_parameter_listener) {
+               AUListenerDispose (_parameter_listener);
+               _parameter_listener = 0;
+       }
+       
        if (unit) {
                DEBUG_TRACE (DEBUG::AudioUnits, "about to call uninitialize in plugin destructor\n");
                unit->Uninitialize ();
@@ -474,7 +487,7 @@ AUPlugin::init ()
                DEBUG_TRACE (DEBUG::AudioUnits, "set render callback in input scope\n");
                if ((err = unit->SetProperty (kAudioUnitProperty_SetRenderCallback, kAudioUnitScope_Input,
                                              0, (void*) &renderCallbackInfo, sizeof(renderCallbackInfo))) != 0) {
-                       cerr << "cannot install render callback (err = " << err << ')' << endl;
+                       error << string_compose (_("cannot install render callback (err = %1)"), err) << endmsg;
                        throw failed_constructor();
                }
        }
@@ -501,6 +514,7 @@ AUPlugin::init ()
                throw failed_constructor();
        }
 
+       create_parameter_listener (AUPlugin::_parameter_change_listener, this, 0.05);
        discover_parameters ();
        discover_factory_presets ();
 
@@ -616,6 +630,10 @@ AUPlugin::discover_parameters ()
                        d.max_unbound = 0; // upper is bound
 
                        descriptors.push_back (d);
+
+                       uint32_t last_param = descriptors.size() - 1;
+                       parameter_map.insert (pair<uint32_t,uint32_t> (d.id, last_param));
+                       listen_to_parameter (last_param);
                }
        }
 }
@@ -666,7 +684,6 @@ AUPlugin::maybe_fix_broken_au_id (const std::string& id)
 
                                /* too close to the end for \xNN parsing: treat as literal characters */
 
-                               cerr << "Parse " << cstr << " as a literal \\" << endl;
                                nascent[in] = *cstr;
                                ++cstr;
                                ++in;
@@ -685,7 +702,6 @@ AUPlugin::maybe_fix_broken_au_id (const std::string& id)
                                } else {
 
                                        /* treat as literal characters */
-                                       cerr << "Parse " << cstr << " as a literal \\" << endl;
                                        nascent[in] = *cstr;
                                        ++cstr;
                                        ++in;
@@ -794,7 +810,7 @@ AUPlugin::get_parameter (uint32_t which) const
        float val = 0.0;
        if (which < descriptors.size()) {
                const AUParameterDescriptor& d (descriptors[which]);
-               DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("get value of parameter %1 in scope %2 element %3\n", d.id, d.scope, d.element));
+               // DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("get value of parameter %1 in scope %2 element %3\n", d.id, d.scope, d.element));
                unit->GetParameter(d.id, d.scope, d.element, val);
        }
        return val;
@@ -872,7 +888,7 @@ AUPlugin::set_block_size (pframes_t nframes)
        DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("set MaximumFramesPerSlice in global scope to %1\n", numFrames));
        if ((err = unit->SetProperty (kAudioUnitProperty_MaximumFramesPerSlice, kAudioUnitScope_Global,
                                      0, &numFrames, sizeof (numFrames))) != noErr) {
-               cerr << "cannot set max frames (err = " << err << ')' << endl;
+               error << string_compose (_("AU: cannot set max frames (err = %1)"), err) << endmsg;
                return -1;
        }
 
@@ -1004,7 +1020,8 @@ AUPlugin::can_support_io_configuration (const ChanCount& in, ChanCount& out) con
 
        vector<pair<int,int> >& io_configs = pinfo->cache.io_configs;
 
-       DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("%1 has %2 IO configurations\n", name(), io_configs.size()));
+       DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("%1 has %2 IO configurations, looking for %3 in, %4 out\n", 
+                                                       name(), io_configs.size(), in, out));
 
        //Ardour expects the plugin to tell it the output
        //configuration but AU plugins can have multiple I/O
@@ -1019,7 +1036,13 @@ AUPlugin::can_support_io_configuration (const ChanCount& in, ChanCount& out) con
                int32_t possible_out = i->second;
 
                if ((possible_in == audio_in) && (possible_out == audio_out)) {
-                       DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("\tCHOSEN: in %1 out %2\n", in, out));
+                       DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("\tCHOSEN: %1 in %2 out to match in %3 out %4\n", 
+                                                                       possible_in, possible_out,
+                                                                       in, out));
+
+                       out.set (DataType::MIDI, 0);
+                       out.set (DataType::AUDIO, audio_out);
+
                        return 1;
                }
        }
@@ -1057,8 +1080,21 @@ AUPlugin::can_support_io_configuration (const ChanCount& in, ChanCount& out) con
                                audio_out = 2;
                                found = true;
                        } else if (possible_out < -2) {
-                               /* explicitly variable number of outputs, pick maximum */
-                               audio_out = -possible_out;
+                               /* explicitly variable number of outputs. 
+
+                                   Since Ardour can handle any configuration,
+                                   we have to somehow pick a number. 
+
+                                   We'll use the number of inputs
+                                   to the master bus, or 2 if there
+                                   is no master bus.
+                                */
+                                boost::shared_ptr<Route> master = _session.master_out();
+                                if (master) {
+                                        audio_out = master->input()->n_ports().n_audio();
+                                } else {
+                                        audio_out = 2;
+                                }
                                found = true;
                        } else {
                                /* exact number of outputs */
@@ -1121,6 +1157,7 @@ AUPlugin::can_support_io_configuration (const ChanCount& in, ChanCount& out) con
                                /* request is too large */
                        }
 
+
                        if (possible_out == -1) {
                                /* any output configuration possible, provide stereo out */
                                audio_out = 2;
@@ -1132,8 +1169,21 @@ AUPlugin::can_support_io_configuration (const ChanCount& in, ChanCount& out) con
                                audio_out = 2;
                                found = true;
                        } else if (possible_out < -2) {
-                               /* explicitly variable number of outputs, pick maximum */
-                               audio_out = -possible_out;
+                               /* explicitly variable number of outputs. 
+
+                                   Since Ardour can handle any configuration,
+                                   we have to somehow pick a number. 
+
+                                   We'll use the number of inputs
+                                   to the master bus, or 2 if there
+                                   is no master bus.
+                                */
+                                boost::shared_ptr<Route> master = _session.master_out();
+                                if (master) {
+                                        audio_out = master->input()->n_ports().n_audio();
+                                } else {
+                                        audio_out = 2;
+                                }
                                found = true;
                        } else {
                                /* exact number of outputs */
@@ -1248,11 +1298,11 @@ AUPlugin::render_callback(AudioUnitRenderActionFlags*,
        DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("%1: render callback, frames %2 bufs %3\n",
                                                        name(), inNumberFrames, ioData->mNumberBuffers));
 
-       if (current_maxbuf == 0) {
+       if (input_maxbuf == 0) {
                error << _("AUPlugin: render callback called illegally!") << endmsg;
                return kAudioUnitErr_CannotDoInCurrentContext;
        }
-       uint32_t limit = min ((uint32_t) ioData->mNumberBuffers, current_maxbuf);
+       uint32_t limit = min ((uint32_t) ioData->mNumberBuffers, input_maxbuf);
 
        for (uint32_t i = 0; i < limit; ++i) {
                ioData->mBuffers[i].mNumberChannels = 1;
@@ -1263,7 +1313,7 @@ AUPlugin::render_callback(AudioUnitRenderActionFlags*,
                   passed to PluginInsert::connect_and_run()
                */
 
-               ioData->mBuffers[i].mData = current_buffers->get_audio (i).data (cb_offset + current_offset);
+               ioData->mBuffers[i].mData = input_buffers->get_audio (i).data (cb_offset + input_offset);
        }
 
        cb_offset += inNumberFrames;
@@ -1285,29 +1335,32 @@ AUPlugin::connect_and_run (BufferSet& bufs, ChanMapping in_map, ChanMapping out_
                _last_nframes = nframes;
        }
 
+       DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("%1 in %2 out %3 MIDI %4 bufs %5 (available %6)\n",
+                                                       name(), input_channels, output_channels, _has_midi_input,
+                                                       bufs.count(), bufs.available()));
+
        /* the apparent number of buffers matches our input configuration, but we know that the bufferset
           has the capacity to handle our outputs.
        */
+
        assert (bufs.available() >= ChanCount (DataType::AUDIO, output_channels));
 
-       current_buffers = &bufs;
-       current_maxbuf = bufs.count().n_audio(); // number of input audio buffers
-       current_offset = offset;
+       input_buffers = &bufs;
+       input_maxbuf = bufs.count().n_audio(); // number of input audio buffers
+       input_offset = offset;
        cb_offset = 0;
 
-       DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("%1 in %2 out %3 MIDI %4 bufs %5 (available %6)\n",
-                                                       name(), input_channels, output_channels, _has_midi_input,
-                                                       bufs.count(), bufs.available()));
-
        buffers->mNumberBuffers = output_channels;
 
-       for (uint32_t i = 0; i < output_channels; ++i) {
+       for (int32_t i = 0; i < output_channels; ++i) {
                buffers->mBuffers[i].mNumberChannels = 1;
                buffers->mBuffers[i].mDataByteSize = nframes * sizeof (Sample);
                /* setting this to 0 indicates to the AU that it can provide buffers here
                   if necessary. if it can process in-place, it will use the buffers provided
-                  as input by ::render_callback() above. no documentation on what setting it
-                  to a non-null value means.
+                  as input by ::render_callback() above. 
+                   
+                   a non-null values tells the plugin to render into the buffer pointed
+                   at by the value.
                */
                buffers->mBuffers[i].mData = 0;
        }
@@ -1342,12 +1395,12 @@ AUPlugin::connect_and_run (BufferSet& bufs, ChanMapping in_map, ChanMapping out_
        ts.mSampleTime = frames_processed;
        ts.mFlags = kAudioTimeStampSampleTimeValid;
 
-       // DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("%1 render flags=%2 time=%3 nframes=%4 buffers=%5\n",
-       // name(), flags, frames_processed, nframes, buffers->mNumberBuffers));
+       DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("%1 render flags=%2 time=%3 nframes=%4 buffers=%5\n",
+                                                        name(), flags, frames_processed, nframes, buffers->mNumberBuffers));
 
        if ((err = unit->Render (&flags, &ts, 0, nframes, buffers)) == noErr) {
 
-               current_maxbuf = 0;
+               input_maxbuf = 0;
                frames_processed += nframes;
 
                DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("%1 rendered %2 buffers of %3\n",
@@ -1376,7 +1429,7 @@ AUPlugin::connect_and_run (BufferSet& bufs, ChanMapping in_map, ChanMapping out_
                return 0;
        }
 
-       cerr << name() << " render status " << err << endl;
+       error << string_compose (_("AU: render error for %1, status = %2"), name(), err) << endmsg;
        return -1;
 }
 
@@ -1398,12 +1451,12 @@ AUPlugin::get_beat_and_tempo_callback (Float64* outCurrentBeat,
        }
 
        Timecode::BBT_Time bbt;
-       TempoMetric metric = tmap.metric_at (_session.transport_frame() + current_offset);
-       tmap.bbt_time_with_metric (_session.transport_frame() + current_offset, bbt, metric);
+       TempoMetric metric = tmap.metric_at (_session.transport_frame() + input_offset);
+       tmap.bbt_time (_session.transport_frame() + input_offset, bbt);
 
        if (outCurrentBeat) {
                float beat;
-               beat = metric.meter().beats_per_bar() * bbt.bars;
+               beat = metric.meter().divisions_per_bar() * bbt.bars;
                beat += bbt.beats;
                beat += bbt.ticks / Timecode::BBT_Time::ticks_per_beat;
                *outCurrentBeat = beat;
@@ -1437,8 +1490,8 @@ AUPlugin::get_musical_time_location_callback (UInt32*   outDeltaSampleOffsetToNe
        }
 
        Timecode::BBT_Time bbt;
-       TempoMetric metric = tmap.metric_at (_session.transport_frame() + current_offset);
-       tmap.bbt_time_with_metric (_session.transport_frame() + current_offset, bbt, metric);
+       TempoMetric metric = tmap.metric_at (_session.transport_frame() + input_offset);
+       tmap.bbt_time (_session.transport_frame() + input_offset, bbt);
 
        if (outDeltaSampleOffsetToNextBeat) {
                if (bbt.ticks == 0) {
@@ -1447,12 +1500,12 @@ AUPlugin::get_musical_time_location_callback (UInt32*   outDeltaSampleOffsetToNe
                } else {
                        *outDeltaSampleOffsetToNextBeat = (UInt32) 
                                floor (((Timecode::BBT_Time::ticks_per_beat - bbt.ticks)/Timecode::BBT_Time::ticks_per_beat) * // fraction of a beat to next beat
-                                      metric.tempo().frames_per_beat(_session.frame_rate(), metric.meter())); // frames per beat
+                                      metric.tempo().frames_per_beat (_session.frame_rate())); // frames per beat
                }
        }
 
        if (outTimeSig_Numerator) {
-               *outTimeSig_Numerator = (UInt32) lrintf (metric.meter().beats_per_bar());
+               *outTimeSig_Numerator = (UInt32) lrintf (metric.meter().divisions_per_bar());
        }
        if (outTimeSig_Denominator) {
                *outTimeSig_Denominator = (UInt32) lrintf (metric.meter().note_divisor());
@@ -1462,12 +1515,12 @@ AUPlugin::get_musical_time_location_callback (UInt32*   outDeltaSampleOffsetToNe
 
                /* beat for the start of the bar.
                   1|1|0 -> 1
-                  2|1|0 -> 1 + beats_per_bar
-                  3|1|0 -> 1 + (2 * beats_per_bar)
+                  2|1|0 -> 1 + divisions_per_bar
+                  3|1|0 -> 1 + (2 * divisions_per_bar)
                   etc.
                */
 
-               *outCurrentMeasureDownBeat = 1 + metric.meter().beats_per_bar() * (bbt.bars - 1);
+               *outCurrentMeasureDownBeat = 1 + metric.meter().divisions_per_bar() * (bbt.bars - 1);
        }
 
        return noErr;
@@ -1505,9 +1558,9 @@ AUPlugin::get_transport_state_callback (Boolean*  outIsPlaying,
 
        if (outCurrentSampleInTimeLine) {
                /* this assumes that the AU can only call this host callback from render context,
-                  where current_offset is valid.
+                  where input_offset is valid.
                */
-               *outCurrentSampleInTimeLine = _session.transport_frame() + current_offset;
+               *outCurrentSampleInTimeLine = _session.transport_frame() + input_offset;
        }
 
        if (outIsCycling) {
@@ -1532,11 +1585,11 @@ AUPlugin::get_transport_state_callback (Boolean*  outIsPlaying,
                                Timecode::BBT_Time bbt;
 
                                if (outCycleStartBeat) {
-                                       TempoMetric metric = tmap.metric_at (loc->start() + current_offset);
-                                       _session.tempo_map().bbt_time_with_metric (loc->start(), bbt, metric);
+                                       TempoMetric metric = tmap.metric_at (loc->start() + input_offset);
+                                       _session.tempo_map().bbt_time (loc->start(), bbt);
 
                                        float beat;
-                                       beat = metric.meter().beats_per_bar() * bbt.bars;
+                                       beat = metric.meter().divisions_per_bar() * bbt.bars;
                                        beat += bbt.beats;
                                        beat += bbt.ticks / Timecode::BBT_Time::ticks_per_beat;
 
@@ -1544,11 +1597,11 @@ AUPlugin::get_transport_state_callback (Boolean*  outIsPlaying,
                                }
 
                                if (outCycleEndBeat) {
-                                       TempoMetric metric = tmap.metric_at (loc->end() + current_offset);
-                                       _session.tempo_map().bbt_time_with_metric (loc->end(), bbt, metric);
+                                       TempoMetric metric = tmap.metric_at (loc->end() + input_offset);
+                                       _session.tempo_map().bbt_time (loc->end(), bbt);
 
                                        float beat;
-                                       beat = metric.meter().beats_per_bar() * bbt.bars;
+                                       beat = metric.meter().divisions_per_bar() * bbt.bars;
                                        beat += bbt.beats;
                                        beat += bbt.ticks / Timecode::BBT_Time::ticks_per_beat;
 
@@ -2084,7 +2137,7 @@ AUPlugin::find_presets ()
        for (FactoryPresetMap::iterator i = factory_preset_map.begin(); i != factory_preset_map.end(); ++i) {
                /* XXX: dubious */
                string const uri = string_compose ("%1", _presets.size ());
-               _presets.insert (make_pair (uri, Plugin::PresetRecord (uri, i->first)));
+               _presets.insert (make_pair (uri, Plugin::PresetRecord (uri, i->first, i->second)));
        }
 
 #endif
@@ -2140,7 +2193,7 @@ AUPluginInfo::load (Session& session)
 Glib::ustring
 AUPluginInfo::au_cache_path ()
 {
-       return Glib::build_filename (ARDOUR::user_config_directory().to_string(), "au_cache");
+       return Glib::build_filename (ARDOUR::user_config_directory(), "au_cache");
 }
 
 PluginInfoList*
@@ -2656,9 +2709,141 @@ void
 AUPlugin::set_info (PluginInfoPtr info)
 {
        Plugin::set_info (info);
-
+       
        AUPluginInfoPtr pinfo = boost::dynamic_pointer_cast<AUPluginInfo>(get_info());
-
        _has_midi_input = pinfo->needs_midi_input ();
        _has_midi_output = false;
 }
+
+int
+AUPlugin::create_parameter_listener (AUEventListenerProc cb, void* arg, float interval_secs)
+{
+#ifdef WITH_CARBON
+       CFRunLoopRef run_loop = (CFRunLoopRef) GetCFRunLoopFromEventLoop(GetCurrentEventLoop()); 
+#else
+       CFRunLoopRef run_loop = CFRunLoopGetCurrent();
+#endif
+       CFStringRef  loop_mode = kCFRunLoopDefaultMode;
+
+       if (AUEventListenerCreate (cb, arg, run_loop, loop_mode, interval_secs, interval_secs, &_parameter_listener) != noErr) {
+               return -1;
+       }
+
+       _parameter_listener_arg = arg;
+
+       return 0;
+}
+
+int
+AUPlugin::listen_to_parameter (uint32_t param_id)
+{
+       AudioUnitEvent      event;
+
+       if (!_parameter_listener || param_id >= descriptors.size()) {
+               return -2;
+       }
+
+       event.mEventType = kAudioUnitEvent_ParameterValueChange;
+       event.mArgument.mParameter.mAudioUnit = unit->AU();
+       event.mArgument.mParameter.mParameterID = descriptors[param_id].id;
+       event.mArgument.mParameter.mScope = descriptors[param_id].scope;
+       event.mArgument.mParameter.mElement = descriptors[param_id].element;
+
+       if (AUEventListenerAddEventType (_parameter_listener, _parameter_listener_arg, &event) != noErr) {
+               return -1;
+       } 
+
+       event.mEventType = kAudioUnitEvent_BeginParameterChangeGesture;
+       event.mArgument.mParameter.mAudioUnit = unit->AU();
+       event.mArgument.mParameter.mParameterID = descriptors[param_id].id;
+       event.mArgument.mParameter.mScope = descriptors[param_id].scope;
+       event.mArgument.mParameter.mElement = descriptors[param_id].element;
+
+       if (AUEventListenerAddEventType (_parameter_listener, _parameter_listener_arg, &event) != noErr) {
+               return -1;
+       } 
+
+       event.mEventType = kAudioUnitEvent_EndParameterChangeGesture;
+       event.mArgument.mParameter.mAudioUnit = unit->AU();
+       event.mArgument.mParameter.mParameterID = descriptors[param_id].id;
+       event.mArgument.mParameter.mScope = descriptors[param_id].scope;
+       event.mArgument.mParameter.mElement = descriptors[param_id].element;
+
+       if (AUEventListenerAddEventType (_parameter_listener, _parameter_listener_arg, &event) != noErr) {
+               return -1;
+       } 
+
+       return 0;
+}
+
+int
+AUPlugin::end_listen_to_parameter (uint32_t param_id)
+{
+       AudioUnitEvent      event;
+
+       if (!_parameter_listener || param_id >= descriptors.size()) {
+               return -2;
+       }
+
+       event.mEventType = kAudioUnitEvent_ParameterValueChange;
+       event.mArgument.mParameter.mAudioUnit = unit->AU();
+       event.mArgument.mParameter.mParameterID = descriptors[param_id].id;
+       event.mArgument.mParameter.mScope = descriptors[param_id].scope;
+       event.mArgument.mParameter.mElement = descriptors[param_id].element;
+
+       if (AUEventListenerRemoveEventType (_parameter_listener, _parameter_listener_arg, &event) != noErr) {
+               return -1;
+       } 
+
+       event.mEventType = kAudioUnitEvent_BeginParameterChangeGesture;
+       event.mArgument.mParameter.mAudioUnit = unit->AU();
+       event.mArgument.mParameter.mParameterID = descriptors[param_id].id;
+       event.mArgument.mParameter.mScope = descriptors[param_id].scope;
+       event.mArgument.mParameter.mElement = descriptors[param_id].element;
+
+       if (AUEventListenerRemoveEventType (_parameter_listener, _parameter_listener_arg, &event) != noErr) {
+               return -1;
+       } 
+
+       event.mEventType = kAudioUnitEvent_EndParameterChangeGesture;
+       event.mArgument.mParameter.mAudioUnit = unit->AU();
+       event.mArgument.mParameter.mParameterID = descriptors[param_id].id;
+       event.mArgument.mParameter.mScope = descriptors[param_id].scope;
+       event.mArgument.mParameter.mElement = descriptors[param_id].element;
+
+       if (AUEventListenerRemoveEventType (_parameter_listener, _parameter_listener_arg, &event) != noErr) {
+               return -1;
+       } 
+
+       return 0;
+}
+
+void
+AUPlugin::_parameter_change_listener (void* arg, void* src, const AudioUnitEvent* event, UInt64 host_time, Float32 new_value)
+{
+       ((AUPlugin*) arg)->parameter_change_listener (arg, src, event, host_time, new_value);
+}
+
+void
+AUPlugin::parameter_change_listener (void* /*arg*/, void* /*src*/, const AudioUnitEvent* event, UInt64 /*host_time*/, Float32 new_value)
+{
+        ParameterMap::iterator i;
+
+        if ((i = parameter_map.find (event->mArgument.mParameter.mParameterID)) == parameter_map.end()) {
+                return;
+        }
+        
+        switch (event->mEventType) {
+        case kAudioUnitEvent_BeginParameterChangeGesture:
+                StartTouch (i->second);
+                break;
+        case kAudioUnitEvent_EndParameterChangeGesture:
+                EndTouch (i->second);
+                break;
+        case kAudioUnitEvent_ParameterValueChange:
+                ParameterChanged (i->second, new_value);
+                break;
+        default:
+                break;
+        }
+}