Move MIDI filters imprecise handling from 2nd pass to 1st
[ardour.git] / libs / ardour / luaproc.cc
index f1f64f038b2c8403fc8f3bc5491f2c4ad6b15578..9b9b7fa46dfa79853b15182e5fc5da6195b7e655 100644 (file)
@@ -346,10 +346,6 @@ LuaProc::can_support_io_configuration (const ChanCount& in, ChanCount& out, Chan
        // caller must hold process lock (no concurrent calls to interpreter
        _output_configs.clear ();
 
-       if (in.n_midi() > 0 && !_has_midi_input && !imprecise) {
-               return false;
-       }
-
        lua_State* L = lua.getState ();
        luabridge::LuaRef ioconfig = luabridge::getGlobal (L, "dsp_ioconfig");
        if (!ioconfig.isFunction ()) {
@@ -379,6 +375,7 @@ LuaProc::can_support_io_configuration (const ChanCount& in, ChanCount& out, Chan
        }
 
        const int audio_in = in.n_audio ();
+       const int midi_in = in.n_midi ();
 
        // preferred setting (provided by plugin_insert)
        const int preferred_out = out.n_audio ();
@@ -388,17 +385,32 @@ LuaProc::can_support_io_configuration (const ChanCount& in, ChanCount& out, Chan
        float penalty = 9999;
        bool found = false;
 
-#define FOUNDCFG(nch) {                            \
-  float p = fabsf ((float)(nch) - preferred_out);  \
-  _output_configs.insert (nch);                    \
-  if ((nch) > preferred_out) { p *= 1.1; }         \
+#define FOUNDCFG_PENALTY(in, out, p) {             \
+  _output_configs.insert (out);                    \
   if (p < penalty) {                               \
-    audio_out = (nch);                             \
+    audio_out = (out);                             \
+    if (imprecise) {                               \
+      imprecise->set (DataType::AUDIO, (in));      \
+      imprecise->set (DataType::MIDI,              \
+                      possible_midiin);            \
+    }                                              \
     penalty = p;                                   \
     found = true;                                  \
   }                                                \
 }
 
+#define FOUNDCFG_IMPRECISE(in, out) {              \
+  float p = fabsf ((float)(out) - preferred_out);  \
+  if (in != audio_in) {                            \
+    p += 1000;                                     \
+  }                                                \
+  if ((out) > preferred_out) { p *= 1.1; }         \
+  FOUNDCFG_PENALTY(in, out, p);                    \
+}
+
+#define FOUNDCFG(out)                              \
+  FOUNDCFG_IMPRECISE(audio_in, out)
+
 #define ANYTHINGGOES                               \
   _output_configs.insert (0);
 
@@ -408,6 +420,10 @@ LuaProc::can_support_io_configuration (const ChanCount& in, ChanCount& out, Chan
   }                                                \
 }
 
