2 Copyright (C) 2008 Paul Davis
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <ardour/audio_playlist_importer.h>
25 #include <pbd/failed_constructor.h>
26 #include <pbd/compose.h>
27 #include <pbd/error.h>
29 #include <ardour/audio_region_importer.h>
30 #include <ardour/session.h>
31 #include <ardour/playlist.h>
32 #include <ardour/playlist_factory.h>
35 using namespace ARDOUR;
38 AudioPlaylistImportHandler::AudioPlaylistImportHandler (XMLTree const & source, Session & session, AudioRegionImportHandler & region_handler, const char * nodename) :
39 ElementImportHandler (source, session),
40 region_handler (region_handler)
42 XMLNode const * root = source.root();
43 XMLNode const * playlists;
45 if (!(playlists = root->child (nodename))) {
46 throw failed_constructor();
49 XMLNodeList const & pl_children = playlists->children();
50 for (XMLNodeList::const_iterator it = pl_children.begin(); it != pl_children.end(); ++it) {
51 const XMLProperty* type = (*it)->property("type");
52 if ( !type || type->value() == "audio" ) {
54 elements.push_back (ElementPtr ( new AudioPlaylistImporter (source, session, *this, **it)));
55 } catch (failed_constructor err) {
63 AudioPlaylistImportHandler::get_info () const
65 return _("Audio Playlists");
69 AudioPlaylistImportHandler::get_regions (XMLNode const & node, ElementList & list)
71 region_handler.create_regions_from_children (node, list);
75 AudioPlaylistImportHandler::update_region_id (XMLProperty* id_prop)
77 PBD::ID old_id (id_prop->value());
78 PBD::ID new_id (region_handler.get_new_id (old_id));
79 id_prop->set_value (new_id.to_s());
82 /*** AudioPlaylistImporter ***/
83 AudioPlaylistImporter::AudioPlaylistImporter (XMLTree const & source, Session & session, AudioPlaylistImportHandler & handler, XMLNode const & node) :
84 ElementImporter (source, session),
91 // Populate region list
92 ElementImportHandler::ElementList elements;
93 handler.get_regions (node, elements);
94 for (ElementImportHandler::ElementList::iterator it = elements.begin(); it != elements.end(); ++it) {
95 regions.push_back (boost::dynamic_pointer_cast<AudioRegionImporter> (*it));
99 XMLPropertyList const & props = xml_playlist.properties();
100 for (XMLPropertyList::const_iterator it = props.begin(); it != props.end(); ++it) {
101 string prop = (*it)->name();
102 if (!prop.compare("type") || !prop.compare("frozen")) {
104 } else if (!prop.compare("name")) {
105 name = (*it)->value();
106 } else if (!prop.compare("orig-diskstream-id")) {
109 std::cerr << string_compose (X_("AudioPlaylistImporter did not recognise XML-property \"%1\""), prop) << endmsg;
114 error << string_compose (X_("AudioPlaylistImporter (%1): did not find XML-property \"orig_diskstream_id\" which is mandatory"), name) << endmsg;
115 throw failed_constructor();
120 AudioPlaylistImporter::get_info () const
122 XMLNodeList children = xml_playlist.children();
123 unsigned int regions = 0;
124 std::ostringstream oss;
126 for (XMLNodeIterator it = children.begin(); it != children.end(); it++) {
127 if ((*it)->name() == "Region") {
132 oss << regions << " ";
144 AudioPlaylistImporter::prepare_move ()
147 while (session.playlist_by_name (name) || !handler.check_name (name)) {
148 std::pair<bool, string> rename_pair = Rename (_("A playlist with this name already exists, please rename it."), name);
149 if (!rename_pair.first) {
152 name = rename_pair.second;
154 xml_playlist.property ("name")->set_value (name);
155 handler.add_name (name);
162 AudioPlaylistImporter::cancel_move ()
164 handler.remove_name (name);
169 AudioPlaylistImporter::move ()
171 boost::shared_ptr<Playlist> playlist;
173 // Update diskstream id
174 xml_playlist.property ("orig-diskstream-id")->set_value (diskstream_id.to_s());
176 // Update region XML in playlist and prepare sources
177 xml_playlist.remove_nodes("Region");
178 for (RegionList::iterator it = regions.begin(); it != regions.end(); ++it) {
179 xml_playlist.add_child_copy ((*it)->get_xml());
180 (*it)->add_sources_to_session();
181 if ((*it)->broken()) {
184 return; // TODO clean up?
188 // Update region ids in crossfades
189 XMLNodeList crossfades = xml_playlist.children("Crossfade");
190 for (XMLNodeIterator it = crossfades.begin(); it != crossfades.end(); ++it) {
191 XMLProperty* in = (*it)->property("in");
192 XMLProperty* out = (*it)->property("out");
194 error << string_compose (X_("AudioPlaylistImporter (%1): did not find the \"in\" or \"out\" property from a crossfade"), name) << endmsg;
197 handler.update_region_id (in);
198 handler.update_region_id (out);
200 // rate convert length and position
201 XMLProperty* length = (*it)->property("length");
203 length->set_value (rate_convert_samples (length->value()));
206 XMLProperty* position = (*it)->property("position");
208 position->set_value (rate_convert_samples (position->value()));
213 playlist = PlaylistFactory::create (session, xml_playlist, false, true);
217 AudioPlaylistImporter::set_diskstream (PBD::ID const & id)