Add menu to set frequency controls in beats, and half or double current value.
[ardour.git] / gtk2_ardour / automation_controller.cc
1 /*
2     Copyright (C) 2007 Paul Davis
3     Author: David Robillard
4
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.
9
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.
14
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.
18
19 */
20
21 #include <iomanip>
22 #include <cmath>
23
24 #include "pbd/compose.h"
25 #include "pbd/error.h"
26
27 #include "ardour/automatable.h"
28 #include "ardour/automation_control.h"
29 #include "ardour/session.h"
30 #include "ardour/tempo.h"
31
32 #include "ardour_ui.h"
33 #include "automation_controller.h"
34 #include "gui_thread.h"
35 #include "note_select_dialog.h"
36
37 #include "i18n.h"
38
39 using namespace ARDOUR;
40 using namespace Gtk;
41
42 AutomationController::AutomationController(boost::shared_ptr<Automatable>       printer,
43                                            boost::shared_ptr<AutomationControl> ac,
44                                            Adjustment*                          adj)
45         : BarController (*adj, ac)
46         , _ignore_change(false)
47         , _printer (printer)
48         , _controllable(ac)
49         , _adjustment(adj)
50 {
51         assert (_printer);
52
53         set_name (X_("ProcessorControlSlider"));
54
55         StartGesture.connect (sigc::mem_fun(*this, &AutomationController::start_touch));
56         StopGesture.connect (sigc::mem_fun(*this, &AutomationController::end_touch));
57
58         signal_button_release_event().connect(
59                 sigc::mem_fun(*this, &AutomationController::on_button_release));
60
61         _adjustment->signal_value_changed().connect (
62                         sigc::mem_fun(*this, &AutomationController::value_adjusted));
63
64         _screen_update_connection = ARDOUR_UI::RapidScreenUpdate.connect (
65                         sigc::mem_fun (*this, &AutomationController::display_effective_value));
66
67         ac->Changed.connect (_changed_connection, invalidator (*this), boost::bind (&AutomationController::value_changed, this), gui_context());
68 }
69
70 AutomationController::~AutomationController()
71 {
72 }
73
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)
79 {
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;
87         }
88         if (largestep == 0.0) {
89                 largestep = (up - lo) / 10;
90         }
91         smallstep = ac->internal_to_interface(smallstep);
92         largestep = ac->internal_to_interface(largestep);
93
94         Gtk::Adjustment* adjustment = manage (
95                 new Gtk::Adjustment (normal, lo, up, smallstep, largestep));
96
97         assert (ac);
98         assert(ac->parameter() == param);
99         return boost::shared_ptr<AutomationController>(new AutomationController(printer, ac, adjustment));
100 }
101
102 std::string
103 AutomationController::get_label (double& xpos)
104 {
105         xpos = 0.5;
106         return _printer->value_as_string (_controllable);
107 }
108
109 void
110 AutomationController::display_effective_value()
111 {
112         double const interface_value = _controllable->internal_to_interface(_controllable->get_value());
113
114         if (_adjustment->get_value () != interface_value) {
115                 _ignore_change = true;
116                 _adjustment->set_value (interface_value);
117                 _ignore_change = false;
118         }
119 }
120
121 void
122 AutomationController::value_adjusted ()
123 {
124         if (!_ignore_change) {
125                 _controllable->set_value (_controllable->interface_to_internal(_adjustment->get_value()));
126         }
127 }
128
129 void
130 AutomationController::start_touch()
131 {
132         _controllable->start_touch (_controllable->session().transport_frame());
133 }
134
135 void
136 AutomationController::end_touch ()
137 {
138         if (!_controllable->alist()) return;
139         if (_controllable->automation_state() == Touch) {
140
141                 bool mark = false;
142                 double when = 0;
143
144                 if (_controllable->session().transport_rolling()) {
145                         mark = true;
146                         when = _controllable->session().transport_frame();
147                 }
148
149                 _controllable->stop_touch (mark, when);
150         }
151 }
152
153 void
154 AutomationController::run_note_select_dialog()
155 {
156         NoteSelectDialog* dialog = new NoteSelectDialog();
157         if (dialog->run() == Gtk::RESPONSE_ACCEPT) {
158                 _controllable->set_value(dialog->note_number());
159         }
160         delete dialog;
161 }
162
163 void
164 AutomationController::set_freq_beats(double beats)
165 {
166         const ARDOUR::Session& session = _controllable->session();
167         const ARDOUR::Tempo&   tempo   = session.tempo_map().tempo_at(0);
168         const double           bpm     = tempo.beats_per_minute();
169         const double           bps     = bpm / 60.0;
170         _controllable->set_value(bps / beats);
171 }
172
173 void
174 AutomationController::set_ratio(double ratio)
175 {
176         _controllable->set_value(_controllable->get_value() * ratio);
177 }
178
179 bool
180 AutomationController::on_button_release(GdkEventButton* ev)
181 {
182         using namespace Gtk::Menu_Helpers;
183
184         if (ev->button != 3) {
185                 return false;
186         }
187
188         if (_controllable->desc().unit == ARDOUR::ParameterDescriptor::MIDI_NOTE) {
189                 Gtk::Menu* menu  = manage(new Menu());
190                 MenuList&  items = menu->items();
191                 items.push_back(MenuElem(_("Select Note..."),
192                                          sigc::mem_fun(*this, &AutomationController::run_note_select_dialog)));
193                 menu->popup(1, ev->time);
194                 return true;
195         } else if (_controllable->desc().unit == ARDOUR::ParameterDescriptor::HZ) {
196                 Gtk::Menu* menu  = manage(new Menu());
197                 MenuList&  items = menu->items();
198                 items.push_back(MenuElem(_("Halve"),
199                                          sigc::bind(sigc::mem_fun(*this, &AutomationController::set_ratio),
200                                                     0.5)));
201                 items.push_back(MenuElem(_("Double"),
202                                          sigc::bind(sigc::mem_fun(*this, &AutomationController::set_ratio),
203                                                     2.0)));
204                 for (double beats = 1.0; beats <= 16; ++beats) {
205                         items.push_back(MenuElem(string_compose(_("Set to %1 beat(s)"), (int)beats),
206                                                  sigc::bind(sigc::mem_fun(*this, &AutomationController::set_freq_beats),
207                                                             beats)));
208                 }
209                 menu->popup(1, ev->time);
210                 return true;
211         }
212
213         return false;
214 }
215
216 void
217 AutomationController::value_changed ()
218 {
219         Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&AutomationController::display_effective_value, this));
220 }
221
222 /** Stop updating our value from our controllable */
223 void
224 AutomationController::stop_updating ()
225 {
226         _screen_update_connection.disconnect ();
227 }