Fixes to get legacy 2.x sends working.
[ardour.git] / libs / ardour / crossfade.cc
index fd2fced83c473ffb1c445e716ea5bea4e39c2d5e..f3dfa28165c089339433c5f6d41d14545ff46dee 100644 (file)
     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 <pbd/stacktrace.h>
+
 #include <ardour/types.h>
 #include <ardour/crossfade.h>
 #include <ardour/crossfade_compare.h>
@@ -27,6 +28,7 @@
 #include <ardour/playlist.h>
 #include <ardour/utils.h>
 #include <ardour/session.h>
+#include <ardour/source.h>
 
 #include "i18n.h"
 #include <locale.h>
@@ -75,32 +77,27 @@ Crossfade::Crossfade (boost::shared_ptr<AudioRegion> in, boost::shared_ptr<Audio
                      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 (Parameter(FadeInAutomation), 0.0, 2.0, 1.0), // linear (gain coefficient) => -inf..+6dB
+         _fade_out (Parameter(FadeOutAutomation), 0.0, 2.0, 1.0) // linear (gain coefficient) => -inf..+6dB
+
 {
        _in = in;
        _out = out;
-       _length = length;
-       _position = position;
-       _anchor_point = ap;
 
-       switch (Config->get_xfade_model()) {
-       case ShortCrossfade:
-               _follow_overlap = false;
-               break;
-       default:
-               _follow_overlap = true;
-       }
+       _anchor_point = ap;
+       _follow_overlap = false;
 
        _active = 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 (Parameter(FadeInAutomation), 0.0, 2.0, 1.0), // linear (gain coefficient) => -inf..+6dB
+         _fade_out (Parameter(FadeOutAutomation), 0.0, 2.0, 1.0) // linear (gain coefficient) => -inf..+6dB
 {
        _in_update = false;
        _fixed = false;
@@ -113,11 +110,14 @@ Crossfade::Crossfade (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioR
 
        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 (Parameter(FadeInAutomation), 0.0, 2.0, 1.0), // linear (gain coefficient) => -inf..+6dB
+         _fade_out (Parameter(FadeOutAutomation), 0.0, 2.0, 1.0) // linear (gain coefficient) => -inf..+6dB
+
 {
        boost::shared_ptr<Region> r;
        XMLProperty* prop;
@@ -161,23 +161,23 @@ Crossfade::Crossfade (const Playlist& playlist, XMLNode& node)
 
        _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;
@@ -189,6 +189,7 @@ Crossfade::Crossfade (const Crossfade &orig, boost::shared_ptr<AudioRegion> newi
        _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);
@@ -197,14 +198,19 @@ Crossfade::Crossfade (const Crossfade &orig, boost::shared_ptr<AudioRegion> newi
 
 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 ();
@@ -230,12 +236,241 @@ Crossfade::initialize ()
        _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));
-
        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, nframes_t start, nframes_t cnt) const
+{
+#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);
+
+       delete [] mixdown;
+       delete [] gain;
+
+       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) const
+{
+       nframes_t offset;
+       nframes_t to_write;
+
+       if (!_active) {
+               return 0;
+       }
+
+       if (start < _position) {
+
+               /* handle an initial section of the read area that we do not
+                  cover.
+               */
+
+               offset = _position - start;
+
+               if (offset < cnt) {
+                       cnt -= offset;
+               } else {
+                       return 0;
+               }
+               
+               start = _position;
+               buf += offset;
+               to_write = min (_length, cnt);
+
+       } else {
+               
+               to_write = min (_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.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. 
+       */
+
+       for (nframes_t n = 0; n < to_write; ++n) {
+               buf[n] = (crossfade_buffer_out[n] * fov[n]) + (crossfade_buffer_in[n] * fiv[n]);
+       }
+
+       delete [] fov;
+       delete [] fiv;
+
+       return to_write;
 }      
 
+OverlapType 
+Crossfade::coverage (nframes_t start, nframes_t end) const
+{
+       nframes_t my_end = _position + _length;
+
+       if ((start >= _position) && (end <= my_end)) {
+               return OverlapInternal;
+       }
+       if ((end >= _position) && (end <= my_end)) {
+               return OverlapStart;
+       }
+       if ((start >= _position) && (start <= my_end)) {
+               return OverlapEnd;
+       }
+       if ((_position >= start) && (_position <= end) && (my_end <= end)) {
+               return OverlapExternal;
+       }
+       return OverlapNone;
+}
+
+void
+Crossfade::set_active (bool yn)
+{
+       if (_active != yn) {
+               _active = yn;
+               StateChanged (ActiveChanged);
+       }
+}
+
+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;
+       }
+
+       /* 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, 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 */
+       }
+
+       _in_update = false;
+
+       return true;
+}
+
+bool
+Crossfade::update ()
+{
+       nframes_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;
+       }
+
+       return true;
+}
+
 int
 Crossfade::compute (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioRegion> b, CrossfadeModel model)
 {
@@ -256,7 +491,7 @@ Crossfade::compute (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioReg
        /* 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()) {
@@ -357,15 +592,16 @@ Crossfade::compute (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioReg
 
                        _in = bottom;
                        _out = top;
-                       _position = bottom->first_frame();
-                       _anchor_point = StartOfIn;
+                       _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;
                                
@@ -401,207 +637,6 @@ Crossfade::compute (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioReg
        return 0;
 }
 
-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 offset;
-       nframes_t to_write;
-
-       if (!_active) {
-               return 0;
-       }
-
-       if (start < _position) {
-
-               /* handle an initial section of the read area that we do not
-                  cover.
-               */
-
-               offset = _position - start;
-
-               if (offset < cnt) {
-                       cnt -= offset;
-               } else {
-                       return 0;
-               }
-               
-               start = _position;
-               buf += offset;
-               to_write = min (_length, cnt);
-
-       } else {
-               
-               to_write = min (_length - (start - _position), cnt);
-               
-       }
-
-       offset = start - _position;
-
-       _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);
-
-       /* 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. 
-       */
-
-       for (nframes_t n = 0; n < to_write; ++n) {
-               buf[n] = (crossfade_buffer_out[n] * fov[n]) + (crossfade_buffer_in[n] * fiv[n]);
-       }
-
-       delete [] fov;
-       delete [] fiv;
-
-       return to_write;
-}      
-
-OverlapType 
-Crossfade::coverage (nframes_t start, nframes_t end) const
-{
-       nframes_t my_end = _position + _length;
-
-       if ((start >= _position) && (end <= my_end)) {
-               return OverlapInternal;
-       }
-       if ((end >= _position) && (end <= my_end)) {
-               return OverlapStart;
-       }
-       if ((start >= _position) && (start <= my_end)) {
-               return OverlapEnd;
-       }
-       if ((_position >= start) && (_position <= end) && (my_end <= end)) {
-               return OverlapExternal;
-       }
-       return OverlapNone;
-}
-
-void
-Crossfade::set_active (bool yn)
-{
-       if (_active != yn) {
-               _active = yn;
-               StateChanged (ActiveChanged);
-       }
-}
-
-bool
-Crossfade::refresh ()
-{
-       /* crossfades must be between non-muted regions */
-       
-       if (_out->muted() || _in->muted()) {
-               Invalidated (this);
-               return false;
-       }
-
-       /* overlap type must be Start, End or External */
-
-       OverlapType ot;
-       
-       ot = _in->coverage (_out->first_frame(), _out->last_frame());
-       
-       switch (ot) {
-       case OverlapNone:
-       case OverlapInternal:
-               Invalidated (this);
-               return false;
-               
-       default:
-               break;
-       }
-               
-       /* overlap type must not have altered */
-       
-       if (ot != overlap_type) {
-               Invalidated (this);
-               return false;
-       } 
-
-       /* time to update */
-
-       return update (false);
-}
-
-bool
-Crossfade::update (bool force)
-{
-       nframes_t newlen;
-
-       if (_follow_overlap) {
-               newlen = _out->first_frame() + _out->length() - _in->first_frame();
-       } else {
-               newlen = _length;
-       }
-
-       if (newlen == 0) {
-               Invalidated (this);
-               return false;
-       }
-
-       _in_update = true;
-
-       if (force || (_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:
-               if (_position != _in->first_frame()) {
-                       _position = _in->first_frame();
-               }
-               break;
-
-       case EndOfIn:
-               if (_position != _in->last_frame() - _length) {
-                       _position = _in->last_frame() - _length;
-               }
-               break;
-
-       case EndOfOut:
-               if (_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)
-{
-       Change what_we_care_about = Change (Region::MuteChanged|
-                                           Region::LayerChanged|
-                                           BoundsChanged);
-
-       if (what_changed & what_we_care_about) {
-               refresh ();
-       }
-}
-
 XMLNode&
 Crossfade::get_state () 
 {
@@ -865,5 +900,5 @@ Crossfade::set_short_xfade_length (nframes_t n)
 void
 Crossfade::invalidate ()
 {
-       Invalidated (this); /* EMIT SIGNAL */
+       Invalidated (shared_from_this ()); /* EMIT SIGNAL */
 }