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/controllable.h"
28 #include "pbd/convert.h"
29 #include "pbd/failed_constructor.h"
38 using namespace ARDOUR;
40 /*** AudioTrackImportHandler ***/
42 AudioTrackImportHandler::AudioTrackImportHandler (XMLTree const & source, Session & session, AudioPlaylistImportHandler & pl_handler) :
43 ElementImportHandler (source, session),
44 pl_handler (pl_handler)
46 XMLNode const * root = source.root();
47 XMLNode const * routes;
49 if (!(routes = root->child ("Routes"))) {
50 throw failed_constructor();
53 XMLNodeList const & route_list = routes->children();
54 for (XMLNodeList::const_iterator it = route_list.begin(); it != route_list.end(); ++it) {
55 const XMLProperty* type = (*it)->property("default-type");
56 if ( (!type || type->value() == "audio") && ((*it)->property ("diskstream") != 0 || (*it)->property ("diskstream-id") != 0)) {
58 elements.push_back (ElementPtr ( new AudioTrackImporter (source, session, *this, **it, pl_handler)));
59 } catch (failed_constructor err) {
67 AudioTrackImportHandler::get_info () const
69 return _("Audio Tracks");
73 /*** AudioTrackImporter ***/
75 AudioTrackImporter::AudioTrackImporter (XMLTree const & source,
77 AudioTrackImportHandler & track_handler,
79 AudioPlaylistImportHandler & pl_handler) :
80 ElementImporter (source, session),
81 track_handler (track_handler),
83 pl_handler (pl_handler)
87 if (!parse_route_xml ()) {
88 throw failed_constructor();
92 throw failed_constructor();
95 XMLNodeList const & controllables = node.children (Controllable::xml_node_name);
96 for (XMLNodeList::const_iterator it = controllables.begin(); it != controllables.end(); ++it) {
97 parse_controllable (**it);
100 XMLNode * remote_control = xml_track.child ("RemoteControl");
101 if (remote_control && (prop = remote_control->property ("id"))) {
102 uint32_t control_id = session.ntracks() + session.nbusses() + 1;
103 prop->set_value (to_string (control_id, std::dec));
106 xml_track.remove_nodes_and_delete ("Extra");
109 AudioTrackImporter::~AudioTrackImporter ()
115 AudioTrackImporter::parse_route_xml ()
119 // Remove order keys, new ones will be generated
120 xml_track.remove_property ("order-keys");
122 XMLPropertyList const & props = xml_track.properties();
123 for (XMLPropertyList::const_iterator it = props.begin(); it != props.end(); ++it) {
124 string prop = (*it)->name();
125 if (!prop.compare ("default-type") || !prop.compare ("flags") ||
126 !prop.compare ("active") || !prop.compare ("muted") ||
127 !prop.compare ("soloed") || !prop.compare ("phase-invert") ||
128 !prop.compare ("denormal-protection") || !prop.compare("mute-affects-pre-fader") ||
129 !prop.compare ("mute-affects-post-fader") || !prop.compare("mute-affects-control-outs") ||
130 !prop.compare ("mute-affects-main-outs") || !prop.compare("mode")) {
132 } else if (!prop.compare("diskstream-id")) {
133 old_ds_id = (*it)->value();
134 (*it)->set_value (new_ds_id.to_s());
137 std::cerr << string_compose (X_("AudioTrackImporter: did not recognise XML-property \"%1\""), prop) << endmsg;
142 error << X_("AudioTrackImporter: did not find necessary XML-property \"diskstream-id\"") << endmsg;
150 AudioTrackImporter::parse_io ()
153 bool name_ok = false;
156 if (!(io = xml_track.child ("IO"))) {
160 XMLPropertyList const & props = io->properties();
162 for (XMLPropertyList::const_iterator it = props.begin(); it != props.end(); ++it) {
163 string prop = (*it)->name();
164 if (!prop.compare ("gain") || !prop.compare ("iolimits")) {
166 } else if (!prop.compare("name")) {
167 name = (*it)->value();
169 } else if (!prop.compare("id")) {
171 (*it)->set_value (id.to_s());
173 } else if (!prop.compare("inputs")) {
174 // TODO Handle this properly!
175 /* Input and output ports are counted and added empty, so that no in/output connecting function fails. */
176 uint32_t num_inputs = std::count ((*it)->value().begin(), (*it)->value().end(), '{');
178 for (uint32_t i = 0; i < num_inputs; i++) { value += "{}"; }
179 (*it)->set_value (value);
180 } else if (!prop.compare("outputs")) {
181 // TODO See comments above
182 uint32_t num_outputs = std::count ((*it)->value().begin(), (*it)->value().end(), '{');
184 for (uint32_t i = 0; i < num_outputs; i++) { value += "{}"; }
185 (*it)->set_value (value);
187 std::cerr << string_compose (X_("AudioTrackImporter: did not recognise XML-property \"%1\""), prop) << endmsg;
192 error << X_("AudioTrackImporter: did not find necessary XML-property \"name\"") << endmsg;
197 error << X_("AudioTrackImporter: did not find necessary XML-property \"id\"") << endmsg;
201 XMLNodeList const & controllables = io->children (Controllable::xml_node_name);
202 for (XMLNodeList::const_iterator it = controllables.begin(); it != controllables.end(); ++it) {
203 parse_controllable (**it);
206 XMLNodeList const & processors = io->children ("Processor");
207 for (XMLNodeList::const_iterator it = processors.begin(); it != processors.end(); ++it) {
208 parse_processor (**it);
211 XMLNodeList const & automations = io->children ("Automation");
212 for (XMLNodeList::const_iterator it = automations.begin(); it != automations.end(); ++it) {
213 parse_automation (**it);
220 AudioTrackImporter::get_info () const
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;
250 xml_track.child ("IO")->property ("name")->set_value (name);
251 track_handler.add_name (name);
257 AudioTrackImporter::_cancel_move ()
259 track_handler.remove_name (name);
264 AudioTrackImporter::_move ()
268 boost::shared_ptr<XMLSharedNodeList> ds_node_list;
269 string xpath = "/Session/DiskStreams/AudioDiskstream[@id='" + old_ds_id.to_s() + "']";
270 ds_node_list = source.find (xpath);
272 if (ds_node_list->size() != 1) {
273 error << string_compose (_("Error Importing Audio track %1"), name) << endmsg;
277 boost::shared_ptr<XMLNode> ds_node = ds_node_list->front();
278 XMLProperty* p = ds_node->property (X_("id"));
280 p->set_value (new_ds_id.to_s());
282 boost::shared_ptr<Diskstream> new_ds (new AudioDiskstream (session, *ds_node));
283 new_ds->set_name (name);
284 new_ds->do_refill_with_alloc ();
285 new_ds->set_block_size (session.get_block_size ());
287 /* Import playlists */
289 for (PlaylistList::const_iterator it = playlists.begin(); it != playlists.end(); ++it) {
295 XMLNode routes ("Routes");
296 routes.add_child_copy (xml_track);
297 session.load_routes (routes, 3000);
301 AudioTrackImporter::parse_processor (XMLNode & node)
303 XMLNode * automation = node.child ("Automation");
305 parse_automation (*automation);
312 AudioTrackImporter::parse_controllable (XMLNode & node)
316 if ((prop = node.property ("id"))) {
318 prop->set_value (new_id.to_s());
327 AudioTrackImporter::parse_automation (XMLNode & node)
330 XMLNodeList const & lists = node.children ("AutomationList");
331 for (XMLNodeList::const_iterator it = lists.begin(); it != lists.end(); ++it) {
334 if ((prop = (*it)->property ("id"))) {
336 prop->set_value (id.to_s());
339 if (!(*it)->name().compare ("events")) {
340 rate_convert_events (**it);
348 AudioTrackImporter::rate_convert_events (XMLNode & node)
350 if (node.children().empty()) {
354 XMLNode* content_node = node.children().front();
356 if (content_node->content().empty()) {
360 std::stringstream str (content_node->content());
361 std::ostringstream new_content;
378 new_content << rate_convert_samples (x) << ' ' << y;
382 error << X_("AudioTrackImporter: error in rate converting automation events") << endmsg;
386 content_node->set_content (new_content.str());