2 Copyright (C) 2009 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "ardour/debug.h"
22 #include "ardour/playlist.h"
23 #include "ardour/playlist_factory.h"
24 #include "ardour/session_playlists.h"
25 #include "ardour/track.h"
27 #include "pbd/compose.h"
28 #include "pbd/xml++.h"
32 using namespace ARDOUR;
34 SessionPlaylists::~SessionPlaylists ()
36 DEBUG_TRACE (DEBUG::Destruction, "delete playlists\n");
38 for (List::iterator i = playlists.begin(); i != playlists.end(); ) {
39 SessionPlaylists::List::iterator tmp;
44 DEBUG_TRACE(DEBUG::Destruction, string_compose ("Dropping for used playlist %1 ; pre-ref = %2\n", (*i)->name(), (*i).use_count()));
45 boost::shared_ptr<Playlist> keeper (*i);
46 (*i)->drop_references ();
51 DEBUG_TRACE (DEBUG::Destruction, "delete unused playlists\n");
52 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ) {
58 DEBUG_TRACE(DEBUG::Destruction, string_compose ("Dropping for unused playlist %1 ; pre-ref = %2\n", (*i)->name(), (*i).use_count()));
59 boost::shared_ptr<Playlist> keeper (*i);
60 (*i)->drop_references ();
66 unused_playlists.clear ();
70 SessionPlaylists::add (boost::shared_ptr<Playlist> playlist)
72 Glib::Threads::Mutex::Lock lm (lock);
74 bool const existing = find (playlists.begin(), playlists.end(), playlist) != playlists.end();
77 playlists.insert (playlists.begin(), playlist);
78 playlist->InUse.connect_same_thread (*this, boost::bind (&SessionPlaylists::track, this, _1, boost::weak_ptr<Playlist>(playlist)));
79 playlist->DropReferences.connect_same_thread (
80 *this, boost::bind (&SessionPlaylists::remove_weak, this, boost::weak_ptr<Playlist> (playlist))
88 SessionPlaylists::remove_weak (boost::weak_ptr<Playlist> playlist)
90 boost::shared_ptr<Playlist> p = playlist.lock ();
97 SessionPlaylists::remove (boost::shared_ptr<Playlist> playlist)
99 Glib::Threads::Mutex::Lock lm (lock);
103 i = find (playlists.begin(), playlists.end(), playlist);
104 if (i != playlists.end()) {
108 i = find (unused_playlists.begin(), unused_playlists.end(), playlist);
109 if (i != unused_playlists.end()) {
110 unused_playlists.erase (i);
116 SessionPlaylists::track (bool inuse, boost::weak_ptr<Playlist> wpl)
118 boost::shared_ptr<Playlist> pl(wpl.lock());
127 /* its not supposed to be visible */
132 Glib::Threads::Mutex::Lock lm (lock);
136 unused_playlists.insert (pl);
138 if ((x = playlists.find (pl)) != playlists.end()) {
145 playlists.insert (pl);
147 if ((x = unused_playlists.find (pl)) != unused_playlists.end()) {
148 unused_playlists.erase (x);
155 SessionPlaylists::n_playlists () const
157 Glib::Threads::Mutex::Lock lm (lock);
158 return playlists.size();
161 boost::shared_ptr<Playlist>
162 SessionPlaylists::by_name (string name)
164 Glib::Threads::Mutex::Lock lm (lock);
166 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
167 if ((*i)->name() == name) {
172 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
173 if ((*i)->name() == name) {
178 return boost::shared_ptr<Playlist>();
181 boost::shared_ptr<Playlist>
182 SessionPlaylists::by_id (const PBD::ID& id)
184 Glib::Threads::Mutex::Lock lm (lock);
186 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
187 if ((*i)->id() == id) {
192 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
193 if ((*i)->id() == id) {
198 return boost::shared_ptr<Playlist>();
202 SessionPlaylists::unassigned (std::list<boost::shared_ptr<Playlist> > & list)
204 Glib::Threads::Mutex::Lock lm (lock);
206 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
207 if (!(*i)->get_orig_track_id().to_s().compare ("0")) {
212 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
213 if (!(*i)->get_orig_track_id().to_s().compare ("0")) {
220 SessionPlaylists::get (vector<boost::shared_ptr<Playlist> >& s) const
222 Glib::Threads::Mutex::Lock lm (lock);
224 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
228 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
234 SessionPlaylists::destroy_region (boost::shared_ptr<Region> r)
236 Glib::Threads::Mutex::Lock lm (lock);
238 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
239 (*i)->destroy_region (r);
242 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
243 (*i)->destroy_region (r);
249 SessionPlaylists::find_equivalent_playlist_regions (boost::shared_ptr<Region> region, vector<boost::shared_ptr<Region> >& result)
251 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i)
252 (*i)->get_region_list_equivalent_regions (region, result);
255 /** Return the number of playlists (not regions) that contain @a src
256 * Important: this counts usage in both used and not-used playlists.
259 SessionPlaylists::source_use_count (boost::shared_ptr<const Source> src) const
263 for (List::const_iterator p = playlists.begin(); p != playlists.end(); ++p) {
264 if ((*p)->uses_source (src)) {
270 for (List::const_iterator p = unused_playlists.begin(); p != unused_playlists.end(); ++p) {
271 if ((*p)->uses_source (src)) {
281 SessionPlaylists::sync_all_regions_with_regions ()
283 Glib::Threads::Mutex::Lock lm (lock);
285 for (List::const_iterator p = playlists.begin(); p != playlists.end(); ++p) {
286 (*p)->sync_all_regions_with_regions ();
291 SessionPlaylists::update_after_tempo_map_change ()
293 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
294 (*i)->update_after_tempo_map_change ();
297 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
298 (*i)->update_after_tempo_map_change ();
303 SessionPlaylists::add_state (XMLNode* node, bool full_state)
305 XMLNode* child = node->add_child ("Playlists");
306 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
307 if (!(*i)->hidden()) {
309 child->add_child_nocopy ((*i)->get_state());
311 child->add_child_nocopy ((*i)->get_template());
316 child = node->add_child ("UnusedPlaylists");
317 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
318 if (!(*i)->hidden()) {
319 if (!(*i)->empty()) {
321 child->add_child_nocopy ((*i)->get_state());
323 child->add_child_nocopy ((*i)->get_template());
330 /** @return true for `stop cleanup', otherwise false */
332 SessionPlaylists::maybe_delete_unused (boost::function<int(boost::shared_ptr<Playlist>)> ask)
334 vector<boost::shared_ptr<Playlist> > playlists_tbd;
336 for (List::iterator x = unused_playlists.begin(); x != unused_playlists.end(); ++x) {
338 int status = ask (*x);
345 playlists_tbd.push_back (*x);
354 /* now delete any that were marked for deletion */
356 for (vector<boost::shared_ptr<Playlist> >::iterator x = playlists_tbd.begin(); x != playlists_tbd.end(); ++x) {
357 boost::shared_ptr<Playlist> keeper (*x);
358 (*x)->drop_references ();
361 playlists_tbd.clear ();
367 SessionPlaylists::load (Session& session, const XMLNode& node)
370 XMLNodeConstIterator niter;
371 boost::shared_ptr<Playlist> playlist;
373 nlist = node.children();
375 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
377 if ((playlist = XMLPlaylistFactory (session, **niter)) == 0) {
378 error << _("Session: cannot create Playlist from XML description.") << endmsg;
386 SessionPlaylists::load_unused (Session& session, const XMLNode& node)
389 XMLNodeConstIterator niter;
390 boost::shared_ptr<Playlist> playlist;
392 nlist = node.children();
394 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
396 if ((playlist = XMLPlaylistFactory (session, **niter)) == 0) {
397 error << _("Session: cannot create Playlist from XML description.") << endmsg;
401 // now manually untrack it
403 track (false, boost::weak_ptr<Playlist> (playlist));
409 boost::shared_ptr<Playlist>
410 SessionPlaylists::XMLPlaylistFactory (Session& session, const XMLNode& node)
413 return PlaylistFactory::create (session, node);
416 catch (failed_constructor& err) {
417 return boost::shared_ptr<Playlist>();
421 boost::shared_ptr<Crossfade>
422 SessionPlaylists::find_crossfade (const PBD::ID& id)
424 Glib::Threads::Mutex::Lock lm (lock);
426 boost::shared_ptr<Crossfade> c;
428 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
429 c = (*i)->find_crossfade (id);
435 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
436 c = (*i)->find_crossfade (id);
442 return boost::shared_ptr<Crossfade> ();
446 SessionPlaylists::region_use_count (boost::shared_ptr<Region> region) const
448 Glib::Threads::Mutex::Lock lm (lock);
451 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
452 cnt += (*i)->region_use_count (region);
455 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
456 cnt += (*i)->region_use_count (region);
462 /** @return list of Playlists that are associated with a track */
463 vector<boost::shared_ptr<Playlist> >
464 SessionPlaylists::playlists_for_track (boost::shared_ptr<Track> tr) const
466 vector<boost::shared_ptr<Playlist> > pl;
469 vector<boost::shared_ptr<Playlist> > pl_tr;
471 for (vector<boost::shared_ptr<Playlist> >::iterator i = pl.begin(); i != pl.end(); ++i) {
472 if (((*i)->get_orig_track_id() == tr->id()) || (tr->playlist()->id() == (*i)->id())) {
473 pl_tr.push_back (*i);