X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fardour%2Fregion_factory.cc;h=fba60fe0a01b5f356e36de2ec16c5856d03c3982;hb=9756b7569accf82d1c2de87cd632845acd107f9d;hp=cc41405551d2ba65aaa4265c6dba54594c29c862;hpb=539aa71d180d6b3d5c887707c356d3d00c0b37e8;p=ardour.git diff --git a/libs/ardour/region_factory.cc b/libs/ardour/region_factory.cc index cc41405551..fba60fe0a0 100644 --- a/libs/ardour/region_factory.cc +++ b/libs/ardour/region_factory.cc @@ -1,5 +1,5 @@ /* - Copyright (C) 2000-2006 Paul Davis + Copyright (C) 2000-2006 Paul Davis This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -17,199 +17,588 @@ */ -#include +#include -#include +#include "pbd/error.h" +#include "pbd/boost_debug.h" -#include -#include -#include -#include -#include -#include +#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 "i18n.h" using namespace ARDOUR; using namespace PBD; +using namespace std; -sigc::signal > RegionFactory::CheckNewRegion; +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; +RegionFactory::CompoundAssociations RegionFactory::_compound_associations; boost::shared_ptr -RegionFactory::create (boost::shared_ptr region, nframes_t start, - nframes_t length, std::string name, - layer_t layer, Region::Flag flags, bool announce) +RegionFactory::create (boost::shared_ptr region, bool announce) { - boost::shared_ptr other_a; - boost::shared_ptr other_m; + boost::shared_ptr ret; + boost::shared_ptr ar; + boost::shared_ptr mr; - if ((other_a = boost::dynamic_pointer_cast(region)) != 0) { - AudioRegion* ar = new AudioRegion (other_a, start, length, name, layer, flags); - boost::shared_ptr arp (ar); - boost::shared_ptr ret (boost::static_pointer_cast (arp)); - if (announce) { - CheckNewRegion (ret); + if ((ar = boost::dynamic_pointer_cast(region)) != 0) { + + ret = boost::shared_ptr (new AudioRegion (ar, 0)); + + } else if ((mr = boost::dynamic_pointer_cast(region)) != 0) { + + if (mr->session().config.get_midi_copy_is_fork()) { + ret = mr->clone (); + } else { + ret = boost::shared_ptr (new MidiRegion (mr, 0)); } - return ret; - } else if ((other_m = boost::dynamic_pointer_cast(region)) != 0) { - MidiRegion* ar = new MidiRegion (other_m, start, length, name, layer, flags); - boost::shared_ptr arp (ar); - boost::shared_ptr ret (boost::static_pointer_cast (arp)); + + } else { + fatal << _("programming error: RegionFactory::create() called with unknown Region type") + << endmsg; + /*NOTREACHED*/ + } + + if (ret) { + ret->set_name (new_region_name(ret->name())); + map_add (ret); + + /* pure copy constructor - no property list */ if (announce) { CheckNewRegion (ret); } - return 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, const PropertyList& plist, bool 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); + 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) -{ - boost::shared_ptr ar; - boost::shared_ptr mr; - - if ((ar = boost::dynamic_pointer_cast(region)) != 0) { - boost::shared_ptr ret (new AudioRegion (ar)); - /* pure copy constructor - no CheckNewRegion emitted */ - return ret; - } else if ((mr = boost::dynamic_pointer_cast(region)) != 0) { - boost::shared_ptr ret (new MidiRegion (mr)); - /* pure copy constructor - no CheckNewRegion emitted */ - return ret; +RegionFactory::create (boost::shared_ptr region, frameoffset_t offset, const PropertyList& plist, bool 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, offset)); + + } else if ((other_m = boost::dynamic_pointer_cast(region)) != 0) { + + ret = boost::shared_ptr (new MidiRegion (other_m, offset)); + } else { fatal << _("programming error: RegionFactory::create() called with unknown Region type") << endmsg; /*NOTREACHED*/ return boost::shared_ptr(); } + + if (ret) { + ret->apply_changes (plist); + 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 (const Playlist& playlist, boost::shared_ptr region) -{ - boost::shared_ptr xfade; - boost::shared_ptr mr; - - if ((xfade = boost::dynamic_pointer_cast(region)) != 0) { - boost::shared_ptr ret (new Crossfade (playlist, xfade)); - /* pure copy constructor - no CheckNewRegion emitted */ - return ret; +RegionFactory::create (boost::shared_ptr region, const SourceList& srcs, const PropertyList& plist, bool announce) +{ + boost::shared_ptr ret; + boost::shared_ptr other; + + /* used by AudioFilter when constructing a new region that is intended to have nearly + identical settings to an original, but using different sources. + */ + + 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()) + + ret = boost::shared_ptr (new AudioRegion (other, srcs)); + } else { - fatal << _("programming error: RegionFactory::create(Playlist,Region) called with unknown Region type") + fatal << _("programming error: RegionFactory::create() called with unknown Region type") << endmsg; /*NOTREACHED*/ - return boost::shared_ptr(); } + + if (ret) { + ret->apply_changes (plist); + 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 src, const PropertyList& plist, bool announce) +{ + SourceList srcs; + srcs.push_back (src); + return create (srcs, plist, announce); } boost::shared_ptr -RegionFactory::create (boost::shared_ptr region, nframes_t start, - nframes_t length, std::string name, - layer_t layer, Region::Flag flags, bool announce) +RegionFactory::create (const SourceList& srcs, const PropertyList& plist, bool announce) { - return create (boost::static_pointer_cast (region), start, length, name, layer, flags, announce); + boost::shared_ptr ret; + boost::shared_ptr as; + boost::shared_ptr ms; + + if ((as = boost::dynamic_pointer_cast(srcs[0])) != 0) { + + ret = boost::shared_ptr (new AudioRegion (srcs)); + + } else if ((ms = boost::dynamic_pointer_cast(srcs[0])) != 0) { + + ret = boost::shared_ptr (new MidiRegion (srcs)); + + } + + if (ret) { + ret->apply_changes (plist); + 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 (Session& session, XMLNode& node, bool yn) { - boost::shared_ptr r = session.XMLRegionFactory (node, yn); - CheckNewRegion (r); - return r; + return session.XMLRegionFactory (node, yn); } - -boost::shared_ptr -RegionFactory::create (SourceList& srcs, nframes_t start, nframes_t length, const string& name, layer_t layer, Region::Flag flags, bool announce) + +boost::shared_ptr +RegionFactory::create (SourceList& srcs, const XMLNode& node) { + boost::shared_ptr ret; + if (srcs.empty()) { - return boost::shared_ptr(); + return ret; } if (srcs[0]->type() == DataType::AUDIO) { - - AudioRegion* ar = new AudioRegion (srcs, start, length, name, layer, flags); - boost::shared_ptr arp (ar); - boost::shared_ptr ret (boost::static_pointer_cast (arp)); - if (announce) { - CheckNewRegion (ret); - } - return ret; + + ret = boost::shared_ptr (new AudioRegion (srcs)); } else if (srcs[0]->type() == DataType::MIDI) { - - MidiRegion* ar = new MidiRegion (srcs, start, length, name, layer, flags); - boost::shared_ptr mrp (ar); - boost::shared_ptr ret (boost::static_pointer_cast (mrp)); - if (announce) { + + ret = boost::shared_ptr (new MidiRegion (srcs)); + + } + + if (ret) { + if (ret->set_state (node, Stateful::loading_state_version)) { + ret.reset (); + } else { + map_add (ret); CheckNewRegion (ret); } - return 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 r) +{ + pair > p; + p.first = r->id(); + p.second = r; + { + Glib::Mutex::Lock lm (region_map_lock); + region_map.insert (p); } - return boost::shared_ptr (); -} + 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)) + ); -boost::shared_ptr -RegionFactory::create (const Playlist& playlist, const XMLNode& node) + update_region_name_map (r); +} + +void +RegionFactory::map_remove (boost::shared_ptr r) { - /* this is a constructor for "dependent" region types. - these objects require a playlist so that they can - look up the region instances that they depend upon. - */ + Glib::Mutex::Lock lm (region_map_lock); + RegionMap::iterator i = region_map.find (r->id()); - if (node.name() == Crossfade::node_name()) { - boost::shared_ptr ret (new Crossfade (playlist, node)); - return ret; + 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; - return boost::shared_ptr (); + 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 -RegionFactory::create (SourceList& srcs, const XMLNode& node) +boost::shared_ptr +RegionFactory::region_by_id (const PBD::ID& id) { - if (srcs.empty()) { + RegionMap::iterator i = region_map.find (id); + + if (i == region_map.end()) { return boost::shared_ptr(); } - if (srcs[0]->type() == DataType::AUDIO) { - boost::shared_ptr ret (new AudioRegion (srcs, node)); - CheckNewRegion (ret); - return ret; - } else if (srcs[0]->type() == DataType::MIDI) { - boost::shared_ptr ret (new MidiRegion (srcs, node)); - CheckNewRegion (ret); - return ret; + 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(); +} - 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(); } -boost::shared_ptr -RegionFactory::create (boost::shared_ptr src, nframes_t start, nframes_t length, const string& name, layer_t layer, Region::Flag flags, bool announce) +void +RegionFactory::clear_map () { - boost::shared_ptr as; - boost::shared_ptr ms; + 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; + + /* copy region list */ + { + Glib::Mutex::Lock lm (region_map_lock); + copy = region_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 (); + } + + /* 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 (); +} + +void +RegionFactory::update_region_name_map (boost::shared_ptr region) +{ + 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); + + /* note that if there is no number, we get zero from atoi, + which is just fine + */ + + Glib::Mutex::Lock lm (region_name_map_lock); + region_name_map[base] = atoi (number.c_str ()); + } +} + +void +RegionFactory::region_changed (PropertyChange const & what_changed, boost::weak_ptr w) +{ + boost::shared_ptr r = w.lock (); + if (!r) { + return; + } + + if (what_changed.contains (Properties::name)) { + update_region_name_map (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); - if ((as = boost::dynamic_pointer_cast(src)) != 0) { - boost::shared_ptr ret (new AudioRegion (as, start, length, name, layer, flags)); - if (announce) { - CheckNewRegion (ret); } - return ret; - } else if ((ms = boost::dynamic_pointer_cast(src)) != 0) { - boost::shared_ptr ret (new MidiRegion (ms, start, length, name, layer, flags)); - if (announce) { - CheckNewRegion (ret); + + { + Glib::Mutex::Lock lm (region_name_map_lock); + + map::iterator x; + + result = subbase; + + if ((x = region_name_map.find (subbase)) == region_name_map.end()) { + result += ".1"; + region_name_map[subbase] = 1; + } else { + x->second++; + snprintf (buf, sizeof (buf), ".%d", x->second); + + result += buf; + } } - return ret; } - return boost::shared_ptr(); + 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) { + + /* 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)) { + + 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; + + for (i = regions.begin(); i != regions.end(); ++i) { + if (i->second->name() == sbuf) { + break; + } + } + + if (i == regions.end()) { + break; + } + } + + if (number != (UINT_MAX-1)) { + return buf; + } + + error << string_compose (_("cannot create new name for region \"%1\""), old) << endmsg; + return old; +} + +void +RegionFactory::get_regions_using_source (boost::shared_ptr s, std::set >& r) +{ + 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 (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; }