remove using namespace sigc everywhere to ensure clarity over which bind/mem_fun...
[ardour.git] / libs / ardour / automation_list.cc
1 /*
2     Copyright (C) 2002 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
20 #include <set>
21 #include <climits>
22 #include <float.h>
23 #include <cmath>
24 #include <sstream>
25 #include <algorithm>
26 #include <sigc++/bind.h>
27 #include "ardour/automation_list.h"
28 #include "ardour/event_type_map.h"
29 #include "evoral/Curve.hpp"
30 #include "pbd/stacktrace.h"
31 #include "pbd/enumwriter.h"
32
33 #include "i18n.h"
34
35 using namespace std;
36 using namespace ARDOUR;
37 using namespace PBD;
38
39 sigc::signal<void,AutomationList *> AutomationList::AutomationListCreated;
40
41 #if 0
42 static void dumpit (const AutomationList& al, string prefix = "")
43 {
44         cerr << prefix << &al << endl;
45         for (AutomationList::const_iterator i = al.const_begin(); i != al.const_end(); ++i) {
46                 cerr << prefix << '\t' << (*i)->when << ',' << (*i)->value << endl;
47         }
48         cerr << "\n";
49 }
50 #endif
51
52 AutomationList::AutomationList (Evoral::Parameter id)
53         : ControlList(id)
54 {
55         _state = Off;
56         _style = Absolute;
57         _touching = false;
58
59         create_curve_if_necessary();
60
61         assert(_parameter.type() != NullAutomation);
62         AutomationListCreated(this);
63 }
64
65 AutomationList::AutomationList (const AutomationList& other)
66         : StatefulDestructible()
67         , ControlList(other)
68 {
69         _style = other._style;
70         _state = other._state;
71         _touching = other._touching;
72
73         create_curve_if_necessary();
74
75         assert(_parameter.type() != NullAutomation);
76         AutomationListCreated(this);
77 }
78
79 AutomationList::AutomationList (const AutomationList& other, double start, double end)
80         : ControlList(other, start, end)
81 {
82         _style = other._style;
83         _state = other._state;
84         _touching = other._touching;
85
86         create_curve_if_necessary();
87
88         assert(_parameter.type() != NullAutomation);
89         AutomationListCreated(this);
90 }
91
92 /** \a id is used for legacy sessions where the type is not present
93  * in or below the <AutomationList> node.  It is used if \a id is non-null.
94  */
95 AutomationList::AutomationList (const XMLNode& node, Evoral::Parameter id)
96         : ControlList(id)
97 {
98         _touching = false;
99         _state = Off;
100         _style = Absolute;
101
102         set_state (node, Stateful::loading_state_version);
103
104         if (id) {
105                 _parameter = id;
106         }
107
108         create_curve_if_necessary();
109
110         assert(_parameter.type() != NullAutomation);
111         AutomationListCreated(this);
112 }
113
114 AutomationList::~AutomationList()
115 {
116         GoingAway ();
117 }
118
119 boost::shared_ptr<Evoral::ControlList>
120 AutomationList::create(Evoral::Parameter id)
121 {
122         return boost::shared_ptr<Evoral::ControlList>(new AutomationList(id));
123 }
124
125 void
126 AutomationList::create_curve_if_necessary()
127 {
128         switch (_parameter.type()) {
129         case GainAutomation:
130         case PanAutomation:
131         case FadeInAutomation:
132         case FadeOutAutomation:
133         case EnvelopeAutomation:
134                 create_curve();
135                 break;
136         default:
137                 break;
138         }
139 }
140
141 bool
142 AutomationList::operator== (const AutomationList& other)
143 {
144         return _events == other._events;
145 }
146
147 AutomationList&
148 AutomationList::operator= (const AutomationList& other)
149 {
150         if (this != &other) {
151
152                 _events.clear ();
153
154                 for (const_iterator i = other._events.begin(); i != other._events.end(); ++i) {
155                         _events.push_back (new Evoral::ControlEvent (**i));
156                 }
157
158                 _min_yval = other._min_yval;
159                 _max_yval = other._max_yval;
160                 _max_xval = other._max_xval;
161                 _default_value = other._default_value;
162
163                 mark_dirty ();
164                 maybe_signal_changed ();
165         }
166
167         return *this;
168 }
169
170 void
171 AutomationList::maybe_signal_changed ()
172 {
173         ControlList::maybe_signal_changed ();
174
175         if (!_frozen) {
176                 StateChanged (); /* EMIT SIGNAL */
177         }
178 }
179
180 void
181 AutomationList::set_automation_state (AutoState s)
182 {
183         if (s != _state) {
184                 _state = s;
185                 automation_state_changed (); /* EMIT SIGNAL */
186         }
187 }
188
189 void
190 AutomationList::set_automation_style (AutoStyle s)
191 {
192         if (s != _style) {
193                 _style = s;
194                 automation_style_changed (); /* EMIT SIGNAL */
195         }
196 }
197
198 void
199 AutomationList::start_touch ()
200 {
201         _touching = true;
202         _new_value = true;
203 }
204
205 void
206 AutomationList::stop_touch ()
207 {
208         _touching = false;
209         _new_value = false;
210 }
211
212 void
213 AutomationList::freeze ()
214 {
215         _frozen++;
216 }
217
218 void
219 AutomationList::thaw ()
220 {
221         ControlList::thaw();
222
223         if (_changed_when_thawed) {
224                 StateChanged(); /* EMIT SIGNAL */
225         }
226 }
227
228 void
229 AutomationList::mark_dirty () const
230 {
231         ControlList::mark_dirty ();
232         Dirty (); /* EMIT SIGNAL */
233 }
234
235 XMLNode&
236 AutomationList::get_state ()
237 {
238         return state (true);
239 }
240
241 XMLNode&
242 AutomationList::state (bool full)
243 {
244         XMLNode* root = new XMLNode (X_("AutomationList"));
245         char buf[64];
246         LocaleGuard lg (X_("POSIX"));
247
248         root->add_property ("automation-id", EventTypeMap::instance().to_symbol(_parameter));
249
250         root->add_property ("id", _id.to_s());
251
252         snprintf (buf, sizeof (buf), "%.12g", _default_value);
253         root->add_property ("default", buf);
254         snprintf (buf, sizeof (buf), "%.12g", _min_yval);
255         root->add_property ("min-yval", buf);
256         snprintf (buf, sizeof (buf), "%.12g", _max_yval);
257         root->add_property ("max-yval", buf);
258         snprintf (buf, sizeof (buf), "%.12g", _max_xval);
259         root->add_property ("max-xval", buf);
260
261         root->add_property ("interpolation-style", enum_2_string (_interpolation));
262
263         if (full) {
264                 root->add_property ("state", auto_state_to_string (_state));
265         } else {
266                 /* never save anything but Off for automation state to a template */
267                 root->add_property ("state", auto_state_to_string (Off));
268         }
269
270         root->add_property ("style", auto_style_to_string (_style));
271
272         if (!_events.empty()) {
273                 root->add_child_nocopy (serialize_events());
274         }
275
276         return *root;
277 }
278
279 XMLNode&
280 AutomationList::serialize_events ()
281 {
282         XMLNode* node = new XMLNode (X_("events"));
283         stringstream str;
284
285         str.precision(15);  //10 digits is enough digits for 24 hours at 96kHz
286
287         for (iterator xx = _events.begin(); xx != _events.end(); ++xx) {
288                 str << (double) (*xx)->when;
289                 str << ' ';
290                 str <<(double) (*xx)->value;
291                 str << '\n';
292         }
293
294         /* XML is a bit wierd */
295
296         XMLNode* content_node = new XMLNode (X_("foo")); /* it gets renamed by libxml when we set content */
297         content_node->set_content (str.str());
298
299         node->add_child_nocopy (*content_node);
300
301         return *node;
302 }
303
304 int
305 AutomationList::deserialize_events (const XMLNode& node)
306 {
307         if (node.children().empty()) {
308                 return -1;
309         }
310
311         XMLNode* content_node = node.children().front();
312
313         if (content_node->content().empty()) {
314                 return -1;
315         }
316
317         freeze ();
318         clear ();
319
320         stringstream str (content_node->content());
321
322         double x;
323         double y;
324         bool ok = true;
325
326         while (str) {
327                 str >> x;
328                 if (!str) {
329                         break;
330                 }
331                 str >> y;
332                 if (!str) {
333                         ok = false;
334                         break;
335                 }
336                 fast_simple_add (x, y);
337         }
338
339         if (!ok) {
340                 clear ();
341                 error << _("automation list: cannot load coordinates from XML, all points ignored") << endmsg;
342         } else {
343                 mark_dirty ();
344                 reposition_for_rt_add (0);
345                 maybe_signal_changed ();
346         }
347
348         thaw ();
349
350         return 0;
351 }
352
353 int
354 AutomationList::set_state (const XMLNode& node, int version)
355 {
356         XMLNodeList nlist = node.children();
357         XMLNode* nsos;
358         XMLNodeIterator niter;
359         const XMLProperty* prop;
360
361         if (node.name() == X_("events")) {
362                 /* partial state setting*/
363                 return deserialize_events (node);
364         }
365
366         if (node.name() == X_("Envelope") || node.name() == X_("FadeOut") || node.name() == X_("FadeIn")) {
367
368                 if ((nsos = node.child (X_("AutomationList")))) {
369                         /* new school in old school clothing */
370                         return set_state (*nsos, version);
371                 }
372
373                 /* old school */
374
375                 const XMLNodeList& elist = node.children();
376                 XMLNodeConstIterator i;
377                 XMLProperty* prop;
378                 nframes_t x;
379                 double y;
380
381                 freeze ();
382                 clear ();
383
384                 for (i = elist.begin(); i != elist.end(); ++i) {
385
386                         if ((prop = (*i)->property ("x")) == 0) {
387                                 error << _("automation list: no x-coordinate stored for control point (point ignored)") << endmsg;
388                                 continue;
389                         }
390                         x = atoi (prop->value().c_str());
391
392                         if ((prop = (*i)->property ("y")) == 0) {
393                                 error << _("automation list: no y-coordinate stored for control point (point ignored)") << endmsg;
394                                 continue;
395                         }
396                         y = atof (prop->value().c_str());
397
398                         fast_simple_add (x, y);
399                 }
400
401                 thaw ();
402
403                 return 0;
404         }
405
406         if (node.name() != X_("AutomationList") ) {
407                 error << string_compose (_("AutomationList: passed XML node called %1, not \"AutomationList\" - ignored"), node.name()) << endmsg;
408                 return -1;
409         }
410
411         if ((prop = node.property ("id")) != 0) {
412                 _id = prop->value ();
413                 /* update session AL list */
414                 AutomationListCreated(this);
415         }
416
417         if ((prop = node.property (X_("automation-id"))) != 0){
418                 _parameter = EventTypeMap::instance().new_parameter(prop->value());
419         } else {
420                 warning << "Legacy session: automation list has no automation-id property.";
421         }
422
423         if ((prop = node.property (X_("interpolation-style"))) != 0) {
424                 _interpolation = (InterpolationStyle)string_2_enum(prop->value(), _interpolation);
425         } else {
426                 _interpolation = Linear;
427         }
428
429         if ((prop = node.property (X_("default"))) != 0){
430                 _default_value = atof (prop->value().c_str());
431         } else {
432                 _default_value = 0.0;
433         }
434
435         if ((prop = node.property (X_("style"))) != 0) {
436                 _style = string_to_auto_style (prop->value());
437         } else {
438                 _style = Absolute;
439         }
440
441         if ((prop = node.property (X_("state"))) != 0) {
442                 _state = string_to_auto_state (prop->value());
443         } else {
444                 _state = Off;
445         }
446
447         if ((prop = node.property (X_("min_yval"))) != 0) {
448                 _min_yval = atof (prop->value ().c_str());
449         } else {
450                 _min_yval = FLT_MIN;
451         }
452
453         if ((prop = node.property (X_("max_yval"))) != 0) {
454                 _max_yval = atof (prop->value ().c_str());
455         } else {
456                 _max_yval = FLT_MAX;
457         }
458
459         if ((prop = node.property (X_("max_xval"))) != 0) {
460                 _max_xval = atof (prop->value ().c_str());
461         } else {
462                 _max_xval = 0; // means "no limit ;
463         }
464
465         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
466                 if ((*niter)->name() == X_("events")) {
467                         deserialize_events (*(*niter));
468                 }
469         }
470
471         return 0;
472 }
473