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