X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fardour%2Fregion_factory.cc;h=5e408b2786a4181ab7d87c21f27f2b80c5eaf1ff;hb=bc3e1a212250a67e53e86c972287a275834a967a;hp=65649ad8f745d15ba5a853bce0011c97396eb61f;hpb=f4401c59284258c6aa56707da64e3da32756329f;p=ardour.git diff --git a/libs/ardour/region_factory.cc b/libs/ardour/region_factory.cc index 65649ad8f7..5e408b2786 100644 --- a/libs/ardour/region_factory.cc +++ b/libs/ardour/region_factory.cc @@ -17,7 +17,6 @@ */ -#define __STDC_FORMAT_MACROS #include #include "pbd/error.h" @@ -37,16 +36,18 @@ using namespace ARDOUR; using namespace PBD; +using namespace std; PBD::Signal1 > 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 RegionFactory::region_name_map; +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 RegionFactory::region_name_map; +RegionFactory::CompoundAssociations RegionFactory::_compound_associations; boost::shared_ptr -RegionFactory::create (boost::shared_ptr region) +RegionFactory::create (boost::shared_ptr region, bool announce) { boost::shared_ptr ret; boost::shared_ptr ar; @@ -54,17 +55,15 @@ RegionFactory::create (boost::shared_ptr region) if ((ar = boost::dynamic_pointer_cast(region)) != 0) { - AudioRegion* arn = new AudioRegion (ar, 0, true); - boost_debug_shared_ptr_mark_interesting (arn, "Region"); - - boost::shared_ptr arp (arn); - ret = boost::static_pointer_cast (arp); + ret = boost::shared_ptr (new AudioRegion (ar, 0)); } else if ((mr = boost::dynamic_pointer_cast(region)) != 0) { - MidiRegion* mrn = new MidiRegion (mr, 0, true); - boost::shared_ptr mrp (mrn); - ret = boost::static_pointer_cast (mrp); + if (mr->session().config.get_midi_copy_is_fork()) { + ret = mr->clone (); + } else { + ret = boost::shared_ptr (new MidiRegion (mr, 0)); + } } else { fatal << _("programming error: RegionFactory::create() called with unknown Region type") @@ -73,30 +72,71 @@ RegionFactory::create (boost::shared_ptr region) } if (ret) { - cerr << "Pure copy constructor region " << ret << " named " << ret->name() << endl; + 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); + } + map_add (ret); /* pure copy constructor - no property list */ - /* pure copy constructor - no CheckNewRegion emitted */ + if (announce) { + CheckNewRegion (ret); + } } +#ifdef BOOST_SP_ENABLE_DEBUG_HOOKS + // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region"); +#endif return ret; } -boost::shared_ptr -RegionFactory::create (boost::shared_ptr region, frameoffset_t offset, const PropertyList& plist, bool announce) -{ - return create (region, offset, true, plist, announce); -} - boost::shared_ptr RegionFactory::create (boost::shared_ptr region, const PropertyList& plist, bool announce) { - return create (region, 0, false, plist, announce); + boost::shared_ptr ret; + boost::shared_ptr other_a; + boost::shared_ptr other_m; + + if ((other_a = boost::dynamic_pointer_cast(region)) != 0) { + + ret = boost::shared_ptr (new AudioRegion (other_a)); + + } else if ((other_m = boost::dynamic_pointer_cast(region)) != 0) { + + ret = boost::shared_ptr (new MidiRegion (other_m)); + + } else { + fatal << _("programming error: RegionFactory::create() called with unknown Region type") + << endmsg; + /*NOTREACHED*/ + return boost::shared_ptr(); + } + + if (ret) { + ret->apply_changes (plist); + + if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) { + ret->set_position_lock_style (MusicTime); + } + + map_add (ret); + + if (announce) { + CheckNewRegion (ret); + } + } + +#ifdef BOOST_SP_ENABLE_DEBUG_HOOKS + // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region"); +#endif + return ret; } boost::shared_ptr -RegionFactory::create (boost::shared_ptr region, frameoffset_t offset, bool offset_relative, const PropertyList& plist, bool announce) +RegionFactory::create (boost::shared_ptr region, frameoffset_t offset, const PropertyList& plist, bool announce) { boost::shared_ptr ret; boost::shared_ptr other_a; @@ -104,17 +144,11 @@ RegionFactory::create (boost::shared_ptr region, frameoffset_t offset, b if ((other_a = boost::dynamic_pointer_cast(region)) != 0) { - AudioRegion* ar = new AudioRegion (other_a, offset, offset_relative); - boost_debug_shared_ptr_mark_interesting (ar, "Region"); - - boost::shared_ptr arp (ar); - ret = boost::static_pointer_cast (arp); + ret = boost::shared_ptr (new AudioRegion (other_a, offset)); } else if ((other_m = boost::dynamic_pointer_cast(region)) != 0) { - MidiRegion* mr = new MidiRegion (other_m, offset, offset_relative); - boost::shared_ptr mrp (mr); - ret = boost::static_pointer_cast (mrp); + ret = boost::shared_ptr (new MidiRegion (other_m, offset)); } else { fatal << _("programming error: RegionFactory::create() called with unknown Region type") @@ -124,8 +158,12 @@ RegionFactory::create (boost::shared_ptr region, frameoffset_t offset, b } if (ret) { - ret->set_properties (plist); - cerr << "Partial copy constructor region\n"; + ret->apply_changes (plist); + + if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) { + ret->set_position_lock_style (MusicTime); + } + map_add (ret); if (announce) { @@ -133,6 +171,9 @@ RegionFactory::create (boost::shared_ptr region, frameoffset_t offset, b } } +#ifdef BOOST_SP_ENABLE_DEBUG_HOOKS + // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region"); +#endif return ret; } @@ -149,12 +190,8 @@ RegionFactory::create (boost::shared_ptr region, const SourceList& srcs, if ((other = boost::dynamic_pointer_cast(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 arp (ar); - ret = boost::static_pointer_cast (arp); + ret = boost::shared_ptr (new AudioRegion (other, srcs)); } else { fatal << _("programming error: RegionFactory::create() called with unknown Region type") @@ -163,9 +200,12 @@ RegionFactory::create (boost::shared_ptr region, const SourceList& srcs, } if (ret) { + ret->apply_changes (plist); + + if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) { + ret->set_position_lock_style (MusicTime); + } - ret->set_properties (plist); - cerr << "New sources copy constructor region\n"; map_add (ret); if (announce) { @@ -173,8 +213,10 @@ RegionFactory::create (boost::shared_ptr region, const SourceList& srcs, } } +#ifdef BOOST_SP_ENABLE_DEBUG_HOOKS + // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region"); +#endif return ret; - } boost::shared_ptr @@ -188,30 +230,27 @@ RegionFactory::create (boost::shared_ptr src, const PropertyList& plist, boost::shared_ptr RegionFactory::create (const SourceList& srcs, const PropertyList& plist, bool announce) { - boost::shared_ptr ret; + boost::shared_ptr ret; boost::shared_ptr as; boost::shared_ptr ms; if ((as = boost::dynamic_pointer_cast(srcs[0])) != 0) { - AudioRegion* ar = new AudioRegion (srcs); - boost_debug_shared_ptr_mark_interesting (ar, "Region"); - - boost::shared_ptr arp (ar); - ret = boost::static_pointer_cast (arp); + ret = boost::shared_ptr (new AudioRegion (srcs)); } else if ((ms = boost::dynamic_pointer_cast(srcs[0])) != 0) { - MidiRegion* mr = new MidiRegion (srcs); - boost_debug_shared_ptr_mark_interesting (mr, "Region"); - boost::shared_ptr mrp (mr); - ret = boost::static_pointer_cast (mrp); + ret = boost::shared_ptr (new MidiRegion (srcs)); + } if (ret) { + ret->apply_changes (plist); - ret->set_properties (plist); - cerr << "de-novo constructor region " << ret << " named " << ret->name() << endl; + if (ret->session().config.get_glue_new_regions_to_bars_and_beats ()) { + ret->set_position_lock_style (MusicTime); + } + map_add (ret); if (announce) { @@ -219,6 +258,9 @@ RegionFactory::create (const SourceList& srcs, const PropertyList& plist, bool a } } +#ifdef BOOST_SP_ENABLE_DEBUG_HOOKS + // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region"); +#endif return ret; } @@ -239,30 +281,31 @@ RegionFactory::create (SourceList& srcs, const XMLNode& node) if (srcs[0]->type() == DataType::AUDIO) { - AudioRegion* ar = new AudioRegion (srcs); - boost_debug_shared_ptr_mark_interesting (ar, "Region"); - - boost::shared_ptr arp (ar); - ret = boost::static_pointer_cast (arp); + ret = boost::shared_ptr (new AudioRegion (srcs)); } else if (srcs[0]->type() == DataType::MIDI) { - - MidiRegion* mr = new MidiRegion (srcs); - boost::shared_ptr mrp (mr); - ret = boost::static_pointer_cast (mrp); + ret = boost::shared_ptr (new MidiRegion (srcs)); + } if (ret) { - if (ret->set_state (node, Stateful::loading_state_version)) { ret.reset (); } else { map_add (ret); + + /* Don't fiddle with position_lock_style here as the region + description is coming from XML. + */ + CheckNewRegion (ret); } } +#ifdef BOOST_SP_ENABLE_DEBUG_HOOKS + // boost_debug_shared_ptr_mark_interesting (ret.get(), "Region"); +#endif return ret; } @@ -273,57 +316,49 @@ RegionFactory::map_add (boost::shared_ptr r) p.first = r->id(); p.second = r; - { - Glib::Mutex::Lock lm (region_map_lock); - region_map.insert (p); - } + { + 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)); + r->DropReferences.connect_same_thread (region_list_connections, boost::bind (&RegionFactory::map_remove, r)); r->PropertyChanged.connect_same_thread ( region_list_connections, boost::bind (&RegionFactory::region_changed, _1, boost::weak_ptr (r)) ); - cerr << "Added region with ID = " << r->id() << " named " << r->name() << endl; - update_region_name_map (r); } void RegionFactory::map_remove (boost::shared_ptr r) { - Glib::Mutex::Lock lm (region_map_lock); - RegionMap::iterator i = region_map.find (r->id()); - - if (i != region_map.end()) { - region_map.erase (i); - cerr << "Removed region with ID = " << r->id() << " named " << r->name() << endl;; - } + Glib::Mutex::Lock lm (region_map_lock); + RegionMap::iterator i = region_map.find (r->id()); + if (i != region_map.end()) { + region_map.erase (i); + } } void RegionFactory::map_remove_with_equivalents (boost::shared_ptr r) { - Glib::Mutex::Lock lm (region_map_lock); - - for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ) { - RegionMap::iterator tmp = i; - ++tmp; + Glib::Mutex::Lock lm (region_map_lock); - if (r->region_list_equivalent (i->second)) { - cerr << "Removed equivalent region " << i->second->name() << '/' << i->first << endl; - region_map.erase (i); - } else if (r == i->second) { - cerr << "Removed actual region " << i->second->name() << '/' << i->first << endl; - region_map.erase (i); - } - - i = tmp; - } + 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 @@ -332,54 +367,75 @@ RegionFactory::region_by_id (const PBD::ID& id) RegionMap::iterator i = region_map.find (id); if (i == region_map.end()) { - cerr << "ID " << id << " not found in region map\n"; return boost::shared_ptr(); } return i->second; } - + +boost::shared_ptr +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(); +} + +boost::shared_ptr +RegionFactory::region_by_name (const std::string& name) +{ + for (RegionMap::iterator i = region_map.begin(); i != region_map.end(); ++i) { + if (i->second->name() == name) { + return i->second; + } + } + return boost::shared_ptr(); +} + void RegionFactory::clear_map () { - region_list_connections.drop_connections (); - - { - Glib::Mutex::Lock lm (region_map_lock); - region_map.clear (); - } + region_list_connections.drop_connections (); + { + Glib::Mutex::Lock lm (region_map_lock); + region_map.clear (); + _compound_associations.clear (); + } } void RegionFactory::delete_all_regions () { - RegionMap copy; + RegionMap copy; - /* copy region list */ - { - Glib::Mutex::Lock lm (region_map_lock); - copy = region_map; - } + /* copy region list */ + { + Glib::Mutex::Lock lm (region_map_lock); + copy = region_map; + } - /* clear existing map */ - clear_map (); + /* clear existing map */ + clear_map (); - /* tell everyone to drop references */ - for (RegionMap::iterator i = copy.begin(); i != copy.end(); ++i) { - i->second->drop_references (); - } + /* tell everyone to drop references */ + for (RegionMap::iterator i = copy.begin(); i != copy.end(); ++i) { + i->second->drop_references (); + } - /* the copy should now hold the only references, which will - vanish as we leave this scope, thus calling all destructors. - */ + /* 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::Mutex::Lock lm (region_map_lock); - return region_map.size (); + Glib::Mutex::Lock lm (region_map_lock); + return region_map.size (); } void @@ -467,12 +523,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) { @@ -485,19 +552,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) { @@ -519,14 +603,33 @@ RegionFactory::new_region_name (string old) return old; } -void +void RegionFactory::get_regions_using_source (boost::shared_ptr s, std::set >& r) { - Glib::Mutex::Lock lm (region_map_lock); + Glib::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 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 (s)) { - r.insert (i->second); + 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 orig, boost::shared_ptr copy) +{ + Glib::Mutex::Lock lm (region_map_lock); + _compound_associations[copy] = orig; }