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