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 "automation_controller.h"
34 #include "gui_thread.h"
35 #include "note_select_dialog.h"
40 using namespace ARDOUR;
43 using PBD::Controllable;
45 AutomationBarController::AutomationBarController (
46 boost::shared_ptr<Automatable> printer,
47 boost::shared_ptr<AutomationControl> ac,
49 : Gtkmm2ext::BarController(*adj, ac)
56 AutomationBarController::get_label (double& xpos)
59 return _printer->value_as_string (_controllable);
62 AutomationBarController::~AutomationBarController()
66 AutomationController::AutomationController(boost::shared_ptr<Automatable> printer,
67 boost::shared_ptr<AutomationControl> ac,
73 , _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_controllable(ac);
89 but->signal_clicked.connect(
90 sigc::mem_fun(*this, &AutomationController::toggled));
93 AutomationBarController* bar = manage(new AutomationBarController(_printer, ac, adj));
95 bar->set_name(X_("ProcessorControlSlider"));
96 bar->StartGesture.connect(
97 sigc::mem_fun(*this, &AutomationController::start_touch));
98 bar->StopGesture.connect(
99 sigc::mem_fun(*this, &AutomationController::end_touch));
100 bar->signal_button_release_event().connect(
101 sigc::mem_fun(*this, &AutomationController::on_button_release));
106 _adjustment->signal_value_changed().connect(
107 sigc::mem_fun(*this, &AutomationController::value_adjusted));
109 _screen_update_connection = Timers::rapid_connect (
110 sigc::mem_fun (*this, &AutomationController::display_effective_value));
112 ac->Changed.connect (_changed_connection, invalidator (*this), boost::bind (&AutomationController::value_changed, this), gui_context());
118 AutomationController::~AutomationController()
122 boost::shared_ptr<AutomationController>
123 AutomationController::create(boost::shared_ptr<Automatable> printer,
124 const Evoral::Parameter& param,
125 const ParameterDescriptor& desc,
126 boost::shared_ptr<AutomationControl> ac)
128 const double lo = ac->internal_to_interface(desc.lower);
129 const double up = ac->internal_to_interface(desc.upper);
130 const double normal = ac->internal_to_interface(desc.normal);
131 const double smallstep = ac->internal_to_interface(desc.lower + desc.smallstep);
132 const double largestep = ac->internal_to_interface(desc.lower + desc.largestep);
134 Gtk::Adjustment* adjustment = manage (
135 new Gtk::Adjustment (normal, lo, up, smallstep, largestep));
138 assert(ac->parameter() == param);
139 return boost::shared_ptr<AutomationController>(new AutomationController(printer, ac, adjustment));
143 AutomationController::display_effective_value()
145 double const interface_value = _controllable->internal_to_interface(_controllable->get_value());
147 if (_adjustment->get_value () != interface_value) {
148 _ignore_change = true;
149 _adjustment->set_value (interface_value);
150 _ignore_change = false;
155 AutomationController::value_adjusted ()
157 if (!_ignore_change) {
158 _controllable->set_value (_controllable->interface_to_internal(_adjustment->get_value()), Controllable::NoGroup);
161 /* A bar controller will automatically follow the adjustment, but for a
162 button we have to do it manually. */
163 ArdourButton* but = dynamic_cast<ArdourButton*>(_widget);
165 const bool active = _adjustment->get_value() >= 0.5;
166 if (but->get_active() != active) {
167 but->set_active(active);
173 AutomationController::start_touch()
175 _controllable->start_touch (_controllable->session().transport_frame());
176 StartGesture.emit(); /* EMIT SIGNAL */
180 AutomationController::end_touch ()
182 if (_controllable->automation_state() == Touch) {
187 if (_controllable->session().transport_rolling()) {
189 when = _controllable->session().transport_frame();
192 _controllable->stop_touch (mark, when);
194 StopGesture.emit(); /* EMIT SIGNAL */
198 AutomationController::toggled ()
200 ArdourButton* but = dynamic_cast<ArdourButton*>(_widget);
201 const AutoState as = _controllable->automation_state ();
202 const double where = _controllable->session ().audible_frame ();
203 const bool to_list = _controllable->list () && _controllable->session().transport_rolling () && (as == Touch || as == Write);
207 if (as == Touch && _controllable->list ()->in_new_write_pass ()) {
208 _controllable->alist ()->start_write_pass (where);
210 _controllable->list ()->set_in_write_pass (true, false, where);
212 /* set to opposite value.*/
213 _controllable->set_double (but->get_active () ? 0.0 : 1.0, where, to_list);
215 const bool active = _controllable->get_double (to_list, where) >= 0.5;
216 if (active && !but->get_active ()) {
217 _adjustment->set_value (1.0);
218 but->set_active (true);
219 } else if (!active && but->get_active ()) {
220 _adjustment->set_value (0.0);
221 but->set_active (false);
227 midi_note_to_hz(int note)
229 const double tuning = 440.0;
230 return tuning * pow(2, (note - 69.0) / 12.0);
234 clamp(double val, double min, double max)
238 } else if (val > max) {
245 AutomationController::run_note_select_dialog()
247 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
248 NoteSelectDialog* dialog = new NoteSelectDialog();
249 if (dialog->run() == Gtk::RESPONSE_ACCEPT) {
250 const double value = ((_controllable->desc().unit == ARDOUR::ParameterDescriptor::HZ)
251 ? midi_note_to_hz(dialog->note_number())
252 : dialog->note_number());
253 _controllable->set_value(clamp(value, desc.lower, desc.upper), Controllable::NoGroup);
259 AutomationController::set_freq_beats(double beats)
261 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
262 const ARDOUR::Session& session = _controllable->session();
263 const framepos_t pos = session.transport_frame();
264 const ARDOUR::Tempo& tempo = session.tempo_map().tempo_at(pos);
265 const double bpm = tempo.beats_per_minute();
266 const double bps = bpm / 60.0;
267 const double freq = bps / beats;
268 _controllable->set_value(clamp(freq, desc.lower, desc.upper), Controllable::NoGroup);
272 AutomationController::set_ratio(double ratio)
274 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
275 const double value = _controllable->get_value() * ratio;
276 _controllable->set_value(clamp(value, desc.lower, desc.upper), Controllable::NoGroup);
280 AutomationController::on_button_release(GdkEventButton* ev)
282 using namespace Gtk::Menu_Helpers;
284 if (ev->button != 3) {
288 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
289 if (desc.unit == ARDOUR::ParameterDescriptor::MIDI_NOTE) {
290 Gtk::Menu* menu = manage(new Menu());
291 MenuList& items = menu->items();
292 items.push_back(MenuElem(_("Select Note..."),
293 sigc::mem_fun(*this, &AutomationController::run_note_select_dialog)));
294 menu->popup(1, ev->time);
296 } else if (desc.unit == ARDOUR::ParameterDescriptor::HZ) {
297 Gtk::Menu* menu = manage(new Menu());
298 MenuList& items = menu->items();
299 items.push_back(MenuElem(_("Halve"),
300 sigc::bind(sigc::mem_fun(*this, &AutomationController::set_ratio),
302 items.push_back(MenuElem(_("Double"),
303 sigc::bind(sigc::mem_fun(*this, &AutomationController::set_ratio),
305 const bool is_audible = desc.upper > 40.0;
306 const bool is_low = desc.lower < 1.0;
308 items.push_back(MenuElem(_("Select Note..."),
309 sigc::mem_fun(*this, &AutomationController::run_note_select_dialog)));
312 for (int beats = 1; beats <= 16; ++beats) {
313 items.push_back(MenuElem (string_compose(P_("Set to %1 beat", "Set to %1 beats", beats), beats),
314 sigc::bind(sigc::mem_fun(*this, &AutomationController::set_freq_beats),
318 menu->popup(1, ev->time);
326 AutomationController::value_changed ()
328 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&AutomationController::display_effective_value, this));
331 /** Stop updating our value from our controllable */
333 AutomationController::stop_updating ()
335 _screen_update_connection.disconnect ();
339 AutomationController::disable_vertical_scroll ()
341 AutomationBarController* bar = dynamic_cast<AutomationBarController*>(_widget);
344 Gtkmm2ext::PixFader::Tweaks(bar->tweaks() |
345 Gtkmm2ext::PixFader::NoVerticalScroll));