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