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_ui.h"
33 #include "automation_controller.h"
34 #include "gui_thread.h"
35 #include "note_select_dialog.h"
39 using namespace ARDOUR;
42 AutomationController::AutomationController(boost::shared_ptr<Automatable> printer,
43 boost::shared_ptr<AutomationControl> ac,
45 : BarController (*adj, ac)
46 , _ignore_change(false)
53 set_name (X_("ProcessorControlSlider"));
55 StartGesture.connect (sigc::mem_fun(*this, &AutomationController::start_touch));
56 StopGesture.connect (sigc::mem_fun(*this, &AutomationController::end_touch));
58 signal_button_release_event().connect(
59 sigc::mem_fun(*this, &AutomationController::on_button_release));
61 _adjustment->signal_value_changed().connect (
62 sigc::mem_fun(*this, &AutomationController::value_adjusted));
64 _screen_update_connection = ARDOUR_UI::RapidScreenUpdate.connect (
65 sigc::mem_fun (*this, &AutomationController::display_effective_value));
67 ac->Changed.connect (_changed_connection, invalidator (*this), boost::bind (&AutomationController::value_changed, this), gui_context());
70 AutomationController::~AutomationController()
74 boost::shared_ptr<AutomationController>
75 AutomationController::create(boost::shared_ptr<Automatable> printer,
76 const Evoral::Parameter& param,
77 const ParameterDescriptor& desc,
78 boost::shared_ptr<AutomationControl> ac)
80 const double lo = ac->internal_to_interface(desc.lower);
81 const double up = ac->internal_to_interface(desc.upper);
82 const double normal = ac->internal_to_interface(desc.normal);
83 double smallstep = desc.smallstep;
84 double largestep = desc.largestep;
85 if (smallstep == 0.0) {
86 smallstep = (up - lo) / 100;
88 if (largestep == 0.0) {
89 largestep = (up - lo) / 10;
91 smallstep = ac->internal_to_interface(smallstep);
92 largestep = ac->internal_to_interface(largestep);
94 Gtk::Adjustment* adjustment = manage (
95 new Gtk::Adjustment (normal, lo, up, smallstep, largestep));
98 assert(ac->parameter() == param);
99 return boost::shared_ptr<AutomationController>(new AutomationController(printer, ac, adjustment));
103 AutomationController::get_label (double& xpos)
106 return _printer->value_as_string (_controllable);
110 AutomationController::display_effective_value()
112 double const interface_value = _controllable->internal_to_interface(_controllable->get_value());
114 if (_adjustment->get_value () != interface_value) {
115 _ignore_change = true;
116 _adjustment->set_value (interface_value);
117 _ignore_change = false;
122 AutomationController::value_adjusted ()
124 if (!_ignore_change) {
125 _controllable->set_value (_controllable->interface_to_internal(_adjustment->get_value()));
130 AutomationController::start_touch()
132 _controllable->start_touch (_controllable->session().transport_frame());
136 AutomationController::end_touch ()
138 if (!_controllable->alist()) return;
139 if (_controllable->automation_state() == Touch) {
144 if (_controllable->session().transport_rolling()) {
146 when = _controllable->session().transport_frame();
149 _controllable->stop_touch (mark, when);
154 midi_note_to_hz(int note)
156 const double tuning = 440.0;
157 return tuning * pow(2, (note - 69.0) / 12.0);
161 clamp(double val, double min, double max)
165 } else if (val > max) {
172 AutomationController::run_note_select_dialog()
174 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
175 NoteSelectDialog* dialog = new NoteSelectDialog();
176 if (dialog->run() == Gtk::RESPONSE_ACCEPT) {
177 const double value = ((_controllable->desc().unit == ARDOUR::ParameterDescriptor::HZ)
178 ? midi_note_to_hz(dialog->note_number())
179 : dialog->note_number());
180 _controllable->set_value(clamp(value, desc.lower, desc.upper));
186 AutomationController::set_freq_beats(double beats)
188 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
189 const ARDOUR::Session& session = _controllable->session();
190 const framepos_t pos = session.transport_frame();
191 const ARDOUR::Tempo& tempo = session.tempo_map().tempo_at(pos);
192 const double bpm = tempo.beats_per_minute();
193 const double bps = bpm / 60.0;
194 const double freq = bps / beats;
195 _controllable->set_value(clamp(freq, desc.lower, desc.upper));
199 AutomationController::set_ratio(double ratio)
201 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
202 const double value = _controllable->get_value() * ratio;
203 _controllable->set_value(clamp(value, desc.lower, desc.upper));
207 AutomationController::on_button_release(GdkEventButton* ev)
209 using namespace Gtk::Menu_Helpers;
211 if (ev->button != 3) {
215 const ARDOUR::ParameterDescriptor& desc = _controllable->desc();
216 if (desc.unit == ARDOUR::ParameterDescriptor::MIDI_NOTE) {
217 Gtk::Menu* menu = manage(new Menu());
218 MenuList& items = menu->items();
219 items.push_back(MenuElem(_("Select Note..."),
220 sigc::mem_fun(*this, &AutomationController::run_note_select_dialog)));
221 menu->popup(1, ev->time);
223 } else if (desc.unit == ARDOUR::ParameterDescriptor::HZ) {
224 Gtk::Menu* menu = manage(new Menu());
225 MenuList& items = menu->items();
226 items.push_back(MenuElem(_("Halve"),
227 sigc::bind(sigc::mem_fun(*this, &AutomationController::set_ratio),
229 items.push_back(MenuElem(_("Double"),
230 sigc::bind(sigc::mem_fun(*this, &AutomationController::set_ratio),
232 const bool is_audible = desc.upper > 40.0;
233 const bool is_low = desc.lower < 1.0;
235 items.push_back(MenuElem(_("Select Note..."),
236 sigc::mem_fun(*this, &AutomationController::run_note_select_dialog)));
239 for (double beats = 1.0; beats <= 16; ++beats) {
240 items.push_back(MenuElem(string_compose(_("Set to %1 beat(s)"), (int)beats),
241 sigc::bind(sigc::mem_fun(*this, &AutomationController::set_freq_beats),
245 menu->popup(1, ev->time);
253 AutomationController::value_changed ()
255 Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&AutomationController::display_effective_value, this));
258 /** Stop updating our value from our controllable */
260 AutomationController::stop_updating ()
262 _screen_update_connection.disconnect ();