Strip trailing whitespace and fix other whitespace errors (e.g. space/tab mixing...
[ardour.git] / libs / ardour / crossfade.cc
index 739ea1cc0e5cc89654c1bd99fefefd6ddad0e733..f12b32228d558bd9a4051092db094c22fa7fdcad 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2003-2006 Paul Davis 
+    Copyright (C) 2003-2006 Paul Davis
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
     along with this program; if not, write to the Free Software
     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
-    $Id$
 */
 
 #include <sigc++/bind.h>
 
-#include <ardour/types.h>
-#include <ardour/crossfade.h>
-#include <ardour/crossfade_compare.h>
-#include <ardour/audioregion.h>
-#include <ardour/playlist.h>
-#include <ardour/utils.h>
-#include <ardour/session.h>
+#include "pbd/stacktrace.h"
+
+#include "ardour/types.h"
+#include "ardour/crossfade.h"
+#include "ardour/crossfade_compare.h"
+#include "ardour/audioregion.h"
+#include "ardour/playlist.h"
+#include "ardour/utils.h"
+#include "ardour/session.h"
+#include "ardour/source.h"
 
 #include "i18n.h"
 #include <locale.h>
@@ -49,15 +51,11 @@ Sample* Crossfade::crossfade_buffer_in = 0;
 void
 Crossfade::set_buffer_size (nframes_t sz)
 {
-       if (crossfade_buffer_out) {
-               delete [] crossfade_buffer_out;
-               crossfade_buffer_out = 0;
-       }
+       delete [] crossfade_buffer_out;
+       crossfade_buffer_out = 0;
 
-       if (crossfade_buffer_in) {
-               delete [] crossfade_buffer_in;
-               crossfade_buffer_in = 0;
-       }
+       delete [] crossfade_buffer_in;
+       crossfade_buffer_in = 0;
 
        if (sz) {
                crossfade_buffer_out = new Sample[sz];
@@ -71,39 +69,34 @@ Crossfade::operator== (const Crossfade& other)
        return (_in == other._in) && (_out == other._out);
 }
 
-Crossfade::Crossfade (boost::shared_ptr<AudioRegion> in, boost::shared_ptr<AudioRegion> out, 
+Crossfade::Crossfade (boost::shared_ptr<AudioRegion> in, boost::shared_ptr<AudioRegion> out,
                      nframes_t length,
                      nframes_t position,
                      AnchorPoint ap)
-       : _fade_in (0.0, 2.0, 1.0), // linear (gain coefficient) => -inf..+6dB
-         _fade_out (0.0, 2.0, 1.0) // linear (gain coefficient) => -inf..+6dB
+       : AudioRegion (in->session(), position, length, "foobar"),
+         _fade_in (Evoral::Parameter(FadeInAutomation)), // linear (gain coefficient) => -inf..+6dB
+         _fade_out (Evoral::Parameter(FadeOutAutomation)) // linear (gain coefficient) => -inf..+6dB
+
 {
        _in = in;
        _out = out;
-       _length = length;
-       _position = position;
        _anchor_point = ap;
+       _follow_overlap = false;
 
-       switch (Config->get_xfade_model()) {
-       case ShortCrossfade:
-               _follow_overlap = false;
-               break;
-       default:
-               _follow_overlap = true;
-       }
-
-       _active = Config->get_xfades_active ();
+       _active = _session.config.get_xfades_active ();
        _fixed = true;
-               
+
        initialize ();
 }
 
 Crossfade::Crossfade (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioRegion> b, CrossfadeModel model, bool act)
-       : _fade_in (0.0, 2.0, 1.0), // linear (gain coefficient) => -inf..+6dB
-         _fade_out (0.0, 2.0, 1.0) // linear (gain coefficient) => -inf..+6dB
+       : AudioRegion (a->session(), 0, 0, "foobar"),
+         _fade_in (Evoral::Parameter(FadeInAutomation)), // linear (gain coefficient) => -inf..+6dB
+         _fade_out (Evoral::Parameter(FadeOutAutomation)) // linear (gain coefficient) => -inf..+6dB
 {
        _in_update = false;
        _fixed = false;
+       _follow_overlap = false;
 
        if (compute (a, b, model)) {
                throw failed_constructor();
@@ -112,12 +105,13 @@ Crossfade::Crossfade (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioR
        _active = act;
 
        initialize ();
-
 }
 
 Crossfade::Crossfade (const Playlist& playlist, XMLNode& node)
-       :  _fade_in (0.0, 2.0, 1.0), // linear (gain coefficient) => -inf..+6dB
-          _fade_out (0.0, 2.0, 1.0) // linear (gain coefficient) => -inf..+6dB
+       : AudioRegion (playlist.session(), 0, 0, "foobar"),
+         _fade_in (Evoral::Parameter(FadeInAutomation)), // linear (gain coefficient) => -inf..+6dB
+         _fade_out (Evoral::Parameter(FadeOutAutomation)) // linear (gain coefficient) => -inf..+6dB
+
 {
        boost::shared_ptr<Region> r;
        XMLProperty* prop;
@@ -129,7 +123,7 @@ Crossfade::Crossfade (const Playlist& playlist, XMLNode& node)
                error << _("Crossfade: no \"in\" region in state") << endmsg;
                throw failed_constructor();
        }
-       
+
        PBD::ID id (prop->value());
 
        if ((r = playlist.find_region (id)) == 0) {
@@ -137,7 +131,7 @@ Crossfade::Crossfade (const Playlist& playlist, XMLNode& node)
                      << endmsg;
                throw failed_constructor();
        }
-       
+
        if ((_in = boost::dynamic_pointer_cast<AudioRegion> (r)) == 0) {
                throw failed_constructor();
        }
@@ -154,257 +148,150 @@ Crossfade::Crossfade (const Playlist& playlist, XMLNode& node)
                      << endmsg;
                throw failed_constructor();
        }
-       
+
        if ((_out = boost::dynamic_pointer_cast<AudioRegion> (r)) == 0) {
                throw failed_constructor();
        }
 
        _length = 0;
        initialize();
-       
+       _active = true;
+
        if (set_state (node)) {
                throw failed_constructor();
        }
 }
 
-Crossfade::Crossfade (const Crossfade &orig, boost::shared_ptr<AudioRegion> newin, boost::shared_ptr<AudioRegion> newout)
-       : _fade_in(orig._fade_in),
-         _fade_out(orig._fade_out)
+Crossfade::Crossfade (boost::shared_ptr<Crossfade> orig, boost::shared_ptr<AudioRegion> newin, boost::shared_ptr<AudioRegion> newout)
+       : AudioRegion (boost::dynamic_pointer_cast<const AudioRegion> (orig)),
+         _fade_in (orig->_fade_in),
+         _fade_out (orig->_fade_out)
 {
-       _active           = orig._active;
-       _in_update        = orig._in_update;
-       _length           = orig._length;
-       _position         = orig._position;
-       _anchor_point     = orig._anchor_point;
-       _follow_overlap   = orig._follow_overlap;
-       _fixed            = orig._fixed;
-       
+       _active           = orig->_active;
+       _in_update        = orig->_in_update;
+       _anchor_point     = orig->_anchor_point;
+       _follow_overlap   = orig->_follow_overlap;
+       _fixed            = orig->_fixed;
+
        _in = newin;
        _out = newout;
 
        // copied from Crossfade::initialize()
        _in_update = false;
-       
+
        _out->suspend_fade_out ();
        _in->suspend_fade_in ();
 
        overlap_type = _in->coverage (_out->position(), _out->last_frame());
+       layer_relation = (int32_t) (_in->layer() - _out->layer());
 
        // Let's make sure the fade isn't too long
-       set_length(_length);
+       set_xfade_length(_length);
 }
 
 
 Crossfade::~Crossfade ()
 {
-       cerr << "Deleting xfade @ " << this << endl;
-       Invalidated (this);
-       cerr << "invalidation signal sent\n";
+       notify_callbacks ();
 }
 
 void
 Crossfade::initialize ()
 {
+       /* merge source lists from regions */
+
+       _sources = _in->sources();
+       _sources.insert (_sources.end(), _out->sources().begin(), _out->sources().end());
+       _master_sources = _in->master_sources();
+       _master_sources.insert(_master_sources.end(), _out->master_sources().begin(), _out->master_sources().end());
+
        _in_update = false;
-       
+
        _out->suspend_fade_out ();
        _in->suspend_fade_in ();
 
        _fade_out.freeze ();
        _fade_out.clear ();
-       _fade_out.add (0.0, 1.0);
-       _fade_out.add ((_length * 0.1), 0.99);
-       _fade_out.add ((_length * 0.2), 0.97);
-       _fade_out.add ((_length * 0.8), 0.03);
-       _fade_out.add ((_length * 0.9), 0.01);
-       _fade_out.add (_length, 0.0);
-       _fade_out.thaw ();
-       
-       _fade_in.freeze ();
-       _fade_in.clear ();
-       _fade_in.add (0.0, 0.0);
-       _fade_in.add ((_length * 0.1),  0.01);
-       _fade_in.add ((_length * 0.2),  0.03);
-       _fade_in.add ((_length * 0.8),  0.97);
-       _fade_in.add ((_length * 0.9),  0.99);
-       _fade_in.add (_length, 1.0);
-       _fade_in.thaw ();
 
-       _in->StateChanged.connect (sigc::mem_fun (*this, &Crossfade::member_changed));
-       _out->StateChanged.connect (sigc::mem_fun (*this, &Crossfade::member_changed));
+#define EQUAL_POWER_MINUS_3DB
+#ifdef  EQUAL_POWER_MINUS_3DB
 
-       overlap_type = _in->coverage (_out->position(), _out->last_frame());
-}      
+       _fade_out.add ((_length * 0.000000), 1.000000);
+       _fade_out.add ((_length * 0.166667), 0.948859);
+       _fade_out.add ((_length * 0.333333), 0.851507);
+       _fade_out.add ((_length * 0.500000), 0.707946);
+       _fade_out.add ((_length * 0.666667), 0.518174);
+       _fade_out.add ((_length * 0.833333), 0.282192);
+       _fade_out.add ((_length * 1.000000), 0.000000);
 
-int
-Crossfade::compute (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioRegion> b, CrossfadeModel model)
-{
-       boost::shared_ptr<AudioRegion> top;
-       boost::shared_ptr<AudioRegion> bottom;
-       nframes_t short_xfade_length;
+#else // EQUAL_POWER_MINUS_6DB
 
-       short_xfade_length = _short_xfade_length; 
+       _fade_out.add ((_length * 0.000000), 1.000000);
+       _fade_out.add ((_length * 0.166667), 0.833033);
+       _fade_out.add ((_length * 0.333333), 0.666186);
+       _fade_out.add ((_length * 0.500000), 0.499459);
+       _fade_out.add ((_length * 0.666667), 0.332853);
+       _fade_out.add ((_length * 0.833333), 0.166366);
+       _fade_out.add ((_length * 1.000000), 0.000000);
+#endif
 
-       if (a->layer() < b->layer()) {
-               top = b;
-               bottom = a;
-       } else {
-               top = a;
-               bottom = b;
-       }
-       
-       /* first check for matching ends */
-       
-       if (top->first_frame() == bottom->first_frame()) {
-               
-               /* Both regions start at the same point */
-               
-               if (top->last_frame() < bottom->last_frame()) {
-                       
-                       /* top ends before bottom, so put an xfade
-                          in at the end of top.
-                       */
-                       
-                       /* [-------- top ---------- ]
-                         * {====== bottom =====================}
-                        */
-
-                       _in = bottom;
-                       _out = top;
-
-                       if (top->last_frame() < short_xfade_length) {
-                               _position = 0;
-                       } else {
-                               _position = top->last_frame() - short_xfade_length;
-                       }
-
-                       _length = min (short_xfade_length, top->length());
-                       _follow_overlap = false;
-                       _anchor_point = EndOfIn;
-                       _active = true;
-                       _fixed = true;
-
-               } else {
-                       /* top ends after (or same time) as bottom - no xfade
-                        */
-                       
-                       /* [-------- top ------------------------ ]
-                         * {====== bottom =====================}
-                        */
-
-                       throw NoCrossfadeHere();
-               }
-               
-       } else if (top->last_frame() == bottom->last_frame()) {
-               
-               /* Both regions end at the same point */
-               
-               if (top->first_frame() > bottom->first_frame()) {
-                       
-                       /* top starts after bottom, put an xfade in at the
-                          start of top
-                       */
-                       
-                       /*            [-------- top ---------- ]
-                         * {====== bottom =====================}
-                        */
+       _fade_out.thaw ();
 
-                       _in = top;
-                       _out = bottom;
-                       _position = top->first_frame();
-                       _length = min (short_xfade_length, top->length());
-                       _follow_overlap = false;
-                       _anchor_point = StartOfIn;
-                       _active = true;
-                       _fixed = true;
-                       
-               } else {
-                       /* top starts before bottom - no xfade
-                        */
+       _fade_in.freeze ();
+       _fade_in.clear ();
 
-                       /* [-------- top ------------------------ ]
-                         *    {====== bottom =====================}
-                        */
+#define EQUAL_POWER_MINUS_3DB
+#ifdef  EQUAL_POWER_MINUS_3DB
 
-                       throw NoCrossfadeHere();
-               }
+       _fade_in.add ((_length * 0.000000), 0.000000);
+       _fade_in.add ((_length * 0.166667), 0.282192);
+       _fade_in.add ((_length * 0.333333), 0.518174);
+       _fade_in.add ((_length * 0.500000), 0.707946);
+       _fade_in.add ((_length * 0.666667), 0.851507);
+       _fade_in.add ((_length * 0.833333), 0.948859);
+       _fade_in.add ((_length * 1.000000), 1.000000);
 
-       } else {
-       
-               /* OK, time to do more regular overlapping */
+#else // EQUAL_POWER_MINUS_SIX_DB
 
-               OverlapType ot = top->coverage (bottom->first_frame(), bottom->last_frame());
+       _fade_in.add ((_length * 0.000000), 0.000000);
+       _fade_in.add ((_length * 0.166667), 0.166366);
+       _fade_in.add ((_length * 0.333333), 0.332853);
+       _fade_in.add ((_length * 0.500000), 0.499459);
+       _fade_in.add ((_length * 0.666667), 0.666186);
+       _fade_in.add ((_length * 0.833333), 0.833033);
+       _fade_in.add ((_length * 1.000000), 1.000000);
 
-               switch (ot) {
-               case OverlapNone:
-                       /* should be NOTREACHED as a precondition of creating
-                          a new crossfade, but we need to handle it here.
-                       */
-                       throw NoCrossfadeHere();
-                       break;
-                       
-               case OverlapInternal:
-               case OverlapExternal:
-                       /* should be NOTREACHED because of tests above */
-                       throw NoCrossfadeHere();
-                       break;
-                       
-               case OverlapEnd: /* top covers start of bottom but ends within it */
+#endif
 
-                       /* [---- top ------------------------] 
-                        *                { ==== bottom ============ } 
-                        */ 
+       _fade_in.thaw ();
 
-                       _in = bottom;
-                       _out = top;
-                       _position = bottom->first_frame();
-                       _anchor_point = StartOfIn;
+       overlap_type = _in->coverage (_out->position(), _out->last_frame());
+       layer_relation = (int32_t) (_in->layer() - _out->layer());
+}
 
-                       if (model == FullCrossfade) {
-                               _length = _out->first_frame() + _out->length() - _in->first_frame();
-                               /* leave active alone */
-                               _follow_overlap = true;
-                       } else {
-                               _length = min (short_xfade_length, top->length());
-                               _active = true;
-                               _follow_overlap = false;
-                               
-                       }
-                       break;
-                       
-               case OverlapStart:   /* top starts within bottom but covers bottom's end */
+nframes_t
+Crossfade::read_raw_internal (Sample* /*buf*/, sframes_t /*start*/, nframes_t cnt, int /*channel*/) const
+{
+       // FIXME: Why is this disabled?
+#if 0
+       Sample* mixdown = new Sample[cnt];
+       float* gain = new float[cnt];
+       nframes_t ret;
 
-                       /*                   { ==== top ============ } 
-                        *   [---- bottom -------------------] 
-                        */
+       ret = read_at (buf, mixdown, gain, start, cnt, chan_n, cnt);
 
-                       _in = top;
-                       _out = bottom;
-                       _position = top->first_frame();
-                       _anchor_point = StartOfIn;
+       delete [] mixdown;
+       delete [] gain;
 
-                       if (model == FullCrossfade) {
-                               _length = _out->first_frame() + _out->length() - _in->first_frame();
-                               /* leave active alone */
-                               _follow_overlap = true;
-                       } else {
-                               _length = min (short_xfade_length, top->length());
-                               _active = true;
-                               _follow_overlap = false;
-                               
-                       }
-                       
-                       break;
-               }
-       }
-       
-       return 0;
+       return ret;
+#endif
+       return cnt;
 }
 
-nframes_t 
-Crossfade::read_at (Sample *buf, Sample *mixdown_buffer, 
-                   float *gain_buffer, nframes_t start, nframes_t cnt, uint32_t chan_n,
-                   nframes_t read_frames, nframes_t skip_frames)
+nframes_t
+Crossfade::read_at (Sample *buf, Sample *mixdown_buffer,
+                   float *gain_buffer, sframes_t start, nframes_t cnt, uint32_t chan_n,
+                   nframes_t read_frames, nframes_t skip_frames) const
 {
        nframes_t offset;
        nframes_t to_write;
@@ -426,32 +313,39 @@ Crossfade::read_at (Sample *buf, Sample *mixdown_buffer,
                } else {
                        return 0;
                }
-               
+
                start = _position;
                buf += offset;
                to_write = min (_length, cnt);
 
        } else {
-               
-               to_write = min (_length - (start - _position), cnt);
-               
+
+               to_write = min (nframes_t(_length - (start - _position)), cnt);
+
        }
 
        offset = start - _position;
 
+       /* Prevent data from piling up inthe crossfade buffers when reading a transparent region */
+       if (!(_out->opaque())) {
+               memset (crossfade_buffer_out, 0, sizeof (Sample) * to_write);
+       } else if (!(_in->opaque())) {
+               memset (crossfade_buffer_in, 0, sizeof (Sample) * to_write);
+       }
+
        _out->read_at (crossfade_buffer_out, mixdown_buffer, gain_buffer, start, to_write, chan_n, read_frames, skip_frames);
        _in->read_at (crossfade_buffer_in, mixdown_buffer, gain_buffer, start, to_write, chan_n, read_frames, skip_frames);
 
        float* fiv = new float[to_write];
        float* fov = new float[to_write];
 
-       _fade_in.get_vector (offset, offset+to_write, fiv, to_write);
-       _fade_out.get_vector (offset, offset+to_write, fov, to_write);
+       _fade_in.curve().get_vector (offset, offset+to_write, fiv, to_write);
+       _fade_out.curve().get_vector (offset, offset+to_write, fov, to_write);
 
        /* note: although we have not explicitly taken into account the return values
           from _out->read_at() or _in->read_at(), the length() function does this
           implicitly. why? because it computes a value based on the in+out regions'
-          position and length, and so we know precisely how much data they could return. 
+          position and length, and so we know precisely how much data they could return.
        */
 
        for (nframes_t n = 0; n < to_write; ++n) {
@@ -462,9 +356,9 @@ Crossfade::read_at (Sample *buf, Sample *mixdown_buffer,
        delete [] fiv;
 
        return to_write;
-}      
+}
 
-OverlapType 
+OverlapType
 Crossfade::coverage (nframes_t start, nframes_t end) const
 {
        nframes_t my_end = _position + _length;
@@ -497,42 +391,73 @@ bool
 Crossfade::refresh ()
 {
        /* crossfades must be between non-muted regions */
-       
+
        if (_out->muted() || _in->muted()) {
-               Invalidated (this);
+               Invalidated (shared_from_this ());
                return false;
        }
 
-       /* overlap type must be Start, End or External */
+       /* Top layer shouldn't be transparent */
 
-       OverlapType ot;
-       
-       ot = _in->coverage (_out->first_frame(), _out->last_frame());
-       
-       switch (ot) {
-       case OverlapNone:
-       case OverlapInternal:
-               Invalidated (this);
+       if (!((layer_relation > 0 ? _in : _out)->opaque())) {
+               Invalidated (shared_from_this());
                return false;
-               
-       default:
-               break;
        }
-               
-       /* overlap type must not have altered */
-       
-       if (ot != overlap_type) {
-               Invalidated (this);
+
+       /* layer ordering cannot change */
+
+       int32_t new_layer_relation = (int32_t) (_in->layer() - _out->layer());
+
+       if (new_layer_relation * layer_relation < 0) { // different sign, layers rotated
+               Invalidated (shared_from_this ());
+               return false;
+       }
+
+       OverlapType ot = _in->coverage (_out->first_frame(), _out->last_frame());
+
+       if (ot == OverlapNone) {
+               Invalidated (shared_from_this ());
                return false;
-       } 
+       }
+
+       bool send_signal;
+
+       if (ot != overlap_type) {
+
+               if (_follow_overlap) {
+
+                       try {
+                               compute (_in, _out, _session.config.get_xfade_model());
+                       }
+
+                       catch (NoCrossfadeHere& err) {
+                               Invalidated (shared_from_this ());
+                               return false;
+                       }
+
+                       send_signal = true;
+
+               } else {
+                       Invalidated (shared_from_this ());
+                       return false;
+               }
+
+       } else {
+
+               send_signal = update ();
+       }
+
+       if (send_signal) {
+               StateChanged (BoundsChanged); /* EMIT SIGNAL */
+       }
 
-       /* time to update */
+       _in_update = false;
 
-       return update (false);
+       return true;
 }
 
 bool
-Crossfade::update (bool force)
+Crossfade::update ()
 {
        nframes_t newlen;
 
@@ -543,67 +468,206 @@ Crossfade::update (bool force)
        }
 
        if (newlen == 0) {
-               Invalidated (this);
+               Invalidated (shared_from_this ());
                return false;
        }
 
        _in_update = true;
 
-       if (force || (_follow_overlap && newlen != _length) || (_length > newlen)) {
+       if ((_follow_overlap && newlen != _length) || (_length > newlen)) {
 
                double factor =  newlen / (double) _length;
-               
+
                _fade_out.x_scale (factor);
                _fade_in.x_scale (factor);
-               
-               _length = newlen;
 
-       } 
+               _length = newlen;
+       }
 
        switch (_anchor_point) {
        case StartOfIn:
-               if (_position != _in->first_frame()) {
-                       _position = _in->first_frame();
-               }
+               _position = _in->first_frame();
                break;
 
        case EndOfIn:
-               if (_position != _in->last_frame() - _length) {
-                       _position = _in->last_frame() - _length;
-               }
+               _position = _in->last_frame() - _length;
                break;
 
        case EndOfOut:
-               if (_position != _out->last_frame() - _length) {
-                       _position = _out->last_frame() - _length;
-               }
+               _position = _out->last_frame() - _length;
        }
 
-       /* UI's may need to know that the overlap changed even 
-          though the xfade length did not.
-       */
-       
-       StateChanged (BoundsChanged); /* EMIT SIGNAL */
-
-       _in_update = false;
-
        return true;
 }
 
-void
-Crossfade::member_changed (Change what_changed)
+int
+Crossfade::compute (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioRegion> b, CrossfadeModel model)
 {
-       Change what_we_care_about = Change (Region::MuteChanged|
-                                           Region::LayerChanged|
-                                           BoundsChanged);
+       boost::shared_ptr<AudioRegion> top;
+       boost::shared_ptr<AudioRegion> bottom;
+       nframes_t short_xfade_length;
 
-       if (what_changed & what_we_care_about) {
-               refresh ();
+       short_xfade_length = _short_xfade_length;
+
+       if (a->layer() < b->layer()) {
+               top = b;
+               bottom = a;
+       } else {
+               top = a;
+               bottom = b;
        }
+
+       /* first check for matching ends */
+
+       if (top->first_frame() == bottom->first_frame()) {
+
+               /* Both regions start at the same point */
+
+               if (top->last_frame() < bottom->last_frame()) {
+
+                       /* top ends before bottom, so put an xfade
+                          in at the end of top.
+                       */
+
+                       /* [-------- top ---------- ]
+                         * {====== bottom =====================}
+                        */
+
+                       _in = bottom;
+                       _out = top;
+
+                       if (top->last_frame() < short_xfade_length) {
+                               _position = 0;
+                       } else {
+                               _position = top->last_frame() - short_xfade_length;
+                       }
+
+                       _length = min (short_xfade_length, top->length());
+                       _follow_overlap = false;
+                       _anchor_point = EndOfIn;
+                       _active = true;
+                       _fixed = true;
+
+               } else {
+                       /* top ends after (or same time) as bottom - no xfade
+                        */
+
+                       /* [-------- top ------------------------ ]
+                         * {====== bottom =====================}
+                        */
+
+                       throw NoCrossfadeHere();
+               }
+
+       } else if (top->last_frame() == bottom->last_frame()) {
+
+               /* Both regions end at the same point */
+
+               if (top->first_frame() > bottom->first_frame()) {
+
+                       /* top starts after bottom, put an xfade in at the
+                          start of top
+                       */
+
+                       /*            [-------- top ---------- ]
+                         * {====== bottom =====================}
+                        */
+
+                       _in = top;
+                       _out = bottom;
+                       _position = top->first_frame();
+                       _length = min (short_xfade_length, top->length());
+                       _follow_overlap = false;
+                       _anchor_point = StartOfIn;
+                       _active = true;
+                       _fixed = true;
+
+               } else {
+                       /* top starts before bottom - no xfade
+                        */
+
+                       /* [-------- top ------------------------ ]
+                         *    {====== bottom =====================}
+                        */
+
+                       throw NoCrossfadeHere();
+               }
+
+       } else {
+
+               /* OK, time to do more regular overlapping */
+
+               OverlapType ot = top->coverage (bottom->first_frame(), bottom->last_frame());
+
+               switch (ot) {
+               case OverlapNone:
+                       /* should be NOTREACHED as a precondition of creating
+                          a new crossfade, but we need to handle it here.
+                       */
+                       throw NoCrossfadeHere();
+                       break;
+
+               case OverlapInternal:
+               case OverlapExternal:
+                       /* should be NOTREACHED because of tests above */
+                       throw NoCrossfadeHere();
+                       break;
+
+               case OverlapEnd: /* top covers start of bottom but ends within it */
+
+                       /* [---- top ------------------------]
+                        *                { ==== bottom ============ }
+                        */
+
+                       _in = bottom;
+                       _out = top;
+                       _anchor_point = EndOfOut;
+
+                       if (model == FullCrossfade) {
+                               _position = bottom->first_frame(); // "{"
+                               _length = _out->first_frame() + _out->length() - _in->first_frame();
+                               /* leave active alone */
+                               _follow_overlap = true;
+                       } else {
+                               _length = min (short_xfade_length, top->length());
+                               _position = top->last_frame() - _length;  // "]" - length
+                               _active = true;
+                               _follow_overlap = false;
+
+                       }
+                       break;
+
+               case OverlapStart:   /* top starts within bottom but covers bottom's end */
+
+                       /*                   { ==== top ============ }
+                        *   [---- bottom -------------------]
+                        */
+
+                       _in = top;
+                       _out = bottom;
+                       _position = top->first_frame();
+                       _anchor_point = StartOfIn;
+
+                       if (model == FullCrossfade) {
+                               _length = _out->first_frame() + _out->length() - _in->first_frame();
+                               /* leave active alone */
+                               _follow_overlap = true;
+                       } else {
+                               _length = min (short_xfade_length, top->length());
+                               _active = true;
+                               _follow_overlap = false;
+
+                       }
+
+                       break;
+               }
+       }
+
+       return 0;
 }
 
 XMLNode&
-Crossfade::get_state () 
+Crossfade::get_state ()
 {
        XMLNode* node = new XMLNode (X_("Crossfade"));
        XMLNode* child;
@@ -679,7 +743,7 @@ Crossfade::set_state (const XMLNode& node)
        }
 
        if ((prop = node.property ("active")) != 0) {
-               bool x = (prop->value() == "yes");
+               bool x = string_is_affirmative (prop->value());
                if (x != _active) {
                        _active = x;
                        what_changed = Change (what_changed | ActiveChanged);
@@ -689,13 +753,13 @@ Crossfade::set_state (const XMLNode& node)
        }
 
        if ((prop = node.property ("follow-overlap")) != 0) {
-               _follow_overlap = (prop->value() == "yes");
+               _follow_overlap = string_is_affirmative (prop->value());
        } else {
                _follow_overlap = false;
        }
 
        if ((prop = node.property ("fixed")) != 0) {
-               _fixed = (prop->value() == "yes");
+               _fixed = string_is_affirmative (prop->value());
        } else {
                _fixed = false;
        }
@@ -715,11 +779,11 @@ Crossfade::set_state (const XMLNode& node)
                }
 
        } else {
-               
+
                /* XXX this branch is legacy code from before
                   the point where we stored xfade lengths.
                */
-               
+
                if ((_length = overlap_length()) == 0) {
                        throw failed_constructor();
                }
@@ -728,26 +792,26 @@ Crossfade::set_state (const XMLNode& node)
        if ((fi = find_named_node (node, "FadeIn")) == 0) {
                return -1;
        }
-       
+
        if ((fo = find_named_node (node, "FadeOut")) == 0) {
                return -1;
        }
 
        /* fade in */
-       
+
        _fade_in.freeze ();
        _fade_in.clear ();
-       
+
        children = fi->children();
-       
+
        for (i = children.begin(); i != children.end(); ++i) {
                if ((*i)->name() == "point") {
                        nframes_t x;
                        float y;
-                       
+
                        prop = (*i)->property ("x");
                        sscanf (prop->value().c_str(), "%" PRIu32, &x);
-                       
+
                        prop = (*i)->property ("y");
                        sscanf (prop->value().c_str(), "%f", &y);
 
@@ -756,14 +820,14 @@ Crossfade::set_state (const XMLNode& node)
        }
 
        _fade_in.thaw ();
-       
+
         /* fade out */
-       
-       _fade_in.freeze ();
+
+       _fade_out.freeze ();
        _fade_out.clear ();
 
        children = fo->children();
-       
+
        for (i = children.begin(); i != children.end(); ++i) {
                if ((*i)->name() == "point") {
                        nframes_t x;
@@ -775,7 +839,7 @@ Crossfade::set_state (const XMLNode& node)
 
                        prop = (*i)->property ("y");
                        sscanf (prop->value().c_str(), "%f", &y);
-                       
+
                        _fade_out.add (x, y);
                }
        }
@@ -803,18 +867,18 @@ Crossfade::set_follow_overlap (bool yn)
        _follow_overlap = yn;
 
        if (!yn) {
-               set_length (_short_xfade_length);
+               set_xfade_length (_short_xfade_length);
        } else {
-               set_length (_out->first_frame() + _out->length() - _in->first_frame());
+               set_xfade_length (_out->first_frame() + _out->length() - _in->first_frame());
        }
 
        StateChanged (FollowOverlapChanged);
 }
 
 nframes_t
-Crossfade::set_length (nframes_t len)
+Crossfade::set_xfade_length (nframes_t len)
 {
-       nframes_t limit;
+       nframes_t limit = 0;
 
        switch (_anchor_point) {
        case StartOfIn:
@@ -828,7 +892,7 @@ Crossfade::set_length (nframes_t len)
        case EndOfOut:
                limit = _out->length();
                break;
-               
+
        }
 
        len = min (limit, len);
@@ -839,7 +903,7 @@ Crossfade::set_length (nframes_t len)
        _fade_out.x_scale (factor);
        _fade_in.x_scale (factor);
        _in_update = false;
-       
+
        _length = len;
 
        StateChanged (LengthChanged);
@@ -861,9 +925,3 @@ Crossfade::set_short_xfade_length (nframes_t n)
 {
        _short_xfade_length = n;
 }
-
-void
-Crossfade::invalidate ()
-{
-       Invalidated (this); /* EMIT SIGNAL */
-}