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->signal_clicked.connect(
88 sigc::mem_fun(*this, &AutomationController::toggled));
92 AutomationBarController* bar = manage(new AutomationBarController(_printer, ac, adj));
94 bar->set_name(X_("ProcessorControlSlider"));
95 bar->StartGesture.connect(
96 sigc::mem_fun(*this, &AutomationController::start_touch));
97 bar->StopGesture.connect(
98 sigc::mem_fun(*this, &AutomationController::end_touch));
99 bar->signal_button_release_event().connect(
100 sigc::mem_fun(*this, &AutomationController::on_button_release));
105 _adjustment->signal_value_changed().connect(
106 sigc::mem_fun(*this, &AutomationController::value_adjusted));
108 _screen_update_connection = Timers::rapid_connect (
109 sigc::mem_fun (*this, &AutomationController::display_effective_value));
111 ac->Changed.connect (_changed_connection, invalidator (*this), boost::bind (&AutomationController::value_changed, this), gui_context());
117 AutomationController::~AutomationController()
121 boost::shared_ptr<AutomationController>
122 AutomationController::create(boost::shared_ptr<Automatable> printer,
123 const Evoral::Parameter& param,
124 const ParameterDescriptor& desc,
125 boost::shared_ptr<AutomationControl> ac)
127 const double lo = ac->internal_to_interface(desc.lower);
128 const double up = ac->internal_to_interface(desc.upper);
129 const double normal = ac->internal_to_interface(desc.normal);
130 const double smallstep = ac->internal_to_interface(desc.lower + desc.smallstep);
131 const double largestep = ac->internal_to_interface(desc.lower + desc.largestep);
133 Gtk::Adjustment* adjustment = manage (
134 new Gtk::Adjustment (normal, lo, up, smallstep, largestep));
137 assert(ac->parameter() == param);
138 return boost::shared_ptr<AutomationController>(new AutomationController(printer, ac, adjustment));
142 AutomationController::display_effective_value()
144 double const interface_value = _controllable->internal_to_interface(_controllable->get_value());
146 if (_adjustment->get_value () != interface_value) {
147 _ignore_change = true;
148 _adjustment->set_value (interface_value);
149 _ignore_change = false;
154 AutomationController::value_adjusted ()
156 if (!_ignore_change) {
157 _controllable->set_value (_controllable->interface_to_internal(_adjustment->get_value()));
160 /* A bar controller will automatically follow the adjustment, but for a
161 button we have to do it manually. */
162 ArdourButton* but = dynamic_cast<ArdourButton*>(_widget);
164 const bool active = _adjustment->get_value() >= 0.5;
165 if (but->get_active() != active) {
166 but->set_active(active);
172 AutomationController::start_touch()
174 _controllable->start_touch (_controllable->session().transport_frame());
175 StartGesture.emit(); /* EMIT SIGNAL */
179 AutomationController::end_touch ()
181 if (_controllable->automation_state() == Touch) {
186 if (_controllable->session().transport_rolling()) {
188 when = _controllable->session().transport_frame();
191 _controllable->stop_touch (mark, when);
193 StopGesture.emit(); /* EMIT SIGNAL */
197 AutomationController::toggled ()
199 ArdourButton* but = dynamic_cast<ArdourButton*>(_widget);
201 if (_controllable->session().transport_rolling()) {
202 if (_controllable->automation_state() == Touch) {
203 _controllable->set_automation_state(Write);
205 if (_controllable->list()) {
206 _controllable->list()->set_in_write_pass(true, false, _controllable->session().audible_frame());
209 const bool was_active = _controllable->get_value() >= 0.5;
210 if (was_active && but->get_active()) {
211 _adjustment->set_value(0.0);
212 but->set_active(false);
213 } else if (!was_active && !but->get_active()) {
214 _adjustment->set_value(1.0);
215 but->set_active(true);
221 midi_note_to_hz(int note)
223 const double tuning = 440.0;
224 return tuning * pow(2, (note - 69.0) / 12.0);
228 clamp(double val, double min, double max)
232 } else if (val > max) {
239 AutomationController::run_note_select_dialog()
241 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
242 NoteSelectDialog* dialog = new NoteSelectDialog();
243 if (dialog->run() == Gtk::RESPONSE_ACCEPT) {
244 const double value = ((_controllable->desc().unit == ARDOUR::ParameterDescriptor::HZ)
245 ? midi_note_to_hz(dialog->note_number())
246 : dialog->note_number());
247 _controllable->set_value(clamp(value, desc.lower, desc.upper));
253 AutomationController::set_freq_beats(double beats)
255 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
256 const ARDOUR::Session& session = _controllable->session();
257 const framepos_t pos = session.transport_frame();
258 const ARDOUR::Tempo& tempo = session.tempo_map().tempo_at(pos);
259 const double bpm = tempo.beats_per_minute();
260 const double bps = bpm / 60.0;
261 const double freq = bps / beats;
262 _controllable->set_value(clamp(freq, desc.lower, desc.upper));
266 AutomationController::set_ratio(double ratio)
268 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
269 const double value = _controllable->get_value() * ratio;
270 _controllable->set_value(clamp(value, desc.lower, desc.upper));
274 AutomationController::on_button_release(GdkEventButton* ev)
276 using namespace Gtk::Menu_Helpers;
278 if (ev->button != 3) {
282 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
283 if (desc.unit == ARDOUR::ParameterDescriptor::MIDI_NOTE) {
284 Gtk::Menu* menu = manage(new Menu());
285 MenuList& items = menu->items();
286 items.push_back(MenuElem(_("Select Note..."),
287 sigc::mem_fun(*this, &AutomationController::run_note_select_dialog)));
288 menu->popup(1, ev->time);
290 } else if (desc.unit == ARDOUR::ParameterDescriptor::HZ) {
291 Gtk::Menu* menu = manage(new Menu());
292 MenuList& items = menu->items();
293 items.push_back(MenuElem(_("Halve"),
294 sigc::bind(sigc::mem_fun(*this, &AutomationController::set_ratio),
296 items.push_back(MenuElem(_("Double"),
297 sigc::bind(sigc::mem_fun(*this, &AutomationController::set_ratio),
299 const bool is_audible = desc.upper > 40.0;
300 const bool is_low = desc.lower < 1.0;
302 items.push_back(MenuElem(_("Select Note..."),
303 sigc::mem_fun(*this, &AutomationController::run_note_select_dialog)));
306 for (double beats = 1.0; beats <= 16; ++beats) {
307 items.push_back(MenuElem(string_compose(_("Set to %1 beat(s)"), (int)beats),
308 sigc::bind(sigc::mem_fun(*this, &AutomationController::set_freq_beats),
312 menu->popup(1, ev->time);
320 AutomationController::value_changed ()
322 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&AutomationController::display_effective_value, this));
325 /** Stop updating our value from our controllable */
327 AutomationController::stop_updating ()
329 _screen_update_connection.disconnect ();
333 AutomationController::disable_vertical_scroll ()
335 AutomationBarController* bar = dynamic_cast<AutomationBarController*>(_widget);
338 Gtkmm2ext::PixFader::Tweaks(bar->tweaks() |
339 Gtkmm2ext::PixFader::NoVerticalScroll));