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_ui.h"
34 #include "automation_controller.h"
35 #include "gui_thread.h"
36 #include "note_select_dialog.h"
40 using namespace ARDOUR;
43 AutomationController::AutomationController(boost::shared_ptr<Automatable> printer,
44 boost::shared_ptr<AutomationControl> ac,
50 , _ignore_change(false)
55 ArdourButton* but = manage(new ArdourButton());
57 // Apply styles for special types
58 if (ac->parameter().type() == MuteAutomation) {
59 but->set_name("mute button");
60 } else if (ac->parameter().type() == SoloAutomation) {
61 but->set_name("solo button");
63 but->set_name("generic button");
65 but->signal_clicked.connect(
66 sigc::mem_fun(*this, &AutomationController::toggled));
70 Gtkmm2ext::BarController* bar = manage(new Gtkmm2ext::BarController(*adj, ac));
72 bar->set_name(X_("ProcessorControlSlider"));
73 bar->StartGesture.connect(
74 sigc::mem_fun(*this, &AutomationController::start_touch));
75 bar->StopGesture.connect(
76 sigc::mem_fun(*this, &AutomationController::end_touch));
77 bar->signal_button_release_event().connect(
78 sigc::mem_fun(*this, &AutomationController::on_button_release));
83 _adjustment->signal_value_changed().connect(
84 sigc::mem_fun(*this, &AutomationController::value_adjusted));
86 _screen_update_connection = ARDOUR_UI::RapidScreenUpdate.connect (
87 sigc::mem_fun (*this, &AutomationController::display_effective_value));
89 ac->Changed.connect (_changed_connection, invalidator (*this), boost::bind (&AutomationController::value_changed, this), gui_context());
95 AutomationController::~AutomationController()
99 boost::shared_ptr<AutomationController>
100 AutomationController::create(boost::shared_ptr<Automatable> printer,
101 const Evoral::Parameter& param,
102 const ParameterDescriptor& desc,
103 boost::shared_ptr<AutomationControl> ac)
105 const double lo = ac->internal_to_interface(desc.lower);
106 const double up = ac->internal_to_interface(desc.upper);
107 const double normal = ac->internal_to_interface(desc.normal);
108 double smallstep = desc.smallstep;
109 double largestep = desc.largestep;
110 if (smallstep == 0.0) {
111 smallstep = (up - lo) / 100;
113 if (largestep == 0.0) {
114 largestep = (up - lo) / 10;
116 smallstep = ac->internal_to_interface(smallstep);
117 largestep = ac->internal_to_interface(largestep);
119 Gtk::Adjustment* adjustment = manage (
120 new Gtk::Adjustment (normal, lo, up, smallstep, largestep));
123 assert(ac->parameter() == param);
124 return boost::shared_ptr<AutomationController>(new AutomationController(printer, ac, adjustment));
128 AutomationController::get_label (double& xpos)
131 return _printer->value_as_string (_controllable);
135 AutomationController::display_effective_value()
137 double const interface_value = _controllable->internal_to_interface(_controllable->get_value());
139 if (_adjustment->get_value () != interface_value) {
140 _ignore_change = true;
141 _adjustment->set_value (interface_value);
142 _ignore_change = false;
147 AutomationController::value_adjusted ()
149 if (!_ignore_change) {
150 _controllable->set_value (_controllable->interface_to_internal(_adjustment->get_value()));
152 /* A bar controller will automatically follow the adjustment, but for a
153 button we have to do it manually. */
154 ArdourButton* but = dynamic_cast<ArdourButton*>(_widget);
156 but->set_active(_adjustment->get_value() >= 0.5);
162 AutomationController::start_touch()
164 _controllable->start_touch (_controllable->session().transport_frame());
165 StartGesture.emit(); /* EMIT SIGNAL */
169 AutomationController::end_touch ()
171 if (_controllable->automation_state() == Touch) {
176 if (_controllable->session().transport_rolling()) {
178 when = _controllable->session().transport_frame();
181 _controllable->stop_touch (mark, when);
183 StopGesture.emit(); /* EMIT SIGNAL */
187 AutomationController::toggled ()
189 ArdourButton* but = dynamic_cast<ArdourButton*>(_widget);
191 const bool was_active = _controllable->get_value() >= 0.5;
193 _adjustment->set_value(0.0);
194 but->set_active(false);
196 _adjustment->set_value(1.0);
197 but->set_active(true);
203 midi_note_to_hz(int note)
205 const double tuning = 440.0;
206 return tuning * pow(2, (note - 69.0) / 12.0);
210 clamp(double val, double min, double max)
214 } else if (val > max) {
221 AutomationController::run_note_select_dialog()
223 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
224 NoteSelectDialog* dialog = new NoteSelectDialog();
225 if (dialog->run() == Gtk::RESPONSE_ACCEPT) {
226 const double value = ((_controllable->desc().unit == ARDOUR::ParameterDescriptor::HZ)
227 ? midi_note_to_hz(dialog->note_number())
228 : dialog->note_number());
229 _controllable->set_value(clamp(value, desc.lower, desc.upper));
235 AutomationController::set_freq_beats(double beats)
237 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
238 const ARDOUR::Session& session = _controllable->session();
239 const framepos_t pos = session.transport_frame();
240 const ARDOUR::Tempo& tempo = session.tempo_map().tempo_at(pos);
241 const double bpm = tempo.beats_per_minute();
242 const double bps = bpm / 60.0;
243 const double freq = bps / beats;
244 _controllable->set_value(clamp(freq, desc.lower, desc.upper));
248 AutomationController::set_ratio(double ratio)
250 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
251 const double value = _controllable->get_value() * ratio;
252 _controllable->set_value(clamp(value, desc.lower, desc.upper));
256 AutomationController::on_button_release(GdkEventButton* ev)
258 using namespace Gtk::Menu_Helpers;
260 if (ev->button != 3) {
264 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
265 if (desc.unit == ARDOUR::ParameterDescriptor::MIDI_NOTE) {
266 Gtk::Menu* menu = manage(new Menu());
267 MenuList& items = menu->items();
268 items.push_back(MenuElem(_("Select Note..."),
269 sigc::mem_fun(*this, &AutomationController::run_note_select_dialog)));
270 menu->popup(1, ev->time);
272 } else if (desc.unit == ARDOUR::ParameterDescriptor::HZ) {
273 Gtk::Menu* menu = manage(new Menu());
274 MenuList& items = menu->items();
275 items.push_back(MenuElem(_("Halve"),
276 sigc::bind(sigc::mem_fun(*this, &AutomationController::set_ratio),
278 items.push_back(MenuElem(_("Double"),
279 sigc::bind(sigc::mem_fun(*this, &AutomationController::set_ratio),
281 const bool is_audible = desc.upper > 40.0;
282 const bool is_low = desc.lower < 1.0;
284 items.push_back(MenuElem(_("Select Note..."),
285 sigc::mem_fun(*this, &AutomationController::run_note_select_dialog)));
288 for (double beats = 1.0; beats <= 16; ++beats) {
289 items.push_back(MenuElem(string_compose(_("Set to %1 beat(s)"), (int)beats),
290 sigc::bind(sigc::mem_fun(*this, &AutomationController::set_freq_beats),
294 menu->popup(1, ev->time);
302 AutomationController::value_changed ()
304 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&AutomationController::display_effective_value, this));
307 /** Stop updating our value from our controllable */
309 AutomationController::stop_updating ()
311 _screen_update_connection.disconnect ();
315 AutomationController::disable_vertical_scroll ()
317 Gtkmm2ext::BarController* bar = dynamic_cast<Gtkmm2ext::BarController*>(_widget);
320 Gtkmm2ext::PixFader::Tweaks(bar->tweaks() |
321 Gtkmm2ext::PixFader::NoVerticalScroll));