enough with umpteen "i18n.h" files. Consolidate on pbd/i18n.h
[ardour.git] / libs / ardour / audio_unit.cc
index 1862844cd90a89b1c78f2a2869b19534853975e0..88cc197d60d5b50023d047dfd56db65f7edf258d 100644 (file)
@@ -71,7 +71,7 @@
 #define ArdourFindNext AudioComponentFindNext
 #endif
 
-#include "i18n.h"
+#include "pbd/i18n.h"
 
 using namespace std;
 using namespace PBD;
@@ -448,7 +448,8 @@ AUPlugin::AUPlugin (AudioEngine& engine, Session& session, boost::shared_ptr<CAC
        , audio_input_cnt (0)
        , _parameter_listener (0)
        , _parameter_listener_arg (0)
-       , last_transport_rolling (false)
+       , transport_frame (false)
+       , transport_speed (false)
        , last_transport_speed (0.0)
 {
        if (!preset_search_path_initialized) {
@@ -1136,7 +1137,7 @@ AUPlugin::configure_io (ChanCount in, ChanCount out)
        uint32_t used_in = 0;
        uint32_t used_out = 0;
 
-       if (variable_inputs) {
+       if (variable_inputs || input_elements == 1) {
                // we only ever use the first bus
                if (input_elements > 1) {
                        warning << string_compose (_("AU %1 has multiple input busses and variable port count."), name()) << endmsg;
@@ -1165,7 +1166,7 @@ AUPlugin::configure_io (ChanCount in, ChanCount out)
                }
        }
 
-       if (variable_outputs) {
+       if (variable_outputs || output_elements == 1) {
                if (output_elements > 1) {
                        warning << string_compose (_("AU %1 has multiple output busses and variable port count."), name()) << endmsg;
                }
@@ -1255,13 +1256,48 @@ AUPlugin::can_support_io_configuration (const ChanCount& in, ChanCount& out, Cha
                return false;
        }
 
-       vector<pair<int,int> >& io_configs = pinfo->cache.io_configs;
+       vector<pair<int,int> > io_configs = pinfo->cache.io_configs;
+
+       if (input_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 < input_elements; ++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));
 
 #if 0
-       printf ("AU I/O Configs %s %d\n", name().c_str(), io_configs.size());
+       printf ("AU I/O Configs %s %d\n", name(), io_configs.size());
        for (vector<pair<int,int> >::iterator i = io_configs.begin(); i != io_configs.end(); ++i) {
                printf ("- I/O  %d / %d\n", i->first, i->second);
        }
@@ -1343,7 +1379,7 @@ AUPlugin::can_support_io_configuration (const ChanCount& in, ChanCount& out, Cha
   _output_configs.insert (0);
 
 #define UPTO(nch) {                                \
-  for (int n = 1; n < nch; ++n) {                  \
+  for (int n = 1; n <= nch; ++n) {                 \
     _output_configs.insert (n);                    \
   }                                                \
 }
@@ -1584,9 +1620,15 @@ AUPlugin::render_callback(AudioUnitRenderActionFlags*,
 }
 
 int
-AUPlugin::connect_and_run (BufferSet& bufs, ChanMapping in_map, ChanMapping out_map, pframes_t nframes, framecnt_t offset)
+AUPlugin::connect_and_run (BufferSet& bufs,
+               framepos_t start, framepos_t end, double speed,
+               ChanMapping in_map, ChanMapping out_map,
+               pframes_t nframes, framecnt_t offset)
 {
-       Plugin::connect_and_run (bufs, in_map, out_map, nframes, offset);
+       Plugin::connect_and_run(bufs, start, end, speed, in_map, out_map, nframes, offset);
+
+       transport_frame = start;
+       transport_speed = speed;
 
        AudioUnitRenderActionFlags flags = 0;
        AudioTimeStamp ts;
@@ -1606,9 +1648,10 @@ AUPlugin::connect_and_run (BufferSet& bufs, ChanMapping in_map, ChanMapping out_
                inplace = false;
        }
 
-       DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("%1 in %2 out %3 MIDI %4 bufs %5 (available %6) Inplace: %7\n",
+       DEBUG_TRACE (DEBUG::AudioUnits, string_compose ("%1 in %2 out %3 MIDI %4 bufs %5 (available %6) InBus %7 OutBus %8 Inplace: %9 var-i/o %10 %11\n",
                                name(), input_channels, output_channels, _has_midi_input,
-                               bufs.count(), bufs.available(), inplace));
+                               bufs.count(), bufs.available(),
+                               configured_input_busses, configured_output_busses, inplace, variable_inputs, variable_outputs));
 
        /* the apparent number of buffers matches our input configuration, but we know that the bufferset
         * has the capacity to handle our outputs.
@@ -1651,7 +1694,12 @@ AUPlugin::connect_and_run (BufferSet& bufs, ChanMapping in_map, ChanMapping out_
        uint32_t busoff = 0;
        uint32_t remain = output_channels;
        for (uint32_t bus = 0; remain > 0 && bus < configured_output_busses; ++bus) {
-               uint32_t cnt = std::min (remain, bus_outputs[bus]);
+               uint32_t cnt;
+               if (variable_outputs || (output_elements == configured_output_busses && configured_output_busses == 1)) {
+                       cnt = output_channels;
+               } else {
+                       cnt = std::min (remain, bus_outputs[bus]);
+               }
                assert (cnt > 0);
 
                buffers->mNumberBuffers = cnt;
@@ -1704,7 +1752,8 @@ AUPlugin::connect_and_run (BufferSet& bufs, ChanMapping in_map, ChanMapping out_
                                }
                        }
                } else {
-                       error << string_compose (_("AU: render error for %1, bus %2 status = %2"), name(), bus, err) << endmsg;
+                       DEBUG_TRACE (DEBUG::AudioUnits, string_compose (_("AU: render error for %1, bus %2 status = %3\n"), name(), bus, err));
+                       error << string_compose (_("AU: render error for %1, bus %2 status = %3"), name(), bus, err) << endmsg;
                        ok = false;
                        break;
                }
@@ -1753,16 +1802,16 @@ AUPlugin::get_beat_and_tempo_callback (Float64* outCurrentBeat,
                return kAudioUnitErr_CannotDoInCurrentContext;
        }
 
-       Timecode::BBT_Time bbt;
-       TempoMetric metric = tmap.metric_at (_session.transport_frame() + input_offset);
-       tmap.bbt_time (_session.transport_frame() + input_offset, bbt);
+       TempoMetric metric = tmap.metric_at (transport_frame + input_offset);
+       Timecode::BBT_Time bbt = _session.tempo_map().bbt_at_frame (transport_frame + input_offset);
 
        if (outCurrentBeat) {
+               const double ppq_scaling = metric.meter().note_divisor() / 4.0;
                float beat;
-               beat = metric.meter().divisions_per_bar() * bbt.bars;
-               beat += bbt.beats;
+               beat = metric.meter().divisions_per_bar() * (bbt.bars - 1);
+               beat += (bbt.beats - 1);
                beat += bbt.ticks / Timecode::BBT_Time::ticks_per_beat;
-               *outCurrentBeat = beat;
+               *outCurrentBeat = beat * ppq_scaling;
        }
 
        if (outCurrentTempo) {
@@ -1792,18 +1841,16 @@ AUPlugin::get_musical_time_location_callback (UInt32*   outDeltaSampleOffsetToNe
                return kAudioUnitErr_CannotDoInCurrentContext;
        }
 
-       Timecode::BBT_Time bbt;
-       TempoMetric metric = tmap.metric_at (_session.transport_frame() + input_offset);
-       tmap.bbt_time (_session.transport_frame() + input_offset, bbt);
+       TempoMetric metric = tmap.metric_at (transport_frame + input_offset);
+       Timecode::BBT_Time bbt = _session.tempo_map().bbt_at_frame (transport_frame + input_offset);
 
        if (outDeltaSampleOffsetToNextBeat) {
                if (bbt.ticks == 0) {
                        /* on the beat */
                        *outDeltaSampleOffsetToNextBeat = 0;
                } 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())); // frames per beat
+                       double const beat_frac_to_next = (Timecode::BBT_Time::ticks_per_beat - bbt.ticks) / Timecode::BBT_Time::ticks_per_beat;
+                       *outDeltaSampleOffsetToNextBeat = tmap.frame_at_beat (tmap.beat_at_frame (transport_frame + input_offset) + beat_frac_to_next);
                }
        }
 
@@ -1837,22 +1884,20 @@ AUPlugin::get_transport_state_callback (Boolean*  outIsPlaying,
                                        Float64*  outCycleStartBeat,
                                        Float64*  outCycleEndBeat)
 {
-       bool rolling;
-       float speed;
+       const bool rolling = (transport_speed != 0);
+       const bool last_transport_rolling = (last_transport_speed != 0);
 
        DEBUG_TRACE (DEBUG::AudioUnits, "AU calls ardour transport state callback\n");
 
-       rolling = _session.transport_rolling();
-       speed = _session.transport_speed ();
 
        if (outIsPlaying) {
-               *outIsPlaying = _session.transport_rolling();
+               *outIsPlaying = rolling;
        }
 
        if (outTransportStateChanged) {
                if (rolling != last_transport_rolling) {
                        *outTransportStateChanged = true;
-               } else if (speed != last_transport_speed) {
+               } else if (transport_speed != last_transport_speed) {
                        *outTransportStateChanged = true;
                } else {
                        *outTransportStateChanged = false;
@@ -1863,13 +1908,14 @@ AUPlugin::get_transport_state_callback (Boolean*  outIsPlaying,
                /* this assumes that the AU can only call this host callback from render context,
                   where input_offset is valid.
                */
-               *outCurrentSampleInTimeLine = _session.transport_frame() + input_offset;
+               *outCurrentSampleInTimeLine = transport_frame + input_offset;
        }
 
        if (outIsCycling) {
+               // TODO check bounce-processing
                Location* loc = _session.locations()->auto_loop_location();
 
-               *outIsCycling = (loc && _session.transport_rolling() && _session.get_play_loop());
+               *outIsCycling = (loc && rolling && _session.get_play_loop());
 
                if (*outIsCycling) {
 
@@ -1889,7 +1935,7 @@ AUPlugin::get_transport_state_callback (Boolean*  outIsPlaying,
 
                                if (outCycleStartBeat) {
                                        TempoMetric metric = tmap.metric_at (loc->start() + input_offset);
-                                       _session.tempo_map().bbt_time (loc->start(), bbt);
+                                       bbt = _session.tempo_map().bbt_at_frame (loc->start() + input_offset);
 
                                        float beat;
                                        beat = metric.meter().divisions_per_bar() * bbt.bars;
@@ -1901,7 +1947,7 @@ AUPlugin::get_transport_state_callback (Boolean*  outIsPlaying,
 
                                if (outCycleEndBeat) {
                                        TempoMetric metric = tmap.metric_at (loc->end() + input_offset);
-                                       _session.tempo_map().bbt_time (loc->end(), bbt);
+                                       bbt = _session.tempo_map().bbt_at_frame (loc->end() + input_offset);
 
                                        float beat;
                                        beat = metric.meter().divisions_per_bar() * bbt.bars;
@@ -1914,8 +1960,7 @@ AUPlugin::get_transport_state_callback (Boolean*  outIsPlaying,
                }
        }
 
-       last_transport_rolling = rolling;
-       last_transport_speed = speed;
+       last_transport_speed = transport_speed;
 
        return noErr;
 }
@@ -2046,7 +2091,7 @@ AUPlugin::parameter_is_output (uint32_t param) const
 void
 AUPlugin::add_state (XMLNode* root) const
 {
-       LocaleGuard lg (X_("C"));
+       LocaleGuard lg;
        CFDataRef xmlData;
        CFPropertyListRef propertyList;
 
@@ -2085,7 +2130,7 @@ AUPlugin::set_state(const XMLNode& node, int version)
 {
        int ret = -1;
        CFPropertyListRef propertyList;
-       LocaleGuard lg (X_("C"));
+       LocaleGuard lg;
 
        if (node.name() != state_node_name()) {
                error << _("Bad node sent to AUPlugin::set_state") << endmsg;
@@ -2698,6 +2743,8 @@ AUPluginInfo::discover (bool scan_only)
 
        if (!Glib::file_test (au_cache_path(), Glib::FILE_TEST_EXISTS)) {
                ARDOUR::BootMessage (_("Discovering AudioUnit plugins (could take some time ...)"));
+               // flush RAM cache -- after clear_cache()
+               cached_info.clear();
        }
        // create crash log file
        au_start_crashlog ();
@@ -3038,17 +3085,21 @@ AUPluginInfo::cached_io_configuration (const std::string& unique_id,
        }
 
        if (ret > 0) {
-
-               /* no explicit info available, so default to 1in/1out */
-
-               /* XXX this is wrong. we should be indicating wildcard values */
-
+               /* AU is expected to deal with same channel valance in and out */
                cinfo.io_configs.push_back (pair<int,int> (-1, -1));
-
        } else {
-               /* store each configuration */
-               if (comp.Desc().IsGenerator() || comp.Desc().IsMusicDevice()) {
-                       // incrementally add busses
+               /* CAAudioUnit::GetChannelInfo silently merges bus formats
+                * check if this was the case and if so, add
+                * bus configs as incremental options.
+                */
+               Boolean* isWritable = 0;
+               UInt32  dataSize = 0;
+               OSStatus result = AudioUnitGetPropertyInfo (unit.AU(),
+                               kAudioUnitProperty_SupportedNumChannels,
+                               kAudioUnitScope_Global, 0,
+                               &dataSize, isWritable);
+               if (result != noErr && (comp.Desc().IsGenerator() || comp.Desc().IsMusicDevice())) {
+                       /* incrementally add busses */
                        int in = 0;
                        int out = 0;
                        for (uint32_t n = 0; n < cnt; ++n) {
@@ -3057,6 +3108,7 @@ AUPluginInfo::cached_io_configuration (const std::string& unique_id,
                                cinfo.io_configs.push_back (pair<int,int> (in, out));
                        }
                } else {
+                       /* store each configuration */
                        for (uint32_t n = 0; n < cnt; ++n) {
                                cinfo.io_configs.push_back (pair<int,int> (channel_info[n].inChannels,
                                                        channel_info[n].outChannels));
@@ -3072,6 +3124,17 @@ AUPluginInfo::cached_io_configuration (const std::string& unique_id,
        return 0;
 }
 
+void
+AUPluginInfo::clear_cache ()
+{
+       const string& fn = au_cache_path();
+       if (Glib::file_test (fn, Glib::FILE_TEST_EXISTS)) {
+               ::g_unlink(fn.c_str());
+       }
+       // keep cached_info in RAM until restart or re-scan
+       cached_info.clear();
+}
+
 void
 AUPluginInfo::add_cached_info (const std::string& id, AUPluginCachedInfo& cinfo)
 {
@@ -3140,7 +3203,7 @@ AUPluginInfo::load_cached_info ()
        }
 
        //initial version has incorrectly stored i/o info, and/or garbage chars.
-       const XMLProperty* version = root->property(X_("version"));
+       XMLProperty const * version = root->property(X_("version"));
        if (! ((version != NULL) && (version->value() == X_(AU_CACHE_VERSION)))) {
                error << "au_cache is not correct version.  AU plugins will be re-scanned" << endmsg;
                return -1;
@@ -3158,7 +3221,7 @@ AUPluginInfo::load_cached_info ()
 
                        const XMLNode* gchild;
                        const XMLNodeList gchildren = child->children();
-                       const XMLProperty* prop = child->property (X_("id"));
+                       XMLProperty const * prop = child->property (X_("id"));
 
                        if (!prop) {
                                continue;
@@ -3196,8 +3259,8 @@ AUPluginInfo::load_cached_info ()
 
                                        int in;
                                        int out;
-                                       const XMLProperty* iprop;
-                                       const XMLProperty* oprop;
+                                       XMLProperty const * iprop;
+                                       XMLProperty const * oprop;
 
                                        if (((iprop = gchild->property (X_("in"))) != 0) &&
                                            ((oprop = gchild->property (X_("out"))) != 0)) {