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