Comment.
[ardour.git] / libs / ardour / crossfade.cc
index 2d46c0c01ae042dc48392aef9d6f6071a603a7cb..d2271030a5933c4c0c5c051c9a8106c885e3ac0f 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
 
 */
 
-#include <sigc++/bind.h>
 
 #include "pbd/stacktrace.h"
 
+#include "ardour/debug.h"
 #include "ardour/types.h"
 #include "ardour/crossfade.h"
 #include "ardour/crossfade_compare.h"
@@ -29,6 +29,7 @@
 #include "ardour/utils.h"
 #include "ardour/session.h"
 #include "ardour/source.h"
+#include "ardour/region_factory.h"
 
 #include "i18n.h"
 #include <locale.h>
@@ -37,9 +38,7 @@ using namespace std;
 using namespace ARDOUR;
 using namespace PBD;
 
-nframes_t Crossfade::_short_xfade_length = 0;
-Change Crossfade::ActiveChanged = new_change();
-Change Crossfade::FollowOverlapChanged = new_change();
+framecnt_t Crossfade::_short_xfade_length = 0;
 
 /* XXX if and when we ever implement parallel processing of the process()
    callback, these will need to be handled on a per-thread basis.
@@ -48,8 +47,27 @@ Change Crossfade::FollowOverlapChanged = new_change();
 Sample* Crossfade::crossfade_buffer_out = 0;
 Sample* Crossfade::crossfade_buffer_in = 0;
 
+
+#define CROSSFADE_DEFAULT_PROPERTIES \
+       _active (Properties::active, _session.config.get_xfades_active ()) \
+       , _follow_overlap (Properties::follow_overlap, false)
+
+
+namespace ARDOUR {
+       namespace Properties {
+               PropertyDescriptor<bool> follow_overlap;
+       }
+}
+
+void
+Crossfade::make_property_quarks ()
+{
+       Properties::follow_overlap.property_id = g_quark_from_static_string (X_("follow-overlap"));
+        DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for follow-overlap = %1\n",     Properties::follow_overlap.property_id));
+}
+
 void
-Crossfade::set_buffer_size (nframes_t sz)
+Crossfade::set_buffer_size (framecnt_t sz)
 {
        delete [] crossfade_buffer_out;
        crossfade_buffer_out = 0;
@@ -69,30 +87,30 @@ Crossfade::operator== (const Crossfade& other)
        return (_in == other._in) && (_out == other._out);
 }
 
-Crossfade::Crossfade (boost::shared_ptr<AudioRegion> in, boost::shared_ptr<AudioRegion> out, 
-                     nframes_t length,
-                     nframes_t position,
+Crossfade::Crossfade (boost::shared_ptr<AudioRegion> in, boost::shared_ptr<AudioRegion> out,
+                     framecnt_t length,
+                     framepos_t position,
                      AnchorPoint ap)
-       : 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
+       : AudioRegion (in->session(), position, length, in->name() + string ("<>") + out->name())
+       , CROSSFADE_DEFAULT_PROPERTIES
+       , _fade_in (Evoral::Parameter(FadeInAutomation)) // linear (gain coefficient) => -inf..+6dB
+       , _fade_out (Evoral::Parameter(FadeOutAutomation)) // linear (gain coefficient) => -inf..+6dB
 
 {
        _in = in;
        _out = out;
        _anchor_point = ap;
-       _follow_overlap = false;
-
-       _active = _session.config.get_xfades_active ();
        _fixed = true;
+        _follow_overlap = false;
 
        initialize ();
 }
 
 Crossfade::Crossfade (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioRegion> b, CrossfadeModel model, bool act)
-       : 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
+       : AudioRegion (a->session(), 0, 0, a->name() + string ("<>") + b->name())
+       , CROSSFADE_DEFAULT_PROPERTIES
+       , _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;
@@ -107,14 +125,15 @@ Crossfade::Crossfade (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioR
        initialize ();
 }
 
-Crossfade::Crossfade (const Playlist& playlist, XMLNode& node)
-       : 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
+Crossfade::Crossfade (const Playlist& playlist, XMLNode const & node)
+       : AudioRegion (playlist.session(), 0, 0, "unnamed crossfade")
+       , CROSSFADE_DEFAULT_PROPERTIES
+       , _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;
+       XMLProperty const * prop;
        LocaleGuard lg (X_("POSIX"));
 
        /* we have to find the in/out regions before we can do anything else */
