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