Toggled automation fixes.
[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 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->set_controllable(ac);
87                 but->signal_clicked.connect(
88                         sigc::mem_fun(*this, &AutomationController::toggled));
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 = Timers::rapid_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         const double smallstep = ac->internal_to_interface(desc.lower + desc.smallstep);
130         const double largestep = ac->internal_to_interface(desc.lower + desc.largestep);
131
132         Gtk::Adjustment* adjustment = manage (
133                 new Gtk::Adjustment (normal, lo, up, smallstep, largestep));
134
135         assert (ac);
136         assert(ac->parameter() == param);
137         return boost::shared_ptr<AutomationController>(new AutomationController(printer, ac, adjustment));
138 }
139
140 void
141 AutomationController::display_effective_value()
142 {
143         double const interface_value = _controllable->internal_to_interface(_controllable->get_value());
144
145         if (_adjustment->get_value () != interface_value) {
146                 _ignore_change = true;
147                 _adjustment->set_value (interface_value);
148                 _ignore_change = false;
149         }
150 }
151
152 void
153 AutomationController::value_adjusted ()
154 {
155         if (!_ignore_change) {
156                 _controllable->set_value (_controllable->interface_to_internal(_adjustment->get_value()));
157         }
158
159         /* A bar controller will automatically follow the adjustment, but for a
160            button we have to do it manually. */
161         ArdourButton* but = dynamic_cast<ArdourButton*>(_widget);
162         if (but) {
163                 const bool active = _adjustment->get_value() >= 0.5;
164                 if (but->get_active() != active) {
165                         but->set_active(active);
166                 }
167         }
168 }
169
170 void
171 AutomationController::start_touch()
172 {
173         _controllable->start_touch (_controllable->session().transport_frame());
174         StartGesture.emit();  /* EMIT SIGNAL */
175 }
176
177 void
178 AutomationController::end_touch ()
179 {
180         if (_controllable->automation_state() == Touch) {
181
182                 bool mark = false;
183                 double when = 0;
184
185                 if (_controllable->session().transport_rolling()) {
186                         mark = true;
187                         when = _controllable->session().transport_frame();
188                 }
189
190                 _controllable->stop_touch (mark, when);
191         }
192         StopGesture.emit();  /* EMIT SIGNAL */
193 }
194
195 void
196 AutomationController::toggled ()
197 {
198         ArdourButton* but = dynamic_cast<ArdourButton*>(_widget);
199         if (but) {
200                 const bool was_active = _controllable->get_value() >= 0.5;
201                 if (was_active && but->get_active()) {
202                         _adjustment->set_value(0.0);
203                         but->set_active(false);
204                 } else if (!was_active && !but->get_active()) {
205                         _adjustment->set_value(1.0);
206                         but->set_active(true);
207                 }
208         }
209 }
210
211 static double
212 midi_note_to_hz(int note)
213 {
214         const double tuning = 440.0;
215         return tuning * pow(2, (note - 69.0) / 12.0);
216 }
217
218 static double
219 clamp(double val, double min, double max)
220 {
221         if (val < min) {
222                 return min;
223         } else if (val > max) {
224                 return max;
225         }
226         return val;
227 }
228
229 void
230 AutomationController::run_note_select_dialog()
231 {
232         const ARDOUR::ParameterDescriptor& desc   = _controllable->desc();
233         NoteSelectDialog*                  dialog = new NoteSelectDialog();
234         if (dialog->run() == Gtk::RESPONSE_ACCEPT) {
235                 const double value = ((_controllable->desc().unit == ARDOUR::ParameterDescriptor::HZ)
236                                       ? midi_note_to_hz(dialog->note_number())
237                                       : dialog->note_number());
238                 _controllable->set_value(clamp(value, desc.lower, desc.upper));
239         }
240         delete dialog;
241 }
242
243 void
244 AutomationController::set_freq_beats(double beats)
245 {
246         const ARDOUR::ParameterDescriptor& desc    = _controllable->desc();
247         const ARDOUR::Session&             session = _controllable->session();
248         const framepos_t                   pos     = session.transport_frame();
249         const ARDOUR::Tempo&               tempo   = session.tempo_map().tempo_at(pos);
250         const double                       bpm     = tempo.beats_per_minute();
251         const double                       bps     = bpm / 60.0;
252         const double                       freq    = bps / beats;
253         _controllable->set_value(clamp(freq, desc.lower, desc.upper));
254 }
255
256 void
257 AutomationController::set_ratio(double ratio)
258 {
259         const ARDOUR::ParameterDescriptor& desc  = _controllable->desc();
260         const double                       value = _controllable->get_value() * ratio;
261         _controllable->set_value(clamp(value, desc.lower, desc.upper));
262 }
263
264 bool
265 AutomationController::on_button_release(GdkEventButton* ev)
266 {
267         using namespace Gtk::Menu_Helpers;
268
269         if (ev->button != 3) {
270                 return false;
271         }
272
273         const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
274         if (desc.unit == ARDOUR::ParameterDescriptor::MIDI_NOTE) {
275                 Gtk::Menu* menu  = manage(new Menu());
276                 MenuList&  items = menu->items();
277                 items.push_back(MenuElem(_("Select Note..."),
278                                          sigc::mem_fun(*this, &AutomationController::run_note_select_dialog)));
279                 menu->popup(1, ev->time);
280                 return true;
281         } else if (desc.unit == ARDOUR::ParameterDescriptor::HZ) {
282                 Gtk::Menu* menu  = manage(new Menu());
283                 MenuList&  items = menu->items();
284                 items.push_back(MenuElem(_("Halve"),
285                                          sigc::bind(sigc::mem_fun(*this, &AutomationController::set_ratio),
286                                                     0.5)));
287                 items.push_back(MenuElem(_("Double"),
288                                          sigc::bind(sigc::mem_fun(*this, &AutomationController::set_ratio),
289                                                     2.0)));
290                 const bool is_audible = desc.upper > 40.0;
291                 const bool is_low     = desc.lower < 1.0;
292                 if (is_audible) {
293                         items.push_back(MenuElem(_("Select Note..."),
294                                                  sigc::mem_fun(*this, &AutomationController::run_note_select_dialog)));
295                 }
296                 if (is_low) {
297                         for (int beats = 1; beats <= 16; ++beats) {
298                                 items.push_back(MenuElem (string_compose(P_("Set to %1 beat", "Set to %1 beats", beats), beats),
299                                                          sigc::bind(sigc::mem_fun(*this, &AutomationController::set_freq_beats),
300                                                                     (double)beats)));
301                         }
302                 }
303                 menu->popup(1, ev->time);
304                 return true;
305         }
306
307         return false;
308 }
309
310 void
311 AutomationController::value_changed ()
312 {
313         Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&AutomationController::display_effective_value, this));
314 }
315
316 /** Stop updating our value from our controllable */
317 void
318 AutomationController::stop_updating ()
319 {
320         _screen_update_connection.disconnect ();
321 }
322
323 void
324 AutomationController::disable_vertical_scroll ()
325 {
326         AutomationBarController* bar = dynamic_cast<AutomationBarController*>(_widget);
327         if (bar) {
328                 bar->set_tweaks (
329                         Gtkmm2ext::PixFader::Tweaks(bar->tweaks() |
330                                                     Gtkmm2ext::PixFader::NoVerticalScroll));
331         }
332 }