Add a missing #define to our MSVC project (portaudio_backend)
[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/convert.h"
22 #include "pbd/compose.h"
23
24 #include "ardour/boost_debug.h"
25 #include "ardour/debug.h"
26 #include "ardour/automation_control.h"
27 #include "ardour/automation_list.h"
28 #include "ardour/pannable.h"
29 #include "ardour/panner.h"
30 #include "ardour/pan_controllable.h"
31 #include "ardour/session.h"
32
33 #include "pbd/i18n.h"
34
35 using namespace std;
36 using namespace PBD;
37 using namespace ARDOUR;
38
39 Pannable::Pannable (Session& s)
40         : Automatable (s)
41         , SessionHandleRef (s)
42         , pan_azimuth_control (new PanControllable (s, "", this, PanAzimuthAutomation))
43         , pan_elevation_control (new PanControllable (s, "", this, PanElevationAutomation))
44         , pan_width_control (new PanControllable (s, "", this, PanWidthAutomation))
45         , pan_frontback_control (new PanControllable (s, "", this, PanFrontBackAutomation))
46         , pan_lfe_control (new PanControllable (s, "", this, PanLFEAutomation))
47         , _auto_state (Off)
48         , _auto_style (Absolute)
49         , _has_state (false)
50         , _responding_to_control_auto_state_change (0)
51 {
52         //boost_debug_shared_ptr_mark_interesting (this, "pannable");
53
54         add_control (pan_azimuth_control);
55         add_control (pan_elevation_control);
56         add_control (pan_width_control);
57         add_control (pan_frontback_control);
58         add_control (pan_lfe_control);
59
60         /* all controls change state together */
61
62         pan_azimuth_control->alist()->automation_state_changed.connect_same_thread (*this, boost::bind (&Pannable::control_auto_state_changed, this, _1));
63         pan_elevation_control->alist()->automation_state_changed.connect_same_thread (*this, boost::bind (&Pannable::control_auto_state_changed, this, _1));
64         pan_width_control->alist()->automation_state_changed.connect_same_thread (*this, boost::bind (&Pannable::control_auto_state_changed, this, _1));
65         pan_frontback_control->alist()->automation_state_changed.connect_same_thread (*this, boost::bind (&Pannable::control_auto_state_changed, this, _1));
66         pan_lfe_control->alist()->automation_state_changed.connect_same_thread (*this, boost::bind (&Pannable::control_auto_state_changed, this, _1));
67
68         pan_azimuth_control->Changed.connect_same_thread (*this, boost::bind (&Pannable::value_changed, this));
69         pan_elevation_control->Changed.connect_same_thread (*this, boost::bind (&Pannable::value_changed, this));
70         pan_width_control->Changed.connect_same_thread (*this, boost::bind (&Pannable::value_changed, this));
71         pan_frontback_control->Changed.connect_same_thread (*this, boost::bind (&Pannable::value_changed, this));
72         pan_lfe_control->Changed.connect_same_thread (*this, boost::bind (&Pannable::value_changed, this));
73 }
74
75 Pannable::~Pannable ()
76 {
77         DEBUG_TRACE (DEBUG::Destruction, string_compose ("pannable @ %1 destructor\n", this));
78 }
79
80 void
81 Pannable::control_auto_state_changed (AutoState new_state)
82 {
83         if (_responding_to_control_auto_state_change) {
84                 return;
85         }
86
87         _responding_to_control_auto_state_change++;
88
89         pan_azimuth_control->set_automation_state (new_state);
90         pan_width_control->set_automation_state (new_state);
91         pan_elevation_control->set_automation_state (new_state);
92         pan_frontback_control->set_automation_state (new_state);
93         pan_lfe_control->set_automation_state (new_state);
94
95         _responding_to_control_auto_state_change--;
96
97         _auto_state = new_state;
98         automation_state_changed (new_state);  /* EMIT SIGNAL */
99 }
100
101 void
102 Pannable::set_panner (boost::shared_ptr<Panner> p)
103 {
104         _panner = p;
105 }
106
107 void
108 Pannable::value_changed ()
109 {
110         if (!has_state ()) {
111                 // prevent the GUI from resetting panners
112                 // e.g. when switching to aux-sends and back.
113                 _has_state = true;
114         }
115
116         _session.set_dirty ();
117 }
118
119 void
120 Pannable::set_automation_state (AutoState state)
121 {
122         if (state != _auto_state) {
123                 _auto_state = state;
124
125                 const Controls& c (controls());
126
127                 for (Controls::const_iterator ci = c.begin(); ci != c.end(); ++ci) {
128                         boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl>(ci->second);
129                         if (ac) {
130                                 ac->alist()->set_automation_state (state);
131                         }
132                 }
133
134                 session().set_dirty ();
135                 automation_state_changed (_auto_state);
136         }
137 }
138
139 void
140 Pannable::set_automation_style (AutoStyle style)
141 {
142         if (style != _auto_style) {
143                 _auto_style = style;
144
145                 const Controls& c (controls());
146
147                 for (Controls::const_iterator ci = c.begin(); ci != c.end(); ++ci) {
148                         boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl>(ci->second);
149                         if (ac) {
150                                 ac->alist()->set_automation_style (style);
151                         }
152                 }
153
154                 session().set_dirty ();
155                 automation_style_changed ();
156         }
157 }
158
159 void
160 Pannable::start_touch (double when)
161 {
162         const Controls& c (controls());
163
164         for (Controls::const_iterator ci = c.begin(); ci != c.end(); ++ci) {
165                 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl>(ci->second);
166                 if (ac) {
167                         ac->alist()->start_touch (when);
168                 }
169         }
170         g_atomic_int_set (&_touching, 1);
171 }
172
173 void
174 Pannable::stop_touch (bool mark, double when)
175 {
176         const Controls& c (controls());
177
178         for (Controls::const_iterator ci = c.begin(); ci != c.end(); ++ci) {
179                 boost::shared_ptr<AutomationControl> ac = boost::dynamic_pointer_cast<AutomationControl>(ci->second);
180                 if (ac) {
181                         ac->alist()->stop_touch (mark, when);
182                 }
183         }
184         g_atomic_int_set (&_touching, 0);
185 }
186
187 XMLNode&
188 Pannable::get_state ()
189 {
190         return state (true);
191 }
192
193 XMLNode&
194 Pannable::state (bool /*full*/)
195 {
196         LocaleGuard lg;
197         XMLNode* node = new XMLNode (X_("Pannable"));
198
199         node->add_child_nocopy (pan_azimuth_control->get_state());
200         node->add_child_nocopy (pan_width_control->get_state());
201         node->add_child_nocopy (pan_elevation_control->get_state());
202         node->add_child_nocopy (pan_frontback_control->get_state());
203         node->add_child_nocopy (pan_lfe_control->get_state());
204
205         node->add_child_nocopy (get_automation_xml_state ());
206
207         return *node;
208 }
209
210 int
211 Pannable::set_state (const XMLNode& root, int version)
212 {
213         if (root.name() != X_("Pannable")) {
214                 warning << string_compose (_("Pannable given XML data for %1 - ignored"), root.name()) << endmsg;
215                 return -1;
216         }
217
218         const XMLNodeList& nlist (root.children());
219         XMLNodeConstIterator niter;
220
221         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
222                 if ((*niter)->name() == Controllable::xml_node_name) {
223                         XMLProperty const * prop = (*niter)->property (X_("name"));
224
225                         if (!prop) {
226                                 continue;
227                         }
228
229                         if (prop->value() == pan_azimuth_control->name()) {
230                                 pan_azimuth_control->set_state (**niter, version);
231                         } else if (prop->value() == pan_width_control->name()) {
232                                 pan_width_control->set_state (**niter, version);
233                         } else if (prop->value() == pan_elevation_control->name()) {
234                                 pan_elevation_control->set_state (**niter, version);
235                         } else if (prop->value() == pan_frontback_control->name()) {
236                                 pan_frontback_control->set_state (**niter, version);
237                         } else if (prop->value() == pan_lfe_control->name()) {
238                                 pan_lfe_control->set_state (**niter, version);
239                         }
240
241                 } else if ((*niter)->name() == Automatable::xml_node_name) {
242                         set_automation_xml_state (**niter, PanAzimuthAutomation);
243
244                 } else {
245                         XMLProperty const * prop;
246
247                         /* old school (alpha1-6) XML info */
248
249                         if ((*niter)->name() == X_("azimuth")) {
250                                 prop = (*niter)->property (X_("value"));
251                                 if (prop) {
252                                         pan_azimuth_control->set_value (atof (prop->value()), Controllable::NoGroup);
253                                 }
254                         } else if ((*niter)->name() == X_("width")) {
255                                 prop = (*niter)->property (X_("value"));
256                                 if (prop) {
257                                         pan_width_control->set_value (atof (prop->value()), Controllable::NoGroup);
258                                 }
259                         } else if ((*niter)->name() == X_("elevation")) {
260                                 prop = (*niter)->property (X_("value"));
261                                 if (prop) {
262                                         pan_elevation_control->set_value (atof (prop->value()), Controllable::NoGroup);
263                                 }
264                         } else if ((*niter)->name() == X_("frontback")) {
265                                 prop = (*niter)->property (X_("value"));
266                                 if (prop) {
267                                         pan_frontback_control->set_value (atof (prop->value()), Controllable::NoGroup);
268                                 }
269                         } else if ((*niter)->name() == X_("lfe")) {
270                                 prop = (*niter)->property (X_("value"));
271                                 if (prop) {
272                                         pan_lfe_control->set_value (atof (prop->value()), Controllable::NoGroup);
273                                 }
274                         }
275                 }
276         }
277
278         _has_state = true;
279
280         return 0;
281 }
282
283 string
284 Pannable::value_as_string (boost::shared_ptr<const AutomationControl> ac) const
285 {
286         boost::shared_ptr<Panner> p = panner ();
287
288         if (p) {
289                 return p->value_as_string (ac);
290         }
291
292         return Automatable::value_as_string (ac);
293 }