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