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