various fixes to MidiRegionView selection handling, key handling, drawing of ghost...
[ardour2.git] / libs / ardour / automation_list.cc
blobb2bda4a20302dd572de6d15fafc674ebb8874874
1 /*
2 Copyright (C) 2002 Paul Davis
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.
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.
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.
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"
32 #include "i18n.h"
34 using namespace std;
35 using namespace ARDOUR;
36 using namespace PBD;
38 PBD::Signal1<void,AutomationList *> AutomationList::AutomationListCreated;
40 #if 0
41 static void dumpit (const AutomationList& al, string prefix = "")
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;
47 cerr << "\n";
49 #endif
50 AutomationList::AutomationList (Evoral::Parameter id)
51 : ControlList(id)
53 _state = Off;
54 _style = Absolute;
55 g_atomic_int_set (&_touching, 0);
57 create_curve_if_necessary();
59 assert(_parameter.type() != NullAutomation);
60 AutomationListCreated(this);
63 AutomationList::AutomationList (const AutomationList& other)
64 : StatefulDestructible()
65 , ControlList(other)
67 _style = other._style;
68 _state = other._state;
69 g_atomic_int_set (&_touching, other.touching());
71 create_curve_if_necessary();
73 assert(_parameter.type() != NullAutomation);
74 AutomationListCreated(this);
77 AutomationList::AutomationList (const AutomationList& other, double start, double end)
78 : ControlList(other, start, end)
80 _style = other._style;
81 _state = other._state;
82 g_atomic_int_set (&_touching, other.touching());
84 create_curve_if_necessary();
86 assert(_parameter.type() != NullAutomation);
87 AutomationListCreated(this);
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.
93 AutomationList::AutomationList (const XMLNode& node, Evoral::Parameter id)
94 : ControlList(id)
96 g_atomic_int_set (&_touching, 0);
97 _state = Off;
98 _style = Absolute;
100 set_state (node, Stateful::loading_state_version);
102 if (id) {
103 _parameter = id;
106 create_curve_if_necessary();
108 assert(_parameter.type() != NullAutomation);
109 AutomationListCreated(this);
112 AutomationList::~AutomationList()
116 boost::shared_ptr<Evoral::ControlList>
117 AutomationList::create(Evoral::Parameter id)
119 return boost::shared_ptr<Evoral::ControlList>(new AutomationList(id));
122 void
123 AutomationList::create_curve_if_necessary()
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;
140 bool
141 AutomationList::operator== (const AutomationList& other)
143 return _events == other._events;
146 AutomationList&
147 AutomationList::operator= (const AutomationList& other)
149 if (this != &other) {
151 _events.clear ();
153 for (const_iterator i = other._events.begin(); i != other._events.end(); ++i) {
154 _events.push_back (new Evoral::ControlEvent (**i));
157 _min_yval = other._min_yval;
158 _max_yval = other._max_yval;
159 _max_xval = other._max_xval;
160 _default_value = other._default_value;
162 mark_dirty ();
163 maybe_signal_changed ();
166 return *this;
169 void
170 AutomationList::maybe_signal_changed ()
172 ControlList::maybe_signal_changed ();
174 if (!ControlList::frozen()) {
175 StateChanged (); /* EMIT SIGNAL */
179 void
180 AutomationList::set_automation_state (AutoState s)
182 if (s != _state) {
183 _state = s;
185 if (_state == Write) {
186 Glib::Mutex::Lock lm (ControlList::_lock);
187 nascent.push_back (new NascentInfo ());
189 automation_state_changed (s); /* EMIT SIGNAL */
193 void
194 AutomationList::set_automation_style (AutoStyle s)
196 if (s != _style) {
197 _style = s;
198 automation_style_changed (); /* EMIT SIGNAL */
202 void
203 AutomationList::start_touch (double when)
205 if (_state == Touch) {
206 Glib::Mutex::Lock lm (ControlList::_lock);
207 nascent.push_back (new NascentInfo (when));
210 g_atomic_int_set (&_touching, 1);
213 void
214 AutomationList::stop_touch (bool mark, double when)
216 if (g_atomic_int_get (&_touching) == 0) {
217 /* this touch has already been stopped (probably by Automatable::transport_stopped),
218 so we've nothing to do.
220 return;
223 g_atomic_int_set (&_touching, 0);
225 if (_state == Touch) {
227 assert (!nascent.empty ());
229 Glib::Mutex::Lock lm (ControlList::_lock);
231 if (mark) {
233 nascent.back()->end_time = when;
235 } else {
237 /* nascent info created in start touch but never used. just get rid of it.
240 NascentInfo* ninfo = nascent.back ();
241 nascent.erase (nascent.begin());
242 delete ninfo;
247 void
248 AutomationList::thaw ()
250 ControlList::thaw();
252 if (_changed_when_thawed) {
253 _changed_when_thawed = false;
254 StateChanged(); /* EMIT SIGNAL */
258 XMLNode&
259 AutomationList::get_state ()
261 return state (true);
264 XMLNode&
265 AutomationList::state (bool full)
267 XMLNode* root = new XMLNode (X_("AutomationList"));
268 char buf[64];
269 LocaleGuard lg (X_("POSIX"));
271 root->add_property ("automation-id", EventTypeMap::instance().to_symbol(_parameter));
273 root->add_property ("id", _id.to_s());
275 snprintf (buf, sizeof (buf), "%.12g", _default_value);
276 root->add_property ("default", buf);
277 snprintf (buf, sizeof (buf), "%.12g", _min_yval);
278 root->add_property ("min-yval", buf);
279 snprintf (buf, sizeof (buf), "%.12g", _max_yval);
280 root->add_property ("max-yval", buf);
281 snprintf (buf, sizeof (buf), "%.12g", _max_xval);
282 root->add_property ("max-xval", buf);
284 root->add_property ("interpolation-style", enum_2_string (_interpolation));
286 if (full) {
287 /* never serialize state with Write enabled - too dangerous
288 for the user's data
290 if (_state != Write) {
291 root->add_property ("state", auto_state_to_string (_state));
292 } else {
293 root->add_property ("state", auto_state_to_string (Off));
295 } else {
296 /* never save anything but Off for automation state to a template */
297 root->add_property ("state", auto_state_to_string (Off));
300 root->add_property ("style", auto_style_to_string (_style));
302 if (!_events.empty()) {
303 root->add_child_nocopy (serialize_events());
306 return *root;
309 XMLNode&
310 AutomationList::serialize_events ()
312 XMLNode* node = new XMLNode (X_("events"));
313 stringstream str;
315 str.precision(15); //10 digits is enough digits for 24 hours at 96kHz
317 for (iterator xx = _events.begin(); xx != _events.end(); ++xx) {
318 str << (double) (*xx)->when;
319 str << ' ';
320 str <<(double) (*xx)->value;
321 str << '\n';
324 /* XML is a bit wierd */
326 XMLNode* content_node = new XMLNode (X_("foo")); /* it gets renamed by libxml when we set content */
327 content_node->set_content (str.str());
329 node->add_child_nocopy (*content_node);
331 return *node;
335 AutomationList::deserialize_events (const XMLNode& node)
337 if (node.children().empty()) {
338 return -1;
341 XMLNode* content_node = node.children().front();
343 if (content_node->content().empty()) {
344 return -1;
347 ControlList::freeze ();
348 clear ();
350 stringstream str (content_node->content());
352 double x;
353 double y;
354 bool ok = true;
356 while (str) {
357 str >> x;
358 if (!str) {
359 break;
361 str >> y;
362 if (!str) {
363 ok = false;
364 break;
366 fast_simple_add (x, y);
369 if (!ok) {
370 clear ();
371 error << _("automation list: cannot load coordinates from XML, all points ignored") << endmsg;
372 } else {
373 mark_dirty ();
374 maybe_signal_changed ();
377 thaw ();
379 return 0;
383 AutomationList::set_state (const XMLNode& node, int version)
385 XMLNodeList nlist = node.children();
386 XMLNode* nsos;
387 XMLNodeIterator niter;
388 const XMLProperty* prop;
390 if (node.name() == X_("events")) {
391 /* partial state setting*/
392 return deserialize_events (node);
395 if (node.name() == X_("Envelope") || node.name() == X_("FadeOut") || node.name() == X_("FadeIn")) {
397 if ((nsos = node.child (X_("AutomationList")))) {
398 /* new school in old school clothing */
399 return set_state (*nsos, version);
402 /* old school */
404 const XMLNodeList& elist = node.children();
405 XMLNodeConstIterator i;
406 XMLProperty* prop;
407 pframes_t x;
408 double y;
410 ControlList::freeze ();
411 clear ();
413 for (i = elist.begin(); i != elist.end(); ++i) {
415 if ((prop = (*i)->property ("x")) == 0) {
416 error << _("automation list: no x-coordinate stored for control point (point ignored)") << endmsg;
417 continue;
419 x = atoi (prop->value().c_str());
421 if ((prop = (*i)->property ("y")) == 0) {
422 error << _("automation list: no y-coordinate stored for control point (point ignored)") << endmsg;
423 continue;
425 y = atof (prop->value().c_str());
427 fast_simple_add (x, y);
430 thaw ();
432 return 0;
435 if (node.name() != X_("AutomationList") ) {
436 error << string_compose (_("AutomationList: passed XML node called %1, not \"AutomationList\" - ignored"), node.name()) << endmsg;
437 return -1;
440 if ((prop = node.property ("id")) != 0) {
441 _id = prop->value ();
442 /* update session AL list */
443 AutomationListCreated(this);
446 if ((prop = node.property (X_("automation-id"))) != 0){
447 _parameter = EventTypeMap::instance().new_parameter(prop->value());
448 } else {
449 warning << "Legacy session: automation list has no automation-id property.";
452 if ((prop = node.property (X_("interpolation-style"))) != 0) {
453 _interpolation = (InterpolationStyle)string_2_enum(prop->value(), _interpolation);
454 } else {
455 _interpolation = Linear;
458 if ((prop = node.property (X_("default"))) != 0){
459 _default_value = atof (prop->value().c_str());
460 } else {
461 _default_value = 0.0;
464 if ((prop = node.property (X_("style"))) != 0) {
465 _style = string_to_auto_style (prop->value());
466 } else {
467 _style = Absolute;
470 if ((prop = node.property (X_("state"))) != 0) {
471 _state = string_to_auto_state (prop->value());
472 if (_state == Write) {
473 _state = Off;
475 } else {
476 _state = Off;
479 if ((prop = node.property (X_("min-yval"))) != 0) {
480 _min_yval = atof (prop->value ().c_str());
481 } else {
482 _min_yval = FLT_MIN;
485 if ((prop = node.property (X_("max-yval"))) != 0) {
486 _max_yval = atof (prop->value ().c_str());
487 } else {
488 _max_yval = FLT_MAX;
491 if ((prop = node.property (X_("max-xval"))) != 0) {
492 _max_xval = atof (prop->value ().c_str());
493 } else {
494 _max_xval = 0; // means "no limit ;
497 bool have_events = false;
499 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
500 if ((*niter)->name() == X_("events")) {
501 deserialize_events (*(*niter));
502 have_events = true;
506 if (!have_events) {
507 /* there was no Events child node; clear any current events */
508 freeze ();
509 clear ();
510 mark_dirty ();
511 maybe_signal_changed ();
512 thaw ();
515 return 0;