tweak wscript files to set PACKAGE in one place
[ardour.git] / libs / ardour / region_factory.cc
index 1098cb21433227d7e130b47726bb1e1fa45bcc4b..fba60fe0a01b5f356e36de2ec16c5856d03c3982 100644 (file)
@@ -36,6 +36,7 @@
 
 using namespace ARDOUR;
 using namespace PBD;
+using namespace std;
 
 PBD::Signal1<void,boost::shared_ptr<Region> > RegionFactory::CheckNewRegion;
 Glib::StaticMutex                             RegionFactory::region_map_lock;
@@ -43,6 +44,7 @@ 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;
+RegionFactory::CompoundAssociations           RegionFactory::_compound_associations;
 
 boost::shared_ptr<Region>
 RegionFactory::create (boost::shared_ptr<const Region> region, bool announce)
@@ -53,15 +55,15 @@ RegionFactory::create (boost::shared_ptr<const Region> region, bool announce)
 
        if ((ar = boost::dynamic_pointer_cast<const AudioRegion>(region)) != 0) {
 
-               AudioRegion* arn = new AudioRegion (ar, 0);
-               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);
-               boost::shared_ptr<MidiRegion> mrp (mrn);
-               ret = boost::static_pointer_cast<Region> (mrp);
+               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")
@@ -74,13 +76,14 @@ RegionFactory::create (boost::shared_ptr<const Region> region, bool announce)
                map_add (ret);
 
                /* pure copy constructor - no property list */
-               /* pure copy constructor - no CheckNewRegion emitted */
                if (announce) {
                        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;
 }
 
@@ -93,15 +96,11 @@ 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);
-               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);
-               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")
@@ -119,7 +118,9 @@ RegionFactory::create (boost::shared_ptr<Region> region, const PropertyList& pli
                }
        }
 
-       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;
 }
 
@@ -132,15 +133,11 @@ 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);
-               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, offset);
-               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")
@@ -158,7 +155,9 @@ RegionFactory::create (boost::shared_ptr<Region> region, frameoffset_t offset, c
                }
        }
 
-       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;
 }
 
@@ -175,10 +174,8 @@ 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::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")
@@ -195,7 +192,9 @@ RegionFactory::create (boost::shared_ptr<Region> region, const SourceList& srcs,
                }
        }
 
-       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;
 }
 
@@ -210,21 +209,17 @@ 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;
 
        if ((as = boost::dynamic_pointer_cast<AudioSource>(srcs[0])) != 0) {
 
-               AudioRegion* ar = new AudioRegion (srcs);
-               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::shared_ptr<MidiRegion> mrp (mr);
-               ret = boost::static_pointer_cast<Region> (mrp);
+               ret = boost::shared_ptr<Region> (new MidiRegion (srcs));
 
        }
 
@@ -237,7 +232,9 @@ RegionFactory::create (const SourceList& srcs, const PropertyList& plist, bool a
                }
        }
 
-       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;
 }
 
@@ -258,20 +255,15 @@ RegionFactory::create (SourceList& srcs, const XMLNode& node)
 
        if (srcs[0]->type() == DataType::AUDIO) {
 
-               AudioRegion* ar = new AudioRegion (srcs);
-               boost::shared_ptr<AudioRegion> arp (ar);
-               ret = boost::static_pointer_cast<Region> (arp);
+               ret = boost::shared_ptr<Region> (new AudioRegion (srcs));
 
        } else if (srcs[0]->type() == DataType::MIDI) {
-               
-               MidiRegion* mr = new MidiRegion (srcs);
-               boost::shared_ptr<MidiRegion> mrp (mr);
-               ret = boost::static_pointer_cast<Region> (mrp);
+
+               ret = boost::shared_ptr<Region> (new MidiRegion (srcs));
 
        }
 
        if (ret) {
-
                if (ret->set_state (node, Stateful::loading_state_version)) {
                        ret.reset ();
                } else {
@@ -280,7 +272,9 @@ RegionFactory::create (SourceList& srcs, const XMLNode& node)
                }
        }
 
-       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;
 }
 
@@ -291,7 +285,7 @@ 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);
        }
@@ -330,7 +324,7 @@ RegionFactory::map_remove_with_equivalents (boost::shared_ptr<Region> r)
                        region_map.erase (i);
                } else if (r == i->second) {
                        region_map.erase (i);
-               } 
+               }
 
                i = tmp;
        }
@@ -357,7 +351,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)
@@ -368,7 +362,7 @@ RegionFactory::region_by_name (const std::string& name)
                }
        }
        return boost::shared_ptr<Region>();
-}      
+}
 
 void
 RegionFactory::clear_map ()
@@ -378,8 +372,8 @@ RegionFactory::clear_map ()
        {
                Glib::Mutex::Lock lm (region_map_lock);
                region_map.clear ();
+               _compound_associations.clear ();
        }
-
 }
 
 void
@@ -405,7 +399,7 @@ RegionFactory::delete_all_regions ()
           vanish as we leave this scope, thus calling all destructors.
        */
 }
-        
+
 uint32_t
 RegionFactory::nregions ()
 {
@@ -498,12 +492,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) {
@@ -516,19 +521,36 @@ 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) {
@@ -550,7 +572,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);
@@ -561,3 +583,22 @@ 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);
+
+       for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) {
+               if (i->second->uses_source (src)) {
+                       region_map.erase (i);
+                }
+       }
+}
+
+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;
+}