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>
39 #include "ardour_ui.h"
40 #include "gain_meter.h"
43 #include "gui_thread.h"
45 #include "public_editor.h"
47 #include <ardour/session.h>
48 #include <ardour/route.h>
49 #include <ardour/meter.h>
53 using namespace ARDOUR;
55 using namespace Gtkmm2ext;
60 sigc::signal<void> GainMeter::ResetAllPeakDisplays;
61 sigc::signal<void,RouteGroup*> GainMeter::ResetGroupPeakDisplays;
62 Glib::RefPtr<Gdk::Pixbuf> GainMeter::slider;
63 Glib::RefPtr<Gdk::Pixbuf> GainMeter::rail;
64 map<string,Glib::RefPtr<Gdk::Pixmap> > GainMeter::metric_pixmaps;
67 GainMeter::setup_slider_pix ()
69 slider = ::get_icon ("fader_belt");
73 GainMeter::GainMeter (boost::shared_ptr<IO> io, Session& s)
77 // 0.781787 is the value needed for gain to be set to 0.
78 gain_adjustment (0.781787, 0.0, 1.0, 0.01, 0.1),
79 gain_automation_style_button (""),
80 gain_automation_state_button ("")
87 ignore_toggle = false;
89 next_release_selects = false;
92 gain_slider = manage (new VSliderController (slider,
94 *_io->gain_control().get(),
97 gain_slider->signal_button_press_event().connect (mem_fun(*this, &GainMeter::start_gain_touch));
98 gain_slider->signal_button_release_event().connect (mem_fun(*this, &GainMeter::end_gain_touch));
99 gain_slider->set_name ("GainFader");
101 gain_display.set_name ("MixerStripGainDisplay");
102 gain_display.set_has_frame (false);
103 set_size_request_to_display_given_text (gain_display, "-80.g", 2, 6); /* note the descender */
104 gain_display.signal_activate().connect (mem_fun (*this, &GainMeter::gain_activated));
105 gain_display.signal_focus_in_event().connect (mem_fun (*this, &GainMeter::gain_focused), false);
106 gain_display.signal_focus_out_event().connect (mem_fun (*this, &GainMeter::gain_focused), false);
108 gain_display_box.set_name ("MeterMetricsStrip");
109 gain_display_box.set_homogeneous (true);
110 gain_display_box.set_spacing (2);
111 gain_display_box.pack_start (gain_display, true, true);
113 peak_display.set_name ("MixerStripPeakDisplay");
114 // peak_display.set_has_frame (false);
115 // peak_display.set_editable (false);
116 set_size_request_to_display_given_text (peak_display, "-80.g", 2, 6); /* note the descender */
117 max_peak = minus_infinity();
118 peak_display.set_label (_("-inf"));
119 peak_display.unset_flags (Gtk::CAN_FOCUS);
121 meter_metric_area.set_name ("MeterMetricsStrip");
122 set_size_request_to_display_given_text (meter_metric_area, "-50", 0, 0);
124 meter_packer.set_spacing (2);
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 HBox* fader_centering_box = manage (new HBox);
139 fader_centering_box->pack_start (*gain_slider, true, false);
141 fader_vbox = manage (new Gtk::VBox());
142 fader_vbox->set_spacing (0);
143 fader_vbox->pack_start (*fader_centering_box, false, false, 0);
145 hbox.set_spacing (2);
147 if (_io->default_type() == ARDOUR::DataType::AUDIO) {
148 hbox.pack_start (*fader_vbox, true, true);
155 if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
158 if we have a route (ie. we're not the click),
159 pack some route-dependent stuff.
162 gain_display_box.pack_end (peak_display, true, true);
164 hbox.pack_end (meter_packer, true, true);
166 using namespace Menu_Helpers;
168 gain_astate_menu.items().push_back (MenuElem (_("Manual"),
169 bind (mem_fun (*_io, &IO::set_parameter_automation_state),
170 Parameter(GainAutomation), (AutoState) Off)));
171 gain_astate_menu.items().push_back (MenuElem (_("Play"),
172 bind (mem_fun (*_io, &IO::set_parameter_automation_state),
173 Parameter(GainAutomation), (AutoState) Play)));
174 gain_astate_menu.items().push_back (MenuElem (_("Write"),
175 bind (mem_fun (*_io, &IO::set_parameter_automation_state),
176 Parameter(GainAutomation), (AutoState) Write)));
177 gain_astate_menu.items().push_back (MenuElem (_("Touch"),
178 bind (mem_fun (*_io, &IO::set_parameter_automation_state),
179 Parameter(GainAutomation), (AutoState) Touch)));
181 gain_astyle_menu.items().push_back (MenuElem (_("Trim")));
182 gain_astyle_menu.items().push_back (MenuElem (_("Abs")));
184 gain_astate_menu.set_name ("ArdourContextMenu");
185 gain_astyle_menu.set_name ("ArdourContextMenu");
187 gain_automation_style_button.signal_button_press_event().connect (mem_fun(*this, &GainMeter::gain_automation_style_button_event), false);
188 gain_automation_state_button.signal_button_press_event().connect (mem_fun(*this, &GainMeter::gain_automation_state_button_event), false);
190 r->gain_control()->list()->automation_state_changed.connect (mem_fun(*this, &GainMeter::gain_automation_state_changed));
191 r->gain_control()->list()->automation_style_changed.connect (mem_fun(*this, &GainMeter::gain_automation_style_changed));
192 fader_vbox->pack_start (gain_automation_state_button, false, false, 0);
194 gain_automation_state_changed ();
199 pack_start (gain_display_box, Gtk::PACK_SHRINK);
200 pack_start (hbox, Gtk::PACK_SHRINK);
202 _io->gain_control()->Changed.connect (mem_fun(*this, &GainMeter::gain_changed));
204 meter_metric_area.signal_expose_event().connect (mem_fun(*this, &GainMeter::meter_metrics_expose));
205 gain_adjustment.signal_value_changed().connect (mem_fun(*this, &GainMeter::gain_adjusted));
206 peak_display.signal_button_release_event().connect (mem_fun(*this, &GainMeter::peak_button_release), false);
207 gain_display.signal_key_press_event().connect (mem_fun(*this, &GainMeter::gain_key_press), false);
209 Config->ParameterChanged.connect (mem_fun (*this, &GainMeter::parameter_changed));
214 update_gain_sensitive ();
216 ResetAllPeakDisplays.connect (mem_fun(*this, &GainMeter::reset_peak_display));
217 ResetGroupPeakDisplays.connect (mem_fun(*this, &GainMeter::reset_group_peak_display));
219 UI::instance()->theme_changed.connect (mem_fun(*this, &GainMeter::on_theme_changed));
221 fader_centering_box->show();
229 gain_display_box.show();
231 meter_metric_area.show();
232 gain_automation_style_button.show();
233 gain_automation_state_button.show();
238 GainMeter::set_width (Width w)
253 Glib::RefPtr<Gdk::Pixmap>
254 GainMeter::render_metrics (Gtk::Widget& w)
256 //cerr << "GainMeter::render_metrics() called, red = " << w.get_style()->get_bg(Gtk::STATE_NORMAL).get_red() << endl;//DEBUG
257 Glib::RefPtr<Gdk::Window> win (w.get_window());
258 Glib::RefPtr<Gdk::GC> fg_gc (w.get_style()->get_fg_gc (Gtk::STATE_NORMAL));
259 Glib::RefPtr<Gdk::GC> bg_gc (w.get_style()->get_bg_gc (Gtk::STATE_NORMAL));
261 int db_points[] = { -50, -40, -20, -30, -10, -3, 0, 4 };
264 win->get_size (width, height);
266 Glib::RefPtr<Gdk::Pixmap> pixmap = Gdk::Pixmap::create (win, width, height);
268 metric_pixmaps[w.get_name()] = pixmap;
270 pixmap->draw_rectangle (bg_gc, true, 0, 0, width, height);
272 Glib::RefPtr<Pango::Layout> layout = w.create_pango_layout("");
274 for (uint32_t i = 0; i < sizeof (db_points)/sizeof (db_points[0]); ++i) {
276 float fraction = log_meter (db_points[i]);
277 gint pos = height - (gint) floor (height * fraction);
279 snprintf (buf, sizeof (buf), "%d", abs (db_points[i]));
281 layout->set_text (buf);
283 /* we want logical extents, not ink extents here */
286 layout->get_pixel_size (width, height);
288 pixmap->draw_line (fg_gc, 0, pos, 4, pos);
289 pixmap->draw_layout (fg_gc, 6, pos - (height/2), layout);
296 GainMeter::meter_metrics_expose (GdkEventExpose *ev)
298 /* Only draw dB scale if we're metering audio */
299 if (_io->n_inputs().n_audio() + _io->n_outputs().n_audio() == 0)
302 static Glib::RefPtr<Gtk::Style> meter_style;
305 meter_style = meter_metric_area.get_style();
308 Glib::RefPtr<Gdk::Window> win (meter_metric_area.get_window());
309 Glib::RefPtr<Gdk::GC> fg_gc (meter_style->get_fg_gc (Gtk::STATE_NORMAL));
310 Glib::RefPtr<Gdk::GC> bg_gc (meter_style->get_bg_gc (Gtk::STATE_NORMAL));
312 GdkRectangle base_rect;
313 GdkRectangle draw_rect;
316 win->get_size (width, height);
318 base_rect.width = width;
319 base_rect.height = height;
323 Glib::RefPtr<Gdk::Pixmap> pixmap;
324 std::map<string,Glib::RefPtr<Gdk::Pixmap> >::iterator i = metric_pixmaps.find (meter_metric_area.get_name());
326 if (i == metric_pixmaps.end() || style_changed) {
327 pixmap = render_metrics (meter_metric_area);
332 gdk_rectangle_intersect (&ev->area, &base_rect, &draw_rect);
333 win->draw_rectangle (bg_gc, true, draw_rect.x, draw_rect.y, draw_rect.width, draw_rect.height);
334 win->draw_drawable (fg_gc, pixmap, draw_rect.x, draw_rect.y, draw_rect.x, draw_rect.y, draw_rect.width, draw_rect.height);
336 style_changed = false;
341 GainMeter::on_theme_changed()
343 style_changed = true;
346 GainMeter::~GainMeter ()
352 for (vector<MeterInfo>::iterator i = meters.begin(); i != meters.end(); i++) {
360 GainMeter::update_meters ()
362 vector<MeterInfo>::iterator i;
367 for (n = 0, i = meters.begin(); i != meters.end(); ++i, ++n) {
369 peak = _io->peak_meter().peak_power (n);
371 (*i).meter->set (log_meter (peak));
373 mpeak = _io->peak_meter().max_peak_power(n);
375 if (mpeak > max_peak) {
377 /* set peak display */
378 if (max_peak <= -200.0f) {
379 peak_display.set_label (_("-inf"));
381 snprintf (buf, sizeof(buf), "%.1f", max_peak);
382 peak_display.set_label (buf);
385 if (max_peak >= 0.0f) {
386 peak_display.set_name ("MixerStripPeakDisplayPeak");
394 GainMeter::parameter_changed(const char* parameter_name)
396 #define PARAM_IS(x) (!strcmp (parameter_name, (x)))
398 ENSURE_GUI_THREAD (bind (mem_fun(*this, &GainMeter::parameter_changed), parameter_name));
400 if (PARAM_IS ("meter-hold")) {
402 vector<MeterInfo>::iterator i;
405 for (n = 0, i = meters.begin(); i != meters.end(); ++i, ++n) {
407 (*i).meter->set_hold_count ((uint32_t) floor(Config->get_meter_hold()));
415 GainMeter::hide_all_meters ()
417 bool remove_metric_area = false;
419 for (vector<MeterInfo>::iterator i = meters.begin(); i != meters.end(); ++i) {
421 remove_metric_area = true;
422 meter_packer.remove (*((*i).meter));
427 if (remove_metric_area) {
428 if (meter_metric_area.get_parent()) {
429 meter_packer.remove (meter_metric_area);
435 GainMeter::setup_meters ()
437 uint32_t nmeters = _io->n_outputs().n_total();
444 if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
446 switch (r->meter_point()) {
448 nmeters = r->n_inputs().n_total();
451 nmeters = r->pre_fader_streams().n_total();
454 nmeters = r->n_outputs().n_total();
460 nmeters = _io->n_outputs().n_total();
469 width = regular_meter_width;
471 width = thin_meter_width;
474 while (meters.size() < nmeters) {
475 meters.push_back (MeterInfo());
478 /* pack them backwards */
480 if (_width == Wide) {
481 meter_packer.pack_end (meter_metric_area, false, false);
482 meter_metric_area.show_all ();
485 for (int32_t n = nmeters-1; nmeters && n >= 0 ; --n) {
486 if (meters[n].width != width) {
487 delete meters[n].meter;
488 meters[n].meter = new FastMeter ((uint32_t) floor (Config->get_meter_hold()), width, FastMeter::Vertical);
489 meters[n].width = width;
491 meters[n].meter->add_events (Gdk::BUTTON_RELEASE_MASK);
492 meters[n].meter->signal_button_release_event().connect (bind (mem_fun(*this, &GainMeter::meter_button_release), n));
495 meter_packer.pack_end (*meters[n].meter, false, false);
496 meters[n].meter->show_all ();
497 meters[n].packed = true;
502 GainMeter::get_gm_width ()
504 Gtk::Requisition sz = hbox.size_request ();
509 GainMeter::gain_key_press (GdkEventKey* ev)
511 if (key_is_legal_for_numeric_entry (ev->keyval)) {
512 /* drop through to normal handling */
515 /* illegal key for gain entry */
520 GainMeter::peak_button_release (GdkEventButton* ev)
522 /* reset peak label */
524 if (ev->button == 1 && Keyboard::modifier_state_equals (ev->state, Keyboard::Control|Keyboard::Shift)) {
525 ResetAllPeakDisplays ();
526 } else if (ev->button == 1 && Keyboard::modifier_state_equals (ev->state, Keyboard::Control)) {
528 if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
529 ResetGroupPeakDisplays (r->mix_group());
532 reset_peak_display ();
539 GainMeter::reset_peak_display ()
542 if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
543 r->peak_meter().reset_max();
546 max_peak = -INFINITY;
547 peak_display.set_label (_("-Inf"));
548 peak_display.set_name ("MixerStripPeakDisplay");
552 GainMeter::reset_group_peak_display (RouteGroup* group)
555 if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
556 if (group == r->mix_group()) {
557 reset_peak_display ();
563 GainMeter::meter_button_release (GdkEventButton* ev, uint32_t which)
565 switch (ev->button) {
567 meters[which].meter->clear();
568 max_peak = minus_infinity();
569 peak_display.set_label (_("-inf"));
570 peak_display.set_name ("MixerStripPeakDisplay");
574 // popup_meter_menu (ev);
582 GainMeter::popup_meter_menu (GdkEventButton *ev)
584 using namespace Menu_Helpers;
586 if (meter_menu == 0) {
587 meter_menu = new Gtk::Menu;
588 MenuList& items = meter_menu->items();
590 items.push_back (MenuElem ("-inf .. +0dBFS"));
591 items.push_back (MenuElem ("-10dB .. +0dBFS"));
592 items.push_back (MenuElem ("-4 .. +0dBFS"));
593 items.push_back (SeparatorElem());
594 items.push_back (MenuElem ("-inf .. -2dBFS"));
595 items.push_back (MenuElem ("-10dB .. -2dBFS"));
596 items.push_back (MenuElem ("-4 .. -2dBFS"));
599 meter_menu->popup (1, ev->time);
603 GainMeter::gain_focused (GdkEventFocus* ev)
606 gain_display.select_region (0, -1);
608 gain_display.select_region (0, 0);
614 GainMeter::gain_activated ()
618 if (sscanf (gain_display.get_text().c_str(), "%f", &f) == 1) {
620 /* clamp to displayable values */
624 _io->gain_control()->set_value (dB_to_coefficient(f));
626 if (gain_display.has_focus()) {
627 PublicEditor::instance().reset_focus();
633 GainMeter::show_gain ()
637 float v = gain_adjustment.get_value();
640 strcpy (buf, _("-inf"));
642 snprintf (buf, 32, "%.1f", coefficient_to_dB (slider_position_to_gain (v)));
645 gain_display.set_text (buf);
649 GainMeter::gain_adjusted ()
651 if (!ignore_toggle) {
652 _io->gain_control()->set_value (slider_position_to_gain (gain_adjustment.get_value()));
658 GainMeter::effective_gain_display ()
660 gfloat value = gain_to_slider_position (_io->effective_gain());
662 if (gain_adjustment.get_value() != value) {
663 ignore_toggle = true;
664 gain_adjustment.set_value (value);
665 ignore_toggle = false;
670 GainMeter::gain_changed ()
672 Gtkmm2ext::UI::instance()->call_slot (mem_fun(*this, &GainMeter::effective_gain_display));
676 GainMeter::set_meter_strip_name (const char * name)
678 meter_metric_area.set_name (name);
682 GainMeter::set_fader_name (const char * name)
684 gain_slider->set_name (name);
688 GainMeter::update_gain_sensitive ()
690 static_cast<Gtkmm2ext::SliderController*>(gain_slider)->set_sensitive (!(_io->gain_control()->list()->automation_state() & Play));
695 next_meter_point (MeterPoint mp)
699 return MeterPreFader;
703 return MeterPostFader;
715 GainMeter::meter_press(GdkEventButton* ev)
719 wait_for_release = false;
721 if ((_route = dynamic_cast<Route*>(_io.get())) == 0) {
725 if (!ignore_toggle) {
727 if (Keyboard::is_context_menu_event (ev)) {
729 // no menu at this time.
733 if (ev->button == 2) {
735 // ctrl-button2 click is the midi binding click
736 // button2-click is "momentary"
738 if (!Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::Control))) {
739 wait_for_release = true;
740 old_meter_point = _route->meter_point ();
744 if (ev->button == 1 || ev->button == 2) {
746 if (Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::Control|Keyboard::Shift))) {
748 /* ctrl-shift-click applies change to all routes */
750 _session.begin_reversible_command (_("meter point change"));
751 Session::GlobalMeteringStateCommand *cmd = new Session::GlobalMeteringStateCommand (_session, this);
752 _session.foreach_route (this, &GainMeter::set_meter_point, next_meter_point (_route->meter_point()));
754 _session.add_command (cmd);
755 _session.commit_reversible_command ();
758 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::Control)) {
760 /* ctrl-click: solo mix group.
761 ctrl-button2 is MIDI learn.
764 if (ev->button == 1) {
765 _session.begin_reversible_command (_("meter point change"));
766 Session::GlobalMeteringStateCommand *cmd = new Session::GlobalMeteringStateCommand (_session, this);
767 set_mix_group_meter_point (*_route, next_meter_point (_route->meter_point()));
769 _session.add_command (cmd);
770 _session.commit_reversible_command ();
775 /* click: change just this route */
779 _route->set_meter_point (next_meter_point (_route->meter_point()), this);
790 GainMeter::meter_release(GdkEventButton* ev)
794 if (wait_for_release){
795 wait_for_release = false;
796 set_meter_point (*(dynamic_cast<Route*>(_io.get())), old_meter_point);
803 GainMeter::set_meter_point (Route& route, MeterPoint mp)
805 route.set_meter_point (mp, this);
809 GainMeter::set_mix_group_meter_point (Route& route, MeterPoint mp)
811 RouteGroup* mix_group;
813 if((mix_group = route.mix_group()) != 0){
814 mix_group->apply (&Route::set_meter_point, mp, this);
816 route.set_meter_point (mp, this);
821 GainMeter::meter_point_clicked ()
825 if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
831 GainMeter::start_gain_touch (GdkEventButton* ev)
833 _io->gain_control()->list()->start_touch ();
838 GainMeter::end_gain_touch (GdkEventButton* ev)
840 _io->gain_control()->list()->stop_touch ();
845 GainMeter::gain_automation_state_button_event (GdkEventButton *ev)
847 if (ev->type == GDK_BUTTON_RELEASE) {
851 switch (ev->button) {
853 gain_astate_menu.popup (1, ev->time);
863 GainMeter::gain_automation_style_button_event (GdkEventButton *ev)
865 if (ev->type == GDK_BUTTON_RELEASE) {
869 switch (ev->button) {
871 gain_astyle_menu.popup (1, ev->time);
880 GainMeter::astate_string (AutoState state)
882 return _astate_string (state, false);
886 GainMeter::short_astate_string (AutoState state)
888 return _astate_string (state, true);
892 GainMeter::_astate_string (AutoState state, bool shrt)
898 sstr = (shrt ? "M" : _("M"));
901 sstr = (shrt ? "P" : _("P"));
904 sstr = (shrt ? "T" : _("T"));
907 sstr = (shrt ? "W" : _("W"));
915 GainMeter::astyle_string (AutoStyle style)
917 return _astyle_string (style, false);
921 GainMeter::short_astyle_string (AutoStyle style)
923 return _astyle_string (style, true);
927 GainMeter::_astyle_string (AutoStyle style, bool shrt)
932 /* XXX it might different in different languages */
934 return (shrt ? _("Abs") : _("Abs"));
939 GainMeter::gain_automation_style_changed ()
941 // Route* _route = dynamic_cast<Route*>(&_io);
944 gain_automation_style_button.set_label (astyle_string(_io->gain_control()->list()->automation_style()));
947 gain_automation_style_button.set_label (short_astyle_string(_io->gain_control()->list()->automation_style()));
953 GainMeter::gain_automation_state_changed ()
955 ENSURE_GUI_THREAD(mem_fun(*this, &GainMeter::gain_automation_state_changed));
956 //Route* _route = dynamic_cast<Route*>(&_io);
962 gain_automation_state_button.set_label (astate_string(_io->gain_control()->list()->automation_state()));
965 gain_automation_state_button.set_label (short_astate_string(_io->gain_control()->list()->automation_state()));
969 x = (_io->gain_control()->list()->automation_state() != Off);
971 if (gain_automation_state_button.get_active() != x) {
972 ignore_toggle = true;
973 gain_automation_state_button.set_active (x);
974 ignore_toggle = false;
977 update_gain_sensitive ();
979 /* start watching automation so that things move */
981 gain_watching.disconnect();
984 gain_watching = ARDOUR_UI::RapidScreenUpdate.connect (mem_fun (*this, &GainMeter::effective_gain_display));