change API for GainControl, VCA and VCAManager
[ardour.git] / libs / ardour / audio_unit.cc
index e28d89205395d6c1c2b75d7636cf5155fa4b172c..70d2853f8c15ad5942d5b2d8b766496613dad569 100644 (file)
@@ -1136,7 +1136,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 +1165,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;
                }
@@ -1261,7 +1261,7 @@ AUPlugin::can_support_io_configuration (const ChanCount& in, ChanCount& out, Cha
                                                        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 +1343,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);                    \
   }                                                \
 }
@@ -1606,9 +1606,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 +1652,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 +1710,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 +1760,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);
+       Timecode::BBT_Time bbt = _session.tempo_map().bbt_at_frame (_session.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 +1799,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);
+       Timecode::BBT_Time bbt = _session.tempo_map().bbt_at_frame (_session.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 (_session.transport_frame() + input_offset) + beat_frac_to_next);
                }
        }
 
@@ -1889,7 +1894,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 +1906,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;
@@ -2046,7 +2051,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 +2090,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;
@@ -3040,17 +3045,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) {
@@ -3059,6 +3068,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));
@@ -3153,7 +3163,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;
@@ -3171,7 +3181,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;
@@ -3209,8 +3219,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)) {