68827fb1613170996f081a838cbfb440b0e677fd
[ardour.git] / libs / ardour / audio_track_importer.cc
1 /*
2     Copyright (C) 2008 Paul Davis
3     Author: Sakari Bergen
4
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.
9
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.
14
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.
18
19 */
20
21 #include <ardour/audio_track_importer.h>
22
23 #include <ardour/session.h>
24
25 #include <pbd/id.h>
26 #include <pbd/failed_constructor.h>
27 #include <pbd/convert.h>
28
29 #include "i18n.h"
30
31 using namespace PBD;
32 using namespace ARDOUR;
33
34 /*** AudioTrackImportHandler ***/
35
36 AudioTrackImportHandler::AudioTrackImportHandler (XMLTree const & source, Session & session) :
37   ElementImportHandler (source, session)
38 {
39         XMLNode const * root = source.root();
40         XMLNode const * routes;
41         
42         if (!(routes = root->child ("Routes"))) {
43                 throw failed_constructor();
44         }
45         
46         XMLNodeList const & route_list = routes->children();
47         for (XMLNodeList::const_iterator it = route_list.begin(); it != route_list.end(); ++it) {
48                 const XMLProperty* type = (*it)->property("default-type");
49                 if ( !type || type->value() == "audio" ) {
50                         try {
51                                 elements.push_back (ElementPtr ( new AudioTrackImporter (source, session, *this, **it)));
52                         } catch (failed_constructor err) {
53                                 set_dirty();
54                         }
55                 }
56         }
57 }
58
59 string
60 AudioTrackImportHandler::get_info () const
61 {
62         return _("Audio Tracks");
63 }
64
65
66 /*** AudioTrackImporter ***/
67
68 AudioTrackImporter::AudioTrackImporter (XMLTree const & source, Session & session, AudioTrackImportHandler & handler, XMLNode const & node) :
69   ElementImporter (source, session),
70   xml_track (node)
71 {
72         XMLProperty * prop;
73
74         if (!parse_route_xml ()) {
75                 throw failed_constructor();
76         }
77         
78         if (!parse_io ()) {
79                 throw failed_constructor();
80         }
81         
82         XMLNodeList const & controllables = node.children ("controllable");
83         for (XMLNodeList::const_iterator it = controllables.begin(); it != controllables.end(); ++it) {
84                 parse_controllable (**it);
85         }
86         
87         XMLNode * remote_control = xml_track.child ("remote_control");
88         if (remote_control && (prop = remote_control->property ("id"))) {
89                 uint32_t control_id = session.ntracks() + session.nbusses() + 1;
90                 prop->set_value (to_string (control_id, std::dec));
91         }
92         
93         xml_track.remove_nodes_and_delete ("extra");
94 }
95
96 bool
97 AudioTrackImporter::parse_route_xml ()
98 {
99         XMLPropertyList const & props = xml_track.properties();
100
101         for (XMLPropertyList::const_iterator it = props.begin(); it != props.end(); ++it) {
102                 string prop = (*it)->name();
103                 if (!prop.compare ("default-type") || !prop.compare ("flags") ||
104                   !prop.compare ("active") || !prop.compare ("muted") ||
105                   !prop.compare ("soloed") || !prop.compare ("phase-invert") ||
106                   !prop.compare ("denormal-protection") || !prop.compare("mute-affects-pre-fader") ||
107                   !prop.compare ("mute-affects-post-fader") || !prop.compare("mute-affects-control-outs") ||
108                   !prop.compare ("mute-affects-main-outs") || !prop.compare("mode")) {
109                         // All ok
110                 } else if (!prop.compare("order-keys")) {
111                         // TODO
112                 } else if (!prop.compare("diskstream-id")) {
113                         // TODO
114                 } else {
115                         std::cerr << string_compose (X_("AudioTrackImporter: did not recognise XML-property \"%1\""), prop) << endmsg;
116                 }
117         }
118         
119         return true;
120 }
121
122 bool
123 AudioTrackImporter::parse_io ()
124 {
125         XMLNode * io;
126         bool name_ok = false;
127         bool id_ok = false;
128
129         if (!(io = xml_track.child ("IO"))) {
130                 return false;
131         }
132         
133         XMLPropertyList const & props = io->properties();
134
135         for (XMLPropertyList::const_iterator it = props.begin(); it != props.end(); ++it) {
136                 string prop = (*it)->name();
137                 if (!prop.compare ("gain") || !prop.compare ("iolimits")) {
138                         // All ok
139                 } else if (!prop.compare("name")) {
140                         name = prop;
141                         name_ok = true;
142                 } else if (!prop.compare("id")) {
143                         PBD::ID id;
144                         (*it)->set_value (id.to_s());
145                         id_ok = true;
146                         // TODO
147                 } else if (!prop.compare("inputs")) {
148                         // TODO
149                 } else if (!prop.compare("outputs")) {
150                         // TODO
151                 } else {
152                         std::cerr << string_compose (X_("AudioTrackImporter: did not recognise XML-property \"%1\""), prop) << endmsg;
153                 }
154         }
155         
156         if (!name_ok) {
157                 error << X_("AudioTrackImporter: did not find necessary XML-property \"name\"") << endmsg;
158                 return false;
159         }
160         
161         if (!id_ok) {
162                 error << X_("AudioTrackImporter: did not find necessary XML-property \"id\"") << endmsg;
163                 return false;
164         }
165         
166         XMLNodeList const & controllables = io->children ("controllable");
167         for (XMLNodeList::const_iterator it = controllables.begin(); it != controllables.end(); ++it) {
168                 parse_controllable (**it);
169         }
170         
171         XMLNodeList const & processors = io->children ("Processor");
172         for (XMLNodeList::const_iterator it = processors.begin(); it != processors.end(); ++it) {
173                 parse_processor (**it);
174         }
175         
176         XMLNodeList const & automations = io->children ("Automation");
177         for (XMLNodeList::const_iterator it = automations.begin(); it != automations.end(); ++it) {
178                 parse_automation (**it);
179         }
180         
181         return true;
182 }
183
184 string
185 AudioTrackImporter::get_info () const
186 {
187         // TODO
188         return name;
189 }
190
191 bool
192 AudioTrackImporter::prepare_move ()
193 {
194         // TODO
195         return false;
196 }
197
198 void
199 AudioTrackImporter::cancel_move ()
200 {
201         // TODO
202 }
203
204 void
205 AudioTrackImporter::move ()
206 {
207         // TODO
208 }
209
210 bool
211 AudioTrackImporter::parse_processor (XMLNode & node)
212 {
213         XMLNode * automation = node.child ("Automation");
214         if (automation) {
215                 parse_automation (*automation);
216         }
217         
218         return true;
219 }
220
221 bool
222 AudioTrackImporter::parse_controllable (XMLNode & node)
223 {
224         XMLProperty * prop;
225         
226         if ((prop = node.property ("id"))) {
227                 PBD::ID new_id;
228                 prop->set_value (new_id.to_s());
229         } else {
230                 return false;
231         }
232
233         return true;
234 }
235
236 bool
237 AudioTrackImporter::parse_automation (XMLNode & node)
238 {
239
240         XMLNodeList const & lists = node.children ("AutomationList");
241         for (XMLNodeList::const_iterator it = lists.begin(); it != lists.end(); ++it) {
242                 XMLProperty * prop;
243                 
244                 if ((prop = (*it)->property ("id"))) {
245                         PBD::ID id;
246                         prop->set_value (id.to_s());
247                 }
248                 
249                 // TODO rate convert events
250         }
251
252         return true;
253 }