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 <gtkmm2ext/utils.h>
30 #include <gtkmm2ext/fastmeter.h>
31 #include <gtkmm2ext/stop_signal.h>
32 #include <gtkmm2ext/barcontroller.h>
33 #include <midi++/manager.h>
34 #include <pbd/fastlog.h>
36 #include "ardour_ui.h"
37 #include "gain_meter.h"
40 #include "gui_thread.h"
42 #include "public_editor.h"
44 #include <ardour/session.h>
45 #include <ardour/route.h>
49 using namespace ARDOUR;
51 using namespace Gtkmm2ext;
56 sigc::signal<void> GainMeterBase::ResetAllPeakDisplays;
57 sigc::signal<void,RouteGroup*> GainMeterBase::ResetGroupPeakDisplays;
59 map<string,Glib::RefPtr<Gdk::Pixmap> > GainMeter::metric_pixmaps;
60 Glib::RefPtr<Gdk::Pixbuf> GainMeter::slider;
64 GainMeter::setup_slider_pix ()
66 if ((slider = ::get_icon ("fader_belt")) == 0) {
67 throw failed_constructor();
71 GainMeterBase::GainMeterBase (boost::shared_ptr<IO> io, Session& s,
72 const Glib::RefPtr<Gdk::Pixbuf>& pix,
76 // 0.781787 is the value needed for gain to be set to 0.
77 gain_adjustment (0.781787, 0.0, 1.0, 0.01, 0.1),
78 gain_automation_style_button (""),
79 gain_automation_state_button ("")
82 ignore_toggle = false;
84 next_release_selects = false;
88 gain_slider = manage (new HSliderController (pix,
93 gain_slider = manage (new VSliderController (pix,
99 level_meter = new LevelMeter(_io, _session);
101 gain_slider->signal_button_press_event().connect (mem_fun(*this, &GainMeter::start_gain_touch));
102 gain_slider->signal_button_release_event().connect (mem_fun(*this, &GainMeter::end_gain_touch));
103 gain_slider->set_name ("GainFader");
105 gain_display.set_name ("MixerStripGainDisplay");
106 gain_display.set_has_frame (false);
107 set_size_request_to_display_given_text (gain_display, "-80.g", 2, 6); /* note the descender */
108 gain_display.signal_activate().connect (mem_fun (*this, &GainMeter::gain_activated));
109 gain_display.signal_focus_in_event().connect (mem_fun (*this, &GainMeter::gain_focused), false);
110 gain_display.signal_focus_out_event().connect (mem_fun (*this, &GainMeter::gain_focused), false);
112 peak_display.set_name ("MixerStripPeakDisplay");
113 // peak_display.set_has_frame (false);
114 // peak_display.set_editable (false);
115 set_size_request_to_display_given_text (peak_display, "-80.g", 2, 6); /* note the descender */
116 max_peak = minus_infinity();
117 peak_display.set_label (_("-inf"));
118 peak_display.unset_flags (Gtk::CAN_FOCUS);
120 gain_automation_style_button.set_name ("MixerAutomationModeButton");
121 gain_automation_state_button.set_name ("MixerAutomationPlaybackButton");
123 ARDOUR_UI::instance()->tooltips().set_tip (gain_automation_state_button, _("Fader automation mode"));
124 ARDOUR_UI::instance()->tooltips().set_tip (gain_automation_style_button, _("Fader automation type"));
126 gain_automation_style_button.unset_flags (Gtk::CAN_FOCUS);
127 gain_automation_state_button.unset_flags (Gtk::CAN_FOCUS);
129 gain_automation_state_button.set_size_request(15, 15);
130 gain_automation_style_button.set_size_request(15, 15);
134 if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
138 using namespace Menu_Helpers;
140 gain_astate_menu.items().push_back (MenuElem (_("Manual"),
141 bind (mem_fun (*_io, &IO::set_gain_automation_state), (AutoState) Off)));
142 gain_astate_menu.items().push_back (MenuElem (_("Play"),
143 bind (mem_fun (*_io, &IO::set_gain_automation_state), (AutoState) Play)));
144 gain_astate_menu.items().push_back (MenuElem (_("Write"),
145 bind (mem_fun (*_io, &IO::set_gain_automation_state), (AutoState) Write)));
146 gain_astate_menu.items().push_back (MenuElem (_("Touch"),
147 bind (mem_fun (*_io, &IO::set_gain_automation_state), (AutoState) Touch)));
149 gain_astyle_menu.items().push_back (MenuElem (_("Trim")));
150 gain_astyle_menu.items().push_back (MenuElem (_("Abs")));
152 gain_astate_menu.set_name ("ArdourContextMenu");
153 gain_astyle_menu.set_name ("ArdourContextMenu");
155 gain_automation_style_button.signal_button_press_event().connect (mem_fun(*this, &GainMeterBase::gain_automation_style_button_event), false);
156 gain_automation_state_button.signal_button_press_event().connect (mem_fun(*this, &GainMeterBase::gain_automation_state_button_event), false);
158 r->gain_automation_curve().automation_state_changed.connect (mem_fun(*this, &GainMeterBase::gain_automation_state_changed));
159 r->gain_automation_curve().automation_style_changed.connect (mem_fun(*this, &GainMeterBase::gain_automation_style_changed));
161 gain_automation_state_changed ();
165 _io->gain_changed.connect (mem_fun(*this, &GainMeterBase::gain_changed));
167 gain_adjustment.signal_value_changed().connect (mem_fun(*this, &GainMeterBase::gain_adjusted));
168 peak_display.signal_button_release_event().connect (mem_fun(*this, &GainMeterBase::peak_button_release), false);
169 gain_display.signal_key_press_event().connect (mem_fun(*this, &GainMeterBase::gain_key_press), false);
174 update_gain_sensitive ();
176 ResetAllPeakDisplays.connect (mem_fun(*this, &GainMeterBase::reset_peak_display));
177 ResetGroupPeakDisplays.connect (mem_fun(*this, &GainMeterBase::reset_group_peak_display));
179 UI::instance()->theme_changed.connect (mem_fun(*this, &GainMeterBase::on_theme_changed));
180 ColorsChanged.connect (bind(mem_fun (*this, &GainMeterBase::color_handler), false));
181 DPIReset.connect (bind(mem_fun (*this, &GainMeterBase::color_handler), true));
184 GainMeterBase::~GainMeterBase ()
196 GainMeterBase::hide_all_meters ()
198 level_meter->hide_meters();
202 GainMeter::hide_all_meters ()
204 bool remove_metric_area = false;
206 GainMeterBase::hide_all_meters ();
208 if (remove_metric_area) {
209 if (meter_metric_area.get_parent()) {
210 level_meter->remove (meter_metric_area);
216 GainMeterBase::setup_meters (int len)
218 level_meter->setup_meters(len, 5);
222 GainMeter::setup_meters (int len)
224 if (!meter_metric_area.get_parent()) {
225 level_meter->pack_end (meter_metric_area, false, false);
226 meter_metric_area.show_all ();
228 GainMeterBase::setup_meters (len);
232 GainMeterBase::gain_key_press (GdkEventKey* ev)
234 if (key_is_legal_for_numeric_entry (ev->keyval)) {
235 /* drop through to normal handling */
238 /* illegal key for gain entry */
243 GainMeterBase::peak_button_release (GdkEventButton* ev)
245 /* reset peak label */
247 if (ev->button == 1 && Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier|Keyboard::TertiaryModifier)) {
248 ResetAllPeakDisplays ();
249 } else if (ev->button == 1 && Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
251 if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
252 ResetGroupPeakDisplays (r->mix_group());
255 reset_peak_display ();
262 GainMeterBase::reset_peak_display ()
265 if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
266 r->reset_max_peak_meters();
269 level_meter->clear_meters();
270 max_peak = -INFINITY;
271 peak_display.set_label (_("-Inf"));
272 peak_display.set_name ("MixerStripPeakDisplay");
276 GainMeterBase::reset_group_peak_display (RouteGroup* group)
279 if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
280 if (group == r->mix_group()) {
281 reset_peak_display ();
287 GainMeterBase::popup_meter_menu (GdkEventButton *ev)
289 using namespace Menu_Helpers;
291 if (meter_menu == 0) {
292 meter_menu = new Gtk::Menu;
293 MenuList& items = meter_menu->items();
295 items.push_back (MenuElem ("-inf .. +0dBFS"));
296 items.push_back (MenuElem ("-10dB .. +0dBFS"));
297 items.push_back (MenuElem ("-4 .. +0dBFS"));
298 items.push_back (SeparatorElem());
299 items.push_back (MenuElem ("-inf .. -2dBFS"));
300 items.push_back (MenuElem ("-10dB .. -2dBFS"));
301 items.push_back (MenuElem ("-4 .. -2dBFS"));
304 meter_menu->popup (1, ev->time);
308 GainMeterBase::gain_focused (GdkEventFocus* ev)
311 gain_display.select_region (0, -1);
313 gain_display.select_region (0, 0);
319 GainMeterBase::gain_activated ()
323 if (sscanf (gain_display.get_text().c_str(), "%f", &f) == 1) {
325 /* clamp to displayable values */
329 _io->set_gain (dB_to_coefficient(f), this);
331 if (gain_display.has_focus()) {
332 PublicEditor::instance().reset_focus();
338 GainMeterBase::show_gain ()
342 float v = gain_adjustment.get_value();
345 strcpy (buf, _("-inf"));
347 snprintf (buf, 32, "%.1f", coefficient_to_dB (slider_position_to_gain (v)));
350 gain_display.set_text (buf);
354 GainMeterBase::gain_adjusted ()
356 if (!ignore_toggle) {
357 _io->set_gain (slider_position_to_gain (gain_adjustment.get_value()), this);
363 GainMeterBase::effective_gain_display ()
365 gfloat value = gain_to_slider_position (_io->effective_gain());
367 if (gain_adjustment.get_value() != value) {
368 ignore_toggle = true;
369 gain_adjustment.set_value (value);
370 ignore_toggle = false;
375 GainMeterBase::gain_changed (void *src)
377 Gtkmm2ext::UI::instance()->call_slot (mem_fun(*this, &GainMeterBase::effective_gain_display));
381 GainMeterBase::set_meter_strip_name (const char * name)
383 meter_metric_area.set_name (name);
384 style_changed = true;
388 GainMeterBase::set_fader_name (const char * name)
390 gain_slider->set_name (name);
394 GainMeterBase::update_gain_sensitive ()
396 static_cast<Gtkmm2ext::SliderController*>(gain_slider)->set_sensitive (!(_io->gain_automation_state() & Play));
401 next_meter_point (MeterPoint mp)
405 return MeterPreFader;
409 return MeterPostFader;
421 GainMeterBase::meter_press(GdkEventButton* ev)
425 wait_for_release = false;
427 if ((_route = dynamic_cast<Route*>(_io.get())) == 0) {
431 if (!ignore_toggle) {
433 if (Keyboard::is_context_menu_event (ev)) {
435 // no menu at this time.
439 if (ev->button == 2) {
441 // Primary-button2 click is the midi binding click
442 // button2-click is "momentary"
444 if (!Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::PrimaryModifier))) {
445 wait_for_release = true;
446 old_meter_point = _route->meter_point ();
450 if (ev->button == 1 || ev->button == 2) {
452 if (Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::PrimaryModifier|Keyboard::TertiaryModifier))) {
454 /* Primary+Tertiary-click applies change to all routes */
456 _session.begin_reversible_command (_("meter point change"));
457 Session::GlobalMeteringStateCommand *cmd = new Session::GlobalMeteringStateCommand (_session, this);
458 _session.foreach_route (this, &GainMeterBase::set_meter_point, next_meter_point (_route->meter_point()));
460 _session.add_command (cmd);
461 _session.commit_reversible_command ();
464 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
466 /* Primary-click: solo mix group.
467 NOTE: Primary-button2 is MIDI learn.
470 if (ev->button == 1) {
471 _session.begin_reversible_command (_("meter point change"));
472 Session::GlobalMeteringStateCommand *cmd = new Session::GlobalMeteringStateCommand (_session, this);
473 set_mix_group_meter_point (*_route, next_meter_point (_route->meter_point()));
475 _session.add_command (cmd);
476 _session.commit_reversible_command ();
481 /* click: change just this route */
485 _route->set_meter_point (next_meter_point (_route->meter_point()), this);
496 GainMeterBase::meter_release(GdkEventButton* ev)
500 if (wait_for_release){
501 wait_for_release = false;
502 set_meter_point (*(dynamic_cast<Route*>(_io.get())), old_meter_point);
509 GainMeterBase::set_meter_point (Route& route, MeterPoint mp)
511 route.set_meter_point (mp, this);
515 GainMeterBase::set_mix_group_meter_point (Route& route, MeterPoint mp)
517 RouteGroup* mix_group;
519 if((mix_group = route.mix_group()) != 0){
520 mix_group->apply (&Route::set_meter_point, mp, this);
522 route.set_meter_point (mp, this);
527 GainMeterBase::meter_point_clicked ()
531 if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
537 GainMeterBase::start_gain_touch (GdkEventButton* ev)
539 _io->start_gain_touch ();
544 GainMeterBase::end_gain_touch (GdkEventButton* ev)
546 _io->end_gain_touch ();
551 GainMeterBase::gain_automation_state_button_event (GdkEventButton *ev)
553 if (ev->type == GDK_BUTTON_RELEASE) {
557 switch (ev->button) {
559 gain_astate_menu.popup (1, ev->time);
569 GainMeterBase::gain_automation_style_button_event (GdkEventButton *ev)
571 if (ev->type == GDK_BUTTON_RELEASE) {
575 switch (ev->button) {
577 gain_astyle_menu.popup (1, ev->time);
586 GainMeterBase::astate_string (AutoState state)
588 return _astate_string (state, false);
592 GainMeterBase::short_astate_string (AutoState state)
594 return _astate_string (state, true);
598 GainMeterBase::_astate_string (AutoState state, bool shrt)
604 sstr = (shrt ? "M" : _("M"));
607 sstr = (shrt ? "P" : _("P"));
610 sstr = (shrt ? "T" : _("T"));
613 sstr = (shrt ? "W" : _("W"));
621 GainMeterBase::astyle_string (AutoStyle style)
623 return _astyle_string (style, false);
627 GainMeterBase::short_astyle_string (AutoStyle style)
629 return _astyle_string (style, true);
633 GainMeterBase::_astyle_string (AutoStyle style, bool shrt)
638 /* XXX it might different in different languages */
640 return (shrt ? _("Abs") : _("Abs"));
645 GainMeterBase::gain_automation_style_changed ()
647 // Route* _route = dynamic_cast<Route*>(&_io);
650 gain_automation_style_button.set_label (astyle_string(_io->gain_automation_curve().automation_style()));
653 gain_automation_style_button.set_label (short_astyle_string(_io->gain_automation_curve().automation_style()));
659 GainMeterBase::gain_automation_state_changed ()
661 ENSURE_GUI_THREAD(mem_fun(*this, &GainMeterBase::gain_automation_state_changed));
667 gain_automation_state_button.set_label (astate_string(_io->gain_automation_curve().automation_state()));
670 gain_automation_state_button.set_label (short_astate_string(_io->gain_automation_curve().automation_state()));
674 x = (_io->gain_automation_state() != Off);
676 if (gain_automation_state_button.get_active() != x) {
677 ignore_toggle = true;
678 gain_automation_state_button.set_active (x);
679 ignore_toggle = false;
682 update_gain_sensitive ();
684 /* start watching automation so that things move */
686 gain_watching.disconnect();
689 gain_watching = ARDOUR_UI::RapidScreenUpdate.connect (mem_fun (*this, &GainMeterBase::effective_gain_display));
694 GainMeterBase::update_meters()
697 float mpeak = level_meter->update_meters();
699 if (mpeak > max_peak) {
701 if (mpeak <= -200.0f) {
702 peak_display.set_label (_("-inf"));
704 snprintf (buf, sizeof(buf), "%.1f", mpeak);
705 peak_display.set_label (buf);
709 peak_display.set_name ("MixerStripPeakDisplayPeak");
714 void GainMeterBase::color_handler(bool dpi)
716 color_changed = true;
717 dpi_changed = (dpi) ? true : false;
722 GainMeterBase::set_width (Width w, int len)
725 level_meter->setup_meters (len);
730 GainMeterBase::on_theme_changed()
732 style_changed = true;
735 GainMeter::GainMeter (boost::shared_ptr<IO> io, Session& s)
736 : GainMeterBase (io, s, slider, false)
739 gain_display_box.set_homogeneous (true);
740 gain_display_box.set_spacing (2);
741 gain_display_box.pack_start (gain_display, true, true);
743 meter_metric_area.set_name ("AudioTrackMetrics");
744 set_size_request_to_display_given_text (meter_metric_area, "-50", 0, 0);
746 gain_automation_style_button.set_name ("MixerAutomationModeButton");
747 gain_automation_state_button.set_name ("MixerAutomationPlaybackButton");
749 ARDOUR_UI::instance()->tooltips().set_tip (gain_automation_state_button, _("Fader automation mode"));
750 ARDOUR_UI::instance()->tooltips().set_tip (gain_automation_style_button, _("Fader automation type"));
752 gain_automation_style_button.unset_flags (Gtk::CAN_FOCUS);
753 gain_automation_state_button.unset_flags (Gtk::CAN_FOCUS);
755 gain_automation_state_button.set_size_request(15, 15);
756 gain_automation_style_button.set_size_request(15, 15);
758 HBox* fader_centering_box = manage (new HBox);
759 fader_centering_box->pack_start (*gain_slider, true, false);
761 fader_vbox = manage (new Gtk::VBox());
762 fader_vbox->set_spacing (0);
763 fader_vbox->pack_start (*fader_centering_box, false, false, 0);
765 hbox.set_spacing (2);
766 hbox.pack_start (*fader_vbox, true, true);
770 if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
773 if we have a non-hidden route (ie. we're not the click or the auditioner),
774 pack some route-dependent stuff.
777 gain_display_box.pack_end (peak_display, true, true);
778 hbox.pack_end (*level_meter, true, true);
781 fader_vbox->pack_start (gain_automation_state_button, false, false, 0);
787 pack_start (gain_display_box, Gtk::PACK_SHRINK);
788 pack_start (hbox, Gtk::PACK_SHRINK);
790 meter_metric_area.signal_expose_event().connect (mem_fun(*this, &GainMeter::meter_metrics_expose));
795 GainMeter::get_gm_width ()
798 hbox.size_request (sz);
802 Glib::RefPtr<Gdk::Pixmap>
803 GainMeter::render_metrics (Gtk::Widget& w)
805 Glib::RefPtr<Gdk::Window> win (w.get_window());
806 Glib::RefPtr<Gdk::GC> fg_gc (w.get_style()->get_fg_gc (Gtk::STATE_NORMAL));
807 Glib::RefPtr<Gdk::GC> bg_gc (w.get_style()->get_bg_gc (Gtk::STATE_NORMAL));
809 int db_points[] = { -50, -40, -20, -30, -10, -3, 0, 4 };
812 win->get_size (width, height);
814 Glib::RefPtr<Gdk::Pixmap> pixmap = Gdk::Pixmap::create (win, width, height);
816 metric_pixmaps[w.get_name()] = pixmap;
818 pixmap->draw_rectangle (bg_gc, true, 0, 0, width, height);
820 Glib::RefPtr<Pango::Layout> layout = w.create_pango_layout("");
822 for (uint32_t i = 0; i < sizeof (db_points)/sizeof (db_points[0]); ++i) {
824 float fraction = log_meter (db_points[i]);
825 gint pos = height - (gint) floor (height * fraction);
827 snprintf (buf, sizeof (buf), "%d", abs (db_points[i]));
829 layout->set_text (buf);
831 /* we want logical extents, not ink extents here */
834 layout->get_pixel_size (width, height);
836 pixmap->draw_line (fg_gc, 0, pos, 4, pos);
837 pixmap->draw_layout (fg_gc, 6, pos - (height/2), layout);
844 GainMeter::meter_metrics_expose (GdkEventExpose *ev)
846 static Glib::RefPtr<Gtk::Style> meter_style;
848 meter_style = meter_metric_area.get_style();
850 Glib::RefPtr<Gdk::Window> win (meter_metric_area.get_window());
851 Glib::RefPtr<Gdk::GC> bg_gc (meter_style->get_bg_gc (Gtk::STATE_INSENSITIVE));
852 GdkRectangle base_rect;
853 GdkRectangle draw_rect;
856 win->get_size (width, height);
858 base_rect.width = width;
859 base_rect.height = height;
863 Glib::RefPtr<Gdk::Pixmap> pixmap;
864 std::map<string,Glib::RefPtr<Gdk::Pixmap> >::iterator i = metric_pixmaps.find (meter_metric_area.get_name());
866 if (i == metric_pixmaps.end() || style_changed || dpi_changed) {
867 pixmap = render_metrics (meter_metric_area);
872 gdk_rectangle_intersect (&ev->area, &base_rect, &draw_rect);
873 win->draw_drawable (bg_gc, pixmap, draw_rect.x, draw_rect.y, draw_rect.x, draw_rect.y, draw_rect.width, draw_rect.height);
874 style_changed = false;