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"
37 using namespace ARDOUR;
39 /*** AudioTrackImportHandler ***/
41 AudioTrackImportHandler::AudioTrackImportHandler (XMLTree const & source, Session & session, AudioPlaylistImportHandler & pl_handler) :
42 ElementImportHandler (source, session),
43 pl_handler (pl_handler)
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 const XMLProperty* 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 err) {
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");
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, std::dec));
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");
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
226 AudioTrackImporter::_prepare_move ()
228 /* Copy dependent playlists */
230 pl_handler.playlists_by_diskstream (old_ds_id, playlists);
232 for (PlaylistList::iterator it = playlists.begin(); it != playlists.end(); ++it) {
233 if (!(*it)->prepare_move ()) {
237 (*it)->set_diskstream (new_ds_id);
242 while (session.route_by_name (name) || !track_handler.check_name (name)) {
243 std::pair<bool, string> rename_pair = Rename (_("A playlist with this name already exists, please rename it."), name);
244 if (!rename_pair.first) {
247 name = rename_pair.second;
249 xml_track.child ("IO")->property ("name")->set_value (name);
250 track_handler.add_name (name);
256 AudioTrackImporter::_cancel_move ()
258 track_handler.remove_name (name);
263 AudioTrackImporter::_move ()
267 boost::shared_ptr<XMLSharedNodeList> ds_node_list;
268 string xpath = "/Session/DiskStreams/AudioDiskstream[@id='" + old_ds_id.to_s() + "']";
269 ds_node_list = source.root()->find (xpath);
271 if (ds_node_list->size() != 1) {
272 error << string_compose (_("Error Importing Audio track %1"), name) << endmsg;
276 boost::shared_ptr<XMLNode> ds_node = ds_node_list->front();
277 ds_node->property ("id")->set_value (new_ds_id.to_s());
279 boost::shared_ptr<Diskstream> new_ds (new AudioDiskstream (session, *ds_node));
280 new_ds->set_name (name);
281 session.add_diskstream (new_ds);
283 /* Import playlists */
285 for (PlaylistList::const_iterator it = playlists.begin(); it != playlists.end(); ++it) {
291 XMLNode routes ("Routes");
292 routes.add_child_copy (xml_track);
293 session.load_routes (routes, 3000);
297 AudioTrackImporter::parse_processor (XMLNode & node)
299 XMLNode * automation = node.child ("Automation");
301 parse_automation (*automation);
308 AudioTrackImporter::parse_controllable (XMLNode & node)
312 if ((prop = node.property ("id"))) {
314 prop->set_value (new_id.to_s());
323 AudioTrackImporter::parse_automation (XMLNode & node)
326 XMLNodeList const & lists = node.children ("AutomationList");
327 for (XMLNodeList::const_iterator it = lists.begin(); it != lists.end(); ++it) {
330 if ((prop = (*it)->property ("id"))) {
332 prop->set_value (id.to_s());
335 if (!(*it)->name().compare ("events")) {
336 rate_convert_events (**it);
344 AudioTrackImporter::rate_convert_events (XMLNode & node)
346 if (node.children().empty()) {
350 XMLNode* content_node = node.children().front();
352 if (content_node->content().empty()) {
356 std::stringstream str (content_node->content());
357 std::ostringstream new_content;
374 new_content << rate_convert_samples (x) << ' ' << y;
378 error << X_("AudioTrackImporter: error in rate converting automation events") << endmsg;
382 content_node->set_content (new_content.str());