strict i/o: limit output channels.
[ardour.git] / libs / ardour / region_factory.cc
index ae4df173e4b0b59623a815d28f67256aff2f0543..33c714cd1d80c8d3bb337e9f448cf5a7b528865a 100644 (file)
 
 */
 
-#include "pbd/error.h"
-#include "pbd/boost_debug.h"
+#include <inttypes.h>
 
-#include "ardour/session.h"
+#include "pbd/basename.h"
+#include "pbd/error.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/midi_source.h"
+#include "ardour/region.h"
+#include "ardour/region_factory.h"
+#include "ardour/session.h"
 
 #include "i18n.h"
 
 using namespace ARDOUR;
 using namespace PBD;
+using namespace std;
 
 PBD::Signal1<void,boost::shared_ptr<Region> > RegionFactory::CheckNewRegion;
-map<PBD::ID,boost::weak_ptr<Region> > RegionFactory::region_map;
+Glib::Threads::Mutex                          RegionFactory::region_map_lock;
+RegionFactory::RegionMap                      RegionFactory::region_map;
+PBD::ScopedConnectionList*                    RegionFactory::region_list_connections = 0;
+Glib::Threads::Mutex                          RegionFactory::region_name_maps_mutex;
+std::map<std::string, uint32_t>               RegionFactory::region_name_number_map;
+std::map<std::string, PBD::ID>                RegionFactory::region_name_map;
+RegionFactory::CompoundAssociations           RegionFactory::_compound_associations;
 
 boost::shared_ptr<Region>
-RegionFactory::create (boost::shared_ptr<const Region> region)
+RegionFactory::create (boost::shared_ptr<const Region> region, bool announce)
 {
        boost::shared_ptr<Region> ret;
        boost::shared_ptr<const AudioRegion> ar;
@@ -46,37 +54,55 @@ RegionFactory::create (boost::shared_ptr<const Region> region)
 
        if ((ar = boost::dynamic_pointer_cast<const AudioRegion>(region)) != 0) {
 
-               AudioRegion* arn = new AudioRegion (ar, 0, true);
-               boost_debug_shared_ptr_mark_interesting (arn, "Region");
-
-               boost::shared_ptr<AudioRegion> arp (arn);
-               ret = boost::static_pointer_cast<Region> (arp);
+               ret = boost::shared_ptr<Region> (new AudioRegion (ar, 0));
 
        } else if ((mr = boost::dynamic_pointer_cast<const MidiRegion>(region)) != 0) {
 
-               MidiRegion* mrn = new MidiRegion (mr, 0, true);
-               boost::shared_ptr<MidiRegion> mrp (mrn);
-               ret = boost::static_pointer_cast<Region> (mrp);
+               if (mr->session().config.get_midi_copy_is_fork()) {
+                       /* What we really want to do here is what Editor::fork_region()
+                          does via Session::create_midi_source_by_stealing_name(), but we
+                          don't have a Track.  We'll just live with the skipped number,
+                          and store the ancestral name of sources so multiple clones
+                          generates reasonable names that don't have too many suffixes. */
+                       const std::string ancestor_name = mr->sources().front()->ancestor_name();
+                       const std::string base          = PBD::basename_nosuffix(ancestor_name);
+
+                       boost::shared_ptr<MidiSource> source = mr->session().create_midi_source_for_session(base);
+                       source->set_ancestor_name(mr->sources().front()->name());
+                       ret = mr->clone(source);
+               } else {
+                       ret = boost::shared_ptr<Region> (new MidiRegion (mr, 0));
+               }
 
        } else {
                fatal << _("programming error: RegionFactory::create() called with unknown Region type")
                      << endmsg;
-               /*NOTREACHED*/
+               abort(); /*NOTREACHED*/
        }
 
        if (ret) {
-               ret->unlock_property_changes ();
-               map_add (ret);
+               ret->set_name (new_region_name(ret->name()));
+               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 */
-               /* pure copy constructor - no CheckNewRegion emitted */
+               if (announce) {
+                       map_add (ret);
+                       CheckNewRegion (ret);
+               }
        }
 
+#ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
+       // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
+#endif
        return ret;
 }
 
 boost::shared_ptr<Region>
