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_track_importer.h>
23 #include <ardour/audio_playlist_importer.h>
24 #include <ardour/audio_diskstream.h>
25 #include <ardour/session.h>
27 #include <pbd/failed_constructor.h>
28 #include <pbd/convert.h>
36 using namespace ARDOUR;
38 /*** AudioTrackImportHandler ***/
40 AudioTrackImportHandler::AudioTrackImportHandler (XMLTree const & source, Session & session, AudioPlaylistImportHandler & pl_handler) :
41 ElementImportHandler (source, session),
42 pl_handler (pl_handler)
44 XMLNode const * root = source.root();
45 XMLNode const * routes;
47 if (!(routes = root->child ("Routes"))) {
48 throw failed_constructor();
51 XMLNodeList const & route_list = routes->children();
52 for (XMLNodeList::const_iterator it = route_list.begin(); it != route_list.end(); ++it) {
53 const XMLProperty* type = (*it)->property("default-type");
54 if ( (!type || type->value() == "audio") && ((*it)->property ("diskstream") != 0 || (*it)->property ("diskstream-id") != 0)) {
56 elements.push_back (ElementPtr ( new AudioTrackImporter (source, session, *this, **it, pl_handler)));
57 } catch (failed_constructor err) {
65 AudioTrackImportHandler::get_info () const
67 return _("Audio Tracks");
71 /*** AudioTrackImporter ***/
73 AudioTrackImporter::AudioTrackImporter (XMLTree const & source,
75 AudioTrackImportHandler & track_handler,
77 AudioPlaylistImportHandler & pl_handler) :
78 ElementImporter (source, session),
79 track_handler (track_handler),
81 pl_handler (pl_handler)
85 if (!parse_route_xml ()) {
86 throw failed_constructor();
90 throw failed_constructor();
93 XMLNodeList const & controllables = node.children ("controllable");
94 for (XMLNodeList::const_iterator it = controllables.begin(); it != controllables.end(); ++it) {
95 parse_controllable (**it);
98 XMLNode * remote_control = xml_track.child ("remote_control");
99 if (remote_control && (prop = remote_control->property ("id"))) {
100 uint32_t control_id = session.ntracks() + session.nbusses() + 1;
101 prop->set_value (to_string (control_id, std::dec));
104 xml_track.remove_nodes_and_delete ("extra");
107 AudioTrackImporter::~AudioTrackImporter ()
113 AudioTrackImporter::parse_route_xml ()
117 // Remove order keys, new ones will be generated
118 xml_track.remove_property ("order-keys");
120 XMLPropertyList const & props = xml_track.properties();
121 for (XMLPropertyList::const_iterator it = props.begin(); it != props.end(); ++it) {
122 string prop = (*it)->name();
123 if (!prop.compare ("default-type") || !prop.compare ("flags") ||
124 !prop.compare ("active") || !prop.compare ("muted") ||
125 !prop.compare ("soloed") || !prop.compare ("phase-invert") ||
126 !prop.compare ("denormal-protection") || !prop.compare("mute-affects-pre-fader") ||
127 !prop.compare ("mute-affects-post-fader") || !prop.compare("mute-affects-control-outs") ||
128 !prop.compare ("mute-affects-main-outs") || !prop.compare("mode")) {
130 } else if (!prop.compare("diskstream-id")) {
131 old_ds_id = (*it)->value();
132 (*it)->set_value (new_ds_id.to_s());
135 std::cerr << string_compose (X_("AudioTrackImporter: did not recognise XML-property \"%1\""), prop) << endmsg;
140 error << X_("AudioTrackImporter: did not find necessary XML-property \"diskstream-id\"") << endmsg;
148 AudioTrackImporter::parse_io ()
151 bool name_ok = false;
154 if (!(io = xml_track.child ("IO"))) {
158 XMLPropertyList const & props = io->properties();
160 for (XMLPropertyList::const_iterator it = props.begin(); it != props.end(); ++it) {
161 string prop = (*it)->name();
162 if (!prop.compare ("gain") || !prop.compare ("iolimits")) {
164 } else if (!prop.compare("name")) {
165 name = (*it)->value();
167 } else if (!prop.compare("id")) {
169 (*it)->set_value (id.to_s());
171 } else if (!prop.compare("inputs")) {
172 // TODO Handle this properly!
173 /* Input and output ports are counted and added empty, so that no in/output connecting function fails. */
174 uint32_t num_inputs = std::count ((*it)->value().begin(), (*it)->value().end(), '{');
176 for (uint32_t i = 0; i < num_inputs; i++) { value += "{}"; }
177 (*it)->set_value (value);
178 } else if (!prop.compare("outputs")) {
179 // TODO See comments above
180 uint32_t num_outputs = std::count ((*it)->value().begin(), (*it)->value().end(), '{');
182 for (uint32_t i = 0; i < num_outputs; i++) { value += "{}"; }
183 (*it)->set_value (value);
185 std::cerr << string_compose (X_("AudioTrackImporter: did not recognise XML-property \"%1\""), prop) << endmsg;
190 error << X_("AudioTrackImporter: did not find necessary XML-property \"name\"") << endmsg;
195 error << X_("AudioTrackImporter: did not find necessary XML-property \"id\"") << endmsg;
199 XMLNodeList const & controllables = io->children ("controllable");
200 for (XMLNodeList::const_iterator it = controllables.begin(); it != controllables.end(); ++it) {
201 parse_controllable (**it);
204 XMLNodeList const & processors = io->children ("Processor");
205 for (XMLNodeList::const_iterator it = processors.begin(); it != processors.end(); ++it) {
206 parse_processor (**it);
209 XMLNodeList const & automations = io->children ("Automation");
210 for (XMLNodeList::const_iterator it = automations.begin(); it != automations.end(); ++it) {
211 parse_automation (**it);
218 AudioTrackImporter::get_info () const
225 AudioTrackImporter::_prepare_move ()
227 /* Copy dependent playlists */
229 pl_handler.playlists_by_diskstream (old_ds_id, playlists);
231 for (PlaylistList::iterator it = playlists.begin(); it != playlists.end(); ++it) {
232 if (!(*it)->prepare_move ()) {
236 (*it)->set_diskstream (new_ds_id);
241 while (session.route_by_name (name) || !track_handler.check_name (name)) {
242 std::pair<bool, string> rename_pair = Rename (_("A playlist with this name already exists, please rename it."), name);
243 if (!rename_pair.first) {
246 name = rename_pair.second;
248 xml_track.child ("IO")->property ("name")->set_value (name);
249 track_handler.add_name (name);
255 AudioTrackImporter::_cancel_move ()
257 track_handler.remove_name (name);
262 AudioTrackImporter::_move ()
266 boost::shared_ptr<XMLSharedNodeList> ds_node_list;
267 string xpath = "/Session/DiskStreams/AudioDiskstream[@id='" + old_ds_id.to_s() + "']";
268 ds_node_list = source.root()->find (xpath);
270 if (ds_node_list->size() != 1) {
271 error << string_compose (_("Error Importing Audio track %1"), name) << endmsg;
275 boost::shared_ptr<XMLNode> ds_node = ds_node_list->front();
276 ds_node->property ("id")->set_value (new_ds_id.to_s());
278 boost::shared_ptr<Diskstream> new_ds (new AudioDiskstream (session, *ds_node));
279 new_ds->set_name (name);
280 session.add_diskstream (new_ds);
282 /* Import playlists */
284 for (PlaylistList::const_iterator it = playlists.begin(); it != playlists.end(); ++it) {
290 XMLNode routes ("Routes");
291 routes.add_child_copy (xml_track);
292 session.load_routes (routes);
296 AudioTrackImporter::parse_processor (XMLNode & node)
298 XMLNode * automation = node.child ("Automation");
300 parse_automation (*automation);
307 AudioTrackImporter::parse_controllable (XMLNode & node)
311 if ((prop = node.property ("id"))) {
313 prop->set_value (new_id.to_s());
322 AudioTrackImporter::parse_automation (XMLNode & node)
325 XMLNodeList const & lists = node.children ("AutomationList");
326 for (XMLNodeList::const_iterator it = lists.begin(); it != lists.end(); ++it) {
329 if ((prop = (*it)->property ("id"))) {
331 prop->set_value (id.to_s());
334 if (!(*it)->name().compare ("events")) {
335 rate_convert_events (**it);
343 AudioTrackImporter::rate_convert_events (XMLNode & node)
345 if (node.children().empty()) {
349 XMLNode* content_node = node.children().front();
351 if (content_node->content().empty()) {
355 std::stringstream str (content_node->content());
356 std::ostringstream new_content;
373 new_content << rate_convert_samples (x) << ' ' << y;
377 error << X_("AudioTrackImporter: error in rate converting automation events") << endmsg;
381 content_node->set_content (new_content.str());