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