merge 3.0-panexp (pan experiments) branch, revisions 8534-8585 into 3.0, thus ending...
[ardour2.git] / libs / ardour / pannable.cc
blob4243ab45dfe07e2856a42c43d127810729c7dd91
1 /*
2 Copyright (C) 2011 Paul Davis
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.
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.
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.
20 #include "pbd/error.h"
21 #include "pbd/convert.h"
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"
29 using namespace PBD;
30 using namespace ARDOUR;
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)
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);
51 /* all controls change state together */
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));
60 void
61 Pannable::control_auto_state_changed (AutoState new_state)
63 if (_responding_to_control_auto_state_change) {
64 return;
67 _responding_to_control_auto_state_change++;
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);
75 _responding_to_control_auto_state_change--;
77 _auto_state = new_state;
78 automation_state_changed (new_state); /* EMIT SIGNAL */
81 void
82 Pannable::set_panner (boost::shared_ptr<Panner> p)
84 _panner = p;
87 void
88 Pannable::set_automation_state (AutoState state)
90 if (state != _auto_state) {
91 _auto_state = state;
93 const Controls& c (controls());
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);
102 session().set_dirty ();
103 automation_state_changed (_auto_state);
107 void
108 Pannable::set_automation_style (AutoStyle style)
110 if (style != _auto_style) {
111 _auto_style = style;
113 const Controls& c (controls());
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);
122 session().set_dirty ();
123 automation_style_changed ();
127 void
128 Pannable::start_touch (double when)
130 const Controls& c (controls());
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);
138 g_atomic_int_set (&_touching, 1);
141 void
142 Pannable::stop_touch (bool mark, double when)
144 const Controls& c (controls());
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);
152 g_atomic_int_set (&_touching, 0);
155 XMLNode&
156 Pannable::get_state ()
158 return state (true);
161 XMLNode&
162 Pannable::state (bool full)
164 XMLNode* node = new XMLNode (X_("Pannable"));
165 XMLNode* control_node;
166 char buf[32];
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);
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);
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);
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);
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);
193 node->add_child_nocopy (get_automation_xml_state ());
195 return *node;
199 Pannable::set_state (const XMLNode& root, int /*version - not used*/)
201 if (root.name() != X_("Pannable")) {
202 warning << string_compose (_("Pannable given XML data for %1 - ignored"), root.name()) << endmsg;
203 return -1;
206 XMLNodeList nlist;
207 XMLNodeConstIterator niter;
208 const XMLProperty *prop;
210 nlist = root.children();
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()));
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()));
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()));
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()));
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()));
238 } else if ((*niter)->name() == Automatable::xml_node_name) {
239 set_automation_xml_state (**niter, PanAzimuthAutomation);
243 _has_state = true;
245 return 0;