Use a map of region names to speed up
[ardour.git] / libs / ardour / region_factory.cc
index 96a88a6e090fb6d2543c6b315fd2b5d68be117a0..ddc0451523c356884442f3c8405d14d4e36aac23 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"
 
 using namespace ARDOUR;
 using namespace PBD;
+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;
-Glib::StaticMutex                             RegionFactory::region_name_map_lock;
-std::map<std::string, uint32_t>               RegionFactory::region_name_map;
+PBD::ScopedConnectionList*                    RegionFactory::region_list_connections = 0;
+Glib::StaticMutex                             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, bool announce)
@@ -57,7 +57,11 @@ RegionFactory::create (boost::shared_ptr<const Region> region, bool announce)
 
        } else if ((mr = boost::dynamic_pointer_cast<const MidiRegion>(region)) != 0) {
 
-               ret = boost::shared_ptr<Region> (new MidiRegion (mr, 0));
+               if (mr->session().config.get_midi_copy_is_fork()) {
+                       ret = mr->clone ();
+               } else {
+                       ret = boost::shared_ptr<Region> (new MidiRegion (mr, 0));
+               }
 
        } else {
                fatal << _("programming error: RegionFactory::create() called with unknown Region type")
@@ -67,15 +71,22 @@ 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);
                }
        }
 
-       boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
+#ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
+       // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
+#endif
        return ret;
 }
 
@@ -103,14 +114,20 @@ 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);
                }
        }
 
-       boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
+#ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
+        // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
+#endif
        return ret;
 }
 
@@ -124,7 +141,7 @@ RegionFactory::create (boost::shared_ptr<Region> region, frameoffset_t offset, c
        if ((other_a = boost::dynamic_pointer_cast<AudioRegion>(region)) != 0) {
 
                ret = boost::shared_ptr<Region> (new AudioRegion (other_a, offset));
-               
+
        } else if ((other_m = boost::dynamic_pointer_cast<MidiRegion>(region)) != 0) {
 
                ret = boost::shared_ptr<Region> (new MidiRegion (other_m, offset));
@@ -138,14 +155,20 @@ 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);
                }
        }
 
-       boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
+#ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
+       // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
+#endif
        return ret;
 }
 
@@ -162,7 +185,7 @@ 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())
-               
+
                ret = boost::shared_ptr<Region> (new AudioRegion (other, srcs));
 
        } else {
@@ -173,14 +196,20 @@ 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);
                }
        }
 
-       boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
+#ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
+        // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
+#endif
        return ret;
 }
 
@@ -195,7 +224,7 @@ RegionFactory::create (boost::shared_ptr<Source> src, const PropertyList& plist,
 boost::shared_ptr<Region>
 RegionFactory::create (const SourceList& srcs, const PropertyList& plist, bool announce)
 {
-       boost::shared_ptr<Region> ret; 
+       boost::shared_ptr<Region> ret;
        boost::shared_ptr<AudioSource> as;
        boost::shared_ptr<MidiSource> ms;
 
@@ -211,14 +240,20 @@ 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);
                }
        }
 
-       boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
+#ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
+       // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
+#endif
        return ret;
 }
 
@@ -242,7 +277,7 @@ RegionFactory::create (SourceList& srcs, const XMLNode& node)
                ret = boost::shared_ptr<Region> (new AudioRegion (srcs));
 
        } else if (srcs[0]->type() == DataType::MIDI) {
-               
+
                ret = boost::shared_ptr<Region> (new MidiRegion (srcs));
 
        }
@@ -252,11 +287,18 @@ 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);
                }
        }
 
-       boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
+#ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
+       // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region");
+#endif
        return ret;
 }
 
@@ -267,51 +309,38 @@ RegionFactory::map_add (boost::shared_ptr<Region> r)
        p.first = r->id();
        p.second = r;
 
-       { 
+       {
                Glib::Mutex::Lock lm (region_map_lock);
                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);
+       add_to_region_name_maps (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());
 
        if (i != region_map.end()) {
+               remove_from_region_name_map (i->second->name ());
                region_map.erase (i);
        }
 }
 
-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)
 {
@@ -333,7 +362,7 @@ RegionFactory::wholefile_region_by_name (const std::string& name)
                }
        }
        return boost::shared_ptr<Region>();
