+DataType
+MixerStrip::guess_main_type(bool for_input, bool favor_connected) const
+{
+ /* The heuristic follows these principles:
+ * A) If all ports that the user connected are of the same type, then he
+ * very probably intends to use the IO with that type. A common subcase
+ * is when the IO has only ports of the same type (connected or not).
+ * B) If several types of ports are connected, then we should guess based
+ * on the likeliness of the user wanting to use a given type.
+ * We assume that the DataTypes are ordered from the most likely to the
+ * least likely when iterating or comparing them with "<".
+ * C) If no port is connected, the same logic can be applied with all ports
+ * instead of connected ones. TODO: Try other ideas, for instance look at
+ * the last plugin output when |for_input| is false (note: when StrictIO
+ * the outs of the last plugin should be the same as the outs of the route
+ * modulo the panner which forwards non-audio anyway).
+ * All of these constraints are respected by the following algorithm that
+ * just returns the most likely datatype found in connected ports if any, or
+ * available ports if any (since if all ports are of the same type, the most
+ * likely found will be that one obviously). */
+
+ boost::shared_ptr<IO> io = for_input ? _route->input() : _route->output();
+
+ /* Find most likely type among connected ports */
+ if (favor_connected) {
+ DataType type = DataType::NIL; /* NIL is always last so least likely */
+ for (PortSet::iterator p = io->ports().begin(); p != io->ports().end(); ++p) {
+ if (p->connected() && p->type() < type)
+ type = p->type();
+ }
+ if (type != DataType::NIL) {
+ /* There has been a connected port (necessarily non-NIL) */
+ return type;
+ }
+ }
+
+ /* Find most likely type among available ports.
+ * The iterator stops before NIL. */
+ for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) {
+ if (io->n_ports().n(*t) > 0)
+ return *t;
+ }
+
+ /* No port at all, return the most likely datatype by default */
+ return DataType::front();
+}
+