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