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 <midi++/manager.h>
36 #include <pbd/fastlog.h>
38 #include "ardour_ui.h"
39 #include "gain_meter.h"
42 #include "gui_thread.h"
44 #include "public_editor.h"
46 #include <ardour/session.h>
47 #include <ardour/route.h>
48 #include <ardour/meter.h>
52 using namespace ARDOUR;
54 using namespace Gtkmm2ext;
59 sigc::signal<void> GainMeter::ResetAllPeakDisplays;
60 sigc::signal<void,RouteGroup*> GainMeter::ResetGroupPeakDisplays;
61 Glib::RefPtr<Gdk::Pixbuf> GainMeter::slider;
62 Glib::RefPtr<Gdk::Pixbuf> GainMeter::rail;
63 map<string,Glib::RefPtr<Gdk::Pixmap> > GainMeter::metric_pixmaps;
66 GainMeter::setup_slider_pix ()
68 slider = ::get_icon ("fader_belt");
72 GainMeter::GainMeter (boost::shared_ptr<IO> io, Session& s)
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 ("")
86 ignore_toggle = false;
88 next_release_selects = false;
91 gain_slider = manage (new VSliderController (slider,
96 gain_slider->signal_button_press_event().connect (mem_fun(*this, &GainMeter::start_gain_touch));
97 gain_slider->signal_button_release_event().connect (mem_fun(*this, &GainMeter::end_gain_touch));
98 gain_slider->set_name ("GainFader");
100 gain_display.set_name ("MixerStripGainDisplay");
101 gain_display.set_has_frame (false);
102 set_size_request_to_display_given_text (gain_display, "-80.g", 2, 6); /* note the descender */
103 gain_display.signal_activate().connect (mem_fun (*this, &GainMeter::gain_activated));
104 gain_display.signal_focus_in_event().connect (mem_fun (*this, &GainMeter::gain_focused), false);
105 gain_display.signal_focus_out_event().connect (mem_fun (*this, &GainMeter::gain_focused), false);
107 gain_display_box.set_name ("MeterMetricsStrip");
108 gain_display_box.set_homogeneous (true);
109 gain_display_box.set_spacing (2);
110 gain_display_box.pack_start (gain_display, true, true);
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 meter_metric_area.set_name ("MeterMetricsStrip");
121 set_size_request_to_display_given_text (meter_metric_area, "-50", 0, 0);
123 meter_packer.set_spacing (2);
125 gain_automation_style_button.set_name ("MixerAutomationModeButton");
126 gain_automation_state_button.set_name ("MixerAutomationPlaybackButton");
128 ARDOUR_UI::instance()->tooltips().set_tip (gain_automation_state_button, _("Fader automation mode"));
129 ARDOUR_UI::instance()->tooltips().set_tip (gain_automation_style_button, _("Fader automation type"));
131 gain_automation_style_button.unset_flags (Gtk::CAN_FOCUS);
132 gain_automation_state_button.unset_flags (Gtk::CAN_FOCUS);
134 gain_automation_state_button.set_size_request(15, 15);
135 gain_automation_style_button.set_size_request(15, 15);
137 HBox* fader_centering_box = manage (new HBox);
138 fader_centering_box->pack_start (*gain_slider, true, false);
140 fader_vbox = manage (new Gtk::VBox());
141 fader_vbox->set_spacing (0);
142 fader_vbox->pack_start (*fader_centering_box, false, false, 0);
144 hbox.set_spacing (2);
146 if (_io->default_type() == ARDOUR::DataType::AUDIO) {
147 hbox.pack_start (*fader_vbox, true, true);
154 if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
157 if we have a route (ie. we're not the click),
158 pack some route-dependent stuff.
161 gain_display_box.pack_end (peak_display, true, true);
163 hbox.pack_end (meter_packer, true, true);
165 using namespace Menu_Helpers;
167 gain_astate_menu.items().push_back (MenuElem (_("Manual"),
168 bind (mem_fun (*_io, &IO::set_gain_automation_state), (AutoState) Off)));
169 gain_astate_menu.items().push_back (MenuElem (_("Play"),
170 bind (mem_fun (*_io, &IO::set_gain_automation_state), (AutoState) Play)));
171 gain_astate_menu.items().push_back (MenuElem (_("Write"),
172 bind (mem_fun (*_io, &IO::set_gain_automation_state), (AutoState) Write)));
173 gain_astate_menu.items().push_back (MenuElem (_("Touch"),
174 bind (mem_fun (*_io, &IO::set_gain_automation_state), (AutoState) Touch)));
176 gain_astyle_menu.items().push_back (MenuElem (_("Trim")));
177 gain_astyle_menu.items().push_back (MenuElem (_("Abs")));
179 gain_astate_menu.set_name ("ArdourContextMenu");
180 gain_astyle_menu.set_name ("ArdourContextMenu");
182 gain_automation_style_button.signal_button_press_event().connect (mem_fun(*this, &GainMeter::gain_automation_style_button_event), false);
183 gain_automation_state_button.signal_button_press_event().connect (mem_fun(*this, &GainMeter::gain_automation_state_button_event), false);
185 r->gain_automation_curve().automation_state_changed.connect (mem_fun(*this, &GainMeter::gain_automation_state_changed));
186 r->gain_automation_curve().automation_style_changed.connect (mem_fun(*this, &GainMeter::gain_automation_style_changed));
187 fader_vbox->pack_start (gain_automation_state_button, false, false, 0);
189 gain_automation_state_changed ();
194 pack_start (gain_display_box, Gtk::PACK_SHRINK);
195 pack_start (hbox, Gtk::PACK_SHRINK);
197 _io->gain_changed.connect (mem_fun(*this, &GainMeter::gain_changed));
199 meter_metric_area.signal_expose_event().connect (mem_fun(*this, &GainMeter::meter_metrics_expose));
200 gain_adjustment.signal_value_changed().connect (mem_fun(*this, &GainMeter::gain_adjusted));
201 peak_display.signal_button_release_event().connect (mem_fun(*this, &GainMeter::peak_button_release), false);
202 gain_display.signal_key_press_event().connect (mem_fun(*this, &GainMeter::gain_key_press), false);
204 Config->ParameterChanged.connect (mem_fun (*this, &GainMeter::parameter_changed));
209 update_gain_sensitive ();
211 ResetAllPeakDisplays.connect (mem_fun(*this, &GainMeter::reset_peak_display));
212 ResetGroupPeakDisplays.connect (mem_fun(*this, &GainMeter::reset_group_peak_display));
214 ThemeChanged.connect (mem_fun(*this, &GainMeter::redraw_pixmaps));
218 GainMeter::set_width (Width w)
233 Glib::RefPtr<Gdk::Pixmap>
234 GainMeter::render_metrics (Gtk::Widget& w)
236 cerr << "GainMeter::render_metrics() called, red = " << w.get_style()->get_bg(Gtk::STATE_NORMAL).get_red() << endl;//DEBUG
237 Glib::RefPtr<Gdk::Window> win (w.get_window());
238 Glib::RefPtr<Gdk::GC> fg_gc (w.get_style()->get_fg_gc (Gtk::STATE_NORMAL));
239 Glib::RefPtr<Gdk::GC> bg_gc (w.get_style()->get_bg_gc (Gtk::STATE_NORMAL));
241 int db_points[] = { -50, -40, -20, -30, -10, -3, 0, 4 };
244 win->get_size (width, height);
246 Glib::RefPtr<Gdk::Pixmap> pixmap = Gdk::Pixmap::create (win, width, height);
248 metric_pixmaps[w.get_name()] = pixmap;
250 pixmap->draw_rectangle (bg_gc, true, 0, 0, width, height);
252 Glib::RefPtr<Pango::Layout> layout = w.create_pango_layout("");
254 for (uint32_t i = 0; i < sizeof (db_points)/sizeof (db_points[0]); ++i) {
256 float fraction = log_meter (db_points[i]);
257 gint pos = height - (gint) floor (height * fraction);
259 snprintf (buf, sizeof (buf), "%d", abs (db_points[i]));
261 layout->set_text (buf);
263 /* we want logical extents, not ink extents here */
266 layout->get_pixel_size (width, height);
268 pixmap->draw_line (fg_gc, 0, pos, 4, pos);
269 pixmap->draw_layout (fg_gc, 6, pos - (height/2), layout);
276 GainMeter::meter_metrics_expose (GdkEventExpose *ev)
278 static Glib::RefPtr<Gtk::Style> meter_style;
281 meter_style = meter_metric_area.get_style();
284 Glib::RefPtr<Gdk::Window> win (meter_metric_area.get_window());
285 Glib::RefPtr<Gdk::GC> fg_gc (meter_style->get_fg_gc (Gtk::STATE_NORMAL));
286 Glib::RefPtr<Gdk::GC> bg_gc (meter_style->get_bg_gc (Gtk::STATE_NORMAL));
288 GdkRectangle base_rect;
289 GdkRectangle draw_rect;
292 win->get_size (width, height);
294 base_rect.width = width;
295 base_rect.height = height;
299 Glib::RefPtr<Gdk::Pixmap> pixmap;
300 std::map<string,Glib::RefPtr<Gdk::Pixmap> >::iterator i = metric_pixmaps.find (meter_metric_area.get_name());
302 if (i == metric_pixmaps.end() || style_changed) {
303 pixmap = render_metrics (meter_metric_area);
308 gdk_rectangle_intersect (&ev->area, &base_rect, &draw_rect);
309 win->draw_rectangle (bg_gc, true, draw_rect.x, draw_rect.y, draw_rect.width, draw_rect.height);
310 win->draw_drawable (fg_gc, pixmap, draw_rect.x, draw_rect.y, draw_rect.x, draw_rect.y, draw_rect.width, draw_rect.height);
312 style_changed = false;
317 GainMeter::redraw_pixmaps(string blah)
319 style_changed = true;
323 GainMeter::~GainMeter ()
329 for (vector<MeterInfo>::iterator i = meters.begin(); i != meters.end(); i++) {
337 GainMeter::update_meters ()
339 vector<MeterInfo>::iterator i;
344 for (n = 0, i = meters.begin(); i != meters.end(); ++i, ++n) {
346 peak = _io->peak_meter().peak_power (n);
348 (*i).meter->set (log_meter (peak));
350 mpeak = _io->peak_meter().max_peak_power(n);
352 if (mpeak > max_peak) {
354 /* set peak display */
355 if (max_peak <= -200.0f) {
356 peak_display.set_label (_("-inf"));
358 snprintf (buf, sizeof(buf), "%.1f", max_peak);
359 peak_display.set_label (buf);
362 if (max_peak >= 0.0f) {
363 peak_display.set_name ("MixerStripPeakDisplayPeak");
371 GainMeter::parameter_changed(const char* parameter_name)
373 #define PARAM_IS(x) (!strcmp (parameter_name, (x)))
375 ENSURE_GUI_THREAD (bind (mem_fun(*this, &GainMeter::parameter_changed), parameter_name));
377 if (PARAM_IS ("meter-hold")) {
379 vector<MeterInfo>::iterator i;
382 for (n = 0, i = meters.begin(); i != meters.end(); ++i, ++n) {
384 (*i).meter->set_hold_count ((uint32_t) floor(Config->get_meter_hold()));
392 GainMeter::hide_all_meters ()
394 bool remove_metric_area = false;
396 for (vector<MeterInfo>::iterator i = meters.begin(); i != meters.end(); ++i) {
398 remove_metric_area = true;
399 meter_packer.remove (*((*i).meter));
404 if (remove_metric_area) {
405 if (meter_metric_area.get_parent()) {
406 meter_packer.remove (meter_metric_area);
412 GainMeter::setup_meters ()
414 uint32_t nmeters = _io->n_outputs().n_audio();
421 if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
423 switch (r->meter_point()) {
426 nmeters = r->n_inputs().n_audio();
429 nmeters = r->n_outputs().n_audio();
435 nmeters = _io->n_outputs().n_audio();
444 width = regular_meter_width;
446 width = thin_meter_width;
449 while (meters.size() < nmeters) {
450 meters.push_back (MeterInfo());
453 /* pack them backwards */
455 if (_width == Wide) {
456 meter_packer.pack_end (meter_metric_area, false, false);
457 meter_metric_area.show_all ();
460 for (int32_t n = nmeters-1; nmeters && n >= 0 ; --n) {
461 if (meters[n].width != width) {
462 delete meters[n].meter;
463 meters[n].meter = new FastMeter ((uint32_t) floor (Config->get_meter_hold()), width, FastMeter::Vertical);
464 meters[n].width = width;
466 meters[n].meter->add_events (Gdk::BUTTON_RELEASE_MASK);
467 meters[n].meter->signal_button_release_event().connect (bind (mem_fun(*this, &GainMeter::meter_button_release), n));
470 meter_packer.pack_end (*meters[n].meter, false, false);
471 meters[n].meter->show_all ();
472 meters[n].packed = true;
477 GainMeter::get_gm_width ()
479 Gtk::Requisition sz = hbox.size_request ();
484 GainMeter::gain_key_press (GdkEventKey* ev)
486 if (key_is_legal_for_numeric_entry (ev->keyval)) {
487 /* drop through to normal handling */
490 /* illegal key for gain entry */
495 GainMeter::peak_button_release (GdkEventButton* ev)
497 /* reset peak label */
499 if (ev->button == 1 && Keyboard::modifier_state_equals (ev->state, Keyboard::Control|Keyboard::Shift)) {
500 ResetAllPeakDisplays ();
501 } else if (ev->button == 1 && Keyboard::modifier_state_equals (ev->state, Keyboard::Control)) {
503 if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
504 ResetGroupPeakDisplays (r->mix_group());
507 reset_peak_display ();
514 GainMeter::reset_peak_display ()
517 if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
518 r->peak_meter().reset_max();
521 max_peak = -INFINITY;
522 peak_display.set_label (_("-Inf"));
523 peak_display.set_name ("MixerStripPeakDisplay");
527 GainMeter::reset_group_peak_display (RouteGroup* group)
530 if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
531 if (group == r->mix_group()) {
532 reset_peak_display ();
538 GainMeter::meter_button_release (GdkEventButton* ev, uint32_t which)
540 switch (ev->button) {
542 meters[which].meter->clear();
543 max_peak = minus_infinity();
544 peak_display.set_label (_("-inf"));
545 peak_display.set_name ("MixerStripPeakDisplay");
549 // popup_meter_menu (ev);
557 GainMeter::popup_meter_menu (GdkEventButton *ev)
559 using namespace Menu_Helpers;
561 if (meter_menu == 0) {
562 meter_menu = new Gtk::Menu;
563 MenuList& items = meter_menu->items();
565 items.push_back (MenuElem ("-inf .. +0dBFS"));
566 items.push_back (MenuElem ("-10dB .. +0dBFS"));
567 items.push_back (MenuElem ("-4 .. +0dBFS"));
568 items.push_back (SeparatorElem());
569 items.push_back (MenuElem ("-inf .. -2dBFS"));
570 items.push_back (MenuElem ("-10dB .. -2dBFS"));
571 items.push_back (MenuElem ("-4 .. -2dBFS"));
574 meter_menu->popup (1, ev->time);
578 GainMeter::gain_focused (GdkEventFocus* ev)
581 gain_display.select_region (0, -1);
583 gain_display.select_region (0, 0);
589 GainMeter::gain_activated ()
593 if (sscanf (gain_display.get_text().c_str(), "%f", &f) == 1) {
595 /* clamp to displayable values */
599 _io->set_gain (dB_to_coefficient(f), this);
601 if (gain_display.has_focus()) {
602 PublicEditor::instance().reset_focus();
608 GainMeter::show_gain ()
612 float v = gain_adjustment.get_value();
615 strcpy (buf, _("-inf"));
617 snprintf (buf, 32, "%.1f", coefficient_to_dB (slider_position_to_gain (v)));
620 gain_display.set_text (buf);
624 GainMeter::gain_adjusted ()
626 if (!ignore_toggle) {
627 _io->set_gain (slider_position_to_gain (gain_adjustment.get_value()), this);
633 GainMeter::effective_gain_display ()
635 gfloat value = gain_to_slider_position (_io->effective_gain());
637 if (gain_adjustment.get_value() != value) {
638 ignore_toggle = true;
639 gain_adjustment.set_value (value);
640 ignore_toggle = false;
645 GainMeter::gain_changed (void *src)
647 Gtkmm2ext::UI::instance()->call_slot (mem_fun(*this, &GainMeter::effective_gain_display));
651 GainMeter::set_meter_strip_name (const char * name)
653 meter_metric_area.set_name (name);
657 GainMeter::set_fader_name (const char * name)
659 gain_slider->set_name (name);
663 GainMeter::update_gain_sensitive ()
665 static_cast<Gtkmm2ext::SliderController*>(gain_slider)->set_sensitive (!(_io->gain_automation_state() & Play));
670 next_meter_point (MeterPoint mp)
674 return MeterPreFader;
678 return MeterPostFader;
690 GainMeter::meter_press(GdkEventButton* ev)
694 wait_for_release = false;
696 if ((_route = dynamic_cast<Route*>(_io.get())) == 0) {
700 if (!ignore_toggle) {
702 if (Keyboard::is_context_menu_event (ev)) {
704 // no menu at this time.
708 if (ev->button == 2) {
710 // ctrl-button2 click is the midi binding click
711 // button2-click is "momentary"
713 if (!Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::Control))) {
714 wait_for_release = true;
715 old_meter_point = _route->meter_point ();
719 if (ev->button == 1 || ev->button == 2) {
721 if (Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::Control|Keyboard::Shift))) {
723 /* ctrl-shift-click applies change to all routes */
725 _session.begin_reversible_command (_("meter point change"));
726 Session::GlobalMeteringStateCommand *cmd = new Session::GlobalMeteringStateCommand (_session, this);
727 _session.foreach_route (this, &GainMeter::set_meter_point, next_meter_point (_route->meter_point()));
729 _session.add_command (cmd);
730 _session.commit_reversible_command ();
733 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::Control)) {
735 /* ctrl-click: solo mix group.
736 ctrl-button2 is MIDI learn.
739 if (ev->button == 1) {
740 _session.begin_reversible_command (_("meter point change"));
741 Session::GlobalMeteringStateCommand *cmd = new Session::GlobalMeteringStateCommand (_session, this);
742 set_mix_group_meter_point (*_route, next_meter_point (_route->meter_point()));
744 _session.add_command (cmd);
745 _session.commit_reversible_command ();
750 /* click: change just this route */
754 _route->set_meter_point (next_meter_point (_route->meter_point()), this);
765 GainMeter::meter_release(GdkEventButton* ev)
769 if (wait_for_release){
770 wait_for_release = false;
771 set_meter_point (*(dynamic_cast<Route*>(_io.get())), old_meter_point);
778 GainMeter::set_meter_point (Route& route, MeterPoint mp)
780 route.set_meter_point (mp, this);
784 GainMeter::set_mix_group_meter_point (Route& route, MeterPoint mp)
786 RouteGroup* mix_group;
788 if((mix_group = route.mix_group()) != 0){
789 mix_group->apply (&Route::set_meter_point, mp, this);
791 route.set_meter_point (mp, this);
796 GainMeter::meter_point_clicked ()
800 if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
806 GainMeter::start_gain_touch (GdkEventButton* ev)
808 _io->start_gain_touch ();
813 GainMeter::end_gain_touch (GdkEventButton* ev)
815 _io->end_gain_touch ();
820 GainMeter::gain_automation_state_button_event (GdkEventButton *ev)
822 if (ev->type == GDK_BUTTON_RELEASE) {
826 switch (ev->button) {
828 gain_astate_menu.popup (1, ev->time);
838 GainMeter::gain_automation_style_button_event (GdkEventButton *ev)
840 if (ev->type == GDK_BUTTON_RELEASE) {
844 switch (ev->button) {
846 gain_astyle_menu.popup (1, ev->time);
855 GainMeter::astate_string (AutoState state)
857 return _astate_string (state, false);
861 GainMeter::short_astate_string (AutoState state)
863 return _astate_string (state, true);
867 GainMeter::_astate_string (AutoState state, bool shrt)
873 sstr = (shrt ? "M" : _("M"));
876 sstr = (shrt ? "P" : _("P"));
879 sstr = (shrt ? "T" : _("T"));
882 sstr = (shrt ? "W" : _("W"));
890 GainMeter::astyle_string (AutoStyle style)
892 return _astyle_string (style, false);
896 GainMeter::short_astyle_string (AutoStyle style)
898 return _astyle_string (style, true);
902 GainMeter::_astyle_string (AutoStyle style, bool shrt)
907 /* XXX it might different in different languages */
909 return (shrt ? _("Abs") : _("Abs"));
914 GainMeter::gain_automation_style_changed ()
916 // Route* _route = dynamic_cast<Route*>(&_io);
919 gain_automation_style_button.set_label (astyle_string(_io->gain_automation_curve().automation_style()));
922 gain_automation_style_button.set_label (short_astyle_string(_io->gain_automation_curve().automation_style()));
928 GainMeter::gain_automation_state_changed ()
930 ENSURE_GUI_THREAD(mem_fun(*this, &GainMeter::gain_automation_state_changed));
931 //Route* _route = dynamic_cast<Route*>(&_io);
937 gain_automation_state_button.set_label (astate_string(_io->gain_automation_curve().automation_state()));
940 gain_automation_state_button.set_label (short_astate_string(_io->gain_automation_curve().automation_state()));
944 x = (_io->gain_automation_state() != Off);
946 if (gain_automation_state_button.get_active() != x) {
947 ignore_toggle = true;
948 gain_automation_state_button.set_active (x);
949 ignore_toggle = false;
952 update_gain_sensitive ();
954 /* start watching automation so that things move */
956 gain_watching.disconnect();
959 gain_watching = ARDOUR_UI::RapidScreenUpdate.connect (mem_fun (*this, &GainMeter::effective_gain_display));