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