From e3dd8cc3ee481924997a1650e3a130b2579be209 Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Tue, 30 Mar 2010 23:40:28 +0000 Subject: Move region naming stuff from Session into RegionFactory, cleaning up some vestiges of when Session had a list of regions. git-svn-id: svn://localhost/ardour2/branches/3.0@6812 d708f5d6-7413-0410-9779-e7cbd77b26cf --- libs/ardour/ardour/region_factory.h | 18 +++- libs/ardour/ardour/session.h | 20 ---- libs/ardour/audio_diskstream.cc | 2 +- libs/ardour/audio_region_importer.cc | 4 +- libs/ardour/filter.cc | 2 +- libs/ardour/midi_diskstream.cc | 2 +- libs/ardour/playlist.cc | 18 ++-- libs/ardour/region.cc | 2 +- libs/ardour/region_factory.cc | 198 ++++++++++++++++++++++++++++------- libs/ardour/session.cc | 188 --------------------------------- libs/ardour/session_state.cc | 1 - libs/ardour/strip_silence.cc | 2 +- 12 files changed, 190 insertions(+), 267 deletions(-) (limited to 'libs/ardour') diff --git a/libs/ardour/ardour/region_factory.h b/libs/ardour/ardour/region_factory.h index 2bff4dd104..ca19f52fec 100644 --- a/libs/ardour/ardour/region_factory.h +++ b/libs/ardour/ardour/region_factory.h @@ -83,11 +83,27 @@ class RegionFactory { static const RegionMap& regions() { return region_map; } static uint32_t nregions (); + static int region_name (std::string &, std::string, bool new_level = false); + static std::string new_region_name (std::string); + private: + + static boost::shared_ptr create (boost::shared_ptr, frameoffset_t offset, + bool offset_relative, + const PBD::PropertyList&, bool announce = true); + + static void region_changed (PBD::PropertyChange const &, boost::weak_ptr); + static Glib::StaticMutex region_map_lock; + static RegionMap region_map; static void map_add (boost::shared_ptr); - + + static Glib::StaticMutex region_name_map_lock; + + static std::map region_name_map; + static void update_region_name_map (boost::shared_ptr); + static PBD::ScopedConnectionList region_list_connections; }; diff --git a/libs/ardour/ardour/session.h b/libs/ardour/ardour/session.h index 36687d5cf1..4af432eeb5 100644 --- a/libs/ardour/ardour/session.h +++ b/libs/ardour/ardour/session.h @@ -261,10 +261,6 @@ class Session : public PBD::StatefulDestructible, public PBD::ScopedConnectionLi void maybe_write_autosave (); - /* Proxy signal for region hidden changes */ - - PBD::Signal1 > RegionHiddenChange; - /* Emitted when all i/o connections are complete */ PBD::Signal0 IOConnectionsComplete; @@ -314,8 +310,6 @@ class Session : public PBD::StatefulDestructible, public PBD::ScopedConnectionLi int wipe (); - int remove_region_from_region_list (boost::shared_ptr); - nframes_t get_maximum_extent () const; nframes_t current_end_frame() const { return end_location->start(); } nframes_t current_start_frame() const { return start_location->start(); } @@ -493,13 +487,6 @@ class Session : public PBD::StatefulDestructible, public PBD::ScopedConnectionLi boost::shared_ptr find_whole_file_parent (boost::shared_ptr) const; - void add_regions (std::vector >&); - - PBD::Signal1 >&> RegionsAdded; - PBD::Signal1 > RegionRemoved; - - int region_name (std::string& result, std::string base = std::string(""), bool newlevel = false); - std::string new_region_name (std::string); std::string path_from_region_name (DataType type, std::string name, std::string identifier); boost::shared_ptr XMLRegionFactory (const XMLNode&, bool full); @@ -1241,15 +1228,8 @@ class Session : public PBD::StatefulDestructible, public PBD::ScopedConnectionLi /* REGION MANAGEMENT */ - std::map region_name_map; - void update_region_name_map (boost::shared_ptr); - mutable Glib::Mutex region_lock; - void add_region (boost::shared_ptr); - void region_changed (const PBD::PropertyChange&, boost::weak_ptr); - void remove_region (boost::weak_ptr); - int load_regions (const XMLNode& node); void route_group_changed (); diff --git a/libs/ardour/audio_diskstream.cc b/libs/ardour/audio_diskstream.cc index c7194d7495..d832a39e28 100644 --- a/libs/ardour/audio_diskstream.cc +++ b/libs/ardour/audio_diskstream.cc @@ -1473,7 +1473,7 @@ AudioDiskstream::transport_stopped (struct tm& when, time_t twhen, bool abort_ca string region_name; - _session.region_name (region_name, whole_file_region_name, false); + RegionFactory::region_name (region_name, whole_file_region_name, false); // cerr << _name << ": based on ci of " << (*ci)->start << " for " << (*ci)->frames << " add region " << region_name << endl; diff --git a/libs/ardour/audio_region_importer.cc b/libs/ardour/audio_region_importer.cc index ebe4ae5270..7be155b5f5 100644 --- a/libs/ardour/audio_region_importer.cc +++ b/libs/ardour/audio_region_importer.cc @@ -176,8 +176,6 @@ AudioRegionImporter::_move () if (broken()) { return; } - - session.add_regions (region); } bool @@ -209,7 +207,7 @@ AudioRegionImporter::parse_xml_region () } else if (!prop.compare("name")) { // rename region if necessary name = (*it)->value(); - name = session.new_region_name (name); + name = RegionFactory::new_region_name (name); (*it)->set_value (name); name_ok = true; } else { diff --git a/libs/ardour/filter.cc b/libs/ardour/filter.cc index 2ff93316f3..44e7f11cbd 100644 --- a/libs/ardour/filter.cc +++ b/libs/ardour/filter.cc @@ -116,7 +116,7 @@ Filter::finish (boost::shared_ptr region, SourceList& nsrcs, string regi /* create a new region */ if (region_name.empty()) { - region_name = session.new_region_name (region->name()); + region_name = RegionFactory::new_region_name (region->name()); } results.clear (); diff --git a/libs/ardour/midi_diskstream.cc b/libs/ardour/midi_diskstream.cc index e0d7abe880..dbee0de562 100644 --- a/libs/ardour/midi_diskstream.cc +++ b/libs/ardour/midi_diskstream.cc @@ -989,7 +989,7 @@ MidiDiskstream::transport_stopped (struct tm& /*when*/, time_t /*twhen*/, bool a string region_name; - _session.region_name (region_name, _write_source->name(), false); + RegionFactory::region_name (region_name, _write_source->name(), false); // cerr << _name << ": based on ci of " << (*ci)->start << " for " << (*ci)->frames << " add a region\n"; diff --git a/libs/ardour/playlist.cc b/libs/ardour/playlist.cc index 5968f67f21..35b0aedcd8 100644 --- a/libs/ardour/playlist.cc +++ b/libs/ardour/playlist.cc @@ -264,7 +264,7 @@ Playlist::Playlist (boost::shared_ptr other, framepos_t start, f break; } - _session.region_name (new_name, region->name(), false); + RegionFactory::region_name (new_name, region->name(), false); PropertyList plist; @@ -693,7 +693,7 @@ Playlist::add_region (boost::shared_ptr region, framepos_t position, flo if (floor (times) != times) { length = (framecnt_t) floor (region->length() * (times - floor (times))); string name; - _session.region_name (name, region->name(), false); + RegionFactory::region_name (name, region->name(), false); { PropertyList plist; @@ -963,7 +963,7 @@ Playlist::partition_internal (framepos_t start, framepos_t end, bool cutting, Re if (!cutting) { /* "middle" ++++++ */ - _session.region_name (new_name, current->name(), false); + RegionFactory::region_name (new_name, current->name(), false); PropertyList plist; @@ -982,7 +982,7 @@ Playlist::partition_internal (framepos_t start, framepos_t end, bool cutting, Re /* "end" ====== */ - _session.region_name (new_name, current->name(), false); + RegionFactory::region_name (new_name, current->name(), false); PropertyList plist; @@ -1020,7 +1020,7 @@ Playlist::partition_internal (framepos_t start, framepos_t end, bool cutting, Re /* end +++++ */ - _session.region_name (new_name, current->name(), false); + RegionFactory::region_name (new_name, current->name(), false); PropertyList plist; @@ -1063,7 +1063,7 @@ Playlist::partition_internal (framepos_t start, framepos_t end, bool cutting, Re if (!cutting) { /* front **** */ - _session.region_name (new_name, current->name(), false); + RegionFactory::region_name (new_name, current->name(), false); PropertyList plist; @@ -1267,7 +1267,7 @@ Playlist::duplicate (boost::shared_ptr region, framepos_t position, floa if (floor (times) != times) { framecnt_t length = (framecnt_t) floor (region->length() * (times - floor (times))); string name; - _session.region_name (name, region->name(), false); + RegionFactory::region_name (name, region->name(), false); { PropertyList plist; @@ -1368,7 +1368,7 @@ Playlist::_split_region (boost::shared_ptr region, framepos_t playlist_p before = playlist_position - region->position(); after = region->length() - before; - _session.region_name (before_name, region->name(), false); + RegionFactory::region_name (before_name, region->name(), false); { PropertyList plist; @@ -1381,7 +1381,7 @@ Playlist::_split_region (boost::shared_ptr region, framepos_t playlist_p left = RegionFactory::create (region, plist); } - _session.region_name (after_name, region->name(), false); + RegionFactory::region_name (after_name, region->name(), false); { PropertyList plist; diff --git a/libs/ardour/region.cc b/libs/ardour/region.cc index acd4e1b9dc..bb9338d98d 100644 --- a/libs/ardour/region.cc +++ b/libs/ardour/region.cc @@ -486,7 +486,7 @@ Region::first_edit () if (_first_edit != EditChangesNothing && pl) { - _name = _session.new_region_name (_name); + _name = RegionFactory::new_region_name (_name); _first_edit = EditChangesNothing; send_change (Properties::name); diff --git a/libs/ardour/region_factory.cc b/libs/ardour/region_factory.cc index 8a24bc954c..5a4b129710 100644 --- a/libs/ardour/region_factory.cc +++ b/libs/ardour/region_factory.cc @@ -17,6 +17,9 @@ */ +#define __STDC_FORMAT_MACROS +#include + #include "pbd/error.h" #include "pbd/boost_debug.h" @@ -39,6 +42,8 @@ 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; boost::shared_ptr RegionFactory::create (boost::shared_ptr region) @@ -80,45 +85,17 @@ RegionFactory::create (boost::shared_ptr region) boost::shared_ptr 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) { - - AudioRegion* ar = new AudioRegion (other_a, offset, true); - boost_debug_shared_ptr_mark_interesting (ar, "Region"); - - boost::shared_ptr arp (ar); - ret = boost::static_pointer_cast (arp); - - } else if ((other_m = boost::dynamic_pointer_cast(region)) != 0) { - - MidiRegion* mr = new MidiRegion (other_m, offset, true); - boost::shared_ptr mrp (mr); - ret = boost::static_pointer_cast (mrp); - - } else { - fatal << _("programming error: RegionFactory::create() called with unknown Region type") - << endmsg; - /*NOTREACHED*/ - return boost::shared_ptr(); - } - - if (ret) { - ret->set_properties (plist); - map_add (ret); - - if (announce) { - CheckNewRegion (ret); - } - } - - return ret; + 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 +RegionFactory::create (boost::shared_ptr region, frameoffset_t offset, bool offset_relative, const PropertyList& plist, bool announce) { boost::shared_ptr ret; boost::shared_ptr other_a; @@ -126,7 +103,7 @@ RegionFactory::create (boost::shared_ptr region, const PropertyList& pli if ((other_a = boost::dynamic_pointer_cast(region)) != 0) { - AudioRegion* ar = new AudioRegion (other_a, 0, false); + AudioRegion* ar = new AudioRegion (other_a, offset, offset_relative); boost_debug_shared_ptr_mark_interesting (ar, "Region"); boost::shared_ptr arp (ar); @@ -134,7 +111,7 @@ RegionFactory::create (boost::shared_ptr region, const PropertyList& pli } else if ((other_m = boost::dynamic_pointer_cast(region)) != 0) { - MidiRegion* mr = new MidiRegion (other_m, 0, false); + MidiRegion* mr = new MidiRegion (other_m, offset, offset_relative); boost::shared_ptr mrp (mr); ret = boost::static_pointer_cast (mrp); @@ -157,9 +134,6 @@ RegionFactory::create (boost::shared_ptr region, const PropertyList& pli return ret; } - - - boost::shared_ptr RegionFactory::create (boost::shared_ptr region, const SourceList& srcs, const PropertyList& plist, bool announce) { @@ -301,6 +275,13 @@ RegionFactory::map_add (boost::shared_ptr 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)) + ); + + update_region_name_map (r); } void @@ -369,3 +350,140 @@ 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); + + } + + { + 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 0; +} + +string +RegionFactory::new_region_name (string old) +{ + string::size_type last_period; + uint32_t number; + string::size_type len = old.length() + 64; + 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 { + + number = atoi (old.substr (last_period+1).c_str()); + + } + + 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); + 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; +} diff --git a/libs/ardour/session.cc b/libs/ardour/session.cc index 5a34176afe..d7a71ff0c5 100644 --- a/libs/ardour/session.cc +++ b/libs/ardour/session.cc @@ -2436,194 +2436,6 @@ Session::diskstream_by_id (const PBD::ID& id) /* Region management */ -string -Session::new_region_name (string old) -{ - string::size_type last_period; - uint32_t number; - string::size_type len = old.length() + 64; - 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 { - - number = atoi (old.substr (last_period+1).c_str()); - - } - - while (number < (UINT_MAX-1)) { - - const RegionFactory::RegionMap& regions (RegionFactory::regions()); - RegionFactory::RegionMap::const_iterator i; - string sbuf; - - number++; - - snprintf (buf, len, "%s%" PRIu32, old.substr (0, last_period + 1).c_str(), number); - 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; -} - -int -Session::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); - - } - - { - Glib::Mutex::Lock lm (region_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 0; -} - -void -Session::add_region (boost::shared_ptr region) -{ - vector > v; - v.push_back (region); - add_regions (v); -} - -void -Session::add_regions (vector >& new_regions) -{ - /* mark dirty because something has changed - */ - - set_dirty (); - - for (vector >::iterator ii = new_regions.begin(); ii != new_regions.end(); ++ii) { - - boost::shared_ptr region = *ii; - assert (region); - - region->PropertyChanged.connect_same_thread (*this, boost::bind (&Session::region_changed, this, _1, boost::weak_ptr(region))); - update_region_name_map (region); - } - - if (!new_regions.empty()) { - RegionsAdded (new_regions); /* EMIT SIGNAL */ - } -} - -void -Session::update_region_name_map (boost::shared_ptr region) -{ - string::size_type last_period = region->name().find_last_of ('.'); - - if (last_period != string::npos && last_period < region->name().length() - 1) { - - string base = region->name().substr (0, last_period); - string number = region->name().substr (last_period+1); - map::iterator x; - - /* note that if there is no number, we get zero from atoi, - which is just fine - */ - - region_name_map[base] = atoi (number); - } -} - -void -Session::region_changed (const PropertyChange& what_changed, boost::weak_ptr weak_region) -{ - boost::shared_ptr region (weak_region.lock ()); - - if (!region) { - return; - } - - if (what_changed.contains (Properties::hidden)) { - /* relay hidden changes */ - RegionHiddenChange (region); - } - - if (what_changed.contains (Properties::name)) { - update_region_name_map (region); - } -} - -void -Session::remove_region (boost::weak_ptr weak_region) -{ - boost::shared_ptr region (weak_region.lock ()); - - if (!region) { - return; - } - - RegionFactory::map_remove (region); - set_dirty(); - - RegionRemoved(region); /* EMIT SIGNAL */ -} - boost::shared_ptr Session::find_whole_file_parent (boost::shared_ptr child) const { diff --git a/libs/ardour/session_state.cc b/libs/ardour/session_state.cc index 4c0dcfd72d..c231f7eb1d 100644 --- a/libs/ardour/session_state.cc +++ b/libs/ardour/session_state.cc @@ -270,7 +270,6 @@ Session::first_stage_init (string fullpath, string snapshot_name) /* These are all static "per-class" signals */ - RegionFactory::CheckNewRegion.connect_same_thread (*this, boost::bind (&Session::add_region, this, _1)); SourceFactory::SourceCreated.connect_same_thread (*this, boost::bind (&Session::add_source, this, _1)); PlaylistFactory::PlaylistCreated.connect_same_thread (*this, boost::bind (&Session::add_playlist, this, _1, _2)); Processor::ProcessorCreated.connect_same_thread (*this, boost::bind (&Session::add_processor, this, _1)); diff --git a/libs/ardour/strip_silence.cc b/libs/ardour/strip_silence.cc index 21e1ad5450..3952311aa2 100644 --- a/libs/ardour/strip_silence.cc +++ b/libs/ardour/strip_silence.cc @@ -113,7 +113,7 @@ StripSilence::run (boost::shared_ptr r) copy = boost::dynamic_pointer_cast (RegionFactory::create (region, start, plist)); - copy->set_name (session.new_region_name (region->name ())); + copy->set_name (RegionFactory::new_region_name (region->name ())); std::cerr << "New silent delineated region called " << copy->name() << " @ " << copy->start() << " length = " << copy->length() << " pos = " << -- cgit v1.2.3