@@ -123,15 +142,24 @@ 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) {
-               error << string_compose (_("Crossfade: no \"in\" region %1 found in playlist %2"), id, playlist.name())
+       r = playlist.find_region (id);
+
+       if (!r) {
+               /* the `in' region is not in a playlist, which probably means that this crossfade
+                  is in the undo record, so we have to find the region in the global region map.
+               */
+               r = RegionFactory::region_by_id (id);
+       }
+       
+       if (!r) {
+               error << string_compose (_("Crossfade: no \"in\" region %1 found in playlist %2 nor in region map"), id, playlist.name())
                      << endmsg;
                throw failed_constructor();
        }
-       
+
        if ((_in = boost::dynamic_pointer_cast<AudioRegion> (r)) == 0) {
                throw failed_constructor();
        }
@@ -143,12 +171,18 @@ Crossfade::Crossfade (const Playlist& playlist, XMLNode& node)
 
        PBD::ID id2 (prop->value());
 
-       if ((r = playlist.find_region (id2)) == 0) {
-               error << string_compose (_("Crossfade: no \"out\" region %1 found in playlist %2"), id2, playlist.name())
+       r = playlist.find_region (id2);
+
+       if (!r) {
+               r = RegionFactory::region_by_id (id2);
+       }
+       
+       if (!r) {
+               error << string_compose (_("Crossfade: no \"out\" region %1 found in playlist %2 nor in region map"), id2, playlist.name())
                      << endmsg;
                throw failed_constructor();
        }
-       
+
        if ((_out = boost::dynamic_pointer_cast<AudioRegion> (r)) == 0) {
                throw failed_constructor();
        }
@@ -156,29 +190,30 @@ Crossfade::Crossfade (const Playlist& playlist, XMLNode& node)
        _length = 0;
        initialize();
        _active = true;
-       
-       if (set_state (node)) {
+
+       if (set_state (node, Stateful::loading_state_version)) {
                throw failed_constructor();
        }
 }
 
 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)
+       : AudioRegion (boost::dynamic_pointer_cast<const AudioRegion> (orig), 0, true)
+       , CROSSFADE_DEFAULT_PROPERTIES
+       , _fade_in (orig->_fade_in)
+       , _fade_out (orig->_fade_out)
 {
        _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 ();
 
@@ -192,7 +227,6 @@ Crossfade::Crossfade (boost::shared_ptr<Crossfade> orig, boost::shared_ptr<Audio
 
 Crossfade::~Crossfade ()
 {
-       notify_callbacks ();
 }
 
 void
@@ -202,11 +236,20 @@ Crossfade::initialize ()
 
        _sources = _in->sources();
        _sources.insert (_sources.end(), _out->sources().begin(), _out->sources().end());
+
+        for (SourceList::iterator i = _sources.begin(); i != _sources.end(); ++i) {
+                (*i)->inc_use_count ();
+        }
+        
        _master_sources = _in->master_sources();
        _master_sources.insert(_master_sources.end(), _out->master_sources().begin(), _out->master_sources().end());
-       
+
+        for (SourceList::iterator i = _master_sources.begin(); i != _master_sources.end(); ++i) {
+                (*i)->inc_use_count ();
+        }
+
        _in_update = false;
-       
+
        _out->suspend_fade_out ();
        _in->suspend_fade_in ();
 
@@ -236,7 +279,7 @@ Crossfade::initialize ()
 #endif
 
        _fade_out.thaw ();
-       
+
        _fade_in.freeze ();
        _fade_in.clear ();
 
@@ -267,34 +310,30 @@ Crossfade::initialize ()
 
        overlap_type = _in->coverage (_out->position(), _out->last_frame());
        layer_relation = (int32_t) (_in->layer() - _out->layer());
-}      
+}
 
-nframes_t 
-Crossfade::read_raw_internal (Sample* /*buf*/, sframes_t /*start*/, nframes_t cnt, int /*channel*/) const
+framecnt_t
+Crossfade::read_raw_internal (Sample* buf, framecnt_t start, framecnt_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;
-
-       ret = read_at (buf, mixdown, gain, start, cnt, chan_n, cnt);
+       framecnt_t ret;
+       
+       ret = read_at (buf, mixdown, gain, start, cnt, channel, cnt);
 
        delete [] mixdown;
        delete [] gain;
 
        return ret;
-#endif
-       return cnt;
 }
 
-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
+framecnt_t
+Crossfade::read_at (Sample *buf, Sample *mixdown_buffer,
+                   float *gain_buffer, framepos_t start, framecnt_t cnt, uint32_t chan_n,
+                   framecnt_t read_frames, framecnt_t skip_frames) const
 {
-       nframes_t offset;
-       nframes_t to_write;
+       frameoffset_t offset;
+       framecnt_t to_write;
 
        if (!_active) {
                return 0;
@@ -313,15 +352,15 @@ Crossfade::read_at (Sample *buf, Sample *mixdown_buffer,
                } else {
                        return 0;
                }
-               
+
                start = _position;
                buf += offset;
-               to_write = min (_length, cnt);
+               to_write = min (_length.val(), cnt);
 
        } else {
-               
-               to_write = min (nframes_t(_length - (start - _position)), cnt);
-               
+
+               to_write = min ((_length - (start - _position)), cnt);
+
        }
 
        offset = start - _position;
@@ -345,10 +384,10 @@ Crossfade::read_at (Sample *buf, Sample *mixdown_buffer,
        /* 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) {
+       for (framecnt_t n = 0; n < to_write; ++n) {
                buf[n] = (crossfade_buffer_out[n] * fov[n]) + (crossfade_buffer_in[n] * fiv[n]);
        }
 
@@ -356,12 +395,12 @@ Crossfade::read_at (Sample *buf, Sample *mixdown_buffer,
        delete [] fiv;
 
        return to_write;
-}      
+}
 
-OverlapType 
-Crossfade::coverage (nframes_t start, nframes_t end) const
+OverlapType
+Crossfade::coverage (framepos_t start, framepos_t end) const
 {
-       nframes_t my_end = _position + _length;
+       framepos_t my_end = _position + _length;
 
        if ((start >= _position) && (end <= my_end)) {
                return OverlapInternal;
@@ -383,7 +422,7 @@ Crossfade::set_active (bool yn)
 {
        if (_active != yn) {
                _active = yn;
-               StateChanged (ActiveChanged);
+               PropertyChanged (PropertyChange (Properties::active));
        }
 }
 
@@ -391,14 +430,14 @@ bool
 Crossfade::refresh ()
 {
        /* crossfades must be between non-muted regions */
-       
+
        if (_out->muted() || _in->muted()) {
                Invalidated (shared_from_this ());
                return false;
        }
 
        /* Top layer shouldn't be transparent */
-       
+
        if (!((layer_relation > 0 ? _in : _out)->opaque())) {
                Invalidated (shared_from_this());
                return false;
@@ -418,7 +457,7 @@ Crossfade::refresh ()
        if (ot == OverlapNone) {
                Invalidated (shared_from_this ());
                return false;
-       } 
+       }
 
        bool send_signal;
 
@@ -428,7 +467,7 @@ Crossfade::refresh ()
 
                        try {
                                compute (_in, _out, _session.config.get_xfade_model());
-                       } 
+                       }
 
                        catch (NoCrossfadeHere& err) {
                                Invalidated (shared_from_this ());
@@ -448,7 +487,11 @@ Crossfade::refresh ()
        }
 
        if (send_signal) {
-               StateChanged (BoundsChanged); /* EMIT SIGNAL */
+               PropertyChange bounds;
+               bounds.add (Properties::start);
+               bounds.add (Properties::position);
+               bounds.add (Properties::length);
+               PropertyChanged (bounds); /* EMIT SIGNAL */
        }
 
        _in_update = false;
@@ -459,40 +502,40 @@ Crossfade::refresh ()
 bool
 Crossfade::update ()
 {
-       nframes_t newlen;
-       
+       framecnt_t newlen;
+
        if (_follow_overlap) {
                newlen = _out->first_frame() + _out->length() - _in->first_frame();
        } else {
                newlen = _length;
        }
-       
+
        if (newlen == 0) {
                Invalidated (shared_from_this ());
                return false;
        }
-       
+
        _in_update = true;
-       
+
        if ((_follow_overlap && newlen != _length) || (_length > newlen)) {
-               
+
                double factor =  newlen / (double) _length;
-               
+
                _fade_out.x_scale (factor);
                _fade_in.x_scale (factor);
-               
+
                _length = newlen;
-       } 
-               
+       }
+
        switch (_anchor_point) {
        case StartOfIn:
                _position = _in->first_frame();
                break;
-               
+
        case EndOfIn:
                _position = _in->last_frame() - _length;
                break;
-               
+
        case EndOfOut:
                _position = _out->last_frame() - _length;
        }
@@ -505,9 +548,9 @@ Crossfade::compute (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioReg
 {
        boost::shared_ptr<AudioRegion> top;
        boost::shared_ptr<AudioRegion> bottom;
-       nframes_t short_xfade_length;
+       framecnt_t short_xfade_length;
 
-       short_xfade_length = _short_xfade_length; 
+       short_xfade_length = _short_xfade_length;
 
        if (a->layer() < b->layer()) {
                top = b;
@@ -516,19 +559,19 @@ Crossfade::compute (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioReg
                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 =====================}
                         */
@@ -551,24 +594,24 @@ Crossfade::compute (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioReg
                } 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 =====================}
                         */
@@ -581,7 +624,7 @@ Crossfade::compute (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioReg
                        _anchor_point = StartOfIn;
                        _active = true;
                        _fixed = true;
-                       
+
                } else {
                        /* top starts before bottom - no xfade
                         */
@@ -594,7 +637,7 @@ Crossfade::compute (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioReg
                }
 
        } else {
-       
+
                /* OK, time to do more regular overlapping */
 
                OverlapType ot = top->coverage (bottom->first_frame(), bottom->last_frame());
@@ -606,18 +649,18 @@ Crossfade::compute (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioReg
                        */
                        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 ============ } 
-                        */ 
+                       /* [---- top ------------------------]
+                        *                { ==== bottom ============ }
+                        */
 
                        _in = bottom;
                        _out = top;
@@ -630,17 +673,17 @@ Crossfade::compute (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioReg
                                _follow_overlap = true;
                        } else {
                                _length = min (short_xfade_length, top->length());
-                               _position = top->last_frame() - _length;  // "]" - 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 -------------------] 
+                       /*                   { ==== top ============ }
+                        *   [---- bottom -------------------]
                         */
 
                        _in = top;
@@ -656,24 +699,26 @@ Crossfade::compute (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioReg
                                _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;
        char buf[64];
        LocaleGuard lg (X_("POSIX"));
 
+       id().print (buf, sizeof (buf));
+       node->add_property ("id", buf);
        _out->id().print (buf, sizeof (buf));
        node->add_property ("out", buf);
        _in->id().print (buf, sizeof (buf));
@@ -681,11 +726,11 @@ Crossfade::get_state ()
        node->add_property ("active", (_active ? "yes" : "no"));
        node->add_property ("follow-overlap", (_follow_overlap ? "yes" : "no"));
        node->add_property ("fixed", (_fixed ? "yes" : "no"));
-       snprintf (buf, sizeof(buf), "%" PRIu32, _length);
+       snprintf (buf, sizeof(buf), "%" PRId64, _length.val());
        node->add_property ("length", buf);
        snprintf (buf, sizeof(buf), "%" PRIu32, (uint32_t) _anchor_point);
        node->add_property ("anchor-point", buf);
-       snprintf (buf, sizeof(buf), "%" PRIu32, (uint32_t) _position);
+       snprintf (buf, sizeof(buf), "%" PRId64, _position.val());
        node->add_property ("position", buf);
 
        child = node->add_child ("FadeIn");
@@ -695,7 +740,7 @@ Crossfade::get_state ()
 
                pnode = new XMLNode ("point");
 
-               snprintf (buf, sizeof (buf), "%" PRIu32, (nframes_t) floor ((*ii)->when));
+               snprintf (buf, sizeof (buf), "%" PRId64, (framepos_t) floor ((*ii)->when));
                pnode->add_property ("x", buf);
                snprintf (buf, sizeof (buf), "%.12g", (*ii)->value);
                pnode->add_property ("y", buf);
@@ -709,7 +754,7 @@ Crossfade::get_state ()
 
                pnode = new XMLNode ("point");
 
-               snprintf (buf, sizeof (buf), "%" PRIu32, (nframes_t) floor ((*ii)->when));
+               snprintf (buf, sizeof (buf), "%" PRId64, (framepos_t) floor ((*ii)->when));
                pnode->add_property ("x", buf);
                snprintf (buf, sizeof (buf), "%.12g", (*ii)->value);
                pnode->add_property ("y", buf);
@@ -720,7 +765,7 @@ Crossfade::get_state ()
 }
 
 int
-Crossfade::set_state (const XMLNode& node)
+Crossfade::set_state (const XMLNode& node, int /*version*/)
 {
        XMLNodeConstIterator i;
        XMLNodeList children;
@@ -728,14 +773,18 @@ Crossfade::set_state (const XMLNode& node)
        XMLNode* fo;
        const XMLProperty* prop;
        LocaleGuard lg (X_("POSIX"));
-       Change what_changed = Change (0);
-       nframes_t val;
+       PropertyChange what_changed;
+       framepos_t val;
+
+       if ((prop = node.property (X_("id")))) {
+               _id = prop->value();
+       }
 
        if ((prop = node.property ("position")) != 0) {
-               sscanf (prop->value().c_str(), "%" PRIu32, &val);
+               sscanf (prop->value().c_str(), "%" PRId64, &val);
                if (val != _position) {
                        _position = val;
-                       what_changed = Change (what_changed | PositionChanged);
+                       what_changed.add (Properties::position);
                }
        } else {
                warning << _("old-style crossfade information - no position information") << endmsg;
@@ -746,7 +795,7 @@ Crossfade::set_state (const XMLNode& node)
                bool x = string_is_affirmative (prop->value());
                if (x != _active) {
                        _active = x;
-                       what_changed = Change (what_changed | ActiveChanged);
+                       what_changed.add (Properties::active);
                }
        } else {
                _active = true;
@@ -772,18 +821,18 @@ Crossfade::set_state (const XMLNode& node)
 
        if ((prop = node.property ("length")) != 0) {
 
-               sscanf (prop->value().c_str(), "%" PRIu32, &val);
+               sscanf (prop->value().c_str(), "%" PRId64, &val);
                if (val != _length) {
-                       _length = atol (prop->value().c_str());
-                       what_changed = Change (what_changed | LengthChanged);
+                       _length = val;
+                       what_changed.add (Properties::length);
                }
 
        } else {
-               
+
                /* XXX this branch is legacy code from before
                   the point where we stored xfade lengths.
                */
-               
+
                if ((_length = overlap_length()) == 0) {
                        throw failed_constructor();
                }
@@ -792,26 +841,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;
+                       framepos_t x;
                        float y;
-                       
+
                        prop = (*i)->property ("x");
-                       sscanf (prop->value().c_str(), "%" PRIu32, &x);
-                       
+                       sscanf (prop->value().c_str(), "%" PRId64, &x);
+
                        prop = (*i)->property ("y");
                        sscanf (prop->value().c_str(), "%f", &y);
 
@@ -819,34 +868,41 @@ Crossfade::set_state (const XMLNode& node)
                }
        }
 
+        _fade_in.front()->value = 0.0;
+        _fade_in.back()->value = 1.0;
+
        _fade_in.thaw ();
-       
+
         /* fade out */
-       
+
        _fade_out.freeze ();
        _fade_out.clear ();
 
        children = fo->children();
-       
+
        for (i = children.begin(); i != children.end(); ++i) {
                if ((*i)->name() == "point") {
-                       nframes_t x;
+                       framepos_t x;
                        float y;
                        XMLProperty* prop;
 
                        prop = (*i)->property ("x");
-                       sscanf (prop->value().c_str(), "%" PRIu32, &x);
+                       sscanf (prop->value().c_str(), "%" PRId64, &x);
 
                        prop = (*i)->property ("y");
                        sscanf (prop->value().c_str(), "%f", &y);
-                       
+
                        _fade_out.add (x, y);
                }
        }
 
+        _fade_out.front()->value = 1.0;
+        _fade_out.back()->value = 0.0;
+
        _fade_out.thaw ();
 
-       StateChanged (what_changed); /* EMIT SIGNAL */
+       PropertyChanged (what_changed); /* EMIT SIGNAL */
+       FadesChanged (); /* EMIT SIGNAL */
 
        return 0;
 }
@@ -872,13 +928,13 @@ Crossfade::set_follow_overlap (bool yn)
                set_xfade_length (_out->first_frame() + _out->length() - _in->first_frame());
        }
 
-       StateChanged (FollowOverlapChanged);
+       PropertyChanged (PropertyChange (Properties::follow_overlap));
 }
 
-nframes_t
-Crossfade::set_xfade_length (nframes_t len)
+framecnt_t
+Crossfade::set_xfade_length (framecnt_t len)
 {
-       nframes_t limit = 0;
+       framecnt_t limit = 0;
 
        switch (_anchor_point) {
        case StartOfIn:
@@ -892,7 +948,7 @@ Crossfade::set_xfade_length (nframes_t len)
        case EndOfOut:
                limit = _out->length();
                break;
-               
+
        }
 
        len = min (limit, len);
@@ -903,15 +959,15 @@ Crossfade::set_xfade_length (nframes_t len)
        _fade_out.x_scale (factor);
        _fade_in.x_scale (factor);
        _in_update = false;
-       
-       _length = len;
 
-       StateChanged (LengthChanged);
+       _length = len;
 
+       PropertyChanged (PropertyChange (Properties::length));
+       
        return len;
 }
 
-nframes_t
+framecnt_t
 Crossfade::overlap_length () const
 {
        if (_fixed) {
@@ -921,7 +977,7 @@ Crossfade::overlap_length () const
 }
 
 void
-Crossfade::set_short_xfade_length (nframes_t n)
+Crossfade::set_short_xfade_length (framecnt_t n)
 {
        _short_xfade_length = n;
 }