fix clicking when processors become active/inactive; reduce crazy 2.5sec delay for...
[ardour.git] / gtk2_ardour / gain_meter.cc
index 182cf2d89da4445ee26321d3a6bb96523e8d74a1..e6f0c7404221f5bdf6fd37372dd29056d453e8ce 100644 (file)
 
 #include <limits.h>
 
-#include <ardour/io.h>
-#include <ardour/route.h>
-#include <ardour/route_group.h>
-#include <ardour/session.h>
-#include <ardour/session_route.h>
-#include <ardour/dB.h>
+#include "ardour/amp.h"
+#include "ardour/io.h"
+#include "ardour/route.h"
+#include "ardour/route_group.h"
+#include "ardour/session.h"
+#include "ardour/session_route.h"
+#include "ardour/dB.h"
 
 #include <gtkmm/style.h>
 #include <gdkmm/color.h>
@@ -33,8 +34,9 @@
 #include <gtkmm2ext/stop_signal.h>
 #include <gtkmm2ext/barcontroller.h>
 #include <gtkmm2ext/gtk_ui.h>
-#include <midi++/manager.h>
-#include <pbd/fastlog.h>
+#include "midi++/manager.h"
+#include "pbd/fastlog.h"
+#include "pbd/stacktrace.h"
 
 #include "ardour_ui.h"
 #include "gain_meter.h"
@@ -44,9 +46,9 @@
 #include "keyboard.h"
 #include "public_editor.h"
 
-#include <ardour/session.h>
-#include <ardour/route.h>
-#include <ardour/meter.h>
+#include "ardour/session.h"
+#include "ardour/route.h"
+#include "ardour/meter.h"
 
 #include "i18n.h"
 
@@ -72,17 +74,19 @@ GainMeter::setup_slider_pix ()
        }
 }
 
