improve AU Latency PropertyChange Events
[ardour.git] / libs / ardour / audio_unit.cc
index a520fbb00e981d4477ee4ae77f6f92a622590536..85a387e9339aa6b9b7e0d6fe9393fd3b2da92730 100644 (file)
@@ -71,7 +71,7 @@
 #define ArdourFindNext AudioComponentFindNext
 #endif
 
-#include "i18n.h"
+#include "pbd/i18n.h"
 
 using namespace std;
 using namespace PBD;
@@ -430,7 +430,8 @@ AUPlugin::AUPlugin (AudioEngine& engine, Session& session, boost::shared_ptr<CAC
        , comp (_comp)
        , unit (new CAAudioUnit)
        , initialized (false)
-       , _current_block_size (0)
+       , _last_nframes (0)
+       , _current_latency (UINT_MAX)
        , _requires_fixed_size_buffers (false)
        , buffers (0)
        , variable_inputs (false)
@@ -448,8 +449,8 @@ AUPlugin::AUPlugin (AudioEngine& engine, Session& session, boost::shared_ptr<CAC
        , audio_input_cnt (0)
        , _parameter_listener (0)
        , _parameter_listener_arg (0)
-       , transport_frame (false)
-       , transport_speed (false)
+       , transport_frame (0)
+       , transport_speed (0)
        , last_transport_speed (0.0)
 {
        if (!preset_search_path_initialized) {
@@ -470,8 +471,8 @@ AUPlugin::AUPlugin (const AUPlugin& other)
        , comp (other.get_comp())
        , unit (new CAAudioUnit)
        , initialized (false)
-       , _current_block_size (0)
        , _last_nframes (0)
+       , _current_latency (UINT_MAX)
        , _requires_fixed_size_buffers (false)
        , buffers (0)
        , variable_inputs (false)
@@ -482,11 +483,15 @@ AUPlugin::AUPlugin (const AUPlugin& other)
        , bus_outputs (0)
        , input_maxbuf (0)
        , input_offset (0)
+       , cb_offsets (0)
        , input_buffers (0)
        , input_map (0)
        , frames_processed (0)
        , _parameter_listener (0)
        , _parameter_listener_arg (0)
+       , transport_frame (0)
+       , transport_speed (0)
+       , last_transport_speed (0.0)
 
 {
        init ();
@@ -950,7 +955,12 @@ AUPlugin::default_value (uint32_t port)
 framecnt_t
 AUPlugin::signal_latency () const
 {
-       return unit->Latency() * _session.frame_rate();
+       guint lat = g_atomic_int_get (&_current_latency);;
+       if (lat == UINT_MAX) {
+               lat = unit->Latency() * _session.frame_rate();
+               g_atomic_int_set (&_current_latency, lat);
+       }
+       return lat;
 }
 
 void
@@ -1077,8 +1087,6 @@ AUPlugin::set_block_size (pframes_t nframes)
                activate ();
        }
 
-       _current_block_size = nframes;
-
        return 0;
 }
 
@@ -1263,16 +1271,36 @@ AUPlugin::can_support_io_configuration (const ChanCount& in, ChanCount& out, Cha
                for (vector<pair<int,int> >::const_iterator i = ioc.begin(); i != ioc.end(); ++i) {
                        int32_t possible_in = i->first;
                        int32_t possible_out = i->second;
-                       if (possible_in <= 1 || possible_out <= 1) {
+                       if (possible_in < 1 || possible_out < 1) {
                                continue;
                        }
                        for (uint32_t i = 1; i < input_elements; ++i) {
-                               // TODO: consider up-to bus_inputs[i]
+                               // can't use up-to bus_inputs[]
+                               // waves' SC-C6(s) for example fails to configure with only 1 input
+                               // on the 2nd bus.
                                io_configs.push_back (pair<int,int> (possible_in + bus_inputs[i], possible_out));
                        }
                }
        }
 
+       if (output_elements > 1) {
+               const vector<pair<int,int> >& ioc (pinfo->cache.io_configs);
+               for (vector<pair<int,int> >::const_iterator i = ioc.begin(); i != ioc.end(); ++i) {
+                       int32_t possible_in = i->first;
+                       int32_t possible_out = i->second;
+                       if (possible_in < 1 || possible_out < 1) {
+                               continue;
+                       }
+                       for (uint32_t i = 1; i < output_elements; ++i) {
+                               int32_t c = bus_outputs[i];
+                               for (uint32_t j = 1; j < i; ++j) {
+                                       c += bus_outputs [j];
+                               }
+                               io_configs.push_back (pair<int,int> (possible_in, possible_out + c));
+                       }
+               }
+       }
+
        DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("%1 has %2 IO configurations, looking for %3 in, %4 out\n",
                                                        name(), io_configs.size(), in, out));
 
@@ -3337,6 +3365,19 @@ AUPlugin::create_parameter_listener (AUEventListenerProc cb, void* arg, float in
 
        _parameter_listener_arg = arg;
 
+       // listen for latency changes
+       AudioUnitEvent event;
+       event.mEventType = kAudioUnitEvent_PropertyChange;
+       event.mArgument.mProperty.mAudioUnit = unit->AU();
+       event.mArgument.mProperty.mPropertyID = kAudioUnitProperty_Latency;
+       event.mArgument.mProperty.mScope = kAudioUnitScope_Global;
+       event.mArgument.mProperty.mElement = 0;
+
+       if (AUEventListenerAddEventType (_parameter_listener, _parameter_listener_arg, &event) != noErr) {
+               PBD::error << "Failed to create latency event listener\n";
+               // TODO don't cache _current_latency
+       }
+
        return 0;
 }
 
@@ -3433,6 +3474,15 @@ AUPlugin::_parameter_change_listener (void* arg, void* src, const AudioUnitEvent
 void
 AUPlugin::parameter_change_listener (void* /*arg*/, void* src, const AudioUnitEvent* event, UInt64 /*host_time*/, Float32 new_value)
 {
+       if (event->mEventType == kAudioUnitEvent_PropertyChange) {
+               if (event->mArgument.mProperty.mPropertyID == kAudioUnitProperty_Latency) {
+                       DEBUG_TRACE (DEBUG::AudioUnits, string_compose("AU Latency Change Event %1 <> %2\n", new_value, unit->Latency()));
+                       guint lat = unit->Latency() * _session.frame_rate();
+                       g_atomic_int_set (&_current_latency, lat);
+               }
+               return;
+       }
+
         ParameterMap::iterator i;
 
         if ((i = parameter_map.find (event->mArgument.mParameter.mParameterID)) == parameter_map.end()) {