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