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