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, true);
150 const double normal = ac->internal_to_interface(desc.normal, true);
151 const double smallstep = fabs (ac->internal_to_interface(desc.lower + desc.smallstep, true) - lo);
152 const double largestep = fabs (ac->internal_to_interface(desc.lower + desc.largestep, true) - lo);
154 /* even though internal_to_interface() may not generate the full range
155 * 0..1, the interface range is 0..1 by definition, so just hard code
159 Gtk::Adjustment* adjustment = manage (new Gtk::Adjustment (normal, 0.0, 1.0, smallstep, largestep));
162 assert(ac->parameter() == param);
163 return boost::shared_ptr<AutomationController>(new AutomationController(ac, adjustment, use_knob));
167 AutomationController::automation_state_changed ()
169 bool x = _controllable->alist()->automation_state() & Play;
170 _widget->set_sensitive (!x);
174 AutomationController::display_effective_value ()
176 double const interface_value = _controllable->internal_to_interface(_controllable->get_value(), true);
179 /* we cannot use _controllable->touching() here
180 * because that's only set in Write or Touch mode.
181 * Besides ctrl-surfaces may also set touching()
185 if (_adjustment->get_value () != interface_value) {
186 _ignore_change = true;
187 _adjustment->set_value (interface_value);
188 _ignore_change = false;
194 AutomationController::value_adjusted ()
196 if (!_ignore_change) {
197 const double new_val = _controllable->interface_to_internal(_adjustment->get_value(), true);
198 if (_controllable->user_double() != new_val) {
199 _controllable->set_value (new_val, Controllable::NoGroup);
203 /* A bar controller will automatically follow the adjustment, but for a
204 button we have to do it manually. */
205 ArdourButton* but = dynamic_cast<ArdourButton*>(_widget);
207 const bool active = _adjustment->get_value() >= 0.5;
208 if (but->get_active() != active) {
209 but->set_active(active);
215 AutomationController::start_touch()
218 _controllable->start_touch (_controllable->session().transport_sample());
222 AutomationController::end_touch ()
224 _controllable->stop_touch (_controllable->session().transport_sample());
227 display_effective_value ();
232 AutomationController::button_press (GdkEventButton*)
234 ArdourButton* but = dynamic_cast<ArdourButton*>(_widget);
237 _controllable->set_value (but->get_active () ? 0.0 : 1.0, Controllable::UseGroup);
243 AutomationController::button_release (GdkEventButton*)
250 midi_note_to_hz(int note)
252 const double tuning = 440.0;
253 return tuning * pow(2, (note - 69.0) / 12.0);
257 clamp(double val, double min, double max)
261 } else if (val > max) {
268 AutomationController::run_note_select_dialog()
270 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
271 NoteSelectDialog* dialog = new NoteSelectDialog();
272 if (dialog->run() == Gtk::RESPONSE_ACCEPT) {
273 const double value = ((_controllable->desc().unit == ARDOUR::ParameterDescriptor::HZ)
274 ? midi_note_to_hz(dialog->note_number())
275 : dialog->note_number());
276 _controllable->set_value(clamp(value, desc.lower, desc.upper), Controllable::NoGroup);
282 AutomationController::set_freq_beats(double beats)
284 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
285 const ARDOUR::Session& session = _controllable->session();
286 const samplepos_t pos = session.transport_sample();
287 const ARDOUR::Tempo& tempo = session.tempo_map().tempo_at_sample (pos);
288 const double bpm = tempo.note_types_per_minute();
289 const double bps = bpm / 60.0;
290 const double freq = bps / beats;
291 _controllable->set_value(clamp(freq, desc.lower, desc.upper), Controllable::NoGroup);
295 AutomationController::set_ratio(double ratio)
297 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
298 const double value = _controllable->get_value() * ratio;
299 _controllable->set_value(clamp(value, desc.lower, desc.upper), Controllable::NoGroup);
303 AutomationController::on_button_release(GdkEventButton* ev)
305 using namespace Gtk::Menu_Helpers;
307 if (ev->button != 3) {
311 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
312 if (desc.unit == ARDOUR::ParameterDescriptor::MIDI_NOTE) {
313 Gtk::Menu* menu = ARDOUR_UI_UTILS::shared_popup_menu ();
314 MenuList& items = menu->items();
315 items.push_back(MenuElem(_("Select Note..."),
316 sigc::mem_fun(*this, &AutomationController::run_note_select_dialog)));
317 menu->popup(1, ev->time);
319 } else if (desc.unit == ARDOUR::ParameterDescriptor::HZ) {
320 Gtk::Menu* menu = ARDOUR_UI_UTILS::shared_popup_menu ();
321 MenuList& items = menu->items();
322 items.push_back(MenuElem(_("Halve"),
323 sigc::bind(sigc::mem_fun(*this, &AutomationController::set_ratio),
325 items.push_back(MenuElem(_("Double"),
326 sigc::bind(sigc::mem_fun(*this, &AutomationController::set_ratio),
328 const bool is_audible = desc.upper > 40.0;
329 const bool is_low = desc.lower < 1.0;
331 items.push_back(MenuElem(_("Select Note..."),
332 sigc::mem_fun(*this, &AutomationController::run_note_select_dialog)));
335 for (int beats = 1; beats <= 16; ++beats) {
336 items.push_back(MenuElem (string_compose(P_("Set to %1 beat", "Set to %1 beats", beats), beats),
337 sigc::bind(sigc::mem_fun(*this, &AutomationController::set_freq_beats),
341 menu->popup(1, ev->time);
348 /** Stop updating our value from our controllable */
350 AutomationController::stop_updating ()
352 _screen_update_connection.disconnect ();
356 AutomationController::disable_vertical_scroll ()
358 AutomationBarController* bar = dynamic_cast<AutomationBarController*>(_widget);
361 ArdourWidgets::ArdourFader::Tweaks(bar->tweaks() | ArdourWidgets::ArdourFader::NoVerticalScroll));