X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fardour%2Fregion_factory.cc;h=c1f3ecf47428cb42e6fe5c5a5dff95a3e7718260;hb=79abf3dfa6d649fbf1fb6fd16cd489f434d0b909;hp=6af256f169ec356399893ed6694dc235cc5b4ed7;hpb=23f03f995f850898c1e8d133db318a210774bcd7;p=ardour.git diff --git a/libs/ardour/region_factory.cc b/libs/ardour/region_factory.cc index 6af256f169..c1f3ecf474 100644 --- a/libs/ardour/region_factory.cc +++ b/libs/ardour/region_factory.cc @@ -19,35 +19,34 @@ #include +#include "pbd/basename.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" +#include "pbd/i18n.h" using namespace ARDOUR; using namespace PBD; using namespace std; PBD::Signal1 > RegionFactory::CheckNewRegion; -Glib::StaticMutex RegionFactory::region_map_lock; +Glib::Threads::Mutex RegionFactory::region_map_lock; RegionFactory::RegionMap RegionFactory::region_map; PBD::ScopedConnectionList* RegionFactory::region_list_connections = 0; -Glib::StaticMutex RegionFactory::region_name_map_lock; -std::map RegionFactory::region_name_map; +Glib::Threads::Mutex RegionFactory::region_name_maps_mutex; +std::map RegionFactory::region_name_number_map; +std::map RegionFactory::region_name_map; RegionFactory::CompoundAssociations RegionFactory::_compound_associations; boost::shared_ptr -RegionFactory::create (boost::shared_ptr region, bool announce) +RegionFactory::create (boost::shared_ptr region, bool announce, bool fork) { boost::shared_ptr ret; boost::shared_ptr ar; @@ -55,34 +54,42 @@ RegionFactory::create (boost::shared_ptr region, bool announce) if ((ar = boost::dynamic_pointer_cast(region)) != 0) { - ret = boost::shared_ptr (new AudioRegion (ar, 0)); + ret = boost::shared_ptr (new AudioRegion (ar, MusicFrame (0, 0))); } else if ((mr = boost::dynamic_pointer_cast(region)) != 0) { - if (mr->session().config.get_midi_copy_is_fork()) { - ret = mr->clone (); + if (mr->session().config.get_midi_copy_is_fork() || 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 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 (new MidiRegion (mr, 0)); + ret = boost::shared_ptr (new MidiRegion (mr, MusicFrame (0, 0))); } } else { fatal << _("programming error: RegionFactory::create() called with unknown Region type") << endmsg; - /*NOTREACHED*/ + abort(); /*NOTREACHED*/ } if (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 ()) { + + if (ret->session().config.get_glue_new_regions_to_bars_and_beats() && ret->position_lock_style() != MusicTime) { ret->set_position_lock_style (MusicTime); } - - map_add (ret); /* pure copy constructor - no property list */ if (announce) { + map_add (ret); CheckNewRegion (ret); } } @@ -111,20 +118,19 @@ RegionFactory::create (boost::shared_ptr region, const PropertyList& pli } else { fatal << _("programming error: RegionFactory::create() called with unknown Region type") << endmsg; - /*NOTREACHED*/ + abort(); /*NOTREACHED*/ return boost::shared_ptr(); } if (ret) { ret->apply_changes (plist); - if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) { + if (ret->session().config.get_glue_new_regions_to_bars_and_beats() && ret->position_lock_style() != MusicTime) { ret->set_position_lock_style (MusicTime); } - - map_add (ret); if (announce) { + map_add (ret); CheckNewRegion (ret); } } @@ -136,7 +142,7 @@ RegionFactory::create (boost::shared_ptr region, const PropertyList& pli } boost::shared_ptr -RegionFactory::create (boost::shared_ptr region, frameoffset_t offset, const PropertyList& plist, bool announce) +RegionFactory::create (boost::shared_ptr region, MusicFrame offset, const PropertyList& plist, bool announce) { boost::shared_ptr ret; boost::shared_ptr other_a; @@ -153,20 +159,19 @@ RegionFactory::create (boost::shared_ptr region, frameoffset_t offset, c } else { fatal << _("programming error: RegionFactory::create() called with unknown Region type") << endmsg; - /*NOTREACHED*/ + abort(); /*NOTREACHED*/ return boost::shared_ptr(); } if (ret) { ret->apply_changes (plist); - if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) { + if (ret->session().config.get_glue_new_regions_to_bars_and_beats() && ret->position_lock_style() != MusicTime) { ret->set_position_lock_style (MusicTime); } - map_add (ret); - if (announce) { + map_add (ret); CheckNewRegion (ret); } } @@ -196,19 +201,18 @@ RegionFactory::create (boost::shared_ptr region, const SourceList& srcs, } else { fatal << _("programming error: RegionFactory::create() called with unknown Region type") << endmsg; - /*NOTREACHED*/ + abort(); /*NOTREACHED*/ } if (ret) { ret->apply_changes (plist); - if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) { + if (ret->session().config.get_glue_new_regions_to_bars_and_beats() && ret->position_lock_style() != MusicTime) { ret->set_position_lock_style (MusicTime); } - map_add (ret); - if (announce) { + map_add (ret); CheckNewRegion (ret); } } @@ -247,13 +251,12 @@ RegionFactory::create (const SourceList& srcs, const PropertyList& plist, bool a if (ret) { ret->apply_changes (plist); - if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) { + if (ret->session().config.get_glue_new_regions_to_bars_and_beats() && ret->position_lock_style() != MusicTime) { ret->set_position_lock_style (MusicTime); } - - map_add (ret); if (announce) { + map_add (ret); CheckNewRegion (ret); } } @@ -298,7 +301,7 @@ RegionFactory::create (SourceList& srcs, const XMLNode& node) /* Don't fiddle with position_lock_style here as the region description is coming from XML. */ - + CheckNewRegion (ret); } } @@ -317,7 +320,7 @@ RegionFactory::map_add (boost::shared_ptr r) p.second = r; { - Glib::Mutex::Lock lm (region_map_lock); + Glib::Threads::Mutex::Lock lm (region_map_lock); region_map.insert (p); } @@ -328,7 +331,7 @@ RegionFactory::map_add (boost::shared_ptr r) r->DropReferences.connect_same_thread (*region_list_connections, boost::bind (&RegionFactory::map_remove, boost::weak_ptr (r))); r->PropertyChanged.connect_same_thread (*region_list_connections, boost::bind (&RegionFactory::region_changed, _1, boost::weak_ptr (r))); - update_region_name_map (r); + add_to_region_name_maps (r); } void @@ -338,11 +341,12 @@ RegionFactory::map_remove (boost::weak_ptr w) if (!r) { return; } - - Glib::Mutex::Lock lm (region_map_lock); + + 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); } } @@ -389,9 +393,10 @@ RegionFactory::clear_map () } { - Glib::Mutex::Lock lm (region_map_lock); + Glib::Threads::Mutex::Lock lm (region_map_lock); region_map.clear (); _compound_associations.clear (); + region_name_map.clear (); } } @@ -402,7 +407,7 @@ RegionFactory::delete_all_regions () /* copy region list */ { - Glib::Mutex::Lock lm (region_map_lock); + Glib::Threads::Mutex::Lock lm (region_map_lock); copy = region_map; } @@ -422,12 +427,53 @@ RegionFactory::delete_all_regions () uint32_t RegionFactory::nregions () { - Glib::Mutex::Lock lm (region_map_lock); + Glib::Threads::Mutex::Lock lm (region_map_lock); return region_map.size (); } +/** Add a region to the two region name maps */ void -RegionFactory::update_region_name_map (boost::shared_ptr region) +RegionFactory::add_to_region_name_maps (boost::shared_ptr 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) +{ + update_region_name_number_map (region); + + Glib::Threads::Mutex::Lock lm (region_name_maps_mutex); + + map::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::remove_from_region_name_map (string n) +{ + map::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) { string::size_type const last_period = region->name().find_last_of ('.'); @@ -440,8 +486,8 @@ RegionFactory::update_region_name_map (boost::shared_ptr region) which is just fine */ - Glib::Mutex::Lock lm (region_name_map_lock); - region_name_map[base] = atoi (number.c_str ()); + Glib::Threads::Mutex::Lock lm (region_name_maps_mutex); + region_name_number_map[base] = atoi (number.c_str ()); } } @@ -454,7 +500,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); } } @@ -490,15 +536,15 @@ RegionFactory::region_name (string& result, string base, bool newlevel) } { - Glib::Mutex::Lock lm (region_name_map_lock); + Glib::Threads::Mutex::Lock lm (region_name_maps_mutex); map::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); @@ -528,7 +574,7 @@ RegionFactory::new_region_name (string old) uint32_t number; string::size_type len = old.length() + 64; string remainder; - char buf[len]; + std::vector buf(len); if ((last_period = old.find_last_of ('.')) == string::npos) { @@ -563,28 +609,20 @@ RegionFactory::new_region_name (string old) while (number < (UINT_MAX-1)) { - const RegionMap& regions (RegionFactory::regions()); - RegionMap::const_iterator i; string sbuf; number++; - snprintf (buf, len, "%s%" PRIu32 "%s", old.substr (0, last_period + 1).c_str(), number, remainder.c_str()); - sbuf = buf; + snprintf (&buf[0], len, "%s%" PRIu32 "%s", old.substr (0, last_period + 1).c_str(), number, remainder.c_str()); + sbuf = &buf[0]; - 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; } } if (number != (UINT_MAX-1)) { - return buf; + return &buf[0]; } error << string_compose (_("cannot create new name for region \"%1\""), old) << endmsg; @@ -594,9 +632,9 @@ RegionFactory::new_region_name (string old) void RegionFactory::get_regions_using_source (boost::shared_ptr s, std::set >& r) { - Glib::Mutex::Lock lm (region_map_lock); + Glib::Threads::Mutex::Lock lm (region_map_lock); - for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) { + for (RegionMap::const_iterator i = region_map.begin(); i != region_map.end(); ++i) { if (i->second->uses_source (s)) { r.insert (i->second); } @@ -606,15 +644,16 @@ RegionFactory::get_regions_using_source (boost::shared_ptr s, std::set src) { - Glib::Mutex::Lock lm (region_map_lock); + 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); } @@ -625,6 +664,6 @@ RegionFactory::remove_regions_using_source (boost::shared_ptr src) void RegionFactory::add_compound_association (boost::shared_ptr orig, boost::shared_ptr copy) { - Glib::Mutex::Lock lm (region_map_lock); + Glib::Threads::Mutex::Lock lm (region_map_lock); _compound_associations[copy] = orig; }