From 8018bed0ded65003279dfbdf446bae2697378488 Mon Sep 17 00:00:00 2001 From: Robin Gareus Date: Fri, 23 Sep 2016 20:34:28 +0200 Subject: [PATCH] add an option to exclude unused Audio Sources when archiving the session --- libs/ardour/ardour/session.h | 2 +- libs/ardour/ardour/session_playlists.h | 2 +- libs/ardour/session_playlists.cc | 5 +- libs/ardour/session_state.cc | 102 ++++++++++++++----------- 4 files changed, 65 insertions(+), 46 deletions(-) diff --git a/libs/ardour/ardour/session.h b/libs/ardour/ardour/session.h index af8f1952f1..80fe71fd1f 100644 --- a/libs/ardour/ardour/session.h +++ b/libs/ardour/ardour/session.h @@ -524,7 +524,7 @@ class LIBARDOUR_API Session : public PBD::StatefulDestructible, public PBD::Scop FLAC_24BIT }; - int archive_session (const std::string&, const std::string&, ArchiveEncode compress_audio = FLAC_16BIT, Progress* p = 0); + int archive_session (const std::string&, const std::string&, ArchiveEncode compress_audio = FLAC_16BIT, bool only_used_sources = false, Progress* p = 0); int restore_state (std::string snapshot_name); int save_template (std::string template_name, bool replace_existing = false); diff --git a/libs/ardour/ardour/session_playlists.h b/libs/ardour/ardour/session_playlists.h index cc6d301b31..273b12d040 100644 --- a/libs/ardour/ardour/session_playlists.h +++ b/libs/ardour/ardour/session_playlists.h @@ -54,7 +54,7 @@ public: uint32_t source_use_count (boost::shared_ptr src) const; uint32_t region_use_count (boost::shared_ptr region) const; template void foreach (T *obj, void (T::*func)(boost::shared_ptr)); - void foreach (boost::function)> functor); + void foreach (boost::function)> functor, bool incl_unused = true); void get (std::vector >&) const; void unassigned (std::list > & list); void destroy_region (boost::shared_ptr); diff --git a/libs/ardour/session_playlists.cc b/libs/ardour/session_playlists.cc index 95982b6158..5713e15458 100644 --- a/libs/ardour/session_playlists.cc +++ b/libs/ardour/session_playlists.cc @@ -505,7 +505,7 @@ SessionPlaylists::playlists_for_track (boost::shared_ptr tr) const } void -SessionPlaylists::foreach (boost::function)> functor) +SessionPlaylists::foreach (boost::function)> functor, bool incl_unused) { Glib::Threads::Mutex::Lock lm (lock); for (List::iterator i = playlists.begin(); i != playlists.end(); i++) { @@ -513,6 +513,9 @@ SessionPlaylists::foreach (boost::functionhidden()) { functor (*i); diff --git a/libs/ardour/session_state.cc b/libs/ardour/session_state.cc index d1e20030d6..14de04163c 100644 --- a/libs/ardour/session_state.cc +++ b/libs/ardour/session_state.cc @@ -4924,7 +4924,11 @@ static void set_progress (Progress* p, size_t n, size_t t) } int -Session::archive_session (const std::string& dest, const std::string& name, ArchiveEncode compress_audio, Progress* progress) +Session::archive_session (const std::string& dest, + const std::string& name, + ArchiveEncode compress_audio, + bool only_used_sources, + Progress* progress) { if (dest.empty () || name.empty ()) { return -1; @@ -5014,34 +5018,68 @@ Session::archive_session (const std::string& dest, const std::string& name, Arch std::map, std::string> orig_sources; - // TODO: build Source list once - // add option to only include *used* sources (see Session::cleanup_sources) + set > sources_used_by_this_snapshot; + if (only_used_sources) { + playlists->sync_all_regions_with_regions (); + playlists->foreach (boost::bind (merge_all_sources, _1, &sources_used_by_this_snapshot), false); + } - if (compress_audio != NO_ENCODE) { + // collect audio sources for this session, calc total size for encoding + // add option to only include *used* sources (see Session::cleanup_sources) + size_t total_size = 0; + { Glib::Threads::Mutex::Lock lm (source_lock); - size_t total_size = 0; - if (progress) { - for (SourceMap::const_iterator i = sources.begin(); i != sources.end(); ++i) { - boost::shared_ptr afs = boost::dynamic_pointer_cast (i->second); - if (!afs) { - continue; - } - if (afs->readable_length () == 0) { - continue; - } - total_size += afs->readable_length (); + for (SourceMap::const_iterator i = sources.begin(); i != sources.end(); ++i) { + boost::shared_ptr afs = boost::dynamic_pointer_cast (i->second); + if (!afs || afs->readable_length () == 0) { + continue; } + + if (only_used_sources) { + if (!afs->used()) { + continue; + } + if (sources_used_by_this_snapshot.find (afs) == sources_used_by_this_snapshot.end ()) { + continue; + } + } + + std::string from = afs->path(); + + if (compress_audio != NO_ENCODE) { + total_size += afs->readable_length (); + } else { + if (afs->within_session()) { + filemap[from] = make_new_media_path (from, name, name); + } else { + filemap[from] = make_new_media_path (from, name, name); + remove_dir_from_search_path (Glib::path_get_dirname (from), DataType::AUDIO); + } + } + } + } + + /* encode audio */ + if (compress_audio != NO_ENCODE) { + if (progress) { progress->set_progress (2); // set to "encoding" progress->set_progress (0); } + Glib::Threads::Mutex::Lock lm (source_lock); for (SourceMap::const_iterator i = sources.begin(); i != sources.end(); ++i) { boost::shared_ptr afs = boost::dynamic_pointer_cast (i->second); - if (!afs) { + if (!afs || afs->readable_length () == 0) { continue; } - if (afs->readable_length () == 0) { - continue; + + if (only_used_sources) { + if (!afs->used()) { + continue; + } + if (sources_used_by_this_snapshot.find (afs) == sources_used_by_this_snapshot.end ()) { + continue; + } } orig_sources[afs] = afs->path(); @@ -5053,6 +5091,7 @@ Session::archive_session (const std::string& dest, const std::string& name, Arch if (progress) { progress->descend ((float)afs->readable_length () / total_size); } + try { SndFileSource* ns = new SndFileSource (*this, *(afs.get()), new_path, compress_audio == FLAC_16BIT, progress); afs->replace_file (new_path); @@ -5060,6 +5099,7 @@ Session::archive_session (const std::string& dest, const std::string& name, Arch } catch (...) { cerr << "failed to encode " << afs->path() << " to " << new_path << "\n"; } + if (progress) { progress->ascend (); } @@ -5098,9 +5138,7 @@ Session::archive_session (const std::string& dest, const std::string& name, Arch #endif if (from.find (audiofile_dir_string) != string::npos) { - if (!compress_audio != NO_ENCODE) { - filemap[from] = make_new_media_path (from, name, name); - } + ; // handled above } else if (from.find (midifile_dir_string) != string::npos) { filemap[from] = make_new_media_path (from, name, name); } else if (from.find (videofile_dir_string) != string::npos) { @@ -5127,28 +5165,6 @@ Session::archive_session (const std::string& dest, const std::string& name, Arch } } - /* include external media */ - { - Glib::Threads::Mutex::Lock lm (source_lock); - for (SourceMap::const_iterator i = sources.begin(); i != sources.end(); ++i) { - boost::shared_ptr fs = boost::dynamic_pointer_cast (i->second); - if (!fs) { - continue; - } - if (fs->within_session()) { - continue; - } - - if (fs->type () != DataType::AUDIO) { - continue; - } - - std::string from = fs->path(); - filemap[from] = make_new_media_path (from, name, name); - remove_dir_from_search_path (Glib::path_get_dirname (from), DataType::AUDIO); - } - } - /* write session file */ _path = to_dir; g_mkdir_with_parents (externals_dir ().c_str (), 0755);