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 AutomationBarController::AutomationBarController (
44 boost::shared_ptr<Automatable> printer,
45 boost::shared_ptr<AutomationControl> ac,
47 : Gtkmm2ext::BarController(*adj, ac)
54 AutomationBarController::get_label (double& xpos)
57 return _printer->value_as_string (_controllable);
60 AutomationBarController::~AutomationBarController()
64 AutomationController::AutomationController(boost::shared_ptr<Automatable> printer,
65 boost::shared_ptr<AutomationControl> ac,
71 , _ignore_change(false)
76 ArdourButton* but = manage(new ArdourButton());
78 // Apply styles for special types
79 if (ac->parameter().type() == MuteAutomation) {
80 but->set_name("mute button");
81 } else if (ac->parameter().type() == SoloAutomation) {
82 but->set_name("solo button");
84 but->set_name("generic button");
86 but->signal_clicked.connect(
87 sigc::mem_fun(*this, &AutomationController::toggled));
91 AutomationBarController* bar = manage(new AutomationBarController(_printer, ac, adj));
93 bar->set_name(X_("ProcessorControlSlider"));
94 bar->StartGesture.connect(
95 sigc::mem_fun(*this, &AutomationController::start_touch));
96 bar->StopGesture.connect(
97 sigc::mem_fun(*this, &AutomationController::end_touch));
98 bar->signal_button_release_event().connect(
99 sigc::mem_fun(*this, &AutomationController::on_button_release));
104 _adjustment->signal_value_changed().connect(
105 sigc::mem_fun(*this, &AutomationController::value_adjusted));
107 _screen_update_connection = ARDOUR_UI::RapidScreenUpdate.connect (
108 sigc::mem_fun (*this, &AutomationController::display_effective_value));
110 ac->Changed.connect (_changed_connection, invalidator (*this), boost::bind (&AutomationController::value_changed, this), gui_context());
116 AutomationController::~AutomationController()
120 boost::shared_ptr<AutomationController>
121 AutomationController::create(boost::shared_ptr<Automatable> printer,
122 const Evoral::Parameter& param,
123 const ParameterDescriptor& desc,
124 boost::shared_ptr<AutomationControl> ac)
126 const double lo = ac->internal_to_interface(desc.lower);
127 const double up = ac->internal_to_interface(desc.upper);
128 const double normal = ac->internal_to_interface(desc.normal);
129 const double smallstep = ac->internal_to_interface(desc.lower + desc.smallstep);
130 const double largestep = ac->internal_to_interface(desc.lower + desc.largestep);
132 Gtk::Adjustment* adjustment = manage (
133 new Gtk::Adjustment (normal, lo, up, smallstep, largestep));
136 assert(ac->parameter() == param);
137 return boost::shared_ptr<AutomationController>(new AutomationController(printer, ac, adjustment));
141 AutomationController::display_effective_value()
143 double const interface_value = _controllable->internal_to_interface(_controllable->get_value());
145 if (_adjustment->get_value () != interface_value) {
146 _ignore_change = true;
147 _adjustment->set_value (interface_value);
148 _ignore_change = false;
153 AutomationController::value_adjusted ()
155 if (!_ignore_change) {
156 _controllable->set_value (_controllable->interface_to_internal(_adjustment->get_value()));
159 /* A bar controller will automatically follow the adjustment, but for a
160 button we have to do it manually. */
161 ArdourButton* but = dynamic_cast<ArdourButton*>(_widget);
163 const bool active = _adjustment->get_value() >= 0.5;
164 if (but->get_active() != active) {
165 but->set_active(active);
171 AutomationController::start_touch()
173 _controllable->start_touch (_controllable->session().transport_frame());
174 StartGesture.emit(); /* EMIT SIGNAL */
178 AutomationController::end_touch ()
180 if (_controllable->automation_state() == Touch) {
185 if (_controllable->session().transport_rolling()) {
187 when = _controllable->session().transport_frame();
190 _controllable->stop_touch (mark, when);
192 StopGesture.emit(); /* EMIT SIGNAL */
196 AutomationController::toggled ()
198 ArdourButton* but = dynamic_cast<ArdourButton*>(_widget);
200 if (_controllable->session().transport_rolling()) {
201 if (_controllable->automation_state() == Touch) {
202 _controllable->set_automation_state(Write);
204 if (_controllable->list()) {
205 _controllable->list()->set_in_write_pass(true, false, _controllable->session().audible_frame());
208 const bool was_active = _controllable->get_value() >= 0.5;
209 if (was_active && but->get_active()) {
210 _adjustment->set_value(0.0);
211 but->set_active(false);
212 } else if (!was_active && !but->get_active()) {
213 _adjustment->set_value(1.0);
214 but->set_active(true);
220 midi_note_to_hz(int note)
222 const double tuning = 440.0;
223 return tuning * pow(2, (note - 69.0) / 12.0);
227 clamp(double val, double min, double max)
231 } else if (val > max) {
238 AutomationController::run_note_select_dialog()
240 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
241 NoteSelectDialog* dialog = new NoteSelectDialog();
242 if (dialog->run() == Gtk::RESPONSE_ACCEPT) {
243 const double value = ((_controllable->desc().unit == ARDOUR::ParameterDescriptor::HZ)
244 ? midi_note_to_hz(dialog->note_number())
245 : dialog->note_number());
246 _controllable->set_value(clamp(value, desc.lower, desc.upper));
252 AutomationController::set_freq_beats(double beats)
254 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
255 const ARDOUR::Session& session = _controllable->session();
256 const framepos_t pos = session.transport_frame();
257 const ARDOUR::Tempo& tempo = session.tempo_map().tempo_at(pos);
258 const double bpm = tempo.beats_per_minute();
259 const double bps = bpm / 60.0;
260 const double freq = bps / beats;
261 _controllable->set_value(clamp(freq, desc.lower, desc.upper));
265 AutomationController::set_ratio(double ratio)
267 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
268 const double value = _controllable->get_value() * ratio;
269 _controllable->set_value(clamp(value, desc.lower, desc.upper));
273 AutomationController::on_button_release(GdkEventButton* ev)
275 using namespace Gtk::Menu_Helpers;
277 if (ev->button != 3) {
281 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
282 if (desc.unit == ARDOUR::ParameterDescriptor::MIDI_NOTE) {
283 Gtk::Menu* menu = manage(new Menu());
284 MenuList& items = menu->items();
285 items.push_back(MenuElem(_("Select Note..."),
286 sigc::mem_fun(*this, &AutomationController::run_note_select_dialog)));
287 menu->popup(1, ev->time);
289 } else if (desc.unit == ARDOUR::ParameterDescriptor::HZ) {
290 Gtk::Menu* menu = manage(new Menu());
291 MenuList& items = menu->items();
292 items.push_back(MenuElem(_("Halve"),
293 sigc::bind(sigc::mem_fun(*this, &AutomationController::set_ratio),
295 items.push_back(MenuElem(_("Double"),
296 sigc::bind(sigc::mem_fun(*this, &AutomationController::set_ratio),
298 const bool is_audible = desc.upper > 40.0;
299 const bool is_low = desc.lower < 1.0;
301 items.push_back(MenuElem(_("Select Note..."),
302 sigc::mem_fun(*this, &AutomationController::run_note_select_dialog)));
305 for (double beats = 1.0; beats <= 16; ++beats) {
306 items.push_back(MenuElem(string_compose(_("Set to %1 beat(s)"), (int)beats),
307 sigc::bind(sigc::mem_fun(*this, &AutomationController::set_freq_beats),
311 menu->popup(1, ev->time);
319 AutomationController::value_changed ()
321 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&AutomationController::display_effective_value, this));
324 /** Stop updating our value from our controllable */
326 AutomationController::stop_updating ()
328 _screen_update_connection.disconnect ();
332 AutomationController::disable_vertical_scroll ()
334 AutomationBarController* bar = dynamic_cast<AutomationBarController*>(_widget);
337 Gtkmm2ext::PixFader::Tweaks(bar->tweaks() |
338 Gtkmm2ext::PixFader::NoVerticalScroll));