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"
43 #include "public_editor.h"
45 #include <ardour/session.h>
46 #include <ardour/route.h>
47 #include <ardour/meter.h>
51 using namespace ARDOUR;
53 using namespace Gtkmm2ext;
58 sigc::signal<void> GainMeter::ResetAllPeakDisplays;
59 sigc::signal<void,RouteGroup*> GainMeter::ResetGroupPeakDisplays;
60 Glib::RefPtr<Gdk::Pixbuf> GainMeter::slider;
61 Glib::RefPtr<Gdk::Pixbuf> GainMeter::rail;
62 map<string,Glib::RefPtr<Gdk::Pixmap> > GainMeter::metric_pixmaps;
65 GainMeter::setup_slider_pix ()
67 slider = ::get_icon ("fader_belt");
71 GainMeter::GainMeter (boost::shared_ptr<IO> io, Session& s)
75 // 0.781787 is the value needed for gain to be set to 0.
76 gain_adjustment (0.781787, 0.0, 1.0, 0.01, 0.1),
77 gain_automation_style_button (""),
78 gain_automation_state_button ("")
85 ignore_toggle = false;
87 next_release_selects = false;
89 gain_slider = manage (new VSliderController (slider,
94 gain_slider->signal_button_press_event().connect (mem_fun(*this, &GainMeter::start_gain_touch));
95 gain_slider->signal_button_release_event().connect (mem_fun(*this, &GainMeter::end_gain_touch));
96 gain_slider->set_name ("GainFader");
98 gain_display.set_name ("MixerStripGainDisplay");
99 gain_display.set_has_frame (false);
100 set_size_request_to_display_given_text (gain_display, "-80.g", 2, 6); /* note the descender */
101 gain_display.signal_activate().connect (mem_fun (*this, &GainMeter::gain_activated));
102 gain_display.signal_focus_in_event().connect (mem_fun (*this, &GainMeter::gain_focused), false);
103 gain_display.signal_focus_out_event().connect (mem_fun (*this, &GainMeter::gain_focused), false);
105 gain_display_box.set_homogeneous (true);
106 gain_display_box.set_spacing (2);
107 gain_display_box.pack_start (gain_display, true, true);
109 peak_display.set_name ("MixerStripPeakDisplay");
110 peak_display.set_has_frame (false);
111 peak_display.set_editable (false);
112 set_size_request_to_display_given_text (peak_display, "-80.g", 2, 6); /* note the descender */
113 max_peak = minus_infinity();
114 peak_display.set_text (_("-inf"));
115 peak_display.unset_flags (Gtk::CAN_FOCUS);
117 meter_metric_area.set_name ("MeterMetricsStrip");
118 set_size_request_to_display_given_text (meter_metric_area, "-50", 0, 0);
120 meter_packer.set_spacing (2);
122 gain_automation_style_button.set_name ("MixerAutomationModeButton");
123 gain_automation_state_button.set_name ("MixerAutomationPlaybackButton");
125 ARDOUR_UI::instance()->tooltips().set_tip (gain_automation_state_button, _("Fader automation mode"));
126 ARDOUR_UI::instance()->tooltips().set_tip (gain_automation_style_button, _("Fader automation type"));
128 gain_automation_style_button.unset_flags (Gtk::CAN_FOCUS);
129 gain_automation_state_button.unset_flags (Gtk::CAN_FOCUS);
131 gain_automation_state_button.set_size_request(15, 15);
132 gain_automation_style_button.set_size_request(15, 15);
134 HBox* fader_centering_box = manage (new HBox);
135 fader_centering_box->pack_start (*gain_slider, true, false);
137 fader_vbox = manage (new Gtk::VBox());
138 fader_vbox->set_spacing (0);
139 fader_vbox->pack_start (*fader_centering_box, false, false, 0);
141 hbox.set_spacing (2);
142 hbox.pack_start (*fader_vbox, true, true);
144 if (_io->default_type() == ARDOUR::DataType::AUDIO)
145 hbox.pack_start (*fader_vbox, false, false, 2);
151 if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
154 if we have a route (ie. we're not the click),
155 pack some route-dependent stuff.
158 gain_display_box.pack_end (peak_display, true, true);
160 hbox.pack_end (meter_packer, true, true);
162 using namespace Menu_Helpers;
164 gain_astate_menu.items().push_back (MenuElem (_("Off"),
165 bind (mem_fun (*_io, &IO::set_gain_automation_state), (AutoState) Off)));
166 gain_astate_menu.items().push_back (MenuElem (_("Play"),
167 bind (mem_fun (*_io, &IO::set_gain_automation_state), (AutoState) Play)));
168 gain_astate_menu.items().push_back (MenuElem (_("Write"),
169 bind (mem_fun (*_io, &IO::set_gain_automation_state), (AutoState) Write)));
170 gain_astate_menu.items().push_back (MenuElem (_("Touch"),
171 bind (mem_fun (*_io, &IO::set_gain_automation_state), (AutoState) Touch)));
173 gain_astyle_menu.items().push_back (MenuElem (_("Trim")));
174 gain_astyle_menu.items().push_back (MenuElem (_("Abs")));
176 gain_astate_menu.set_name ("ArdourContextMenu");
177 gain_astyle_menu.set_name ("ArdourContextMenu");
179 gain_automation_style_button.signal_button_press_event().connect (mem_fun(*this, &GainMeter::gain_automation_style_button_event), false);
180 gain_automation_state_button.signal_button_press_event().connect (mem_fun(*this, &GainMeter::gain_automation_state_button_event), false);
182 r->gain_automation_curve().automation_state_changed.connect (mem_fun(*this, &GainMeter::gain_automation_state_changed));
183 r->gain_automation_curve().automation_style_changed.connect (mem_fun(*this, &GainMeter::gain_automation_style_changed));
184 fader_vbox->pack_start (gain_automation_state_button, false, false, 0);
186 gain_automation_state_changed ();
191 pack_start (gain_display_box, Gtk::PACK_SHRINK);
192 pack_start (hbox, Gtk::PACK_SHRINK);
194 _io->gain_changed.connect (mem_fun(*this, &GainMeter::gain_changed));
196 meter_metric_area.signal_expose_event().connect (mem_fun(*this, &GainMeter::meter_metrics_expose));
197 gain_adjustment.signal_value_changed().connect (mem_fun(*this, &GainMeter::gain_adjusted));
198 peak_display.signal_button_release_event().connect (mem_fun(*this, &GainMeter::peak_button_release), false);
199 gain_display.signal_key_press_event().connect (mem_fun(*this, &GainMeter::gain_key_press), false);
201 Config->ParameterChanged.connect (mem_fun (*this, &GainMeter::parameter_changed));
206 update_gain_sensitive ();
208 ResetAllPeakDisplays.connect (mem_fun(*this, &GainMeter::reset_peak_display));
209 ResetGroupPeakDisplays.connect (mem_fun(*this, &GainMeter::reset_group_peak_display));
213 GainMeter::set_width (Width w)
228 Glib::RefPtr<Gdk::Pixmap>
229 GainMeter::render_metrics (Gtk::Widget& w)
231 Glib::RefPtr<Gdk::Window> win (w.get_window());
232 Glib::RefPtr<Gdk::GC> fg_gc (w.get_style()->get_fg_gc (Gtk::STATE_NORMAL));
233 Glib::RefPtr<Gdk::GC> bg_gc (w.get_style()->get_bg_gc (Gtk::STATE_NORMAL));
234 gint x, y, width, height, depth;
235 int db_points[] = { -50, -40, -20, -30, -10, -3, 0, 4 };
238 win->get_geometry (x, y, width, height, depth);
240 Glib::RefPtr<Gdk::Pixmap> pixmap = Gdk::Pixmap::create (win, width, height);
242 metric_pixmaps[w.get_name()] = pixmap;
244 pixmap->draw_rectangle (bg_gc, true, 0, 0, width, height);
246 Glib::RefPtr<Pango::Layout> layout = w.create_pango_layout("");
248 for (uint32_t i = 0; i < sizeof (db_points)/sizeof (db_points[0]); ++i) {
250 float fraction = log_meter (db_points[i]);
251 gint pos = height - (gint) floor (height * fraction);
253 snprintf (buf, sizeof (buf), "%d", abs (db_points[i]));
255 layout->set_text (buf);
257 /* we want logical extents, not ink extents here */
260 layout->get_pixel_size (width, height);
262 pixmap->draw_line (fg_gc, 0, pos, 4, pos);
263 pixmap->draw_layout (fg_gc, 6, pos - (height/2), layout);
270 GainMeter::meter_metrics_expose (GdkEventExpose *ev)
272 Glib::RefPtr<Gdk::Window> win (meter_metric_area.get_window());
273 Glib::RefPtr<Gdk::GC> fg_gc (meter_metric_area.get_style()->get_fg_gc (Gtk::STATE_NORMAL));
274 Glib::RefPtr<Gdk::GC> bg_gc (meter_metric_area.get_style()->get_bg_gc (Gtk::STATE_NORMAL));
275 GdkRectangle base_rect;
276 GdkRectangle draw_rect;
277 gint x, y, width, height, depth;
279 win->get_geometry (x, y, width, height, depth);
281 base_rect.width = width;
282 base_rect.height = height;
286 Glib::RefPtr<Gdk::Pixmap> pixmap;
287 std::map<string,Glib::RefPtr<Gdk::Pixmap> >::iterator i = metric_pixmaps.find (meter_metric_area.get_name());
289 if (i == metric_pixmaps.end()) {
290 pixmap = render_metrics (meter_metric_area);
295 gdk_rectangle_intersect (&ev->area, &base_rect, &draw_rect);
296 win->draw_rectangle (bg_gc, true, draw_rect.x, draw_rect.y, draw_rect.width, draw_rect.height);
297 win->draw_drawable (bg_gc, pixmap, draw_rect.x, draw_rect.y, draw_rect.x, draw_rect.y, draw_rect.width, draw_rect.height);
302 GainMeter::~GainMeter ()
308 for (vector<MeterInfo>::iterator i = meters.begin(); i != meters.end(); i++) {
316 GainMeter::update_meters ()
318 vector<MeterInfo>::iterator i;
323 for (n = 0, i = meters.begin(); i != meters.end(); ++i, ++n) {
325 peak = _io->peak_meter().peak_power (n);
327 (*i).meter->set (log_meter (peak), peak);
329 mpeak = _io->peak_meter().max_peak_power(n);
331 if (mpeak > max_peak) {
333 /* set peak display */
334 if (max_peak <= -200.0f) {
335 peak_display.set_text (_("-inf"));
337 snprintf (buf, sizeof(buf), "%.1f", max_peak);
338 peak_display.set_text (buf);
341 if (max_peak >= 0.0f) {
342 peak_display.set_name ("MixerStripPeakDisplayPeak");
350 GainMeter::parameter_changed(const char* parameter_name)
352 #define PARAM_IS(x) (!strcmp (parameter_name, (x)))
354 ENSURE_GUI_THREAD (bind (mem_fun(*this, &GainMeter::parameter_changed), parameter_name));
356 if (PARAM_IS ("meter-hold")) {
358 vector<MeterInfo>::iterator i;
361 for (n = 0, i = meters.begin(); i != meters.end(); ++i, ++n) {
363 (*i).meter->set_hold_count ((uint32_t) floor(Config->get_meter_hold()));
371 GainMeter::hide_all_meters ()
373 bool remove_metric_area = false;
375 for (vector<MeterInfo>::iterator i = meters.begin(); i != meters.end(); ++i) {
377 remove_metric_area = true;
378 meter_packer.remove (*((*i).meter));
383 if (remove_metric_area) {
384 if (meter_metric_area.get_parent()) {
385 meter_packer.remove (meter_metric_area);
391 GainMeter::setup_meters ()
393 uint32_t nmeters = _io->n_outputs().get(DataType::AUDIO);
400 if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
402 switch (r->meter_point()) {
405 nmeters = r->n_inputs().get(DataType::AUDIO);
408 nmeters = r->n_outputs().get(DataType::AUDIO);
414 nmeters = _io->n_outputs().get(DataType::AUDIO);
423 width = regular_meter_width;
425 width = thin_meter_width;
428 while (meters.size() < nmeters) {
429 meters.push_back (MeterInfo());
432 /* pack them backwards */
434 if (_width == Wide) {
435 meter_packer.pack_end (meter_metric_area, false, false);
436 meter_metric_area.show_all ();
439 for (int32_t n = nmeters-1; nmeters && n >= 0 ; --n) {
440 if (meters[n].width != width) {
441 delete meters[n].meter;
442 meters[n].meter = new FastMeter ((uint32_t) floor (Config->get_meter_hold()), width, FastMeter::Vertical);
443 meters[n].width = width;
445 meters[n].meter->add_events (Gdk::BUTTON_RELEASE_MASK);
446 meters[n].meter->signal_button_release_event().connect (bind (mem_fun(*this, &GainMeter::meter_button_release), n));
449 meter_packer.pack_end (*meters[n].meter, false, false);
450 meters[n].meter->show_all ();
451 meters[n].packed = true;
456 GainMeter::gain_key_press (GdkEventKey* ev)
458 if (key_is_legal_for_numeric_entry (ev->keyval)) {
459 /* drop through to normal handling */
462 /* illegal key for gain entry */
467 GainMeter::peak_button_release (GdkEventButton* ev)
469 /* reset peak label */
471 if (ev->button == 1 && Keyboard::modifier_state_equals (ev->state, Keyboard::Control|Keyboard::Shift)) {
472 ResetAllPeakDisplays ();
473 } else if (ev->button == 1 && Keyboard::modifier_state_equals (ev->state, Keyboard::Control)) {
475 if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
476 ResetGroupPeakDisplays (r->mix_group());
479 reset_peak_display ();
486 GainMeter::reset_peak_display ()
489 if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
490 r->peak_meter().reset_max();
493 max_peak = -INFINITY;
494 peak_display.set_text (_("-Inf"));
495 peak_display.set_name ("MixerStripPeakDisplay");
499 GainMeter::reset_group_peak_display (RouteGroup* group)
502 if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
503 if (group == r->mix_group()) {
504 reset_peak_display ();
510 GainMeter::meter_button_release (GdkEventButton* ev, uint32_t which)
512 switch (ev->button) {
514 meters[which].meter->clear();
515 max_peak = minus_infinity();
516 peak_display.set_text (_("-inf"));
517 peak_display.set_name ("MixerStripPeakDisplay");
521 // popup_meter_menu (ev);
529 GainMeter::popup_meter_menu (GdkEventButton *ev)
531 using namespace Menu_Helpers;
533 if (meter_menu == 0) {
534 meter_menu = new Gtk::Menu;
535 MenuList& items = meter_menu->items();
537 items.push_back (MenuElem ("-inf .. +0dBFS"));
538 items.push_back (MenuElem ("-10dB .. +0dBFS"));
539 items.push_back (MenuElem ("-4 .. +0dBFS"));
540 items.push_back (SeparatorElem());
541 items.push_back (MenuElem ("-inf .. -2dBFS"));
542 items.push_back (MenuElem ("-10dB .. -2dBFS"));
543 items.push_back (MenuElem ("-4 .. -2dBFS"));
546 meter_menu->popup (1, ev->time);
550 GainMeter::gain_focused (GdkEventFocus* ev)
553 gain_display.select_region (0, -1);
555 gain_display.select_region (0, 0);
561 GainMeter::gain_activated ()
565 if (sscanf (gain_display.get_text().c_str(), "%f", &f) == 1) {
567 /* clamp to displayable values */
571 _io->set_gain (dB_to_coefficient(f), this);
573 if (gain_display.has_focus()) {
574 PublicEditor::instance().reset_focus();
580 GainMeter::show_gain ()
584 float v = gain_adjustment.get_value();
587 strcpy (buf, _("-inf"));
589 snprintf (buf, 32, "%.1f", coefficient_to_dB (slider_position_to_gain (v)));
592 gain_display.set_text (buf);
596 GainMeter::gain_adjusted ()
598 if (!ignore_toggle) {
599 _io->set_gain (slider_position_to_gain (gain_adjustment.get_value()), this);
605 GainMeter::effective_gain_display ()
607 gfloat value = gain_to_slider_position (_io->effective_gain());
609 if (gain_adjustment.get_value() != value) {
610 ignore_toggle = true;
611 gain_adjustment.set_value (value);
612 ignore_toggle = false;
617 GainMeter::gain_changed (void *src)
619 Gtkmm2ext::UI::instance()->call_slot (mem_fun(*this, &GainMeter::effective_gain_display));
623 GainMeter::set_meter_strip_name (const char * name)
625 meter_metric_area.set_name (name);
629 GainMeter::set_fader_name (const char * name)
631 gain_slider->set_name (name);
635 GainMeter::update_gain_sensitive ()
637 static_cast<Gtkmm2ext::SliderController*>(gain_slider)->set_sensitive (!(_io->gain_automation_state() & Play));
642 next_meter_point (MeterPoint mp)
646 return MeterPreFader;
650 return MeterPostFader;
662 GainMeter::meter_press(GdkEventButton* ev)
666 wait_for_release = false;
668 if ((_route = dynamic_cast<Route*>(_io.get())) == 0) {
672 if (!ignore_toggle) {
674 if (Keyboard::is_context_menu_event (ev)) {
676 // no menu at this time.
680 if (ev->button == 2) {
682 // ctrl-button2 click is the midi binding click
683 // button2-click is "momentary"
685 if (!Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::Control))) {
686 wait_for_release = true;
687 old_meter_point = _route->meter_point ();
691 if (ev->button == 1 || ev->button == 2) {
693 if (Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::Control|Keyboard::Shift))) {
695 /* ctrl-shift-click applies change to all routes */
697 _session.begin_reversible_command (_("meter point change"));
698 Session::GlobalMeteringStateCommand *cmd = new Session::GlobalMeteringStateCommand (_session, this);
699 _session.foreach_route (this, &GainMeter::set_meter_point, next_meter_point (_route->meter_point()));
701 _session.add_command (cmd);
702 _session.commit_reversible_command ();
705 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::Control)) {
707 /* ctrl-click: solo mix group.
708 ctrl-button2 is MIDI learn.
711 if (ev->button == 1) {
712 _session.begin_reversible_command (_("meter point change"));
713 Session::GlobalMeteringStateCommand *cmd = new Session::GlobalMeteringStateCommand (_session, this);
714 set_mix_group_meter_point (*_route, next_meter_point (_route->meter_point()));
716 _session.add_command (cmd);
717 _session.commit_reversible_command ();
722 /* click: change just this route */
726 _route->set_meter_point (next_meter_point (_route->meter_point()), this);
737 GainMeter::meter_release(GdkEventButton* ev)
741 if (wait_for_release){
742 wait_for_release = false;
743 set_meter_point (*(dynamic_cast<Route*>(_io.get())), old_meter_point);
750 GainMeter::set_meter_point (Route& route, MeterPoint mp)
752 route.set_meter_point (mp, this);
756 GainMeter::set_mix_group_meter_point (Route& route, MeterPoint mp)
758 RouteGroup* mix_group;
760 if((mix_group = route.mix_group()) != 0){
761 mix_group->apply (&Route::set_meter_point, mp, this);
763 route.set_meter_point (mp, this);
768 GainMeter::meter_point_clicked ()
772 if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
778 GainMeter::start_gain_touch (GdkEventButton* ev)
780 _io->start_gain_touch ();
785 GainMeter::end_gain_touch (GdkEventButton* ev)
787 _io->end_gain_touch ();
792 GainMeter::gain_automation_state_button_event (GdkEventButton *ev)
794 if (ev->type == GDK_BUTTON_RELEASE) {
798 switch (ev->button) {
800 gain_astate_menu.popup (1, ev->time);
810 GainMeter::gain_automation_style_button_event (GdkEventButton *ev)
812 if (ev->type == GDK_BUTTON_RELEASE) {
816 switch (ev->button) {
818 gain_astyle_menu.popup (1, ev->time);
827 GainMeter::astate_string (AutoState state)
829 return _astate_string (state, false);
833 GainMeter::short_astate_string (AutoState state)
835 return _astate_string (state, true);
839 GainMeter::_astate_string (AutoState state, bool shrt)
845 sstr = (shrt ? "O" : _("O"));
848 sstr = (shrt ? "P" : _("P"));
851 sstr = (shrt ? "T" : _("T"));
854 sstr = (shrt ? "W" : _("W"));
862 GainMeter::astyle_string (AutoStyle style)
864 return _astyle_string (style, false);
868 GainMeter::short_astyle_string (AutoStyle style)
870 return _astyle_string (style, true);
874 GainMeter::_astyle_string (AutoStyle style, bool shrt)
879 /* XXX it might different in different languages */
881 return (shrt ? _("Abs") : _("Abs"));
886 GainMeter::gain_automation_style_changed ()
888 // Route* _route = dynamic_cast<Route*>(&_io);
891 gain_automation_style_button.set_label (astyle_string(_io->gain_automation_curve().automation_style()));
894 gain_automation_style_button.set_label (short_astyle_string(_io->gain_automation_curve().automation_style()));
900 GainMeter::gain_automation_state_changed ()
902 ENSURE_GUI_THREAD(mem_fun(*this, &GainMeter::gain_automation_state_changed));
903 //Route* _route = dynamic_cast<Route*>(&_io);
909 gain_automation_state_button.set_label (astate_string(_io->gain_automation_curve().automation_state()));
912 gain_automation_state_button.set_label (short_astate_string(_io->gain_automation_curve().automation_state()));
916 x = (_io->gain_automation_state() != Off);
918 if (gain_automation_state_button.get_active() != x) {
919 ignore_toggle = true;
920 gain_automation_state_button.set_active (x);
921 ignore_toggle = false;
924 update_gain_sensitive ();
926 /* start watching automation so that things move */
928 gain_watching.disconnect();
931 gain_watching = ARDOUR_UI::RapidScreenUpdate.connect (mem_fun (*this, &GainMeter::effective_gain_display));