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