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 "pbd/xml++.h"
22 #include "pbd/compose.h"
23 #include "ardour/debug.h"
24 #include "ardour/session_playlists.h"
25 #include "ardour/playlist.h"
26 #include "ardour/region.h"
27 #include "ardour/playlist_factory.h"
28 #include "ardour/session.h"
29 #include "ardour/source.h"
30 #include "ardour/track.h"
35 using namespace ARDOUR;
37 SessionPlaylists::~SessionPlaylists ()
39 DEBUG_TRACE (DEBUG::Destruction, "delete playlists\n");
41 for (List::iterator i = playlists.begin(); i != playlists.end(); ) {
42 SessionPlaylists::List::iterator tmp;
47 DEBUG_TRACE(DEBUG::Destruction, string_compose ("Dropping for used playlist %1 ; pre-ref = %2\n", (*i)->name(), (*i).use_count()));
48 boost::shared_ptr<Playlist> keeper (*i);
49 (*i)->drop_references ();
54 DEBUG_TRACE (DEBUG::Destruction, "delete unused playlists\n");
55 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ) {
61 DEBUG_TRACE(DEBUG::Destruction, string_compose ("Dropping for unused playlist %1 ; pre-ref = %2\n", (*i)->name(), (*i).use_count()));
62 boost::shared_ptr<Playlist> keeper (*i);
63 (*i)->drop_references ();
69 unused_playlists.clear ();
73 SessionPlaylists::add (boost::shared_ptr<Playlist> playlist)
75 Glib::Mutex::Lock lm (lock);
77 bool const existing = find (playlists.begin(), playlists.end(), playlist) != playlists.end();
80 playlists.insert (playlists.begin(), playlist);
81 playlist->InUse.connect_same_thread (*this, boost::bind (&SessionPlaylists::track, this, _1, boost::weak_ptr<Playlist>(playlist)));
82 playlist->DropReferences.connect_same_thread (
83 *this, boost::bind (&SessionPlaylists::remove_weak, this, boost::weak_ptr<Playlist> (playlist))
91 SessionPlaylists::remove_weak (boost::weak_ptr<Playlist> playlist)
93 boost::shared_ptr<Playlist> p = playlist.lock ();
100 SessionPlaylists::remove (boost::shared_ptr<Playlist> playlist)
102 Glib::Mutex::Lock lm (lock);
106 i = find (playlists.begin(), playlists.end(), playlist);
107 if (i != playlists.end()) {
111 i = find (unused_playlists.begin(), unused_playlists.end(), playlist);
112 if (i != unused_playlists.end()) {
113 unused_playlists.erase (i);
119 SessionPlaylists::track (bool inuse, boost::weak_ptr<Playlist> wpl)
121 boost::shared_ptr<Playlist> pl(wpl.lock());
130 /* its not supposed to be visible */
135 Glib::Mutex::Lock lm (lock);
139 unused_playlists.insert (pl);
141 if ((x = playlists.find (pl)) != playlists.end()) {
148 playlists.insert (pl);
150 if ((x = unused_playlists.find (pl)) != unused_playlists.end()) {
151 unused_playlists.erase (x);
158 SessionPlaylists::n_playlists () const
160 Glib::Mutex::Lock lm (lock);
161 return playlists.size();
164 boost::shared_ptr<Playlist>
165 SessionPlaylists::by_name (string name)
167 Glib::Mutex::Lock lm (lock);
169 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
170 if ((*i)->name() == name) {
175 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
176 if ((*i)->name() == name) {
181 return boost::shared_ptr<Playlist>();
184 boost::shared_ptr<Playlist>
185 SessionPlaylists::by_id (const PBD::ID& id)
187 Glib::Mutex::Lock lm (lock);
189 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
190 if ((*i)->id() == id) {
195 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
196 if ((*i)->id() == id) {
201 return boost::shared_ptr<Playlist>();
205 SessionPlaylists::unassigned (std::list<boost::shared_ptr<Playlist> > & list)
207 Glib::Mutex::Lock lm (lock);
209 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
210 if (!(*i)->get_orig_track_id().to_s().compare ("0")) {
215 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
216 if (!(*i)->get_orig_track_id().to_s().compare ("0")) {
223 SessionPlaylists::get (vector<boost::shared_ptr<Playlist> >& s) const
225 Glib::Mutex::Lock lm (lock);
227 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
231 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
237 SessionPlaylists::destroy_region (boost::shared_ptr<Region> r)
239 Glib::Mutex::Lock lm (lock);
241 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
242 (*i)->destroy_region (r);
245 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
246 (*i)->destroy_region (r);
252 SessionPlaylists::find_equivalent_playlist_regions (boost::shared_ptr<Region> region, vector<boost::shared_ptr<Region> >& result)
254 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i)
255 (*i)->get_region_list_equivalent_regions (region, result);
258 /** Return the number of playlists (not regions) that contain @a src */
260 SessionPlaylists::source_use_count (boost::shared_ptr<const Source> src) const
264 for (List::const_iterator p = playlists.begin(); p != playlists.end(); ++p) {
265 if ((*p)->uses_source (src)) {
274 SessionPlaylists::sync_all_regions_with_regions ()
276 Glib::Mutex::Lock lm (lock);
278 for (List::const_iterator p = playlists.begin(); p != playlists.end(); ++p) {
279 (*p)->sync_all_regions_with_regions ();
284 SessionPlaylists::update_after_tempo_map_change ()
286 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
287 (*i)->update_after_tempo_map_change ();
290 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
291 (*i)->update_after_tempo_map_change ();
296 SessionPlaylists::add_state (XMLNode* node, bool full_state)
298 XMLNode* child = node->add_child ("Playlists");
299 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
300 if (!(*i)->hidden()) {
302 child->add_child_nocopy ((*i)->get_state());
304 child->add_child_nocopy ((*i)->get_template());
309 child = node->add_child ("UnusedPlaylists");
310 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
311 if (!(*i)->hidden()) {
312 if (!(*i)->empty()) {
314 child->add_child_nocopy ((*i)->get_state());
316 child->add_child_nocopy ((*i)->get_template());
323 /** @return true for `stop cleanup', otherwise false */
325 SessionPlaylists::maybe_delete_unused (boost::function<int(boost::shared_ptr<Playlist>)> ask)
327 vector<boost::shared_ptr<Playlist> > playlists_tbd;
329 for (List::iterator x = unused_playlists.begin(); x != unused_playlists.end(); ++x) {
331 int status = ask (*x);
338 playlists_tbd.push_back (*x);
347 /* now delete any that were marked for deletion */
349 for (vector<boost::shared_ptr<Playlist> >::iterator x = playlists_tbd.begin(); x != playlists_tbd.end(); ++x) {
350 boost::shared_ptr<Playlist> keeper (*x);
351 (*x)->drop_references ();
354 playlists_tbd.clear ();
360 SessionPlaylists::load (Session& session, const XMLNode& node)
363 XMLNodeConstIterator niter;
364 boost::shared_ptr<Playlist> playlist;
366 nlist = node.children();
368 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
370 if ((playlist = XMLPlaylistFactory (session, **niter)) == 0) {
371 error << _("Session: cannot create Playlist from XML description.") << endmsg;
379 SessionPlaylists::load_unused (Session& session, const XMLNode& node)
382 XMLNodeConstIterator niter;
383 boost::shared_ptr<Playlist> playlist;
385 nlist = node.children();
387 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
389 if ((playlist = XMLPlaylistFactory (session, **niter)) == 0) {
390 error << _("Session: cannot create Playlist from XML description.") << endmsg;
394 // now manually untrack it
396 track (false, boost::weak_ptr<Playlist> (playlist));
402 boost::shared_ptr<Playlist>
403 SessionPlaylists::XMLPlaylistFactory (Session& session, const XMLNode& node)
406 return PlaylistFactory::create (session, node);
409 catch (failed_constructor& err) {
410 return boost::shared_ptr<Playlist>();
414 boost::shared_ptr<Crossfade>
415 SessionPlaylists::find_crossfade (const PBD::ID& id)
417 Glib::Mutex::Lock lm (lock);
419 boost::shared_ptr<Crossfade> c;
421 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
422 c = (*i)->find_crossfade (id);
428 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
429 c = (*i)->find_crossfade (id);
435 return boost::shared_ptr<Crossfade> ();
439 SessionPlaylists::region_use_count (boost::shared_ptr<Region> region) const
441 Glib::Mutex::Lock lm (lock);
444 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
445 cnt += (*i)->region_use_count (region);
448 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
449 cnt += (*i)->region_use_count (region);
455 /** @return list of Playlists that are associated with a track */
456 vector<boost::shared_ptr<Playlist> >
457 SessionPlaylists::playlists_for_track (boost::shared_ptr<Track> tr) const
459 vector<boost::shared_ptr<Playlist> > pl;
462 vector<boost::shared_ptr<Playlist> > pl_tr;
464 for (vector<boost::shared_ptr<Playlist> >::iterator i = pl.begin(); i != pl.end(); ++i) {
465 if (((*i)->get_orig_track_id() == tr->id()) || (tr->playlist()->id() == (*i)->id())) {
466 pl_tr.push_back (*i);