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