Normalize notes on all channels.
[ardour.git] / libs / ardour / plugin_insert.cc
index 5279a3696216e3ac216ed11e57d8adb5a6b80168..599a929deaa3dcd62304ad96b9683392d69de629 100644 (file)
@@ -240,23 +240,27 @@ PluginInsert::create_automatable_parameters ()
 
        set<Evoral::Parameter> a = _plugins.front()->automatable ();
 
-       Plugin::ParameterDescriptor desc;
-
        for (set<Evoral::Parameter>::iterator i = a.begin(); i != a.end(); ++i) {
                if (i->type() == PluginAutomation) {
 
                        Evoral::Parameter param(*i);
 
+                       ParameterDescriptor desc;
                        _plugins.front()->get_parameter_descriptor(i->id(), desc);
 
-                       /* the Parameter belonging to the actual plugin doesn't have its range set
-                          but we want the Controllable related to this Parameter to have those limits.
-                       */
-
-                       param.set_range (desc.lower, desc.upper, _plugins.front()->default_value(i->id()), desc.toggled);
                        can_automate (param);
-                       boost::shared_ptr<AutomationList> list(new AutomationList(param));
-                       add_control (boost::shared_ptr<AutomationControl> (new PluginControl(this, param, list)));
+                       boost::shared_ptr<AutomationList> list(new AutomationList(param, desc));
+                       add_control (boost::shared_ptr<AutomationControl> (new PluginControl(this, param, desc, list)));
+               } else if (i->type() == PluginPropertyAutomation) {
+                       Evoral::Parameter param(*i);
+                       const ParameterDescriptor& desc = _plugins.front()->get_property_descriptor(param.id());
+                       if (desc.datatype != Variant::NOTHING) {
+                               boost::shared_ptr<AutomationList> list;
+                               if (Variant::type_is_numeric(desc.datatype)) {
+                                       list = boost::shared_ptr<AutomationList>(new AutomationList(param, desc));
+                               }
+                               add_control (boost::shared_ptr<AutomationControl> (new PluginPropertyControl(this, param, desc, list)));
+                       }
                }
        }
 }
