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)
39 Speaker::move (const AngularVector& new_position)
41 _angles = new_position;
42 _angles.cartesian (_coords);
49 Speakers::Speakers (const Speakers& s)
51 _speakers = s._speakers;
54 Speakers::~Speakers ()
59 Speakers::operator= (const Speakers& s)
62 _speakers = s._speakers;
68 Speakers::dump_speakers (ostream& o)
70 for (vector<Speaker>::iterator i = _speakers.begin(); i != _speakers.end(); ++i) {
71 o << "Speaker " << (*i).id << " @ "
72 << (*i).coords().x << ", " << (*i).coords().y << ", " << (*i).coords().z
73 << " azimuth " << (*i).angles().azi
74 << " elevation " << (*i).angles().ele
75 << " distance " << (*i).angles().length
81 Speakers::clear_speakers ()
88 Speakers::add_speaker (const AngularVector& position)
90 int id = _speakers.size();
92 _speakers.push_back (Speaker (id, position));
101 Speakers::remove_speaker (int id)
103 for (vector<Speaker>::iterator i = _speakers.begin(); i != _speakers.end(); ) {
105 i = _speakers.erase (i);
113 Speakers::move_speaker (int id, const AngularVector& new_position)
115 for (vector<Speaker>::iterator i = _speakers.begin(); i != _speakers.end(); ++i) {
117 (*i).move (new_position);
125 Speakers::setup_default_speakers (uint32_t n)
127 /* default assignment of speaker position for n speakers */
133 add_speaker (AngularVector (0.0, 0.0));
137 add_speaker (AngularVector (0.0, 0.0));
138 add_speaker (AngularVector (180.0, 0,0));
142 /* top, bottom kind-of-left & bottom kind-of-right */
143 add_speaker (AngularVector (90.0, 0.0));
144 add_speaker (AngularVector (215.0, 0,0));
145 add_speaker (AngularVector (335.0, 0,0));
148 /* clockwise from top left */
149 add_speaker (AngularVector (135.0, 0.0));
150 add_speaker (AngularVector (45.0, 0.0));
151 add_speaker (AngularVector (335.0, 0.0));
152 add_speaker (AngularVector (215.0, 0.0));
157 double degree_step = 360.0 / n;
161 /* even number of speakers? make sure the top two are either side of "top".
162 otherwise, just start at the "top" (90.0 degrees) and rotate around
166 deg = 90.0 - degree_step;
170 for (i = 0; i < n; ++i, deg += degree_step) {
171 add_speaker (AngularVector (deg, 0.0));
178 Speakers::get_state ()
180 XMLNode* node = new XMLNode (X_("Speakers"));
182 LocaleGuard lg (X_("POSIX"));
184 for (vector<Speaker>::const_iterator i = _speakers.begin(); i != _speakers.end(); ++i) {
185 XMLNode* speaker = new XMLNode (X_("Speaker"));
187 snprintf (buf, sizeof (buf), "%.12g", (*i).angles().azi);
188 speaker->add_property (X_("azimuth"), buf);
189 snprintf (buf, sizeof (buf), "%.12g", (*i).angles().ele);
190 speaker->add_property (X_("elevation"), buf);
191 snprintf (buf, sizeof (buf), "%.12g", (*i).angles().length);
192 speaker->add_property (X_("distance"), buf);
194 node->add_child_nocopy (*speaker);
201 Speakers::set_state (const XMLNode& node, int /*version*/)
203 XMLNodeConstIterator i;
204 const XMLProperty* prop;
206 LocaleGuard lg (X_("POSIX"));
211 for (i = node.children().begin(); i != node.children().end(); ++i, ++n) {
212 if ((*i)->name() == X_("Speaker")) {
213 if ((prop = (*i)->property (X_("azimuth"))) == 0) {
214 warning << _("Speaker information is missing azimuth - speaker ignored") << endmsg;
217 a = atof (prop->value());
219 if ((prop = (*i)->property (X_("elevation"))) == 0) {
220 warning << _("Speaker information is missing elevation - speaker ignored") << endmsg;
223 e = atof (prop->value());
225 if ((prop = (*i)->property (X_("distance"))) == 0) {
226 warning << _("Speaker information is missing distance - speaker ignored") << endmsg;
229 d = atof (prop->value());
231 add_speaker (AngularVector (a, e, d));