-RegionFactory::create (boost::shared_ptr<Region> region, frameoffset_t offset, const PropertyList& plist, bool announce)
+RegionFactory::create (boost::shared_ptr<Region> region, const PropertyList& plist, bool announce)
 {
        boost::shared_ptr<Region> ret;
        boost::shared_ptr<const AudioRegion> other_a;
@@ -84,42 +110,40 @@ RegionFactory::create (boost::shared_ptr<Region> region, frameoffset_t offset, c
 
        if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
 
-               AudioRegion* ar = new AudioRegion (other_a, offset, true);
-               boost_debug_shared_ptr_mark_interesting (ar, "Region");
-
-               boost::shared_ptr<AudioRegion> arp (ar);
-               ret = boost::static_pointer_cast<Region> (arp);
+               ret = boost::shared_ptr<Region> (new AudioRegion (other_a));
 
        } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
 
-               MidiRegion* mr = new MidiRegion (other_m, offset, true);
-               boost::shared_ptr<MidiRegion> mrp (mr);
-               ret = boost::static_pointer_cast<Region> (mrp);
+               ret = boost::shared_ptr<Region> (new MidiRegion (other_m));
 
        } else {
                fatal << _("programming error: RegionFactory::create() called with unknown Region type")
                      << endmsg;
-               /*NOTREACHED*/
+               abort(); /*NOTREACHED*/
                return boost::shared_ptr<Region>();
        }
 
        if (ret) {
+               ret->apply_changes (plist);
 
-               ret->set_properties (plist);
-               ret->unlock_property_changes ();
-
-               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);
                }
        }
 
+#ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
+        // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
+#endif
        return ret;
 }
 
 boost::shared_ptr<Region>
-RegionFactory::create (boost::shared_ptr<Region> region, const PropertyList& plist, bool announce)
+RegionFactory::create (boost::shared_ptr<Region> region, frameoffset_t offset, const PropertyList& plist, bool announce)
 {
        boost::shared_ptr<Region> ret;
        boost::shared_ptr<const AudioRegion> other_a;
@@ -127,43 +151,38 @@ RegionFactory::create (boost::shared_ptr<Region> region, const PropertyList& pli
 
        if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
 
-               AudioRegion* ar = new AudioRegion (other_a, 0, false);
-               boost_debug_shared_ptr_mark_interesting (ar, "Region");
-
-               boost::shared_ptr<AudioRegion> arp (ar);
-               ret = boost::static_pointer_cast<Region> (arp);
+               ret = boost::shared_ptr<Region> (new AudioRegion (other_a, offset));
 
        } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
 
-               MidiRegion* mr = new MidiRegion (other_m, 0, false);
-               boost::shared_ptr<MidiRegion> mrp (mr);
-               ret = boost::static_pointer_cast<Region> (mrp);
+               ret = boost::shared_ptr<Region> (new MidiRegion (other_m, offset));
 
        } else {
                fatal << _("programming error: RegionFactory::create() called with unknown Region type")
                      << endmsg;
-               /*NOTREACHED*/
+               abort(); /*NOTREACHED*/
                return boost::shared_ptr<Region>();
        }
 
        if (ret) {
+               ret->apply_changes (plist);
 
-               ret->set_properties (plist);
-               ret->unlock_property_changes ();
-
-               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);
                }
        }
 
+#ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
+       // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
+#endif
        return ret;
 }
 
