Set mixbufs count in the case of a panner being used, as
[ardour2.git] / gtk2_ardour / gain_meter.cc
blob33688fb339f7645fc551e21258907ff2d3722b62
1 /*
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.
20 #include <limits.h>
22 #include "ardour/amp.h"
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"
29 #include "ardour/utils.h"
31 #include <gtkmm/style.h>
32 #include <gdkmm/color.h>
33 #include <gtkmm2ext/utils.h>
34 #include <gtkmm2ext/fastmeter.h>
35 #include <gtkmm2ext/barcontroller.h>
36 #include <gtkmm2ext/gtk_ui.h>
37 #include "midi++/manager.h"
38 #include "pbd/fastlog.h"
39 #include "pbd/stacktrace.h"
41 #include "ardour_ui.h"
42 #include "gain_meter.h"
43 #include "global_signals.h"
44 #include "logmeter.h"
45 #include "gui_thread.h"
46 #include "keyboard.h"
47 #include "public_editor.h"
48 #include "utils.h"
50 #include "ardour/session.h"
51 #include "ardour/route.h"
52 #include "ardour/meter.h"
54 #include "i18n.h"
56 using namespace ARDOUR;
57 using namespace PBD;
58 using namespace Gtkmm2ext;
59 using namespace Gtk;
60 using namespace std;
61 using Gtkmm2ext::Keyboard;
63 sigc::signal<void> GainMeterBase::ResetAllPeakDisplays;
64 sigc::signal<void,RouteGroup*> GainMeterBase::ResetGroupPeakDisplays;
66 map<string,Glib::RefPtr<Gdk::Pixmap> > GainMeter::metric_pixmaps;
67 Glib::RefPtr<Gdk::Pixbuf> GainMeter::slider;
70 void
71 GainMeter::setup_slider_pix ()
73 if ((slider = ::get_icon ("fader_belt")) == 0) {
74 throw failed_constructor();
78 GainMeterBase::GainMeterBase (Session* s,
79 const Glib::RefPtr<Gdk::Pixbuf>& pix,
80 bool horizontal,
81 int fader_length)
82 : gain_adjustment (gain_to_slider_position_with_max (1.0, Config->get_max_gain()), 0.0, 1.0, 0.01, 0.1)
83 , gain_automation_style_button ("")
84 , gain_automation_state_button ("")
85 , style_changed (false)
86 , dpi_changed (false)
87 , _is_midi (false)
90 using namespace Menu_Helpers;
92 set_session (s);
94 ignore_toggle = false;
95 meter_menu = 0;
96 next_release_selects = false;
97 _width = Wide;
99 if (horizontal) {
100 gain_slider = manage (new HSliderController (pix,
101 &gain_adjustment,
102 fader_length,
103 false));
104 } else {
105 gain_slider = manage (new VSliderController (pix,
106 &gain_adjustment,
107 fader_length,
108 false));
111 level_meter = new LevelMeter(_session);
113 gain_slider->signal_button_press_event().connect (sigc::mem_fun(*this, &GainMeter::gain_slider_button_press));
114 gain_slider->signal_button_release_event().connect (sigc::mem_fun(*this, &GainMeter::gain_slider_button_release));
115 gain_slider->set_name ("GainFader");
117 gain_display.set_name ("MixerStripGainDisplay");
118 set_size_request_to_display_given_text (gain_display, "-80.g", 2, 6); /* note the descender */
119 gain_display.signal_activate().connect (sigc::mem_fun (*this, &GainMeter::gain_activated));
120 gain_display.signal_focus_in_event().connect (sigc::mem_fun (*this, &GainMeter::gain_focused), false);
121 gain_display.signal_focus_out_event().connect (sigc::mem_fun (*this, &GainMeter::gain_focused), false);
123 peak_display.set_name ("MixerStripPeakDisplay");
124 set_size_request_to_display_given_text (peak_display, "-80.g", 2, 6); /* note the descender */
125 max_peak = minus_infinity();
126 peak_display.set_label (_("-inf"));
127 peak_display.unset_flags (Gtk::CAN_FOCUS);
129 gain_automation_style_button.set_name ("MixerAutomationModeButton");
130 gain_automation_state_button.set_name ("MixerAutomationPlaybackButton");
132 ARDOUR_UI::instance()->set_tip (gain_automation_state_button, _("Fader automation mode"));
133 ARDOUR_UI::instance()->set_tip (gain_automation_style_button, _("Fader automation type"));
135 gain_automation_style_button.unset_flags (Gtk::CAN_FOCUS);
136 gain_automation_state_button.unset_flags (Gtk::CAN_FOCUS);
138 gain_automation_state_button.set_size_request(15, 15);
139 gain_automation_style_button.set_size_request(15, 15);
141 gain_astyle_menu.items().push_back (MenuElem (_("Trim")));
142 gain_astyle_menu.items().push_back (MenuElem (_("Abs")));
144 gain_astate_menu.set_name ("ArdourContextMenu");
145 gain_astyle_menu.set_name ("ArdourContextMenu");
147 gain_adjustment.signal_value_changed().connect (sigc::mem_fun(*this, &GainMeterBase::gain_adjusted));
148 peak_display.signal_button_release_event().connect (sigc::mem_fun(*this, &GainMeterBase::peak_button_release), false);
149 gain_display.signal_key_press_event().connect (sigc::mem_fun(*this, &GainMeterBase::gain_key_press), false);
151 ResetAllPeakDisplays.connect (sigc::mem_fun(*this, &GainMeterBase::reset_peak_display));
152 ResetGroupPeakDisplays.connect (sigc::mem_fun(*this, &GainMeterBase::reset_group_peak_display));
154 UI::instance()->theme_changed.connect (sigc::mem_fun(*this, &GainMeterBase::on_theme_changed));
155 ColorsChanged.connect (sigc::bind(sigc::mem_fun (*this, &GainMeterBase::color_handler), false));
156 DPIReset.connect (sigc::bind(sigc::mem_fun (*this, &GainMeterBase::color_handler), true));
159 GainMeterBase::~GainMeterBase ()
161 delete meter_menu;
162 delete level_meter;
165 void
166 GainMeterBase::set_controls (boost::shared_ptr<Route> r,
167 boost::shared_ptr<PeakMeter> pm,
168 boost::shared_ptr<Amp> amp)
170 connections.clear ();
171 model_connections.drop_connections ();
173 if (!pm && !amp) {
174 level_meter->set_meter (0);
175 gain_slider->set_controllable (boost::shared_ptr<PBD::Controllable>());
176 _meter.reset ();
177 _amp.reset ();
178 _route.reset ();
179 return;
182 _meter = pm;
183 _amp = amp;
184 _route = r;
186 level_meter->set_meter (pm.get());
187 gain_slider->set_controllable (amp->gain_control());
189 if (!_route || _route->output()->n_ports().n_midi() == 0) {
190 _is_midi = false;
191 gain_adjustment.set_lower (0.0);
192 gain_adjustment.set_upper (1.0);
193 gain_adjustment.set_step_increment (0.01);
194 gain_adjustment.set_page_increment (0.1);
195 } else {
196 _is_midi = true;
197 gain_adjustment.set_lower (0.0);
198 gain_adjustment.set_upper (2.0);
199 gain_adjustment.set_step_increment (0.05);
200 gain_adjustment.set_page_increment (0.1);
203 if (!_route || !_route->is_hidden()) {
205 using namespace Menu_Helpers;
207 gain_astate_menu.items().clear ();
209 gain_astate_menu.items().push_back (MenuElem (_("Manual"),
210 sigc::bind (sigc::mem_fun (*(amp.get()), &Automatable::set_parameter_automation_state),
211 Evoral::Parameter(GainAutomation), (AutoState) Off)));
212 gain_astate_menu.items().push_back (MenuElem (_("Play"),
213 sigc::bind (sigc::mem_fun (*(amp.get()), &Automatable::set_parameter_automation_state),
214 Evoral::Parameter(GainAutomation), (AutoState) Play)));
215 gain_astate_menu.items().push_back (MenuElem (_("Write"),
216 sigc::bind (sigc::mem_fun (*(amp.get()), &Automatable::set_parameter_automation_state),
217 Evoral::Parameter(GainAutomation), (AutoState) Write)));
218 gain_astate_menu.items().push_back (MenuElem (_("Touch"),
219 sigc::bind (sigc::mem_fun (*(amp.get()), &Automatable::set_parameter_automation_state),
220 Evoral::Parameter(GainAutomation), (AutoState) Touch)));
222 connections.push_back (gain_automation_style_button.signal_button_press_event().connect (sigc::mem_fun(*this, &GainMeterBase::gain_automation_style_button_event), false));
223 connections.push_back (gain_automation_state_button.signal_button_press_event().connect (sigc::mem_fun(*this, &GainMeterBase::gain_automation_state_button_event), false));
225 boost::shared_ptr<AutomationControl> gc = amp->gain_control();
227 gc->alist()->automation_state_changed.connect (model_connections, invalidator (*this), boost::bind (&GainMeter::gain_automation_state_changed, this), gui_context());
228 gc->alist()->automation_style_changed.connect (model_connections, invalidator (*this), boost::bind (&GainMeter::gain_automation_style_changed, this), gui_context());
230 gain_automation_state_changed ();
233 amp->gain_control()->Changed.connect (model_connections, invalidator (*this), boost::bind (&GainMeterBase::gain_changed, this), gui_context());
235 gain_changed ();
236 show_gain ();
237 update_gain_sensitive ();
240 void
241 GainMeterBase::hide_all_meters ()
243 level_meter->hide_meters();
246 void
247 GainMeter::hide_all_meters ()
249 bool remove_metric_area = false;
251 GainMeterBase::hide_all_meters ();
253 if (remove_metric_area) {
254 if (meter_metric_area.get_parent()) {
255 level_meter->remove (meter_metric_area);
260 void
261 GainMeterBase::setup_meters (int len)
263 level_meter->setup_meters(len, 5);
266 void
267 GainMeter::setup_meters (int len)
269 if (!meter_metric_area.get_parent()) {
270 level_meter->pack_end (meter_metric_area, false, false);
271 meter_metric_area.show_all ();
273 GainMeterBase::setup_meters (len);
276 bool
277 GainMeterBase::gain_key_press (GdkEventKey* ev)
279 if (key_is_legal_for_numeric_entry (ev->keyval)) {
280 /* drop through to normal handling */
281 return false;
283 /* illegal key for gain entry */
284 return true;
287 bool
288 GainMeterBase::peak_button_release (GdkEventButton* ev)
290 /* reset peak label */
292 if (ev->button == 1 && Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier|Keyboard::TertiaryModifier)) {
293 ResetAllPeakDisplays ();
294 } else if (ev->button == 1 && Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
295 if (_route) {
296 ResetGroupPeakDisplays (_route->route_group());
298 } else {
299 reset_peak_display ();
302 return true;
305 void
306 GainMeterBase::reset_peak_display ()
308 _meter->reset_max();
309 level_meter->clear_meters();
310 max_peak = -INFINITY;
311 peak_display.set_label (_("-Inf"));
312 peak_display.set_name ("MixerStripPeakDisplay");
315 void
316 GainMeterBase::reset_group_peak_display (RouteGroup* group)
318 if (_route && group == _route->route_group()) {
319 reset_peak_display ();
323 void
324 GainMeterBase::popup_meter_menu (GdkEventButton *ev)
326 using namespace Menu_Helpers;
328 if (meter_menu == 0) {
329 meter_menu = new Gtk::Menu;
330 MenuList& items = meter_menu->items();
332 items.push_back (MenuElem ("-inf .. +0dBFS"));
333 items.push_back (MenuElem ("-10dB .. +0dBFS"));
334 items.push_back (MenuElem ("-4 .. +0dBFS"));
335 items.push_back (SeparatorElem());
336 items.push_back (MenuElem ("-inf .. -2dBFS"));
337 items.push_back (MenuElem ("-10dB .. -2dBFS"));
338 items.push_back (MenuElem ("-4 .. -2dBFS"));
341 meter_menu->popup (1, ev->time);
344 bool
345 GainMeterBase::gain_focused (GdkEventFocus* ev)
347 if (ev->in) {
348 gain_display.select_region (0, -1);
349 } else {
350 gain_display.select_region (0, 0);
352 return false;
355 void
356 GainMeterBase::gain_activated ()
358 float f;
360 if (sscanf (gain_display.get_text().c_str(), "%f", &f) == 1) {
362 /* clamp to displayable values */
364 f = min (f, 6.0f);
366 _amp->set_gain (dB_to_coefficient(f), this);
368 if (gain_display.has_focus()) {
369 PublicEditor::instance().reset_focus();
374 void
375 GainMeterBase::show_gain ()
377 char buf[32];
379 float v = gain_adjustment.get_value();
381 if (!_is_midi) {
382 if (v == 0.0) {
383 strcpy (buf, _("-inf"));
384 } else {
385 snprintf (buf, sizeof (buf), "%.1f", accurate_coefficient_to_dB (slider_position_to_gain_with_max (v, Config->get_max_gain())));
387 } else {
388 snprintf (buf, sizeof (buf), "%.1f", v);
391 gain_display.set_text (buf);
394 void
395 GainMeterBase::gain_adjusted ()
397 if (!ignore_toggle) {
398 if (_route && _route->amp() == _amp) {
399 if (_is_midi) {
400 _route->set_gain (gain_adjustment.get_value(), this);
401 } else {
402 _route->set_gain (slider_position_to_gain_with_max (gain_adjustment.get_value(), Config->get_max_gain()), this);
404 } else {
405 _amp->set_gain (slider_position_to_gain_with_max (gain_adjustment.get_value(), Config->get_max_gain()), this);
409 show_gain ();
412 void
413 GainMeterBase::effective_gain_display ()
415 gfloat value;
417 if (!_route || _route->output()->n_ports().n_midi() == 0) {
418 value = gain_to_slider_position_with_max (_amp->gain(), Config->get_max_gain());
419 } else {
420 value = _amp->gain ();
423 if (gain_adjustment.get_value() != value) {
424 ignore_toggle = true;
425 gain_adjustment.set_value (value);
426 ignore_toggle = false;
430 void
431 GainMeterBase::gain_changed ()
433 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&GainMeterBase::effective_gain_display, this));
436 void
437 GainMeterBase::set_meter_strip_name (const char * name)
439 meter_metric_area.set_name (name);
442 void
443 GainMeterBase::set_fader_name (const char * name)
445 uint32_t rgb_active = rgba_from_style (name, 0xff, 0, 0xff, 0, "bg", STATE_ACTIVE, false);
446 uint32_t rgb_normal = rgba_from_style (name, 0xff, 0xff, 0, 0, "bg", STATE_NORMAL, false);
448 gain_slider->set_border_colors (rgb_normal, rgb_active);
451 void
452 GainMeterBase::update_gain_sensitive ()
454 bool x = !(_amp->gain_control()->alist()->automation_state() & Play);
455 static_cast<Gtkmm2ext::SliderController*>(gain_slider)->set_sensitive (x);
458 static MeterPoint
459 next_meter_point (MeterPoint mp)
461 switch (mp) {
462 case MeterInput:
463 return MeterPreFader;
464 break;
466 case MeterPreFader:
467 return MeterPostFader;
468 break;
470 case MeterPostFader:
471 return MeterOutput;
472 break;
474 case MeterOutput:
475 return MeterCustom;
476 break;
478 case MeterCustom:
479 return MeterInput;
480 break;
483 /*NOTREACHED*/
484 return MeterInput;
487 gint
488 GainMeterBase::meter_press(GdkEventButton* ev)
490 wait_for_release = false;
492 if (!_route) {
493 return FALSE;
496 if (!ignore_toggle) {
498 if (Keyboard::is_context_menu_event (ev)) {
500 // no menu at this time.
502 } else {
504 if (Keyboard::is_button2_event(ev)) {
506 // Primary-button2 click is the midi binding click
507 // button2-click is "momentary"
509 if (!Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::PrimaryModifier))) {
510 wait_for_release = true;
511 old_meter_point = _route->meter_point ();
515 if (_route && (ev->button == 1 || Keyboard::is_button2_event (ev))) {
517 if (Keyboard::modifier_state_equals (ev->state, Keyboard::ModifierMask (Keyboard::PrimaryModifier|Keyboard::TertiaryModifier))) {
519 /* Primary+Tertiary-click applies change to all routes */
521 _session->foreach_route (this, &GainMeterBase::set_meter_point, next_meter_point (_route->meter_point()));
524 } else if (Keyboard::modifier_state_equals (ev->state, Keyboard::PrimaryModifier)) {
526 /* Primary-click: solo mix group.
527 NOTE: Primary-button2 is MIDI learn.
530 if (ev->button == 1) {
531 set_route_group_meter_point (*_route, next_meter_point (_route->meter_point()));
534 } else {
536 /* click: change just this route */
538 // XXX no undo yet
540 _route->set_meter_point (next_meter_point (_route->meter_point()));
546 return true;
550 gint
551 GainMeterBase::meter_release(GdkEventButton*)
553 if (!ignore_toggle) {
554 if (wait_for_release) {
555 wait_for_release = false;
557 if (_route) {
558 set_meter_point (*_route, old_meter_point);
563 return true;
566 void
567 GainMeterBase::set_meter_point (Route& route, MeterPoint mp)
569 route.set_meter_point (mp);
572 void
573 GainMeterBase::set_route_group_meter_point (Route& route, MeterPoint mp)
575 RouteGroup* route_group;
577 if ((route_group = route.route_group ()) != 0) {
578 route_group->foreach_route (boost::bind (&Route::set_meter_point, _1, mp, false));
579 } else {
580 route.set_meter_point (mp);
584 void
585 GainMeterBase::meter_point_clicked ()
587 if (_route) {
588 /* WHAT? */
592 bool
593 GainMeterBase::gain_slider_button_press (GdkEventButton* ev)
595 switch (ev->type) {
596 case GDK_BUTTON_PRESS:
597 _amp->gain_control()->start_touch (_amp->session().transport_frame());
598 break;
599 default:
600 return false;
603 return true;
606 bool
607 GainMeterBase::gain_slider_button_release (GdkEventButton* ev)
609 _amp->gain_control()->stop_touch (false, _amp->session().transport_frame());
610 return true;
613 gint
614 GainMeterBase::gain_automation_state_button_event (GdkEventButton *ev)
616 if (ev->type == GDK_BUTTON_RELEASE) {
617 return TRUE;
620 switch (ev->button) {
621 case 1:
622 gain_astate_menu.popup (1, ev->time);
623 break;
624 default:
625 break;
628 return TRUE;
631 gint
632 GainMeterBase::gain_automation_style_button_event (GdkEventButton *ev)
634 if (ev->type == GDK_BUTTON_RELEASE) {
635 return TRUE;
638 switch (ev->button) {
639 case 1:
640 gain_astyle_menu.popup (1, ev->time);
641 break;
642 default:
643 break;
645 return TRUE;
648 string
649 GainMeterBase::astate_string (AutoState state)
651 return _astate_string (state, false);
654 string
655 GainMeterBase::short_astate_string (AutoState state)
657 return _astate_string (state, true);
660 string
661 GainMeterBase::_astate_string (AutoState state, bool shrt)
663 string sstr;
665 switch (state) {
666 case Off:
667 sstr = (shrt ? "M" : _("M"));
668 break;
669 case Play:
670 sstr = (shrt ? "P" : _("P"));
671 break;
672 case Touch:
673 sstr = (shrt ? "T" : _("T"));
674 break;
675 case Write:
676 sstr = (shrt ? "W" : _("W"));
677 break;
680 return sstr;
683 string
684 GainMeterBase::astyle_string (AutoStyle style)
686 return _astyle_string (style, false);
689 string
690 GainMeterBase::short_astyle_string (AutoStyle style)
692 return _astyle_string (style, true);
695 string
696 GainMeterBase::_astyle_string (AutoStyle style, bool shrt)
698 if (style & Trim) {
699 return _("Trim");
700 } else {
701 /* XXX it might different in different languages */
703 return (shrt ? _("Abs") : _("Abs"));
707 void
708 GainMeterBase::gain_automation_style_changed ()
710 switch (_width) {
711 case Wide:
712 gain_automation_style_button.set_label (astyle_string(_amp->gain_control()->alist()->automation_style()));
713 break;
714 case Narrow:
715 gain_automation_style_button.set_label (short_astyle_string(_amp->gain_control()->alist()->automation_style()));
716 break;
720 void
721 GainMeterBase::gain_automation_state_changed ()
723 ENSURE_GUI_THREAD (*this, &GainMeterBase::gain_automation_state_changed)
725 bool x;
727 switch (_width) {
728 case Wide:
729 gain_automation_state_button.set_label (astate_string(_amp->gain_control()->alist()->automation_state()));
730 break;
731 case Narrow:
732 gain_automation_state_button.set_label (short_astate_string(_amp->gain_control()->alist()->automation_state()));
733 break;
736 x = (_amp->gain_control()->alist()->automation_state() != Off);
738 if (gain_automation_state_button.get_active() != x) {
739 ignore_toggle = true;
740 gain_automation_state_button.set_active (x);
741 ignore_toggle = false;
744 update_gain_sensitive ();
746 /* start watching automation so that things move */
748 gain_watching.disconnect();
750 if (x) {
751 gain_watching = ARDOUR_UI::RapidScreenUpdate.connect (sigc::mem_fun (*this, &GainMeterBase::effective_gain_display));
755 void
756 GainMeterBase::update_meters()
758 char buf[32];
759 float mpeak = level_meter->update_meters();
761 if (mpeak > max_peak) {
762 max_peak = mpeak;
763 if (mpeak <= -200.0f) {
764 peak_display.set_label (_("-inf"));
765 } else {
766 snprintf (buf, sizeof(buf), "%.1f", mpeak);
767 peak_display.set_label (buf);
770 if (mpeak >= 0.0f) {
771 peak_display.set_name ("MixerStripPeakDisplayPeak");
776 void GainMeterBase::color_handler(bool dpi)
778 color_changed = true;
779 dpi_changed = (dpi) ? true : false;
780 setup_meters();
783 void
784 GainMeterBase::set_width (Width w, int len)
786 _width = w;
787 level_meter->setup_meters (len);
791 void
792 GainMeterBase::on_theme_changed()
794 style_changed = true;
797 GainMeter::GainMeter (Session* s, int fader_length)
798 : GainMeterBase (s, slider, false, fader_length)
799 , gain_display_box(true, 0)
800 , hbox(true, 2)
802 gain_display_box.pack_start (gain_display, true, true);
804 meter_metric_area.set_name ("AudioTrackMetrics");
805 set_size_request_to_display_given_text (meter_metric_area, "-127", 0, 0);
807 gain_automation_style_button.set_name ("MixerAutomationModeButton");
808 gain_automation_state_button.set_name ("MixerAutomationPlaybackButton");
810 ARDOUR_UI::instance()->set_tip (gain_automation_state_button, _("Fader automation mode"));
811 ARDOUR_UI::instance()->set_tip (gain_automation_style_button, _("Fader automation type"));
813 gain_automation_style_button.unset_flags (Gtk::CAN_FOCUS);
814 gain_automation_state_button.unset_flags (Gtk::CAN_FOCUS);
816 gain_automation_state_button.set_size_request(15, 15);
817 gain_automation_style_button.set_size_request(15, 15);
819 fader_vbox = manage (new Gtk::VBox());
820 fader_vbox->set_spacing (0);
821 fader_vbox->pack_start (*gain_slider, true, true);
823 fader_alignment.set (0.5, 0.5, 0.0, 1.0);
824 fader_alignment.add (*fader_vbox);
826 hbox.pack_start (fader_alignment, true, true);
828 set_spacing (2);
830 pack_start (gain_display_box, Gtk::PACK_SHRINK);
831 pack_start (hbox, Gtk::PACK_SHRINK);
833 meter_alignment.set (0.5, 0.5, 0.0, 1.0);
834 meter_alignment.add (*level_meter);
836 meter_metric_area.signal_expose_event().connect (
837 sigc::mem_fun(*this, &GainMeter::meter_metrics_expose));
840 void
841 GainMeter::set_controls (boost::shared_ptr<Route> r,
842 boost::shared_ptr<PeakMeter> meter,
843 boost::shared_ptr<Amp> amp)
845 if (meter_alignment.get_parent()) {
846 hbox.remove (meter_alignment);
849 if (peak_display.get_parent()) {
850 gain_display_box.remove (peak_display);
853 if (gain_automation_state_button.get_parent()) {
854 fader_vbox->remove (gain_automation_state_button);
857 GainMeterBase::set_controls (r, meter, amp);
859 if (_meter) {
860 _meter->ConfigurationChanged.connect (
861 model_connections, invalidator (*this), ui_bind (&GainMeter::meter_configuration_changed, this, _1), gui_context()
864 meter_configuration_changed (_meter->input_streams ());
869 if we have a non-hidden route (ie. we're not the click or the auditioner),
870 pack some route-dependent stuff.
873 gain_display_box.pack_end (peak_display, true, true);
874 hbox.pack_start (meter_alignment, true, true);
876 if (r && !r->is_hidden()) {
877 fader_vbox->pack_start (gain_automation_state_button, false, false, 0);
880 setup_meters ();
881 hbox.show_all ();
885 GainMeter::get_gm_width ()
887 Gtk::Requisition sz;
888 hbox.size_request (sz);
889 return sz.width;
892 Glib::RefPtr<Gdk::Pixmap>
893 GainMeter::render_metrics (Gtk::Widget& w, vector<DataType> types)
895 Glib::RefPtr<Gdk::Window> win (w.get_window());
896 Glib::RefPtr<Gdk::GC> bg_gc (w.get_style()->get_bg_gc (Gtk::STATE_NORMAL));
898 gint width, height;
899 win->get_size (width, height);
901 Glib::RefPtr<Gdk::Pixmap> pixmap = Gdk::Pixmap::create (win, width, height);
903 metric_pixmaps[w.get_name()] = pixmap;
905 pixmap->draw_rectangle (bg_gc, true, 0, 0, width, height);
907 Glib::RefPtr<Pango::Layout> layout = w.create_pango_layout ("");
909 for (vector<DataType>::const_iterator i = types.begin(); i != types.end(); ++i) {
911 Glib::RefPtr<Gdk::GC> fg_gc (w.get_style()->get_fg_gc (Gtk::STATE_NORMAL));
913 if (types.size() > 1) {
914 /* we're overlaying more than 1 set of marks, so use different colours */
915 Gdk::Color c;
916 switch (*i) {
917 case DataType::AUDIO:
918 c.set_rgb_p (1, 1, 1);
919 break;
920 case DataType::MIDI:
921 c.set_rgb_p (0.2, 0.2, 0.5);
922 break;
925 fg_gc->set_rgb_fg_color (c);
928 vector<int> points;
930 switch (*i) {
931 case DataType::AUDIO:
932 points.push_back (-50);
933 points.push_back (-40);
934 points.push_back (-30);
935 points.push_back (-20);
936 points.push_back (-10);
937 points.push_back (-3);
938 points.push_back (0);
939 points.push_back (4);
940 break;
942 case DataType::MIDI:
943 points.push_back (0);
944 if (types.size() == 1) {
945 points.push_back (32);
946 } else {
947 /* tweak so as not to overlay the -30dB mark */
948 points.push_back (48);
950 points.push_back (64);
951 points.push_back (96);
952 points.push_back (127);
953 break;
956 char buf[32];
958 for (vector<int>::const_iterator j = points.begin(); j != points.end(); ++j) {
960 float fraction = 0;
961 switch (*i) {
962 case DataType::AUDIO:
963 fraction = log_meter (*j);
964 break;
965 case DataType::MIDI:
966 fraction = *j / 127.0;
967 break;
970 gint const pos = height - (gint) floor (height * fraction);
972 snprintf (buf, sizeof (buf), "%d", abs (*j));
974 layout->set_text (buf);
976 /* we want logical extents, not ink extents here */
978 int tw, th;
979 layout->get_pixel_size (tw, th);
981 pixmap->draw_line (fg_gc, 0, pos, 4, pos);
983 int p = pos - (th / 2);
984 p = min (p, height - th);
985 p = max (p, 0);
987 pixmap->draw_layout (fg_gc, 6, p, layout);
991 return pixmap;
994 gint
995 GainMeter::meter_metrics_expose (GdkEventExpose *ev)
997 Glib::RefPtr<Gdk::Window> win (meter_metric_area.get_window());
998 Glib::RefPtr<Gdk::GC> bg_gc (meter_metric_area.get_style()->get_bg_gc (Gtk::STATE_INSENSITIVE));
999 GdkRectangle base_rect;
1000 GdkRectangle draw_rect;
1001 gint width, height;
1003 win->get_size (width, height);
1005 base_rect.width = width;
1006 base_rect.height = height;
1007 base_rect.x = 0;
1008 base_rect.y = 0;
1010 Glib::RefPtr<Gdk::Pixmap> pixmap;
1011 std::map<string,Glib::RefPtr<Gdk::Pixmap> >::iterator i = metric_pixmaps.find (meter_metric_area.get_name());
1013 if (i == metric_pixmaps.end() || style_changed || dpi_changed) {
1014 pixmap = render_metrics (meter_metric_area, _types);
1015 } else {
1016 pixmap = i->second;
1019 gdk_rectangle_intersect (&ev->area, &base_rect, &draw_rect);
1020 win->draw_drawable (bg_gc, pixmap, draw_rect.x, draw_rect.y, draw_rect.x, draw_rect.y, draw_rect.width, draw_rect.height);
1021 style_changed = false;
1022 return true;
1025 boost::shared_ptr<PBD::Controllable>
1026 GainMeterBase::get_controllable()
1028 if (_amp) {
1029 return _amp->gain_control();
1030 } else {
1031 return boost::shared_ptr<PBD::Controllable>();
1035 void
1036 GainMeter::meter_configuration_changed (ChanCount c)
1038 _types.clear ();
1040 for (DataType::iterator i = DataType::begin(); i != DataType::end(); ++i) {
1041 if (c.get (*i) > 0) {
1042 _types.push_back (*i);
1046 style_changed = true;
1047 meter_metric_area.queue_draw ();