Use an explicit class-member for the mixer-strip container
[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/compose.h"
25 #include "pbd/error.h"
26
27 #include "ardour/automatable.h"
28 #include "ardour/automation_control.h"
29 #include "ardour/session.h"
30 #include "ardour/tempo.h"
31
32 #include "widgets/ardour_button.h"
33 #include "widgets/ardour_knob.h"
34
35 #include "automation_controller.h"
36 #include "context_menu_helper.h"
37 #include "gui_thread.h"
38 #include "note_select_dialog.h"
39 #include "timers.h"
40
41 #include "pbd/i18n.h"
42
43 using namespace ARDOUR;
44 using namespace Gtk;
45 using namespace ArdourWidgets;
46
47 using PBD::Controllable;
48
49 AutomationBarController::AutomationBarController (
50                 boost::shared_ptr<AutomationControl> ac,
51                 Adjustment*                          adj)
52         : ArdourWidgets::BarController (*adj, ac)
53         , _controllable (ac)
54 {
55 }
56
57 std::string
58 AutomationBarController::get_label (double& xpos)
59 {
60         xpos = 0.5;
61         return _controllable->get_user_string();
62 }
63
64 AutomationBarController::~AutomationBarController()
65 {
66 }
67
68 AutomationController::AutomationController(boost::shared_ptr<AutomationControl> ac,
69                                            Adjustment*                          adj,
70                                            bool                                 use_knob)
71         : _widget(NULL)
72         , _controllable(ac)
73         , _adjustment(adj)
74         , _ignore_change(false)
75         , _grabbed(false)
76 {
77         if (ac->toggled()) {
78                 ArdourButton* but = manage(new ArdourButton());
79
80                 // Apply styles for special types
81                 if (ac->parameter().type() == MuteAutomation) {
82                         but->set_name("mute button");
83                 } else if (ac->parameter().type() == SoloAutomation) {
84                         but->set_name("solo button");
85                 } else {
86                         but->set_name("generic button");
87                 }
88                 but->set_fallthrough_to_parent(true);
89                 but->set_controllable(ac);
90                 but->signal_button_press_event().connect(
91                         sigc::mem_fun(*this, &AutomationController::button_press));
92                 but->signal_button_release_event().connect(
93                         sigc::mem_fun(*this, &AutomationController::button_release));
94                 const bool active = _adjustment->get_value() >= 0.5;
95                 if (but->get_active() != active) {
96                         but->set_active(active);
97                 }
98                 _widget = but;
99         } else if (use_knob) {
100                 ArdourKnob* knob = manage (new ArdourKnob (ArdourKnob::default_elements, ArdourKnob::Detent));
101                 knob->set_controllable (ac);
102                 knob->set_name("processor control knob");
103                 _widget = knob;
104                 knob->StartGesture.connect(sigc::mem_fun(*this, &AutomationController::start_touch));
105                 knob->StopGesture.connect(sigc::mem_fun(*this, &AutomationController::end_touch));
106         } else {
107                 AutomationBarController* bar = manage(new AutomationBarController(ac, adj));
108
109                 bar->set_name(X_("ProcessorControlSlider"));
110                 bar->StartGesture.connect(
111                         sigc::mem_fun(*this, &AutomationController::start_touch));
112                 bar->StopGesture.connect(
113                         sigc::mem_fun(*this, &AutomationController::end_touch));
114                 bar->signal_button_release_event().connect(
115                         sigc::mem_fun(*this, &AutomationController::on_button_release));
116
117                 _widget = bar;
118         }
119
120         _adjustment->signal_value_changed().connect(
121                 sigc::mem_fun(*this, &AutomationController::value_adjusted));
122
123         ac->Changed.connect (_changed_connections, invalidator (*this), boost::bind (&AutomationController::display_effective_value, this), gui_context());
124         display_effective_value ();
125
126         if (ac->alist ()) {
127                 ac->alist()->automation_state_changed.connect (_changed_connections, invalidator (*this), boost::bind (&AutomationController::automation_state_changed, this), gui_context());
128                 automation_state_changed ();
129         }
130
131         add(*_widget);
132         show_all();
133 }
134
135 AutomationController::~AutomationController()
136 {
137 }
138
139 boost::shared_ptr<AutomationController>
140 AutomationController::create(const Evoral::Parameter&             param,
141                              const ParameterDescriptor&           desc,
142                              boost::shared_ptr<AutomationControl> ac,
143                              bool use_knob)
144 {
145         const double lo        = ac->internal_to_interface(desc.lower);
146         const double up        = ac->internal_to_interface(desc.upper);
147         const double normal    = ac->internal_to_interface(desc.normal);
148         const double smallstep = ac->internal_to_interface(desc.lower + desc.smallstep) - lo;
149         const double largestep = ac->internal_to_interface(desc.lower + desc.largestep) - lo;
150
151         Gtk::Adjustment* adjustment = manage (
152                 new Gtk::Adjustment (normal, lo, up, smallstep, largestep));
153
154         assert (ac);
155         assert(ac->parameter() == param);
156         return boost::shared_ptr<AutomationController>(new AutomationController(ac, adjustment, use_knob));
157 }
158
159 void
160 AutomationController::automation_state_changed ()
161 {
162         bool x = _controllable->alist()->automation_state() & Play;
163         _widget->set_sensitive (!x);
164 }
165
166 void
167 AutomationController::display_effective_value ()
168 {
169         double const interface_value = _controllable->internal_to_interface(_controllable->get_value());
170
171         if (_grabbed) {
172                 /* we cannot use _controllable->touching() here
173                  * because that's only set in Write or Touch mode.
174                  * Besides ctrl-surfaces may also set touching()
175                  */
176                 return;
177         }
178         if (_adjustment->get_value () != interface_value) {
179                 _ignore_change = true;
180                 _adjustment->set_value (interface_value);
181                 _ignore_change = false;
182         }
183
184 }
185
186 void
187 AutomationController::value_adjusted ()
188 {
189         if (!_ignore_change) {
190                 const double new_val = _controllable->interface_to_internal(_adjustment->get_value());
191                 if (_controllable->user_double() != new_val) {
192                         _controllable->set_value (new_val, Controllable::NoGroup);
193                 }
194         }
195
196         /* A bar controller will automatically follow the adjustment, but for a
197            button we have to do it manually. */
198         ArdourButton* but = dynamic_cast<ArdourButton*>(_widget);
199         if (but) {
200                 const bool active = _adjustment->get_value() >= 0.5;
201                 if (but->get_active() != active) {
202                         but->set_active(active);
203                 }
204         }
205 }
206
207 void
208 AutomationController::start_touch()
209 {
210         _grabbed = true;
211         _controllable->start_touch (_controllable->session().transport_sample());
212 }
213
214 void
215 AutomationController::end_touch ()
216 {
217         _controllable->stop_touch (_controllable->session().transport_sample());
218         if (_grabbed) {
219                 _grabbed = false;
220                 display_effective_value ();
221         }
222 }
223
224 bool
225 AutomationController::button_press (GdkEventButton*)
226 {
227         ArdourButton* but = dynamic_cast<ArdourButton*>(_widget);
228         if (but) {
229                 start_touch ();
230                 _controllable->set_value (but->get_active () ? 0.0 : 1.0, Controllable::UseGroup);
231         }
232         return false;
233 }
234
235 bool
236 AutomationController::button_release (GdkEventButton*)
237 {
238         end_touch ();
239         return true;
240 }
241
242 static double
243 midi_note_to_hz(int note)
244 {
245         const double tuning = 440.0;
246         return tuning * pow(2, (note - 69.0) / 12.0);
247 }
248
249 static double
250 clamp(double val, double min, double max)
251 {
252         if (val < min) {
253                 return min;
254         } else if (val > max) {
255                 return max;
256         }
257         return val;
258 }
259
260 void
261 AutomationController::run_note_select_dialog()
262 {
263         const ARDOUR::ParameterDescriptor& desc   = _controllable->desc();
264         NoteSelectDialog*                  dialog = new NoteSelectDialog();
265         if (dialog->run() == Gtk::RESPONSE_ACCEPT) {
266                 const double value = ((_controllable->desc().unit == ARDOUR::ParameterDescriptor::HZ)
267                                       ? midi_note_to_hz(dialog->note_number())
268                                       : dialog->note_number());
269                 _controllable->set_value(clamp(value, desc.lower, desc.upper), Controllable::NoGroup);
270         }
271         delete dialog;
272 }
273
274 void
275 AutomationController::set_freq_beats(double beats)
276 {
277         const ARDOUR::ParameterDescriptor& desc    = _controllable->desc();
278         const ARDOUR::Session&             session = _controllable->session();
279         const samplepos_t                  pos     = session.transport_sample();
280         const ARDOUR::Tempo&               tempo   = session.tempo_map().tempo_at_sample (pos);
281         const double                       bpm     = tempo.note_types_per_minute();
282         const double                       bps     = bpm / 60.0;
283         const double                       freq    = bps / beats;
284         _controllable->set_value(clamp(freq, desc.lower, desc.upper), Controllable::NoGroup);
285 }
286
287 void
288 AutomationController::set_ratio(double ratio)
289 {
290         const ARDOUR::ParameterDescriptor& desc  = _controllable->desc();
291         const double                       value = _controllable->get_value() * ratio;
292         _controllable->set_value(clamp(value, desc.lower, desc.upper), Controllable::NoGroup);
293 }
294
295 bool
296 AutomationController::on_button_release(GdkEventButton* ev)
297 {
298         using namespace Gtk::Menu_Helpers;
299
300         if (ev->button != 3) {
301                 return false;
302         }
303
304         const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
305         if (desc.unit == ARDOUR::ParameterDescriptor::MIDI_NOTE) {
306                 Gtk::Menu* menu  = ARDOUR_UI_UTILS::shared_popup_menu ();
307                 MenuList&  items = menu->items();
308                 items.push_back(MenuElem(_("Select Note..."),
309                                          sigc::mem_fun(*this, &AutomationController::run_note_select_dialog)));
310                 menu->popup(1, ev->time);
311                 return true;
312         } else if (desc.unit == ARDOUR::ParameterDescriptor::HZ) {
313                 Gtk::Menu* menu  = ARDOUR_UI_UTILS::shared_popup_menu ();
314                 MenuList&  items = menu->items();
315                 items.push_back(MenuElem(_("Halve"),
316                                          sigc::bind(sigc::mem_fun(*this, &AutomationController::set_ratio),
317                                                     0.5)));
318                 items.push_back(MenuElem(_("Double"),
319                                          sigc::bind(sigc::mem_fun(*this, &AutomationController::set_ratio),
320                                                     2.0)));
321                 const bool is_audible = desc.upper > 40.0;
322                 const bool is_low     = desc.lower < 1.0;
323                 if (is_audible) {
324                         items.push_back(MenuElem(_("Select Note..."),
325                                                  sigc::mem_fun(*this, &AutomationController::run_note_select_dialog)));
326                 }
327                 if (is_low) {
328                         for (int beats = 1; beats <= 16; ++beats) {
329                                 items.push_back(MenuElem (string_compose(P_("Set to %1 beat", "Set to %1 beats", beats), beats),
330                                                          sigc::bind(sigc::mem_fun(*this, &AutomationController::set_freq_beats),
331                                                                     (double)beats)));
332                         }
333                 }
334                 menu->popup(1, ev->time);
335                 return true;
336         }
337
338         return false;
339 }
340
341 /** Stop updating our value from our controllable */
342 void
343 AutomationController::stop_updating ()
344 {
345         _screen_update_connection.disconnect ();
346 }
347
348 void
349 AutomationController::disable_vertical_scroll ()
350 {
351         AutomationBarController* bar = dynamic_cast<AutomationBarController*>(_widget);
352         if (bar) {
353                 bar->set_tweaks (
354                         ArdourWidgets::ArdourFader::Tweaks(bar->tweaks() | ArdourWidgets::ArdourFader::NoVerticalScroll));
355         }
356 }