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 string path = ARDOUR::find_data_file("vslider02_slider.xpm", "pixmaps");
67 error << _("cannot find images for fader slider") << endmsg;
70 slider = Gdk::Pixbuf::create_from_file (path);
72 path = ARDOUR::find_data_file("vslider02_rail.xpm", "pixmaps");
74 error << _("cannot find images for fader rail") << endmsg;
77 rail = Gdk::Pixbuf::create_from_file (path);
82 GainMeter::GainMeter (boost::shared_ptr<IO> io, Session& s)
86 // 0.781787 is the value needed for gain to be set to 0.
87 gain_adjustment (0.781787, 0.0, 1.0, 0.01, 0.1),
88 gain_display (&gain_adjustment, "MixerStripGainDisplay"),
89 gain_automation_style_button (""),
90 gain_automation_state_button ("")
97 ignore_toggle = false;
100 gain_slider = manage (new VSliderController (slider, rail,
105 gain_slider->signal_button_press_event().connect (mem_fun(*this, &GainMeter::start_gain_touch));
106 gain_slider->signal_button_release_event().connect (mem_fun(*this, &GainMeter::end_gain_touch));
107 gain_slider->set_name ("MixerGainMeter");
109 gain_display.set_print_func (_gain_printer, this);
111 gain_display_box.set_spacing (2);
112 set_size_request_to_display_given_text (gain_display_frame, "-86.g", 2, 6); /* note the descender */
113 gain_display_frame.set_shadow_type (Gtk::SHADOW_IN);
114 gain_display_frame.set_name ("BaseFrame");
115 gain_display_frame.add (gain_display);
116 gain_display_box.pack_start (gain_display_frame, Gtk::PACK_SHRINK);
118 peak_display.set_name ("MixerStripPeakDisplay");
119 peak_display.add (peak_display_label);
120 set_size_request_to_display_given_text (peak_display_frame, "-86.g", 2, 6); /* note the descender */
121 peak_display_frame.set_shadow_type (Gtk::SHADOW_IN);
122 peak_display_frame.set_name ("BaseFrame");
123 peak_display_frame.add (peak_display);
124 max_peak = minus_infinity();
125 peak_display_label.set_text (_("-inf"));
127 meter_metric_area.set_size_request (25, -1);
128 meter_metric_area.set_name ("MeterMetricsStrip");
130 meter_packer.set_spacing (2);
132 gain_automation_style_button.set_name ("MixerAutomationModeButton");
133 gain_automation_state_button.set_name ("MixerAutomationPlaybackButton");
135 ARDOUR_UI::instance()->tooltips().set_tip (gain_automation_state_button, _("Fader automation mode"));
136 ARDOUR_UI::instance()->tooltips().set_tip (gain_automation_style_button, _("Fader automation type"));
138 gain_automation_style_button.unset_flags (Gtk::CAN_FOCUS);
139 gain_automation_state_button.unset_flags (Gtk::CAN_FOCUS);
141 gain_automation_state_button.set_size_request(15, 15);
142 gain_automation_style_button.set_size_request(15, 15);
144 fader_vbox = manage (new Gtk::VBox());
145 fader_vbox->set_spacing (0);
146 fader_vbox->pack_start (*gain_slider, false, false, 0);
148 hbox.set_spacing (0);
149 hbox.pack_start (*fader_vbox, false, false, 2);
155 if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
157 if we don't have a route (if we're the click),
158 pack some route-dependent stuff.
161 gain_display_box.pack_end (peak_display_frame, Gtk::PACK_SHRINK);
163 hbox.pack_start (meter_packer, true, false);
165 using namespace Menu_Helpers;
167 gain_astate_menu.items().push_back (MenuElem (_("Off"),
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 ();
195 pack_start (gain_display_box, Gtk::PACK_SHRINK);
196 pack_start (hbox, Gtk::PACK_SHRINK);
198 _io->gain_changed.connect (mem_fun(*this, &GainMeter::gain_changed));
200 meter_metric_area.signal_expose_event().connect (mem_fun(*this, &GainMeter::meter_metrics_expose));
201 gain_adjustment.signal_value_changed().connect (mem_fun(*this, &GainMeter::gain_adjusted));
202 peak_display.signal_button_release_event().connect (mem_fun(*this, &GainMeter::peak_button_release));
204 Config->ParameterChanged.connect (mem_fun (*this, &GainMeter::parameter_changed));
207 update_gain_sensitive ();
209 ResetAllPeakDisplays.connect (mem_fun(*this, &GainMeter::reset_peak_display));
210 ResetGroupPeakDisplays.connect (mem_fun(*this, &GainMeter::reset_group_peak_display));
214 GainMeter::set_width (Width w)
218 peak_display_frame.show_all();
221 peak_display_frame.hide_all();
229 Glib::RefPtr<Gdk::Pixmap>
230 GainMeter::render_metrics (Gtk::Widget& w)
232 Glib::RefPtr<Gdk::Window> win (w.get_window());
233 Glib::RefPtr<Gdk::GC> fg_gc (w.get_style()->get_fg_gc (Gtk::STATE_NORMAL));
234 Glib::RefPtr<Gdk::GC> bg_gc (w.get_style()->get_bg_gc (Gtk::STATE_NORMAL));
235 gint x, y, width, height, depth;
236 int db_points[] = { -50, -40, -20, -30, -10, -3, 0, 4 };
239 win->get_geometry (x, y, width, height, depth);
241 Glib::RefPtr<Gdk::Pixmap> pixmap = Gdk::Pixmap::create (win, width, height);
243 metric_pixmaps[w.get_name()] = pixmap;
245 pixmap->draw_rectangle (bg_gc, true, 0, 0, width, height);
247 Glib::RefPtr<Pango::Layout> layout = w.create_pango_layout("");
249 for (uint32_t i = 0; i < sizeof (db_points)/sizeof (db_points[0]); ++i) {
251 float fraction = log_meter (db_points[i]);
252 gint pos = height - (gint) floor (height * fraction);
254 snprintf (buf, sizeof (buf), "%d", abs (db_points[i]));
256 layout->set_text (buf);
258 /* we want logical extents, not ink extents here */
261 layout->get_pixel_size (width, height);
263 pixmap->draw_line (fg_gc, 0, pos, 4, pos);
264 pixmap->draw_layout (fg_gc, 6, pos - (height/2), layout);
271 GainMeter::meter_metrics_expose (GdkEventExpose *ev)
273 Glib::RefPtr<Gdk::Window> win (meter_metric_area.get_window());
274 Glib::RefPtr<Gdk::GC> fg_gc (meter_metric_area.get_style()->get_fg_gc (Gtk::STATE_NORMAL));
275 Glib::RefPtr<Gdk::GC> bg_gc (meter_metric_area.get_style()->get_bg_gc (Gtk::STATE_NORMAL));
276 GdkRectangle base_rect;
277 GdkRectangle draw_rect;
278 gint x, y, width, height, depth;
280 win->get_geometry (x, y, width, height, depth);
282 base_rect.width = width;
283 base_rect.height = height;
287 Glib::RefPtr<Gdk::Pixmap> pixmap;
288 std::map<string,Glib::RefPtr<Gdk::Pixmap> >::iterator i = metric_pixmaps.find (meter_metric_area.get_name());
290 if (i == metric_pixmaps.end()) {
291 pixmap = render_metrics (meter_metric_area);
296 gdk_rectangle_intersect (&ev->area, &base_rect, &draw_rect);
297 win->draw_rectangle (bg_gc, true, draw_rect.x, draw_rect.y, draw_rect.width, draw_rect.height);
298 win->draw_drawable (bg_gc, pixmap, draw_rect.x, draw_rect.y, draw_rect.x, draw_rect.y, draw_rect.width, draw_rect.height);
303 GainMeter::~GainMeter ()
309 for (vector<MeterInfo>::iterator i = meters.begin(); i != meters.end(); i++) {
317 GainMeter::update_meters ()
319 vector<MeterInfo>::iterator i;
324 for (n = 0, i = meters.begin(); i != meters.end(); ++i, ++n) {
326 peak = _io->peak_input_power (n);
328 (*i).meter->set (log_meter (peak), peak);
330 if (peak > max_peak) {
332 /* set peak display */
333 if (max_peak <= -200.0f) {
334 peak_display_label.set_text (_("-inf"));
336 snprintf (buf, sizeof(buf), "%.1f", max_peak);
337 peak_display_label.set_text (buf);
340 if (max_peak >= 0.0f) {
341 peak_display.set_name ("MixerStripPeakDisplayPeak");
349 GainMeter::parameter_changed(const char* parameter_name)
351 #define PARAM_IS(x) (!strcmp (parameter_name, (x)))
353 ENSURE_GUI_THREAD (bind (mem_fun(*this, &GainMeter::parameter_changed), parameter_name));
355 if (PARAM_IS ("meter-hold")) {
357 vector<MeterInfo>::iterator i;
360 for (n = 0, i = meters.begin(); i != meters.end(); ++i, ++n) {
362 (*i).meter->set_hold_count ((uint32_t) floor(Config->get_meter_hold()));
370 GainMeter::hide_all_meters ()
372 bool remove_metric_area = false;
374 for (vector<MeterInfo>::iterator i = meters.begin(); i != meters.end(); ++i) {
376 remove_metric_area = true;
377 meter_packer.remove (*((*i).meter));
382 if (remove_metric_area) {
383 if (meter_metric_area.get_parent()) {
384 meter_packer.remove (meter_metric_area);
390 GainMeter::setup_meters ()
392 uint32_t nmeters = _io->n_outputs();
399 if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
401 switch (r->meter_point()) {
404 nmeters = r->n_inputs();
407 nmeters = r->n_outputs();
413 nmeters = _io->n_outputs();
422 width = regular_meter_width;
424 width = thin_meter_width;
427 while (meters.size() < nmeters) {
428 meters.push_back (MeterInfo());
431 for (uint32_t n = 0; n < nmeters; ++n) {
432 if (meters[n].width != width) {
433 delete meters[n].meter;
434 meters[n].meter = new FastMeter ((uint32_t) floor (Config->get_meter_hold()), width, FastMeter::Vertical);
435 meters[n].width = width;
437 meters[n].meter->add_events (Gdk::BUTTON_RELEASE_MASK);
438 meters[n].meter->signal_button_release_event().connect (bind (mem_fun(*this, &GainMeter::meter_button_release), n));
441 meter_packer.pack_start (*meters[n].meter, Gtk::PACK_SHRINK);
442 meters[n].meter->show_all ();
443 meters[n].packed = true;
446 if (_width == Wide) {
447 meter_packer.pack_start (meter_metric_area, Gtk::PACK_SHRINK);
448 meter_metric_area.show_all ();
453 GainMeter::peak_button_release (GdkEventButton* ev)
455 /* reset peak label */
457 if (ev->button == 1 && Keyboard::modifier_state_equals (ev->state, Keyboard::Control|Keyboard::Shift)) {
458 ResetAllPeakDisplays ();
459 } else if (ev->button == 1 && Keyboard::modifier_state_equals (ev->state, Keyboard::Control)) {
461 if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
462 ResetGroupPeakDisplays (r->mix_group());
465 reset_peak_display ();
471 GainMeter::reset_peak_display ()
473 max_peak = minus_infinity();
474 peak_display_label.set_text (_("-Inf"));
475 peak_display.set_name ("MixerStripPeakDisplay");
479 GainMeter::reset_group_peak_display (RouteGroup* group)
482 if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
483 if (group == r->mix_group()) {
484 reset_peak_display ();
490 GainMeter::meter_button_release (GdkEventButton* ev, uint32_t which)
492 switch (ev->button) {
494 meters[which].meter->clear();
495 max_peak = minus_infinity();
496 peak_display_label.set_text (_("-inf"));
497 peak_display.set_name ("MixerStripPeakDisplay");
501 // popup_meter_menu (ev);
509 GainMeter::popup_meter_menu (GdkEventButton *ev)
511 using namespace Menu_Helpers;
513 if (meter_menu == 0) {
514 meter_menu = new Gtk::Menu;
515 MenuList& items = meter_menu->items();
517 items.push_back (MenuElem ("-inf .. +0dBFS"));
518 items.push_back (MenuElem ("-10dB .. +0dBFS"));
519 items.push_back (MenuElem ("-4 .. +0dBFS"));
520 items.push_back (SeparatorElem());
521 items.push_back (MenuElem ("-inf .. -2dBFS"));
522 items.push_back (MenuElem ("-10dB .. -2dBFS"));
523 items.push_back (MenuElem ("-4 .. -2dBFS"));
526 meter_menu->popup (1, ev->time);
530 GainMeter::_gain_printer (char buf[32], Gtk::Adjustment& adj, void *arg)
532 static_cast<GainMeter *>(arg)->gain_printer (buf, adj);
536 GainMeter::gain_printer (char buf[32], Gtk::Adjustment& adj)
538 float v = adj.get_value();
541 strcpy (buf, _("-inf"));
543 snprintf (buf, 32, "%.1f", coefficient_to_dB (slider_position_to_gain (v)));
548 GainMeter::gain_adjusted ()
550 if (!ignore_toggle) {
551 _io->set_gain (slider_position_to_gain (gain_adjustment.get_value()), this);
556 GainMeter::effective_gain_display ()
558 gfloat value = gain_to_slider_position (_io->effective_gain());
560 if (gain_adjustment.get_value() != value) {
561 ignore_toggle = true;
562 gain_adjustment.set_value (value);
563 ignore_toggle = false;
568 GainMeter::gain_changed (void *src)
570 Gtkmm2ext::UI::instance()->call_slot (mem_fun(*this, &GainMeter::effective_gain_display));
574 GainMeter::set_meter_strip_name (const char * name)
576 meter_metric_area.set_name (name);
580 GainMeter::set_fader_name (const char * name)
582 gain_slider->set_name (name);
586 GainMeter::update_gain_sensitive ()
588 static_cast<Gtkmm2ext::SliderController*>(gain_slider)->set_sensitive (!(_io->gain_automation_state() & Play));
593 next_meter_point (MeterPoint mp)
597 return MeterPreFader;
601 return MeterPostFader;
613 GainMeter::meter_press(GdkEventButton* ev)
617 wait_for_release = false;
619 if ((_route = dynamic_cast<Route*>(_io.get())) == 0) {
623 if (!ignore_toggle) {
625 if (Keyboard::is_context_menu_event (ev)) {
627 // no menu at this time.
631 if (ev->button == 2) {
633 // ctrl-button2 click is the midi binding click
634 // button2-click is "momentary"
636 if (!Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::Control))) {
637 wait_for_release = true;
638 old_meter_point = _route->meter_point ();
642 if (ev->button == 1 || ev->button == 2) {
644 if (Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::Control|Keyboard::Shift))) {
646 /* ctrl-shift-click applies change to all routes */
648 _session.foreach_route (this, &GainMeter::set_meter_point, next_meter_point (_route->meter_point()));
650 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::Control)) {
652 /* ctrl-click: solo mix group.
653 ctrl-button2 is MIDI learn.
656 if (ev->button == 1) {
657 set_mix_group_meter_point (*_route, next_meter_point (_route->meter_point()));
662 /* click: solo this route */
664 _route->set_meter_point (next_meter_point (_route->meter_point()), this);
675 GainMeter::meter_release(GdkEventButton* ev)
679 if (wait_for_release){
680 wait_for_release = false;
681 set_meter_point (*(dynamic_cast<Route*>(_io.get())), old_meter_point);
688 GainMeter::set_meter_point (Route& route, MeterPoint mp)
690 route.set_meter_point (mp, this);
694 GainMeter::set_mix_group_meter_point (Route& route, MeterPoint mp)
696 RouteGroup* mix_group;
698 if((mix_group = route.mix_group()) != 0){
699 mix_group->apply (&Route::set_meter_point, mp, this);
701 route.set_meter_point (mp, this);
706 GainMeter::meter_point_clicked ()
710 if ((r = dynamic_cast<Route*> (_io.get())) != 0) {
716 GainMeter::start_gain_touch (GdkEventButton* ev)
718 _io->start_gain_touch ();
723 GainMeter::end_gain_touch (GdkEventButton* ev)
725 _io->end_gain_touch ();
730 GainMeter::gain_automation_state_button_event (GdkEventButton *ev)
732 if (ev->type == GDK_BUTTON_RELEASE) {
736 switch (ev->button) {
738 gain_astate_menu.popup (1, ev->time);
748 GainMeter::gain_automation_style_button_event (GdkEventButton *ev)
750 if (ev->type == GDK_BUTTON_RELEASE) {
754 switch (ev->button) {
756 gain_astyle_menu.popup (1, ev->time);
765 GainMeter::astate_string (AutoState state)
767 return _astate_string (state, false);
771 GainMeter::short_astate_string (AutoState state)
773 return _astate_string (state, true);
777 GainMeter::_astate_string (AutoState state, bool shrt)
783 sstr = (shrt ? "O" : _("O"));
786 sstr = (shrt ? "P" : _("P"));
789 sstr = (shrt ? "T" : _("T"));
792 sstr = (shrt ? "W" : _("W"));
800 GainMeter::astyle_string (AutoStyle style)
802 return _astyle_string (style, false);
806 GainMeter::short_astyle_string (AutoStyle style)
808 return _astyle_string (style, true);
812 GainMeter::_astyle_string (AutoStyle style, bool shrt)
817 /* XXX it might different in different languages */
819 return (shrt ? _("Abs") : _("Abs"));
824 GainMeter::gain_automation_style_changed ()
826 // Route* _route = dynamic_cast<Route*>(&_io);
829 gain_automation_style_button.set_label (astyle_string(_io->gain_automation_curve().automation_style()));
832 gain_automation_style_button.set_label (short_astyle_string(_io->gain_automation_curve().automation_style()));
838 GainMeter::gain_automation_state_changed ()
840 ENSURE_GUI_THREAD(mem_fun(*this, &GainMeter::gain_automation_state_changed));
841 //Route* _route = dynamic_cast<Route*>(&_io);
847 gain_automation_state_button.set_label (astate_string(_io->gain_automation_curve().automation_state()));
850 gain_automation_state_button.set_label (short_astate_string(_io->gain_automation_curve().automation_state()));
854 x = (_io->gain_automation_state() != Off);
856 if (gain_automation_state_button.get_active() != x) {
857 ignore_toggle = true;
858 gain_automation_state_button.set_active (x);
859 ignore_toggle = false;
862 update_gain_sensitive ();
864 /* start watching automation so that things move */
866 gain_watching.disconnect();
869 gain_watching = ARDOUR_UI::RapidScreenUpdate.connect (mem_fun (*this, &GainMeter::effective_gain_display));