Automation of LV2 plugin properties.
[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/error.h"
25
26 #include "ardour/automatable.h"
27 #include "ardour/automation_control.h"
28 #include "ardour/session.h"
29
30 #include "ardour_ui.h"
31 #include "automation_controller.h"
32 #include "gui_thread.h"
33
34 #include "i18n.h"
35
36 using namespace ARDOUR;
37 using namespace Gtk;
38
39 AutomationController::AutomationController(boost::shared_ptr<Automatable>       printer,
40                                            boost::shared_ptr<AutomationControl> ac,
41                                            Adjustment*                          adj)
42         : BarController (*adj, ac)
43         , _ignore_change(false)
44         , _printer (printer)
45         , _controllable(ac)
46         , _adjustment(adj)
47 {
48         assert (_printer);
49
50         set_name (X_("ProcessorControlSlider"));
51
52         StartGesture.connect (sigc::mem_fun(*this, &AutomationController::start_touch));
53         StopGesture.connect (sigc::mem_fun(*this, &AutomationController::end_touch));
54
55         _adjustment->signal_value_changed().connect (
56                         sigc::mem_fun(*this, &AutomationController::value_adjusted));
57
58         _screen_update_connection = ARDOUR_UI::RapidScreenUpdate.connect (
59                         sigc::mem_fun (*this, &AutomationController::display_effective_value));
60
61         ac->Changed.connect (_changed_connection, invalidator (*this), boost::bind (&AutomationController::value_changed, this), gui_context());
62 }
63
64 AutomationController::~AutomationController()
65 {
66 }
67
68 boost::shared_ptr<AutomationController>
69 AutomationController::create(boost::shared_ptr<Automatable>       printer,
70                              const Evoral::Parameter&             param,
71                              const ParameterDescriptor&           desc,
72                              boost::shared_ptr<AutomationControl> ac)
73 {
74         const double lo        = ac->internal_to_interface(desc.lower);
75         const double up        = ac->internal_to_interface(desc.upper);
76         const double normal    = ac->internal_to_interface(desc.normal);
77         double       smallstep = desc.smallstep;
78         double       largestep = desc.largestep;
79         if (smallstep == 0.0) {
80                 smallstep = (up - lo) / 100;
81         }
82         if (largestep == 0.0) {
83                 largestep = (up - lo) / 10;
84         }
85         smallstep = ac->internal_to_interface(smallstep);
86         largestep = ac->internal_to_interface(largestep);
87
88         Gtk::Adjustment* adjustment = manage (
89                 new Gtk::Adjustment (normal, lo, up, smallstep, largestep));
90
91         assert (ac);
92         assert(ac->parameter() == param);
93         return boost::shared_ptr<AutomationController>(new AutomationController(printer, ac, adjustment));
94 }
95
96 std::string
97 AutomationController::get_label (double& xpos)
98 {
99         xpos = 0.5;
100         return _printer->value_as_string (_controllable);
101 }
102
103 void
104 AutomationController::display_effective_value()
105 {
106         double const interface_value = _controllable->internal_to_interface(_controllable->get_value());
107
108         if (_adjustment->get_value () != interface_value) {
109                 _ignore_change = true;
110                 _adjustment->set_value (interface_value);
111                 _ignore_change = false;
112         }
113 }
114
115 void
116 AutomationController::value_adjusted ()
117 {
118         if (!_ignore_change) {
119                 _controllable->set_value (_controllable->interface_to_internal(_adjustment->get_value()));
120         }
121 }
122
123 void
124 AutomationController::start_touch()
125 {
126         _controllable->start_touch (_controllable->session().transport_frame());
127 }
128
129 void
130 AutomationController::end_touch ()
131 {
132         if (!_controllable->alist()) return;
133         if (_controllable->automation_state() == Touch) {
134
135                 bool mark = false;
136                 double when = 0;
137
138                 if (_controllable->session().transport_rolling()) {
139                         mark = true;
140                         when = _controllable->session().transport_frame();
141                 }
142
143                 _controllable->stop_touch (mark, when);
144         }
145 }
146
147 void
148 AutomationController::value_changed ()
149 {
150         Gtkmm2ext::UI::instance()->call_slot (invalidator (*this), boost::bind (&AutomationController::display_effective_value, this));
151 }
152
153 /** Stop updating our value from our controllable */
154 void
155 AutomationController::stop_updating ()
156 {
157         _screen_update_connection.disconnect ();
158 }