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 "widgets/ardour_button.h"
33 #include "widgets/ardour_knob.h"
35 #include "automation_controller.h"
36 #include "context_menu_helper.h"
37 #include "gui_thread.h"
38 #include "note_select_dialog.h"
43 using namespace ARDOUR;
45 using namespace ArdourWidgets;
47 using PBD::Controllable;
49 AutomationBarController::AutomationBarController (
50 boost::shared_ptr<AutomationControl> ac,
52 : ArdourWidgets::BarController (*adj, ac)
58 AutomationBarController::get_label (double& xpos)
61 return _controllable->get_user_string();
64 AutomationBarController::~AutomationBarController()
68 AutomationController::AutomationController(boost::shared_ptr<AutomationControl> ac,
74 , _ignore_change(false)
78 ArdourButton* but = manage(new ArdourButton());
80 // Apply styles for special types
81 if (ac->parameter().type() == MuteAutomation) {
82 but->set_name("mute button");
83 } else if (ac->parameter().type() == SoloAutomation) {
84 but->set_name("solo button");
86 but->set_name("generic button");
88 but->set_fallthrough_to_parent(true);
89 but->set_controllable(ac);
90 but->signal_button_press_event().connect(
91 sigc::mem_fun(*this, &AutomationController::button_press));
92 but->signal_button_release_event().connect(
93 sigc::mem_fun(*this, &AutomationController::button_release));
94 const bool active = _adjustment->get_value() >= 0.5;
95 if (but->get_active() != active) {
96 but->set_active(active);
99 } else if (use_knob) {
100 ArdourKnob* knob = manage (new ArdourKnob (ArdourKnob::default_elements, ArdourKnob::Detent));
101 knob->set_controllable (ac);
102 knob->set_name("processor control knob");
104 knob->StartGesture.connect(sigc::mem_fun(*this, &AutomationController::start_touch));
105 knob->StopGesture.connect(sigc::mem_fun(*this, &AutomationController::end_touch));
107 AutomationBarController* bar = manage(new AutomationBarController(ac, adj));
109 bar->set_name(X_("ProcessorControlSlider"));
110 bar->StartGesture.connect(
111 sigc::mem_fun(*this, &AutomationController::start_touch));
112 bar->StopGesture.connect(
113 sigc::mem_fun(*this, &AutomationController::end_touch));
114 bar->signal_button_release_event().connect(
115 sigc::mem_fun(*this, &AutomationController::on_button_release));
120 _adjustment->signal_value_changed().connect(
121 sigc::mem_fun(*this, &AutomationController::value_adjusted));
123 ac->Changed.connect (_changed_connections, invalidator (*this), boost::bind (&AutomationController::display_effective_value, this), gui_context());
124 display_effective_value ();
127 ac->alist()->automation_state_changed.connect (_changed_connections, invalidator (*this), boost::bind (&AutomationController::automation_state_changed, this), gui_context());
128 automation_state_changed ();
135 AutomationController::~AutomationController()
139 boost::shared_ptr<AutomationController>
140 AutomationController::create(const Evoral::Parameter& param,
141 const ParameterDescriptor& desc,
142 boost::shared_ptr<AutomationControl> ac,
145 const double lo = ac->internal_to_interface(desc.lower);
146 const double up = ac->internal_to_interface(desc.upper);
147 const double normal = ac->internal_to_interface(desc.normal);
148 const double smallstep = ac->internal_to_interface(desc.lower + desc.smallstep) - lo;
149 const double largestep = ac->internal_to_interface(desc.lower + desc.largestep) - lo;
151 Gtk::Adjustment* adjustment = manage (
152 new Gtk::Adjustment (normal, lo, up, smallstep, largestep));
155 assert(ac->parameter() == param);
156 return boost::shared_ptr<AutomationController>(new AutomationController(ac, adjustment, use_knob));
160 AutomationController::automation_state_changed ()
162 bool x = _controllable->alist()->automation_state() & Play;
163 _widget->set_sensitive (!x);
167 AutomationController::display_effective_value ()
169 double const interface_value = _controllable->internal_to_interface(_controllable->get_value());
172 /* we cannot use _controllable->touching() here
173 * because that's only set in Write or Touch mode.
174 * Besides ctrl-surfaces may also set touching()
178 if (_adjustment->get_value () != interface_value) {
179 _ignore_change = true;
180 _adjustment->set_value (interface_value);
181 _ignore_change = false;
187 AutomationController::value_adjusted ()
189 if (!_ignore_change) {
190 const double new_val = _controllable->interface_to_internal(_adjustment->get_value());
191 if (_controllable->user_double() != new_val) {
192 _controllable->set_value (new_val, Controllable::NoGroup);
196 /* A bar controller will automatically follow the adjustment, but for a
197 button we have to do it manually. */
198 ArdourButton* but = dynamic_cast<ArdourButton*>(_widget);
200 const bool active = _adjustment->get_value() >= 0.5;
201 if (but->get_active() != active) {
202 but->set_active(active);
208 AutomationController::start_touch()
211 _controllable->start_touch (_controllable->session().transport_sample());
215 AutomationController::end_touch ()
217 _controllable->stop_touch (_controllable->session().transport_sample());
220 display_effective_value ();
225 AutomationController::button_press (GdkEventButton*)
227 ArdourButton* but = dynamic_cast<ArdourButton*>(_widget);
230 _controllable->set_value (but->get_active () ? 0.0 : 1.0, Controllable::UseGroup);
236 AutomationController::button_release (GdkEventButton*)
243 midi_note_to_hz(int note)
245 const double tuning = 440.0;
246 return tuning * pow(2, (note - 69.0) / 12.0);
250 clamp(double val, double min, double max)
254 } else if (val > max) {
261 AutomationController::run_note_select_dialog()
263 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
264 NoteSelectDialog* dialog = new NoteSelectDialog();
265 if (dialog->run() == Gtk::RESPONSE_ACCEPT) {
266 const double value = ((_controllable->desc().unit == ARDOUR::ParameterDescriptor::HZ)
267 ? midi_note_to_hz(dialog->note_number())
268 : dialog->note_number());
269 _controllable->set_value(clamp(value, desc.lower, desc.upper), Controllable::NoGroup);
275 AutomationController::set_freq_beats(double beats)
277 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
278 const ARDOUR::Session& session = _controllable->session();
279 const samplepos_t pos = session.transport_sample();
280 const ARDOUR::Tempo& tempo = session.tempo_map().tempo_at_sample (pos);
281 const double bpm = tempo.note_types_per_minute();
282 const double bps = bpm / 60.0;
283 const double freq = bps / beats;
284 _controllable->set_value(clamp(freq, desc.lower, desc.upper), Controllable::NoGroup);
288 AutomationController::set_ratio(double ratio)
290 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
291 const double value = _controllable->get_value() * ratio;
292 _controllable->set_value(clamp(value, desc.lower, desc.upper), Controllable::NoGroup);
296 AutomationController::on_button_release(GdkEventButton* ev)
298 using namespace Gtk::Menu_Helpers;
300 if (ev->button != 3) {
304 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
305 if (desc.unit == ARDOUR::ParameterDescriptor::MIDI_NOTE) {
306 Gtk::Menu* menu = ARDOUR_UI_UTILS::shared_popup_menu ();
307 MenuList& items = menu->items();
308 items.push_back(MenuElem(_("Select Note..."),
309 sigc::mem_fun(*this, &AutomationController::run_note_select_dialog)));
310 menu->popup(1, ev->time);
312 } else if (desc.unit == ARDOUR::ParameterDescriptor::HZ) {
313 Gtk::Menu* menu = ARDOUR_UI_UTILS::shared_popup_menu ();
314 MenuList& items = menu->items();
315 items.push_back(MenuElem(_("Halve"),
316 sigc::bind(sigc::mem_fun(*this, &AutomationController::set_ratio),
318 items.push_back(MenuElem(_("Double"),
319 sigc::bind(sigc::mem_fun(*this, &AutomationController::set_ratio),
321 const bool is_audible = desc.upper > 40.0;
322 const bool is_low = desc.lower < 1.0;
324 items.push_back(MenuElem(_("Select Note..."),
325 sigc::mem_fun(*this, &AutomationController::run_note_select_dialog)));
328 for (int beats = 1; beats <= 16; ++beats) {
329 items.push_back(MenuElem (string_compose(P_("Set to %1 beat", "Set to %1 beats", beats), beats),
330 sigc::bind(sigc::mem_fun(*this, &AutomationController::set_freq_beats),
334 menu->popup(1, ev->time);
341 /** Stop updating our value from our controllable */
343 AutomationController::stop_updating ()
345 _screen_update_connection.disconnect ();
349 AutomationController::disable_vertical_scroll ()
351 AutomationBarController* bar = dynamic_cast<AutomationBarController*>(_widget);
354 ArdourWidgets::ArdourFader::Tweaks(bar->tweaks() | ArdourWidgets::ArdourFader::NoVerticalScroll));