merge 3.0-panexp (pan experiments) branch, revisions 8534-8585 into 3.0, thus ending...
[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 void
39 Speaker::move (const AngularVector& new_position)
40 {
41         _angles = new_position;
42         _angles.cartesian (_coords);
43 }
44
45 Speakers::Speakers ()
46 {
47 }
48
49 Speakers::~Speakers ()
50 {
51 }
52
53 void
54 Speakers::dump_speakers (ostream& o)
55 {
56         for (vector<Speaker>::iterator i = _speakers.begin(); i != _speakers.end(); ++i) {
57                 o << "Speaker " << (*i).id << " @ " 
58                   << (*i).coords().x << ", " << (*i).coords().y << ", " << (*i).coords().z
59                   << " azimuth " << (*i).angles().azi
60                   << " elevation " << (*i).angles().ele
61                   << " distance " << (*i).angles().length
62                   << endl;
63         }
64 }
65
66 void
67 Speakers::clear_speakers ()
68 {
69         _speakers.clear ();
70         update ();
71 }
72
73 int 
74 Speakers::add_speaker (const AngularVector& position)
75 {
76         int id = _speakers.size();
77
78         cerr << "Added speaker " << id << " at " << position.azi << " /= " << position.ele << endl;
79
80         _speakers.push_back (Speaker (id, position));
81         update ();
82
83         dump_speakers (cerr);
84         Changed ();
85
86         return id;
87 }        
88
89 void
90 Speakers::remove_speaker (int id)
91 {
92         for (vector<Speaker>::iterator i = _speakers.begin(); i != _speakers.end(); ) {
93                 if ((*i).id == id) {
94                         i = _speakers.erase (i);
95                         update ();
96                         break;
97                 } 
98         }
99 }
100
101 void
102 Speakers::move_speaker (int id, const AngularVector& new_position)
103 {
104         for (vector<Speaker>::iterator i = _speakers.begin(); i != _speakers.end(); ++i) {
105                 if ((*i).id == id) {
106                         (*i).move (new_position);
107                         update ();
108                         break;
109                 }
110         }
111 }
112
113 void
114 Speakers::setup_default_speakers (uint32_t n)
115 {
116         /* default assignment of speaker position for n speakers */
117
118         assert (n>0);
119
120         switch (n) {
121         case 1:
122                 add_speaker (AngularVector (0.0, 0.0));
123                 break;
124
125         case 2:
126                 add_speaker (AngularVector (0.0, 0.0));
127                 add_speaker (AngularVector (180.0, 0,0));
128                 break;
129
130         case 3:
131                 /* top, bottom kind-of-left & bottom kind-of-right */
132                 add_speaker (AngularVector (90.0, 0.0));
133                 add_speaker (AngularVector (215.0, 0,0));
134                 add_speaker (AngularVector (335.0, 0,0));
135                 break;
136         case 4:
137                 /* clockwise from top left */
138                 add_speaker (AngularVector (135.0, 0.0));
139                 add_speaker (AngularVector (45.0, 0.0));
140                 add_speaker (AngularVector (335.0, 0.0));
141                 add_speaker (AngularVector (215.0, 0.0));
142                 break;
143
144         default: 
145         {
146                 double degree_step = 360.0 / n;
147                 double deg;
148                 uint32_t i;
149
150                 /* even number of speakers? make sure the top two are either side of "top".
151                    otherwise, just start at the "top" (90.0 degrees) and rotate around
152                 */
153
154                 if (n % 2) {
155                         deg = 90.0 - degree_step;
156                 } else {
157                         deg = 90.0;
158                 }
159                 for (i = 0; i < n; ++i, deg += degree_step) {
160                         add_speaker (AngularVector (deg, 0.0));
161                 }
162         }
163         }
164 }
165         
166 XMLNode&
167 Speakers::get_state ()
168 {
169         XMLNode* node = new XMLNode (X_("Speakers"));
170         char buf[32];
171         LocaleGuard lg (X_("POSIX"));
172
173         for (vector<Speaker>::const_iterator i = _speakers.begin(); i != _speakers.end(); ++i) {
174                 XMLNode* speaker = new XMLNode (X_("Speaker"));
175
176                 snprintf (buf, sizeof (buf), "%.12g", (*i).angles().azi);
177                 speaker->add_property (X_("azimuth"), buf);
178                 snprintf (buf, sizeof (buf), "%.12g", (*i).angles().ele);
179                 speaker->add_property (X_("elevation"), buf);
180                 snprintf (buf, sizeof (buf), "%.12g", (*i).angles().length);
181                 speaker->add_property (X_("distance"), buf);
182
183                 node->add_child_nocopy (*speaker);
184         }
185         
186         return *node;
187 }
188
189 int
190 Speakers::set_state (const XMLNode& node, int /*version*/)
191 {
192         XMLNodeConstIterator i;
193         const XMLProperty* prop;
194         double a, e, d;
195         LocaleGuard lg (X_("POSIX"));
196         int n = 0;
197
198         _speakers.clear ();
199
200         for (i = node.children().begin(); i != node.children().end(); ++i, ++n) {
201                 if ((*i)->name() == X_("Speaker")) {
202                         if ((prop = (*i)->property (X_("azimuth"))) == 0) {
203                                 warning << _("Speaker information is missing azimuth - speaker ignored") << endmsg;
204                                 continue;
205                         }
206                         a = atof (prop->value());
207
208                         if ((prop = (*i)->property (X_("elevation"))) == 0) {
209                                 warning << _("Speaker information is missing elevation - speaker ignored") << endmsg;
210                                 continue;
211                         }
212                         e = atof (prop->value());
213                                             
214                         if ((prop = (*i)->property (X_("distance"))) == 0) {
215                                 warning << _("Speaker information is missing distance - speaker ignored") << endmsg;
216                                 continue;
217                         }
218                         d = atof (prop->value());
219
220                         add_speaker (AngularVector (a, e, d));
221                 }
222         }
223
224         update ();
225         
226         return 0;
227 }