@@ -368,7 +372,7 @@ PluginInsert::connect_and_run (BufferSet& bufs, pframes_t nframes, framecnt_t of
                        boost::shared_ptr<AutomationControl> c
                                = boost::dynamic_pointer_cast<AutomationControl>(li->second);
 
-                       if (c->parameter().type() == PluginAutomation && c->automation_playback()) {
+                       if (c->list() && c->automation_playback()) {
                                bool valid;
 
                                const float val = c->list()->rt_safe_eval (now, valid);
@@ -591,7 +595,7 @@ PluginInsert::default_parameter_value (const Evoral::Parameter& param)
        if (_plugins.empty()) {
                fatal << _("programming error: ") << X_("PluginInsert::default_parameter_value() called with no plugin")
                      << endmsg;
-               /*NOTREACHED*/
+               abort(); /*NOTREACHED*/
        }
 
        return _plugins[0]->default_value (param.id());
@@ -637,7 +641,7 @@ PluginInsert::plugin_factory (boost::shared_ptr<Plugin> other)
        fatal << string_compose (_("programming error: %1"),
                          X_("unknown plugin type in PluginInsert::plugin_factory"))
              << endmsg;
-       /*NOTREACHED*/
+       abort(); /*NOTREACHED*/
        return boost::shared_ptr<Plugin> ((Plugin*) 0);
 }
 
@@ -1042,8 +1046,6 @@ PluginInsert::set_state(const XMLNode& node, int version)
 
                if ((*niter)->name() == plugin->state_node_name()) {
 
-                       plugin->set_state (**niter, version);
-
                        for (Plugins::iterator i = _plugins.begin(); i != _plugins.end(); ++i) {
                                (*i)->set_state (**niter, version);
                        }
@@ -1139,7 +1141,7 @@ PluginInsert::set_parameter_state_2X (const XMLNode& node, int version)
                                        float min_y = c->alist()->get_min_y ();
                                        float max_y = c->alist()->get_max_y ();
 
-                                       Plugin::ParameterDescriptor desc;
+                                       ParameterDescriptor desc;
                                        _plugins.front()->get_parameter_descriptor (port_id, desc);
 
                                        if (min_y == FLT_MIN) {
@@ -1167,11 +1169,15 @@ PluginInsert::set_parameter_state_2X (const XMLNode& node, int version)
 string
 PluginInsert::describe_parameter (Evoral::Parameter param)
 {
-       if (param.type() != PluginAutomation) {
-               return Automatable::describe_parameter(param);
+       if (param.type() == PluginAutomation) {
+               return _plugins[0]->describe_parameter (param);
+       } else if (param.type() == PluginPropertyAutomation) {
+               boost::shared_ptr<AutomationControl> c(automation_control(param));
+               if (c && !c->desc().label.empty()) {
+                       return c->desc().label;
+               }
        }
-
-       return _plugins[0]->describe_parameter (param);
+       return Automatable::describe_parameter(param);
 }
 
 ARDOUR::framecnt_t
@@ -1190,19 +1196,19 @@ PluginInsert::type ()
        return plugin()->get_info()->type;
 }
 
-PluginInsert::PluginControl::PluginControl (PluginInsert* p, const Evoral::Parameter &param, boost::shared_ptr<AutomationList> list)
-       : AutomationControl (p->session(), param, list, p->describe_parameter(param))
+PluginInsert::PluginControl::PluginControl (PluginInsert*                     p,
+                                            const Evoral::Parameter&          param,
+                                            const ParameterDescriptor&        desc,
+                                            boost::shared_ptr<AutomationList> list)
+       : AutomationControl (p->session(), param, desc, list, p->describe_parameter(param))
        , _plugin (p)
 {
-       Plugin::ParameterDescriptor desc;
-       boost::shared_ptr<Plugin> plugin = p->plugin (0);
-       
-       alist()->reset_default (plugin->default_value (param.id()));
-
-       plugin->get_parameter_descriptor (param.id(), desc);
-       _logarithmic = desc.logarithmic;
-       _sr_dependent = desc.sr_dependent;
-       _toggled = desc.toggled;
+       if (alist()) {
+               alist()->reset_default (desc.normal);
+               if (desc.toggled) {
+                       list->set_interpolation(Evoral::ControlList::Discrete);
+               }
+       }
 
        if (desc.toggled) {
                set_flags(Controllable::Toggle);
@@ -1230,24 +1236,13 @@ PluginInsert::PluginControl::set_value (double user_val)
 double
 PluginInsert::PluginControl::internal_to_interface (double val) const
 {
-       if (_logarithmic) {
-               /* some plugins have a log-scale range "0.."
-                * ideally we'd map the range down to infinity somehow :)
-                *
-                * one solution could be to use
-                *   val = exp(lower + log(range) * value);
-                *   (log(val) - lower) / range)
-                * This approach would require access to the actual range (ie
-                * Plugin::ParameterDescriptor) and also require handling
-                * of unbound ranges..
-                *
-                * currently an arbitrarly low number is assumed to represnt
-                * log(0) as hot-fix solution.
-                */
+       val = Controllable::internal_to_interface(val);
+       
+       if (_desc.logarithmic) {
                if (val > 0) {
-                       val = log (val);
+                       val = pow (val, 1/1.5);
                } else {
-                       val = -8; // ~ -70dB = 20 * log10(exp(-8))
+                       val = 0;
                }
        }
 
@@ -1257,15 +1252,16 @@ PluginInsert::PluginControl::internal_to_interface (double val) const
 double
 PluginInsert::PluginControl::interface_to_internal (double val) const
 {
-       if (_logarithmic) {
-               if (val <= -8) {
-                       /* see note in PluginInsert::PluginControl::internal_to_interface() */
-                       val= 0;
+       if (_desc.logarithmic) {
+               if (val <= 0) {
+                       val = 0;
                } else {
-                       val = exp (val);
+                       val = pow (val, 1.5);
                }
        }
 
+       val = Controllable::interface_to_internal(val);
+       
        return val;
 }
 
@@ -1289,6 +1285,62 @@ PluginInsert::PluginControl::get_value () const
        return _plugin->get_parameter (_list->parameter());
 }
 
+PluginInsert::PluginPropertyControl::PluginPropertyControl (PluginInsert*                     p,
+                                                            const Evoral::Parameter&          param,
+                                                            const ParameterDescriptor&        desc,
+                                                            boost::shared_ptr<AutomationList> list)
+       : AutomationControl (p->session(), param, desc, list)
+       , _plugin (p)
+{
+       if (alist()) {
+               alist()->set_yrange (desc.lower, desc.upper);
+               alist()->reset_default (desc.normal);
+       }
+
+       if (desc.toggled) {
+               set_flags(Controllable::Toggle);
+       }
+}
+
+void
+PluginInsert::PluginPropertyControl::set_value (double user_val)
+{
+       /* Old numeric set_value(), coerce to appropriate datatype if possible.
+          This is lossy, but better than nothing until Ardour's automation system
+          can handle various datatypes all the way down. */
+       const Variant value(_desc.datatype, user_val);
+       if (value.type() == Variant::NOTHING) {
+               error << "set_value(double) called for non-numeric property" << endmsg;
+               return;
+       }
+
+       for (Plugins::iterator i = _plugin->_plugins.begin(); i != _plugin->_plugins.end(); ++i) {
+               (*i)->set_property(_list->parameter().id(), value);
+       }
+
+       _value = value;
+       AutomationControl::set_value(user_val);
+}
+
+XMLNode&
+PluginInsert::PluginPropertyControl::get_state ()
+{
+       stringstream ss;
+
+       XMLNode& node (AutomationControl::get_state());
+       ss << parameter().id();
+       node.add_property (X_("property"), ss.str());
+       node.remove_property (X_("value"));
+
+       return node;
+}
+
+double
+PluginInsert::PluginPropertyControl::get_value () const
+{
+       return _value.to_double();
+}
+
 boost::shared_ptr<Plugin>
 PluginInsert::get_impulse_analysis_plugin()
 {
@@ -1319,7 +1371,7 @@ PluginInsert::collect_signal_for_analysis (framecnt_t nframes)
 void
 PluginInsert::add_plugin (boost::shared_ptr<Plugin> plugin)
 {
-       plugin->set_insert_info (this);
+       plugin->set_insert_id (this->id());
        
        if (_plugins.empty()) {
                 /* first (and probably only) plugin instance - connect to relevant signals