+       if (imprecise) {
+               *imprecise = in;
+       }
+
        for (luabridge::Iterator i (iotable); !i.isNil (); ++i) {
                luabridge::LuaRef io (i.value ());
                if (!io.isTable()) {
@@ -416,25 +432,30 @@ LuaProc::can_support_io_configuration (const ChanCount& in, ChanCount& out, Chan
 
                int possible_in = io["audio_in"].isNumber() ? io["audio_in"] : -1;
                int possible_out = io["audio_out"].isNumber() ? io["audio_out"] : -1;
+               int possible_midiin = _has_midi_input ? 1 : 0;
+
+               if (midi_in > 0 && possible_midiin == 0 && !imprecise) {
+                       continue;
+               }
 
                // exact match
                if ((possible_in == audio_in) && (possible_out == preferred_out)) {
-                       _output_configs.insert (preferred_out);
-                       audio_out = preferred_out;
                        /* Set penalty so low that this output configuration
                         * will trump any other one */
-                       penalty = -1;
-                       found = true;
+                       FOUNDCFG_PENALTY(audio_in, preferred_out, -1);
                }
 
                // "imprecise" matches
                if (possible_out == 0) {
-                       if (possible_in == 0) {
-                               if (_has_midi_output && audio_in == 0) {
-                                       // special case midi filters & generators
-                                       audio_out = 0;
-                                       found = true;
+                       /* skip configurations with no audio output, unless
+                        * the plugin is a midi filter or generator */
+                       if (possible_in == 0 && _has_midi_output) {
+                               if (audio_in == 0) {
+                                       FOUNDCFG(possible_out);
                                        break;
+                               } else if (imprecise) {
+                                       // TODO hide audio input from plugin
+                                       FOUNDCFG_IMPRECISE (possible_in, possible_out);
                                }
                        }
                        continue;
@@ -442,12 +463,9 @@ LuaProc::can_support_io_configuration (const ChanCount& in, ChanCount& out, Chan
 
                if (possible_in == 0) {
                        /* no inputs, generators & instruments */
-                       if (possible_out == -1) {
-                               /* any configuration possible, stereo output */
-                               FOUNDCFG (preferred_out);
-                               ANYTHINGGOES;
-                       } else if (possible_out == -2) {
-                               /* invalid, should be (0, -1) */
+                       if (possible_out == -1 || possible_out == -2) {
+                               /* any output configuration possible
+                                * out == -2 is invalid, interpreted as out == -1 */
                                FOUNDCFG (preferred_out);
                                ANYTHINGGOES;
                        } else if (possible_out < -2) {
@@ -460,19 +478,20 @@ LuaProc::can_support_io_configuration (const ChanCount& in, ChanCount& out, Chan
                        }
                }
 
-               if (possible_in == -1) {
+               if (possible_in == -1 || possible_in == -2) {
                        /* wildcard for input */
-                       if (possible_out == -1) {
-                               /* out must match in */
+                       if (possible_out == possible_in) {
+                               /* either both -1 or both -2 (invalid and
+                                * interpreted as both -1): out must match in */
                                FOUNDCFG (audio_in);
-                       } else if (possible_out == -2) {
-                               /* any configuration possible, pick matching */
+                       } else if (possible_out == -3 - possible_in) {
+                               /* one is -1, the other is -2: any output configuration
+                                * possible, pick what the insert prefers */
                                FOUNDCFG (preferred_out);
                                ANYTHINGGOES;
                        } else if (possible_out < -2) {
-                               /* explicitly variable number of outputs, pick maximum */
-                               FOUNDCFG (max (-possible_out, preferred_out));
-                               /* and try min, too, in case the penalty is lower */
+                               /* variable number of outputs up to -N,
+                                * invalid if in == -2 but we accept it anyway */
                                FOUNDCFG (min (-possible_out, preferred_out));
                                UPTO (-possible_out)
                        } else {
@@ -481,45 +500,20 @@ LuaProc::can_support_io_configuration (const ChanCount& in, ChanCount& out, Chan
                        }
                }
 
-               if (possible_in == -2) {
-                       if (possible_out == -1) {
-                               /* any configuration possible, pick matching */
-                               FOUNDCFG (preferred_out);
-                               ANYTHINGGOES;
-                       } else if (possible_out == -2) {
-                               /* invalid. interpret as (-1, -1) */
-                               FOUNDCFG (preferred_out);
-                               ANYTHINGGOES;
-                       } else if (possible_out < -2) {
-                               /* invalid,  interpret as (<-2, <-2)
-                                * variable number of outputs up to -N, */
-                               FOUNDCFG (min (-possible_out, preferred_out));
-                               UPTO (-possible_out)
-                       } else {
-                               /* exact number of outputs */
-                               FOUNDCFG (possible_out);
-                       }
-               }
-
-               if (possible_in < -2) {
-                       /* explicit variable number of inputs */
-                       if (audio_in > -possible_in && imprecise != NULL) {
-                               // hide inputs ports
-                               imprecise->set (DataType::AUDIO, -possible_in);
-                       }
-
-                       if (audio_in > -possible_in && imprecise == NULL) {
+               if (possible_in < -2 || possible_in > 0) {
+                       /* specified number, exact or up to */
+                       if (possible_in < -2 && audio_in > -possible_in && !imprecise) {
                                /* request is too large */
-                       } else if (possible_out == -1) {
-                               /* any output configuration possible */
-                               FOUNDCFG (preferred_out);
-                               ANYTHINGGOES;
-                       } else if (possible_out == -2) {
-                               /* invalid. interpret as (<-2, -1) */
+                       } else if (possible_in > 0 && audio_in != possible_in) {
+                               /* this configuration needed exacty possible_in inputs */
+                       } else if (possible_out == -1 || possible_out == -2) {
+                               /* any output configuration possible
+                                * out == -2 is invalid, interpreted as out == -1 */
                                FOUNDCFG (preferred_out);
                                ANYTHINGGOES;
                        } else if (possible_out < -2) {
-                               /* variable number of outputs up to -N, */
+                               /* variable number of outputs up to -N
+                                * not specified if in > 0, but we accept it anyway */
                                FOUNDCFG (min (-possible_out, preferred_out));
                                UPTO (-possible_out)
                        } else {
@@ -528,30 +522,6 @@ LuaProc::can_support_io_configuration (const ChanCount& in, ChanCount& out, Chan
                        }
                }
 
-               if (possible_in && (possible_in == audio_in)) {
-                       /* exact number of inputs ... must match obviously */
-                       if (possible_out == -1) {
-                               /* any output configuration possible */
-                               FOUNDCFG (preferred_out);
-                               ANYTHINGGOES;
-                       } else if (possible_out == -2) {
-                               /* invalid. interpret as (>0, -1) */
-                               FOUNDCFG (preferred_out);
-                               ANYTHINGGOES;
-                       } else if (possible_out < -2) {
-                               /* > 0, < -2 is not specified
-                                * interpret as up to -N */
-                               FOUNDCFG (min (-possible_out, preferred_out));
-                               UPTO (-possible_out)
-                       } else {
-                               /* exact number of outputs */
-                               FOUNDCFG (possible_out);
-                       }
-               }
-       }
-
-       if (found && imprecise) {
-               *imprecise = in;
        }
 
        if (!found && imprecise) {
@@ -564,27 +534,19 @@ LuaProc::can_support_io_configuration (const ChanCount& in, ChanCount& out, Chan
 
                        int possible_in = io["audio_in"].isNumber() ? io["audio_in"] : -1;
                        int possible_out = io["audio_out"].isNumber() ? io["audio_out"] : -1;
+                       int possible_midiin = _has_midi_input ? 1 : 0;
 
-                       if (possible_out == 0 && possible_in == 0 && _has_midi_output) {
-                               assert (audio_in > 0); // no input is handled above
-                               // TODO hide audio input from plugin
-                               imprecise->set (DataType::AUDIO, 0);
-                               audio_out = 0;
-                               found = true;
-                               continue;
-                       }
 
                        assert (possible_in > 0); // all other cases will have been matched above
 
-                       imprecise->set (DataType::AUDIO, possible_in);
                        if (possible_out == -1 || possible_out == -2) {
-                               FOUNDCFG (2);
+                               FOUNDCFG_IMPRECISE (possible_in, 2);
                        } else if (possible_out < -2) {
                                /* explicitly variable number of outputs, pick maximum */
-                               FOUNDCFG (min (-possible_out, preferred_out));
+                               FOUNDCFG_IMPRECISE (possible_in, min (-possible_out, preferred_out));
                        } else {
                                /* exact number of outputs */
-                               FOUNDCFG (possible_out);
+                               FOUNDCFG_IMPRECISE (possible_in, possible_out);
                        }
                        // ideally we'll also find the closest, best matching
                        // input configuration with minimal output penalty...
@@ -596,7 +558,6 @@ LuaProc::can_support_io_configuration (const ChanCount& in, ChanCount& out, Chan
        }
 
        if (imprecise) {
-               imprecise->set (DataType::MIDI, _has_midi_input ? 1 : 0);
                _selected_in = *imprecise;
        } else {
                _selected_in = in;