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_knob.h"
34 #include "automation_controller.h"
35 #include "gui_thread.h"
36 #include "note_select_dialog.h"
41 using namespace ARDOUR;
44 using PBD::Controllable;
46 AutomationBarController::AutomationBarController (
47 boost::shared_ptr<Automatable> printer,
48 boost::shared_ptr<AutomationControl> ac,
50 : Gtkmm2ext::BarController(*adj, ac)
57 AutomationBarController::get_label (double& xpos)
60 return _printer->value_as_string (_controllable);
63 AutomationBarController::~AutomationBarController()
67 AutomationController::AutomationController(boost::shared_ptr<Automatable> printer,
68 boost::shared_ptr<AutomationControl> ac,
75 , _ignore_change(false)
80 ArdourButton* but = manage(new ArdourButton());
82 // Apply styles for special types
83 if (ac->parameter().type() == MuteAutomation) {
84 but->set_name("mute button");
85 } else if (ac->parameter().type() == SoloAutomation) {
86 but->set_name("solo button");
88 but->set_name("generic button");
90 but->set_controllable(ac);
91 but->signal_clicked.connect(
92 sigc::mem_fun(*this, &AutomationController::toggled));
94 } else if (use_knob) {
95 ArdourKnob* knob = manage (new ArdourKnob (ArdourKnob::default_elements, ArdourKnob::Detent));
96 knob->set_controllable (ac);
97 knob->set_name("processor control knob");
100 AutomationBarController* bar = manage(new AutomationBarController(_printer, ac, adj));
102 bar->set_name(X_("ProcessorControlSlider"));
103 bar->StartGesture.connect(
104 sigc::mem_fun(*this, &AutomationController::start_touch));
105 bar->StopGesture.connect(
106 sigc::mem_fun(*this, &AutomationController::end_touch));
107 bar->signal_button_release_event().connect(
108 sigc::mem_fun(*this, &AutomationController::on_button_release));
113 _adjustment->signal_value_changed().connect(
114 sigc::mem_fun(*this, &AutomationController::value_adjusted));
116 _screen_update_connection = Timers::rapid_connect (
117 sigc::mem_fun (*this, &AutomationController::display_effective_value));
119 ac->Changed.connect (_changed_connection, invalidator (*this), boost::bind (&AutomationController::value_changed, this), gui_context());
125 AutomationController::~AutomationController()
129 boost::shared_ptr<AutomationController>
130 AutomationController::create(boost::shared_ptr<Automatable> printer,
131 const Evoral::Parameter& param,
132 const ParameterDescriptor& desc,
133 boost::shared_ptr<AutomationControl> ac,
136 const double lo = ac->internal_to_interface(desc.lower);
137 const double up = ac->internal_to_interface(desc.upper);
138 const double normal = ac->internal_to_interface(desc.normal);
139 const double smallstep = ac->internal_to_interface(desc.lower + desc.smallstep);
140 const double largestep = ac->internal_to_interface(desc.lower + desc.largestep);
142 Gtk::Adjustment* adjustment = manage (
143 new Gtk::Adjustment (normal, lo, up, smallstep, largestep));
146 assert(ac->parameter() == param);
147 return boost::shared_ptr<AutomationController>(new AutomationController(printer, ac, adjustment, use_knob));
151 AutomationController::display_effective_value()
153 double const interface_value = _controllable->internal_to_interface(_controllable->get_value());
155 if (_adjustment->get_value () != interface_value) {
156 _ignore_change = true;
157 _adjustment->set_value (interface_value);
158 _ignore_change = false;
163 AutomationController::value_adjusted ()
165 if (!_ignore_change) {
166 _controllable->set_value (_controllable->interface_to_internal(_adjustment->get_value()), Controllable::NoGroup);
169 /* A bar controller will automatically follow the adjustment, but for a
170 button we have to do it manually. */
171 ArdourButton* but = dynamic_cast<ArdourButton*>(_widget);
173 const bool active = _adjustment->get_value() >= 0.5;
174 if (but->get_active() != active) {
175 but->set_active(active);
181 AutomationController::start_touch()
183 _controllable->start_touch (_controllable->session().transport_frame());
187 AutomationController::end_touch ()
189 if (_controllable->automation_state() == Touch) {
194 if (_controllable->session().transport_rolling()) {
196 when = _controllable->session().transport_frame();
199 _controllable->stop_touch (mark, when);
201 _controllable->stop_touch (false, _controllable->session().transport_frame());
206 AutomationController::toggled ()
208 ArdourButton* but = dynamic_cast<ArdourButton*>(_widget);
209 const AutoState as = _controllable->automation_state ();
210 const double where = _controllable->session ().audible_frame ();
211 const bool to_list = _controllable->list () && _controllable->session().transport_rolling () && (as == Touch || as == Write);
215 if (as == Touch && _controllable->list ()->in_new_write_pass ()) {
216 _controllable->alist ()->start_write_pass (where);
218 _controllable->list ()->set_in_write_pass (true, false, where);
220 /* set to opposite value.*/
221 _controllable->set_double (but->get_active () ? 0.0 : 1.0, where, to_list);
223 const bool active = _controllable->get_double (to_list, where) >= 0.5;
224 if (active && !but->get_active ()) {
225 _adjustment->set_value (1.0);
226 but->set_active (true);
227 } else if (!active && but->get_active ()) {
228 _adjustment->set_value (0.0);
229 but->set_active (false);
235 midi_note_to_hz(int note)
237 const double tuning = 440.0;
238 return tuning * pow(2, (note - 69.0) / 12.0);
242 clamp(double val, double min, double max)
246 } else if (val > max) {
253 AutomationController::run_note_select_dialog()
255 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
256 NoteSelectDialog* dialog = new NoteSelectDialog();
257 if (dialog->run() == Gtk::RESPONSE_ACCEPT) {
258 const double value = ((_controllable->desc().unit == ARDOUR::ParameterDescriptor::HZ)
259 ? midi_note_to_hz(dialog->note_number())
260 : dialog->note_number());
261 _controllable->set_value(clamp(value, desc.lower, desc.upper), Controllable::NoGroup);
267 AutomationController::set_freq_beats(double beats)
269 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
270 const ARDOUR::Session& session = _controllable->session();
271 const framepos_t pos = session.transport_frame();
272 const ARDOUR::Tempo& tempo = session.tempo_map().tempo_at_frame (pos);
273 const double bpm = tempo.beats_per_minute();
274 const double bps = bpm / 60.0;
275 const double freq = bps / beats;
276 _controllable->set_value(clamp(freq, desc.lower, desc.upper), Controllable::NoGroup);
280 AutomationController::set_ratio(double ratio)
282 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
283 const double value = _controllable->get_value() * ratio;
284 _controllable->set_value(clamp(value, desc.lower, desc.upper), Controllable::NoGroup);
288 AutomationController::on_button_release(GdkEventButton* ev)
290 using namespace Gtk::Menu_Helpers;
292 if (ev->button != 3) {
296 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
297 if (desc.unit == ARDOUR::ParameterDescriptor::MIDI_NOTE) {
298 Gtk::Menu* menu = manage(new Menu());
299 MenuList& items = menu->items();
300 items.push_back(MenuElem(_("Select Note..."),
301 sigc::mem_fun(*this, &AutomationController::run_note_select_dialog)));
302 menu->popup(1, ev->time);
304 } else if (desc.unit == ARDOUR::ParameterDescriptor::HZ) {
305 Gtk::Menu* menu = manage(new Menu());
306 MenuList& items = menu->items();
307 items.push_back(MenuElem(_("Halve"),
308 sigc::bind(sigc::mem_fun(*this, &AutomationController::set_ratio),
310 items.push_back(MenuElem(_("Double"),
311 sigc::bind(sigc::mem_fun(*this, &AutomationController::set_ratio),
313 const bool is_audible = desc.upper > 40.0;
314 const bool is_low = desc.lower < 1.0;
316 items.push_back(MenuElem(_("Select Note..."),
317 sigc::mem_fun(*this, &AutomationController::run_note_select_dialog)));
320 for (int beats = 1; beats <= 16; ++beats) {
321 items.push_back(MenuElem (string_compose(P_("Set to %1 beat", "Set to %1 beats", beats), beats),
322 sigc::bind(sigc::mem_fun(*this, &AutomationController::set_freq_beats),
326 menu->popup(1, ev->time);
334 AutomationController::value_changed ()
336 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&AutomationController::display_effective_value, this));
339 /** Stop updating our value from our controllable */
341 AutomationController::stop_updating ()
343 _screen_update_connection.disconnect ();
347 AutomationController::disable_vertical_scroll ()
349 AutomationBarController* bar = dynamic_cast<AutomationBarController*>(_widget);
352 Gtkmm2ext::PixFader::Tweaks(bar->tweaks() |
353 Gtkmm2ext::PixFader::NoVerticalScroll));