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;
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;
87 gain_slider = manage (new VSliderController (slider,
92 gain_slider->signal_button_press_event().connect (mem_fun(*this, &GainMeter::start_gain_touch));
93 gain_slider->signal_button_release_event().connect (mem_fun(*this, &GainMeter::end_gain_touch));
94 gain_slider->set_name ("GainFader");
96 gain_display.set_name ("MixerStripGainDisplay");
97 gain_display.set_has_frame (false);
98 set_size_request_to_display_given_text (gain_display, "-80.g", 2, 6); /* note the descender */
99 gain_display.signal_activate().connect (mem_fun (*this, &GainMeter::gain_activated));
100 gain_display.signal_focus_in_event().connect (mem_fun (*this, &GainMeter::gain_focused), false);
101 gain_display.signal_focus_out_event().connect (mem_fun (*this, &GainMeter::gain_focused), false);
103 gain_display_box.set_homogeneous (true);
104 gain_display_box.set_spacing (2);
105 gain_display_box.pack_start (gain_display, true, true);
107 peak_display.set_name ("MixerStripPeakDisplay");
108 peak_display.set_has_frame (false);
109 peak_display.set_editable (false);
110 set_size_request_to_display_given_text (peak_display, "-80.g", 2, 6); /* note the descender */
111 max_peak = minus_infinity();
112 peak_display.set_text (_("-inf"));
113 peak_display.unset_flags (Gtk::CAN_FOCUS);
115 meter_metric_area.set_name ("MeterMetricsStrip");
116 set_size_request_to_display_given_text (meter_metric_area, "-50", 0, 0);
118 meter_packer.set_spacing (2);
120 gain_automation_style_button.set_name ("MixerAutomationModeButton");
121 gain_automation_state_button.set_name ("MixerAutomationPlaybackButton");
123 ARDOUR_UI::instance()->tooltips().set_tip (gain_automation_state_button, _("Fader automation mode"));
124 ARDOUR_UI::instance()->tooltips().set_tip (gain_automation_style_button, _("Fader automation type"));
126 gain_automation_style_button.unset_flags (Gtk::CAN_FOCUS);
127 gain_automation_state_button.unset_flags (Gtk::CAN_FOCUS);
129 gain_automation_state_button.set_size_request(15, 15);
130 gain_automation_style_button.set_size_request(15, 15);
132 HBox* fader_centering_box = manage (new HBox);
133 fader_centering_box->pack_start (*gain_slider, true, false);
135 fader_vbox = manage (new Gtk::VBox());
136 fader_vbox->set_spacing (0);
137 fader_vbox->pack_start (*fader_centering_box, false, false, 0);
139 hbox.set_spacing (2);
140 hbox.pack_start (*fader_vbox, true, true);
146 if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
149 if we have a route (ie. we're not the click),
150 pack some route-dependent stuff.
153 gain_display_box.pack_end (peak_display, true, true);
155 hbox.pack_end (meter_packer, true, true);
157 using namespace Menu_Helpers;
159 gain_astate_menu.items().push_back (MenuElem (_("Off"),
160 bind (mem_fun (*_io, &IO::set_gain_automation_state), (AutoState) Off)));
161 gain_astate_menu.items().push_back (MenuElem (_("Play"),
162 bind (mem_fun (*_io, &IO::set_gain_automation_state), (AutoState) Play)));
163 gain_astate_menu.items().push_back (MenuElem (_("Write"),
164 bind (mem_fun (*_io, &IO::set_gain_automation_state), (AutoState) Write)));
165 gain_astate_menu.items().push_back (MenuElem (_("Touch"),
166 bind (mem_fun (*_io, &IO::set_gain_automation_state), (AutoState) Touch)));
168 gain_astyle_menu.items().push_back (MenuElem (_("Trim")));
169 gain_astyle_menu.items().push_back (MenuElem (_("Abs")));
171 gain_astate_menu.set_name ("ArdourContextMenu");
172 gain_astyle_menu.set_name ("ArdourContextMenu");
174 gain_automation_style_button.signal_button_press_event().connect (mem_fun(*this, &GainMeter::gain_automation_style_button_event), false);
175 gain_automation_state_button.signal_button_press_event().connect (mem_fun(*this, &GainMeter::gain_automation_state_button_event), false);
177 r->gain_automation_curve().automation_state_changed.connect (mem_fun(*this, &GainMeter::gain_automation_state_changed));
178 r->gain_automation_curve().automation_style_changed.connect (mem_fun(*this, &GainMeter::gain_automation_style_changed));
179 fader_vbox->pack_start (gain_automation_state_button, false, false, 0);
181 gain_automation_state_changed ();
186 pack_start (gain_display_box, Gtk::PACK_SHRINK);
187 pack_start (hbox, Gtk::PACK_SHRINK);
189 _io->gain_changed.connect (mem_fun(*this, &GainMeter::gain_changed));
191 meter_metric_area.signal_expose_event().connect (mem_fun(*this, &GainMeter::meter_metrics_expose));
192 gain_adjustment.signal_value_changed().connect (mem_fun(*this, &GainMeter::gain_adjusted));
193 peak_display.signal_button_release_event().connect (mem_fun(*this, &GainMeter::peak_button_release), false);
194 gain_display.signal_key_press_event().connect (mem_fun(*this, &GainMeter::gain_key_press), false);
196 Config->ParameterChanged.connect (mem_fun (*this, &GainMeter::parameter_changed));
201 update_gain_sensitive ();
203 ResetAllPeakDisplays.connect (mem_fun(*this, &GainMeter::reset_peak_display));
204 ResetGroupPeakDisplays.connect (mem_fun(*this, &GainMeter::reset_group_peak_display));
208 GainMeter::set_width (Width w)
223 Glib::RefPtr<Gdk::Pixmap>
224 GainMeter::render_metrics (Gtk::Widget& w)
226 Glib::RefPtr<Gdk::Window> win (w.get_window());
227 Glib::RefPtr<Gdk::GC> fg_gc (w.get_style()->get_fg_gc (Gtk::STATE_NORMAL));
228 Glib::RefPtr<Gdk::GC> bg_gc (w.get_style()->get_bg_gc (Gtk::STATE_NORMAL));
229 gint x, y, width, height, depth;
230 int db_points[] = { -50, -40, -20, -30, -10, -3, 0, 4 };
233 win->get_geometry (x, y, width, height, depth);
235 Glib::RefPtr<Gdk::Pixmap> pixmap = Gdk::Pixmap::create (win, width, height);
237 metric_pixmaps[w.get_name()] = pixmap;
239 pixmap->draw_rectangle (bg_gc, true, 0, 0, width, height);
241 Glib::RefPtr<Pango::Layout> layout = w.create_pango_layout("");
243 for (uint32_t i = 0; i < sizeof (db_points)/sizeof (db_points[0]); ++i) {
245 float fraction = log_meter (db_points[i]);
246 gint pos = height - (gint) floor (height * fraction);
248 snprintf (buf, sizeof (buf), "%d", abs (db_points[i]));
250 layout->set_text (buf);
252 /* we want logical extents, not ink extents here */
255 layout->get_pixel_size (width, height);
257 pixmap->draw_line (fg_gc, 0, pos, 4, pos);
258 pixmap->draw_layout (fg_gc, 6, pos - (height/2), layout);
265 GainMeter::meter_metrics_expose (GdkEventExpose *ev)
267 Glib::RefPtr<Gdk::Window> win (meter_metric_area.get_window());
268 Glib::RefPtr<Gdk::GC> fg_gc (meter_metric_area.get_style()->get_fg_gc (Gtk::STATE_NORMAL));
269 Glib::RefPtr<Gdk::GC> bg_gc (meter_metric_area.get_style()->get_bg_gc (Gtk::STATE_NORMAL));
270 GdkRectangle base_rect;
271 GdkRectangle draw_rect;
272 gint x, y, width, height, depth;
274 win->get_geometry (x, y, width, height, depth);
276 base_rect.width = width;
277 base_rect.height = height;
281 Glib::RefPtr<Gdk::Pixmap> pixmap;
282 std::map<string,Glib::RefPtr<Gdk::Pixmap> >::iterator i = metric_pixmaps.find (meter_metric_area.get_name());
284 if (i == metric_pixmaps.end()) {
285 pixmap = render_metrics (meter_metric_area);
290 gdk_rectangle_intersect (&ev->area, &base_rect, &draw_rect);
291 win->draw_rectangle (bg_gc, true, draw_rect.x, draw_rect.y, draw_rect.width, draw_rect.height);
292 win->draw_drawable (bg_gc, pixmap, draw_rect.x, draw_rect.y, draw_rect.x, draw_rect.y, draw_rect.width, draw_rect.height);
297 GainMeter::~GainMeter ()
303 for (vector<MeterInfo>::iterator i = meters.begin(); i != meters.end(); i++) {
311 GainMeter::update_meters ()
313 vector<MeterInfo>::iterator i;
318 for (n = 0, i = meters.begin(); i != meters.end(); ++i, ++n) {
320 peak = _io->peak_input_power (n);
322 (*i).meter->set (log_meter (peak), peak);
324 if (peak > max_peak) {
326 /* set peak display */
327 if (max_peak <= -200.0f) {
328 peak_display.set_text (_("-inf"));
330 snprintf (buf, sizeof(buf), "%.1f", max_peak);
331 peak_display.set_text (buf);
334 if (max_peak >= 0.0f) {
335 peak_display.set_name ("MixerStripPeakDisplayPeak");
343 GainMeter::parameter_changed(const char* parameter_name)
345 #define PARAM_IS(x) (!strcmp (parameter_name, (x)))
347 ENSURE_GUI_THREAD (bind (mem_fun(*this, &GainMeter::parameter_changed), parameter_name));
349 if (PARAM_IS ("meter-hold")) {
351 vector<MeterInfo>::iterator i;
354 for (n = 0, i = meters.begin(); i != meters.end(); ++i, ++n) {
356 (*i).meter->set_hold_count ((uint32_t) floor(Config->get_meter_hold()));
364 GainMeter::hide_all_meters ()
366 bool remove_metric_area = false;
368 for (vector<MeterInfo>::iterator i = meters.begin(); i != meters.end(); ++i) {
370 remove_metric_area = true;
371 meter_packer.remove (*((*i).meter));
376 if (remove_metric_area) {
377 if (meter_metric_area.get_parent()) {
378 meter_packer.remove (meter_metric_area);
384 GainMeter::setup_meters ()
386 uint32_t nmeters = _io->n_outputs();
393 if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
395 switch (r->meter_point()) {
398 nmeters = r->n_inputs();
401 nmeters = r->n_outputs();
407 nmeters = _io->n_outputs();
416 width = regular_meter_width;
418 width = thin_meter_width;
421 while (meters.size() < nmeters) {
422 meters.push_back (MeterInfo());
425 /* pack them backwards */
427 if (_width == Wide) {
428 meter_packer.pack_end (meter_metric_area, false, false);
429 meter_metric_area.show_all ();
432 for (int32_t n = nmeters-1; nmeters && n >= 0 ; --n) {
433 if (meters[n].width != width) {
434 delete meters[n].meter;
435 meters[n].meter = new FastMeter ((uint32_t) floor (Config->get_meter_hold()), width, FastMeter::Vertical);
436 meters[n].width = width;
438 meters[n].meter->add_events (Gdk::BUTTON_RELEASE_MASK);
439 meters[n].meter->signal_button_release_event().connect (bind (mem_fun(*this, &GainMeter::meter_button_release), n));
442 meter_packer.pack_end (*meters[n].meter, false, false);
443 meters[n].meter->show_all ();
444 meters[n].packed = true;
449 GainMeter::gain_key_press (GdkEventKey* ev)
451 if (key_is_legal_for_numeric_entry (ev->keyval)) {
452 /* drop through to normal handling */
455 /* illegal key for gain entry */
460 GainMeter::peak_button_release (GdkEventButton* ev)
462 /* reset peak label */
464 if (ev->button == 1 && Keyboard::modifier_state_equals (ev->state, Keyboard::Control|Keyboard::Shift)) {
465 ResetAllPeakDisplays ();
466 } else if (ev->button == 1 && Keyboard::modifier_state_equals (ev->state, Keyboard::Control)) {
468 if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
469 ResetGroupPeakDisplays (r->mix_group());
472 reset_peak_display ();
479 GainMeter::reset_peak_display ()
481 max_peak = minus_infinity();
482 peak_display.set_text (_("-Inf"));
483 peak_display.set_name ("MixerStripPeakDisplay");
487 GainMeter::reset_group_peak_display (RouteGroup* group)
490 if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
491 if (group == r->mix_group()) {
492 reset_peak_display ();
498 GainMeter::meter_button_release (GdkEventButton* ev, uint32_t which)
500 switch (ev->button) {
502 meters[which].meter->clear();
503 max_peak = minus_infinity();
504 peak_display.set_text (_("-inf"));
505 peak_display.set_name ("MixerStripPeakDisplay");
509 // popup_meter_menu (ev);
517 GainMeter::popup_meter_menu (GdkEventButton *ev)
519 using namespace Menu_Helpers;
521 if (meter_menu == 0) {
522 meter_menu = new Gtk::Menu;
523 MenuList& items = meter_menu->items();
525 items.push_back (MenuElem ("-inf .. +0dBFS"));
526 items.push_back (MenuElem ("-10dB .. +0dBFS"));
527 items.push_back (MenuElem ("-4 .. +0dBFS"));
528 items.push_back (SeparatorElem());
529 items.push_back (MenuElem ("-inf .. -2dBFS"));
530 items.push_back (MenuElem ("-10dB .. -2dBFS"));
531 items.push_back (MenuElem ("-4 .. -2dBFS"));
534 meter_menu->popup (1, ev->time);
538 GainMeter::gain_focused (GdkEventFocus* ev)
541 gain_display.select_region (0, -1);
543 gain_display.select_region (0, 0);
549 GainMeter::gain_activated ()
553 if (sscanf (gain_display.get_text().c_str(), "%f", &f) == 1) {
555 /* clamp to displayable values */
559 _io->set_gain (dB_to_coefficient (f), this);
564 GainMeter::show_gain ()
568 float v = gain_adjustment.get_value();
571 strcpy (buf, _("-inf"));
573 snprintf (buf, 32, "%.1f", coefficient_to_dB (slider_position_to_gain (v)));
576 gain_display.set_text (buf);
580 GainMeter::gain_adjusted ()
582 if (!ignore_toggle) {
583 _io->set_gain (slider_position_to_gain (gain_adjustment.get_value()), this);
589 GainMeter::effective_gain_display ()
591 gfloat value = gain_to_slider_position (_io->effective_gain());
593 if (gain_adjustment.get_value() != value) {
594 ignore_toggle = true;
595 gain_adjustment.set_value (value);
596 ignore_toggle = false;
601 GainMeter::gain_changed (void *src)
603 Gtkmm2ext::UI::instance()->call_slot (mem_fun(*this, &GainMeter::effective_gain_display));
607 GainMeter::set_meter_strip_name (const char * name)
609 meter_metric_area.set_name (name);
613 GainMeter::set_fader_name (const char * name)
615 gain_slider->set_name (name);
619 GainMeter::update_gain_sensitive ()
621 static_cast<Gtkmm2ext::SliderController*>(gain_slider)->set_sensitive (!(_io->gain_automation_state() & Play));
626 next_meter_point (MeterPoint mp)
630 return MeterPreFader;
634 return MeterPostFader;
646 GainMeter::meter_press(GdkEventButton* ev)
650 wait_for_release = false;
652 if ((_route = dynamic_cast<Route*>(_io.get())) == 0) {
656 if (!ignore_toggle) {
658 if (Keyboard::is_context_menu_event (ev)) {
660 // no menu at this time.
664 if (ev->button == 2) {
666 // ctrl-button2 click is the midi binding click
667 // button2-click is "momentary"
669 if (!Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::Control))) {
670 wait_for_release = true;
671 old_meter_point = _route->meter_point ();
675 if (ev->button == 1 || ev->button == 2) {
677 if (Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::Control|Keyboard::Shift))) {
679 /* ctrl-shift-click applies change to all routes */
681 _session.foreach_route (this, &GainMeter::set_meter_point, next_meter_point (_route->meter_point()));
683 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::Control)) {
685 /* ctrl-click: solo mix group.
686 ctrl-button2 is MIDI learn.
689 if (ev->button == 1) {
690 set_mix_group_meter_point (*_route, next_meter_point (_route->meter_point()));
695 /* click: solo this route */
697 _route->set_meter_point (next_meter_point (_route->meter_point()), this);
708 GainMeter::meter_release(GdkEventButton* ev)
712 if (wait_for_release){
713 wait_for_release = false;
714 set_meter_point (*(dynamic_cast<Route*>(_io.get())), old_meter_point);
721 GainMeter::set_meter_point (Route& route, MeterPoint mp)
723 route.set_meter_point (mp, this);
727 GainMeter::set_mix_group_meter_point (Route& route, MeterPoint mp)
729 RouteGroup* mix_group;
731 if((mix_group = route.mix_group()) != 0){
732 mix_group->apply (&Route::set_meter_point, mp, this);
734 route.set_meter_point (mp, this);
739 GainMeter::meter_point_clicked ()
743 if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
749 GainMeter::start_gain_touch (GdkEventButton* ev)
751 _io->start_gain_touch ();
756 GainMeter::end_gain_touch (GdkEventButton* ev)
758 _io->end_gain_touch ();
763 GainMeter::gain_automation_state_button_event (GdkEventButton *ev)
765 if (ev->type == GDK_BUTTON_RELEASE) {
769 switch (ev->button) {
771 gain_astate_menu.popup (1, ev->time);
781 GainMeter::gain_automation_style_button_event (GdkEventButton *ev)
783 if (ev->type == GDK_BUTTON_RELEASE) {
787 switch (ev->button) {
789 gain_astyle_menu.popup (1, ev->time);
798 GainMeter::astate_string (AutoState state)
800 return _astate_string (state, false);
804 GainMeter::short_astate_string (AutoState state)
806 return _astate_string (state, true);
810 GainMeter::_astate_string (AutoState state, bool shrt)
816 sstr = (shrt ? "O" : _("O"));
819 sstr = (shrt ? "P" : _("P"));
822 sstr = (shrt ? "T" : _("T"));
825 sstr = (shrt ? "W" : _("W"));
833 GainMeter::astyle_string (AutoStyle style)
835 return _astyle_string (style, false);
839 GainMeter::short_astyle_string (AutoStyle style)
841 return _astyle_string (style, true);
845 GainMeter::_astyle_string (AutoStyle style, bool shrt)
850 /* XXX it might different in different languages */
852 return (shrt ? _("Abs") : _("Abs"));
857 GainMeter::gain_automation_style_changed ()
859 // Route* _route = dynamic_cast<Route*>(&_io);
862 gain_automation_style_button.set_label (astyle_string(_io->gain_automation_curve().automation_style()));
865 gain_automation_style_button.set_label (short_astyle_string(_io->gain_automation_curve().automation_style()));
871 GainMeter::gain_automation_state_changed ()
873 ENSURE_GUI_THREAD(mem_fun(*this, &GainMeter::gain_automation_state_changed));
874 //Route* _route = dynamic_cast<Route*>(&_io);
880 gain_automation_state_button.set_label (astate_string(_io->gain_automation_curve().automation_state()));
883 gain_automation_state_button.set_label (short_astate_string(_io->gain_automation_curve().automation_state()));
887 x = (_io->gain_automation_state() != Off);
889 if (gain_automation_state_button.get_active() != x) {
890 ignore_toggle = true;
891 gain_automation_state_button.set_active (x);
892 ignore_toggle = false;
895 update_gain_sensitive ();
897 /* start watching automation so that things move */
899 gain_watching.disconnect();
902 gain_watching = ARDOUR_UI::RapidScreenUpdate.connect (mem_fun (*this, &GainMeter::effective_gain_display));