Return silence from read_from_sources() if we try to read a channel that our source...
[ardour.git] / libs / ardour / region_factory.cc
index fba60fe0a01b5f356e36de2ec16c5856d03c3982..fa948844abcf2adc224127a03b1115fc5f8eb555 100644 (file)
 #include <inttypes.h>
 
 #include "pbd/error.h"
-#include "pbd/boost_debug.h"
 
-#include "ardour/session.h"
-
-#include "ardour/region_factory.h"
-#include "ardour/region.h"
 #include "ardour/audioregion.h"
 #include "ardour/audiosource.h"
-#include "ardour/midi_source.h"
 #include "ardour/midi_region.h"
-#include "ardour/utils.h"
+#include "ardour/midi_source.h"
+#include "ardour/region.h"
+#include "ardour/region_factory.h"
+#include "ardour/session.h"
 
 #include "i18n.h"
 
@@ -41,7 +38,7 @@ using namespace std;
 PBD::Signal1<void,boost::shared_ptr<Region> > RegionFactory::CheckNewRegion;
 Glib::StaticMutex                             RegionFactory::region_map_lock;
 RegionFactory::RegionMap                      RegionFactory::region_map;
-PBD::ScopedConnectionList                     RegionFactory::region_list_connections;
+PBD::ScopedConnectionList*                    RegionFactory::region_list_connections = 0;
 Glib::StaticMutex                             RegionFactory::region_name_map_lock;
 std::map<std::string, uint32_t>               RegionFactory::region_name_map;
 RegionFactory::CompoundAssociations           RegionFactory::_compound_associations;
@@ -73,10 +70,15 @@ RegionFactory::create (boost::shared_ptr<const Region> region, bool announce)
 
        if (ret) {
                ret->set_name (new_region_name(ret->name()));
-               map_add (ret);
+               ret->set_position (region->position());
+               
+               if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) {
+                       ret->set_position_lock_style (MusicTime);
+               }
 
                /* pure copy constructor - no property list */
                if (announce) {
+                       map_add (ret);
                        CheckNewRegion (ret);
                }
        }
@@ -111,9 +113,13 @@ RegionFactory::create (boost::shared_ptr<Region> region, const PropertyList& pli
 
        if (ret) {
                ret->apply_changes (plist);
-               map_add (ret);
 
+               if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) {
+                       ret->set_position_lock_style (MusicTime);
+               }
+               
                if (announce) {
+                       map_add (ret);
                        CheckNewRegion (ret);
                }
        }
@@ -148,9 +154,13 @@ RegionFactory::create (boost::shared_ptr<Region> region, frameoffset_t offset, c
 
        if (ret) {
                ret->apply_changes (plist);
-               map_add (ret);
+
+               if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) {
+                       ret->set_position_lock_style (MusicTime);
+               }
 
                if (announce) {
+                       map_add (ret);
                        CheckNewRegion (ret);
                }
        }
@@ -185,9 +195,13 @@ RegionFactory::create (boost::shared_ptr<Region> region, const SourceList& srcs,
 
        if (ret) {
                ret->apply_changes (plist);
-               map_add (ret);
+
+               if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) {
+                       ret->set_position_lock_style (MusicTime);
+               }
 
                if (announce) {
+                       map_add (ret);
                        CheckNewRegion (ret);
                }
        }
@@ -225,9 +239,13 @@ RegionFactory::create (const SourceList& srcs, const PropertyList& plist, bool a
 
        if (ret) {
                ret->apply_changes (plist);
-               map_add (ret);
 
+               if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) {
+                       ret->set_position_lock_style (MusicTime);
+               }
+               
                if (announce) {
+                       map_add (ret);
                        CheckNewRegion (ret);
                }
        }
@@ -268,6 +286,11 @@ RegionFactory::create (SourceList& srcs, const XMLNode& node)
                        ret.reset ();
                } else {
                        map_add (ret);
+
+                       /* Don't fiddle with position_lock_style here as the region
+                          description is coming from XML.
+                       */
+                       
                        CheckNewRegion (ret);
                }
        }
@@ -290,19 +313,24 @@ RegionFactory::map_add (boost::shared_ptr<Region> r)
                region_map.insert (p);
        }
 
-       r->DropReferences.connect_same_thread (region_list_connections, boost::bind (&RegionFactory::map_remove, r));
+       if (!region_list_connections) {
+               region_list_connections = new ScopedConnectionList;
+       }
 
-       r->PropertyChanged.connect_same_thread (
-               region_list_connections,
-               boost::bind (&RegionFactory::region_changed, _1, boost::weak_ptr<Region> (r))
-               );
+       r->DropReferences.connect_same_thread (*region_list_connections, boost::bind (&RegionFactory::map_remove, boost::weak_ptr<Region> (r)));
+       r->PropertyChanged.connect_same_thread (*region_list_connections, boost::bind (&RegionFactory::region_changed, _1, boost::weak_ptr<Region> (r)));
 
        update_region_name_map (r);
 }
 
 void
-RegionFactory::map_remove (boost::shared_ptr<Region> r)
+RegionFactory::map_remove (boost::weak_ptr<Region> w)
 {
+       boost::shared_ptr<Region> r = w.lock ();
+       if (!r) {
+               return;
+       }
+       
        Glib::Mutex::Lock lm (region_map_lock);
        RegionMap::iterator i = region_map.find (r->id());
 
@@ -311,25 +339,6 @@ RegionFactory::map_remove (boost::shared_ptr<Region> r)
        }
 }
 
-void
-RegionFactory::map_remove_with_equivalents (boost::shared_ptr<Region> r)
-{
-       Glib::Mutex::Lock lm (region_map_lock);
-
-       for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ) {
-               RegionMap::iterator tmp = i;
-               ++tmp;
-
-               if (r->region_list_equivalent (i->second)) {
-                       region_map.erase (i);
-               } else if (r == i->second) {
-                       region_map.erase (i);
-               }
-
-               i = tmp;
-       }
-}
-
 boost::shared_ptr<Region>
 RegionFactory::region_by_id (const PBD::ID& id)
 {
@@ -367,7 +376,9 @@ RegionFactory::region_by_name (const std::string& name)
 void
 RegionFactory::clear_map ()
 {
-       region_list_connections.drop_connections ();
+       if (region_list_connections) {
+               region_list_connections->drop_connections ();
+       }
 
        {
                Glib::Mutex::Lock lm (region_map_lock);
@@ -589,10 +600,17 @@ RegionFactory::remove_regions_using_source (boost::shared_ptr<Source> src)
 {
        Glib::Mutex::Lock lm (region_map_lock);
 
-       for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
+       RegionMap::iterator i = region_map.begin();
+       while (i != region_map.end()) {
+
+               RegionMap::iterator j = i;
+               ++j;
+               
                if (i->second->uses_source (src)) {
                        region_map.erase (i);
                 }
+
+               i = j;
        }
 }