merge from 2.0-ongoing by hand, minus key binding editor
[ardour.git] / libs / ardour / crossfade.cc
1 /*
2     Copyright (C) 2003-2006 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 <sigc++/bind.h>
21
22 #include <pbd/stacktrace.h>
23
24 #include <ardour/types.h>
25 #include <ardour/crossfade.h>
26 #include <ardour/crossfade_compare.h>
27 #include <ardour/audioregion.h>
28 #include <ardour/playlist.h>
29 #include <ardour/utils.h>
30 #include <ardour/session.h>
31 #include <ardour/source.h>
32
33 #include "i18n.h"
34 #include <locale.h>
35
36 using namespace std;
37 using namespace ARDOUR;
38 using namespace PBD;
39
40 nframes_t Crossfade::_short_xfade_length = 0;
41 Change Crossfade::ActiveChanged = new_change();
42 Change Crossfade::FollowOverlapChanged = new_change();
43
44 /* XXX if and when we ever implement parallel processing of the process()
45    callback, these will need to be handled on a per-thread basis.
46 */
47
48 Sample* Crossfade::crossfade_buffer_out = 0;
49 Sample* Crossfade::crossfade_buffer_in = 0;
50
51 void
52 Crossfade::set_buffer_size (nframes_t sz)
53 {
54         if (crossfade_buffer_out) {
55                 delete [] crossfade_buffer_out;
56                 crossfade_buffer_out = 0;
57         }
58
59         if (crossfade_buffer_in) {
60                 delete [] crossfade_buffer_in;
61                 crossfade_buffer_in = 0;
62         }
63
64         if (sz) {
65                 crossfade_buffer_out = new Sample[sz];
66                 crossfade_buffer_in = new Sample[sz];
67         }
68 }
69
70 bool
71 Crossfade::operator== (const Crossfade& other)
72 {
73         return (_in == other._in) && (_out == other._out);
74 }
75
76 Crossfade::Crossfade (boost::shared_ptr<AudioRegion> in, boost::shared_ptr<AudioRegion> out, 
77                       nframes_t length,
78                       nframes_t position,
79                       AnchorPoint ap)
80         : AudioRegion (in->session(), position, length, "foobar"),
81           _fade_in (Parameter(FadeInAutomation), 0.0, 2.0, 1.0), // linear (gain coefficient) => -inf..+6dB
82           _fade_out (Parameter(FadeOutAutomation), 0.0, 2.0, 1.0) // linear (gain coefficient) => -inf..+6dB
83
84 {
85         _in = in;
86         _out = out;
87         
88         _anchor_point = ap;
89         _follow_overlap = false;
90
91         _active = Config->get_xfades_active ();
92         _fixed = true;
93
94         initialize ();
95 }
96
97 Crossfade::Crossfade (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioRegion> b, CrossfadeModel model, bool act)
98         : AudioRegion (a->session(), 0, 0, "foobar"),
99           _fade_in (Parameter(FadeInAutomation), 0.0, 2.0, 1.0), // linear (gain coefficient) => -inf..+6dB
100           _fade_out (Parameter(FadeOutAutomation), 0.0, 2.0, 1.0) // linear (gain coefficient) => -inf..+6dB
101 {
102         _in_update = false;
103         _fixed = false;
104
105         if (compute (a, b, model)) {
106                 throw failed_constructor();
107         }
108
109         _active = act;
110
111         initialize ();
112
113
114 }
115
116 Crossfade::Crossfade (const Playlist& playlist, XMLNode& node)
117         : AudioRegion (playlist.session(), 0, 0, "foobar"),
118           _fade_in (Parameter(FadeInAutomation), 0.0, 2.0, 1.0), // linear (gain coefficient) => -inf..+6dB
119           _fade_out (Parameter(FadeOutAutomation), 0.0, 2.0, 1.0) // linear (gain coefficient) => -inf..+6dB
120
121 {
122         boost::shared_ptr<Region> r;
123         XMLProperty* prop;
124         LocaleGuard lg (X_("POSIX"));
125
126         /* we have to find the in/out regions before we can do anything else */
127
128         if ((prop = node.property ("in")) == 0) {
129                 error << _("Crossfade: no \"in\" region in state") << endmsg;
130                 throw failed_constructor();
131         }
132         
133         PBD::ID id (prop->value());
134
135         if ((r = playlist.find_region (id)) == 0) {
136                 error << string_compose (_("Crossfade: no \"in\" region %1 found in playlist %2"), id, playlist.name())
137                       << endmsg;
138                 throw failed_constructor();
139         }
140         
141         if ((_in = boost::dynamic_pointer_cast<AudioRegion> (r)) == 0) {
142                 throw failed_constructor();
143         }
144
145         if ((prop = node.property ("out")) == 0) {
146                 error << _("Crossfade: no \"out\" region in state") << endmsg;
147                 throw failed_constructor();
148         }
149
150         PBD::ID id2 (prop->value());
151
152         if ((r = playlist.find_region (id2)) == 0) {
153                 error << string_compose (_("Crossfade: no \"out\" region %1 found in playlist %2"), id2, playlist.name())
154                       << endmsg;
155                 throw failed_constructor();
156         }
157         
158         if ((_out = boost::dynamic_pointer_cast<AudioRegion> (r)) == 0) {
159                 throw failed_constructor();
160         }
161
162         _length = 0;
163         initialize();
164         
165         if (set_state (node)) {
166                 throw failed_constructor();
167         }
168 }
169
170 Crossfade::Crossfade (boost::shared_ptr<Crossfade> orig, boost::shared_ptr<AudioRegion> newin, boost::shared_ptr<AudioRegion> newout)
171         : AudioRegion (boost::dynamic_pointer_cast<const AudioRegion> (orig)),
172           _fade_in (orig->_fade_in),
173           _fade_out (orig->_fade_out)
174 {
175         _active           = orig->_active;
176         _in_update        = orig->_in_update;
177         _anchor_point     = orig->_anchor_point;
178         _follow_overlap   = orig->_follow_overlap;
179         _fixed            = orig->_fixed;
180         
181         _in = newin;
182         _out = newout;
183
184         // copied from Crossfade::initialize()
185         _in_update = false;
186         
187         _out->suspend_fade_out ();
188         _in->suspend_fade_in ();
189
190         overlap_type = _in->coverage (_out->position(), _out->last_frame());
191         layer_relation = (int32_t) (_in->layer() - _out->layer());
192
193         // Let's make sure the fade isn't too long
194         set_length(_length);
195 }
196
197
198 Crossfade::~Crossfade ()
199 {
200         notify_callbacks ();
201 }
202
203 void
204 Crossfade::initialize ()
205 {
206         /* merge source lists from regions */
207
208         _sources = _in->sources();
209         _sources.insert (_sources.end(), _out->sources().begin(), _out->sources().end());
210         _master_sources = _in->master_sources();
211         _master_sources.insert(_master_sources.end(), _out->master_sources().begin(), _out->master_sources().end());
212         
213         _in_update = false;
214         
215         _out->suspend_fade_out ();
216         _in->suspend_fade_in ();
217
218         _fade_out.freeze ();
219         _fade_out.clear ();
220         _fade_out.add (0.0, 1.0);
221         _fade_out.add ((_length * 0.1), 0.99);
222         _fade_out.add ((_length * 0.2), 0.97);
223         _fade_out.add ((_length * 0.8), 0.03);
224         _fade_out.add ((_length * 0.9), 0.01);
225         _fade_out.add (_length, 0.0);
226         _fade_out.thaw ();
227         
228         _fade_in.freeze ();
229         _fade_in.clear ();
230         _fade_in.add (0.0, 0.0);
231         _fade_in.add ((_length * 0.1),  0.01);
232         _fade_in.add ((_length * 0.2),  0.03);
233         _fade_in.add ((_length * 0.8),  0.97);
234         _fade_in.add ((_length * 0.9),  0.99);
235         _fade_in.add (_length, 1.0);
236         _fade_in.thaw ();
237
238         overlap_type = _in->coverage (_out->position(), _out->last_frame());
239         layer_relation = (int32_t) (_in->layer() - _out->layer());
240 }       
241
242 nframes_t 
243 Crossfade::read_raw_internal (Sample* buf, nframes_t start, nframes_t cnt) const
244 {
245 #if 0
246         Sample* mixdown = new Sample[cnt];
247         float* gain = new float[cnt];
248         nframes_t ret;
249
250         ret = read_at (buf, mixdown, gain, start, cnt, chan_n, cnt);
251
252         delete [] mixdown;
253         delete [] gain;
254
255         return ret;
256 #endif
257         return cnt;
258 }
259
260 nframes_t 
261 Crossfade::read_at (Sample *buf, Sample *mixdown_buffer, 
262                     float *gain_buffer, nframes_t start, nframes_t cnt, uint32_t chan_n) const
263 {
264         nframes_t offset;
265         nframes_t to_write;
266
267         if (!_active) {
268                 return 0;
269         }
270
271         if (start < _position) {
272
273                 /* handle an initial section of the read area that we do not
274                    cover.
275                 */
276
277                 offset = _position - start;
278
279                 if (offset < cnt) {
280                         cnt -= offset;
281                 } else {
282                         return 0;
283                 }
284                 
285                 start = _position;
286                 buf += offset;
287                 to_write = min (_length, cnt);
288
289         } else {
290                 
291                 to_write = min (_length - (start - _position), cnt);
292                 
293         }
294
295         offset = start - _position;
296
297         /* Prevent data from piling up inthe crossfade buffers when reading a transparent region */
298         if (!(_out->opaque())) {
299                 memset (crossfade_buffer_out, 0, sizeof (Sample) * to_write);
300         } else if (!(_in->opaque())) {
301                 memset (crossfade_buffer_in, 0, sizeof (Sample) * to_write);
302         }
303         
304         _out->read_at (crossfade_buffer_out, mixdown_buffer, gain_buffer, start, to_write, chan_n);
305         _in->read_at (crossfade_buffer_in, mixdown_buffer, gain_buffer, start, to_write, chan_n);
306
307         float* fiv = new float[to_write];
308         float* fov = new float[to_write];
309
310         _fade_in.curve().get_vector (offset, offset+to_write, fiv, to_write);
311         _fade_out.curve().get_vector (offset, offset+to_write, fov, to_write);
312
313         /* note: although we have not explicitly taken into account the return values
314            from _out->read_at() or _in->read_at(), the length() function does this
315            implicitly. why? because it computes a value based on the in+out regions'
316            position and length, and so we know precisely how much data they could return. 
317         */
318
319         for (nframes_t n = 0; n < to_write; ++n) {
320                 buf[n] = (crossfade_buffer_out[n] * fov[n]) + (crossfade_buffer_in[n] * fiv[n]);
321         }
322
323         delete [] fov;
324         delete [] fiv;
325
326         return to_write;
327 }       
328
329 OverlapType 
330 Crossfade::coverage (nframes_t start, nframes_t end) const
331 {
332         nframes_t my_end = _position + _length;
333
334         if ((start >= _position) && (end <= my_end)) {
335                 return OverlapInternal;
336         }
337         if ((end >= _position) && (end <= my_end)) {
338                 return OverlapStart;
339         }
340         if ((start >= _position) && (start <= my_end)) {
341                 return OverlapEnd;
342         }
343         if ((_position >= start) && (_position <= end) && (my_end <= end)) {
344                 return OverlapExternal;
345         }
346         return OverlapNone;
347 }
348
349 void
350 Crossfade::set_active (bool yn)
351 {
352         if (_active != yn) {
353                 _active = yn;
354                 StateChanged (ActiveChanged);
355         }
356 }
357
358 bool
359 Crossfade::refresh ()
360 {
361         /* crossfades must be between non-muted regions */
362         
363         if (_out->muted() || _in->muted()) {
364                 Invalidated (shared_from_this ());
365                 return false;
366         }
367
368         /* Top layer shouldn't be transparent */
369         
370         if (!((layer_relation > 0 ? _in : _out)->opaque())) {
371                 Invalidated (shared_from_this());
372                 return false;
373         }
374
375         /* layer ordering cannot change */
376
377         int32_t new_layer_relation = (int32_t) (_in->layer() - _out->layer());
378
379         if (new_layer_relation * layer_relation < 0) { // different sign, layers rotated 
380                 Invalidated (shared_from_this ());
381                 return false;
382         }
383
384         OverlapType ot = _in->coverage (_out->first_frame(), _out->last_frame());
385
386         if (ot == OverlapNone) {
387                 Invalidated (shared_from_this ());
388                 return false;
389         } 
390
391         bool send_signal;
392
393         if (ot != overlap_type) {
394
395                 if (_follow_overlap) {
396
397                         try {
398                                 compute (_in, _out, Config->get_xfade_model());
399                         } 
400
401                         catch (NoCrossfadeHere& err) {
402                                 Invalidated (shared_from_this ());
403                                 return false;
404                         }
405
406                         send_signal = true;
407
408                 } else {
409
410                         Invalidated (shared_from_this ());
411                         return false;
412                 }
413
414         } else {
415
416                 send_signal = update ();
417         }
418
419         if (send_signal) {
420                 StateChanged (BoundsChanged); /* EMIT SIGNAL */
421         }
422
423         _in_update = false;
424
425         return true;
426 }
427
428 bool
429 Crossfade::update ()
430 {
431         nframes_t newlen;
432         
433         if (_follow_overlap) {
434                 newlen = _out->first_frame() + _out->length() - _in->first_frame();
435         } else {
436                 newlen = _length;
437         }
438         
439         if (newlen == 0) {
440                 Invalidated (shared_from_this ());
441                 return false;
442         }
443         
444         _in_update = true;
445         
446         if ((_follow_overlap && newlen != _length) || (_length > newlen)) {
447                 
448                 double factor =  newlen / (double) _length;
449                 
450                 _fade_out.x_scale (factor);
451                 _fade_in.x_scale (factor);
452                 
453                 _length = newlen;
454         } 
455                 
456         switch (_anchor_point) {
457         case StartOfIn:
458                 _position = _in->first_frame();
459                 break;
460                 
461         case EndOfIn:
462                 _position = _in->last_frame() - _length;
463                 break;
464                 
465         case EndOfOut:
466                 _position = _out->last_frame() - _length;
467         }
468
469         return true;
470 }
471
472 int
473 Crossfade::compute (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioRegion> b, CrossfadeModel model)
474 {
475         boost::shared_ptr<AudioRegion> top;
476         boost::shared_ptr<AudioRegion> bottom;
477         nframes_t short_xfade_length;
478
479         short_xfade_length = _short_xfade_length; 
480
481         if (a->layer() < b->layer()) {
482                 top = b;
483                 bottom = a;
484         } else {
485                 top = a;
486                 bottom = b;
487         }
488         
489         /* first check for matching ends */
490         
491         if (top->first_frame() == bottom->first_frame()) {
492
493                 /* Both regions start at the same point */
494                 
495                 if (top->last_frame() < bottom->last_frame()) {
496                         
497                         /* top ends before bottom, so put an xfade
498                            in at the end of top.
499                         */
500                         
501                         /* [-------- top ---------- ]
502                          * {====== bottom =====================}
503                          */
504
505                         _in = bottom;
506                         _out = top;
507
508                         if (top->last_frame() < short_xfade_length) {
509                                 _position = 0;
510                         } else {
511                                 _position = top->last_frame() - short_xfade_length;
512                         }
513
514                         _length = min (short_xfade_length, top->length());
515                         _follow_overlap = false;
516                         _anchor_point = EndOfIn;
517                         _active = true;
518                         _fixed = true;
519
520                 } else {
521                         /* top ends after (or same time) as bottom - no xfade
522                          */
523                         
524                         /* [-------- top ------------------------ ]
525                          * {====== bottom =====================}
526                          */
527
528                         throw NoCrossfadeHere();
529                 }
530                 
531         } else if (top->last_frame() == bottom->last_frame()) {
532                 
533                 /* Both regions end at the same point */
534                 
535                 if (top->first_frame() > bottom->first_frame()) {
536                         
537                         /* top starts after bottom, put an xfade in at the
538                            start of top
539                         */
540                         
541                         /*            [-------- top ---------- ]
542                          * {====== bottom =====================}
543                          */
544
545                         _in = top;
546                         _out = bottom;
547                         _position = top->first_frame();
548                         _length = min (short_xfade_length, top->length());
549                         _follow_overlap = false;
550                         _anchor_point = StartOfIn;
551                         _active = true;
552                         _fixed = true;
553                         
554                 } else {
555                         /* top starts before bottom - no xfade
556                          */
557
558                         /* [-------- top ------------------------ ]
559                          *    {====== bottom =====================}
560                          */
561
562                         throw NoCrossfadeHere();
563                 }
564
565         } else {
566         
567                 /* OK, time to do more regular overlapping */
568
569                 OverlapType ot = top->coverage (bottom->first_frame(), bottom->last_frame());
570
571                 switch (ot) {
572                 case OverlapNone:
573                         /* should be NOTREACHED as a precondition of creating
574                            a new crossfade, but we need to handle it here.
575                         */
576                         throw NoCrossfadeHere();
577                         break;
578                         
579                 case OverlapInternal:
580                 case OverlapExternal:
581                         /* should be NOTREACHED because of tests above */
582                         throw NoCrossfadeHere();
583                         break;
584                         
585                 case OverlapEnd: /* top covers start of bottom but ends within it */
586
587                         /* [---- top ------------------------] 
588                          *                { ==== bottom ============ } 
589                          */ 
590
591                         _in = bottom;
592                         _out = top;
593                         _anchor_point = EndOfOut;
594
595                         if (model == FullCrossfade) {
596                                 _position = bottom->first_frame(); // "{"
597                                 _length = _out->first_frame() + _out->length() - _in->first_frame();
598                                 /* leave active alone */
599                                 _follow_overlap = true;
600                         } else {
601                                 _length = min (short_xfade_length, top->length());
602                                 _position = top->last_frame() - _length;  // "]" - length 
603                                 _active = true;
604                                 _follow_overlap = false;
605                                 
606                         }
607                         break;
608                         
609                 case OverlapStart:   /* top starts within bottom but covers bottom's end */
610
611                         /*                   { ==== top ============ } 
612                          *   [---- bottom -------------------] 
613                          */
614
615                         _in = top;
616                         _out = bottom;
617                         _position = top->first_frame();
618                         _anchor_point = StartOfIn;
619
620                         if (model == FullCrossfade) {
621                                 _length = _out->first_frame() + _out->length() - _in->first_frame();
622                                 /* leave active alone */
623                                 _follow_overlap = true;
624                         } else {
625                                 _length = min (short_xfade_length, top->length());
626                                 _active = true;
627                                 _follow_overlap = false;
628                                 
629                         }
630                         
631                         break;
632                 }
633         }
634         
635         return 0;
636 }
637
638 XMLNode&
639 Crossfade::get_state () 
640 {
641         XMLNode* node = new XMLNode (X_("Crossfade"));
642         XMLNode* child;
643         char buf[64];
644         LocaleGuard lg (X_("POSIX"));
645
646         _out->id().print (buf, sizeof (buf));
647         node->add_property ("out", buf);
648         _in->id().print (buf, sizeof (buf));
649         node->add_property ("in", buf);
650         node->add_property ("active", (_active ? "yes" : "no"));
651         node->add_property ("follow-overlap", (_follow_overlap ? "yes" : "no"));
652         node->add_property ("fixed", (_fixed ? "yes" : "no"));
653         snprintf (buf, sizeof(buf), "%" PRIu32, _length);
654         node->add_property ("length", buf);
655         snprintf (buf, sizeof(buf), "%" PRIu32, (uint32_t) _anchor_point);
656         node->add_property ("anchor-point", buf);
657         snprintf (buf, sizeof(buf), "%" PRIu32, (uint32_t) _position);
658         node->add_property ("position", buf);
659
660         child = node->add_child ("FadeIn");
661
662         for (AutomationList::iterator ii = _fade_in.begin(); ii != _fade_in.end(); ++ii) {
663                 XMLNode* pnode;
664
665                 pnode = new XMLNode ("point");
666
667                 snprintf (buf, sizeof (buf), "%" PRIu32, (nframes_t) floor ((*ii)->when));
668                 pnode->add_property ("x", buf);
669                 snprintf (buf, sizeof (buf), "%.12g", (*ii)->value);
670                 pnode->add_property ("y", buf);
671                 child->add_child_nocopy (*pnode);
672         }
673
674         child = node->add_child ("FadeOut");
675
676         for (AutomationList::iterator ii = _fade_out.begin(); ii != _fade_out.end(); ++ii) {
677                 XMLNode* pnode;
678
679                 pnode = new XMLNode ("point");
680
681                 snprintf (buf, sizeof (buf), "%" PRIu32, (nframes_t) floor ((*ii)->when));
682                 pnode->add_property ("x", buf);
683                 snprintf (buf, sizeof (buf), "%.12g", (*ii)->value);
684                 pnode->add_property ("y", buf);
685                 child->add_child_nocopy (*pnode);
686         }
687
688         return *node;
689 }
690
691 int
692 Crossfade::set_state (const XMLNode& node)
693 {
694         XMLNodeConstIterator i;
695         XMLNodeList children;
696         XMLNode* fi;
697         XMLNode* fo;
698         const XMLProperty* prop;
699         LocaleGuard lg (X_("POSIX"));
700         Change what_changed = Change (0);
701         nframes_t val;
702
703         if ((prop = node.property ("position")) != 0) {
704                 sscanf (prop->value().c_str(), "%" PRIu32, &val);
705                 if (val != _position) {
706                         _position = val;
707                         what_changed = Change (what_changed | PositionChanged);
708                 }
709         } else {
710                 warning << _("old-style crossfade information - no position information") << endmsg;
711                 _position = _in->first_frame();
712         }
713
714         if ((prop = node.property ("active")) != 0) {
715                 bool x = (prop->value() == "yes");
716                 if (x != _active) {
717                         _active = x;
718                         what_changed = Change (what_changed | ActiveChanged);
719                 }
720         } else {
721                 _active = true;
722         }
723
724         if ((prop = node.property ("follow-overlap")) != 0) {
725                 _follow_overlap = (prop->value() == "yes");
726         } else {
727                 _follow_overlap = false;
728         }
729
730         if ((prop = node.property ("fixed")) != 0) {
731                 _fixed = (prop->value() == "yes");
732         } else {
733                 _fixed = false;
734         }
735
736         if ((prop = node.property ("anchor-point")) != 0) {
737                 _anchor_point = AnchorPoint (atoi ((prop->value().c_str())));
738         } else {
739                 _anchor_point = StartOfIn;
740         }
741
742         if ((prop = node.property ("length")) != 0) {
743
744                 sscanf (prop->value().c_str(), "%" PRIu32, &val);
745                 if (val != _length) {
746                         _length = atol (prop->value().c_str());
747                         what_changed = Change (what_changed | LengthChanged);
748                 }
749
750         } else {
751                 
752                 /* XXX this branch is legacy code from before
753                    the point where we stored xfade lengths.
754                 */
755                 
756                 if ((_length = overlap_length()) == 0) {
757                         throw failed_constructor();
758                 }
759         }
760
761         if ((fi = find_named_node (node, "FadeIn")) == 0) {
762                 return -1;
763         }
764         
765         if ((fo = find_named_node (node, "FadeOut")) == 0) {
766                 return -1;
767         }
768
769         /* fade in */
770         
771         _fade_in.freeze ();
772         _fade_in.clear ();
773         
774         children = fi->children();
775         
776         for (i = children.begin(); i != children.end(); ++i) {
777                 if ((*i)->name() == "point") {
778                         nframes_t x;
779                         float y;
780                         
781                         prop = (*i)->property ("x");
782                         sscanf (prop->value().c_str(), "%" PRIu32, &x);
783                         
784                         prop = (*i)->property ("y");
785                         sscanf (prop->value().c_str(), "%f", &y);
786
787                         _fade_in.add (x, y);
788                 }
789         }
790
791         _fade_in.thaw ();
792         
793         /* fade out */
794         
795         _fade_out.freeze ();
796         _fade_out.clear ();
797
798         children = fo->children();
799         
800         for (i = children.begin(); i != children.end(); ++i) {
801                 if ((*i)->name() == "point") {
802                         nframes_t x;
803                         float y;
804                         XMLProperty* prop;
805
806                         prop = (*i)->property ("x");
807                         sscanf (prop->value().c_str(), "%" PRIu32, &x);
808
809                         prop = (*i)->property ("y");
810                         sscanf (prop->value().c_str(), "%f", &y);
811                         
812                         _fade_out.add (x, y);
813                 }
814         }
815
816         _fade_out.thaw ();
817
818         StateChanged (what_changed); /* EMIT SIGNAL */
819
820         return 0;
821 }
822
823 bool
824 Crossfade::can_follow_overlap () const
825 {
826         return !_fixed;
827 }
828
829 void
830 Crossfade::set_follow_overlap (bool yn)
831 {
832         if (yn == _follow_overlap || _fixed) {
833                 return;
834         }
835
836         _follow_overlap = yn;
837
838         if (!yn) {
839                 set_length (_short_xfade_length);
840         } else {
841                 set_length (_out->first_frame() + _out->length() - _in->first_frame());
842         }
843
844         StateChanged (FollowOverlapChanged);
845 }
846
847 nframes_t
848 Crossfade::set_length (nframes_t len)
849 {
850         nframes_t limit;
851
852         switch (_anchor_point) {
853         case StartOfIn:
854                 limit = _in->length();
855                 break;
856
857         case EndOfIn:
858                 limit = _in->length();
859                 break;
860
861         case EndOfOut:
862                 limit = _out->length();
863                 break;
864                 
865         }
866
867         len = min (limit, len);
868
869         double factor = len / (double) _length;
870
871         _in_update = true;
872         _fade_out.x_scale (factor);
873         _fade_in.x_scale (factor);
874         _in_update = false;
875         
876         _length = len;
877
878         StateChanged (LengthChanged);
879
880         return len;
881 }
882
883 nframes_t
884 Crossfade::overlap_length () const
885 {
886         if (_fixed) {
887                 return _length;
888         }
889         return _out->first_frame() + _out->length() - _in->first_frame();
890 }
891
892 void
893 Crossfade::set_short_xfade_length (nframes_t n)
894 {
895         _short_xfade_length = n;
896 }
897
898 void
899 Crossfade::invalidate ()
900 {
901         Invalidated (shared_from_this ()); /* EMIT SIGNAL */
902 }