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.
23 #include <ardour/io.h>
24 #include <ardour/route.h>
25 #include <ardour/route_group.h>
26 #include <ardour/session.h>
27 #include <ardour/session_route.h>
28 #include <ardour/dB.h>
30 #include <gtkmm2ext/utils.h>
31 #include <gtkmm2ext/fastmeter.h>
32 #include <gtkmm2ext/stop_signal.h>
33 #include <gtkmm2ext/barcontroller.h>
34 #include <midi++/manager.h>
35 #include <pbd/fastlog.h>
37 #include "ardour_ui.h"
38 #include "gain_meter.h"
41 #include "gui_thread.h"
44 #include <ardour/session.h>
45 #include <ardour/route.h>
49 using namespace ARDOUR;
50 using namespace Gtkmm2ext;
55 sigc::signal<void> GainMeter::ResetAllPeakDisplays;
56 sigc::signal<void,RouteGroup*> GainMeter::ResetGroupPeakDisplays;
57 Glib::RefPtr<Gdk::Pixbuf> GainMeter::slider;
58 Glib::RefPtr<Gdk::Pixbuf> GainMeter::rail;
59 map<string,Glib::RefPtr<Gdk::Pixmap> > GainMeter::metric_pixmaps;
62 GainMeter::setup_slider_pix ()
64 string path = ARDOUR::find_data_file("vslider02_slider.xpm", "pixmaps");
66 error << _("cannot find images for fader slider") << endmsg;
69 slider = Gdk::Pixbuf::create_from_file (path);
71 path = ARDOUR::find_data_file("vslider02_rail.xpm", "pixmaps");
73 error << _("cannot find images for fader rail") << endmsg;
76 rail = Gdk::Pixbuf::create_from_file (path);
81 GainMeter::GainMeter (IO& io, Session& s)
85 // 0.781787 is the value needed for gain to be set to 0.
86 gain_adjustment (0.781787, 0.0, 1.0, 0.01, 0.1),
87 gain_display (&gain_adjustment, "MixerStripGainDisplay"),
88 gain_unit_label (_("dbFS")),
89 meter_point_label (_("pre")),
91 gain_automation_style_button (""),
92 gain_automation_state_button ("")
99 ignore_toggle = false;
102 gain_slider = manage (new VSliderController (slider, rail,
104 & _io.midi_gain_control(),
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 ("MixerGainMeter");
111 if (_session.midi_port()) {
112 _io.set_midi_to_gain_function (slider_position_to_gain);
113 _io.set_gain_to_midi_function (gain_to_slider_position);
116 gain_display.set_print_func (_gain_printer, this);
118 gain_unit_button.add (gain_unit_label);
119 gain_unit_button.set_name ("MixerStripGainUnitButton");
120 gain_unit_label.set_name ("MixerStripGainUnitButton");
122 meter_point_button.signal_button_press_event().connect (mem_fun (*this, &GainMeter::meter_press), false);
123 meter_point_button.signal_button_release_event().connect (mem_fun (*this, &GainMeter::meter_release), false);
126 top_table.set_col_spacings (2);
127 top_table.set_homogeneous (true);
128 top_table.attach (gain_unit_button, 0, 1, 0, 1);
132 if ((r = dynamic_cast<Route*> (&_io)) != 0) {
134 r->meter_change.connect (mem_fun(*this, &GainMeter::meter_changed));
135 meter_point_button.add (meter_point_label);
136 meter_point_button.set_name ("MixerStripMeterPreButton");
137 meter_point_label.set_name ("MixerStripMeterPreButton");
139 switch (r->meter_point()) {
141 meter_point_label.set_text (_("input"));
145 meter_point_label.set_text (_("pre"));
149 meter_point_label.set_text (_("post"));
153 /* TRANSLATORS: this string should be longest of the strings
154 used to describe meter points. In english, its "input".
157 set_size_request_to_display_given_text (meter_point_button, _("tupni"), 5, 5);
160 top_table.attach (meter_point_button, 1, 2, 0, 1);
162 gain_display_box.set_spacing (2);
163 set_size_request_to_display_given_text (gain_display_frame, "-86.g", 2, 6); /* note the descender */
164 gain_display_frame.set_shadow_type (Gtk::SHADOW_IN);
165 gain_display_frame.set_name ("BaseFrame");
166 gain_display_frame.add (gain_display);
167 gain_display_box.pack_start (gain_display_frame, Gtk::PACK_SHRINK);
169 peak_display.set_name ("MixerStripPeakDisplay");
170 peak_display.add (peak_display_label);
171 set_size_request_to_display_given_text (peak_display_frame, "-86.g", 2, 6); /* note the descender */
172 peak_display_frame.set_shadow_type (Gtk::SHADOW_IN);
173 peak_display_frame.set_name ("BaseFrame");
174 peak_display_frame.add (peak_display);
175 max_peak = minus_infinity();
176 peak_display_label.set_text (_("-inf"));
178 gain_display_box.pack_end (peak_display_frame, Gtk::PACK_SHRINK);
180 meter_metric_area.set_size_request (25, -1);
181 meter_metric_area.set_name ("MeterMetricsStrip");
183 meter_packer.set_spacing (2);
185 gain_automation_style_button.set_name ("MixerAutomationModeButton");
186 gain_automation_state_button.set_name ("MixerAutomationPlaybackButton");
188 ARDOUR_UI::instance()->tooltips().set_tip (gain_automation_state_button, _("Fader automation mode"));
189 ARDOUR_UI::instance()->tooltips().set_tip (gain_automation_style_button, _("Fader automation type"));
191 gain_automation_style_button.unset_flags (Gtk::CAN_FOCUS);
192 gain_automation_state_button.unset_flags (Gtk::CAN_FOCUS);
194 using namespace Menu_Helpers;
196 gain_astate_menu.items().push_back (MenuElem (_("Isolate"),
197 bind (mem_fun (dynamic_cast<Route*>(&_io), &IO::set_gain_automation_state), (AutoState) Off)));
198 gain_astate_menu.items().push_back (MenuElem (_("Play"),
199 bind (mem_fun (dynamic_cast<Route*>(&_io), &IO::set_gain_automation_state), (AutoState) Play)));
200 gain_astate_menu.items().push_back (MenuElem (_("Write"),
201 bind (mem_fun (dynamic_cast<Route*>(&_io), &IO::set_gain_automation_state), (AutoState) Write)));
202 gain_astate_menu.items().push_back (MenuElem (_("Touch"),
203 bind (mem_fun (dynamic_cast<Route*>(&_io), &IO::set_gain_automation_state), (AutoState) Touch)));
205 gain_astyle_menu.items().push_back (MenuElem (_("Trim")));
206 gain_astyle_menu.items().push_back (MenuElem (_("Abs")));
208 Route* _route = dynamic_cast<Route*>(&_io);
210 gain_astate_menu.set_name ("ArdourContextMenu");
211 gain_astyle_menu.set_name ("ArdourContextMenu");
213 gain_automation_style_button.signal_button_press_event().connect (mem_fun(*this, &GainMeter::gain_automation_style_button_event), false);
214 gain_automation_state_button.signal_button_press_event().connect (mem_fun(*this, &GainMeter::gain_automation_state_button_event), false);
216 _route->gain_automation_curve().automation_state_changed.connect (mem_fun(*this, &GainMeter::gain_automation_state_changed));
217 _route->gain_automation_curve().automation_style_changed.connect (mem_fun(*this, &GainMeter::gain_automation_style_changed));
219 fader_vbox = manage (new Gtk::VBox());
220 fader_vbox->set_spacing (0);
221 fader_vbox->pack_start (*gain_slider, false, false, 0);
222 fader_vbox->pack_start (gain_automation_state_button, false, false, 0);
224 hbox.set_spacing (0);
225 hbox.pack_start (*fader_vbox, false, false, 2);
226 hbox.pack_start (meter_packer, true, false);
230 pack_start (top_table, Gtk::PACK_SHRINK);
231 pack_start (gain_display_box, Gtk::PACK_SHRINK);
232 pack_start (hbox, Gtk::PACK_SHRINK);
234 _io.gain_changed.connect (mem_fun(*this, &GainMeter::gain_changed));
236 meter_metric_area.signal_expose_event().connect (mem_fun(*this, &GainMeter::meter_metrics_expose));
237 gain_adjustment.signal_value_changed().connect (mem_fun(*this, &GainMeter::gain_adjusted));
238 peak_display.signal_button_release_event().connect (mem_fun(*this, &GainMeter::peak_button_release));
240 _session.MeterHoldChanged.connect (mem_fun(*this, &GainMeter::meter_hold_changed));
242 gain_automation_state_changed ();
245 update_gain_sensitive ();
247 ResetAllPeakDisplays.connect (mem_fun(*this, &GainMeter::reset_peak_display));
248 ResetGroupPeakDisplays.connect (mem_fun(*this, &GainMeter::reset_group_peak_display));
252 GainMeter::set_width (Width w)
256 peak_display_frame.show_all();
259 peak_display_frame.hide_all();
267 Glib::RefPtr<Gdk::Pixmap>
268 GainMeter::render_metrics (Gtk::Widget& w)
270 Glib::RefPtr<Gdk::Window> win (w.get_window());
271 Glib::RefPtr<Gdk::GC> fg_gc (w.get_style()->get_fg_gc (Gtk::STATE_NORMAL));
272 Glib::RefPtr<Gdk::GC> bg_gc (w.get_style()->get_bg_gc (Gtk::STATE_NORMAL));
273 gint x, y, width, height, depth;
274 int db_points[] = { -50, -40, -20, -30, -10, -3, 0, 4 };
277 win->get_geometry (x, y, width, height, depth);
279 Glib::RefPtr<Gdk::Pixmap> pixmap = Gdk::Pixmap::create (win, width, height);
281 metric_pixmaps[w.get_name()] = pixmap;
283 pixmap->draw_rectangle (bg_gc, true, 0, 0, width, height);
285 Glib::RefPtr<Pango::Layout> layout = w.create_pango_layout("");
287 for (uint32_t i = 0; i < sizeof (db_points)/sizeof (db_points[0]); ++i) {
289 float fraction = log_meter (db_points[i]);
290 gint pos = height - (gint) floor (height * fraction);
292 snprintf (buf, sizeof (buf), "%d", abs (db_points[i]));
294 layout->set_text (buf);
296 /* we want logical extents, not ink extents here */
299 layout->get_pixel_size (width, height);
301 pixmap->draw_line (fg_gc, 0, pos, 4, pos);
302 pixmap->draw_layout (fg_gc, 6, pos - (height/2), layout);
309 GainMeter::meter_metrics_expose (GdkEventExpose *ev)
311 Glib::RefPtr<Gdk::Window> win (meter_metric_area.get_window());
312 Glib::RefPtr<Gdk::GC> fg_gc (meter_metric_area.get_style()->get_fg_gc (Gtk::STATE_NORMAL));
313 Glib::RefPtr<Gdk::GC> bg_gc (meter_metric_area.get_style()->get_bg_gc (Gtk::STATE_NORMAL));
314 GdkRectangle base_rect;
315 GdkRectangle draw_rect;
316 gint x, y, width, height, depth;
318 win->get_geometry (x, y, width, height, depth);
320 base_rect.width = width;
321 base_rect.height = height;
325 Glib::RefPtr<Gdk::Pixmap> pixmap;
326 std::map<string,Glib::RefPtr<Gdk::Pixmap> >::iterator i = metric_pixmaps.find (meter_metric_area.get_name());
328 if (i == metric_pixmaps.end()) {
329 pixmap = render_metrics (meter_metric_area);
334 gdk_rectangle_intersect (&ev->area, &base_rect, &draw_rect);
335 win->draw_rectangle (bg_gc, true, draw_rect.x, draw_rect.y, draw_rect.width, draw_rect.height);
336 win->draw_drawable (bg_gc, pixmap, draw_rect.x, draw_rect.y, draw_rect.x, draw_rect.y, draw_rect.width, draw_rect.height);
341 GainMeter::~GainMeter ()
347 for (vector<MeterInfo>::iterator i = meters.begin(); i != meters.end(); i++) {
355 GainMeter::update_meters ()
357 vector<MeterInfo>::iterator i;
362 for (n = 0, i = meters.begin(); i != meters.end(); ++i, ++n) {
364 peak = _io.peak_input_power (n);
366 (*i).meter->set (log_meter (peak), peak);
368 if (peak > max_peak) {
370 /* set peak display */
371 snprintf (buf, sizeof(buf), "%.1f", max_peak);
372 peak_display_label.set_text (buf);
374 if (max_peak >= 0.0f) {
375 peak_display.set_name ("MixerStripPeakDisplayPeak");
383 GainMeter::meter_hold_changed()
385 ENSURE_GUI_THREAD(mem_fun(*this, &GainMeter::meter_hold_changed));
387 vector<MeterInfo>::iterator i;
390 for (n = 0, i = meters.begin(); i != meters.end(); ++i, ++n) {
392 (*i).meter->set_hold_count ((uint32_t) floor(_session.meter_hold()));
397 GainMeter::hide_all_meters ()
399 bool remove_metric_area = false;
401 for (vector<MeterInfo>::iterator i = meters.begin(); i != meters.end(); ++i) {
403 remove_metric_area = true;
404 meter_packer.remove (*((*i).meter));
409 if (remove_metric_area) {
410 if (meter_metric_area.get_parent()) {
411 meter_packer.remove (meter_metric_area);
417 GainMeter::setup_meters ()
419 uint32_t nmeters = _io.n_outputs();
426 if ((r = dynamic_cast<Route*> (&_io)) != 0) {
428 switch (r->meter_point()) {
431 nmeters = r->n_inputs();
434 nmeters = r->n_outputs();
440 nmeters = _io.n_outputs();
449 width = regular_meter_width;
451 width = thin_meter_width;
454 while (meters.size() < nmeters) {
455 meters.push_back (MeterInfo());
458 for (uint32_t n = 0; n < nmeters; ++n) {
459 if (meters[n].width != width) {
460 delete meters[n].meter;
461 meters[n].meter = new FastMeter ((uint32_t) floor (_session.meter_hold()), width, FastMeter::Vertical);
462 meters[n].width = width;
464 meters[n].meter->add_events (Gdk::BUTTON_RELEASE_MASK);
465 meters[n].meter->signal_button_release_event().connect (bind (mem_fun(*this, &GainMeter::meter_button_release), n));
468 meter_packer.pack_start (*meters[n].meter, Gtk::PACK_SHRINK);
469 meters[n].meter->show_all ();
470 meters[n].packed = true;
473 if (_width == Wide) {
474 meter_packer.pack_start (meter_metric_area, Gtk::PACK_SHRINK);
475 meter_metric_area.show_all ();
480 GainMeter::peak_button_release (GdkEventButton* ev)
482 /* reset peak label */
484 if (ev->button == 1 && Keyboard::modifier_state_equals (ev->state, Keyboard::Control|Keyboard::Shift)) {
485 ResetAllPeakDisplays ();
486 } else if (ev->button == 1 && Keyboard::modifier_state_equals (ev->state, Keyboard::Control)) {
488 if ((r = dynamic_cast<Route*> (&_io)) != 0) {
489 ResetGroupPeakDisplays (r->mix_group());
492 reset_peak_display ();
498 GainMeter::reset_peak_display ()
500 max_peak = minus_infinity();
501 peak_display_label.set_text (_("-Inf"));
502 peak_display.set_name ("Mixerstrippeakdisplay");
506 GainMeter::reset_group_peak_display (RouteGroup* group)
509 if ((r = dynamic_cast<Route*> (&_io)) != 0) {
510 if (group == r->mix_group()) {
511 reset_peak_display ();
517 GainMeter::meter_button_release (GdkEventButton* ev, uint32_t which)
519 switch (ev->button) {
521 meters[which].meter->clear();
522 max_peak = minus_infinity();
523 peak_display_label.set_text (_("-inf"));
524 peak_display.set_name ("MixerStripPeakDisplay");
528 // popup_meter_menu (ev);
536 GainMeter::popup_meter_menu (GdkEventButton *ev)
538 using namespace Menu_Helpers;
540 if (meter_menu == 0) {
541 meter_menu = new Gtk::Menu;
542 MenuList& items = meter_menu->items();
544 items.push_back (MenuElem ("-inf .. +0dBFS"));
545 items.push_back (MenuElem ("-10dB .. +0dBFS"));
546 items.push_back (MenuElem ("-4 .. +0dBFS"));
547 items.push_back (SeparatorElem());
548 items.push_back (MenuElem ("-inf .. -2dBFS"));
549 items.push_back (MenuElem ("-10dB .. -2dBFS"));
550 items.push_back (MenuElem ("-4 .. -2dBFS"));
553 meter_menu->popup (1, ev->time);
557 GainMeter::_gain_printer (char buf[32], Gtk::Adjustment& adj, void *arg)
559 static_cast<GainMeter *>(arg)->gain_printer (buf, adj);
563 GainMeter::gain_printer (char buf[32], Gtk::Adjustment& adj)
565 float v = adj.get_value();
568 strcpy (buf, _("-inf"));
570 snprintf (buf, 32, "%.1f", coefficient_to_dB (slider_position_to_gain (v)));
575 GainMeter::gain_adjusted ()
577 if (!ignore_toggle) {
578 _io.set_gain (slider_position_to_gain (gain_adjustment.get_value()), this);
583 GainMeter::effective_gain_display ()
585 gfloat value = gain_to_slider_position (_io.effective_gain());
587 if (gain_adjustment.get_value() != value) {
588 ignore_toggle = true;
589 gain_adjustment.set_value (value);
590 ignore_toggle = false;
595 GainMeter::gain_changed (void *src)
597 Gtkmm2ext::UI::instance()->call_slot (mem_fun(*this, &GainMeter::effective_gain_display));
601 GainMeter::set_meter_strip_name (const char * name)
603 meter_metric_area.set_name (name);
607 GainMeter::set_fader_name (const char * name)
609 gain_slider->set_name (name);
613 GainMeter::update_gain_sensitive ()
615 static_cast<Gtkmm2ext::SliderController*>(gain_slider)->set_sensitive (!(_io.gain_automation_state() & Play));
620 next_meter_point (MeterPoint mp)
624 return MeterPreFader;
628 return MeterPostFader;
640 GainMeter::meter_press(GdkEventButton* ev)
644 wait_for_release = false;
646 if ((_route = dynamic_cast<Route*>(&_io)) == 0) {
650 if (!ignore_toggle) {
652 if (Keyboard::is_context_menu_event (ev)) {
654 // no menu at this time.
658 if (ev->button == 2) {
660 // ctrl-button2 click is the midi binding click
661 // button2-click is "momentary"
663 if (!Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::Control))) {
664 wait_for_release = true;
665 old_meter_point = _route->meter_point ();
669 if (ev->button == 1 || ev->button == 2) {
671 if (Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::Control|Keyboard::Shift))) {
673 /* ctrl-shift-click applies change to all routes */
675 _session.foreach_route (this, &GainMeter::set_meter_point, next_meter_point (_route->meter_point()));
677 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::Control)) {
679 /* ctrl-click: solo mix group.
680 ctrl-button2 is MIDI learn.
683 if (ev->button == 1) {
684 set_mix_group_meter_point (*_route, next_meter_point (_route->meter_point()));
689 /* click: solo this route */
691 _route->set_meter_point (next_meter_point (_route->meter_point()), this);
702 GainMeter::meter_release(GdkEventButton* ev)
706 if (wait_for_release){
707 wait_for_release = false;
708 set_meter_point (*(dynamic_cast<Route*>(&_io)), old_meter_point);
715 GainMeter::set_meter_point (Route& route, MeterPoint mp)
717 route.set_meter_point (mp, this);
721 GainMeter::set_mix_group_meter_point (Route& route, MeterPoint mp)
723 RouteGroup* mix_group;
725 if((mix_group = route.mix_group()) != 0){
726 mix_group->apply (&Route::set_meter_point, mp, this);
728 route.set_meter_point (mp, this);
733 GainMeter::meter_changed (void *src)
737 ENSURE_GUI_THREAD (bind (mem_fun(*this, &GainMeter::meter_changed), src));
739 if ((r = dynamic_cast<Route*> (&_io)) != 0) {
741 switch (r->meter_point()) {
743 meter_point_label.set_text (_("input"));
747 meter_point_label.set_text (_("pre"));
751 meter_point_label.set_text (_("post"));
760 GainMeter::meter_point_clicked ()
764 if ((r = dynamic_cast<Route*> (&_io)) != 0) {
770 GainMeter::start_gain_touch (GdkEventButton* ev)
772 _io.start_gain_touch ();
777 GainMeter::end_gain_touch (GdkEventButton* ev)
779 _io.end_gain_touch ();
784 GainMeter::gain_automation_state_button_event (GdkEventButton *ev)
786 if (ev->type == GDK_BUTTON_RELEASE) {
790 switch (ev->button) {
792 gain_astate_menu.popup (1, ev->time);
802 GainMeter::gain_automation_style_button_event (GdkEventButton *ev)
804 if (ev->type == GDK_BUTTON_RELEASE) {
808 switch (ev->button) {
810 gain_astyle_menu.popup (1, ev->time);
819 GainMeter::astate_string (AutoState state)
821 return _astate_string (state, false);
825 GainMeter::short_astate_string (AutoState state)
827 return _astate_string (state, true);
831 GainMeter::_astate_string (AutoState state, bool shrt)
837 sstr = (shrt ? "I" : _("I"));
840 sstr = (shrt ? "P" : _("P"));
843 sstr = (shrt ? "T" : _("T"));
846 sstr = (shrt ? "W" : _("W"));
854 GainMeter::astyle_string (AutoStyle style)
856 return _astyle_string (style, false);
860 GainMeter::short_astyle_string (AutoStyle style)
862 return _astyle_string (style, true);
866 GainMeter::_astyle_string (AutoStyle style, bool shrt)
871 /* XXX it might different in different languages */
873 return (shrt ? _("Abs") : _("Abs"));
878 GainMeter::gain_automation_style_changed ()
880 Route* _route = dynamic_cast<Route*>(&_io);
883 gain_automation_style_button.set_label (astyle_string(_route->gain_automation_curve().automation_style()));
886 gain_automation_style_button.set_label (short_astyle_string(_route->gain_automation_curve().automation_style()));
892 GainMeter::gain_automation_state_changed ()
894 ENSURE_GUI_THREAD(mem_fun(*this, &GainMeter::gain_automation_state_changed));
895 Route* _route = dynamic_cast<Route*>(&_io);
901 gain_automation_state_button.set_label (astate_string(_route->gain_automation_curve().automation_state()));
904 gain_automation_state_button.set_label (short_astate_string(_route->gain_automation_curve().automation_state()));
908 x = (_route->gain_automation_state() != Off);
910 if (gain_automation_state_button.get_active() != x) {
911 ignore_toggle = true;
912 gain_automation_state_button.set_active (x);
913 ignore_toggle = false;
916 update_gain_sensitive ();
918 /* start watching automation so that things move */
920 gain_watching.disconnect();
923 gain_watching = ARDOUR_UI::RapidScreenUpdate.connect (mem_fun (*this, &GainMeter::effective_gain_display));