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"
41 using namespace ARDOUR;
44 AutomationBarController::AutomationBarController (
45 boost::shared_ptr<Automatable> printer,
46 boost::shared_ptr<AutomationControl> ac,
48 : Gtkmm2ext::BarController(*adj, ac)
55 AutomationBarController::get_label (double& xpos)
58 return _printer->value_as_string (_controllable);
61 AutomationBarController::~AutomationBarController()
65 AutomationController::AutomationController(boost::shared_ptr<Automatable> printer,
66 boost::shared_ptr<AutomationControl> ac,
72 , _ignore_change(false)
77 ArdourButton* but = manage(new ArdourButton());
79 // Apply styles for special types
80 if (ac->parameter().type() == MuteAutomation) {
81 but->set_name("mute button");
82 } else if (ac->parameter().type() == SoloAutomation) {
83 but->set_name("solo button");
85 but->set_name("generic button");
87 but->set_controllable(ac);
88 but->signal_clicked.connect(
89 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()));
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);
202 if (_controllable->session().transport_rolling()) {
203 if (_controllable->automation_state() == Touch) {
204 _controllable->set_automation_state(Write);
206 if (_controllable->list()) {
207 _controllable->list()->set_in_write_pass(true, false, _controllable->session().audible_frame());
210 const bool was_active = _controllable->get_value() >= 0.5;
211 if (was_active && but->get_active()) {
212 _adjustment->set_value(0.0);
213 but->set_active(false);
214 } else if (!was_active && !but->get_active()) {
215 _adjustment->set_value(1.0);
216 but->set_active(true);
222 midi_note_to_hz(int note)
224 const double tuning = 440.0;
225 return tuning * pow(2, (note - 69.0) / 12.0);
229 clamp(double val, double min, double max)
233 } else if (val > max) {
240 AutomationController::run_note_select_dialog()
242 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
243 NoteSelectDialog* dialog = new NoteSelectDialog();
244 if (dialog->run() == Gtk::RESPONSE_ACCEPT) {
245 const double value = ((_controllable->desc().unit == ARDOUR::ParameterDescriptor::HZ)
246 ? midi_note_to_hz(dialog->note_number())
247 : dialog->note_number());
248 _controllable->set_value(clamp(value, desc.lower, desc.upper));
254 AutomationController::set_freq_beats(double beats)
256 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
257 const ARDOUR::Session& session = _controllable->session();
258 const framepos_t pos = session.transport_frame();
259 const ARDOUR::Tempo& tempo = session.tempo_map().tempo_at(pos);
260 const double bpm = tempo.beats_per_minute();
261 const double bps = bpm / 60.0;
262 const double freq = bps / beats;
263 _controllable->set_value(clamp(freq, desc.lower, desc.upper));
267 AutomationController::set_ratio(double ratio)
269 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
270 const double value = _controllable->get_value() * ratio;
271 _controllable->set_value(clamp(value, desc.lower, desc.upper));
275 AutomationController::on_button_release(GdkEventButton* ev)
277 using namespace Gtk::Menu_Helpers;
279 if (ev->button != 3) {
283 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
284 if (desc.unit == ARDOUR::ParameterDescriptor::MIDI_NOTE) {
285 Gtk::Menu* menu = manage(new Menu());
286 MenuList& items = menu->items();
287 items.push_back(MenuElem(_("Select Note..."),
288 sigc::mem_fun(*this, &AutomationController::run_note_select_dialog)));
289 menu->popup(1, ev->time);
291 } else if (desc.unit == ARDOUR::ParameterDescriptor::HZ) {
292 Gtk::Menu* menu = manage(new Menu());
293 MenuList& items = menu->items();
294 items.push_back(MenuElem(_("Halve"),
295 sigc::bind(sigc::mem_fun(*this, &AutomationController::set_ratio),
297 items.push_back(MenuElem(_("Double"),
298 sigc::bind(sigc::mem_fun(*this, &AutomationController::set_ratio),
300 const bool is_audible = desc.upper > 40.0;
301 const bool is_low = desc.lower < 1.0;
303 items.push_back(MenuElem(_("Select Note..."),
304 sigc::mem_fun(*this, &AutomationController::run_note_select_dialog)));
307 for (int beats = 1; beats <= 16; ++beats) {
308 items.push_back(MenuElem (string_compose(P_("Set to %1 beat", "Set to %1 beats", beats), beats),
309 sigc::bind(sigc::mem_fun(*this, &AutomationController::set_freq_beats),
313 menu->popup(1, ev->time);
321 AutomationController::value_changed ()
323 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&AutomationController::display_effective_value, this));
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 Gtkmm2ext::PixFader::Tweaks(bar->tweaks() |
340 Gtkmm2ext::PixFader::NoVerticalScroll));