Generic scale points API.
authorDavid Robillard <d@drobilla.net>
Sat, 14 May 2011 18:43:34 +0000 (18:43 +0000)
committerDavid Robillard <d@drobilla.net>
Sat, 14 May 2011 18:43:34 +0000 (18:43 +0000)
Hide scale points implementation inside specific Plugin subclass.
Don't needlessley/slowly get scale points twice for each port while building UI.
Remove dependence on specific plugin types from GenericPluginUI.

git-svn-id: svn://localhost/ardour2/branches/3.0@9511 d708f5d6-7413-0410-9779-e7cbd77b26cf

gtk2_ardour/generic_pluginui.cc
gtk2_ardour/plugin_ui.h
libs/ardour/ardour/ladspa_plugin.h
libs/ardour/ardour/lv2_plugin.h
libs/ardour/ardour/plugin.h
libs/ardour/ladspa_plugin.cc
libs/ardour/lv2_plugin.cc

index d1eddcfa68975b78cecf31d2a5046ed54b05114f..3ed935275551e65b2e059b9f566d7a2ed3d277c4 100644 (file)
 
 #include "ardour/plugin.h"
 #include "ardour/plugin_insert.h"
-#include "ardour/ladspa_plugin.h"
-#ifdef HAVE_SLV2
-#include "ardour/lv2_plugin.h"
-#endif
 #include "ardour/session.h"
 
 #include <lrdf.h>
@@ -325,7 +321,7 @@ GenericPluginUI::ControlUI::ControlUI ()
           below). be sure to include a descender.
        */
 
-        set_size_request_to_display_given_text (automate_button, _("Mgnual"), 15, 10);
+       set_size_request_to_display_given_text (automate_button, _("Mgnual"), 15, 10);
 
        ignore_change = 0;
        display = 0;
