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