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"
34 #include "automation_controller.h"
35 #include "gui_thread.h"
36 #include "note_select_dialog.h"
41 using namespace ARDOUR;
43 using namespace ArdourWidgets;
45 using PBD::Controllable;
47 AutomationBarController::AutomationBarController (
48 boost::shared_ptr<AutomationControl> ac,
50 : ArdourWidgets::BarController (*adj, ac)
56 AutomationBarController::get_label (double& xpos)
59 return _controllable->get_user_string();
62 AutomationBarController::~AutomationBarController()
66 AutomationController::AutomationController(boost::shared_ptr<AutomationControl> ac,
72 , _ignore_change(false)
75 ArdourButton* but = manage(new ArdourButton());
77 // Apply styles for special types
78 if (ac->parameter().type() == MuteAutomation) {
79 but->set_name("mute button");
80 } else if (ac->parameter().type() == SoloAutomation) {
81 but->set_name("solo button");
83 but->set_name("generic button");
85 but->set_fallthrough_to_parent(true);
86 but->set_controllable(ac);
87 but->signal_button_press_event().connect(
88 sigc::mem_fun(*this, &AutomationController::button_press));
89 but->signal_button_release_event().connect(
90 sigc::mem_fun(*this, &AutomationController::button_release));
91 const bool active = _adjustment->get_value() >= 0.5;
92 if (but->get_active() != active) {
93 but->set_active(active);
96 } else if (use_knob) {
97 ArdourKnob* knob = manage (new ArdourKnob (ArdourKnob::default_elements, ArdourKnob::Detent));
98 knob->set_controllable (ac);
99 knob->set_name("processor control knob");
101 knob->StartGesture.connect(sigc::mem_fun(*this, &AutomationController::start_touch));
102 knob->StopGesture.connect(sigc::mem_fun(*this, &AutomationController::end_touch));
104 AutomationBarController* bar = manage(new AutomationBarController(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 ac->Changed.connect (_changed_connections, invalidator (*this), boost::bind (&AutomationController::display_effective_value, this), gui_context());
121 display_effective_value ();
124 ac->alist()->automation_state_changed.connect (_changed_connections, invalidator (*this), boost::bind (&AutomationController::automation_state_changed, this), gui_context());
125 automation_state_changed ();
132 AutomationController::~AutomationController()
136 boost::shared_ptr<AutomationController>
137 AutomationController::create(const Evoral::Parameter& param,
138 const ParameterDescriptor& desc,
139 boost::shared_ptr<AutomationControl> ac,
142 const double lo = ac->internal_to_interface(desc.lower);
143 const double up = ac->internal_to_interface(desc.upper);
144 const double normal = ac->internal_to_interface(desc.normal);
145 const double smallstep = ac->internal_to_interface(desc.lower + desc.smallstep) - lo;
146 const double largestep = ac->internal_to_interface(desc.lower + desc.largestep) - lo;
148 Gtk::Adjustment* adjustment = manage (
149 new Gtk::Adjustment (normal, lo, up, smallstep, largestep));
152 assert(ac->parameter() == param);
153 return boost::shared_ptr<AutomationController>(new AutomationController(ac, adjustment, use_knob));
157 AutomationController::automation_state_changed ()
159 bool x = _controllable->alist()->automation_state() & Play;
160 _widget->set_sensitive (!x);
164 AutomationController::display_effective_value ()
166 double const interface_value = _controllable->internal_to_interface(_controllable->get_value());
168 if (_adjustment->get_value () != interface_value) {
169 _ignore_change = true;
170 _adjustment->set_value (interface_value);
171 _ignore_change = false;
177 AutomationController::value_adjusted ()
179 if (!_ignore_change) {
180 const double new_val = _controllable->interface_to_internal(_adjustment->get_value());
181 if (_controllable->user_double() != new_val) {
182 _controllable->set_value (new_val, Controllable::NoGroup);
186 /* A bar controller will automatically follow the adjustment, but for a
187 button we have to do it manually. */
188 ArdourButton* but = dynamic_cast<ArdourButton*>(_widget);
190 const bool active = _adjustment->get_value() >= 0.5;
191 if (but->get_active() != active) {
192 but->set_active(active);
198 AutomationController::start_touch()
200 _controllable->start_touch (_controllable->session().transport_sample());
204 AutomationController::end_touch ()
206 _controllable->stop_touch (_controllable->session().transport_sample());
210 AutomationController::button_press (GdkEventButton*)
212 ArdourButton* but = dynamic_cast<ArdourButton*>(_widget);
215 _controllable->set_value (but->get_active () ? 0.0 : 1.0, Controllable::UseGroup);
221 AutomationController::button_release (GdkEventButton*)
228 midi_note_to_hz(int note)
230 const double tuning = 440.0;
231 return tuning * pow(2, (note - 69.0) / 12.0);
235 clamp(double val, double min, double max)
239 } else if (val > max) {
246 AutomationController::run_note_select_dialog()
248 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
249 NoteSelectDialog* dialog = new NoteSelectDialog();
250 if (dialog->run() == Gtk::RESPONSE_ACCEPT) {
251 const double value = ((_controllable->desc().unit == ARDOUR::ParameterDescriptor::HZ)
252 ? midi_note_to_hz(dialog->note_number())
253 : dialog->note_number());
254 _controllable->set_value(clamp(value, desc.lower, desc.upper), Controllable::NoGroup);
260 AutomationController::set_freq_beats(double beats)
262 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
263 const ARDOUR::Session& session = _controllable->session();
264 const samplepos_t pos = session.transport_sample();
265 const ARDOUR::Tempo& tempo = session.tempo_map().tempo_at_sample (pos);
266 const double bpm = tempo.note_types_per_minute();
267 const double bps = bpm / 60.0;
268 const double freq = bps / beats;
269 _controllable->set_value(clamp(freq, desc.lower, desc.upper), Controllable::NoGroup);
273 AutomationController::set_ratio(double ratio)
275 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
276 const double value = _controllable->get_value() * ratio;
277 _controllable->set_value(clamp(value, desc.lower, desc.upper), Controllable::NoGroup);
281 AutomationController::on_button_release(GdkEventButton* ev)
283 using namespace Gtk::Menu_Helpers;
285 if (ev->button != 3) {
289 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
290 if (desc.unit == ARDOUR::ParameterDescriptor::MIDI_NOTE) {
291 Gtk::Menu* menu = manage(new Menu());
292 MenuList& items = menu->items();
293 items.push_back(MenuElem(_("Select Note..."),
294 sigc::mem_fun(*this, &AutomationController::run_note_select_dialog)));
295 menu->popup(1, ev->time);
297 } else if (desc.unit == ARDOUR::ParameterDescriptor::HZ) {
298 Gtk::Menu* menu = manage(new Menu());
299 MenuList& items = menu->items();
300 items.push_back(MenuElem(_("Halve"),
301 sigc::bind(sigc::mem_fun(*this, &AutomationController::set_ratio),
303 items.push_back(MenuElem(_("Double"),
304 sigc::bind(sigc::mem_fun(*this, &AutomationController::set_ratio),
306 const bool is_audible = desc.upper > 40.0;
307 const bool is_low = desc.lower < 1.0;
309 items.push_back(MenuElem(_("Select Note..."),
310 sigc::mem_fun(*this, &AutomationController::run_note_select_dialog)));
313 for (int beats = 1; beats <= 16; ++beats) {
314 items.push_back(MenuElem (string_compose(P_("Set to %1 beat", "Set to %1 beats", beats), beats),
315 sigc::bind(sigc::mem_fun(*this, &AutomationController::set_freq_beats),
319 menu->popup(1, ev->time);
326 /** Stop updating our value from our controllable */
328 AutomationController::stop_updating ()
330 _screen_update_connection.disconnect ();
334 AutomationController::disable_vertical_scroll ()
336 AutomationBarController* bar = dynamic_cast<AutomationBarController*>(_widget);
339 ArdourWidgets::ArdourFader::Tweaks(bar->tweaks() | ArdourWidgets::ArdourFader::NoVerticalScroll));