Comment.
[ardour.git] / libs / ardour / crossfade.cc
index 40857bd56afa5c45fab13f6022904c4213ed5eb8..d2271030a5933c4c0c5c051c9a8106c885e3ac0f 100644 (file)
@@ -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>
@@ -124,7 +125,7 @@ Crossfade::Crossfade (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioR
        initialize ();
 }
 
-Crossfade::Crossfade (const Playlist& playlist, XMLNode& node)
+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
@@ -132,7 +133,7 @@ Crossfade::Crossfade (const Playlist& playlist, XMLNode& node)
 
 {
        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 */
@@ -144,8 +145,17 @@ Crossfade::Crossfade (const Playlist& playlist, XMLNode& node)
 
        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();
        }
@@ -161,8 +171,14 @@ 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();
        }
@@ -220,9 +236,18 @@ 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 ();
@@ -330,7 +355,7 @@ Crossfade::read_at (Sample *buf, Sample *mixdown_buffer,
 
                start = _position;
                buf += offset;
-               to_write = min (_length.val(), (nframes64_t) cnt);
+               to_write = min (_length.val(), cnt);
 
        } else {
 
@@ -692,6 +717,8 @@ Crossfade::get_state ()
        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));
@@ -749,6 +776,10 @@ Crossfade::set_state (const XMLNode& node, int /*version*/)
        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(), "%" PRId64, &val);
                if (val != _position) {
@@ -837,6 +868,9 @@ Crossfade::set_state (const XMLNode& node, int /*version*/)
                }
        }
 
+        _fade_in.front()->value = 0.0;
+        _fade_in.back()->value = 1.0;
+
        _fade_in.thaw ();
 
         /* fade out */
@@ -862,6 +896,9 @@ Crossfade::set_state (const XMLNode& node, int /*version*/)
                }
        }
 
+        _fade_out.front()->value = 1.0;
+        _fade_out.back()->value = 0.0;
+
        _fade_out.thaw ();
 
        PropertyChanged (what_changed); /* EMIT SIGNAL */