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