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