-
-
-
 boost::shared_ptr<Region>
 RegionFactory::create (boost::shared_ptr<Region> region, const SourceList& srcs, const PropertyList& plist, bool announce)
 {
@@ -177,183 +196,475 @@ RegionFactory::create (boost::shared_ptr<Region> region, const SourceList& srcs,
        if ((other = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
 
                // XXX use me in caller where plist is setup, this is start i think srcs.front()->length (srcs.front()->timeline_position())
-               
-               AudioRegion* ar = new AudioRegion (other, srcs);
-               boost_debug_shared_ptr_mark_interesting (ar, "Region");
 
-               boost::shared_ptr<AudioRegion> arp (ar);
-               ret = boost::static_pointer_cast<Region> (arp);
+               ret = boost::shared_ptr<Region> (new AudioRegion (other, srcs));
 
        } else {
                fatal << _("programming error: RegionFactory::create() called with unknown Region type")
                      << endmsg;
-               /*NOTREACHED*/
+               abort(); /*NOTREACHED*/
        }
 
        if (ret) {
+               ret->apply_changes (plist);
 
-               ret->set_properties (plist);
-               ret->unlock_property_changes ();
-
-               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);
                }
        }
 
+#ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
+        // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
+#endif
        return ret;
-
 }
 
 boost::shared_ptr<Region>
 RegionFactory::create (boost::shared_ptr<Source> src, const PropertyList& plist, bool announce)
 {
-       boost::shared_ptr<Region> ret; 
+       SourceList srcs;
+       srcs.push_back (src);
+       return create (srcs, plist, announce);
+}
+
+boost::shared_ptr<Region>
+RegionFactory::create (const SourceList& srcs, const PropertyList& plist, bool announce)
+{
+       boost::shared_ptr<Region> ret;
        boost::shared_ptr<AudioSource> as;
        boost::shared_ptr<MidiSource> ms;
 
-       if ((as = boost::dynamic_pointer_cast<AudioSource>(src)) != 0) {
+       if ((as = boost::dynamic_pointer_cast<AudioSource>(srcs[0])) != 0) {
 
-               AudioRegion* ar = new AudioRegion (as);
-               boost_debug_shared_ptr_mark_interesting (ar, "Region");
+               ret = boost::shared_ptr<Region> (new AudioRegion (srcs));
 
-               boost::shared_ptr<AudioRegion> arp (ar);
-               ret = boost::static_pointer_cast<Region> (arp);
+       } else if ((ms = boost::dynamic_pointer_cast<MidiSource>(srcs[0])) != 0) {
 
-       } else if ((ms = boost::dynamic_pointer_cast<MidiSource>(src)) != 0) {
-               MidiRegion* mr = new MidiRegion (ms);
-               boost_debug_shared_ptr_mark_interesting (mr, "Region");
+               ret = boost::shared_ptr<Region> (new MidiRegion (srcs));
 
-               boost::shared_ptr<MidiRegion> mrp (mr);
-               ret = boost::static_pointer_cast<Region> (mrp);
        }
 
        if (ret) {
-               ret->set_properties (plist);
-               ret->unlock_property_changes ();
+               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);
                }
        }
 
+#ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
+       // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
+#endif
        return ret;
 }
 
 boost::shared_ptr<Region>
-RegionFactory::create (const SourceList& srcs, const PropertyList& plist, bool announce)
+RegionFactory::create (Session& session, XMLNode& node, bool yn)
 {
-       boost::shared_ptr<Region> ret; 
-       boost::shared_ptr<AudioSource> as;
-       boost::shared_ptr<MidiSource> ms;
+       return session.XMLRegionFactory (node, yn);
+}
 