-GainMeterBase::GainMeterBase (boost::shared_ptr<IO> io, Session& s, 
+GainMeterBase::GainMeterBase (Session& s, 
                              const Glib::RefPtr<Gdk::Pixbuf>& pix,
                              bool horizontal)
-       : _io (io),
-         _session (s),
+       : _session (s),
          // 0.781787 is the value needed for gain to be set to 0.
          gain_adjustment (0.781787, 0.0, 1.0, 0.01, 0.1),
          gain_automation_style_button (""),
-         gain_automation_state_button ("")
+         gain_automation_state_button (""),
+         dpi_changed (false)
        
 {
+       using namespace Menu_Helpers;
+
        ignore_toggle = false;
        meter_menu = 0;
        next_release_selects = false;
@@ -92,16 +96,14 @@ GainMeterBase::GainMeterBase (boost::shared_ptr<IO> io, Session& s,
        if (horizontal) {
                gain_slider = manage (new HSliderController (pix,
                                                             &gain_adjustment,
-                                                            _io->gain_control(),
                                                             false));
        } else {
                gain_slider = manage (new VSliderController (pix,
                                                             &gain_adjustment,
-                                                            _io->gain_control(),
                                                             false));
        }
 
-       level_meter = new LevelMeter(_io, _session);
+       level_meter = new LevelMeter(_session);
 
        gain_slider->signal_button_press_event().connect (mem_fun(*this, &GainMeter::start_gain_touch));
        gain_slider->signal_button_release_event().connect (mem_fun(*this, &GainMeter::end_gain_touch));
@@ -133,59 +135,17 @@ GainMeterBase::GainMeterBase (boost::shared_ptr<IO> io, Session& s,
 
        gain_automation_state_button.set_size_request(15, 15);
        gain_automation_style_button.set_size_request(15, 15);
-
-
-
-
-
-       boost::shared_ptr<Route> r;
-
-       if ((r = boost::dynamic_pointer_cast<Route> (_io)) != 0) {
-
-               if (!r->is_hidden()) {
-
-                       using namespace Menu_Helpers;
+  
+       gain_astyle_menu.items().push_back (MenuElem (_("Trim")));
+       gain_astyle_menu.items().push_back (MenuElem (_("Abs")));
        
-                       gain_astate_menu.items().push_back (MenuElem (_("Manual"), 
-                                                                     bind (mem_fun (*_io, &IO::set_parameter_automation_state),
-                                                                           Parameter(GainAutomation), (AutoState) Off)));
-                       gain_astate_menu.items().push_back (MenuElem (_("Play"),
-                                                                     bind (mem_fun (*_io, &IO::set_parameter_automation_state),
-                                                                           Parameter(GainAutomation), (AutoState) Play)));
-                       gain_astate_menu.items().push_back (MenuElem (_("Write"),
-                                                                     bind (mem_fun (*_io, &IO::set_parameter_automation_state),
-                                                                           Parameter(GainAutomation), (AutoState) Write)));
-                       gain_astate_menu.items().push_back (MenuElem (_("Touch"),
-                                                                     bind (mem_fun (*_io, &IO::set_parameter_automation_state),
-                                                                           Parameter(GainAutomation), (AutoState) Touch)));
-                       
-                       gain_astyle_menu.items().push_back (MenuElem (_("Trim")));
-                       gain_astyle_menu.items().push_back (MenuElem (_("Abs")));
-                       
-                       gain_astate_menu.set_name ("ArdourContextMenu");
-                       gain_astyle_menu.set_name ("ArdourContextMenu");
-                       
-                       gain_automation_style_button.signal_button_press_event().connect (mem_fun(*this, &GainMeterBase::gain_automation_style_button_event), false);
-                       gain_automation_state_button.signal_button_press_event().connect (mem_fun(*this, &GainMeterBase::gain_automation_state_button_event), false);
-                       
-                       r->gain_control()->alist()->automation_state_changed.connect (mem_fun(*this, &GainMeter::gain_automation_state_changed));
-                       r->gain_control()->alist()->automation_style_changed.connect (mem_fun(*this, &GainMeter::gain_automation_style_changed));
-
-                       gain_automation_state_changed ();
-               }
-       }
-
-       _io->gain_control()->Changed.connect (mem_fun(*this, &GainMeterBase::gain_changed));
+       gain_astate_menu.set_name ("ArdourContextMenu");
+       gain_astyle_menu.set_name ("ArdourContextMenu");
 
        gain_adjustment.signal_value_changed().connect (mem_fun(*this, &GainMeterBase::gain_adjusted));
        peak_display.signal_button_release_event().connect (mem_fun(*this, &GainMeterBase::peak_button_release), false);
        gain_display.signal_key_press_event().connect (mem_fun(*this, &GainMeterBase::gain_key_press), false);
 
-       gain_changed ();
-       show_gain ();
-
-       update_gain_sensitive ();
-       
        ResetAllPeakDisplays.connect (mem_fun(*this, &GainMeterBase::reset_peak_display));
        ResetGroupPeakDisplays.connect (mem_fun(*this, &GainMeterBase::reset_group_peak_display));
 
@@ -196,13 +156,68 @@ GainMeterBase::GainMeterBase (boost::shared_ptr<IO> io, Session& s,
 
 GainMeterBase::~GainMeterBase ()
 {
-       if (meter_menu) {
-               delete meter_menu;
-       }
+       delete meter_menu;
+       delete level_meter;
+}
+
+void
+GainMeterBase::set_controls (boost::shared_ptr<Route> r,
+                            boost::shared_ptr<PeakMeter> pm, 
+                            boost::shared_ptr<Amp> amp)
+{
+       connections.clear ();
+
+       if (!pm && !amp) {
+               level_meter->set_meter (0);
+               gain_slider->set_controllable (boost::shared_ptr<PBD::Controllable>());
+               _meter.reset ();
+               _amp.reset ();
+               _route.reset ();
+               return;
+       } 
+
+       _meter = pm;
+       _amp = amp;
+       _route = r;
+
+       level_meter->set_meter (pm.get());
+       gain_slider->set_controllable (amp->gain_control());
+       
+       if (!_route || !_route->is_hidden()) {
+               
+               using namespace Menu_Helpers;
+               
+               gain_astate_menu.items().clear ();
+               
+               gain_astate_menu.items().push_back (MenuElem (_("Manual"), 
+                                                             bind (mem_fun (*(amp.get()), &Automatable::set_parameter_automation_state),
+                                                                   Evoral::Parameter(GainAutomation), (AutoState) Off)));
+               gain_astate_menu.items().push_back (MenuElem (_("Play"),
+                                                             bind (mem_fun (*(amp.get()), &Automatable::set_parameter_automation_state),
+                                                                   Evoral::Parameter(GainAutomation), (AutoState) Play)));
+               gain_astate_menu.items().push_back (MenuElem (_("Write"),
+                                                             bind (mem_fun (*(amp.get()), &Automatable::set_parameter_automation_state),
+                                                                   Evoral::Parameter(GainAutomation), (AutoState) Write)));
+               gain_astate_menu.items().push_back (MenuElem (_("Touch"),
+                                                             bind (mem_fun (*(amp.get()), &Automatable::set_parameter_automation_state),
+                                                                   Evoral::Parameter(GainAutomation), (AutoState) Touch)));
+               
+               connections.push_back (gain_automation_style_button.signal_button_press_event().connect (mem_fun(*this, &GainMeterBase::gain_automation_style_button_event), false));
+               connections.push_back (gain_automation_state_button.signal_button_press_event().connect (mem_fun(*this, &GainMeterBase::gain_automation_state_button_event), false));
+
+               boost::shared_ptr<AutomationControl> gc = amp->gain_control();
 
-       if (level_meter) {
-               delete level_meter;
+               connections.push_back (gc->alist()->automation_state_changed.connect (mem_fun(*this, &GainMeter::gain_automation_state_changed)));
+               connections.push_back (gc->alist()->automation_style_changed.connect (mem_fun(*this, &GainMeter::gain_automation_style_changed)));
+               
+               gain_automation_state_changed ();
        }
+
+       connections.push_back (amp->gain_control()->Changed.connect (mem_fun (*this, &GainMeterBase::gain_changed)));
+
+       gain_changed ();
+       show_gain ();
+       update_gain_sensitive ();
 }
 
 void
@@ -260,10 +275,8 @@ GainMeterBase::peak_button_release (GdkEventButton* ev)
        if (ev->button == 1 && Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier|Keyboard::TertiaryModifier)) {
                ResetAllPeakDisplays ();
        } else if (ev->button == 1 && Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
-               boost::shared_ptr<Route> r;
-
-               if ((r = boost::dynamic_pointer_cast<Route> (_io)) != 0) {
-                       ResetGroupPeakDisplays (r->mix_group());
+               if (_route) {
+                       ResetGroupPeakDisplays (_route->route_group());
                }
        } else {
                reset_peak_display ();
@@ -275,12 +288,7 @@ GainMeterBase::peak_button_release (GdkEventButton* ev)
 void
 GainMeterBase::reset_peak_display ()
 {
-       boost::shared_ptr<Route> r;
-
-       if ((r = boost::dynamic_pointer_cast<Route> (_io)) != 0) {
-               r->peak_meter().reset_max();
-       }
-
+       _meter->reset_max();
        level_meter->clear_meters();
        max_peak = -INFINITY;
        peak_display.set_label (_("-Inf"));
@@ -290,13 +298,9 @@ GainMeterBase::reset_peak_display ()
 void
 GainMeterBase::reset_group_peak_display (RouteGroup* group)
 {
-       boost::shared_ptr<Route> r;
-       
-       if ((r = boost::dynamic_pointer_cast<Route> (_io)) != 0) {
-               if (group == r->mix_group()) {
-                       reset_peak_display ();
+       if (_route && group == _route->route_group()) {
+               reset_peak_display ();
                }
-       }
 }
 
 void
@@ -342,7 +346,7 @@ GainMeterBase::gain_activated ()
 
                f = min (f, 6.0f);
 
-               _io->gain_control()->set_value (dB_to_coefficient(f));
+               _amp->set_gain (dB_to_coefficient(f), this);
 
                if (gain_display.has_focus()) {
                        PublicEditor::instance().reset_focus();
@@ -370,16 +374,27 @@ void
 GainMeterBase::gain_adjusted ()
 {
        if (!ignore_toggle) {
-               _io->gain_control()->set_value (slider_position_to_gain (gain_adjustment.get_value()));
+               if (_route) {
+                       if (_route->amp() == _amp) {
+                               _route->set_gain (slider_position_to_gain (gain_adjustment.get_value()), this);
+                       } else {
+                               _amp->set_gain (slider_position_to_gain (gain_adjustment.get_value()), this);
+                       }
+               }
        }
+
        show_gain ();
 }
 
 void
 GainMeterBase::effective_gain_display ()
 {
-       gfloat value = gain_to_slider_position (_io->effective_gain());
+       gfloat value = gain_to_slider_position (_amp->gain());
        
+       //cerr << this << " for " << _io->name() << " EGAIN = " << value
+       //              << " AGAIN = " << gain_adjustment.get_value () << endl;
+       // stacktrace (cerr, 20);
+
        if (gain_adjustment.get_value() != value) {
                ignore_toggle = true; 
                gain_adjustment.set_value (value);
@@ -408,11 +423,10 @@ GainMeterBase::set_fader_name (const char * name)
 void
 GainMeterBase::update_gain_sensitive ()
 {
-       static_cast<Gtkmm2ext::SliderController*>(gain_slider)->set_sensitive (
-                       !(_io->gain_control()->alist()->automation_state() & Play));
+       bool x = !(_amp->gain_control()->alist()->automation_state() & Play);
+       static_cast<Gtkmm2ext::SliderController*>(gain_slider)->set_sensitive (x);
 }
 
-
 static MeterPoint
 next_meter_point (MeterPoint mp)
 {
@@ -436,11 +450,9 @@ next_meter_point (MeterPoint mp)
 gint
 GainMeterBase::meter_press(GdkEventButton* ev)
 {
-       boost::shared_ptr<Route> _route;
-
        wait_for_release = false;
        
-       if ((_route = boost::dynamic_pointer_cast<Route>(_io)) == 0) {
+       if (!_route) {
                return FALSE;
        }
 
@@ -452,7 +464,7 @@ GainMeterBase::meter_press(GdkEventButton* ev)
 
                } else {
 
-                       if (ev->button == 2) {
+                       if (Keyboard::is_button2_event(ev)) {
 
                                // Primary-button2 click is the midi binding click
                                // button2-click is "momentary"
@@ -463,10 +475,10 @@ GainMeterBase::meter_press(GdkEventButton* ev)
                                }
                        }
 
-                       if (ev->button == 1 || ev->button == 2) {
+                       if (_route && (ev->button == 1 || Keyboard::is_button2_event (ev))) {
 
                                if (Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::PrimaryModifier|Keyboard::TertiaryModifier))) {
-
+                                       
                                        /* Primary+Tertiary-click applies change to all routes */
 
                                        _session.begin_reversible_command (_("meter point change"));
@@ -515,10 +527,8 @@ GainMeterBase::meter_release(GdkEventButton* ev)
                if (wait_for_release){
                        wait_for_release = false;
                        
-                       boost::shared_ptr<Route> r;
-                       
-                       if ((r = boost::dynamic_pointer_cast<Route>(_io)) != 0) {
-                               set_meter_point (*r, old_meter_point);
+                       if (_route) {
+                               set_meter_point (*_route, old_meter_point);
                        }
                }
        }
@@ -537,7 +547,7 @@ GainMeterBase::set_mix_group_meter_point (Route& route, MeterPoint mp)
 {
        RouteGroup* mix_group;
 
-       if((mix_group = route.mix_group()) != 0){
+       if((mix_group = route.route_group()) != 0){
                mix_group->apply (&Route::set_meter_point, mp, this);
        } else {
                route.set_meter_point (mp, this);
@@ -547,9 +557,7 @@ GainMeterBase::set_mix_group_meter_point (Route& route, MeterPoint mp)
 void
 GainMeterBase::meter_point_clicked ()
 {
-       boost::shared_ptr<Route> r;
-
-       if ((r = boost::dynamic_pointer_cast<Route> (_io)) != 0) {
+       if (_route) {
                /* WHAT? */
        }
 }
@@ -557,14 +565,14 @@ GainMeterBase::meter_point_clicked ()
 gint
 GainMeterBase::start_gain_touch (GdkEventButton* ev)
 {
-       _io->gain_control()->alist()->start_touch ();
+       _amp->gain_control()->start_touch ();
        return FALSE;
 }
 
 gint
 GainMeterBase::end_gain_touch (GdkEventButton* ev)
 {
-       _io->gain_control()->alist()->stop_touch ();
+       _amp->gain_control()->stop_touch ();
        return FALSE;
 }
 
@@ -667,10 +675,10 @@ GainMeterBase::gain_automation_style_changed ()
 {
        switch (_width) {
        case Wide:
-               gain_automation_style_button.set_label (astyle_string(_io->gain_control()->alist()->automation_style()));
+               gain_automation_style_button.set_label (astyle_string(_amp->gain_control()->alist()->automation_style()));
                break;
        case Narrow:
-               gain_automation_style_button.set_label  (short_astyle_string(_io->gain_control()->alist()->automation_style()));
+               gain_automation_style_button.set_label  (short_astyle_string(_amp->gain_control()->alist()->automation_style()));
                break;
        }
 }
@@ -684,14 +692,14 @@ GainMeterBase::gain_automation_state_changed ()
 
        switch (_width) {
        case Wide:
-               gain_automation_state_button.set_label (astate_string(_io->gain_control()->alist()->automation_state()));
+               gain_automation_state_button.set_label (astate_string(_amp->gain_control()->alist()->automation_state()));
                break;
        case Narrow:
-               gain_automation_state_button.set_label (short_astate_string(_io->gain_control()->alist()->automation_state()));
+               gain_automation_state_button.set_label (short_astate_string(_amp->gain_control()->alist()->automation_state()));
                break;
        }
 
-       x = (_io->gain_control()->alist()->automation_state() != Off);
+       x = (_amp->gain_control()->alist()->automation_state() != Off);
        
        if (gain_automation_state_button.get_active() != x) {
                ignore_toggle = true;
@@ -752,10 +760,9 @@ GainMeterBase::on_theme_changed()
        style_changed = true;
 }
 
-GainMeter::GainMeter (boost::shared_ptr<IO> io, Session& s)
-       : GainMeterBase (io, s, slider, false)
+GainMeter::GainMeter (Session& s)
+       : GainMeterBase (s, slider, false)
 {
-
        gain_display_box.set_homogeneous (true);
        gain_display_box.set_spacing (2);
        gain_display_box.pack_start (gain_display, true, true);
@@ -785,23 +792,6 @@ GainMeter::GainMeter (boost::shared_ptr<IO> io, Session& s)
        hbox.set_spacing (2);
        hbox.pack_start (*fader_vbox, true, true);
 
-       boost::shared_ptr<Route> r;
-
-       if ((r = boost::dynamic_pointer_cast<Route> (_io)) != 0) {
-               
-               /* 
-                  if we have a non-hidden route (ie. we're not the click or the auditioner), 
-                  pack some route-dependent stuff.
-               */
-
-               gain_display_box.pack_end (peak_display, true, true);
-               hbox.pack_end (*level_meter, true, true);
-
-               if (!r->is_hidden()) {
-                       fader_vbox->pack_start (gain_automation_state_button, false, false, 0);
-               }
-       }
-
        set_spacing (2);
 
        pack_start (gain_display_box, Gtk::PACK_SHRINK);
@@ -810,6 +800,40 @@ GainMeter::GainMeter (boost::shared_ptr<IO> io, Session& s)
        meter_metric_area.signal_expose_event().connect (mem_fun(*this, &GainMeter::meter_metrics_expose));
 }
 
+void 
+GainMeter::set_controls (boost::shared_ptr<Route> r,
+                        boost::shared_ptr<PeakMeter> meter,
+                        boost::shared_ptr<Amp> amp)
+{
+       if (level_meter->get_parent()) {
+               hbox.remove (*level_meter);
+       }
+
+       if (peak_display.get_parent()) {
+               gain_display_box.remove (peak_display);
+       }
+
+       if (gain_automation_state_button.get_parent()) {
+               fader_vbox->remove (gain_automation_state_button);
+       }
+
+       GainMeterBase::set_controls (r, meter, amp);
+
+       /* 
+          if we have a non-hidden route (ie. we're not the click or the auditioner), 
+          pack some route-dependent stuff.
+       */
+       
+       gain_display_box.pack_end (peak_display, true, true);
+       hbox.pack_end (*level_meter, true, true);
+       
+       if (!r->is_hidden()) {
+               fader_vbox->pack_start (gain_automation_state_button, false, false, 0);
+       }
+
+       setup_meters ();
+       hbox.show_all ();
+}
 
 int
 GainMeter::get_gm_width ()
@@ -895,3 +919,14 @@ GainMeter::meter_metrics_expose (GdkEventExpose *ev)
        return true;
 }
 
+boost::shared_ptr<PBD::Controllable>
+GainMeterBase::get_controllable()
+{
+       if (_amp) {
+               return _amp->gain_control();
+       } else {
+               return boost::shared_ptr<PBD::Controllable>();
+       }
+}
+
+