2 Copyright (C) 2010 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.
19 #include "pbd/error.h"
20 #include "pbd/convert.h"
21 #include "pbd/locale_guard.h"
23 #include "ardour/speaker.h"
24 #include "ardour/speakers.h"
28 using namespace ARDOUR;
32 Speaker::Speaker (int i, const AngularVector& position)
38 Speaker::Speaker (Speaker const & o)
47 Speaker::operator= (Speaker const & o)
61 Speaker::move (const AngularVector& new_position)
63 _angles = new_position;
64 _angles.cartesian (_coords);
66 PositionChanged (); /* EMIT SIGNAL */
73 Speakers::Speakers (const Speakers& s)
76 _speakers = s._speakers;
79 Speakers::~Speakers ()
84 Speakers::operator= (const Speakers& s)
87 _speakers = s._speakers;
93 Speakers::dump_speakers (ostream& o)
95 for (vector<Speaker>::iterator i = _speakers.begin(); i != _speakers.end(); ++i) {
96 o << "Speaker " << (*i).id << " @ "
97 << (*i).coords().x << ", " << (*i).coords().y << ", " << (*i).coords().z
98 << " azimuth " << (*i).angles().azi
99 << " elevation " << (*i).angles().ele
100 << " distance " << (*i).angles().length
106 Speakers::clear_speakers ()
113 Speakers::add_speaker (const AngularVector& position)
115 int id = _speakers.size();
117 _speakers.push_back (Speaker (id, position));
126 Speakers::remove_speaker (int id)
128 for (vector<Speaker>::iterator i = _speakers.begin(); i != _speakers.end(); ++i) {
130 i = _speakers.erase (i);
138 Speakers::move_speaker (int id, const AngularVector& new_position)
140 for (vector<Speaker>::iterator i = _speakers.begin(); i != _speakers.end(); ++i) {
142 (*i).move (new_position);
150 Speakers::setup_default_speakers (uint32_t n)
152 /* default assignment of speaker position for n speakers */
158 add_speaker (AngularVector (0.0, 0.0));
162 add_speaker (AngularVector (0.0, 0.0));
163 add_speaker (AngularVector (180.0, 0,0));
167 /* top, bottom kind-of-left & bottom kind-of-right */
168 add_speaker (AngularVector (90.0, 0.0));
169 add_speaker (AngularVector (215.0, 0,0));
170 add_speaker (AngularVector (335.0, 0,0));
173 /* clockwise from top left */
174 add_speaker (AngularVector (135.0, 0.0));
175 add_speaker (AngularVector (45.0, 0.0));
176 add_speaker (AngularVector (335.0, 0.0));
177 add_speaker (AngularVector (215.0, 0.0));
182 double degree_step = 360.0 / n;
186 /* even number of speakers? make sure the top two are either side of "top".
187 otherwise, just start at the "top" (90.0 degrees) and rotate around
191 deg = 90.0 - degree_step;
195 for (i = 0; i < n; ++i, deg += degree_step) {
196 add_speaker (AngularVector (deg, 0.0));
203 Speakers::get_state ()
205 XMLNode* node = new XMLNode (X_("Speakers"));
207 LocaleGuard lg (X_("POSIX"));
209 for (vector<Speaker>::const_iterator i = _speakers.begin(); i != _speakers.end(); ++i) {
210 XMLNode* speaker = new XMLNode (X_("Speaker"));
212 snprintf (buf, sizeof (buf), "%.12g", (*i).angles().azi);
213 speaker->add_property (X_("azimuth"), buf);
214 snprintf (buf, sizeof (buf), "%.12g", (*i).angles().ele);
215 speaker->add_property (X_("elevation"), buf);
216 snprintf (buf, sizeof (buf), "%.12g", (*i).angles().length);
217 speaker->add_property (X_("distance"), buf);
219 node->add_child_nocopy (*speaker);
226 Speakers::set_state (const XMLNode& node, int /*version*/)
228 XMLNodeConstIterator i;
229 const XMLProperty* prop;
231 LocaleGuard lg (X_("POSIX"));
236 for (i = node.children().begin(); i != node.children().end(); ++i, ++n) {
237 if ((*i)->name() == X_("Speaker")) {
238 if ((prop = (*i)->property (X_("azimuth"))) == 0) {
239 warning << _("Speaker information is missing azimuth - speaker ignored") << endmsg;
242 a = atof (prop->value());
244 if ((prop = (*i)->property (X_("elevation"))) == 0) {
245 warning << _("Speaker information is missing elevation - speaker ignored") << endmsg;
248 e = atof (prop->value());
250 if ((prop = (*i)->property (X_("distance"))) == 0) {
251 warning << _("Speaker information is missing distance - speaker ignored") << endmsg;
254 d = atof (prop->value());
256 add_speaker (AngularVector (a, e, d));