summaryrefslogtreecommitdiff
path: root/libs/ardour/audio_playlist_importer.cc
diff options
context:
space:
mode:
authorSakari Bergen <sakari.bergen@beatwaves.net>2008-09-26 08:29:30 +0000
committerSakari Bergen <sakari.bergen@beatwaves.net>2008-09-26 08:29:30 +0000
commit572fa80aa713e723f63e1e1822db614307eea6af (patch)
tree6d8e8ed27d6192790f54482f14e93dda73d3a485 /libs/ardour/audio_playlist_importer.cc
parent10d57b266cbec7054399f20f8e8c76cdff7e1592 (diff)
Add Import from session -functionality
git-svn-id: svn://localhost/ardour2/branches/3.0@3805 d708f5d6-7413-0410-9779-e7cbd77b26cf
Diffstat (limited to 'libs/ardour/audio_playlist_importer.cc')
-rw-r--r--libs/ardour/audio_playlist_importer.cc221
1 files changed, 221 insertions, 0 deletions
diff --git a/libs/ardour/audio_playlist_importer.cc b/libs/ardour/audio_playlist_importer.cc
new file mode 100644
index 0000000000..d6a2b2f9fb
--- /dev/null
+++ b/libs/ardour/audio_playlist_importer.cc
@@ -0,0 +1,221 @@
+/*
+ Copyright (C) 2008 Paul Davis
+ Author: Sakari Bergen
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+
+*/
+
+#include <ardour/audio_playlist_importer.h>
+
+#include <sstream>
+
+#include <pbd/failed_constructor.h>
+#include <pbd/compose.h>
+#include <pbd/error.h>
+
+#include <ardour/audio_region_importer.h>
+#include <ardour/session.h>
+#include <ardour/playlist.h>
+#include <ardour/playlist_factory.h>
+
+using namespace PBD;
+using namespace ARDOUR;
+
+/**** Handler ***/
+AudioPlaylistImportHandler::AudioPlaylistImportHandler (XMLTree const & source, Session & session, AudioRegionImportHandler & region_handler, const char * nodename) :
+ ElementImportHandler (source, session),
+ region_handler (region_handler)
+{
+ XMLNode const * root = source.root();
+ XMLNode const * playlists;
+
+ if (!(playlists = root->child (nodename))) {
+ throw failed_constructor();
+ }
+
+ XMLNodeList const & pl_children = playlists->children();
+ for (XMLNodeList::const_iterator it = pl_children.begin(); it != pl_children.end(); ++it) {
+ const XMLProperty* type = (*it)->property("type");
+ if ( !type || type->value() == "audio" ) {
+ try {
+ elements.push_back (ElementPtr ( new AudioPlaylistImporter (source, session, *this, **it)));
+ } catch (failed_constructor err) {
+ set_dirty();
+ }
+ }
+ }
+}
+
+string
+AudioPlaylistImportHandler::get_info () const
+{
+ return _("Audio Playlists");
+}
+
+void
+AudioPlaylistImportHandler::get_regions (XMLNode const & node, ElementList & list)
+{
+ region_handler.create_regions_from_children (node, list);
+}
+
+void
+AudioPlaylistImportHandler::update_region_id (XMLProperty* id_prop)
+{
+ PBD::ID old_id (id_prop->value());
+ PBD::ID new_id (region_handler.get_new_id (old_id));
+ id_prop->set_value (new_id.to_s());
+}
+
+/*** AudioPlaylistImporter ***/
+AudioPlaylistImporter::AudioPlaylistImporter (XMLTree const & source, Session & session, AudioPlaylistImportHandler & handler, XMLNode const & node) :
+ ElementImporter (source, session),
+ handler (handler),
+ xml_playlist (node),
+ diskstream_id ("0")
+{
+ bool ds_ok = false;
+
+ // Populate region list
+ ElementImportHandler::ElementList elements;
+ handler.get_regions (node, elements);
+ for (ElementImportHandler::ElementList::iterator it = elements.begin(); it != elements.end(); ++it) {
+ regions.push_back (boost::dynamic_pointer_cast<AudioRegionImporter> (*it));
+ }
+
+ // Parse XML
+ XMLPropertyList const & props = xml_playlist.properties();
+ for (XMLPropertyList::const_iterator it = props.begin(); it != props.end(); ++it) {
+ string prop = (*it)->name();
+ if (!prop.compare("type") || !prop.compare("frozen")) {
+ // All ok
+ } else if (!prop.compare("name")) {
+ name = (*it)->value();
+ } else if (!prop.compare("orig_diskstream_id")) {
+ ds_ok = true;
+ } else {
+ std::cerr << string_compose (X_("AudioPlaylistImporter did not recognise XML-property \"%1\""), prop) << endmsg;
+ }
+ }
+
+ if (!ds_ok) {
+ error << string_compose (X_("AudioPlaylistImporter (%1): did not find XML-property \"orig_diskstream_id\" which is mandatory"), name) << endmsg;
+ throw failed_constructor();
+ }
+}
+
+string
+AudioPlaylistImporter::get_info () const
+{
+ XMLNodeList children = xml_playlist.children();
+ unsigned int regions = 0;
+ std::ostringstream oss;
+
+ for (XMLNodeIterator it = children.begin(); it != children.end(); it++) {
+ if ((*it)->name() == "Region") {
+ ++regions;
+ }
+ }
+
+ oss << regions << " ";
+
+ if (regions == 1) {
+ oss << _("region");
+ } else {
+ oss << _("regions");
+ }
+
+ return oss.str();
+}
+
+bool
+AudioPlaylistImporter::prepare_move ()
+{
+ // Rename
+ while (session.playlist_by_name (name) || !handler.check_name (name)) {
+ std::pair<bool, string> rename_pair = Rename (_("A playlist with this name already exists, please rename it."), name);
+ if (!rename_pair.first) {
+ return false;
+ }
+ name = rename_pair.second;
+ }
+ xml_playlist.property ("name")->set_value (name);
+ handler.add_name (name);
+
+ queued = true;
+ return true;
+}
+
+void
+AudioPlaylistImporter::cancel_move ()
+{
+ handler.remove_name (name);
+ queued = false;
+}
+
+void
+AudioPlaylistImporter::move ()
+{
+ boost::shared_ptr<Playlist> playlist;
+
+ // Update diskstream id
+ xml_playlist.property ("orig_diskstream_id")->set_value (diskstream_id.to_s());
+
+ // Update region XML in playlist and prepare sources
+ xml_playlist.remove_nodes("Region");
+ for (RegionList::iterator it = regions.begin(); it != regions.end(); ++it) {
+ xml_playlist.add_child_copy ((*it)->get_xml());
+ (*it)->add_sources_to_session();
+ if ((*it)->broken()) {
+ handler.set_dirty();
+ set_broken();
+ return; // TODO clean up?
+ }
+ }
+
+ // Update region ids in crossfades
+ XMLNodeList crossfades = xml_playlist.children("Crossfade");
+ for (XMLNodeIterator it = crossfades.begin(); it != crossfades.end(); ++it) {
+ XMLProperty* in = (*it)->property("in");
+ XMLProperty* out = (*it)->property("out");
+ if (!in || !out) {
+ error << string_compose (X_("AudioPlaylistImporter (%1): did not find the \"in\" or \"out\" property from a crossfade"), name) << endmsg;
+ }
+
+ handler.update_region_id (in);
+ handler.update_region_id (out);
+
+ // rate convert length and position
+ XMLProperty* length = (*it)->property("length");
+ if (length) {
+ length->set_value (rate_convert_samples (length->value()));
+ }
+
+ XMLProperty* position = (*it)->property("position");
+ if (position) {
+ position->set_value (rate_convert_samples (position->value()));
+ }
+ }
+
+ // Create playlist
+ playlist = PlaylistFactory::create (session, xml_playlist, false, true);
+}
+
+void
+AudioPlaylistImporter::set_diskstream (PBD::ID const & id)
+{
+ diskstream_id = id;
+}
+