-}      
+}
 
 boost::shared_ptr<Region>
 RegionFactory::region_by_name (const std::string& name)
@@ -344,18 +373,21 @@ RegionFactory::region_by_name (const std::string& name)
                }
        }
        return boost::shared_ptr<Region>();
-}      
+}
 
 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);
                region_map.clear ();
+               _compound_associations.clear ();
+               region_name_map.clear ();
        }
-
 }
 
 void
@@ -381,7 +413,7 @@ RegionFactory::delete_all_regions ()
           vanish as we leave this scope, thus calling all destructors.
        */
 }
-        
+
 uint32_t
 RegionFactory::nregions ()
 {
@@ -389,8 +421,49 @@ RegionFactory::nregions ()
        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::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::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;
+       }
+
+       /* 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::update_region_name_map (boost::shared_ptr<Region> region)
+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::update_region_name_number_map (boost::shared_ptr<Region> region)
 {
        string::size_type const last_period = region->name().find_last_of ('.');
 
@@ -403,8 +476,8 @@ RegionFactory::update_region_name_map (boost::shared_ptr<Region> region)
                   which is just fine
                */
 
-               Glib::Mutex::Lock lm (region_name_map_lock);
-               region_name_map[base] = atoi (number.c_str ());
+               Glib::Mutex::Lock lm (region_name_maps_mutex);
+               region_name_number_map[base] = atoi (number.c_str ());
        }
 }
 
@@ -417,7 +490,7 @@ RegionFactory::region_changed (PropertyChange const & what_changed, boost::weak_
        }
 
        if (what_changed.contains (Properties::name)) {
-               update_region_name_map (r);
+               rename_in_region_name_maps (r);
        }
 }
 
@@ -453,15 +526,15 @@ RegionFactory::region_name (string& result, string base, bool newlevel)
                }
 
                {
-                       Glib::Mutex::Lock lm (region_name_map_lock);
+                       Glib::Mutex::Lock lm (region_name_maps_mutex);
 
                        map<string,uint32_t>::iterator x;
 
                        result = subbase;
 
-                       if ((x = region_name_map.find (subbase)) == region_name_map.end()) {
+                       if ((x = region_name_number_map.find (subbase)) == region_name_number_map.end()) {
                                result += ".1";
-                               region_name_map[subbase] = 1;
+                               region_name_number_map[subbase] = 1;
                        } else {
                                x->second++;
                                snprintf (buf, sizeof (buf), ".%d", x->second);
@@ -474,12 +547,23 @@ RegionFactory::region_name (string& result, string base, bool newlevel)
        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;
        char buf[len];
 
        if ((last_period = old.find_last_of ('.')) == string::npos) {
@@ -492,28 +576,37 @@ RegionFactory::new_region_name (string old)
 
        } else {
 
-               number = atoi (old.substr (last_period+1).c_str());
+               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)) {
-               
-               const RegionMap& regions (RegionFactory::regions());
-               RegionMap::const_iterator i;
+
                string sbuf;
 
                number++;
 
-               snprintf (buf, len, "%s%" PRIu32, old.substr (0, last_period + 1).c_str(), number);
+               snprintf (buf, len, "%s%" PRIu32 "%s", old.substr (0, last_period + 1).c_str(), number, remainder.c_str());
                sbuf = buf;
 
-               for (i = regions.begin(); i != regions.end(); ++i) {
-                       if (i->second->name() == sbuf) {
-                               break;
-                       }
-               }
-
-               if (i == regions.end()) {
+               if (region_name_map.find (sbuf) == region_name_map.end ()) {
                        break;
                }
        }
@@ -526,7 +619,7 @@ RegionFactory::new_region_name (string old)
        return old;
 }
 
-void 
+void
 RegionFactory::get_regions_using_source (boost::shared_ptr<Source> s, std::set<boost::shared_ptr<Region> >& r)
 {
        Glib::Mutex::Lock lm (region_map_lock);
@@ -537,3 +630,30 @@ RegionFactory::get_regions_using_source (boost::shared_ptr<Source> s, std::set<b
                }
        }
 }
+
+void
+RegionFactory::remove_regions_using_source (boost::shared_ptr<Source> src)
+{
+       Glib::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)
+{
+       Glib::Mutex::Lock lm (region_map_lock);
+       _compound_associations[copy] = orig;
+}