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/disk_reader.h"
25 #include "ardour/session.h"
27 #include "pbd/controllable.h"
28 #include "pbd/failed_constructor.h"
29 #include "pbd/string_convert.h"
38 using namespace ARDOUR;
40 /*** AudioTrackImportHandler ***/
42 AudioTrackImportHandler::AudioTrackImportHandler (XMLTree const & source, Session & session, AudioPlaylistImportHandler & pl_handler) :
43 ElementImportHandler (source, session)
45 XMLNode const * root = source.root();
46 XMLNode const * routes;
48 if (!(routes = root->child ("Routes"))) {
49 throw failed_constructor();
52 XMLNodeList const & route_list = routes->children();
53 for (XMLNodeList::const_iterator it = route_list.begin(); it != route_list.end(); ++it) {
54 XMLProperty const * type = (*it)->property("default-type");
55 if ( (!type || type->value() == "audio") && ((*it)->property ("diskstream") != 0 || (*it)->property ("diskstream-id") != 0)) {
57 elements.push_back (ElementPtr ( new AudioTrackImporter (source, session, *this, **it, pl_handler)));
58 } catch (failed_constructor const&) {
66 AudioTrackImportHandler::get_info () const
68 return _("Audio Tracks");
72 /*** AudioTrackImporter ***/
74 AudioTrackImporter::AudioTrackImporter (XMLTree const & source,
76 AudioTrackImportHandler & track_handler,
78 AudioPlaylistImportHandler & pl_handler) :
79 ElementImporter (source, session),
80 track_handler (track_handler),
82 pl_handler (pl_handler)
86 if (!parse_route_xml ()) {
87 throw failed_constructor();
91 throw failed_constructor();
94 XMLNodeList const & controllables = node.children (Controllable::xml_node_name);
95 for (XMLNodeList::const_iterator it = controllables.begin(); it != controllables.end(); ++it) {
96 parse_controllable (**it);
99 XMLNode * remote_control = xml_track.child ("RemoteControl");
100 if (remote_control && (prop = remote_control->property ("id"))) {
101 uint32_t control_id = session.ntracks() + session.nbusses() + 1;
102 prop->set_value (to_string (control_id));
105 xml_track.remove_nodes_and_delete ("Extra");
108 AudioTrackImporter::~AudioTrackImporter ()
114 AudioTrackImporter::parse_route_xml ()
118 // Remove order keys, new ones will be generated
119 xml_track.remove_property ("order-keys");
121 XMLPropertyList const & props = xml_track.properties();
122 for (XMLPropertyList::const_iterator it = props.begin(); it != props.end(); ++it) {
123 string prop = (*it)->name();
124 if (!prop.compare ("default-type") || !prop.compare ("flags") ||
125 !prop.compare ("active") || !prop.compare ("muted") ||
126 !prop.compare ("soloed") || !prop.compare ("phase-invert") ||
127 !prop.compare ("denormal-protection") || !prop.compare("mute-affects-pre-fader") ||
128 !prop.compare ("mute-affects-post-fader") || !prop.compare("mute-affects-control-outs") ||
129 !prop.compare ("mute-affects-main-outs") || !prop.compare("mode")) {
131 } else if (!prop.compare("diskstream-id")) {
132 old_ds_id = (*it)->value();
133 (*it)->set_value (new_ds_id.to_s());
136 std::cerr << string_compose (X_("AudioTrackImporter: did not recognise XML-property \"%1\""), prop) << endmsg;
141 error << X_("AudioTrackImporter: did not find necessary XML-property \"diskstream-id\"") << endmsg;
149 AudioTrackImporter::parse_io ()
152 bool name_ok = false;
155 if (!(io = xml_track.child ("IO"))) {
159 XMLPropertyList const & props = io->properties();
161 for (XMLPropertyList::const_iterator it = props.begin(); it != props.end(); ++it) {
162 string prop = (*it)->name();
163 if (!prop.compare ("gain") || !prop.compare ("iolimits")) {
165 } else if (!prop.compare("name")) {
166 name = (*it)->value();
168 } else if (!prop.compare("id")) {
170 (*it)->set_value (id.to_s());
172 } else if (!prop.compare("inputs")) {
173 // TODO Handle this properly!
174 /* Input and output ports are counted and added empty, so that no in/output connecting function fails. */
175 uint32_t num_inputs = std::count ((*it)->value().begin(), (*it)->value().end(), '{');
177 for (uint32_t i = 0; i < num_inputs; i++) { value += "{}"; }
178 (*it)->set_value (value);
179 } else if (!prop.compare("outputs")) {
180 // TODO See comments above
181 uint32_t num_outputs = std::count ((*it)->value().begin(), (*it)->value().end(), '{');
183 for (uint32_t i = 0; i < num_outputs; i++) { value += "{}"; }
184 (*it)->set_value (value);
186 std::cerr << string_compose (X_("AudioTrackImporter: did not recognise XML-property \"%1\""), prop) << endmsg;
191 error << X_("AudioTrackImporter: did not find necessary XML-property \"name\"") << endmsg;
196 error << X_("AudioTrackImporter: did not find necessary XML-property \"id\"") << endmsg;
200 XMLNodeList const & controllables = io->children (Controllable::xml_node_name);
201 for (XMLNodeList::const_iterator it = controllables.begin(); it != controllables.end(); ++it) {
202 parse_controllable (**it);
205 XMLNodeList const & processors = io->children ("Processor");
206 for (XMLNodeList::const_iterator it = processors.begin(); it != processors.end(); ++it) {
207 parse_processor (**it);
210 XMLNodeList const & automations = io->children ("Automation");
211 for (XMLNodeList::const_iterator it = automations.begin(); it != automations.end(); ++it) {
212 parse_automation (**it);
219 AudioTrackImporter::get_info () const
225 /** @return true if everything is ok */
227 AudioTrackImporter::_prepare_move ()
229 /* Copy dependent playlists */
231 pl_handler.playlists_by_diskstream (old_ds_id, playlists);
233 for (PlaylistList::iterator it = playlists.begin(); it != playlists.end(); ++it) {
234 if (!(*it)->prepare_move ()) {
238 (*it)->set_diskstream (new_ds_id);
243 while (session.route_by_name (name) || !track_handler.check_name (name)) {
244 std::pair<bool, string> rename_pair = *Rename (_("A playlist with this name already exists, please rename it."), name);
245 if (!rename_pair.first) {
248 name = rename_pair.second;
251 XMLNode* c = xml_track.child ("IO");
253 error << _("badly-formed XML in imported track") << endmsg;
257 XMLProperty * p = c->property ("name");
259 error << _("badly-formed XML in imported track") << endmsg;
265 track_handler.add_name (name);
271 AudioTrackImporter::_cancel_move ()
273 track_handler.remove_name (name);
278 AudioTrackImporter::_move ()
284 boost::shared_ptr<XMLSharedNodeList> ds_node_list;
285 string xpath = "/Session/DiskStreams/AudioDiskstream[@id='" + old_ds_id.to_s() + "']";
286 ds_node_list = source.find (xpath);
288 if (ds_node_list->size() != 1) {
289 error << string_compose (_("Error Importing Audio track %1"), name) << endmsg;
293 boost::shared_ptr<XMLNode> ds_node = ds_node_list->front();
294 XMLProperty * p = ds_node->property (X_("id"));
296 p->set_value (new_ds_id.to_s());
298 boost::shared_ptr<DiskReader> new_ds (new DiskReader (session, *ds_node));
299 new_ds->set_name (name);
300 new_ds->do_refill_with_alloc ();
301 new_ds->set_block_size (session.get_block_size ());
303 /* Import playlists */
305 for (PlaylistList::const_iterator it = playlists.begin(); it != playlists.end(); ++it) {
311 XMLNode routes ("Routes");
312 routes.add_child_copy (xml_track);
313 session.load_routes (routes, 3000);
318 AudioTrackImporter::parse_processor (XMLNode & node)
320 XMLNode * automation = node.child ("Automation");
322 parse_automation (*automation);
329 AudioTrackImporter::parse_controllable (XMLNode & node)
333 if ((prop = node.property ("id"))) {
335 prop->set_value (new_id.to_s());
344 AudioTrackImporter::parse_automation (XMLNode & node)
347 XMLNodeList const & lists = node.children ("AutomationList");
348 for (XMLNodeList::const_iterator it = lists.begin(); it != lists.end(); ++it) {
351 if ((prop = (*it)->property ("id"))) {
353 prop->set_value (id.to_s());
356 if (!(*it)->name().compare ("events")) {
357 rate_convert_events (**it);
365 AudioTrackImporter::rate_convert_events (XMLNode & node)
367 if (node.children().empty()) {
371 XMLNode* content_node = node.children().front();
373 if (content_node->content().empty()) {
377 std::stringstream str (content_node->content());
378 std::ostringstream new_content;
395 new_content << rate_convert_samples (x) << ' ' << y;
399 error << X_("AudioTrackImporter: error in rate converting automation events") << endmsg;
403 content_node->set_content (new_content.str());