1f7ba8d4c082c225c0af691eed9706c7bb02016d
[ardour.git] / gtk2_ardour / ladspa_pluginui.cc
1 /*
2     Copyright (C) 2000 Paul Davis 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <climits>
21 #include <cerrno>
22 #include <cmath>
23 #include <string>
24
25 #include <pbd/stl_delete.h>
26 #include <pbd/xml++.h>
27 #include <pbd/failed_constructor.h>
28
29 #include <gtkmm2ext/click_box.h>
30 #include <gtkmm2ext/fastmeter.h>
31 #include <gtkmm2ext/barcontroller.h>
32 #include <gtkmm2ext/utils.h>
33 #include <gtkmm2ext/doi.h>
34 #include <gtkmm2ext/slider_controller.h>
35
36 #include <midi++/manager.h>
37
38 #include <ardour/plugin.h>
39 #include <ardour/insert.h>
40 #include <ardour/ladspa_plugin.h>
41
42 #include <lrdf.h>
43
44 #include "ardour_ui.h"
45 #include "prompter.h"
46 #include "plugin_ui.h"
47 #include "utils.h"
48 #include "gui_thread.h"
49
50 #include "i18n.h"
51
52 using namespace std;
53 using namespace ARDOUR;
54 using namespace PBD;
55 using namespace Gtkmm2ext;
56 using namespace Gtk;
57 using namespace sigc;
58
59 LadspaPluginUI::LadspaPluginUI (boost::shared_ptr<PluginInsert> pi, bool scrollable)
60         : PlugUIBase (pi),
61           button_table (initial_button_rows, initial_button_cols),
62           output_table (initial_output_rows, initial_output_cols),
63           hAdjustment(0.0, 0.0, 0.0),
64           vAdjustment(0.0, 0.0, 0.0),
65           scroller_view(hAdjustment, vAdjustment),
66           automation_menu (0),
67           is_scrollable(scrollable)
68 {
69         set_name ("PluginEditor");
70         set_border_width (10);
71         set_homogeneous (false);
72
73         settings_box.set_homogeneous (false);
74
75         HBox* constraint_hbox = manage (new HBox);
76         HBox* smaller_hbox = manage (new HBox);
77         Label* combo_label = manage (new Label (_("<span size=\"large\">Presets</span>")));
78         combo_label->set_use_markup (true);
79
80         smaller_hbox->pack_start (*combo_label, false, false, 10);
81         smaller_hbox->pack_start (combo, false, false);
82         smaller_hbox->pack_start (save_button, false, false);
83
84         constraint_hbox->set_spacing (5);
85         constraint_hbox->pack_start (*smaller_hbox, true, false);
86         constraint_hbox->pack_end (bypass_button, false, false);
87
88         settings_box.pack_end (*constraint_hbox, false, false);
89
90         pack_start (settings_box, false, false);
91
92         if ( is_scrollable ) {
93                 scroller.set_policy (Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
94                 scroller.set_name ("PluginEditor");
95                 scroller_view.set_name("PluginEditor");
96                 scroller_view.add (hpacker);
97                 scroller.add (scroller_view);
98                 
99                 pack_start (scroller, true, true);
100
101         }
102         else {
103                 pack_start (hpacker, false, false);
104         }
105
106         insert->active_changed.connect (mem_fun(*this, &LadspaPluginUI::redirect_active_changed));
107         bypass_button.set_active (!insert->active());
108         
109         build ();
110 }
111
112 LadspaPluginUI::~LadspaPluginUI ()
113 {
114         if (output_controls.size() > 0) {
115                 screen_update_connection.disconnect();
116         }
117 }
118
119 void
120 LadspaPluginUI::build ()
121
122 {
123         guint32 i = 0;
124         guint32 x = 0;
125         Frame* frame;
126         Frame* bt_frame;
127         VBox* box;
128         int output_row, output_col;
129         int button_row, button_col;
130         int output_rows, output_cols;
131         int button_rows, button_cols;
132
133         prefheight = 30;
134         hpacker.set_spacing (10);
135
136         output_rows = initial_output_rows;
137         output_cols = initial_output_cols;
138         button_rows = initial_button_rows;
139         button_cols = initial_button_cols;
140         output_row = 0;
141         button_row = 0;
142         output_col = 0;
143         button_col = 0;
144
145         button_table.set_homogeneous (false);
146         button_table.set_row_spacings (2);
147         button_table.set_col_spacings (2);
148         output_table.set_homogeneous (true);
149         output_table.set_row_spacings (2);
150         output_table.set_col_spacings (2);
151         button_table.set_border_width (5);
152         output_table.set_border_width (5);
153
154         hpacker.set_border_width (10);
155
156         bt_frame = manage (new Frame);
157         bt_frame->set_name ("BaseFrame");
158         bt_frame->add (button_table);
159         hpacker.pack_start(*bt_frame, true, true);
160
161         box = manage (new VBox);
162         box->set_border_width (5);
163         box->set_spacing (1);
164
165         frame = manage (new Frame);
166         frame->set_name ("BaseFrame");
167         frame->set_label (_("Controls"));
168         frame->add (*box);
169         hpacker.pack_start(*frame, true, true);
170
171         /* find all ports. build control elements for all appropriate control ports */
172
173         for (i = 0; i < plugin->parameter_count(); ++i) {
174
175                 if (plugin->parameter_is_control (i)) {
176                         
177                         /* Don't show latency control ports */
178
179                         if (plugin->describe_parameter (i) == X_("latency")) {
180                                 continue;
181                         }
182
183                         ControlUI* cui;
184         
185                         /* if we are scrollable, just use one long column */
186
187                         if (!is_scrollable) {
188                                 if (x++ > 7){
189                                         frame = manage (new Frame);
190                                         frame->set_name ("BaseFrame");
191                                         box = manage (new VBox);
192                                         
193                                         box->set_border_width (5);
194                                         box->set_spacing (1);
195
196                                         frame->add (*box);
197                                         hpacker.pack_start(*frame,true,true);
198
199                                         x = 1;
200                                 }
201                         }
202
203                         if ((cui = build_control_ui (i, plugin->get_nth_control (i))) == 0) {
204                                 error << string_compose(_("Plugin Editor: could not build control element for port %1"), i) << endmsg;
205                                 continue;
206                         }
207                                 
208                         if (cui->control || cui->clickbox || cui->combo) {
209
210                                 box->pack_start (*cui, false, false);
211
212                         } else if (cui->button) {
213
214                                 if (button_row == button_rows) {
215                                         button_row = 0;
216                                         if (++button_col == button_cols) {
217                                                 button_cols += 2;
218                                                 button_table.resize (button_rows, button_cols);
219                                         }
220                                 }
221
222                                 button_table.attach (*cui, button_col, button_col + 1, button_row, button_row+1, 
223                                                      FILL|EXPAND, FILL);
224                                 button_row++;
225
226                         } else if (cui->display) {
227
228                                 output_table.attach (*cui, output_col, output_col + 1, output_row, output_row+1, 
229                                                      FILL|EXPAND, FILL);
230                                 
231                                 // TODO: The meters should be divided into multiple rows 
232                                 
233                                 if (++output_col == output_cols) {
234                                         output_cols ++;
235                                         output_table.resize (output_rows, output_cols);
236                                 }
237                                 
238                                 /* old code, which divides meters into
239                                  * columns first, rows later. New code divides into one row
240                                  
241                                 if (output_row == output_rows) {
242                                         output_row = 0;
243                                         if (++output_col == output_cols) {
244                                                 output_cols += 2;
245                                                 output_table.resize (output_rows, output_cols);
246                                         }
247                                 }
248                                 
249                                 output_table.attach (*cui, output_col, output_col + 1, output_row, output_row+1, 
250                                                      FILL|EXPAND, FILL);
251  
252                                 output_row++;
253                                 */
254                         }
255                                 
256                         /* HACK: ideally the preferred height would be queried from
257                            the complete hpacker, but I can't seem to get that
258                            information in time, so this is an estimation 
259                         */
260
261                         prefheight += 30;
262
263                 } 
264         }
265
266         if (box->children().empty()) {
267                 hpacker.remove (*frame);
268         }
269
270         if (button_table.children().empty()) {
271                 hpacker.remove (*bt_frame);
272         }
273
274         if (!output_table.children().empty()) {
275                 frame = manage (new Frame);
276                 frame->set_name ("BaseFrame");
277                 frame->add (output_table);
278                 hpacker.pack_end (*frame, true, true);
279         }
280
281         output_update ();
282
283         output_table.show_all ();
284         button_table.show_all ();
285 }
286
287 LadspaPluginUI::ControlUI::ControlUI ()
288         : automate_button (X_("")) // force creation of a label 
289 {
290         automate_button.set_name ("PluginAutomateButton");
291         ARDOUR_UI::instance()->tooltips().set_tip (automate_button, _("Automation control"));
292
293         /* XXX translators: use a string here that will be at least as long
294            as the longest automation label (see ::automation_state_changed()
295            below). be sure to include a descender.
296         */
297
298         set_size_request_to_display_given_text (*automate_button.get_child(), _("Mgnual"), 5, 5);
299
300         ignore_change = 0;
301         display = 0;
302         button = 0;
303         control = 0;
304         clickbox = 0;
305         adjustment = 0;
306         meterinfo = 0;
307 }
308
309 LadspaPluginUI::ControlUI::~ControlUI() 
310 {
311         if (adjustment) {
312                 delete adjustment;
313         }
314
315         if (meterinfo) {
316                 delete meterinfo->meter;
317                 delete meterinfo;
318         }
319 }
320
321 void
322 LadspaPluginUI::automation_state_changed (ControlUI* cui)
323 {
324         /* update button label */
325
326         switch (insert->get_port_automation_state (cui->port_index) & (Off|Play|Touch|Write)) {
327         case Off:
328                 cui->automate_button.set_label (_("Manual"));
329                 break;
330         case Play:
331                 cui->automate_button.set_label (_("Play"));
332                 break;
333         case Write:
334                 cui->automate_button.set_label (_("Write"));
335                 break;
336         case Touch:
337                 cui->automate_button.set_label (_("Touch"));
338                 break;
339         default:
340                 cui->automate_button.set_label (_("???"));
341                 break;
342         }
343 }
344
345
346 static void integer_printer (char buf[32], Adjustment &adj, void *arg)
347 {
348         snprintf (buf, 32, "%.0f", adj.get_value());
349 }
350
351 void
352 LadspaPluginUI::print_parameter (char *buf, uint32_t len, uint32_t param)
353 {
354         plugin->print_parameter (param, buf, len);
355 }
356
357 LadspaPluginUI::ControlUI*
358 LadspaPluginUI::build_control_ui (guint32 port_index, PBD::Controllable* mcontrol)
359
360 {
361         ControlUI* control_ui;
362         Plugin::ParameterDescriptor desc;
363
364         plugin->get_parameter_descriptor (port_index, desc);
365
366         control_ui = manage (new ControlUI ());
367         control_ui->adjustment = 0;
368         control_ui->combo = 0;
369         control_ui->combo_map = 0;
370         control_ui->port_index = port_index;
371         control_ui->update_pending = false;
372         control_ui->label.set_text (desc.label);
373         control_ui->label.set_alignment (0.0, 0.5);
374         control_ui->label.set_name ("PluginParameterLabel");
375
376         control_ui->set_spacing (5);
377
378         Gtk::Requisition req (control_ui->automate_button.size_request());
379
380         if (plugin->parameter_is_input (port_index)) {
381
382                 boost::shared_ptr<LadspaPlugin> lp;
383
384                 if ((lp = boost::dynamic_pointer_cast<LadspaPlugin>(plugin)) != 0) {
385                         
386                         lrdf_defaults* defaults = lrdf_get_scale_values(lp->unique_id(), port_index);
387                         
388                         if (defaults && defaults->count > 0)    {
389                                 
390                                 control_ui->combo = new Gtk::ComboBoxText;
391                                 //control_ui->combo->set_value_in_list(true, false);
392                                 set_popdown_strings (*control_ui->combo, setup_scale_values(port_index, control_ui));
393                                 control_ui->combo->signal_changed().connect (bind (mem_fun(*this, &LadspaPluginUI::control_combo_changed), control_ui));
394                                 plugin->ParameterChanged.connect (bind (mem_fun (*this, &LadspaPluginUI::parameter_changed), control_ui));
395                                 control_ui->pack_start(control_ui->label, true, true);
396                                 control_ui->pack_start(*control_ui->combo, false, true);
397                                 
398                                 update_control_display(control_ui);
399                                 
400                                 lrdf_free_setting_values(defaults);
401                                 return control_ui;
402                         }
403                 }
404                         
405                 if (desc.toggled) {
406
407                         /* Build a button */
408                 
409                         control_ui->button = manage (new ToggleButton ());
410                         control_ui->button->set_name ("PluginEditorButton");
411                         control_ui->button->set_size_request (20, 20);
412
413                         control_ui->pack_start (control_ui->label, true, true);
414                         control_ui->pack_start (*control_ui->button, false, true);
415                         control_ui->pack_start (control_ui->automate_button, false, false);
416
417                         control_ui->button->signal_clicked().connect (bind (mem_fun(*this, &LadspaPluginUI::control_port_toggled), control_ui));
418                 
419                         if(plugin->get_parameter (port_index) == 1){
420                                 control_ui->button->set_active(true);
421                         }
422
423                         return control_ui;
424                 }
425         
426                 control_ui->adjustment = new Adjustment (0, 0, 0, 0, 0);
427
428                 /* XXX this code is not right yet, because it doesn't handle
429                    the absence of bounds in any sensible fashion.
430                 */
431
432                 control_ui->adjustment->set_lower (desc.lower);
433                 control_ui->adjustment->set_upper (desc.upper);
434
435                 control_ui->logarithmic = desc.logarithmic;
436                 if (control_ui->logarithmic) {
437                         if (control_ui->adjustment->get_lower() == 0.0) {
438                                 control_ui->adjustment->set_lower (control_ui->adjustment->get_upper()/10000);
439                         }
440                         control_ui->adjustment->set_upper (log(control_ui->adjustment->get_upper()));
441                         control_ui->adjustment->set_lower (log(control_ui->adjustment->get_lower()));
442                 }
443         
444                 float delta = desc.upper - desc.lower;
445
446                 control_ui->adjustment->set_page_size (delta/100.0);
447                 control_ui->adjustment->set_step_increment (desc.step);
448                 control_ui->adjustment->set_page_increment (desc.largestep);
449
450                 if (desc.integer_step) {
451                         control_ui->clickbox = new ClickBox (control_ui->adjustment, "PluginUIClickBox");
452                         Gtkmm2ext::set_size_request_to_display_given_text (*control_ui->clickbox, "g9999999", 2, 2);
453                         control_ui->clickbox->set_print_func (integer_printer, 0);
454                 } else {
455                         sigc::slot<void,char*,uint32_t> pslot = sigc::bind (mem_fun(*this, &LadspaPluginUI::print_parameter), (uint32_t) port_index);
456
457                         control_ui->control = new BarController (*control_ui->adjustment, *mcontrol, pslot);
458                         control_ui->control->set_size_request (200, req.height);
459                         control_ui->control->set_name (X_("PluginSlider"));
460                         control_ui->control->set_style (BarController::LeftToRight);
461                         control_ui->control->set_use_parent (true);
462
463                         control_ui->control->StartGesture.connect (bind (mem_fun(*this, &LadspaPluginUI::start_touch), control_ui));
464                         control_ui->control->StopGesture.connect (bind (mem_fun(*this, &LadspaPluginUI::stop_touch), control_ui));
465                         
466                 }
467
468                 if (control_ui->logarithmic) {
469                         control_ui->adjustment->set_value(log(plugin->get_parameter(port_index)));
470                 } else{
471                         control_ui->adjustment->set_value(plugin->get_parameter(port_index));
472                 }
473
474                 /* XXX memory leak: SliderController not destroyed by ControlUI
475                    destructor, and manage() reports object hierarchy
476                    ambiguity.
477                 */
478
479                 control_ui->pack_start (control_ui->label, true, true);
480                 if (desc.integer_step) {
481                         control_ui->pack_start (*control_ui->clickbox, false, false);
482                 } else {
483                         control_ui->pack_start (*control_ui->control, false, false);
484                 }
485
486                 control_ui->pack_start (control_ui->automate_button, false, false);
487                 control_ui->adjustment->signal_value_changed().connect (bind (mem_fun(*this, &LadspaPluginUI::control_adjustment_changed), control_ui));
488                 control_ui->automate_button.signal_clicked().connect (bind (mem_fun(*this, &LadspaPluginUI::astate_clicked), control_ui, (uint32_t) port_index));
489
490                 automation_state_changed (control_ui);
491
492                 plugin->ParameterChanged.connect (bind (mem_fun(*this, &LadspaPluginUI::parameter_changed), control_ui));
493                 insert->automation_list (port_index).automation_state_changed.connect 
494                         (bind (mem_fun(*this, &LadspaPluginUI::automation_state_changed), control_ui));
495
496         } else if (plugin->parameter_is_output (port_index)) {
497
498                 control_ui->display = manage (new EventBox);
499                 control_ui->display->set_name ("ParameterValueDisplay");
500
501                 control_ui->display_label = manage (new Label);
502
503                 control_ui->display_label->set_name ("ParameterValueDisplay");
504
505                 control_ui->display->add (*control_ui->display_label);
506                 Gtkmm2ext::set_size_request_to_display_given_text (*control_ui->display, "-99,99", 2, 2);
507
508                 control_ui->display->show_all ();
509
510                 /* set up a meter */
511                 /* TODO: only make a meter if the port is Hinted for it */
512
513                 MeterInfo * info = new MeterInfo(port_index);
514                 control_ui->meterinfo = info;
515                 
516                 info->meter = new FastMeter (5, 100, FastMeter::Vertical);
517
518                 info->min_unbound = desc.min_unbound;
519                 info->max_unbound = desc.max_unbound;
520
521                 info->min = desc.lower;
522                 info->max = desc.upper;
523
524                 control_ui->vbox = manage (new VBox);
525                 control_ui->hbox = manage (new HBox);
526                 
527                 control_ui->label.set_angle(90);
528                 control_ui->hbox->pack_start (control_ui->label, false, false);
529                 control_ui->hbox->pack_start (*info->meter, false, false);
530
531                 control_ui->vbox->pack_start (*control_ui->hbox, false, false);
532                 
533                 control_ui->vbox->pack_start (*control_ui->display, false, false);
534
535                 control_ui->pack_start (*control_ui->vbox);
536
537                 control_ui->meterinfo->meter->show_all();
538                 control_ui->meterinfo->packed = true;
539                 
540                 output_controls.push_back (control_ui);
541         }
542         
543         plugin->ParameterChanged.connect (bind (mem_fun(*this, &LadspaPluginUI::parameter_changed), control_ui));
544         return control_ui;
545 }
546
547 void
548 LadspaPluginUI::start_touch (LadspaPluginUI::ControlUI* cui)
549 {
550         insert->automation_list (cui->port_index).start_touch ();
551 }
552
553 void
554 LadspaPluginUI::stop_touch (LadspaPluginUI::ControlUI* cui)
555 {
556         insert->automation_list (cui->port_index).stop_touch ();
557 }
558
559 void
560 LadspaPluginUI::astate_clicked (ControlUI* cui, uint32_t port)
561 {
562         using namespace Menu_Helpers;
563
564         if (automation_menu == 0) {
565                 automation_menu = manage (new Menu);
566                 automation_menu->set_name ("ArdourContextMenu");
567         } 
568
569         MenuList& items (automation_menu->items());
570
571         items.clear ();
572         items.push_back (MenuElem (_("Manual"), 
573                                    bind (mem_fun(*this, &LadspaPluginUI::set_automation_state), (AutoState) Off, cui)));
574         items.push_back (MenuElem (_("Play"),
575                                    bind (mem_fun(*this, &LadspaPluginUI::set_automation_state), (AutoState) Play, cui)));
576         items.push_back (MenuElem (_("Write"),
577                                    bind (mem_fun(*this, &LadspaPluginUI::set_automation_state), (AutoState) Write, cui)));
578         items.push_back (MenuElem (_("Touch"),
579                                    bind (mem_fun(*this, &LadspaPluginUI::set_automation_state), (AutoState) Touch, cui)));
580
581         automation_menu->popup (1, gtk_get_current_event_time());
582 }
583
584 void
585 LadspaPluginUI::set_automation_state (AutoState state, ControlUI* cui)
586 {
587         insert->set_port_automation_state (cui->port_index, state);
588 }
589
590 void
591 LadspaPluginUI::control_adjustment_changed (ControlUI* cui)
592 {
593         if (cui->ignore_change) {
594                 return;
595         }
596
597         double value = cui->adjustment->get_value();
598
599         if (cui->logarithmic) {
600                 value = exp(value);
601         }
602
603         insert->set_parameter (cui->port_index, (float) value);
604 }
605
606 void
607 LadspaPluginUI::parameter_changed (uint32_t abs_port_id, float val, ControlUI* cui)
608 {
609         if (cui->port_index == abs_port_id) {
610                 if (!cui->update_pending) {
611                         cui->update_pending = true;
612                         Gtkmm2ext::UI::instance()->call_slot (bind (mem_fun(*this, &LadspaPluginUI::update_control_display), cui));
613                 }
614         }
615 }
616
617 void
618 LadspaPluginUI::update_control_display (ControlUI* cui) 
619 {
620         /* XXX how do we handle logarithmic stuff here ? */
621         
622         cui->update_pending = false;
623
624         float val = plugin->get_parameter (cui->port_index);
625
626         cui->ignore_change++;
627         if (cui->combo) {
628                 std::map<string,float>::iterator it;
629                 for (it = cui->combo_map->begin(); it != cui->combo_map->end(); ++it) {
630                         if (it->second == val) {
631                                 cui->combo->set_active_text(it->first);
632                                 break;
633                         }
634                 }
635         } else if (cui->adjustment == 0) {
636
637                 if (val > 0.5) {
638                         cui->button->set_active (true);
639                 } else {
640                         cui->button->set_active (false);
641                 }
642
643         } else {
644                 if (cui->logarithmic) {
645                         val = log(val);
646                 }
647                 if (val != cui->adjustment->get_value()) {
648                         cui->adjustment->set_value (val);
649                 }
650         }
651         cui->ignore_change--;
652 }
653
654 void
655 LadspaPluginUI::control_port_toggled (ControlUI* cui)
656 {
657         if (!cui->ignore_change) {
658                 insert->set_parameter (cui->port_index, cui->button->get_active());
659         }
660 }
661
662 void
663 LadspaPluginUI::control_combo_changed (ControlUI* cui)
664 {
665         if (!cui->ignore_change) {
666                 string value = cui->combo->get_active_text();
667                 std::map<string,float> mapping = *cui->combo_map;
668                 insert->set_parameter (cui->port_index, mapping[value]);
669         }
670
671 }
672
673 void
674 LadspaPluginUI::redirect_active_changed (Redirect* r, void* src)
675 {
676         ENSURE_GUI_THREAD(bind (mem_fun(*this, &LadspaPluginUI::redirect_active_changed), r, src));
677         
678         bypass_button.set_active (!r->active());
679 }
680
681 bool
682 LadspaPluginUI::start_updating (GdkEventAny* ignored)
683 {
684         if (output_controls.size() > 0 ) {
685                 screen_update_connection.disconnect();
686                 screen_update_connection = ARDOUR_UI::instance()->RapidScreenUpdate.connect 
687                         (mem_fun(*this, &LadspaPluginUI::output_update));
688         }
689         return false;
690 }
691
692 bool
693 LadspaPluginUI::stop_updating (GdkEventAny* ignored)
694 {
695         if (output_controls.size() > 0 ) {
696                 screen_update_connection.disconnect();
697         }
698         return false;
699 }
700
701 void
702 LadspaPluginUI::output_update ()
703 {
704         for (vector<ControlUI*>::iterator i = output_controls.begin(); i != output_controls.end(); ++i) {
705                 float val = plugin->get_parameter ((*i)->port_index);
706                 char buf[32];
707                 snprintf (buf, sizeof(buf), "%.2f", val);
708                 (*i)->display_label->set_text (buf);
709
710                 /* autoscaling for the meter */
711                 if ((*i)->meterinfo && (*i)->meterinfo->packed) {
712                         
713                         if (val < (*i)->meterinfo->min) {
714                                 if ((*i)->meterinfo->min_unbound)
715                                         (*i)->meterinfo->min = val;
716                                 else
717                                         val = (*i)->meterinfo->min;
718                         }
719
720                         if (val > (*i)->meterinfo->max) {
721                                 if ((*i)->meterinfo->max_unbound)
722                                         (*i)->meterinfo->max = val;
723                                 else
724                                         val = (*i)->meterinfo->max;
725                         }
726                         
727                         if ((*i)->meterinfo->max > (*i)->meterinfo->min ) {
728                                 float lval = (val - (*i)->meterinfo->min) / ((*i)->meterinfo->max - (*i)->meterinfo->min) ;
729                                 (*i)->meterinfo->meter->set (lval );
730                         }
731                 }
732         }
733 }
734
735 vector<string> 
736 LadspaPluginUI::setup_scale_values(guint32 port_index, ControlUI* cui)
737 {
738         vector<string> enums;
739         boost::shared_ptr<LadspaPlugin> lp = boost::dynamic_pointer_cast<LadspaPlugin> (plugin);
740
741         cui->combo_map = new std::map<string, float>;
742         lrdf_defaults* defaults = lrdf_get_scale_values(lp->unique_id(), port_index);
743         if (defaults)   {
744                 for (uint32_t i = 0; i < defaults->count; ++i) {
745                         enums.push_back(defaults->items[i].label);
746                         pair<string, float> newpair;
747                         newpair.first = defaults->items[i].label;
748                         newpair.second = defaults->items[i].value;
749                         cui->combo_map->insert(newpair);
750                 }
751
752                 lrdf_free_setting_values(defaults);
753         }
754
755         return enums;
756 }
757