2 * Copyright (C) 2007-2014 David Robillard <d@drobilla.net>
3 * Copyright (C) 2008-2017 Paul Davis <paul@linuxaudiosystems.com>
4 * Copyright (C) 2009-2012 Carl Hetherington <carl@carlh.net>
5 * Copyright (C) 2014-2015 Tim Mayberry <mojofunk@gmail.com>
6 * Copyright (C) 2014-2019 Robin Gareus <robin@gareus.org>
7 * Copyright (C) 2014 Ben Loftis <ben@harrisonconsoles.com>
8 * Copyright (C) 2015-2016 Nick Mainsbridge <mainsbridge@gmail.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "pbd/compose.h"
29 #include "pbd/error.h"
31 #include "ardour/automatable.h"
32 #include "ardour/automation_control.h"
33 #include "ardour/session.h"
34 #include "ardour/tempo.h"
36 #include "widgets/ardour_button.h"
37 #include "widgets/ardour_knob.h"
39 #include "automation_controller.h"
40 #include "context_menu_helper.h"
41 #include "gui_thread.h"
42 #include "note_select_dialog.h"
47 using namespace ARDOUR;
49 using namespace ArdourWidgets;
51 using PBD::Controllable;
53 AutomationBarController::AutomationBarController (
54 boost::shared_ptr<AutomationControl> ac,
56 : ArdourWidgets::BarController (*adj, ac)
62 AutomationBarController::get_label (double& xpos)
65 return _controllable->get_user_string();
68 AutomationBarController::~AutomationBarController()
72 AutomationController::AutomationController(boost::shared_ptr<AutomationControl> ac,
78 , _ignore_change(false)
82 ArdourButton* but = manage(new ArdourButton());
84 // Apply styles for special types
85 if (ac->parameter().type() == MuteAutomation) {
86 but->set_name("mute button");
87 } else if (ac->parameter().type() == SoloAutomation) {
88 but->set_name("solo button");
90 but->set_name("generic button");
92 but->set_fallthrough_to_parent(true);
93 but->set_controllable(ac);
94 but->signal_button_press_event().connect(
95 sigc::mem_fun(*this, &AutomationController::button_press));
96 but->signal_button_release_event().connect(
97 sigc::mem_fun(*this, &AutomationController::button_release));
98 const bool active = _adjustment->get_value() >= 0.5;
99 if (but->get_active() != active) {
100 but->set_active(active);
103 } else if (use_knob) {
104 ArdourKnob* knob = manage (new ArdourKnob (ArdourKnob::default_elements, ArdourKnob::Detent));
105 knob->set_controllable (ac);
106 knob->set_name("processor control knob");
108 knob->StartGesture.connect(sigc::mem_fun(*this, &AutomationController::start_touch));
109 knob->StopGesture.connect(sigc::mem_fun(*this, &AutomationController::end_touch));
111 AutomationBarController* bar = manage(new AutomationBarController(ac, adj));
113 bar->set_name(X_("ProcessorControlSlider"));
114 bar->StartGesture.connect(
115 sigc::mem_fun(*this, &AutomationController::start_touch));
116 bar->StopGesture.connect(
117 sigc::mem_fun(*this, &AutomationController::end_touch));
118 bar->signal_button_release_event().connect(
119 sigc::mem_fun(*this, &AutomationController::on_button_release));
124 _adjustment->signal_value_changed().connect(
125 sigc::mem_fun(*this, &AutomationController::value_adjusted));
127 ac->Changed.connect (_changed_connections, invalidator (*this), boost::bind (&AutomationController::display_effective_value, this), gui_context());
128 display_effective_value ();
131 ac->alist()->automation_state_changed.connect (_changed_connections, invalidator (*this), boost::bind (&AutomationController::automation_state_changed, this), gui_context());
132 automation_state_changed ();
139 AutomationController::~AutomationController()
143 boost::shared_ptr<AutomationController>
144 AutomationController::create(const Evoral::Parameter& param,
145 const ParameterDescriptor& desc,
146 boost::shared_ptr<AutomationControl> ac,
149 const double lo = ac->internal_to_interface(desc.lower);
150 const double up = ac->internal_to_interface(desc.upper);
151 const double normal = ac->internal_to_interface(desc.normal);
152 const double smallstep = ac->internal_to_interface(desc.lower + desc.smallstep) - lo;
153 const double largestep = ac->internal_to_interface(desc.lower + desc.largestep) - lo;
155 Gtk::Adjustment* adjustment = manage (
156 new Gtk::Adjustment (normal, lo, up, smallstep, largestep));
159 assert(ac->parameter() == param);
160 return boost::shared_ptr<AutomationController>(new AutomationController(ac, adjustment, use_knob));
164 AutomationController::automation_state_changed ()
166 bool x = _controllable->alist()->automation_state() & Play;
167 _widget->set_sensitive (!x);
171 AutomationController::display_effective_value ()
173 double const interface_value = _controllable->internal_to_interface(_controllable->get_value());
176 /* we cannot use _controllable->touching() here
177 * because that's only set in Write or Touch mode.
178 * Besides ctrl-surfaces may also set touching()
182 if (_adjustment->get_value () != interface_value) {
183 _ignore_change = true;
184 _adjustment->set_value (interface_value);
185 _ignore_change = false;
191 AutomationController::value_adjusted ()
193 if (!_ignore_change) {
194 const double new_val = _controllable->interface_to_internal(_adjustment->get_value());
195 if (_controllable->user_double() != new_val) {
196 _controllable->set_value (new_val, Controllable::NoGroup);
200 /* A bar controller will automatically follow the adjustment, but for a
201 button we have to do it manually. */
202 ArdourButton* but = dynamic_cast<ArdourButton*>(_widget);
204 const bool active = _adjustment->get_value() >= 0.5;
205 if (but->get_active() != active) {
206 but->set_active(active);
212 AutomationController::start_touch()
215 _controllable->start_touch (_controllable->session().transport_sample());
219 AutomationController::end_touch ()
221 _controllable->stop_touch (_controllable->session().transport_sample());
224 display_effective_value ();
229 AutomationController::button_press (GdkEventButton*)
231 ArdourButton* but = dynamic_cast<ArdourButton*>(_widget);
234 _controllable->set_value (but->get_active () ? 0.0 : 1.0, Controllable::UseGroup);
240 AutomationController::button_release (GdkEventButton*)
247 midi_note_to_hz(int note)
249 const double tuning = 440.0;
250 return tuning * pow(2, (note - 69.0) / 12.0);
254 clamp(double val, double min, double max)
258 } else if (val > max) {
265 AutomationController::run_note_select_dialog()
267 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
268 NoteSelectDialog* dialog = new NoteSelectDialog();
269 if (dialog->run() == Gtk::RESPONSE_ACCEPT) {
270 const double value = ((_controllable->desc().unit == ARDOUR::ParameterDescriptor::HZ)
271 ? midi_note_to_hz(dialog->note_number())
272 : dialog->note_number());
273 _controllable->set_value(clamp(value, desc.lower, desc.upper), Controllable::NoGroup);
279 AutomationController::set_freq_beats(double beats)
281 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
282 const ARDOUR::Session& session = _controllable->session();
283 const samplepos_t pos = session.transport_sample();
284 const ARDOUR::Tempo& tempo = session.tempo_map().tempo_at_sample (pos);
285 const double bpm = tempo.note_types_per_minute();
286 const double bps = bpm / 60.0;
287 const double freq = bps / beats;
288 _controllable->set_value(clamp(freq, desc.lower, desc.upper), Controllable::NoGroup);
292 AutomationController::set_ratio(double ratio)
294 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
295 const double value = _controllable->get_value() * ratio;
296 _controllable->set_value(clamp(value, desc.lower, desc.upper), Controllable::NoGroup);
300 AutomationController::on_button_release(GdkEventButton* ev)
302 using namespace Gtk::Menu_Helpers;
304 if (ev->button != 3) {
308 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
309 if (desc.unit == ARDOUR::ParameterDescriptor::MIDI_NOTE) {
310 Gtk::Menu* menu = ARDOUR_UI_UTILS::shared_popup_menu ();
311 MenuList& items = menu->items();
312 items.push_back(MenuElem(_("Select Note..."),
313 sigc::mem_fun(*this, &AutomationController::run_note_select_dialog)));
314 menu->popup(1, ev->time);
316 } else if (desc.unit == ARDOUR::ParameterDescriptor::HZ) {
317 Gtk::Menu* menu = ARDOUR_UI_UTILS::shared_popup_menu ();
318 MenuList& items = menu->items();
319 items.push_back(MenuElem(_("Halve"),
320 sigc::bind(sigc::mem_fun(*this, &AutomationController::set_ratio),
322 items.push_back(MenuElem(_("Double"),
323 sigc::bind(sigc::mem_fun(*this, &AutomationController::set_ratio),
325 const bool is_audible = desc.upper > 40.0;
326 const bool is_low = desc.lower < 1.0;
328 items.push_back(MenuElem(_("Select Note..."),
329 sigc::mem_fun(*this, &AutomationController::run_note_select_dialog)));
332 for (int beats = 1; beats <= 16; ++beats) {
333 items.push_back(MenuElem (string_compose(P_("Set to %1 beat", "Set to %1 beats", beats), beats),
334 sigc::bind(sigc::mem_fun(*this, &AutomationController::set_freq_beats),
338 menu->popup(1, ev->time);
345 /** Stop updating our value from our controllable */
347 AutomationController::stop_updating ()
349 _screen_update_connection.disconnect ();
353 AutomationController::disable_vertical_scroll ()
355 AutomationBarController* bar = dynamic_cast<AutomationBarController*>(_widget);
358 ArdourWidgets::ArdourFader::Tweaks(bar->tweaks() | ArdourWidgets::ArdourFader::NoVerticalScroll));