Check whether frequency controls are audible or low and provide appropriate controls.
[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_ui.h"
33 #include "automation_controller.h"
34 #include "gui_thread.h"
35 #include "note_select_dialog.h"
36
37 #include "i18n.h"
38
39 using namespace ARDOUR;
40 using namespace Gtk;
41
42 AutomationController::AutomationController(boost::shared_ptr<Automatable>       printer,
43                                            boost::shared_ptr<AutomationControl> ac,
44                                            Adjustment*                          adj)
45         : BarController (*adj, ac)
46         , _ignore_change(false)
47         , _printer (printer)
48         , _controllable(ac)
49         , _adjustment(adj)
50 {
51         assert (_printer);
52
53         set_name (X_("ProcessorControlSlider"));
54
55         StartGesture.connect (sigc::mem_fun(*this, &AutomationController::start_touch));
56         StopGesture.connect (sigc::mem_fun(*this, &AutomationController::end_touch));
57
58         signal_button_release_event().connect(
59                 sigc::mem_fun(*this, &AutomationController::on_button_release));
60
61         _adjustment->signal_value_changed().connect (
62                         sigc::mem_fun(*this, &AutomationController::value_adjusted));
63
64         _screen_update_connection = ARDOUR_UI::RapidScreenUpdate.connect (
65                         sigc::mem_fun (*this, &AutomationController::display_effective_value));
66
67         ac->Changed.connect (_changed_connection, invalidator (*this), boost::bind (&AutomationController::value_changed, this), gui_context());
68 }
69
70 AutomationController::~AutomationController()
71 {
72 }
73
74 boost::shared_ptr<AutomationController>
75 AutomationController::create(boost::shared_ptr<Automatable>       printer,
76                              const Evoral::Parameter&             param,
77                              const ParameterDescriptor&           desc,
78                              boost::shared_ptr<AutomationControl> ac)
79 {
80         const double lo        = ac->internal_to_interface(desc.lower);
81         const double up        = ac->internal_to_interface(desc.upper);
82         const double normal    = ac->internal_to_interface(desc.normal);
83         double       smallstep = desc.smallstep;
84         double       largestep = desc.largestep;
85         if (smallstep == 0.0) {
86                 smallstep = (up - lo) / 100;
87         }
88         if (largestep == 0.0) {
89                 largestep = (up - lo) / 10;
90         }
91         smallstep = ac->internal_to_interface(smallstep);
92         largestep = ac->internal_to_interface(largestep);
93
94         Gtk::Adjustment* adjustment = manage (
95                 new Gtk::Adjustment (normal, lo, up, smallstep, largestep));
96
97         assert (ac);
98         assert(ac->parameter() == param);
99         return boost::shared_ptr<AutomationController>(new AutomationController(printer, ac, adjustment));
100 }
101
102 std::string
103 AutomationController::get_label (double& xpos)
104 {
105         xpos = 0.5;
106         return _printer->value_as_string (_controllable);
107 }
108
109 void
110 AutomationController::display_effective_value()
111 {
112         double const interface_value = _controllable->internal_to_interface(_controllable->get_value());
113
114         if (_adjustment->get_value () != interface_value) {
115                 _ignore_change = true;
116                 _adjustment->set_value (interface_value);
117                 _ignore_change = false;
118         }
119 }
120
121 void
122 AutomationController::value_adjusted ()
123 {
124         if (!_ignore_change) {
125                 _controllable->set_value (_controllable->interface_to_internal(_adjustment->get_value()));
126         }
127 }
128
129 void
130 AutomationController::start_touch()
131 {
132         _controllable->start_touch (_controllable->session().transport_frame());
133 }
134
135 void
136 AutomationController::end_touch ()
137 {
138         if (!_controllable->alist()) return;
139         if (_controllable->automation_state() == Touch) {
140
141                 bool mark = false;
142                 double when = 0;
143
144                 if (_controllable->session().transport_rolling()) {
145                         mark = true;
146                         when = _controllable->session().transport_frame();
147                 }
148
149                 _controllable->stop_touch (mark, when);
150         }
151 }
152
153 static double
154 midi_note_to_hz(int note)
155 {
156         const double tuning = 440.0;
157         return tuning * pow(2, (note - 69.0) / 12.0);
158 }
159
160 static double
161 clamp(double val, double min, double max)
162 {
163         if (val < min) {
164                 return min;
165         } else if (val > max) {
166                 return max;
167         }
168         return val;
169 }
170
171 void
172 AutomationController::run_note_select_dialog()
173 {
174         const ARDOUR::ParameterDescriptor& desc   = _controllable->desc();
175         NoteSelectDialog*                  dialog = new NoteSelectDialog();
176         if (dialog->run() == Gtk::RESPONSE_ACCEPT) {
177                 const double value = ((_controllable->desc().unit == ARDOUR::ParameterDescriptor::HZ)
178                                       ? midi_note_to_hz(dialog->note_number())
179                                       : dialog->note_number());
180                 _controllable->set_value(clamp(value, desc.lower, desc.upper));
181         }
182         delete dialog;
183 }
184
185 void
186 AutomationController::set_freq_beats(double beats)
187 {
188         const ARDOUR::ParameterDescriptor& desc    = _controllable->desc();
189         const ARDOUR::Session&             session = _controllable->session();
190         const framepos_t                   pos     = session.transport_frame();
191         const ARDOUR::Tempo&               tempo   = session.tempo_map().tempo_at(pos);
192         const double                       bpm     = tempo.beats_per_minute();
193         const double                       bps     = bpm / 60.0;
194         const double                       freq    = bps / beats;
195         _controllable->set_value(clamp(freq, desc.lower, desc.upper));
196 }
197
198 void
199 AutomationController::set_ratio(double ratio)
200 {
201         const ARDOUR::ParameterDescriptor& desc  = _controllable->desc();
202         const double                       value = _controllable->get_value() * ratio;
203         _controllable->set_value(clamp(value, desc.lower, desc.upper));
204 }
205
206 bool
207 AutomationController::on_button_release(GdkEventButton* ev)
208 {
209         using namespace Gtk::Menu_Helpers;
210
211         if (ev->button != 3) {
212                 return false;
213         }
214
215         const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
216         if (desc.unit == ARDOUR::ParameterDescriptor::MIDI_NOTE) {
217                 Gtk::Menu* menu  = manage(new Menu());
218                 MenuList&  items = menu->items();
219                 items.push_back(MenuElem(_("Select Note..."),
220                                          sigc::mem_fun(*this, &AutomationController::run_note_select_dialog)));
221                 menu->popup(1, ev->time);
222                 return true;
223         } else if (desc.unit == ARDOUR::ParameterDescriptor::HZ) {
224                 Gtk::Menu* menu  = manage(new Menu());
225                 MenuList&  items = menu->items();
226                 items.push_back(MenuElem(_("Halve"),
227                                          sigc::bind(sigc::mem_fun(*this, &AutomationController::set_ratio),
228                                                     0.5)));
229                 items.push_back(MenuElem(_("Double"),
230                                          sigc::bind(sigc::mem_fun(*this, &AutomationController::set_ratio),
231                                                     2.0)));
232                 const bool is_audible = desc.upper > 40.0;
233                 const bool is_low     = desc.lower < 1.0;
234                 if (is_audible) {
235                         items.push_back(MenuElem(_("Select Note..."),
236                                                  sigc::mem_fun(*this, &AutomationController::run_note_select_dialog)));
237                 }
238                 if (is_low) {
239                         for (double beats = 1.0; beats <= 16; ++beats) {
240                                 items.push_back(MenuElem(string_compose(_("Set to %1 beat(s)"), (int)beats),
241                                                          sigc::bind(sigc::mem_fun(*this, &AutomationController::set_freq_beats),
242                                                                     beats)));
243                         }
244                 }
245                 menu->popup(1, ev->time);
246                 return true;
247         }
248
249         return false;
250 }
251
252 void
253 AutomationController::value_changed ()
254 {
255         Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&AutomationController::display_effective_value, this));
256 }
257
258 /** Stop updating our value from our controllable */
259 void
260 AutomationController::stop_updating ()
261 {
262         _screen_update_connection.disconnect ();
263 }