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