make BusProfile argument to new Session constructor be const (and in associated call...
[ardour.git] / libs / ardour / pannable.cc
1 /*
2  * Copyright (C) 2011-2016 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2011 Carl Hetherington <carl@carlh.net>
4  * Copyright (C) 2011 David Robillard <d@drobilla.net>
5  * Copyright (C) 2015-2017 Robin Gareus <robin@gareus.org>
6  * Copyright (C) 2016 Tim Mayberry <mojofunk@gmail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #include "pbd/error.h"
24 #include "pbd/compose.h"
25
26 #include "ardour/boost_debug.h"
27 #include "ardour/debug.h"
28 #include "ardour/automation_control.h"
29 #include "ardour/automation_list.h"
30 #include "ardour/pannable.h"
31 #include "ardour/panner.h"
32 #include "ardour/pan_controllable.h"
33 #include "ardour/session.h"
34 #include "ardour/value_as_string.h"
35
36 #include "pbd/i18n.h"
37
38 using namespace std;
39 using namespace PBD;
40 using namespace ARDOUR;
41
42 Pannable::Pannable (Session& s)
43         : Automatable (s)
44         , SessionHandleRef (s)
45         , pan_azimuth_control (new PanControllable (s, "", this, PanAzimuthAutomation))
46         , pan_elevation_control (new PanControllable (s, "", this, PanElevationAutomation))
47         , pan_width_control (new PanControllable (s, "", this, PanWidthAutomation))
48         , pan_frontback_control (new PanControllable (s, "", this, PanFrontBackAutomation))
49         , pan_lfe_control (new PanControllable (s, "", this, PanLFEAutomation))
50         , _auto_state (Off)
51         , _has_state (false)
52         , _responding_to_control_auto_state_change (0)
53 {
54         //boost_debug_shared_ptr_mark_interesting (this, "pannable");
55
56         add_control (pan_azimuth_control);
57         add_control (pan_elevation_control);
58         add_control (pan_width_control);
59         add_control (pan_frontback_control);
60         add_control (pan_lfe_control);
61
62         /* all controls change state together */
63
64         pan_azimuth_control->alist()->automation_state_changed.connect_same_thread (*this, boost::bind (&Pannable::control_auto_state_changed, this, _1));
65         pan_elevation_control->alist()->automation_state_changed.connect_same_thread (*this, boost::bind (&Pannable::control_auto_state_changed, this, _1));
66         pan_width_control->alist()->automation_state_changed.connect_same_thread (*this, boost::bind (&Pannable::control_auto_state_changed, this, _1));
67         pan_frontback_control->alist()->automation_state_changed.connect_same_thread (*this, boost::bind (&Pannable::control_auto_state_changed, this, _1));
68         pan_lfe_control->alist()->automation_state_changed.connect_same_thread (*this, boost::bind (&Pannable::control_auto_state_changed, this, _1));
69
70         pan_azimuth_control->Changed.connect_same_thread (*this, boost::bind (&Pannable::value_changed, this));
71         pan_elevation_control->Changed.connect_same_thread (*this, boost::bind (&Pannable::value_changed, this));
72         pan_width_control->Changed.connect_same_thread (*this, boost::bind (&Pannable::value_changed, this));
73         pan_frontback_control->Changed.connect_same_thread (*this, boost::bind (&Pannable::value_changed, this));
74         pan_lfe_control->Changed.connect_same_thread (*this, boost::bind (&Pannable::value_changed, this));
75 }
76
77 Pannable::~Pannable ()
78 {
79         DEBUG_TRACE (DEBUG::Destruction, string_compose ("pannable @ %1 destructor\n", this));
80 }
81
82 void
83 Pannable::control_auto_state_changed (AutoState new_state)
84 {
85         if (_responding_to_control_auto_state_change) {
86                 return;
87         }
88
89         _responding_to_control_auto_state_change++;
90
91         pan_azimuth_control->set_automation_state (new_state);
92         pan_width_control->set_automation_state (new_state);
93         pan_elevation_control->set_automation_state (new_state);
94         pan_frontback_control->set_automation_state (new_state);
95         pan_lfe_control->set_automation_state (new_state);
96
97         _responding_to_control_auto_state_change--;
98
99         _auto_state = new_state;
100         automation_state_changed (new_state);  /* EMIT SIGNAL */
101 }
102
103 void
104 Pannable::set_panner (boost::shared_ptr<Panner> p)
105 {
106         _panner = p;
107 }
108
109 void
110 Pannable::value_changed ()
111 {
112         if (!has_state ()) {
113                 // prevent the GUI from resetting panners
114                 // e.g. when switching to aux-sends and back.
115                 _has_state = true;
116         }
117
118         _session.set_dirty ();
119 }
120
121 void
122 Pannable::set_automation_state (AutoState state)
123 {
124         if (state != _auto_state) {
125                 _auto_state = state;
126
127                 const Controls& c (controls());
128
129                 for (Controls::const_iterator ci = c.begin(); ci != c.end(); ++ci) {
130                         boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl>(ci->second);
131                         if (ac) {
132                                 ac->alist()->set_automation_state (state);
133                         }
134                 }
135
136                 session().set_dirty ();
137                 automation_state_changed (_auto_state);
138         }
139 }
140
141 void
142 Pannable::start_touch (double when)
143 {
144         const Controls& c (controls());
145
146         for (Controls::const_iterator ci = c.begin(); ci != c.end(); ++ci) {
147                 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl>(ci->second);
148                 if (ac) {
149                         ac->alist()->start_touch (when);
150                 }
151         }
152         g_atomic_int_set (&_touching, 1);
153 }
154
155 void
156 Pannable::stop_touch (double when)
157 {
158         const Controls& c (controls());
159
160         for (Controls::const_iterator ci = c.begin(); ci != c.end(); ++ci) {
161                 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl>(ci->second);
162                 if (ac) {
163                         ac->alist()->stop_touch (when);
164                 }
165         }
166         g_atomic_int_set (&_touching, 0);
167 }
168
169 XMLNode&
170 Pannable::get_state ()
171 {
172         return state ();
173 }
174
175 XMLNode&
176 Pannable::state ()
177 {
178         XMLNode* node = new XMLNode (X_("Pannable"));
179
180         node->add_child_nocopy (pan_azimuth_control->get_state());
181         node->add_child_nocopy (pan_width_control->get_state());
182         node->add_child_nocopy (pan_elevation_control->get_state());
183         node->add_child_nocopy (pan_frontback_control->get_state());
184         node->add_child_nocopy (pan_lfe_control->get_state());
185
186         node->add_child_nocopy (get_automation_xml_state ());
187
188         return *node;
189 }
190
191 int
192 Pannable::set_state (const XMLNode& root, int version)
193 {
194         if (root.name() != X_("Pannable")) {
195                 warning << string_compose (_("Pannable given XML data for %1 - ignored"), root.name()) << endmsg;
196                 return -1;
197         }
198
199         const XMLNodeList& nlist (root.children());
200         XMLNodeConstIterator niter;
201
202         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
203                 if ((*niter)->name() == Controllable::xml_node_name) {
204                         std::string control_name;
205
206                         if (!(*niter)->get_property (X_("name"), control_name)) {
207                                 continue;
208                         }
209
210                         if (control_name == pan_azimuth_control->name()) {
211                                 pan_azimuth_control->set_state (**niter, version);
212                         } else if (control_name == pan_width_control->name()) {
213                                 pan_width_control->set_state (**niter, version);
214                         } else if (control_name == pan_elevation_control->name()) {
215                                 pan_elevation_control->set_state (**niter, version);
216                         } else if (control_name == pan_frontback_control->name()) {
217                                 pan_frontback_control->set_state (**niter, version);
218                         } else if (control_name == pan_lfe_control->name()) {
219                                 pan_lfe_control->set_state (**niter, version);
220                         }
221
222                 } else if ((*niter)->name() == Automatable::xml_node_name) {
223                         set_automation_xml_state (**niter, PanAzimuthAutomation);
224
225                 } else {
226                         /* old school (alpha1-6) XML info */
227
228                         float val;
229                         if ((*niter)->name() == X_("azimuth")) {
230                                 if ((*niter)->get_property (X_("value"), val)) {
231                                         pan_azimuth_control->set_value (val, Controllable::NoGroup);
232                                 }
233                         } else if ((*niter)->name() == X_("width")) {
234                                 if ((*niter)->get_property (X_("value"), val)) {
235                                         pan_width_control->set_value (val, Controllable::NoGroup);
236                                 }
237                         } else if ((*niter)->name() == X_("elevation")) {
238                                 if ((*niter)->get_property (X_("value"), val)) {
239                                         pan_elevation_control->set_value (val, Controllable::NoGroup);
240                                 }
241                         } else if ((*niter)->name() == X_("frontback")) {
242                                 if ((*niter)->get_property (X_("value"), val)) {
243                                         pan_frontback_control->set_value (val, Controllable::NoGroup);
244                                 }
245                         } else if ((*niter)->name() == X_("lfe")) {
246                                 if ((*niter)->get_property (X_("value"), val)) {
247                                         pan_lfe_control->set_value (val, Controllable::NoGroup);
248                                 }
249                         }
250                 }
251         }
252
253         _has_state = true;
254
255         return 0;
256 }
257
258 string
259 Pannable::value_as_string (boost::shared_ptr<const AutomationControl> ac) const
260 {
261         boost::shared_ptr<Panner> p = panner ();
262
263         if (p) {
264                 return p->value_as_string (ac);
265         }
266
267         return ARDOUR::value_as_string(ac->desc(), ac->get_value());
268 }