@@ -404,55 +400,35 @@ GenericPluginUI::build_control_ui (guint32 port_index, boost::shared_ptr<Automat
 
        Gtk::Requisition req (control_ui->automate_button.size_request());
 
-       if (plugin->parameter_is_input (port_index)) {
-
-               boost::shared_ptr<LadspaPlugin> lp;
-#ifdef HAVE_SLV2
-               boost::shared_ptr<LV2Plugin> lv2p;
-#endif
-               if ((lp = boost::dynamic_pointer_cast<LadspaPlugin>(plugin)) != 0) {
-
-                       // FIXME: not all plugins have a numeric unique ID
-                       uint32_t id = atol (lp->unique_id().c_str());
-                       lrdf_defaults* defaults = lrdf_get_scale_values(id, port_index);
+       if (plugin->parameter_is_input(port_index)) {
 
-                       if (defaults && defaults->count > 0)    {
+               /* Build a combo box */
 
-                               control_ui->combo = new Gtk::ComboBoxText;
-                               //control_ui->combo->set_value_in_list(true, false);
-                               set_popdown_strings (*control_ui->combo, setup_scale_values(port_index, control_ui));
-                               control_ui->combo->signal_changed().connect (sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::control_combo_changed), control_ui));
-                               mcontrol->Changed.connect (control_connections, invalidator (*this), boost::bind (&GenericPluginUI::ui_parameter_changed, this, control_ui), gui_context());
-                               control_ui->pack_start(control_ui->label, true, true);
-                               control_ui->pack_start(*control_ui->combo, false, true);
+               boost::shared_ptr<ARDOUR::Plugin::ScalePoints> points
+                       = plugin->get_scale_points(port_index);
 
-                               update_control_display(control_ui);
-
-                               lrdf_free_setting_values(defaults);
-                               return control_ui;
+               if (points) {
+                       std::vector<std::string> labels;
+                       for (ARDOUR::Plugin::ScalePoints::const_iterator i = points->begin();
+                            i != points->end(); ++i) {
+                               labels.push_back(i->first);
                        }
 
-#ifdef HAVE_SLV2
-               } else if ((lv2p = boost::dynamic_pointer_cast<LV2Plugin>(plugin)) != 0) {
-
-                       SLV2Port port = lv2p->slv2_port(port_index);
-                       SLV2ScalePoints points = slv2_port_get_scale_points(lv2p->slv2_plugin(), port);
-
-                       if (points) {
-                               control_ui->combo = new Gtk::ComboBoxText;
-                               //control_ui->combo->set_value_in_list(true, false);
-                               set_popdown_strings (*control_ui->combo, setup_scale_values(port_index, control_ui));
-                               control_ui->combo->signal_changed().connect (sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::control_combo_changed), control_ui));
-                               mcontrol->Changed.connect (control_connections, invalidator (*this), boost::bind (&GenericPluginUI::ui_parameter_changed, this, control_ui), gui_context());
-                               control_ui->pack_start(control_ui->label, true, true);
-                               control_ui->pack_start(*control_ui->combo, false, true);
+                       control_ui->combo = new Gtk::ComboBoxText();
+                       set_popdown_strings(*control_ui->combo, labels);
+                       control_ui->combo->signal_changed().connect(
+                               sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::control_combo_changed),
+                                           control_ui));
+                       mcontrol->Changed.connect(control_connections, invalidator(*this),
+                                                 boost::bind(&GenericPluginUI::ui_parameter_changed,
+                                                             this, control_ui),
+                                                 gui_context());
+                       control_ui->pack_start(control_ui->label, true, true);
+                       control_ui->pack_start(*control_ui->combo, false, true);
 
-                               update_control_display(control_ui);
+                       update_control_display(control_ui);
 
-                               slv2_scale_points_free(points);
-                               return control_ui;
-                       }
-#endif
+                       return control_ui;
                }
 
                if (desc.toggled) {
@@ -471,13 +447,13 @@ GenericPluginUI::build_control_ui (guint32 port_index, boost::shared_ptr<Automat
                        control_ui->automate_button.signal_clicked().connect (bind (mem_fun(*this, &GenericPluginUI::astate_clicked), control_ui, (uint32_t) port_index));
 
                        mcontrol->Changed.connect (control_connections, invalidator (*this), boost::bind (&GenericPluginUI::toggle_parameter_changed, this, control_ui), gui_context());
-                        mcontrol->alist()->automation_state_changed.connect (control_connections, invalidator (*this), boost::bind (&GenericPluginUI::automation_state_changed, this, control_ui), gui_context());
+                       mcontrol->alist()->automation_state_changed.connect (control_connections, invalidator (*this), boost::bind (&GenericPluginUI::automation_state_changed, this, control_ui), gui_context());
        
                        if (plugin->get_parameter (port_index) > 0.5){
                                control_ui->button->set_active(true);
                        }
 
-                        automation_state_changed (control_ui);
+                       automation_state_changed (control_ui);
 
                        return control_ui;
                }
@@ -492,7 +468,7 @@ GenericPluginUI::build_control_ui (guint32 port_index, boost::shared_ptr<Automat
                   the absence of bounds in any sensible fashion.
                */
 
-                Adjustment* adj = control_ui->controller->adjustment();
+               Adjustment* adj = control_ui->controller->adjustment();
                boost::shared_ptr<PluginInsert::PluginControl> pc = boost::dynamic_pointer_cast<PluginInsert::PluginControl> (control_ui->control);
 
                adj->set_lower (pc->user_to_ui (desc.lower));
@@ -512,7 +488,7 @@ GenericPluginUI::build_control_ui (guint32 port_index, boost::shared_ptr<Automat
                        control_ui->controller->set_name (X_("PluginSlider"));
                        control_ui->controller->set_style (BarController::LeftToRight);
                        control_ui->controller->set_use_parent (true);
-                        control_ui->controller->set_logarithmic (desc.logarithmic);
+                       control_ui->controller->set_logarithmic (desc.logarithmic);
 
                        control_ui->controller->StartGesture.connect (sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::start_touch), control_ui));
                        control_ui->controller->StopGesture.connect (sigc::bind (sigc::mem_fun(*this, &GenericPluginUI::stop_touch), control_ui));
@@ -675,7 +651,7 @@ GenericPluginUI::update_control_display (ControlUI* cui)
        cui->ignore_change++;
 
        if (cui->combo) {
-               std::map<string,float>::iterator it;
+               std::map<string,float>::iterator it;
                for (it = cui->combo_map->begin(); it != cui->combo_map->end(); ++it) {
                        if (it->second == val) {
                                cui->combo->set_active_text(it->first);
@@ -794,60 +770,4 @@ GenericPluginUI::output_update ()
        }
 }
 
-vector<string>
-GenericPluginUI::setup_scale_values(guint32 port_index, ControlUI* cui)
-{
-       vector<string> enums;
-       boost::shared_ptr<LadspaPlugin> lp;
-#ifdef HAVE_SLV2
-       boost::shared_ptr<LV2Plugin> lv2p;
-#endif
-
-       if ((lp = boost::dynamic_pointer_cast<LadspaPlugin>(plugin)) != 0) {
-               // all LADPSA plugins have a numeric unique ID
-               uint32_t id = atol (lp->unique_id().c_str());
-
-               cui->combo_map = new std::map<string, float>;
-               lrdf_defaults* defaults = lrdf_get_scale_values(id, port_index);
-               if (defaults)   {
-                       for (uint32_t i = 0; i < defaults->count; ++i) {
-                               enums.push_back(defaults->items[i].label);
-                               pair<string, float> newpair;
-                               newpair.first = defaults->items[i].label;
-                               newpair.second = defaults->items[i].value;
-                               cui->combo_map->insert(newpair);
-                       }
-
-                       lrdf_free_setting_values(defaults);
-               }
-
-#ifdef HAVE_SLV2
-       } else if ((lv2p = boost::dynamic_pointer_cast<LV2Plugin>(plugin)) != 0) {
-
-               SLV2Port port = lv2p->slv2_port(port_index);
-               SLV2ScalePoints points = slv2_port_get_scale_points(lv2p->slv2_plugin(), port);
-               cui->combo_map = new std::map<string, float>;
-
-               for (unsigned i=0; i < slv2_scale_points_size(points); ++i) {
-                       SLV2ScalePoint p = slv2_scale_points_get_at(points, i);
-                       SLV2Value label = slv2_scale_point_get_label(p);
-                       SLV2Value value = slv2_scale_point_get_value(p);
-                       if (label && (slv2_value_is_float(value) || slv2_value_is_int(value))) {
-                               enums.push_back(slv2_value_as_string(label));
-                               pair<string, float> newpair;
-                               newpair.first = slv2_value_as_string(label);
-                               newpair.second = slv2_value_as_float(value);
-                               cui->combo_map->insert(newpair);
-                       }
-               }
-
-               slv2_scale_points_free(points);
-#endif
-       }
-
-
-       return enums;
-}
-
-
 
index 52e7b8fb6063e2e7375f410126e1a2825dfd2ea5..3176e4a14d4f89a190406541d526618aec71a89c 100644 (file)
@@ -241,7 +241,6 @@ class GenericPluginUI : public PlugUIBase, public Gtk::VBox
 
        void build ();
        ControlUI* build_control_ui (guint32 port_index, boost::shared_ptr<ARDOUR::AutomationControl>);
-       std::vector<std::string> setup_scale_values(guint32 port_index, ControlUI* cui);
        void ui_parameter_changed (ControlUI* cui);
        void toggle_parameter_changed (ControlUI* cui);
        void update_control_display (ControlUI* cui);
index 457921cf36715baa0e812fc6989fd3cac6fa80d1..c49e9d3a213c181d259d8e974a822adfb10a688c 100644 (file)
@@ -97,7 +97,10 @@ class LadspaPlugin : public ARDOUR::Plugin
        bool parameter_is_output(uint32_t) const;
        bool parameter_is_toggled(uint32_t) const;
 
-       int      set_state (const XMLNode&, int version);
+       boost::shared_ptr<Plugin::ScalePoints>
+       get_scale_points(uint32_t port_index) const;
+
+       int set_state (const XMLNode&, int version);
 
        bool load_preset (PresetRecord);
 
index 91dc2f61a58aa9317300595e89c91d5af57bb56a..ff66ec286810c226f488e020aa403985d2a04c11 100644 (file)
@@ -108,6 +108,9 @@ class LV2Plugin : public ARDOUR::Plugin
        bool parameter_is_output (uint32_t) const;
        bool parameter_is_toggled (uint32_t) const;
 
+       boost::shared_ptr<Plugin::ScalePoints>
+       get_scale_points(uint32_t port_index) const;
+               
        static uint32_t midi_event_type () { return _midi_event_type; }
 
        void set_insert_info(const PluginInsert* insert);
index d8f33c21f45ad5a54018a2c32788d57c70c1c348..bc796712af6bf12de8ad784d2bae827a258e49e7 100644 (file)
@@ -134,6 +134,13 @@ class Plugin : public PBD::StatefulDestructible, public Latent
        virtual bool parameter_is_input(uint32_t) const = 0;
        virtual bool parameter_is_output(uint32_t) const = 0;
 
+       typedef std::map<const std::string, const float> ScalePoints;
+
+       virtual boost::shared_ptr<ScalePoints>
+       get_scale_points(uint32_t port_index) const {
+               return boost::shared_ptr<ScalePoints>();
+       }
+
        void realtime_handle_transport_stopped ();
 
        struct PresetRecord {
index 1ea158d7cdeecd91f08c991be3e9a975e011fb81..f5426500a47d85524b8ce106370780c194c7e47c 100644 (file)
@@ -605,6 +605,28 @@ LadspaPlugin::print_parameter (uint32_t param, char *buf, uint32_t len) const
        }
 }
 
+boost::shared_ptr<Plugin::ScalePoints>
+LadspaPlugin::get_scale_points(uint32_t port_index) const
+{
+       const uint32_t id     = atol(unique_id().c_str());
+       lrdf_defaults* points = lrdf_get_scale_values(id, port_index);
+
+       boost::shared_ptr<Plugin::ScalePoints> ret;
+       if (!points) {
+               return ret;
+       }
+
+       ret = boost::shared_ptr<Plugin::ScalePoints>(new ScalePoints());
+
+       for (uint32_t i = 0; i < points->count; ++i) {
+               ret->insert(make_pair(points->items[i].label,
+                                     points->items[i].value));
+       }
+
+       lrdf_free_setting_values(points);
+       return ret;
+}
+
 void
 LadspaPlugin::run_in_place (pframes_t nframes)
 {
index e7c9dad4ecbc4fcef32f7ba5ae4e98cc6852e96d..6c1664bc01886e548943e2510c87a79538008d31 100644 (file)
@@ -1027,6 +1027,33 @@ LV2Plugin::print_parameter(uint32_t param, char* buf, uint32_t len) const
        }
 }
 
+boost::shared_ptr<Plugin::ScalePoints>
+LV2Plugin::get_scale_points(uint32_t port_index) const
+{
+       SLV2Port        port   = slv2_plugin_get_port_by_index(_plugin, port_index);
+       SLV2ScalePoints points = slv2_port_get_scale_points(_plugin, port);
+
+       boost::shared_ptr<Plugin::ScalePoints> ret;
+       if (!points) {
+               return ret;
+       }
+
+       ret = boost::shared_ptr<Plugin::ScalePoints>(new ScalePoints());
+
+       for (unsigned i = 0; i < slv2_scale_points_size(points); ++i) {
+               SLV2ScalePoint p     = slv2_scale_points_get_at(points, i);
+               SLV2Value      label = slv2_scale_point_get_label(p);
+               SLV2Value      value = slv2_scale_point_get_value(p);
+               if (label && (slv2_value_is_float(value) || slv2_value_is_int(value))) {
+                       ret->insert(make_pair(slv2_value_as_string(label),
+                                             slv2_value_as_float(value)));
+               }
+       }
+
+       slv2_scale_points_free(points);
+       return ret;
+}
+
 void
 LV2Plugin::run(pframes_t nframes)
 {