2 Copyright (C) 2002 Paul Davis
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.
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.
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.
22 #include "ardour/io.h"
23 #include "ardour/route.h"
24 #include "ardour/route_group.h"
25 #include "ardour/session.h"
26 #include "ardour/session_route.h"
27 #include "ardour/dB.h"
29 #include <gtkmm/style.h>
30 #include <gdkmm/color.h>
31 #include <gtkmm2ext/utils.h>
32 #include <gtkmm2ext/fastmeter.h>
33 #include <gtkmm2ext/stop_signal.h>
34 #include <gtkmm2ext/barcontroller.h>
35 #include <gtkmm2ext/gtk_ui.h>
36 #include "midi++/manager.h"
37 #include "pbd/fastlog.h"
38 #include "pbd/stacktrace.h"
40 #include "ardour_ui.h"
41 #include "gain_meter.h"
44 #include "gui_thread.h"
46 #include "public_editor.h"
48 #include "ardour/session.h"
49 #include "ardour/route.h"
50 #include "ardour/meter.h"
54 using namespace ARDOUR;
56 using namespace Gtkmm2ext;
61 sigc::signal<void> GainMeterBase::ResetAllPeakDisplays;
62 sigc::signal<void,RouteGroup*> GainMeterBase::ResetGroupPeakDisplays;
64 map<string,Glib::RefPtr<Gdk::Pixmap> > GainMeter::metric_pixmaps;
65 Glib::RefPtr<Gdk::Pixbuf> GainMeter::slider;
69 GainMeter::setup_slider_pix ()
71 if ((slider = ::get_icon ("fader_belt")) == 0) {
72 throw failed_constructor();
76 GainMeterBase::GainMeterBase (Session& s,
77 const Glib::RefPtr<Gdk::Pixbuf>& pix,
80 // 0.781787 is the value needed for gain to be set to 0.
81 gain_adjustment (0.781787, 0.0, 1.0, 0.01, 0.1),
82 gain_automation_style_button (""),
83 gain_automation_state_button (""),
87 using namespace Menu_Helpers;
89 ignore_toggle = false;
91 next_release_selects = false;
96 gain_slider = manage (new HSliderController (pix,
100 gain_slider = manage (new VSliderController (pix,
105 level_meter = new LevelMeter(_session);
107 gain_slider->signal_button_press_event().connect (mem_fun(*this, &GainMeter::start_gain_touch));
108 gain_slider->signal_button_release_event().connect (mem_fun(*this, &GainMeter::end_gain_touch));
109 gain_slider->set_name ("GainFader");
111 gain_display.set_name ("MixerStripGainDisplay");
112 gain_display.set_has_frame (false);
113 set_size_request_to_display_given_text (gain_display, "-80.g", 2, 6); /* note the descender */
114 gain_display.signal_activate().connect (mem_fun (*this, &GainMeter::gain_activated));
115 gain_display.signal_focus_in_event().connect (mem_fun (*this, &GainMeter::gain_focused), false);
116 gain_display.signal_focus_out_event().connect (mem_fun (*this, &GainMeter::gain_focused), false);
118 peak_display.set_name ("MixerStripPeakDisplay");
119 // peak_display.set_has_frame (false);
120 // peak_display.set_editable (false);
121 set_size_request_to_display_given_text (peak_display, "-80.g", 2, 6); /* note the descender */
122 max_peak = minus_infinity();
123 peak_display.set_label (_("-inf"));
124 peak_display.unset_flags (Gtk::CAN_FOCUS);
126 gain_automation_style_button.set_name ("MixerAutomationModeButton");
127 gain_automation_state_button.set_name ("MixerAutomationPlaybackButton");
129 ARDOUR_UI::instance()->tooltips().set_tip (gain_automation_state_button, _("Fader automation mode"));
130 ARDOUR_UI::instance()->tooltips().set_tip (gain_automation_style_button, _("Fader automation type"));
132 gain_automation_style_button.unset_flags (Gtk::CAN_FOCUS);
133 gain_automation_state_button.unset_flags (Gtk::CAN_FOCUS);
135 gain_automation_state_button.set_size_request(15, 15);
136 gain_automation_style_button.set_size_request(15, 15);
138 gain_astyle_menu.items().push_back (MenuElem (_("Trim")));
139 gain_astyle_menu.items().push_back (MenuElem (_("Abs")));
141 gain_astate_menu.set_name ("ArdourContextMenu");
142 gain_astyle_menu.set_name ("ArdourContextMenu");
144 gain_adjustment.signal_value_changed().connect (mem_fun(*this, &GainMeterBase::gain_adjusted));
145 peak_display.signal_button_release_event().connect (mem_fun(*this, &GainMeterBase::peak_button_release), false);
146 gain_display.signal_key_press_event().connect (mem_fun(*this, &GainMeterBase::gain_key_press), false);
148 ResetAllPeakDisplays.connect (mem_fun(*this, &GainMeterBase::reset_peak_display));
149 ResetGroupPeakDisplays.connect (mem_fun(*this, &GainMeterBase::reset_group_peak_display));
151 UI::instance()->theme_changed.connect (mem_fun(*this, &GainMeterBase::on_theme_changed));
152 ColorsChanged.connect (bind(mem_fun (*this, &GainMeterBase::color_handler), false));
153 DPIReset.connect (bind(mem_fun (*this, &GainMeterBase::color_handler), true));
156 GainMeterBase::~GainMeterBase ()
163 GainMeterBase::set_controls (boost::shared_ptr<Route> r,
164 boost::shared_ptr<PeakMeter> pm,
165 boost::shared_ptr<AutomationControl> gc,
166 boost::shared_ptr<Automatable> gc_owner)
168 connections.clear ();
171 level_meter->set_meter (0);
172 gain_slider->set_controllable (boost::shared_ptr<PBD::Controllable>());
174 _gain_control.reset ();
183 level_meter->set_meter (pm.get());
184 gain_slider->set_controllable (gc);
186 if (!_route || !_route->is_hidden()) {
188 using namespace Menu_Helpers;
190 gain_astate_menu.items().clear ();
192 gain_astate_menu.items().push_back (MenuElem (_("Manual"),
193 bind (mem_fun (*(gc_owner.get()), &Automatable::set_parameter_automation_state),
194 Evoral::Parameter(GainAutomation), (AutoState) Off)));
195 gain_astate_menu.items().push_back (MenuElem (_("Play"),
196 bind (mem_fun (*(gc_owner.get()), &Automatable::set_parameter_automation_state),
197 Evoral::Parameter(GainAutomation), (AutoState) Play)));
198 gain_astate_menu.items().push_back (MenuElem (_("Write"),
199 bind (mem_fun (*(gc_owner.get()), &Automatable::set_parameter_automation_state),
200 Evoral::Parameter(GainAutomation), (AutoState) Write)));
201 gain_astate_menu.items().push_back (MenuElem (_("Touch"),
202 bind (mem_fun (*(gc_owner.get()), &Automatable::set_parameter_automation_state),
203 Evoral::Parameter(GainAutomation), (AutoState) Touch)));
205 connections.push_back (gain_automation_style_button.signal_button_press_event().connect (mem_fun(*this, &GainMeterBase::gain_automation_style_button_event), false));
206 connections.push_back (gain_automation_state_button.signal_button_press_event().connect (mem_fun(*this, &GainMeterBase::gain_automation_state_button_event), false));
208 connections.push_back (gc->alist()->automation_state_changed.connect (mem_fun(*this, &GainMeter::gain_automation_state_changed)));
209 connections.push_back (gc->alist()->automation_style_changed.connect (mem_fun(*this, &GainMeter::gain_automation_style_changed)));
211 gain_automation_state_changed ();
214 connections.push_back (gc->Changed.connect (mem_fun (*this, &GainMeterBase::gain_changed)));
218 update_gain_sensitive ();
222 GainMeterBase::hide_all_meters ()
224 level_meter->hide_meters();
228 GainMeter::hide_all_meters ()
230 bool remove_metric_area = false;
232 GainMeterBase::hide_all_meters ();
234 if (remove_metric_area) {
235 if (meter_metric_area.get_parent()) {
236 level_meter->remove (meter_metric_area);
242 GainMeterBase::setup_meters (int len)
244 level_meter->setup_meters(len, 5);
248 GainMeter::setup_meters (int len)
250 if (!meter_metric_area.get_parent()) {
251 level_meter->pack_end (meter_metric_area, false, false);
252 meter_metric_area.show_all ();
254 GainMeterBase::setup_meters (len);
258 GainMeterBase::gain_key_press (GdkEventKey* ev)
260 if (key_is_legal_for_numeric_entry (ev->keyval)) {
261 /* drop through to normal handling */
264 /* illegal key for gain entry */
269 GainMeterBase::peak_button_release (GdkEventButton* ev)
271 /* reset peak label */
273 if (ev->button == 1 && Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier|Keyboard::TertiaryModifier)) {
274 ResetAllPeakDisplays ();
275 } else if (ev->button == 1 && Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
277 ResetGroupPeakDisplays (_route->mix_group());
280 reset_peak_display ();
287 GainMeterBase::reset_peak_display ()
290 level_meter->clear_meters();
291 max_peak = -INFINITY;
292 peak_display.set_label (_("-Inf"));
293 peak_display.set_name ("MixerStripPeakDisplay");
297 GainMeterBase::reset_group_peak_display (RouteGroup* group)
299 if (_route && group == _route->mix_group()) {
300 reset_peak_display ();
305 GainMeterBase::popup_meter_menu (GdkEventButton *ev)
307 using namespace Menu_Helpers;
309 if (meter_menu == 0) {
310 meter_menu = new Gtk::Menu;
311 MenuList& items = meter_menu->items();
313 items.push_back (MenuElem ("-inf .. +0dBFS"));
314 items.push_back (MenuElem ("-10dB .. +0dBFS"));
315 items.push_back (MenuElem ("-4 .. +0dBFS"));
316 items.push_back (SeparatorElem());
317 items.push_back (MenuElem ("-inf .. -2dBFS"));
318 items.push_back (MenuElem ("-10dB .. -2dBFS"));
319 items.push_back (MenuElem ("-4 .. -2dBFS"));
322 meter_menu->popup (1, ev->time);
326 GainMeterBase::gain_focused (GdkEventFocus* ev)
329 gain_display.select_region (0, -1);
331 gain_display.select_region (0, 0);
337 GainMeterBase::gain_activated ()
341 if (sscanf (gain_display.get_text().c_str(), "%f", &f) == 1) {
343 /* clamp to displayable values */
347 _gain_control->set_value (dB_to_coefficient(f));
349 if (gain_display.has_focus()) {
350 PublicEditor::instance().reset_focus();
356 GainMeterBase::show_gain ()
360 float v = gain_adjustment.get_value();
363 strcpy (buf, _("-inf"));
365 snprintf (buf, 32, "%.1f", coefficient_to_dB (slider_position_to_gain (v)));
368 gain_display.set_text (buf);
372 GainMeterBase::gain_adjusted ()
374 //cerr << this << " for " << _io->name() << " GAIN ADJUSTED\n";
375 if (!ignore_toggle) {
376 //cerr << "Set GC\n";
377 _gain_control->set_value (slider_position_to_gain (gain_adjustment.get_value()));
378 //cerr << "Set GC OUT\n";
384 GainMeterBase::effective_gain_display ()
386 gfloat value = gain_to_slider_position (_gain_control->get_value());
388 //cerr << this << " for " << _io->name() << " EGAIN = " << value
389 // << " AGAIN = " << gain_adjustment.get_value () << endl;
390 // stacktrace (cerr, 20);
392 if (gain_adjustment.get_value() != value) {
393 ignore_toggle = true;
394 gain_adjustment.set_value (value);
395 ignore_toggle = false;
400 GainMeterBase::gain_changed ()
402 Gtkmm2ext::UI::instance()->call_slot (mem_fun(*this, &GainMeterBase::effective_gain_display));
406 GainMeterBase::set_meter_strip_name (const char * name)
408 meter_metric_area.set_name (name);
412 GainMeterBase::set_fader_name (const char * name)
414 gain_slider->set_name (name);
418 GainMeterBase::update_gain_sensitive ()
420 static_cast<Gtkmm2ext::SliderController*>(gain_slider)->set_sensitive (
421 !(_gain_control->alist()->automation_state() & Play));
426 next_meter_point (MeterPoint mp)
430 return MeterPreFader;
434 return MeterPostFader;
446 GainMeterBase::meter_press(GdkEventButton* ev)
448 wait_for_release = false;
454 if (!ignore_toggle) {
456 if (Keyboard::is_context_menu_event (ev)) {
458 // no menu at this time.
462 if (Keyboard::is_button2_event(ev)) {
464 // Primary-button2 click is the midi binding click
465 // button2-click is "momentary"
467 if (!Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::PrimaryModifier))) {
468 wait_for_release = true;
469 old_meter_point = _route->meter_point ();
473 if (_route && (ev->button == 1 || Keyboard::is_button2_event (ev))) {
475 if (Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::PrimaryModifier|Keyboard::TertiaryModifier))) {
477 /* Primary+Tertiary-click applies change to all routes */
479 _session.begin_reversible_command (_("meter point change"));
480 Session::GlobalMeteringStateCommand *cmd = new Session::GlobalMeteringStateCommand (_session, this);
481 _session.foreach_route (this, &GainMeterBase::set_meter_point, next_meter_point (_route->meter_point()));
483 _session.add_command (cmd);
484 _session.commit_reversible_command ();
487 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
489 /* Primary-click: solo mix group.
490 NOTE: Primary-button2 is MIDI learn.
493 if (ev->button == 1) {
494 _session.begin_reversible_command (_("meter point change"));
495 Session::GlobalMeteringStateCommand *cmd = new Session::GlobalMeteringStateCommand (_session, this);
496 set_mix_group_meter_point (*_route, next_meter_point (_route->meter_point()));
498 _session.add_command (cmd);
499 _session.commit_reversible_command ();
504 /* click: change just this route */
508 _route->set_meter_point (next_meter_point (_route->meter_point()), this);
519 GainMeterBase::meter_release(GdkEventButton* ev)
522 if (wait_for_release){
523 wait_for_release = false;
526 set_meter_point (*_route, old_meter_point);
535 GainMeterBase::set_meter_point (Route& route, MeterPoint mp)
537 route.set_meter_point (mp, this);
541 GainMeterBase::set_mix_group_meter_point (Route& route, MeterPoint mp)
543 RouteGroup* mix_group;
545 if((mix_group = route.mix_group()) != 0){
546 mix_group->apply (&Route::set_meter_point, mp, this);
548 route.set_meter_point (mp, this);
553 GainMeterBase::meter_point_clicked ()
561 GainMeterBase::start_gain_touch (GdkEventButton* ev)
563 _gain_control->start_touch ();
568 GainMeterBase::end_gain_touch (GdkEventButton* ev)
570 _gain_control->stop_touch ();
575 GainMeterBase::gain_automation_state_button_event (GdkEventButton *ev)
577 if (ev->type == GDK_BUTTON_RELEASE) {
581 switch (ev->button) {
583 gain_astate_menu.popup (1, ev->time);
593 GainMeterBase::gain_automation_style_button_event (GdkEventButton *ev)
595 if (ev->type == GDK_BUTTON_RELEASE) {
599 switch (ev->button) {
601 gain_astyle_menu.popup (1, ev->time);
610 GainMeterBase::astate_string (AutoState state)
612 return _astate_string (state, false);
616 GainMeterBase::short_astate_string (AutoState state)
618 return _astate_string (state, true);
622 GainMeterBase::_astate_string (AutoState state, bool shrt)
628 sstr = (shrt ? "M" : _("M"));
631 sstr = (shrt ? "P" : _("P"));
634 sstr = (shrt ? "T" : _("T"));
637 sstr = (shrt ? "W" : _("W"));
645 GainMeterBase::astyle_string (AutoStyle style)
647 return _astyle_string (style, false);
651 GainMeterBase::short_astyle_string (AutoStyle style)
653 return _astyle_string (style, true);
657 GainMeterBase::_astyle_string (AutoStyle style, bool shrt)
662 /* XXX it might different in different languages */
664 return (shrt ? _("Abs") : _("Abs"));
669 GainMeterBase::gain_automation_style_changed ()
673 gain_automation_style_button.set_label (astyle_string(_gain_control->alist()->automation_style()));
676 gain_automation_style_button.set_label (short_astyle_string(_gain_control->alist()->automation_style()));
682 GainMeterBase::gain_automation_state_changed ()
684 ENSURE_GUI_THREAD(mem_fun(*this, &GainMeterBase::gain_automation_state_changed));
690 gain_automation_state_button.set_label (astate_string(_gain_control->alist()->automation_state()));
693 gain_automation_state_button.set_label (short_astate_string(_gain_control->alist()->automation_state()));
697 x = (_gain_control->alist()->automation_state() != Off);
699 if (gain_automation_state_button.get_active() != x) {
700 ignore_toggle = true;
701 gain_automation_state_button.set_active (x);
702 ignore_toggle = false;
705 update_gain_sensitive ();
707 /* start watching automation so that things move */
709 gain_watching.disconnect();
712 gain_watching = ARDOUR_UI::RapidScreenUpdate.connect (mem_fun (*this, &GainMeterBase::effective_gain_display));
717 GainMeterBase::update_meters()
720 float mpeak = level_meter->update_meters();
722 if (mpeak > max_peak) {
724 if (mpeak <= -200.0f) {
725 peak_display.set_label (_("-inf"));
727 snprintf (buf, sizeof(buf), "%.1f", mpeak);
728 peak_display.set_label (buf);
732 peak_display.set_name ("MixerStripPeakDisplayPeak");
737 void GainMeterBase::color_handler(bool dpi)
739 color_changed = true;
740 dpi_changed = (dpi) ? true : false;
745 GainMeterBase::set_width (Width w, int len)
748 level_meter->setup_meters (len);
753 GainMeterBase::on_theme_changed()
755 style_changed = true;
758 GainMeter::GainMeter (Session& s)
759 : GainMeterBase (s, slider, false)
761 gain_display_box.set_homogeneous (true);
762 gain_display_box.set_spacing (2);
763 gain_display_box.pack_start (gain_display, true, true);
765 meter_metric_area.set_name ("AudioTrackMetrics");
766 set_size_request_to_display_given_text (meter_metric_area, "-50", 0, 0);
768 gain_automation_style_button.set_name ("MixerAutomationModeButton");
769 gain_automation_state_button.set_name ("MixerAutomationPlaybackButton");
771 ARDOUR_UI::instance()->tooltips().set_tip (gain_automation_state_button, _("Fader automation mode"));
772 ARDOUR_UI::instance()->tooltips().set_tip (gain_automation_style_button, _("Fader automation type"));
774 gain_automation_style_button.unset_flags (Gtk::CAN_FOCUS);
775 gain_automation_state_button.unset_flags (Gtk::CAN_FOCUS);
777 gain_automation_state_button.set_size_request(15, 15);
778 gain_automation_style_button.set_size_request(15, 15);
780 HBox* fader_centering_box = manage (new HBox);
781 fader_centering_box->pack_start (*gain_slider, true, false);
783 fader_vbox = manage (new Gtk::VBox());
784 fader_vbox->set_spacing (0);
785 fader_vbox->pack_start (*fader_centering_box, false, false, 0);
787 hbox.set_spacing (2);
788 hbox.pack_start (*fader_vbox, true, true);
792 pack_start (gain_display_box, Gtk::PACK_SHRINK);
793 pack_start (hbox, Gtk::PACK_SHRINK);
795 meter_metric_area.signal_expose_event().connect (mem_fun(*this, &GainMeter::meter_metrics_expose));
799 GainMeter::set_controls (boost::shared_ptr<Route> r,
800 boost::shared_ptr<PeakMeter> meter,
801 boost::shared_ptr<AutomationControl> gain_control,
802 boost::shared_ptr<Automatable> gc_owner)
804 if (level_meter->get_parent()) {
805 hbox.remove (*level_meter);
808 if (peak_display.get_parent()) {
809 gain_display_box.remove (peak_display);
812 if (gain_automation_state_button.get_parent()) {
813 fader_vbox->remove (gain_automation_state_button);
816 GainMeterBase::set_controls (r, meter, gain_control, gc_owner);
819 if we have a non-hidden route (ie. we're not the click or the auditioner),
820 pack some route-dependent stuff.
823 gain_display_box.pack_end (peak_display, true, true);
824 hbox.pack_end (*level_meter, true, true);
826 if (!r->is_hidden()) {
827 fader_vbox->pack_start (gain_automation_state_button, false, false, 0);
832 GainMeter::get_gm_width ()
835 hbox.size_request (sz);
839 Glib::RefPtr<Gdk::Pixmap>
840 GainMeter::render_metrics (Gtk::Widget& w)
842 Glib::RefPtr<Gdk::Window> win (w.get_window());
843 Glib::RefPtr<Gdk::GC> fg_gc (w.get_style()->get_fg_gc (Gtk::STATE_NORMAL));
844 Glib::RefPtr<Gdk::GC> bg_gc (w.get_style()->get_bg_gc (Gtk::STATE_NORMAL));
846 int db_points[] = { -50, -40, -20, -30, -10, -3, 0, 4 };
849 win->get_size (width, height);
851 Glib::RefPtr<Gdk::Pixmap> pixmap = Gdk::Pixmap::create (win, width, height);
853 metric_pixmaps[w.get_name()] = pixmap;
855 pixmap->draw_rectangle (bg_gc, true, 0, 0, width, height);
857 Glib::RefPtr<Pango::Layout> layout = w.create_pango_layout("");
859 for (uint32_t i = 0; i < sizeof (db_points)/sizeof (db_points[0]); ++i) {
861 float fraction = log_meter (db_points[i]);
862 gint pos = height - (gint) floor (height * fraction);
864 snprintf (buf, sizeof (buf), "%d", abs (db_points[i]));
866 layout->set_text (buf);
868 /* we want logical extents, not ink extents here */
871 layout->get_pixel_size (width, height);
873 pixmap->draw_line (fg_gc, 0, pos, 4, pos);
874 pixmap->draw_layout (fg_gc, 6, pos - (height/2), layout);
881 GainMeter::meter_metrics_expose (GdkEventExpose *ev)
883 static Glib::RefPtr<Gtk::Style> meter_style;
885 meter_style = meter_metric_area.get_style();
887 Glib::RefPtr<Gdk::Window> win (meter_metric_area.get_window());
888 Glib::RefPtr<Gdk::GC> bg_gc (meter_style->get_bg_gc (Gtk::STATE_INSENSITIVE));
889 GdkRectangle base_rect;
890 GdkRectangle draw_rect;
893 win->get_size (width, height);
895 base_rect.width = width;
896 base_rect.height = height;
900 Glib::RefPtr<Gdk::Pixmap> pixmap;
901 std::map<string,Glib::RefPtr<Gdk::Pixmap> >::iterator i = metric_pixmaps.find (meter_metric_area.get_name());
903 if (i == metric_pixmaps.end() || style_changed || dpi_changed) {
904 pixmap = render_metrics (meter_metric_area);
909 gdk_rectangle_intersect (&ev->area, &base_rect, &draw_rect);
910 win->draw_drawable (bg_gc, pixmap, draw_rect.x, draw_rect.y, draw_rect.x, draw_rect.y, draw_rect.width, draw_rect.height);
911 style_changed = false;
915 boost::shared_ptr<PBD::Controllable>
916 GainMeterBase::get_controllable()
918 return _gain_control;