Use ISC license for RDFF (same idea, MIT style, just prettier).
[ardour.git] / libs / ardour / speakers.cc
1 /*
2     Copyright (C) 2010 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 #include "pbd/error.h"
20 #include "pbd/convert.h"
21 #include "pbd/locale_guard.h"
22
23 #include "ardour/speaker.h"
24 #include "ardour/speakers.h"
25
26 #include "i18n.h"
27
28 using namespace ARDOUR;
29 using namespace PBD;
30 using namespace std;
31
32 Speaker::Speaker (int i, const AngularVector& position)
33         : id (i)
34 {
35         move (position);
36 }
37
38 Speaker::Speaker (Speaker const & o)
39         : id (o.id)
40         , _coords (o._coords)
41         , _angles (o._angles)
42 {
43         
44 }
45
46 Speaker &
47 Speaker::operator= (Speaker const & o)
48 {
49         if (&o == this) {
50                 return *this;
51         }
52
53         id = o.id;
54         _coords = o._coords;
55         _angles = o._angles;
56
57         return *this;
58 }
59
60 void
61 Speaker::move (const AngularVector& new_position)
62 {
63         _angles = new_position;
64         _angles.cartesian (_coords);
65
66         PositionChanged (); /* EMIT SIGNAL */
67 }
68
69 Speakers::Speakers ()
70 {
71 }
72
73 Speakers::Speakers (const Speakers& s)
74 {
75         _speakers = s._speakers;
76 }
77
78 Speakers::~Speakers ()
79 {
80 }
81
82 Speakers&
83 Speakers::operator= (const Speakers& s)
84 {
85         if (&s != this) {
86                 _speakers = s._speakers;
87         }
88         return *this;
89 }
90
91 void
92 Speakers::dump_speakers (ostream& o)
93 {
94         for (vector<Speaker>::iterator i = _speakers.begin(); i != _speakers.end(); ++i) {
95                 o << "Speaker " << (*i).id << " @ " 
96                   << (*i).coords().x << ", " << (*i).coords().y << ", " << (*i).coords().z
97                   << " azimuth " << (*i).angles().azi
98                   << " elevation " << (*i).angles().ele
99                   << " distance " << (*i).angles().length
100                   << endl;
101         }
102 }
103
104 void
105 Speakers::clear_speakers ()
106 {
107         _speakers.clear ();
108         update ();
109 }
110
111 int 
112 Speakers::add_speaker (const AngularVector& position)
113 {
114         int id = _speakers.size();
115
116         _speakers.push_back (Speaker (id, position));
117         update ();
118
119         Changed ();
120
121         return id;
122 }        
123
124 void
125 Speakers::remove_speaker (int id)
126 {
127         for (vector<Speaker>::iterator i = _speakers.begin(); i != _speakers.end(); ++i) {
128                 if (i->id == id) {
129                         i = _speakers.erase (i);
130                         update ();
131                         break;
132                 }
133         }
134 }
135
136 void
137 Speakers::move_speaker (int id, const AngularVector& new_position)
138 {
139         for (vector<Speaker>::iterator i = _speakers.begin(); i != _speakers.end(); ++i) {
140                 if ((*i).id == id) {
141                         (*i).move (new_position);
142                         update ();
143                         break;
144                 }
145         }
146 }
147
148 void
149 Speakers::setup_default_speakers (uint32_t n)
150 {
151         /* default assignment of speaker position for n speakers */
152
153         assert (n>0);
154
155         switch (n) {
156         case 1:
157                 add_speaker (AngularVector (0.0, 0.0));
158                 break;
159
160         case 2:
161                 add_speaker (AngularVector (0.0, 0.0));
162                 add_speaker (AngularVector (180.0, 0,0));
163                 break;
164
165         case 3:
166                 /* top, bottom kind-of-left & bottom kind-of-right */
167                 add_speaker (AngularVector (90.0, 0.0));
168                 add_speaker (AngularVector (215.0, 0,0));
169                 add_speaker (AngularVector (335.0, 0,0));
170                 break;
171         case 4:
172                 /* clockwise from top left */
173                 add_speaker (AngularVector (135.0, 0.0));
174                 add_speaker (AngularVector (45.0, 0.0));
175                 add_speaker (AngularVector (335.0, 0.0));
176                 add_speaker (AngularVector (215.0, 0.0));
177                 break;
178
179         default: 
180         {
181                 double degree_step = 360.0 / n;
182                 double deg;
183                 uint32_t i;
184
185                 /* even number of speakers? make sure the top two are either side of "top".
186                    otherwise, just start at the "top" (90.0 degrees) and rotate around
187                 */
188
189                 if (n % 2) {
190                         deg = 90.0 - degree_step;
191                 } else {
192                         deg = 90.0;
193                 }
194                 for (i = 0; i < n; ++i, deg += degree_step) {
195                         add_speaker (AngularVector (deg, 0.0));
196                 }
197         }
198         }
199 }
200         
201 XMLNode&
202 Speakers::get_state ()
203 {
204         XMLNode* node = new XMLNode (X_("Speakers"));
205         char buf[32];
206         LocaleGuard lg (X_("POSIX"));
207
208         for (vector<Speaker>::const_iterator i = _speakers.begin(); i != _speakers.end(); ++i) {
209                 XMLNode* speaker = new XMLNode (X_("Speaker"));
210
211                 snprintf (buf, sizeof (buf), "%.12g", (*i).angles().azi);
212                 speaker->add_property (X_("azimuth"), buf);
213                 snprintf (buf, sizeof (buf), "%.12g", (*i).angles().ele);
214                 speaker->add_property (X_("elevation"), buf);
215                 snprintf (buf, sizeof (buf), "%.12g", (*i).angles().length);
216                 speaker->add_property (X_("distance"), buf);
217
218                 node->add_child_nocopy (*speaker);
219         }
220         
221         return *node;
222 }
223
224 int
225 Speakers::set_state (const XMLNode& node, int /*version*/)
226 {
227         XMLNodeConstIterator i;
228         const XMLProperty* prop;
229         double a, e, d;
230         LocaleGuard lg (X_("POSIX"));
231         int n = 0;
232
233         _speakers.clear ();
234
235         for (i = node.children().begin(); i != node.children().end(); ++i, ++n) {
236                 if ((*i)->name() == X_("Speaker")) {
237                         if ((prop = (*i)->property (X_("azimuth"))) == 0) {
238                                 warning << _("Speaker information is missing azimuth - speaker ignored") << endmsg;
239                                 continue;
240                         }
241                         a = atof (prop->value());
242
243                         if ((prop = (*i)->property (X_("elevation"))) == 0) {
244                                 warning << _("Speaker information is missing elevation - speaker ignored") << endmsg;
245                                 continue;
246                         }
247                         e = atof (prop->value());
248                                             
249                         if ((prop = (*i)->property (X_("distance"))) == 0) {
250                                 warning << _("Speaker information is missing distance - speaker ignored") << endmsg;
251                                 continue;
252                         }
253                         d = atof (prop->value());
254
255                         add_speaker (AngularVector (a, e, d));
256                 }
257         }
258
259         update ();
260         
261         return 0;
262 }