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