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