merge fix
[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->set_controllable(ac);
88                 but->signal_clicked.connect(
89                         sigc::mem_fun(*this, &AutomationController::toggled));
90
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()));
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         StartGesture.emit();  /* EMIT SIGNAL */
177 }
178
179 void
180 AutomationController::end_touch ()
181 {
182         if (_controllable->automation_state() == Touch) {
183
184                 bool mark = false;
185                 double when = 0;
186
187                 if (_controllable->session().transport_rolling()) {
188                         mark = true;
189                         when = _controllable->session().transport_frame();
190                 }
191
192                 _controllable->stop_touch (mark, when);
193         }
194         StopGesture.emit();  /* EMIT SIGNAL */
195 }
196
197 void
198 AutomationController::toggled ()
199 {
200         ArdourButton* but = dynamic_cast<ArdourButton*>(_widget);
201         if (but) {
202                 if (_controllable->session().transport_rolling()) {
203                         if (_controllable->automation_state() == Touch) {
204                                 _controllable->set_automation_state(Write);
205                         }
206                         if (_controllable->list()) {
207                                 _controllable->list()->set_in_write_pass(true, true, _controllable->session().audible_frame());
208                         }
209                 }
210                 const bool was_active = _controllable->get_value() >= 0.5;
211                 if (was_active && but->get_active()) {
212                         _adjustment->set_value(0.0);
213                         but->set_active(false);
214                 } else if (!was_active && !but->get_active()) {
215                         _adjustment->set_value(1.0);
216                         but->set_active(true);
217                 }
218         }
219 }
220
221 static double
222 midi_note_to_hz(int note)
223 {
224         const double tuning = 440.0;
225         return tuning * pow(2, (note - 69.0) / 12.0);
226 }
227
228 static double
229 clamp(double val, double min, double max)
230 {
231         if (val < min) {
232                 return min;
233         } else if (val > max) {
234                 return max;
235         }
236         return val;
237 }
238
239 void
240 AutomationController::run_note_select_dialog()
241 {
242         const ARDOUR::ParameterDescriptor& desc   = _controllable->desc();
243         NoteSelectDialog*                  dialog = new NoteSelectDialog();
244         if (dialog->run() == Gtk::RESPONSE_ACCEPT) {
245                 const double value = ((_controllable->desc().unit == ARDOUR::ParameterDescriptor::HZ)
246                                       ? midi_note_to_hz(dialog->note_number())
247                                       : dialog->note_number());
248                 _controllable->set_value(clamp(value, desc.lower, desc.upper));
249         }
250         delete dialog;
251 }
252
253 void
254 AutomationController::set_freq_beats(double beats)
255 {
256         const ARDOUR::ParameterDescriptor& desc    = _controllable->desc();
257         const ARDOUR::Session&             session = _controllable->session();
258         const framepos_t                   pos     = session.transport_frame();
259         const ARDOUR::Tempo&               tempo   = session.tempo_map().tempo_at(pos);
260         const double                       bpm     = tempo.beats_per_minute();
261         const double                       bps     = bpm / 60.0;
262         const double                       freq    = bps / beats;
263         _controllable->set_value(clamp(freq, desc.lower, desc.upper));
264 }
265
266 void
267 AutomationController::set_ratio(double ratio)
268 {
269         const ARDOUR::ParameterDescriptor& desc  = _controllable->desc();
270         const double                       value = _controllable->get_value() * ratio;
271         _controllable->set_value(clamp(value, desc.lower, desc.upper));
272 }
273
274 bool
275 AutomationController::on_button_release(GdkEventButton* ev)
276 {
277         using namespace Gtk::Menu_Helpers;
278
279         if (ev->button != 3) {
280                 return false;
281         }
282
283         const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
284         if (desc.unit == ARDOUR::ParameterDescriptor::MIDI_NOTE) {
285                 Gtk::Menu* menu  = manage(new Menu());
286                 MenuList&  items = menu->items();
287                 items.push_back(MenuElem(_("Select Note..."),
288                                          sigc::mem_fun(*this, &AutomationController::run_note_select_dialog)));
289                 menu->popup(1, ev->time);
290                 return true;
291         } else if (desc.unit == ARDOUR::ParameterDescriptor::HZ) {
292                 Gtk::Menu* menu  = manage(new Menu());
293                 MenuList&  items = menu->items();
294                 items.push_back(MenuElem(_("Halve"),
295                                          sigc::bind(sigc::mem_fun(*this, &AutomationController::set_ratio),
296                                                     0.5)));
297                 items.push_back(MenuElem(_("Double"),
298                                          sigc::bind(sigc::mem_fun(*this, &AutomationController::set_ratio),
299                                                     2.0)));
300                 const bool is_audible = desc.upper > 40.0;
301                 const bool is_low     = desc.lower < 1.0;
302                 if (is_audible) {
303                         items.push_back(MenuElem(_("Select Note..."),
304                                                  sigc::mem_fun(*this, &AutomationController::run_note_select_dialog)));
305                 }
306                 if (is_low) {
307                         for (int beats = 1; beats <= 16; ++beats) {
308                                 items.push_back(MenuElem (string_compose(P_("Set to %1 beat", "Set to %1 beats", beats), beats),
309                                                          sigc::bind(sigc::mem_fun(*this, &AutomationController::set_freq_beats),
310                                                                     (double)beats)));
311                         }
312                 }
313                 menu->popup(1, ev->time);
314                 return true;
315         }
316
317         return false;
318 }
319
320 void
321 AutomationController::value_changed ()
322 {
323         Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&AutomationController::display_effective_value, this));
324 }
325
326 /** Stop updating our value from our controllable */
327 void
328 AutomationController::stop_updating ()
329 {
330         _screen_update_connection.disconnect ();
331 }
332
333 void
334 AutomationController::disable_vertical_scroll ()
335 {
336         AutomationBarController* bar = dynamic_cast<AutomationBarController*>(_widget);
337         if (bar) {
338                 bar->set_tweaks (
339                         Gtkmm2ext::PixFader::Tweaks(bar->tweaks() |
340                                                     Gtkmm2ext::PixFader::NoVerticalScroll));
341         }
342 }