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