Merge branch 'master' into saveas
[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 "ardour/parameter_descriptor.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 PBD::Signal1<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.begin(); i != al.end(); ++i) {
46                 cerr << prefix << '\t' << (*i)->when << ',' << (*i)->value << endl;
47         }
48         cerr << "\n";
49 }
50 #endif
51 AutomationList::AutomationList (const Evoral::Parameter& id, const Evoral::ParameterDescriptor& desc)
52         : ControlList(id, desc)
53 {
54         _state = Off;
55         _style = Absolute;
56         g_atomic_int_set (&_touching, 0);
57
58         create_curve_if_necessary();
59
60         assert(_parameter.type() != NullAutomation);
61         AutomationListCreated(this);
62 }
63
64 AutomationList::AutomationList (const Evoral::Parameter& id)
65         : ControlList(id, ARDOUR::ParameterDescriptor(id))
66 {
67         _state = Off;
68         _style = Absolute;
69         g_atomic_int_set (&_touching, 0);
70
71         create_curve_if_necessary();
72
73         assert(_parameter.type() != NullAutomation);
74         AutomationListCreated(this);
75 }
76
77 AutomationList::AutomationList (const AutomationList& other)
78         : StatefulDestructible()
79         , ControlList(other)
80 {
81         _style = other._style;
82         _state = other._state;
83         g_atomic_int_set (&_touching, other.touching());
84
85         create_curve_if_necessary();
86
87         assert(_parameter.type() != NullAutomation);
88         AutomationListCreated(this);
89 }
90
91 AutomationList::AutomationList (const AutomationList& other, double start, double end)
92         : ControlList(other, start, end)
93 {
94         _style = other._style;
95         _state = other._state;
96         g_atomic_int_set (&_touching, other.touching());
97
98         create_curve_if_necessary();
99
100         assert(_parameter.type() != NullAutomation);
101         AutomationListCreated(this);
102 }
103
104 /** @param id is used for legacy sessions where the type is not present
105  * in or below the AutomationList node.  It is used if @param id is non-null.
106  */
107 AutomationList::AutomationList (const XMLNode& node, Evoral::Parameter id)
108         : ControlList(id, ARDOUR::ParameterDescriptor(id))
109 {
110         g_atomic_int_set (&_touching, 0);
111         _state = Off;
112         _style = Absolute;
113
114         set_state (node, Stateful::loading_state_version);
115
116         if (id) {
117                 _parameter = id;
118         }
119
120         create_curve_if_necessary();
121
122         assert(_parameter.type() != NullAutomation);
123         AutomationListCreated(this);
124 }
125
126 AutomationList::~AutomationList()
127 {
128 }
129
130 boost::shared_ptr<Evoral::ControlList>
131 AutomationList::create(const Evoral::Parameter&           id,
132                        const Evoral::ParameterDescriptor& desc)
133 {
134         return boost::shared_ptr<Evoral::ControlList>(new AutomationList(id, desc));
135 }
136
137 void
138 AutomationList::create_curve_if_necessary()
139 {
140         switch (_parameter.type()) {
141         case GainAutomation:
142         case PanAzimuthAutomation:
143         case PanElevationAutomation:
144         case PanWidthAutomation:
145         case FadeInAutomation:
146         case FadeOutAutomation:
147         case EnvelopeAutomation:
148                 create_curve();
149                 break;
150         default:
151                 break;
152         }
153 }
154
155 AutomationList&
156 AutomationList::operator= (const AutomationList& other)
157 {
158         if (this != &other) {
159
160
161                 ControlList::operator= (other);
162                 _state = other._state;
163                 _style = other._style;
164                 _touching = other._touching;
165
166                 mark_dirty ();
167                 maybe_signal_changed ();
168         }
169
170         return *this;
171 }
172
173 void
174 AutomationList::maybe_signal_changed ()
175 {
176         ControlList::maybe_signal_changed ();
177
178         if (!ControlList::frozen()) {
179                 StateChanged (); /* EMIT SIGNAL */
180         }
181 }
182
183 void
184 AutomationList::set_automation_state (AutoState s)
185 {
186         if (s != _state) {
187                 _state = s;
188                 automation_state_changed (s); /* EMIT SIGNAL */
189         }
190 }
191
192 void
193 AutomationList::set_automation_style (AutoStyle s)
194 {
195         if (s != _style) {
196                 _style = s;
197                 automation_style_changed (); /* EMIT SIGNAL */
198         }
199 }
200
201 void
202 AutomationList::start_touch (double when)
203 {
204         if (_state == Touch) {
205                 start_write_pass (when);
206         }
207
208         g_atomic_int_set (&_touching, 1);
209 }
210
211 void
212 AutomationList::stop_touch (bool mark, double)
213 {
214         if (g_atomic_int_get (&_touching) == 0) {
215                 /* this touch has already been stopped (probably by Automatable::transport_stopped),
216                    so we've nothing to do.
217                 */
218                 return;
219         }
220
221         g_atomic_int_set (&_touching, 0);
222
223         if (_state == Touch) {
224
225                 if (mark) {
226                         
227                         /* XXX need to mark the last added point with the
228                          * current time 
229                          */
230                 }
231         }
232 }
233
234 void
235 AutomationList::thaw ()
236 {
237         ControlList::thaw();
238
239         if (_changed_when_thawed) {
240                 _changed_when_thawed = false;
241                 StateChanged(); /* EMIT SIGNAL */
242         }
243 }
244
245 XMLNode&
246 AutomationList::get_state ()
247 {
248         return state (true);
249 }
250
251 XMLNode&
252 AutomationList::state (bool full)
253 {
254         XMLNode* root = new XMLNode (X_("AutomationList"));
255         char buf[64];
256         LocaleGuard lg (X_("C"));
257
258         root->add_property ("automation-id", EventTypeMap::instance().to_symbol(_parameter));
259
260         root->add_property ("id", id().to_s());
261
262         snprintf (buf, sizeof (buf), "%.12g", _default_value);
263         root->add_property ("default", buf);
264         snprintf (buf, sizeof (buf), "%.12g", _min_yval);
265         root->add_property ("min-yval", buf);
266         snprintf (buf, sizeof (buf), "%.12g", _max_yval);
267         root->add_property ("max-yval", buf);
268
269         root->add_property ("interpolation-style", enum_2_string (_interpolation));
270
271         if (full) {
272                 /* never serialize state with Write enabled - too dangerous
273                    for the user's data
274                 */
275                 if (_state != Write) {
276                         root->add_property ("state", auto_state_to_string (_state));
277                 } else {
278                         root->add_property ("state", auto_state_to_string (Off));
279                 }
280         } else {
281                 /* never save anything but Off for automation state to a template */
282                 root->add_property ("state", auto_state_to_string (Off));
283         }
284
285         root->add_property ("style", auto_style_to_string (_style));
286
287         if (!_events.empty()) {
288                 root->add_child_nocopy (serialize_events());
289         }
290
291         return *root;
292 }
293
294 XMLNode&
295 AutomationList::serialize_events ()
296 {
297         XMLNode* node = new XMLNode (X_("events"));
298         stringstream str;
299
300         str.precision(15);  //10 digits is enough digits for 24 hours at 96kHz
301
302         for (iterator xx = _events.begin(); xx != _events.end(); ++xx) {
303                 str << (double) (*xx)->when;
304                 str << ' ';
305                 str <<(double) (*xx)->value;
306                 str << '\n';
307         }
308
309         /* XML is a bit wierd */
310
311         XMLNode* content_node = new XMLNode (X_("foo")); /* it gets renamed by libxml when we set content */
312         content_node->set_content (str.str());
313
314         node->add_child_nocopy (*content_node);
315
316         return *node;
317 }
318
319 int
320 AutomationList::deserialize_events (const XMLNode& node)
321 {
322         if (node.children().empty()) {
323                 return -1;
324         }
325
326         XMLNode* content_node = node.children().front();
327
328         if (content_node->content().empty()) {
329                 return -1;
330         }
331
332         ControlList::freeze ();
333         clear ();
334
335         stringstream str (content_node->content());
336
337         double x;
338         double y;
339         bool ok = true;
340
341         while (str) {
342                 str >> x;
343                 if (!str) {
344                         break;
345                 }
346                 str >> y;
347                 if (!str) {
348                         ok = false;
349                         break;
350                 }
351                 fast_simple_add (x, y);
352         }
353
354         if (!ok) {
355                 clear ();
356                 error << _("automation list: cannot load coordinates from XML, all points ignored") << endmsg;
357         } else {
358                 mark_dirty ();
359                 maybe_signal_changed ();
360         }
361
362         thaw ();
363
364         return 0;
365 }
366
367 int
368 AutomationList::set_state (const XMLNode& node, int version)
369 {
370         LocaleGuard lg (X_("C"));
371         XMLNodeList nlist = node.children();
372         XMLNode* nsos;
373         XMLNodeIterator niter;
374         const XMLProperty* prop;
375
376         if (node.name() == X_("events")) {
377                 /* partial state setting*/
378                 return deserialize_events (node);
379         }
380
381         if (node.name() == X_("Envelope") || node.name() == X_("FadeOut") || node.name() == X_("FadeIn")) {
382
383                 if ((nsos = node.child (X_("AutomationList")))) {
384                         /* new school in old school clothing */
385                         return set_state (*nsos, version);
386                 }
387
388                 /* old school */
389
390                 const XMLNodeList& elist = node.children();
391                 XMLNodeConstIterator i;
392                 XMLProperty* prop;
393                 pframes_t x;
394                 double y;
395
396                 ControlList::freeze ();
397                 clear ();
398
399                 for (i = elist.begin(); i != elist.end(); ++i) {
400
401                         if ((prop = (*i)->property ("x")) == 0) {
402                                 error << _("automation list: no x-coordinate stored for control point (point ignored)") << endmsg;
403                                 continue;
404                         }
405                         x = atoi (prop->value().c_str());
406
407                         if ((prop = (*i)->property ("y")) == 0) {
408                                 error << _("automation list: no y-coordinate stored for control point (point ignored)") << endmsg;
409                                 continue;
410                         }
411                         y = atof (prop->value().c_str());
412
413                         fast_simple_add (x, y);
414                 }
415
416                 thaw ();
417
418                 return 0;
419         }
420
421         if (node.name() != X_("AutomationList") ) {
422                 error << string_compose (_("AutomationList: passed XML node called %1, not \"AutomationList\" - ignored"), node.name()) << endmsg;
423                 return -1;
424         }
425
426         if (set_id (node)) {
427                 /* update session AL list */
428                 AutomationListCreated(this);
429         }
430
431         if ((prop = node.property (X_("automation-id"))) != 0){
432                 _parameter = EventTypeMap::instance().from_symbol(prop->value());
433         } else {
434                 warning << "Legacy session: automation list has no automation-id property." << endmsg;
435         }
436
437         if ((prop = node.property (X_("interpolation-style"))) != 0) {
438                 _interpolation = (InterpolationStyle)string_2_enum(prop->value(), _interpolation);
439         } else {
440                 _interpolation = Linear;
441         }
442
443         if ((prop = node.property (X_("default"))) != 0){
444                 _default_value = atof (prop->value().c_str());
445         } else {
446                 _default_value = 0.0;
447         }
448
449         if ((prop = node.property (X_("style"))) != 0) {
450                 _style = string_to_auto_style (prop->value());
451         } else {
452                 _style = Absolute;
453         }
454
455         if ((prop = node.property (X_("state"))) != 0) {
456                 _state = string_to_auto_state (prop->value());
457                 if (_state == Write) {
458                         _state = Off;
459                 }
460                 automation_state_changed(_state);
461         } else {
462                 _state = Off;
463         }
464
465         if ((prop = node.property (X_("min-yval"))) != 0) {
466                 _min_yval = atof (prop->value ().c_str());
467         } else {
468                 _min_yval = FLT_MIN;
469         }
470
471         if ((prop = node.property (X_("max-yval"))) != 0) {
472                 _max_yval = atof (prop->value ().c_str());
473         } else {
474                 _max_yval = FLT_MAX;
475         }
476
477         bool have_events = false;
478
479         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
480                 if ((*niter)->name() == X_("events")) {
481                         deserialize_events (*(*niter));
482                         have_events = true;
483                 }
484         }
485
486         if (!have_events) {
487                 /* there was no Events child node; clear any current events */
488                 freeze ();
489                 clear ();
490                 mark_dirty ();
491                 maybe_signal_changed ();
492                 thaw ();
493         }
494
495         return 0;
496 }
497
498 bool
499 AutomationList::operator!= (AutomationList const & other) const
500 {
501         return (
502                 static_cast<ControlList const &> (*this) != static_cast<ControlList const &> (other) ||
503                 _state != other._state ||
504                 _style != other._style ||
505                 _touching != other._touching
506                 );
507 }
508
509 PBD::PropertyBase *
510 AutomationListProperty::clone () const
511 {
512         return new AutomationListProperty (
513                 this->property_id(),
514                 boost::shared_ptr<AutomationList> (new AutomationList (*this->_old.get())),
515                 boost::shared_ptr<AutomationList> (new AutomationList (*this->_current.get()))
516                 );
517 }
518