2 Copyright (C) 2007 Paul Davis
3 Author: David Robillard
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.
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.
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.
24 #include "pbd/compose.h"
25 #include "pbd/error.h"
27 #include "ardour/automatable.h"
28 #include "ardour/automation_control.h"
29 #include "ardour/session.h"
30 #include "ardour/tempo.h"
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"
41 using namespace ARDOUR;
44 using PBD::Controllable;
46 AutomationBarController::AutomationBarController (
47 boost::shared_ptr<Automatable> printer,
48 boost::shared_ptr<AutomationControl> ac,
50 : Gtkmm2ext::BarController(*adj, ac)
57 AutomationBarController::get_label (double& xpos)
60 return _printer->value_as_string (_controllable);
63 AutomationBarController::~AutomationBarController()
67 AutomationController::AutomationController(boost::shared_ptr<Automatable> printer,
68 boost::shared_ptr<AutomationControl> ac,
75 , _ignore_change(false)
80 ArdourButton* but = manage(new ArdourButton());
82 // Apply styles for special types
83 if (ac->parameter().type() == MuteAutomation) {
84 but->set_name("mute button");
85 } else if (ac->parameter().type() == SoloAutomation) {
86 but->set_name("solo button");
88 but->set_name("generic button");
90 but->set_controllable(ac);
91 but->signal_clicked.connect(
92 sigc::mem_fun(*this, &AutomationController::toggled));
93 const bool active = _adjustment->get_value() >= 0.5;
94 if (but->get_active() != active) {
95 but->set_active(active);
98 } else if (use_knob) {
99 ArdourKnob* knob = manage (new ArdourKnob (ArdourKnob::default_elements, ArdourKnob::Detent));
100 knob->set_controllable (ac);
101 knob->set_name("processor control knob");
104 AutomationBarController* bar = manage(new AutomationBarController(_printer, ac, adj));
106 bar->set_name(X_("ProcessorControlSlider"));
107 bar->StartGesture.connect(
108 sigc::mem_fun(*this, &AutomationController::start_touch));
109 bar->StopGesture.connect(
110 sigc::mem_fun(*this, &AutomationController::end_touch));
111 bar->signal_button_release_event().connect(
112 sigc::mem_fun(*this, &AutomationController::on_button_release));
117 _adjustment->signal_value_changed().connect(
118 sigc::mem_fun(*this, &AutomationController::value_adjusted));
120 _screen_update_connection = Timers::rapid_connect (
121 sigc::mem_fun (*this, &AutomationController::display_effective_value));
123 ac->Changed.connect (_changed_connection, invalidator (*this), boost::bind (&AutomationController::value_changed, this), gui_context());
129 AutomationController::~AutomationController()
133 boost::shared_ptr<AutomationController>
134 AutomationController::create(boost::shared_ptr<Automatable> printer,
135 const Evoral::Parameter& param,
136 const ParameterDescriptor& desc,
137 boost::shared_ptr<AutomationControl> ac,
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);
146 Gtk::Adjustment* adjustment = manage (
147 new Gtk::Adjustment (normal, lo, up, smallstep, largestep));
150 assert(ac->parameter() == param);
151 return boost::shared_ptr<AutomationController>(new AutomationController(printer, ac, adjustment, use_knob));
155 AutomationController::display_effective_value()
157 double const interface_value = _controllable->internal_to_interface(_controllable->get_value());
159 if (_adjustment->get_value () != interface_value) {
160 _ignore_change = true;
161 _adjustment->set_value (interface_value);
162 _ignore_change = false;
167 AutomationController::value_adjusted ()
169 if (!_ignore_change) {
170 _controllable->set_value (_controllable->interface_to_internal(_adjustment->get_value()), Controllable::NoGroup);
173 /* A bar controller will automatically follow the adjustment, but for a
174 button we have to do it manually. */
175 ArdourButton* but = dynamic_cast<ArdourButton*>(_widget);
177 const bool active = _adjustment->get_value() >= 0.5;
178 if (but->get_active() != active) {
179 but->set_active(active);
185 AutomationController::start_touch()
187 _controllable->start_touch (_controllable->session().transport_frame());
191 AutomationController::end_touch ()
193 if (_controllable->automation_state() == Touch) {
198 if (_controllable->session().transport_rolling()) {
200 when = _controllable->session().transport_frame();
203 _controllable->stop_touch (mark, when);
205 _controllable->stop_touch (false, _controllable->session().transport_frame());
210 AutomationController::toggled ()
212 ArdourButton* but = dynamic_cast<ArdourButton*>(_widget);
213 const AutoState as = _controllable->automation_state ();
214 const double where = _controllable->session ().audible_frame ();
215 const bool to_list = _controllable->list () && _controllable->session().transport_rolling () && (as == Touch || as == Write);
219 if (as == Touch && _controllable->list ()->in_new_write_pass ()) {
220 _controllable->alist ()->start_write_pass (where);
222 _controllable->list ()->set_in_write_pass (true, false, where);
224 /* set to opposite value.*/
225 _controllable->set_double (but->get_active () ? 0.0 : 1.0, where, to_list);
227 const bool active = _controllable->get_double (to_list, where) >= 0.5;
228 if (active && !but->get_active ()) {
229 _adjustment->set_value (1.0);
230 but->set_active (true);
231 } else if (!active && but->get_active ()) {
232 _adjustment->set_value (0.0);
233 but->set_active (false);
239 midi_note_to_hz(int note)
241 const double tuning = 440.0;
242 return tuning * pow(2, (note - 69.0) / 12.0);
246 clamp(double val, double min, double max)
250 } else if (val > max) {
257 AutomationController::run_note_select_dialog()
259 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
260 NoteSelectDialog* dialog = new NoteSelectDialog();
261 if (dialog->run() == Gtk::RESPONSE_ACCEPT) {
262 const double value = ((_controllable->desc().unit == ARDOUR::ParameterDescriptor::HZ)
263 ? midi_note_to_hz(dialog->note_number())
264 : dialog->note_number());
265 _controllable->set_value(clamp(value, desc.lower, desc.upper), Controllable::NoGroup);
271 AutomationController::set_freq_beats(double beats)
273 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
274 const ARDOUR::Session& session = _controllable->session();
275 const framepos_t pos = session.transport_frame();
276 const ARDOUR::Tempo& tempo = session.tempo_map().tempo_at_frame (pos);
277 const double bpm = tempo.beats_per_minute();
278 const double bps = bpm / 60.0;
279 const double freq = bps / beats;
280 _controllable->set_value(clamp(freq, desc.lower, desc.upper), Controllable::NoGroup);
284 AutomationController::set_ratio(double ratio)
286 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
287 const double value = _controllable->get_value() * ratio;
288 _controllable->set_value(clamp(value, desc.lower, desc.upper), Controllable::NoGroup);
292 AutomationController::on_button_release(GdkEventButton* ev)
294 using namespace Gtk::Menu_Helpers;
296 if (ev->button != 3) {
300 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
301 if (desc.unit == ARDOUR::ParameterDescriptor::MIDI_NOTE) {
302 Gtk::Menu* menu = manage(new Menu());
303 MenuList& items = menu->items();
304 items.push_back(MenuElem(_("Select Note..."),
305 sigc::mem_fun(*this, &AutomationController::run_note_select_dialog)));
306 menu->popup(1, ev->time);
308 } else if (desc.unit == ARDOUR::ParameterDescriptor::HZ) {
309 Gtk::Menu* menu = manage(new Menu());
310 MenuList& items = menu->items();
311 items.push_back(MenuElem(_("Halve"),
312 sigc::bind(sigc::mem_fun(*this, &AutomationController::set_ratio),
314 items.push_back(MenuElem(_("Double"),
315 sigc::bind(sigc::mem_fun(*this, &AutomationController::set_ratio),
317 const bool is_audible = desc.upper > 40.0;
318 const bool is_low = desc.lower < 1.0;
320 items.push_back(MenuElem(_("Select Note..."),
321 sigc::mem_fun(*this, &AutomationController::run_note_select_dialog)));
324 for (int beats = 1; beats <= 16; ++beats) {
325 items.push_back(MenuElem (string_compose(P_("Set to %1 beat", "Set to %1 beats", beats), beats),
326 sigc::bind(sigc::mem_fun(*this, &AutomationController::set_freq_beats),
330 menu->popup(1, ev->time);
338 AutomationController::value_changed ()
340 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&AutomationController::display_effective_value, this));
343 /** Stop updating our value from our controllable */
345 AutomationController::stop_updating ()
347 _screen_update_connection.disconnect ();
351 AutomationController::disable_vertical_scroll ()
353 AutomationBarController* bar = dynamic_cast<AutomationBarController*>(_widget);
356 Gtkmm2ext::PixFader::Tweaks(bar->tweaks() |
357 Gtkmm2ext::PixFader::NoVerticalScroll));