Allow crossfades from the undo record to be recreated even if their regions are no...
[ardour.git] / libs / ardour / crossfade.cc
index dc84352f4ebbb0dc7647a0db9cfbfa38224da650..67ccea937182d4953fc6017454f554a9a1fc6cd0 100644 (file)
@@ -20,6 +20,7 @@
 
 #include "pbd/stacktrace.h"
 
+#include "ardour/debug.h"
 #include "ardour/types.h"
 #include "ardour/crossfade.h"
 #include "ardour/crossfade_compare.h"
@@ -28,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,8 +39,6 @@ using namespace ARDOUR;
 using namespace PBD;
 
 framecnt_t Crossfade::_short_xfade_length = 0;
-PropertyChange Crossfade::ActiveChanged = new_change();
-PropertyChange Crossfade::FollowOverlapChanged = new_change();
 
 /* XXX if and when we ever implement parallel processing of the process()
    callback, these will need to be handled on a per-thread basis.
@@ -47,6 +47,25 @@ PropertyChange 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 (framecnt_t sz)
 {
@@ -72,26 +91,26 @@ Crossfade::Crossfade (boost::shared_ptr<AudioRegion> in, boost::shared_ptr<Audio
                      framecnt_t length,
                      framepos_t position,
                      AnchorPoint ap)
-       : AudioRegion (in->session(), position, length, in->name() + string ("<>") + out->name()),
-         _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, a->name() + string ("<>") + b->name()),
-         _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;
@@ -106,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, "unnamed crossfade"),
-         _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 */
@@ -125,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();
        }
@@ -142,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();
        }
@@ -163,6 +198,7 @@ Crossfade::Crossfade (const Playlist& playlist, XMLNode& node)
 
 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), 0, true)
+       , CROSSFADE_DEFAULT_PROPERTIES
        , _fade_in (orig->_fade_in)
        , _fade_out (orig->_fade_out)
 {
@@ -200,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 ();
@@ -377,7 +422,7 @@ Crossfade::set_active (bool yn)
 {
        if (_active != yn) {
                _active = yn;
-               StateChanged (ActiveChanged);
+               PropertyChanged (PropertyChange (Properties::active));
        }
 }
 
@@ -442,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;
@@ -722,14 +771,14 @@ Crossfade::set_state (const XMLNode& node, int /*version*/)
        XMLNode* fo;
        const XMLProperty* prop;
        LocaleGuard lg (X_("POSIX"));
-       PropertyChange what_changed = PropertyChange (0);
+       PropertyChange what_changed;
        framepos_t val;
 
        if ((prop = node.property ("position")) != 0) {
                sscanf (prop->value().c_str(), "%" PRId64, &val);
                if (val != _position) {
                        _position = val;
-                       what_changed = PropertyChange (what_changed | PositionChanged);
+                       what_changed.add (Properties::position);
                }
        } else {
                warning << _("old-style crossfade information - no position information") << endmsg;
@@ -740,7 +789,7 @@ Crossfade::set_state (const XMLNode& node, int /*version*/)
                bool x = string_is_affirmative (prop->value());
                if (x != _active) {
                        _active = x;
-                       what_changed = PropertyChange (what_changed | ActiveChanged);
+                       what_changed.add (Properties::active);
                }
        } else {
                _active = true;
@@ -769,7 +818,7 @@ Crossfade::set_state (const XMLNode& node, int /*version*/)
                sscanf (prop->value().c_str(), "%" PRId64, &val);
                if (val != _length) {
                        _length = val;
-                       what_changed = PropertyChange (what_changed | LengthChanged);
+                       what_changed.add (Properties::length);
                }
 
        } else {
@@ -813,6 +862,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 */
@@ -838,9 +890,13 @@ Crossfade::set_state (const XMLNode& node, int /*version*/)
                }
        }
 
+        _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;
 }
@@ -866,7 +922,7 @@ Crossfade::set_follow_overlap (bool yn)
                set_xfade_length (_out->first_frame() + _out->length() - _in->first_frame());
        }
 
-       StateChanged (FollowOverlapChanged);
+       PropertyChanged (PropertyChange (Properties::follow_overlap));
 }
 
 framecnt_t
@@ -900,8 +956,8 @@ Crossfade::set_xfade_length (framecnt_t len)
 
        _length = len;
 
-       StateChanged (LengthChanged);
-
+       PropertyChanged (PropertyChange (Properties::length));
+       
        return len;
 }