-       if ((as = boost::dynamic_pointer_cast<AudioSource>(srcs[0])) != 0) {
+boost::shared_ptr<Region>
+RegionFactory::create (SourceList& srcs, const XMLNode& node)
+{
+       boost::shared_ptr<Region> ret;
+
+       if (srcs.empty()) {
+               return ret;
+       }
 
-               AudioRegion* ar = new AudioRegion (srcs);
-               boost_debug_shared_ptr_mark_interesting (ar, "Region");
+       if (srcs[0]->type() == DataType::AUDIO) {
 
-               boost::shared_ptr<AudioRegion> arp (ar);
-               ret = boost::static_pointer_cast<Region> (arp);
+               ret = boost::shared_ptr<Region> (new AudioRegion (srcs));
 
-       } else if ((ms = boost::dynamic_pointer_cast<MidiSource>(srcs[0])) != 0) {
-               MidiRegion* mr = new MidiRegion (srcs);
-               boost_debug_shared_ptr_mark_interesting (mr, "Region");
+       } else if (srcs[0]->type() == DataType::MIDI) {
+
+               ret = boost::shared_ptr<Region> (new MidiRegion (srcs));
 
-               boost::shared_ptr<MidiRegion> mrp (mr);
-               ret = boost::static_pointer_cast<Region> (mrp);
        }
 
        if (ret) {
-               ret->set_properties (plist);
-               ret->unlock_property_changes ();
+               if (ret->set_state (node, Stateful::loading_state_version)) {
+                       ret.reset ();
+               } else {
+                       map_add (ret);
 
-               map_add (ret);
+                       /* Don't fiddle with position_lock_style here as the region
+                          description is coming from XML.
+                       */
 
-               if (announce) {
                        CheckNewRegion (ret);
                }
        }
 
+#ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
+       // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
+#endif
        return ret;
 }
 
+void
+RegionFactory::map_add (boost::shared_ptr<Region> r)
+{
+       pair<ID,boost::shared_ptr<Region> > p;
+       p.first = r->id();
+       p.second = r;
+
+       {
+               Glib::Threads::Mutex::Lock lm (region_map_lock);
+               region_map.insert (p);
+       }
+
+       if (!region_list_connections) {
+               region_list_connections = new ScopedConnectionList;
+       }
+
+       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)));
+
+       add_to_region_name_maps (r);
+}
+
+void
+RegionFactory::map_remove (boost::weak_ptr<Region> w)
+{
+       boost::shared_ptr<Region> r = w.lock ();
+       if (!r) {
+               return;
+       }
+
+       Glib::Threads::Mutex::Lock lm (region_map_lock);
+       RegionMap::iterator i = region_map.find (r->id());
+
+       if (i != region_map.end()) {
+               remove_from_region_name_map (i->second->name ());
+               region_map.erase (i);
+       }
+}
+
 boost::shared_ptr<Region>
-RegionFactory::create (Session& session, XMLNode& node, bool yn)
+RegionFactory::region_by_id (const PBD::ID& id)
 {
-       boost::shared_ptr<Region> r = session.XMLRegionFactory (node, yn);
+       RegionMap::iterator i = region_map.find (id);
 
-       if (r) {
-               r->unlock_property_changes ();
-               map_add (r);
-               CheckNewRegion (r);
+       if (i == region_map.end()) {
+               return boost::shared_ptr<Region>();
        }
 
-       return r;
+       return i->second;
 }
 
+boost::shared_ptr<Region>
+RegionFactory::wholefile_region_by_name (const std::string& name)
+{
+       for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
+               if (i->second->whole_file() && i->second->name() == name) {
+                       return i->second;
+               }
+       }
+       return boost::shared_ptr<Region>();
+}
 
 boost::shared_ptr<Region>
-RegionFactory::create (SourceList& srcs, const XMLNode& node)
+RegionFactory::region_by_name (const std::string& name)
 {
-       boost::shared_ptr<Region> ret;
+       for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
+               if (i->second->name() == name) {
+                       return i->second;
+               }
+       }
+       return boost::shared_ptr<Region>();
+}
 
