Display amp automation in dB using a logarithmic fader.
[ardour.git] / gtk2_ardour / automation_controller.cc
1 /*
2     Copyright (C) 2007 Paul Davis
3     Author: David Robillard
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #include <iomanip>
22 #include <cmath>
23
24 #include "pbd/error.h"
25
26 #include "ardour/automation_list.h"
27 #include "ardour/automation_control.h"
28 #include "ardour/event_type_map.h"
29 #include "ardour/automatable.h"
30 #include "ardour/panner.h"
31 #include "ardour/pan_controllable.h"
32 #include "ardour/session.h"
33
34 #include "ardour_ui.h"
35 #include "utils.h"
36 #include "automation_controller.h"
37 #include "gui_thread.h"
38
39 #include "i18n.h"
40
41 using namespace ARDOUR;
42 using namespace Gtk;
43
44 AutomationController::AutomationController(boost::shared_ptr<Automatable> printer, boost::shared_ptr<AutomationControl> ac, Adjustment* adj)
45         : BarController (*adj, ac)
46         , _ignore_change(false)
47         , _printer (printer)
48         , _controllable(ac)
49         , _adjustment(adj)
50 {
51         assert (_printer);
52
53         set_name (X_("PluginSlider")); // FIXME: get yer own name!
54         set_style (BarController::LeftToRight);
55         set_use_parent (true);
56
57         StartGesture.connect (sigc::mem_fun(*this, &AutomationController::start_touch));
58         StopGesture.connect (sigc::mem_fun(*this, &AutomationController::end_touch));
59
60         _adjustment->signal_value_changed().connect (
61                         sigc::mem_fun(*this, &AutomationController::value_adjusted));
62
63         _screen_update_connection = ARDOUR_UI::RapidScreenUpdate.connect (
64                         sigc::mem_fun (*this, &AutomationController::display_effective_value));
65
66         ac->Changed.connect (_changed_connection, invalidator (*this), boost::bind (&AutomationController::value_changed, this), gui_context());
67 }
68
69 AutomationController::~AutomationController()
70 {
71 }
72
73 boost::shared_ptr<AutomationController>
74 AutomationController::create(
75                 boost::shared_ptr<Automatable> printer,
76                 const Evoral::Parameter& param,
77                 boost::shared_ptr<AutomationControl> ac)
78 {
79         Gtk::Adjustment* adjustment = manage (
80                 new Gtk::Adjustment (
81                         ac->internal_to_interface (param.normal()),
82                         ac->internal_to_interface (param.min()),
83                         ac->internal_to_interface (param.max()),
84                         (param.max() - param.min()) / 100.0,
85                         (param.max() - param.min()) / 10.0
86                         )
87                 );
88
89         assert (ac);
90         assert(ac->parameter() == param);
91         return boost::shared_ptr<AutomationController>(new AutomationController(printer, ac, adjustment));
92 }
93
94 std::string
95 AutomationController::get_label (double& xpos)
96 {
97         xpos = 0.5;
98         return _printer->value_as_string (_controllable);
99 }
100
101 void
102 AutomationController::display_effective_value()
103 {
104         double const interface_value = _controllable->internal_to_interface (_controllable->get_value());
105
106         if (_adjustment->get_value () != interface_value) {
107                 _ignore_change = true;
108                 _adjustment->set_value (interface_value);
109                 _ignore_change = false;
110         }
111 }
112
113 void
114 AutomationController::value_adjusted ()
115 {
116         if (!_ignore_change) {
117                 _controllable->set_value (_controllable->interface_to_internal (_adjustment->get_value()));
118         }
119 }
120
121 void
122 AutomationController::start_touch()
123 {
124         _controllable->start_touch (_controllable->session().transport_frame());
125 }
126
127 void
128 AutomationController::end_touch ()
129 {
130         if (_controllable->automation_state() == Touch) {
131
132                 bool mark = false;
133                 double when = 0;
134
135                 if (_controllable->session().transport_rolling()) {
136                         mark = true;
137                         when = _controllable->session().transport_frame();
138                 }
139
140                 _controllable->stop_touch (mark, when);
141         }
142 }
143
144 void
145 AutomationController::automation_state_changed ()
146 {
147         ENSURE_GUI_THREAD (*this, &AutomationController::automation_state_changed)
148
149         bool x = (_controllable->automation_state() != Off);
150
151         /* start watching automation so that things move */
152
153         _screen_update_connection.disconnect();
154
155         if (x) {
156                 _screen_update_connection = ARDOUR_UI::RapidScreenUpdate.connect (
157                                 sigc::mem_fun (*this, &AutomationController::display_effective_value));
158         }
159 }
160
161 void
162 AutomationController::value_changed ()
163 {
164         Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&AutomationController::display_effective_value, this));
165 }
166
167 /** Stop updating our value from our controllable */
168 void
169 AutomationController::stop_updating ()
170 {
171         _screen_update_connection.disconnect ();
172 }