Add metadata file with DOAP and LV2 Host Info information.
[ardour.git] / libs / ardour / playlist.cc
index 8f4c94078d7a260349c06727a5d990a198cfc0d3..f57093ea40fa2fd542059310eb2d7f8b288d39a1 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2000-2003 Paul Davis 
+    Copyright (C) 2000-2003 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/failed_constructor.h>
-#include <pbd/stl_delete.h>
-#include <pbd/xml++.h>
-#include <pbd/stacktrace.h>
+#include "pbd/failed_constructor.h"
+#include "pbd/stl_delete.h"
+#include "pbd/xml++.h"
+#include "pbd/stacktrace.h"
 
-#include <ardour/playlist.h>
-#include <ardour/session.h>
-#include <ardour/region.h>
-#include <ardour/region_factory.h>
-#include <ardour/playlist_factory.h>
-#include <ardour/transient_detector.h>
+#include "ardour/playlist.h"
+#include "ardour/session.h"
+#include "ardour/region.h"
+#include "ardour/region_factory.h"
+#include "ardour/playlist_factory.h"
+#include "ardour/transient_detector.h"
 
 #include "i18n.h"
 
@@ -47,8 +47,8 @@ using namespace PBD;
 
 struct ShowMeTheList {
     ShowMeTheList (boost::shared_ptr<Playlist> pl, const string& n) : playlist (pl), name (n) {}
-    ~ShowMeTheList () { 
-           cerr << ">>>>" << name << endl; playlist->dump(); cerr << "<<<<" << name << endl << endl; 
+    ~ShowMeTheList () {
+           cerr << ">>>>" << name << endl; playlist->dump(); cerr << "<<<<" << name << endl << endl;
     };
     boost::shared_ptr<Playlist> playlist;
     string name;
@@ -60,6 +60,23 @@ struct RegionSortByLayer {
     }
 };
 
+struct RegionSortByLayerWithPending {
+       bool operator () (boost::shared_ptr<Region> a, boost::shared_ptr<Region> b) {
+
+               double p = a->layer ();
+               if (a->pending_explicit_relayer()) {
+                       p += 0.5;
+               }
+
+               double q = b->layer ();
+               if (b->pending_explicit_relayer()) {
+                       q += 0.5;
+               }
+
+               return p < q;
+       }
+};
+
 struct RegionSortByPosition {
     bool operator() (boost::shared_ptr<Region> a, boost::shared_ptr<Region> b) {
            return a->position() < b->position();
@@ -80,7 +97,7 @@ Playlist::Playlist (Session& sess, string nom, DataType type, bool hide)
        init (hide);
        first_set_state = false;
        _name = nom;
-       
+
 }
 
 Playlist::Playlist (Session& sess, const XMLNode& node, DataType type, bool hide)
@@ -103,7 +120,7 @@ Playlist::Playlist (boost::shared_ptr<const Playlist> other, string namestr, boo
 
        RegionList tmp;
        other->copy_regions (tmp);
-       
+
        in_set_state++;
 
        for (list<boost::shared_ptr<Region> >::iterator x = tmp.begin(); x != tmp.end(); ++x) {
@@ -123,7 +140,7 @@ Playlist::Playlist (boost::shared_ptr<const Playlist> other, string namestr, boo
        subcnt = 0;
        _read_data_count = 0;
        _frozen = other->_frozen;
-       
+
        layer_op_counter = other->layer_op_counter;
        freeze_length = other->freeze_length;
 }
@@ -188,7 +205,7 @@ Playlist::Playlist (boost::shared_ptr<const Playlist> other, nframes_t start, nf
 
                add_region_internal (new_region, position);
        }
-       
+
        in_set_state--;
        first_set_state = false;
 
@@ -206,7 +223,7 @@ void
 Playlist::release ()
 {
        if (_refcnt > 0) {
-               _refcnt--; 
+               _refcnt--;
        }
 
        if (_refcnt == 0) {
@@ -246,24 +263,11 @@ Playlist::init (bool hide)
        _frozen = false;
        layer_op_counter = 0;
        freeze_length = 0;
+       _explicit_relayering = false;
 
        Modified.connect (mem_fun (*this, &Playlist::mark_session_dirty));
 }
 
-Playlist::Playlist (const Playlist& pl)
-       : SessionObject(pl._session, pl._name)
-       , _type(pl.data_type())
-{
-       fatal << _("playlist const copy constructor called") << endmsg;
-}
-
-Playlist::Playlist (Playlist& pl)
-       : SessionObject(pl._session, pl._name)
-       , _type(pl.data_type())
-{
-       fatal << _("playlist non-const copy constructor called") << endmsg;
-}
-
 Playlist::~Playlist ()
 {
        {
@@ -295,7 +299,7 @@ Playlist::set_name (const string& str)
 
 /***********************************************************************
  CHANGE NOTIFICATION HANDLING
+
  Notifications must be delayed till the region_lock is released. This
  is necessary because handlers for the signals may need to acquire
  the lock (e.g. to read from the playlist).
@@ -326,9 +330,9 @@ Playlist::delay_notifications ()
 void
 Playlist::release_notifications ()
 {
-       if (g_atomic_int_dec_and_test (&block_notifications)) { 
+       if (g_atomic_int_dec_and_test (&block_notifications)) {
                flush_notifications ();
-       } 
+       }
 }
 
 void
@@ -356,6 +360,7 @@ Playlist::notify_region_removed (boost::shared_ptr<Region> r)
                pending_length = false;
                LengthChanged (); /* EMIT SIGNAL */
                pending_modified = false;
+               RegionRemoved (boost::weak_ptr<Region> (r)); /* EMIT SIGNAL */
                Modified (); /* EMIT SIGNAL */
        }
 }
@@ -364,17 +369,16 @@ void
 Playlist::notify_region_moved (boost::shared_ptr<Region> r)
 {
        Evoral::RangeMove<nframes_t> const move (r->last_position (), r->length (), r->position ());
-                       
+
        if (holding_state ()) {
 
                pending_range_moves.push_back (move);
-               
+
        } else {
 
                list< Evoral::RangeMove<nframes_t> > m;
                m.push_back (move);
                RangesMoved (m);
-
        }
 
 }
@@ -394,6 +398,7 @@ Playlist::notify_region_added (boost::shared_ptr<Region> r)
                pending_length = false;
                LengthChanged (); /* EMIT SIGNAL */
                pending_modified = false;
+               RegionAdded (boost::weak_ptr<Region> (r)); /* EMIT SIGNAL */
                Modified (); /* EMIT SIGNAL */
        }
 }
@@ -426,7 +431,7 @@ Playlist::flush_notifications ()
 
        /* we have no idea what order the regions ended up in pending
           bounds (it could be based on selection order, for example).
-          so, to preserve layering in the "most recently moved is higher" 
+          so, to preserve layering in the "most recently moved is higher"
           model, sort them by existing layer, then timestamp them.
        */
 
@@ -434,21 +439,25 @@ Playlist::flush_notifications ()
        // pending_bounds.sort (cmp);
 
        for (RegionList::iterator r = pending_bounds.begin(); r != pending_bounds.end(); ++r) {
-               if (Config->get_layer_model() == MoveAddHigher) {
+               if (_session.config.get_layer_model() == MoveAddHigher) {
                        timestamp_layer_op (*r);
                }
+
                pending_length = true;
                dependent_checks_needed.insert (*r);
-               n++;
-       }
 
-       for (s = pending_adds.begin(); s != pending_adds.end(); ++s) {
-               dependent_checks_needed.insert (*s);
                n++;
        }
 
        for (s = pending_removes.begin(); s != pending_removes.end(); ++s) {
                remove_dependents (*s);
+               RegionRemoved (boost::weak_ptr<Region> (*s)); /* EMIT SIGNAL */
+               n++;
+       }
+
+       for (s = pending_adds.begin(); s != pending_adds.end(); ++s) {
+               RegionAdded (boost::weak_ptr<Region> (*s)); /* EMIT SIGNAL */
+               dependent_checks_needed.insert (*s);
                n++;
        }
 
@@ -464,7 +473,6 @@ Playlist::flush_notifications ()
                }
                pending_modified = false;
                Modified (); /* EMIT SIGNAL */
-               
        }
 
        for (s = dependent_checks_needed.begin(); s != dependent_checks_needed.end(); ++s) {
@@ -488,22 +496,26 @@ Playlist::flush_notifications ()
  *************************************************************/
 
 void
-Playlist::add_region (boost::shared_ptr<Region> region, nframes_t position, float times
+Playlist::add_region (boost::shared_ptr<Region> region, nframes_t position, float times, bool auto_partition)
 {
        RegionLock rlock (this);
        times = fabs (times);
-       
+
        int itimes = (int) floor (times);
 
        nframes_t pos = position;
-       
+
+       if (times == 1 && auto_partition){
+               partition(pos, (nframes_t) (pos + region->length()), true);
+       }
+
        if (itimes >= 1) {
                add_region_internal (region, pos);
                pos += region->length();
                --itimes;
        }
-       
-       
+
+
        /* note that itimes can be zero if we being asked to just
           insert a single fraction of the region.
        */
@@ -513,7 +525,7 @@ Playlist::add_region (boost::shared_ptr<Region> region, nframes_t position, floa
                add_region_internal (copy, pos);
                pos += region->length();
        }
-       
+
        nframes_t length = 0;
 
        if (floor (times) != times) {
@@ -524,7 +536,6 @@ Playlist::add_region (boost::shared_ptr<Region> region, nframes_t position, floa
                add_region_internal (sub, pos);
        }
 
-
        possibly_splice_unlocked (position, (pos + length) - position, boost::shared_ptr<Region>());
 }
 
@@ -543,10 +554,12 @@ Playlist::set_region_ownership ()
 bool
 Playlist::add_region_internal (boost::shared_ptr<Region> region, nframes_t position)
 {
-       if (region->data_type() != _type)
+       if (region->data_type() != _type){
                return false;
+       }
 
        RegionSortByPosition cmp;
+
        nframes_t old_length = 0;
 
        if (!holding_state()) {
@@ -556,7 +569,7 @@ Playlist::add_region_internal (boost::shared_ptr<Region> region, nframes_t posit
        if (!first_set_state) {
                boost::shared_ptr<Playlist> foo (shared_from_this());
                region->set_playlist (boost::weak_ptr<Playlist>(foo));
-       } 
+       }
 
        region->set_position (position, this);
 
@@ -575,16 +588,18 @@ Playlist::add_region_internal (boost::shared_ptr<Region> region, nframes_t posit
        /* we need to notify the existence of new region before checking dependents. Ick. */
 
        notify_region_added (region);
-       
+
        if (!holding_state ()) {
+
                check_dependents (region, false);
+
                if (old_length != _get_maximum_extent()) {
                        notify_length_changed ();
                }
        }
 
        region_state_changed_connections.push_back (
-               region->StateChanged.connect (sigc::bind (mem_fun (this, &Playlist::region_changed_proxy), 
+               region->StateChanged.connect (sigc::bind (mem_fun (this, &Playlist::region_changed_proxy),
                                                          boost::weak_ptr<Region> (region)))
                );
 
@@ -642,7 +657,7 @@ Playlist::remove_region_internal (boost::shared_ptr<Region> region)
                        if (!holding_state ()) {
                                relayer ();
                                remove_dependents (region);
-                               
+
                                if (old_length != _get_maximum_extent()) {
                                        notify_length_changed ();
                                }
@@ -688,11 +703,11 @@ Playlist::get_region_list_equivalent_regions (boost::shared_ptr<Region> other, v
 }
 
 void
-Playlist::partition (nframes_t start, nframes_t end, bool just_top_level)
+Playlist::partition (nframes_t start, nframes_t end, bool cut)
 {
        RegionList thawlist;
 
-       partition_internal (start, end, false, thawlist);
+       partition_internal (start, end, cut, thawlist);
 
        for (RegionList::iterator i = thawlist.begin(); i != thawlist.end(); ++i) {
                (*i)->thaw ("separation");
@@ -706,35 +721,38 @@ Playlist::partition_internal (nframes_t start, nframes_t end, bool cutting, Regi
 
        {
                RegionLock rlock (this);
+
                boost::shared_ptr<Region> region;
                boost::shared_ptr<Region> current;
                string new_name;
                RegionList::iterator tmp;
                OverlapType overlap;
                nframes_t pos1, pos2, pos3, pos4;
-               
+
                in_partition = true;
-               
+
                /* need to work from a copy, because otherwise the regions we add during the process
                   get operated on as well.
                */
-               
+
                RegionList copy = regions;
-               
+
                for (RegionList::iterator i = copy.begin(); i != copy.end(); i = tmp) {
-                       
+
                        tmp = i;
                        ++tmp;
-                       
+
                        current = *i;
 
                        if (current->first_frame() >= start && current->last_frame() < end) {
+
                                if (cutting) {
                                        remove_region_internal (current);
                                }
+
                                continue;
                        }
-                       
+
                        /* coverage will return OverlapStart if the start coincides
                           with the end point. we do not partition such a region,
                           so catch this special case.
@@ -752,13 +770,12 @@ Playlist::partition_internal (nframes_t start, nframes_t end, bool cutting, Regi
                        pos2 = start;
                        pos3 = end;
                        pos4 = current->last_frame();
-                       
+
                        if (overlap == OverlapInternal) {
-                       
                                /* split: we need 3 new regions, the front, middle and end.
                                   cut:   we need 2 regions, the front and end.
                                */
-                               
+
                                /*
                                         start                 end
                          ---------------*************************------------
@@ -767,123 +784,123 @@ Playlist::partition_internal (nframes_t start, nframes_t end, bool cutting, Regi
                          ---------------*****++++++++++++++++====------------
                          CUT
                          ---------------*****----------------====------------
-                         
+
                                */
 
                                if (!cutting) {
-                               
                                        /* "middle" ++++++ */
-                                       
+
                                        _session.region_name (new_name, current->name(), false);
                                        region = RegionFactory::create (current, pos2 - pos1, pos3 - pos2, new_name,
                                                                        regions.size(), Region::Flag(current->flags()|Region::Automatic|Region::LeftOfSplit|Region::RightOfSplit));
                                        add_region_internal (region, start);
                                        new_regions.push_back (region);
                                }
-                               
+
                                /* "end" ====== */
-                       
+
                                _session.region_name (new_name, current->name(), false);
-                               region = RegionFactory::create (current, pos3 - pos1, pos4 - pos3, new_name, 
+                               region = RegionFactory::create (current, pos3 - pos1, pos4 - pos3, new_name,
                                                                regions.size(), Region::Flag(current->flags()|Region::Automatic|Region::RightOfSplit));
-                               
+
                                add_region_internal (region, end);
                                new_regions.push_back (region);
-                               
+
                                /* "front" ***** */
-                               
+
                                current->freeze ();
                                thawlist.push_back (current);
                                current->trim_end (pos2, this);
-                               
+
                        } else if (overlap == OverlapEnd) {
-                               
+
                                /*
                                                              start           end
                                    ---------------*************************------------
                                                   P1           P2         P4   P3
-                                    SPLIT:                                                
+                                    SPLIT:
                                    ---------------**************+++++++++++------------
-                                    CUT:                                                  
+                                    CUT:
                                    ---------------**************-----------------------
                                */
-                               
+
                                if (!cutting) {
-                                       
+
                                        /* end +++++ */
-                                       
+
                                        _session.region_name (new_name, current->name(), false);
                                        region = RegionFactory::create (current, pos2 - pos1, pos4 - pos2, new_name, (layer_t) regions.size(),
                                                                        Region::Flag(current->flags()|Region::Automatic|Region::LeftOfSplit));
+
                                        add_region_internal (region, start);
                                        new_regions.push_back (region);
                                }
-                               
+
                                /* front ****** */
-                               
+
                                current->freeze ();
                                thawlist.push_back (current);
                                current->trim_end (pos2, this);
-                               
+
                        } else if (overlap == OverlapStart) {
-                               
+
                                /* split: we need 2 regions: the front and the end.
                                   cut: just trim current to skip the cut area
                                */
-                               
+
                                /*
                                                        start           end
                                    ---------------*************************------------
-                                      P2          P1 P3                   P4          
+                                      P2          P1 P3                   P4
 
                                    SPLIT:
                                    ---------------****+++++++++++++++++++++------------
                                    CUT:
                                    -------------------*********************------------
-                                   
+
                                */
 
                                if (!cutting) {
-                               
                                        /* front **** */
                                        _session.region_name (new_name, current->name(), false);
                                        region = RegionFactory::create (current, 0, pos3 - pos1, new_name,
                                                                        regions.size(), Region::Flag(current->flags()|Region::Automatic|Region::RightOfSplit));
+
                                        add_region_internal (region, pos1);
                                        new_regions.push_back (region);
-                               } 
-                               
+                               }
+
                                /* end */
-                               
+
                                current->freeze ();
                                thawlist.push_back (current);
                                current->trim_front (pos3, this);
-                               
                        } else if (overlap == OverlapExternal) {
-                               
+
                                /* split: no split required.
                                   cut: remove the region.
                                */
-                               
+
                                /*
                                       start                                      end
                                    ---------------*************************------------
-                                      P2          P1 P3                   P4          
+                                      P2          P1 P3                   P4
 
                                    SPLIT:
                                    ---------------*************************------------
                                    CUT:
                                    ----------------------------------------------------
-                                   
+
                                */
-                               
+
                                if (cutting) {
                                        remove_region_internal (current);
                                }
+
                                new_regions.push_back (current);
                        }
                }
-               
+
                in_partition = false;
        }
 
@@ -908,11 +925,11 @@ Playlist::cut_copy (boost::shared_ptr<Playlist> (Playlist::*pmf)(nframes_t, nfra
        for (list<AudioRange>::iterator i = ranges.begin(); i != ranges.end(); ++i) {
 
                pl = (this->*pmf)((*i).start, (*i).length(), result_is_hidden);
-               
+
                if (i == ranges.begin()) {
                        ret = pl;
                } else {
-                       
+
                        /* paste the next section into the nascent playlist,
                           offset to reflect the start of the first range we
                           chopped.
@@ -968,7 +985,7 @@ boost::shared_ptr<Playlist>
 Playlist::copy (nframes_t start, nframes_t cnt, bool result_is_hidden)
 {
        char buf[32];
-       
+
        snprintf (buf, sizeof (buf), "%" PRIu32, ++subcnt);
        string new_name = _name;
        new_name += '.';
@@ -989,7 +1006,7 @@ Playlist::paste (boost::shared_ptr<Playlist> other, nframes_t position, float ti
                RegionLock rl2 (other.get());
 
                old_length = _get_maximum_extent();
-       
+
                int itimes = (int) floor (times);
                nframes_t pos = position;
                nframes_t shift = other->_get_maximum_extent();
@@ -1002,7 +1019,7 @@ Playlist::paste (boost::shared_ptr<Playlist> other, nframes_t position, float ti
                                /* put these new regions on top of all existing ones, but preserve
                                   the ordering they had in the original playlist.
                                */
-                               
+
                                copy_of_region->set_layer (copy_of_region->layer() + top_layer);
                                add_region_internal (copy_of_region, copy_of_region->position() + pos);
                        }
@@ -1016,7 +1033,7 @@ Playlist::paste (boost::shared_ptr<Playlist> other, nframes_t position, float ti
                        notify_length_changed ();
                }
 
-               
+
        }
 
        return 0;
@@ -1060,14 +1077,14 @@ Playlist::shift (nframes64_t at, nframes64_t distance, bool move_intersected, bo
                        /* too early */
                        continue;
                }
-               
+
                if (at > (*r)->first_frame() && at < (*r)->last_frame()) {
                        /* intersected region */
                        if (!move_intersected) {
                                continue;
                        }
                }
-               
+
                /* do not move regions glued to music time - that
                   has to be done separately.
                */
@@ -1126,13 +1143,13 @@ Playlist::_split_region (boost::shared_ptr<Region> region, nframes_t playlist_po
        string after_name;
 
        /* split doesn't change anything about length, so don't try to splice */
-       
+
        bool old_sp = _splicing;
        _splicing = true;
 
        before = playlist_position - region->position();
        after = region->length() - before;
-       
+
        _session.region_name (before_name, region->name(), false);
        left = RegionFactory::create (region, 0, before, before_name, region->layer(), Region::Flag (region->flags()|Region::LeftOfSplit));
 
@@ -1148,14 +1165,14 @@ Playlist::_split_region (boost::shared_ptr<Region> region, nframes_t playlist_po
                        (*i)->set_last_layer_op( (*i)->last_layer_op() + 1 );
                }
        }
-       
+
        left->set_last_layer_op ( orig_layer_op );
        right->set_last_layer_op ( orig_layer_op + 1);
 
        layer_op_counter++;
 
        finalize_split_region (region, left, right);
-       
+
        remove_region_internal (region);
 
        _splicing = old_sp;
@@ -1219,8 +1236,8 @@ Playlist::core_splice (nframes_t at, nframes64_t distance, boost::shared_ptr<Reg
                                new_pos = 0;
                        } else if (new_pos >= max_frames - (*i)->length()) {
                                new_pos = max_frames - (*i)->length();
-                       } 
-                               
+                       }
+
                        (*i)->set_position (new_pos, this);
                }
        }
@@ -1242,11 +1259,11 @@ Playlist::region_bounds_changed (Change what_changed, boost::shared_ptr<Region>
                /* remove it from the list then add it back in
                   the right place again.
                */
-               
+
                RegionSortByPosition cmp;
 
                RegionList::iterator i = find (regions.begin(), regions.end(), region);
-               
+
                if (i == regions.end()) {
                        warning << string_compose (_("%1: bounds changed received for region (%2)not in playlist"),
                                            _name, region->name())
@@ -1259,16 +1276,16 @@ Playlist::region_bounds_changed (Change what_changed, boost::shared_ptr<Region>
        }
 
        if (what_changed & Change (ARDOUR::PositionChanged|ARDOUR::LengthChanged)) {
-               
+
                nframes64_t delta = 0;
-               
+
                if (what_changed & ARDOUR::PositionChanged) {
                        delta = (nframes64_t) region->position() - (nframes64_t) region->last_position();
-               } 
-               
+               }
+
                if (what_changed & ARDOUR::LengthChanged) {
                        delta += (nframes64_t) region->length() - (nframes64_t) region->last_length();
-               } 
+               }
 
                if (delta) {
                        possibly_splice (region->last_position() + region->last_length(), delta, region);
@@ -1277,11 +1294,11 @@ Playlist::region_bounds_changed (Change what_changed, boost::shared_ptr<Region>
                if (holding_state ()) {
                        pending_bounds.push_back (region);
                } else {
-                       if (Config->get_layer_model() == MoveAddHigher) {
+                       if (_session.config.get_layer_model() == MoveAddHigher) {
                                /* it moved or changed length, so change the timestamp */
                                timestamp_layer_op (region);
                        }
-                       
+
                        notify_length_changed ();
                        relayer ();
                        check_dependents (region, false);
@@ -1318,8 +1335,8 @@ Playlist::region_changed (Change what_changed, boost::shared_ptr<Region> region)
                region_bounds_changed (what_changed, region);
                save = !(_splicing || _nudging);
        }
-               
-       if ((what_changed & our_interests) && 
+
+       if ((what_changed & our_interests) &&
            !(what_changed & Change (ARDOUR::PositionChanged|ARDOUR::LengthChanged))) {
                check_dependents (region, false);
        }
@@ -1327,7 +1344,7 @@ Playlist::region_changed (Change what_changed, boost::shared_ptr<Region> region)
        if (what_changed & Change (ARDOUR::PositionChanged)) {
                notify_region_moved (region);
        }
-               
+
        if (what_changed & our_interests) {
                save = true;
        }
@@ -1353,15 +1370,14 @@ Playlist::clear (bool with_signals)
                        std::list<sigc::connection>::iterator i = region_state_changed_connections.begin ();
                        i != region_state_changed_connections.end ();
                        ++i
-                       ) {
-
+               ) {
                        i->disconnect ();
-                       
                }
-                    
+
                for (RegionList::iterator i = regions.begin(); i != regions.end(); ++i) {
                        pending_removes.insert (*i);
                }
+
                regions.clear ();
        }
 
@@ -1384,7 +1400,7 @@ Playlist::regions_at (nframes_t frame)
 {
        RegionLock rlock (this);
        return find_regions_at (frame);
-}      
+}
 
 boost::shared_ptr<Region>
 Playlist::top_region_at (nframes_t frame)
@@ -1393,16 +1409,47 @@ Playlist::top_region_at (nframes_t frame)
        RegionLock rlock (this);
        RegionList *rlist = find_regions_at (frame);
        boost::shared_ptr<Region> region;
-       
+
        if (rlist->size()) {
                RegionSortByLayer cmp;
                rlist->sort (cmp);
                region = rlist->back();
-       } 
+       }
 
        delete rlist;
        return region;
-}      
+}
+
+boost::shared_ptr<Region>
+Playlist::top_unmuted_region_at (nframes_t frame)
+
+{
+       RegionLock rlock (this);
+       RegionList *rlist = find_regions_at (frame);
+
+       for (RegionList::iterator i = rlist->begin(); i != rlist->end(); ) {
+
+               RegionList::iterator tmp = i;
+               ++tmp;
+
+               if ((*i)->muted()) {
+                       rlist->erase (i);
+               }
+
+               i = tmp;
+       }
+
+       boost::shared_ptr<Region> region;
+
+       if (rlist->size()) {
+               RegionSortByLayer cmp;
+               rlist->sort (cmp);
+               region = rlist->back();
+       }
+
+       delete rlist;
+       return region;
+}
 
 Playlist::RegionList*
 Playlist::regions_to_read (nframes_t start, nframes_t end)
@@ -1460,45 +1507,45 @@ Playlist::regions_to_read (nframes_t start, nframes_t end)
        if (covering.size() == 1) {
 
                rlist->push_back (covering.front());
-               
+
        } else {
-       
+
                for (set<nframes_t>::iterator t = to_check.begin(); t != to_check.end(); ++t) {
-                       
+
                        here.clear ();
-                       
+
                        for (RegionList::iterator x = covering.begin(); x != covering.end(); ++x) {
-                       
+
                                if ((*x)->covers (*t)) {
                                        here.push_back (*x);
                                }
                        }
-                       
+
                        RegionSortByLayer cmp;
                        here.sort (cmp);
-                       
+
                        /* ... and get the top/transparent regions at "here" */
-                       
+
                        for (RegionList::reverse_iterator c = here.rbegin(); c != here.rend(); ++c) {
-                               
+
                                unique.insert (*c);
-                               
+
                                if ((*c)->opaque()) {
-                                       
+
                                        /* the other regions at this position are hidden by this one */
-                                       
+
                                        break;
                                }
                        }
                }
-               
+
                for (set<boost::shared_ptr<Region> >::iterator s = unique.begin(); s != unique.end(); ++s) {
                        rlist->push_back (*s);
                }
 
                if (rlist->size() > 1) {
                        /* now sort by time order */
-                       
+
                        RegionSortByPosition cmp;
                        rlist->sort (cmp);
                }
@@ -1559,26 +1606,26 @@ Playlist::find_next_transient (nframes64_t from, int dir)
                (*i)->get_transients (these_points);
 
                /* add first frame, just, err, because */
-               
+
                these_points.push_back ((*i)->first_frame());
-               
+
                points.insert (points.end(), these_points.begin(), these_points.end());
                these_points.clear ();
        }
-       
+
        if (points.empty()) {
                return -1;
        }
 
        TransientDetector::cleanup_transients (points, _session.frame_rate(), 3.0);
        bool reached = false;
-       
+
        if (dir > 0) {
                for (AnalysisFeatureList::iterator x = points.begin(); x != points.end(); ++x) {
                        if ((*x) >= from) {
                                reached = true;
                        }
-                       
+
                        if (reached && (*x) > from) {
                                return *x;
                        }
@@ -1588,7 +1635,7 @@ Playlist::find_next_transient (nframes64_t from, int dir)
                        if ((*x) <= from) {
                                reached = true;
                        }
-                       
+
                        if (reached && (*x) < from) {
                                return *x;
                        }
@@ -1605,9 +1652,12 @@ Playlist::find_next_region (nframes_t frame, RegionPoint point, int dir)
        boost::shared_ptr<Region> ret;
        nframes_t closest = max_frames;
 
+       bool end_iter = false;
 
        for (RegionList::iterator i = regions.begin(); i != regions.end(); ++i) {
 
+               if(end_iter) break;
+
                nframes_t distance;
                boost::shared_ptr<Region> r = (*i);
                nframes_t pos = 0;
@@ -1620,17 +1670,19 @@ Playlist::find_next_region (nframes_t frame, RegionPoint point, int dir)
                        pos = r->last_frame ();
                        break;
                case SyncPoint:
-                       pos = r->adjust_to_sync (r->first_frame());
+                       pos = r->sync_position ();
+                       // r->adjust_to_sync (r->first_frame());
                        break;
                }
 
                switch (dir) {
                case 1: /* forwards */
 
-                       if (pos >= frame) {
+                       if (pos > frame) {
                                if ((distance = pos - frame) < closest) {
                                        closest = distance;
                                        ret = r;
+                                       end_iter = true;
                                }
                        }
 
@@ -1638,12 +1690,16 @@ Playlist::find_next_region (nframes_t frame, RegionPoint point, int dir)
 
                default: /* backwards */
 
-                       if (pos <= frame) {
+                       if (pos < frame) {
                                if ((distance = frame - pos) < closest) {
                                        closest = distance;
                                        ret = r;
                                }
                        }
+                       else {
+                               end_iter = true;
+                       }
+
                        break;
                }
        }
@@ -1665,11 +1721,11 @@ Playlist::find_next_region_boundary (nframes64_t frame, int dir)
 
                        boost::shared_ptr<Region> r = (*i);
                        nframes64_t distance;
-                       
+
                        if (r->first_frame() > frame) {
 
                                distance = r->first_frame() - frame;
-                               
+
                                if (distance < closest) {
                                        ret = r->first_frame();
                                        closest = distance;
@@ -1677,9 +1733,9 @@ Playlist::find_next_region_boundary (nframes64_t frame, int dir)
                        }
 
                        if (r->last_frame () > frame) {
-                               
+
                                distance = r->last_frame () - frame;
-                               
+
                                if (distance < closest) {
                                        ret = r->last_frame ();
                                        closest = distance;
@@ -1690,14 +1746,14 @@ Playlist::find_next_region_boundary (nframes64_t frame, int dir)
        } else {
 
                for (RegionList::reverse_iterator i = regions.rbegin(); i != regions.rend(); ++i) {
-                       
+
                        boost::shared_ptr<Region> r = (*i);
                        nframes64_t distance;
 
                        if (r->last_frame() < frame) {
 
                                distance = frame - r->last_frame();
-                               
+
                                if (distance < closest) {
                                        ret = r->last_frame();
                                        closest = distance;
@@ -1705,7 +1761,7 @@ Playlist::find_next_region_boundary (nframes64_t frame, int dir)
                        }
 
                        if (r->first_frame() < frame) {
-                               
+
                                distance = frame - r->first_frame();
 
                                if (distance < closest) {
@@ -1723,6 +1779,7 @@ Playlist::find_next_region_boundary (nframes64_t frame, int dir)
 
 
 
+
 void
 Playlist::mark_session_dirty ()
 {
@@ -1732,7 +1789,7 @@ Playlist::mark_session_dirty ()
 }
 
 int
-Playlist::set_state (const XMLNode& node)
+Playlist::set_state (const XMLNode& node, int version)
 {
        XMLNode *child;
        XMLNodeList nlist;
@@ -1757,38 +1814,38 @@ Playlist::set_state (const XMLNode& node)
        for (piter = plist.begin(); piter != plist.end(); ++piter) {
 
                prop = *piter;
-               
+
                if (prop->name() == X_("name")) {
                        _name = prop->value();
                } else if (prop->name() == X_("orig_diskstream_id")) {
                        _orig_diskstream_id = prop->value ();
                } else if (prop->name() == X_("frozen")) {
-                       _frozen = (prop->value() == X_("yes"));
+                       _frozen = string_is_affirmative (prop->value());
                }
        }
 
        clear (false);
-       
+
        nlist = node.children();
 
        for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
 
                child = *niter;
-               
+
                if (child->name() == "Region") {
 
                        if ((prop = child->property ("id")) == 0) {
                                error << _("region state node has no ID, ignored") << endmsg;
                                continue;
                        }
-                       
+
                        ID id = prop->value ();
-                       
+
                        if ((region = region_by_id (id))) {
 
                                Change what_changed = Change (0);
 
-                               if (region->set_live_state (*child, what_changed, true)) {
+                               if (region->set_live_state (*child, version, what_changed, true)) {
                                        error << _("Playlist: cannot reset region state from XML") << endmsg;
                                        continue;
                                }
@@ -1803,15 +1860,15 @@ Playlist::set_state (const XMLNode& node)
                        // So that layer_op ordering doesn't get screwed up
                        region->set_last_layer_op( region->layer());
 
-               }                       
+               }
        }
-       
+
        notify_modified ();
 
        thaw ();
 
-       /* update dependents, which was not done during add_region_internal 
-          due to in_set_state being true 
+       /* update dependents, which was not done during add_region_internal
+          due to in_set_state being true
        */
 
        for (RegionList::iterator r = regions.begin(); r != regions.end(); ++r) {
@@ -1826,21 +1883,23 @@ Playlist::set_state (const XMLNode& node)
 XMLNode&
 Playlist::get_state()
 {
-       return state(true);
+       return state (true);
 }
 
 XMLNode&
 Playlist::get_template()
 {
-       return state(false);
+       return state (false);
 }
 
+/** @param full_state true to include regions in the returned state, otherwise false.
+ */
 XMLNode&
 Playlist::state (bool full_state)
 {
        XMLNode *node = new XMLNode (X_("Playlist"));
        char buf[64];
-       
+
        node->add_property (X_("name"), _name);
        node->add_property (X_("type"), _type.to_string());
 
@@ -1899,7 +1958,7 @@ Playlist::_get_maximum_extent () const
        return max_extent;
 }
 
-string 
+string
 Playlist::bump_name (string name, Session &session)
 {
        string newname = name;
@@ -1915,7 +1974,7 @@ Playlist::bump_name (string name, Session &session)
 layer_t
 Playlist::top_layer() const
 {
-       RegionLock rlock (const_cast<Playlist *> (this));       
+       RegionLock rlock (const_cast<Playlist *> (this));
        layer_t top = 0;
 
        for (RegionList::const_iterator i = regions.begin(); i != regions.end(); ++i) {
@@ -1940,12 +1999,30 @@ Playlist::relayer ()
        /* don't send multiple Modified notifications
           when multiple regions are relayered.
        */
+
        freeze ();
 
-       /* build up a new list of regions on each layer */
+       /* Build up a new list of regions on each layer, stored in a set of lists
+          each of which represent some period of time on some layer.  The idea
+          is to avoid having to search the entire region list to establish whether
+          each region overlaps another */
+
+       /* how many pieces to divide this playlist's time up into */
+       int const divisions = 512;
+
+       /* find the start and end positions of the regions on this playlist */
+       nframes_t start = UINT_MAX;
+       nframes_t end = 0;
+       for (RegionList::const_iterator i = regions.begin(); i != regions.end(); ++i) {
+               start = min (start, (*i)->position());
+               end = max (end, (*i)->position() + (*i)->length());
+       }
+
+       /* hence the size of each time division */
+       double const division_size = (end - start) / double (divisions);
 
-       std::vector<RegionList> layers;
+       vector<vector<RegionList> > layers;
+       layers.push_back (vector<RegionList> (divisions));
 
        /* we want to go through regions from desired lowest to desired highest layer,
           which depends on the layer model
@@ -1953,50 +2030,74 @@ Playlist::relayer ()
 
        RegionList copy = regions;
 
-       /* sort according to the model */
+       /* sort according to the model and the layering mode that we're in */
+
+       if (_explicit_relayering) {
+
+               copy.sort (RegionSortByLayerWithPending ());
+
+       } else if (_session.config.get_layer_model() == MoveAddHigher || _session.config.get_layer_model() == AddHigher) {
+
+               copy.sort (RegionSortByLastLayerOp ());
 
-       if (Config->get_layer_model() == MoveAddHigher || Config->get_layer_model() == AddHigher) {
-               RegionSortByLastLayerOp cmp;
-               copy.sort (cmp);
        }
-       
+
        for (RegionList::iterator i = copy.begin(); i != copy.end(); ++i) {
 
+               /* reset the pending explicit relayer flag for every region, now that we're relayering */
+               (*i)->set_pending_explicit_relayer (false);
+
+               /* find the time divisions that this region covers */
+               int const start_division = floor ( ((*i)->position() - start) / division_size);
+               int end_division = floor ( ((*i)->position() + (*i)->length() - start) / division_size );
+               if (end_division == divisions) {
+                       end_division--;
+               }
+
+               assert (end_division < divisions);
+
                /* find the lowest layer that this region can go on */
                size_t j = layers.size();
                while (j > 0) {
                        /* try layer j - 1; it can go on if it overlaps no other region
                           that is already on that layer
                        */
-                       RegionList::iterator k = layers[j - 1].begin();
-                       while (k != layers[j - 1].end()) {
-                               if ((*k)->overlap_equivalent (*i)) {
+
+                       bool overlap = false;
+                       for (int k = start_division; k <= end_division; ++k) {
+                               RegionList::iterator l = layers[j-1][k].begin ();
+                               while (l != layers[j-1][k].end()) {
+                                       if ((*l)->overlap_equivalent (*i)) {
+                                               overlap = true;
+                                               break;
+                                       }
+                                       l++;
+                               }
+
+                               if (overlap) {
                                        break;
                                }
-                               k++;
                        }
 
-                       if (k != layers[j - 1].end()) {
-                               /* no overlap, so we can use this layer */
+                       if (overlap) {
+                               /* overlap, so we must use layer j */
                                break;
                        }
-                                       
-                       j--;
+
+                       --j;
                }
 
                if (j == layers.size()) {
                        /* we need a new layer for this region */
-                       layers.push_back (RegionList ());
+                       layers.push_back (vector<RegionList> (divisions));
                }
 
-               layers[j].push_back (*i);
-       }
-
-       /* first pass: set up the layer numbers in the regions */
-       for (size_t j = 0; j < layers.size(); ++j) {
-               for (RegionList::iterator i = layers[j].begin(); i != layers[j].end(); ++i) {
-                       (*i)->set_layer (j);
+               /* put a reference to this region in each of the divisions that it exists in */
+               for (int k = start_division; k <= end_division; ++k) {
+                       layers[j][k].push_back (*i);
                }
+
+               (*i)->set_layer (j);
        }
 
        /* sending Modified means that various kinds of layering
@@ -2045,8 +2146,8 @@ void
 Playlist::raise_region_to_top (boost::shared_ptr<Region> region)
 {
        /* does nothing useful if layering mode is later=higher */
-       if ((Config->get_layer_model() == MoveAddHigher) ||
-           (Config->get_layer_model() == AddHigher)) {
+       if ((_session.config.get_layer_model() == MoveAddHigher) ||
+           (_session.config.get_layer_model() == AddHigher)) {
                timestamp_layer_op (region);
                relayer ();
        }
@@ -2056,8 +2157,8 @@ void
 Playlist::lower_region_to_bottom (boost::shared_ptr<Region> region)
 {
        /* does nothing useful if layering mode is later=higher */
-       if ((Config->get_layer_model() == MoveAddHigher) ||
-           (Config->get_layer_model() == AddHigher)) {
+       if ((_session.config.get_layer_model() == MoveAddHigher) ||
+           (_session.config.get_layer_model() == AddHigher)) {
                region->set_last_layer_op (0);
                relayer ();
        }
@@ -2073,9 +2174,9 @@ Playlist::move_region_to_layer (layer_t target_layer, boost::shared_ptr<Region>
 
        {
                RegionLock rlock (const_cast<Playlist *> (this));
-               
+
                for (i = regions.begin(); i != regions.end(); ++i) {
-                       
+
                        if (region == *i) {
                                continue;
                        }
@@ -2085,7 +2186,7 @@ Playlist::move_region_to_layer (layer_t target_layer, boost::shared_ptr<Region>
                                /* region is moving up, move all regions on intermediate layers
                                   down 1
                                */
-                               
+
                                if ((*i)->layer() > region->layer() && (*i)->layer() <= target_layer) {
                                        dest = (*i)->layer() - 1;
                                } else {
@@ -2107,12 +2208,12 @@ Playlist::move_region_to_layer (layer_t target_layer, boost::shared_ptr<Region>
                        }
 
                        LayerInfo newpair;
-                       
+
                        newpair.first = *i;
                        newpair.second = dest;
-                       
+
                        layerinfo.push_back (newpair);
-               } 
+               }
        }
 
        /* now reset the layers without holding the region lock */
@@ -2129,10 +2230,10 @@ Playlist::move_region_to_layer (layer_t target_layer, boost::shared_ptr<Region>
        for (list<LayerInfo>::iterator x = layerinfo.begin(); x != layerinfo.end(); ++x) {
                check_dependents (x->first, false);
        }
-       
+
        check_dependents (region, false);
 #endif
-       
+
        return 0;
 }
 
@@ -2147,7 +2248,7 @@ Playlist::nudge_after (nframes_t start, nframes_t distance, bool forwards)
 
        {
                RegionLock rlock (const_cast<Playlist *> (this));
-               
+
                for (i = regions.begin(); i != regions.end(); ++i) {
 
                        if ((*i)->position() >= start) {
@@ -2159,9 +2260,9 @@ Playlist::nudge_after (nframes_t start, nframes_t distance, bool forwards)
                                        } else {
                                                new_pos = (*i)->position() + distance;
                                        }
-                                       
+
                                } else {
-                                       
+
                                        if ((*i)->position() > distance) {
                                                new_pos = (*i)->position() - distance;
                                        } else {
@@ -2210,7 +2311,7 @@ Playlist::region_by_id (ID id)
        }
        return boost::shared_ptr<Region> ();
 }
-       
+
 void
 Playlist::dump () const
 {
@@ -2222,9 +2323,9 @@ Playlist::dump () const
 
        for (RegionList::const_iterator i = regions.begin(); i != regions.end(); ++i) {
                r = *i;
-               cerr << "  " << r->name() << " [" 
-                    << r->start() << "+" << r->length() 
-                    << "] at " 
+               cerr << "  " << r->name() << " ["
+                    << r->start() << "+" << r->length()
+                    << "] at "
                     << r->position()
                     << " on layer "
                     << r->layer ()
@@ -2261,13 +2362,13 @@ Playlist::shuffle (boost::shared_ptr<Region> region, int dir)
 
        {
                RegionLock rlock (const_cast<Playlist*> (this));
-               
-               
+
+
                if (dir > 0) {
-                       
+
                        RegionList::iterator next;
 
-                       for (RegionList::iterator i = regions.begin(); i != regions.end(); ++i) {       
+                       for (RegionList::iterator i = regions.begin(); i != regions.end(); ++i) {
                                if ((*i) == region) {
                                        next = i;
                                        ++next;
@@ -2307,10 +2408,10 @@ Playlist::shuffle (boost::shared_ptr<Region> region, int dir)
                                }
                        }
                } else {
-                       
+
                        RegionList::iterator prev = regions.end();
-                       
-                       for (RegionList::iterator i = regions.begin(); i != regions.end(); prev = i, ++i) {     
+
+                       for (RegionList::iterator i = regions.begin(); i != regions.end(); prev = i, ++i) {
                                if ((*i) == region) {
 
                                        if (prev != regions.end()) {
@@ -2334,7 +2435,7 @@ Playlist::shuffle (boost::shared_ptr<Region> region, int dir)
 
                                                region->set_position ((*prev)->position(), this);
                                                (*prev)->set_position (new_pos, this);
-                                               
+
                                                /* avoid a full sort */
 
                                                regions.erase (i); // remove region
@@ -2355,17 +2456,17 @@ Playlist::shuffle (boost::shared_ptr<Region> region, int dir)
 
                relayer ();
                check_dependents (region, false);
-               
+
                notify_modified();
        }
 
 }
 
 bool
-Playlist::region_is_shuffle_constrained (boost::shared_ptr<Region>) 
+Playlist::region_is_shuffle_constrained (boost::shared_ptr<Region>)
 {
        RegionLock rlock (const_cast<Playlist*> (this));
-       
+
        if (regions.size() > 1) {
                return true;
        }
@@ -2380,10 +2481,41 @@ Playlist::update_after_tempo_map_change ()
        RegionList copy (regions);
 
        freeze ();
-       
-       for (RegionList::iterator i = copy.begin(); i != copy.end(); ++i) {     
+
+       for (RegionList::iterator i = copy.begin(); i != copy.end(); ++i) {
                (*i)->update_position_after_tempo_map_change ();
        }
 
        thaw ();
 }
+
+void
+Playlist::foreach_region (sigc::slot<void, boost::shared_ptr<Region> > s)
+{
+       RegionLock rl (this, false);
+       for (RegionList::iterator i = regions.begin(); i != regions.end(); ++i) {
+               s (*i);
+       }
+}
+
+void
+Playlist::set_explicit_relayering (bool e)
+{
+       if (e == false && _explicit_relayering == true) {
+
+               /* We are changing from explicit to implicit relayering; layering may have been changed whilst
+                  we were in explicit mode, and we don't want that to be undone next time an implicit relayer
+                  occurs.  Hence now we'll set up region last_layer_op values so that an implicit relayer
+                  at this point would keep regions on the same layers.
+
+                  From then on in, it's just you and your towel.
+               */
+
+               RegionLock rl (this);
+               for (RegionList::iterator i = regions.begin(); i != regions.end(); ++i) {
+                       (*i)->set_last_layer_op ((*i)->layer ());
+               }
+       }
+
+       _explicit_relayering = e;
+}