-       if (srcs.empty()) {
-               return ret;
+void
+RegionFactory::clear_map ()
+{
+       if (region_list_connections) {
+               region_list_connections->drop_connections ();
        }
 
-       if (srcs[0]->type() == DataType::AUDIO) {
+       {
+               Glib::Threads::Mutex::Lock lm (region_map_lock);
+               region_map.clear ();
+               _compound_associations.clear ();
+               region_name_map.clear ();
+       }
+}
 
-               AudioRegion* ar = new AudioRegion (srcs, node);
-               boost_debug_shared_ptr_mark_interesting (ar, "Region");
+void
+RegionFactory::delete_all_regions ()
+{
+       RegionMap copy;
 
-               boost::shared_ptr<AudioRegion> arp (ar);
-               ret = boost::static_pointer_cast<Region> (arp);
+       /* copy region list */
+       {
+               Glib::Threads::Mutex::Lock lm (region_map_lock);
+               copy = region_map;
+       }
 
-       } else if (srcs[0]->type() == DataType::MIDI) {
-               
-               MidiRegion* mr = new MidiRegion (srcs, node);
+       /* clear existing map */
+       clear_map ();
 
-               boost::shared_ptr<MidiRegion> mrp (mr);
-               ret = boost::static_pointer_cast<Region> (mrp);
+       /* tell everyone to drop references */
+       for (RegionMap::iterator i = copy.begin(); i != copy.end(); ++i) {
+               i->second->drop_references ();
        }
 
-       if (ret) {
-               ret->unlock_property_changes ();
-               map_add (ret);
-               CheckNewRegion (ret);
+       /* the copy should now hold the only references, which will
+          vanish as we leave this scope, thus calling all destructors.
+       */
+}
+
+uint32_t
+RegionFactory::nregions ()
+{
+       Glib::Threads::Mutex::Lock lm (region_map_lock);
+       return region_map.size ();
+}
+
+/** Add a region to the two region name maps */
+void
+RegionFactory::add_to_region_name_maps (boost::shared_ptr<Region> region)
+{
+       update_region_name_number_map (region);
+
+       Glib::Threads::Mutex::Lock lm (region_name_maps_mutex);
+       region_name_map[region->name()] = region->id ();
+}
+
+/** Account for a region rename in the two region name maps */
+void
+RegionFactory::rename_in_region_name_maps (boost::shared_ptr<Region> region)
+{
+       update_region_name_number_map (region);
+
+       Glib::Threads::Mutex::Lock lm (region_name_maps_mutex);
+
+       map<string, PBD::ID>::iterator i = region_name_map.begin();
+       while (i != region_name_map.end() && i->second != region->id ()) {
+               ++i;
        }
 
-       return ret;
+       /* Erase the entry for the old name and put in a new one */
+       if (i != region_name_map.end()) {
+               region_name_map.erase (i);
+               region_name_map[region->name()] = region->id ();
+       }
 }
 
+/** Remove a region's details from the region_name_map */
+void
+RegionFactory::remove_from_region_name_map (string n)
+{
+       map<string, PBD::ID>::iterator i = region_name_map.find (n);
+       if (i != region_name_map.end ()) {
+               region_name_map.erase (i);
+       }
+}
 
+/** Update a region's entry in the region_name_number_map */
 void
-RegionFactory::map_add (boost::shared_ptr<Region> r)
+RegionFactory::update_region_name_number_map (boost::shared_ptr<Region> region)
 {
-       pair<ID,boost::weak_ptr<Region> > p;
-       p.first = r->id();
-       p.second = r;
+       string::size_type const last_period = region->name().find_last_of ('.');
+
+       if (last_period != string::npos && last_period < region->name().length() - 1) {
+
+               string const base = region->name().substr (0, last_period);
+               string const number = region->name().substr (last_period + 1);
 
-       region_map.insert (p);
+               /* note that if there is no number, we get zero from atoi,
+                  which is just fine
+               */
+
+               Glib::Threads::Mutex::Lock lm (region_name_maps_mutex);
+               region_name_number_map[base] = atoi (number.c_str ());
+       }
 }
 
-boost::shared_ptr<Region>
-RegionFactory::region_by_id (const PBD::ID& id)
+void
+RegionFactory::region_changed (PropertyChange const & what_changed, boost::weak_ptr<Region> w)
 {
-       map<ID,boost::weak_ptr<Region> >::iterator i = region_map.find (id);
+       boost::shared_ptr<Region> r = w.lock ();
+       if (!r) {
+               return;
+       }
 
-       if (i == region_map.end()) {
-               return boost::shared_ptr<Region>();
+       if (what_changed.contains (Properties::name)) {
+               rename_in_region_name_maps (r);
        }
+}
+
+int
+RegionFactory::region_name (string& result, string base, bool newlevel)
+{
+       char buf[16];
+       string subbase;
+
+       if (base.find("/") != string::npos) {
+               base = base.substr(base.find_last_of("/") + 1);
+       }
+
+       if (base == "") {
+
+               snprintf (buf, sizeof (buf), "%d", RegionFactory::nregions() + 1);
+               result = "region.";
+               result += buf;
+
+       } else {
+
+               if (newlevel) {
+                       subbase = base;
+               } else {
+                       string::size_type pos;
+
+                       pos = base.find_last_of ('.');
+
+                       /* pos may be npos, but then we just use entire base */
+
+                       subbase = base.substr (0, pos);
+
+               }
+
+               {
+                       Glib::Threads::Mutex::Lock lm (region_name_maps_mutex);
+
+                       map<string,uint32_t>::iterator x;
+
+                       result = subbase;
+
+                       if ((x = region_name_number_map.find (subbase)) == region_name_number_map.end()) {
+                               result += ".1";
+                               region_name_number_map[subbase] = 1;
+                       } else {
+                               x->second++;
+                               snprintf (buf, sizeof (buf), ".%d", x->second);
 
-       return i->second.lock();
+                               result += buf;
+                       }
+               }
+       }
+
+       return 0;
+}
+
+string
+RegionFactory::compound_region_name (const string& playlist, uint32_t compound_ops, uint32_t depth, bool whole_source)
+{
+       if (whole_source) {
+               return string_compose (_("%1 compound-%2 (%3)"), playlist, compound_ops+1, depth+1);
+       } else {
+               return string_compose (_("%1 compound-%2.1 (%3)"), playlist, compound_ops+1, depth+1);
+       }
 }
-       
+
+string
+RegionFactory::new_region_name (string old)
+{
+       string::size_type last_period;
+       uint32_t number;
+       string::size_type len = old.length() + 64;
+       string remainder;
+       std::vector<char> buf(len);
+
+       if ((last_period = old.find_last_of ('.')) == string::npos) {
+
+               /* no period present - add one explicitly */
+
+               old += '.';
+               last_period = old.length() - 1;
+               number = 0;
+
+       } else {
+
+               if (last_period < old.length() - 1) {
+
+                       string period_to_end = old.substr (last_period+1);
+
+                       /* extra material after the period */
+
+                       string::size_type numerals_end = period_to_end.find_first_not_of ("0123456789");
+
+                       number = atoi (period_to_end);
+
+                       if (numerals_end < period_to_end.length() - 1) {
+                               /* extra material after the end of the digits */
+                               remainder = period_to_end.substr (numerals_end);
+                       }
+
+               } else {
+                       last_period = old.length();
+                       number = 0;
+               }
+       }
+
+       while (number < (UINT_MAX-1)) {
+
+               string sbuf;
+
+               number++;
+
+               snprintf (&buf[0], len, "%s%" PRIu32 "%s", old.substr (0, last_period + 1).c_str(), number, remainder.c_str());
+               sbuf = &buf[0];
+
+               if (region_name_map.find (sbuf) == region_name_map.end ()) {
+                       break;
+               }
+       }
+
+       if (number != (UINT_MAX-1)) {
+               return &buf[0];
+       }
+
+       error << string_compose (_("cannot create new name for region \"%1\""), old) << endmsg;
+       return old;
+}
+
 void
-RegionFactory::clear_map ()
+RegionFactory::get_regions_using_source (boost::shared_ptr<Source> s, std::set<boost::shared_ptr<Region> >& r)
+{
+       Glib::Threads::Mutex::Lock lm (region_map_lock);
+
+       for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
+               if (i->second->uses_source (s)) {
+                       r.insert (i->second);
+               }
+       }
+}
+
+void
+RegionFactory::remove_regions_using_source (boost::shared_ptr<Source> src)
+{
+       Glib::Threads::Mutex::Lock lm (region_map_lock);
+
+       RegionMap::iterator i = region_map.begin();
+       while (i != region_map.end()) {
+
+               RegionMap::iterator j = i;
+               ++j;
+
+               if (i->second->uses_source (src)) {
+                       remove_from_region_name_map (i->second->name ());
+                       region_map.erase (i);
+                }
+
+               i = j;
+       }
+}
+
+void
+RegionFactory::add_compound_association (boost::shared_ptr<Region> orig, boost::shared_ptr<Region> copy)
 {
-       region_map.clear ();
+       Glib::Threads::Mutex::Lock lm (region_map_lock);
+       _compound_associations[copy] = orig;
 }