Merge branch 'cairocanvas'
[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.begin(); i != al.end(); ++i) {
45                 cerr << prefix << '\t' << (*i)->when << ',' << (*i)->value << endl;
46         }
47         cerr << "\n";
48 }
49 #endif
50 AutomationList::AutomationList (Evoral::Parameter id)
51         : ControlList(id)
52 {
53         _state = Off;
54         _style = Absolute;
55         g_atomic_int_set (&_touching, 0);
56
57         create_curve_if_necessary();
58
59         assert(_parameter.type() != NullAutomation);
60         AutomationListCreated(this);
61 }
62
63 AutomationList::AutomationList (const AutomationList& other)
64         : StatefulDestructible()
65         , ControlList(other)
66 {
67         _style = other._style;
68         _state = other._state;
69         g_atomic_int_set (&_touching, other.touching());
70
71         create_curve_if_necessary();
72
73         assert(_parameter.type() != NullAutomation);
74         AutomationListCreated(this);
75 }
76
77 AutomationList::AutomationList (const AutomationList& other, double start, double end)
78         : ControlList(other, start, end)
79 {
80         _style = other._style;
81         _state = other._state;
82         g_atomic_int_set (&_touching, other.touching());
83
84         create_curve_if_necessary();
85
86         assert(_parameter.type() != NullAutomation);
87         AutomationListCreated(this);
88 }
89
90 /** @param id is used for legacy sessions where the type is not present
91  * in or below the AutomationList node.  It is used if @param id is non-null.
92  */
93 AutomationList::AutomationList (const XMLNode& node, Evoral::Parameter id)
94         : ControlList(id)
95 {
96         g_atomic_int_set (&_touching, 0);
97         _state = Off;
98         _style = Absolute;
99
100         set_state (node, Stateful::loading_state_version);
101
102         if (id) {
103                 _parameter = id;
104         }
105
106         create_curve_if_necessary();
107
108         assert(_parameter.type() != NullAutomation);
109         AutomationListCreated(this);
110 }
111
112 AutomationList::~AutomationList()
113 {
114 }
115
116 boost::shared_ptr<Evoral::ControlList>
117 AutomationList::create(Evoral::Parameter id)
118 {
119         return boost::shared_ptr<Evoral::ControlList>(new AutomationList(id));
120 }
121
122 void
123 AutomationList::create_curve_if_necessary()
124 {
125         switch (_parameter.type()) {
126         case GainAutomation:
127         case PanAzimuthAutomation:
128         case PanElevationAutomation:
129         case PanWidthAutomation:
130         case FadeInAutomation:
131         case FadeOutAutomation:
132         case EnvelopeAutomation:
133                 create_curve();
134                 break;
135         default:
136                 break;
137         }
138 }
139
140 AutomationList&
141 AutomationList::operator= (const AutomationList& other)
142 {
143         if (this != &other) {
144
145
146                 ControlList::operator= (other);
147                 _state = other._state;
148                 _style = other._style;
149                 _touching = other._touching;
150
151                 mark_dirty ();
152                 maybe_signal_changed ();
153         }
154
155         return *this;
156 }
157
158 void
159 AutomationList::maybe_signal_changed ()
160 {
161         ControlList::maybe_signal_changed ();
162
163         if (!ControlList::frozen()) {
164                 StateChanged (); /* EMIT SIGNAL */
165         }
166 }
167
168 void
169 AutomationList::set_automation_state (AutoState s)
170 {
171         if (s != _state) {
172                 _state = s;
173                 automation_state_changed (s); /* EMIT SIGNAL */
174         }
175 }
176
177 void
178 AutomationList::set_automation_style (AutoStyle s)
179 {
180         if (s != _style) {
181                 _style = s;
182                 automation_style_changed (); /* EMIT SIGNAL */
183         }
184 }
185
186 void
187 AutomationList::start_touch (double when)
188 {
189         if (_state == Touch) {
190                 start_write_pass (when);
191         }
192
193         g_atomic_int_set (&_touching, 1);
194 }
195
196 void
197 AutomationList::stop_touch (bool mark, double)
198 {
199         if (g_atomic_int_get (&_touching) == 0) {
200                 /* this touch has already been stopped (probably by Automatable::transport_stopped),
201                    so we've nothing to do.
202                 */
203                 return;
204         }
205
206         g_atomic_int_set (&_touching, 0);
207
208         if (_state == Touch) {
209
210                 if (mark) {
211                         
212                         /* XXX need to mark the last added point with the
213                          * current time 
214                          */
215                 }
216         }
217 }
218
219 void
220 AutomationList::thaw ()
221 {
222         ControlList::thaw();
223
224         if (_changed_when_thawed) {
225                 _changed_when_thawed = false;
226                 StateChanged(); /* EMIT SIGNAL */
227         }
228 }
229
230 XMLNode&
231 AutomationList::get_state ()
232 {
233         return state (true);
234 }
235
236 XMLNode&
237 AutomationList::state (bool full)
238 {
239         XMLNode* root = new XMLNode (X_("AutomationList"));
240         char buf[64];
241         LocaleGuard lg (X_("POSIX"));
242
243         root->add_property ("automation-id", EventTypeMap::instance().to_symbol(_parameter));
244
245         root->add_property ("id", id().to_s());
246
247         snprintf (buf, sizeof (buf), "%.12g", _default_value);
248         root->add_property ("default", buf);
249         snprintf (buf, sizeof (buf), "%.12g", _min_yval);
250         root->add_property ("min-yval", buf);
251         snprintf (buf, sizeof (buf), "%.12g", _max_yval);
252         root->add_property ("max-yval", buf);
253
254         root->add_property ("interpolation-style", enum_2_string (_interpolation));
255
256         if (full) {
257                 /* never serialize state with Write enabled - too dangerous
258                    for the user's data
259                 */
260                 if (_state != Write) {
261                         root->add_property ("state", auto_state_to_string (_state));
262                 } else {
263                         root->add_property ("state", auto_state_to_string (Off));
264                 }
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         ControlList::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                 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         LocaleGuard lg (X_("POSIX"));
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                 pframes_t x;
379                 double y;
380
381                 ControlList::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 (set_id (node)) {
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." << endmsg;
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                 if (_state == Write) {
443                         _state = Off;
444                 }
445                 automation_state_changed(_state);
446         } else {
447                 _state = Off;
448         }
449
450         if ((prop = node.property (X_("min-yval"))) != 0) {
451                 _min_yval = atof (prop->value ().c_str());
452         } else {
453                 _min_yval = FLT_MIN;
454         }
455
456         if ((prop = node.property (X_("max-yval"))) != 0) {
457                 _max_yval = atof (prop->value ().c_str());
458         } else {
459                 _max_yval = FLT_MAX;
460         }
461
462         bool have_events = false;
463
464         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
465                 if ((*niter)->name() == X_("events")) {
466                         deserialize_events (*(*niter));
467                         have_events = true;
468                 }
469         }
470
471         if (!have_events) {
472                 /* there was no Events child node; clear any current events */
473                 freeze ();
474                 clear ();
475                 mark_dirty ();
476                 maybe_signal_changed ();
477                 thaw ();
478         }
479
480         return 0;
481 }
482
483 bool
484 AutomationList::operator!= (AutomationList const & other) const
485 {
486         return (
487                 static_cast<ControlList const &> (*this) != static_cast<ControlList const &> (other) ||
488                 _state != other._state ||
489                 _style != other._style ||
490                 _touching != other._touching
491                 );
492 }
493
494 PBD::PropertyBase *
495 AutomationListProperty::clone () const
496 {
497         return new AutomationListProperty (
498                 this->property_id(),
499                 boost::shared_ptr<AutomationList> (new AutomationList (*this->_old.get())),
500                 boost::shared_ptr<AutomationList> (new AutomationList (*this->_current.get()))
501                 );
502 }
503