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> GainMeter::ResetAllPeakDisplays;
57 sigc::signal<void,RouteGroup*> GainMeter::ResetGroupPeakDisplays;
58 Glib::RefPtr<Gdk::Pixbuf> GainMeter::slider;
59 Glib::RefPtr<Gdk::Pixbuf> GainMeter::rail;
60 map<string,Glib::RefPtr<Gdk::Pixmap> > GainMeter::metric_pixmaps;
63 GainMeter::setup_slider_pix ()
65 slider = ::get_icon ("fader_belt");
69 GainMeter::GainMeter (boost::shared_ptr<IO> io, Session& s)
73 // 0.781787 is the value needed for gain to be set to 0.
74 gain_adjustment (0.781787, 0.0, 1.0, 0.01, 0.1),
75 gain_automation_style_button (""),
76 gain_automation_state_button ("")
83 ignore_toggle = false;
85 next_release_selects = false;
88 gain_slider = manage (new VSliderController (slider,
93 gain_slider->signal_button_press_event().connect (mem_fun(*this, &GainMeter::start_gain_touch));
94 gain_slider->signal_button_release_event().connect (mem_fun(*this, &GainMeter::end_gain_touch));
95 gain_slider->set_name ("GainFader");
97 gain_display.set_name ("MixerStripGainDisplay");
98 gain_display.set_has_frame (false);
99 set_size_request_to_display_given_text (gain_display, "-80.g", 2, 6); /* note the descender */
100 gain_display.signal_activate().connect (mem_fun (*this, &GainMeter::gain_activated));
101 gain_display.signal_focus_in_event().connect (mem_fun (*this, &GainMeter::gain_focused), false);
102 gain_display.signal_focus_out_event().connect (mem_fun (*this, &GainMeter::gain_focused), false);
104 gain_display_box.set_homogeneous (true);
105 gain_display_box.set_spacing (2);
106 gain_display_box.pack_start (gain_display, true, true);
108 peak_display.set_name ("MixerStripPeakDisplay");
109 // peak_display.set_has_frame (false);
110 // peak_display.set_editable (false);
111 set_size_request_to_display_given_text (peak_display, "-80.g", 2, 6); /* note the descender */
112 max_peak = minus_infinity();
113 peak_display.set_label (_("-inf"));
114 peak_display.unset_flags (Gtk::CAN_FOCUS);
116 meter_metric_area.set_name ("AudioTrackMetrics");
117 set_size_request_to_display_given_text (meter_metric_area, "-50", 0, 0);
119 meter_packer.set_spacing (2);
121 gain_automation_style_button.set_name ("MixerAutomationModeButton");
122 gain_automation_state_button.set_name ("MixerAutomationPlaybackButton");
124 ARDOUR_UI::instance()->tooltips().set_tip (gain_automation_state_button, _("Fader automation mode"));
125 ARDOUR_UI::instance()->tooltips().set_tip (gain_automation_style_button, _("Fader automation type"));
127 gain_automation_style_button.unset_flags (Gtk::CAN_FOCUS);
128 gain_automation_state_button.unset_flags (Gtk::CAN_FOCUS);
130 gain_automation_state_button.set_size_request(15, 15);
131 gain_automation_style_button.set_size_request(15, 15);
133 HBox* fader_centering_box = manage (new HBox);
134 fader_centering_box->pack_start (*gain_slider, true, false);
136 fader_vbox = manage (new Gtk::VBox());
137 fader_vbox->set_spacing (0);
138 fader_vbox->pack_start (*fader_centering_box, false, false, 0);
140 hbox.set_spacing (2);
141 hbox.pack_start (*fader_vbox, true, true);
147 if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
150 if we have a non-hidden route (ie. we're not the click or the auditioner),
151 pack some route-dependent stuff.
154 gain_display_box.pack_end (peak_display, true, true);
155 hbox.pack_end (meter_packer, true, true);
159 using namespace Menu_Helpers;
161 gain_astate_menu.items().push_back (MenuElem (_("Manual"),
162 bind (mem_fun (*_io, &IO::set_gain_automation_state), (AutoState) Off)));
163 gain_astate_menu.items().push_back (MenuElem (_("Play"),
164 bind (mem_fun (*_io, &IO::set_gain_automation_state), (AutoState) Play)));
165 gain_astate_menu.items().push_back (MenuElem (_("Write"),
166 bind (mem_fun (*_io, &IO::set_gain_automation_state), (AutoState) Write)));
167 gain_astate_menu.items().push_back (MenuElem (_("Touch"),
168 bind (mem_fun (*_io, &IO::set_gain_automation_state), (AutoState) Touch)));
170 gain_astyle_menu.items().push_back (MenuElem (_("Trim")));
171 gain_astyle_menu.items().push_back (MenuElem (_("Abs")));
173 gain_astate_menu.set_name ("ArdourContextMenu");
174 gain_astyle_menu.set_name ("ArdourContextMenu");
176 gain_automation_style_button.signal_button_press_event().connect (mem_fun(*this, &GainMeter::gain_automation_style_button_event), false);
177 gain_automation_state_button.signal_button_press_event().connect (mem_fun(*this, &GainMeter::gain_automation_state_button_event), false);
179 r->gain_automation_curve().automation_state_changed.connect (mem_fun(*this, &GainMeter::gain_automation_state_changed));
180 r->gain_automation_curve().automation_style_changed.connect (mem_fun(*this, &GainMeter::gain_automation_style_changed));
182 fader_vbox->pack_start (gain_automation_state_button, false, false, 0);
183 gain_automation_state_changed ();
189 pack_start (gain_display_box, Gtk::PACK_SHRINK);
190 pack_start (hbox, Gtk::PACK_SHRINK);
192 _io->gain_changed.connect (mem_fun(*this, &GainMeter::gain_changed));
194 meter_metric_area.signal_expose_event().connect (mem_fun(*this, &GainMeter::meter_metrics_expose));
195 gain_adjustment.signal_value_changed().connect (mem_fun(*this, &GainMeter::gain_adjusted));
196 peak_display.signal_button_release_event().connect (mem_fun(*this, &GainMeter::peak_button_release), false);
197 gain_display.signal_key_press_event().connect (mem_fun(*this, &GainMeter::gain_key_press), false);
199 Config->ParameterChanged.connect (mem_fun (*this, &GainMeter::parameter_changed));
204 update_gain_sensitive ();
206 ResetAllPeakDisplays.connect (mem_fun(*this, &GainMeter::reset_peak_display));
207 ResetGroupPeakDisplays.connect (mem_fun(*this, &GainMeter::reset_group_peak_display));
209 UI::instance()->theme_changed.connect (mem_fun(*this, &GainMeter::on_theme_changed));
213 GainMeter::set_width (Width w)
228 Glib::RefPtr<Gdk::Pixmap>
229 GainMeter::render_metrics (Gtk::Widget& w)
231 //cerr << "name: " << w.get_name();//DEBUG
232 //cerr << " bg_red = " << w.get_style()->get_bg(Gtk::STATE_NORMAL).get_red();//DEBUG
233 //cerr << " fg_red = " << w.get_style()->get_fg(Gtk::STATE_NORMAL).get_red();//DEBUG
234 Glib::RefPtr<Gdk::Window> win (w.get_window());
235 Glib::RefPtr<Gdk::GC> fg_gc (w.get_style()->get_fg_gc (Gtk::STATE_NORMAL));
236 Glib::RefPtr<Gdk::GC> bg_gc (w.get_style()->get_bg_gc (Gtk::STATE_NORMAL));
238 int db_points[] = { -50, -40, -20, -30, -10, -3, 0, 4 };
241 win->get_size (width, height);
243 Glib::RefPtr<Gdk::Pixmap> pixmap = Gdk::Pixmap::create (win, width, height);
245 metric_pixmaps[w.get_name()] = pixmap;
247 pixmap->draw_rectangle (bg_gc, true, 0, 0, width, height);
249 Glib::RefPtr<Pango::Layout> layout = w.create_pango_layout("");
251 for (uint32_t i = 0; i < sizeof (db_points)/sizeof (db_points[0]); ++i) {
253 float fraction = log_meter (db_points[i]);
254 gint pos = height - (gint) floor (height * fraction);
256 snprintf (buf, sizeof (buf), "%d", abs (db_points[i]));
258 layout->set_text (buf);
260 /* we want logical extents, not ink extents here */
263 layout->get_pixel_size (width, height);
265 pixmap->draw_line (fg_gc, 0, pos, 4, pos);
266 pixmap->draw_layout (fg_gc, 6, pos - (height/2), layout);
273 GainMeter::meter_metrics_expose (GdkEventExpose *ev)
275 static Glib::RefPtr<Gtk::Style> meter_style;
276 //cerr << "GainMeter::meter_metrics_expose() called on " << meter_metric_area.get_name();//DEBUG
277 //cerr << " style_changed = " << style_changed;//DEBUG
279 meter_style = meter_metric_area.get_style();
280 // cerr << " get_style() called ";//DEBUG
282 Glib::RefPtr<Gdk::Window> win (meter_metric_area.get_window());
283 //Glib::RefPtr<Gdk::GC> fg_gc (meter_style->get_fg_gc (Gtk::STATE_NORMAL));
284 Glib::RefPtr<Gdk::GC> bg_gc (meter_style->get_bg_gc (Gtk::STATE_INSENSITIVE));
285 GdkRectangle base_rect;
286 GdkRectangle draw_rect;
289 win->get_size (width, height);
291 base_rect.width = width;
292 base_rect.height = height;
296 Glib::RefPtr<Gdk::Pixmap> pixmap;
297 std::map<string,Glib::RefPtr<Gdk::Pixmap> >::iterator i = metric_pixmaps.find (meter_metric_area.get_name());
299 if (i == metric_pixmaps.end() || style_changed) {
300 pixmap = render_metrics (meter_metric_area);
303 // cerr << " re-used " << i->first;//DEBUG
306 gdk_rectangle_intersect (&ev->area, &base_rect, &draw_rect);
307 //win->draw_rectangle (bg_gc, false, draw_rect.x, draw_rect.y, draw_rect.width, draw_rect.height);
308 win->draw_drawable (bg_gc, pixmap, draw_rect.x, draw_rect.y, draw_rect.x, draw_rect.y, draw_rect.width, draw_rect.height);
309 //cerr << endl;//DEBUG
310 style_changed = false;
315 GainMeter::on_theme_changed()
317 style_changed = true;
320 GainMeter::~GainMeter ()
326 for (vector<MeterInfo>::iterator i = meters.begin(); i != meters.end(); i++) {
334 GainMeter::update_meters ()
336 vector<MeterInfo>::iterator i;
341 for (n = 0, i = meters.begin(); i != meters.end(); ++i, ++n) {
343 peak = _io->peak_input_power (n);
345 (*i).meter->set (log_meter (peak));
347 mpeak = _io->max_peak_power(n);
349 if (mpeak > max_peak) {
351 /* set peak display */
352 if (max_peak <= -200.0f) {
353 peak_display.set_label (_("-inf"));
355 snprintf (buf, sizeof(buf), "%.1f", max_peak);
356 peak_display.set_label (buf);
359 if (max_peak >= 0.0f) {
360 peak_display.set_name ("MixerStripPeakDisplayPeak");
368 GainMeter::parameter_changed(const char* parameter_name)
370 #define PARAM_IS(x) (!strcmp (parameter_name, (x)))
372 ENSURE_GUI_THREAD (bind (mem_fun(*this, &GainMeter::parameter_changed), parameter_name));
374 if (PARAM_IS ("meter-hold")) {
376 vector<MeterInfo>::iterator i;
379 for (n = 0, i = meters.begin(); i != meters.end(); ++i, ++n) {
381 (*i).meter->set_hold_count ((uint32_t) floor(Config->get_meter_hold()));
389 GainMeter::hide_all_meters ()
391 bool remove_metric_area = false;
393 for (vector<MeterInfo>::iterator i = meters.begin(); i != meters.end(); ++i) {
395 remove_metric_area = true;
396 meter_packer.remove (*((*i).meter));
401 if (remove_metric_area) {
402 if (meter_metric_area.get_parent()) {
403 meter_packer.remove (meter_metric_area);
409 GainMeter::setup_meters ()
411 uint32_t nmeters = _io->n_outputs();
418 if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
420 switch (r->meter_point()) {
423 nmeters = r->n_inputs();
426 nmeters = r->n_outputs();
432 nmeters = _io->n_outputs();
441 width = regular_meter_width;
443 width = thin_meter_width;
446 while (meters.size() < nmeters) {
447 meters.push_back (MeterInfo());
450 /* pack them backwards */
452 if (_width == Wide) {
453 meter_packer.pack_end (meter_metric_area, false, false);
454 meter_metric_area.show_all ();
457 for (int32_t n = nmeters-1; nmeters && n >= 0 ; --n) {
458 if (meters[n].width != width) {
459 delete meters[n].meter;
460 meters[n].meter = new FastMeter ((uint32_t) floor (Config->get_meter_hold()), width, FastMeter::Vertical);
461 meters[n].width = width;
463 meters[n].meter->add_events (Gdk::BUTTON_RELEASE_MASK);
464 meters[n].meter->signal_button_release_event().connect (bind (mem_fun(*this, &GainMeter::meter_button_release), n));
467 meter_packer.pack_end (*meters[n].meter, false, false);
468 meters[n].meter->show_all ();
469 meters[n].packed = true;
474 GainMeter::get_gm_width ()
477 hbox.size_request (sz);
482 GainMeter::gain_key_press (GdkEventKey* ev)
484 if (key_is_legal_for_numeric_entry (ev->keyval)) {
485 /* drop through to normal handling */
488 /* illegal key for gain entry */
493 GainMeter::peak_button_release (GdkEventButton* ev)
495 /* reset peak label */
497 if (ev->button == 1 && Keyboard::modifier_state_equals (ev->state, Keyboard::Control|Keyboard::Shift)) {
498 ResetAllPeakDisplays ();
499 } else if (ev->button == 1 && Keyboard::modifier_state_equals (ev->state, Keyboard::Control)) {
501 if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
502 ResetGroupPeakDisplays (r->mix_group());
505 reset_peak_display ();
512 GainMeter::reset_peak_display ()
515 if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
516 r->reset_max_peak_meters();
519 max_peak = -INFINITY;
520 peak_display.set_label (_("-Inf"));
521 peak_display.set_name ("MixerStripPeakDisplay");
525 GainMeter::reset_group_peak_display (RouteGroup* group)
528 if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
529 if (group == r->mix_group()) {
530 reset_peak_display ();
536 GainMeter::meter_button_release (GdkEventButton* ev, uint32_t which)
538 switch (ev->button) {
540 meters[which].meter->clear();
541 max_peak = minus_infinity();
542 peak_display.set_label (_("-inf"));
543 peak_display.set_name ("MixerStripPeakDisplay");
547 // popup_meter_menu (ev);
555 GainMeter::popup_meter_menu (GdkEventButton *ev)
557 using namespace Menu_Helpers;
559 if (meter_menu == 0) {
560 meter_menu = new Gtk::Menu;
561 MenuList& items = meter_menu->items();
563 items.push_back (MenuElem ("-inf .. +0dBFS"));
564 items.push_back (MenuElem ("-10dB .. +0dBFS"));
565 items.push_back (MenuElem ("-4 .. +0dBFS"));
566 items.push_back (SeparatorElem());
567 items.push_back (MenuElem ("-inf .. -2dBFS"));
568 items.push_back (MenuElem ("-10dB .. -2dBFS"));
569 items.push_back (MenuElem ("-4 .. -2dBFS"));
572 meter_menu->popup (1, ev->time);
576 GainMeter::gain_focused (GdkEventFocus* ev)
579 gain_display.select_region (0, -1);
581 gain_display.select_region (0, 0);
587 GainMeter::gain_activated ()
591 if (sscanf (gain_display.get_text().c_str(), "%f", &f) == 1) {
593 /* clamp to displayable values */
597 _io->set_gain (dB_to_coefficient(f), this);
599 if (gain_display.has_focus()) {
600 PublicEditor::instance().reset_focus();
606 GainMeter::show_gain ()
610 float v = gain_adjustment.get_value();
613 strcpy (buf, _("-inf"));
615 snprintf (buf, 32, "%.1f", coefficient_to_dB (slider_position_to_gain (v)));
618 gain_display.set_text (buf);
622 GainMeter::gain_adjusted ()
624 if (!ignore_toggle) {
625 _io->set_gain (slider_position_to_gain (gain_adjustment.get_value()), this);
631 GainMeter::effective_gain_display ()
633 gfloat value = gain_to_slider_position (_io->effective_gain());
635 if (gain_adjustment.get_value() != value) {
636 ignore_toggle = true;
637 gain_adjustment.set_value (value);
638 ignore_toggle = false;
643 GainMeter::gain_changed (void *src)
645 Gtkmm2ext::UI::instance()->call_slot (mem_fun(*this, &GainMeter::effective_gain_display));
649 GainMeter::set_meter_strip_name (const char * name)
651 meter_metric_area.set_name (name);
652 style_changed = true;
656 GainMeter::set_fader_name (const char * name)
658 gain_slider->set_name (name);
662 GainMeter::update_gain_sensitive ()
664 static_cast<Gtkmm2ext::SliderController*>(gain_slider)->set_sensitive (!(_io->gain_automation_state() & Play));
669 next_meter_point (MeterPoint mp)
673 return MeterPreFader;
677 return MeterPostFader;
689 GainMeter::meter_press(GdkEventButton* ev)
693 wait_for_release = false;
695 if ((_route = dynamic_cast<Route*>(_io.get())) == 0) {
699 if (!ignore_toggle) {
701 if (Keyboard::is_context_menu_event (ev)) {
703 // no menu at this time.
707 if (ev->button == 2) {
709 // ctrl-button2 click is the midi binding click
710 // button2-click is "momentary"
712 if (!Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::Control))) {
713 wait_for_release = true;
714 old_meter_point = _route->meter_point ();
718 if (ev->button == 1 || ev->button == 2) {
720 if (Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::Control|Keyboard::Shift))) {
722 /* ctrl-shift-click applies change to all routes */
724 _session.begin_reversible_command (_("meter point change"));
725 Session::GlobalMeteringStateCommand *cmd = new Session::GlobalMeteringStateCommand (_session, this);
726 _session.foreach_route (this, &GainMeter::set_meter_point, next_meter_point (_route->meter_point()));
728 _session.add_command (cmd);
729 _session.commit_reversible_command ();
732 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::Control)) {
734 /* ctrl-click: solo mix group.
735 ctrl-button2 is MIDI learn.
738 if (ev->button == 1) {
739 _session.begin_reversible_command (_("meter point change"));
740 Session::GlobalMeteringStateCommand *cmd = new Session::GlobalMeteringStateCommand (_session, this);
741 set_mix_group_meter_point (*_route, next_meter_point (_route->meter_point()));
743 _session.add_command (cmd);
744 _session.commit_reversible_command ();
749 /* click: change just this route */
753 _route->set_meter_point (next_meter_point (_route->meter_point()), this);
764 GainMeter::meter_release(GdkEventButton* ev)
768 if (wait_for_release){
769 wait_for_release = false;
770 set_meter_point (*(dynamic_cast<Route*>(_io.get())), old_meter_point);
777 GainMeter::set_meter_point (Route& route, MeterPoint mp)
779 route.set_meter_point (mp, this);
783 GainMeter::set_mix_group_meter_point (Route& route, MeterPoint mp)
785 RouteGroup* mix_group;
787 if((mix_group = route.mix_group()) != 0){
788 mix_group->apply (&Route::set_meter_point, mp, this);
790 route.set_meter_point (mp, this);
795 GainMeter::meter_point_clicked ()
799 if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
805 GainMeter::start_gain_touch (GdkEventButton* ev)
807 _io->start_gain_touch ();
812 GainMeter::end_gain_touch (GdkEventButton* ev)
814 _io->end_gain_touch ();
819 GainMeter::gain_automation_state_button_event (GdkEventButton *ev)
821 if (ev->type == GDK_BUTTON_RELEASE) {
825 switch (ev->button) {
827 gain_astate_menu.popup (1, ev->time);
837 GainMeter::gain_automation_style_button_event (GdkEventButton *ev)
839 if (ev->type == GDK_BUTTON_RELEASE) {
843 switch (ev->button) {
845 gain_astyle_menu.popup (1, ev->time);
854 GainMeter::astate_string (AutoState state)
856 return _astate_string (state, false);
860 GainMeter::short_astate_string (AutoState state)
862 return _astate_string (state, true);
866 GainMeter::_astate_string (AutoState state, bool shrt)
872 sstr = (shrt ? "M" : _("M"));
875 sstr = (shrt ? "P" : _("P"));
878 sstr = (shrt ? "T" : _("T"));
881 sstr = (shrt ? "W" : _("W"));
889 GainMeter::astyle_string (AutoStyle style)
891 return _astyle_string (style, false);
895 GainMeter::short_astyle_string (AutoStyle style)
897 return _astyle_string (style, true);
901 GainMeter::_astyle_string (AutoStyle style, bool shrt)
906 /* XXX it might different in different languages */
908 return (shrt ? _("Abs") : _("Abs"));
913 GainMeter::gain_automation_style_changed ()
915 // Route* _route = dynamic_cast<Route*>(&_io);
918 gain_automation_style_button.set_label (astyle_string(_io->gain_automation_curve().automation_style()));
921 gain_automation_style_button.set_label (short_astyle_string(_io->gain_automation_curve().automation_style()));
927 GainMeter::gain_automation_state_changed ()
929 ENSURE_GUI_THREAD(mem_fun(*this, &GainMeter::gain_automation_state_changed));
930 //Route* _route = dynamic_cast<Route*>(&_io);
936 gain_automation_state_button.set_label (astate_string(_io->gain_automation_curve().automation_state()));
939 gain_automation_state_button.set_label (short_astate_string(_io->gain_automation_curve().automation_state()));
943 x = (_io->gain_automation_state() != Off);
945 if (gain_automation_state_button.get_active() != x) {
946 ignore_toggle = true;
947 gain_automation_state_button.set_active (x);
948 ignore_toggle = false;
951 update_gain_sensitive ();
953 /* start watching automation so that things move */
955 gain_watching.disconnect();
958 gain_watching = ARDOUR_UI::RapidScreenUpdate.connect (mem_fun (*this, &GainMeter::effective_gain_display));