More tinkering with State<>. Use some StateDiffCommands instead of
[ardour.git] / libs / ardour / audio_region_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_region_importer.h"
22
23 #include <sstream>
24
25 #include "pbd/failed_constructor.h"
26 #include "pbd/compose.h"
27 #include "pbd/error.h"
28
29 #include "ardour/session.h"
30 #include "ardour/region.h"
31 #include "ardour/source_factory.h"
32 #include "ardour/region_factory.h"
33 #include "ardour/session_directory.h"
34
35 #include "i18n.h"
36
37 using namespace std;
38 using namespace PBD;
39 using namespace ARDOUR;
40
41 /**** Handler ***/
42 AudioRegionImportHandler::AudioRegionImportHandler (XMLTree const & source, Session & session) :
43   ElementImportHandler (source, session)
44 {
45         XMLNode const * root = source.root();
46         XMLNode const * regions;
47
48         if (!(regions = root->child (X_("Regions")))) {
49                 throw failed_constructor();
50         }
51
52         create_regions_from_children (*regions, elements);
53 }
54
55 void
56 AudioRegionImportHandler::create_regions_from_children (XMLNode const & node, ElementList & list)
57 {
58         XMLNodeList const & children = node.children();
59         for (XMLNodeList::const_iterator it = children.begin(); it != children.end(); ++it) {
60                 XMLProperty const * type = (*it)->property("type");
61                 if (!(*it)->name().compare ("Region") && (!type || type->value() == "audio") ) {
62                         try {
63                                 list.push_back (ElementPtr ( new AudioRegionImporter (source, session, *this, **it)));
64                         } catch (failed_constructor err) {
65                                 set_dirty();
66                         }
67                 }
68         }
69 }
70
71 string
72 AudioRegionImportHandler::get_info () const
73 {
74         return _("Audio Regions");
75 }
76
77 bool
78 AudioRegionImportHandler::check_source (string const & filename) const
79 {
80         return (sources.find (filename) != sources.end());
81 }
82
83 void
84 AudioRegionImportHandler::add_source (string const & filename, boost::shared_ptr<Source> const & source)
85 {
86         sources.insert (SourcePair (filename, source));
87 }
88
89 boost::shared_ptr<Source> const &
90 AudioRegionImportHandler::get_source (string const & filename) const
91 {
92         return (sources.find (filename))->second;
93 }
94
95 void
96 AudioRegionImportHandler::register_id (PBD::ID & old_id, PBD::ID & new_id)
97 {
98         id_map.insert (IdPair (old_id, new_id));
99 }
100
101 PBD::ID const &
102 AudioRegionImportHandler::get_new_id (PBD::ID & old_id) const
103 {
104         return (id_map.find (old_id))->second;
105 }
106
107 /*** AudioRegionImporter ***/
108 AudioRegionImporter::AudioRegionImporter (XMLTree const & source, Session & session, AudioRegionImportHandler & handler, XMLNode const & node) :
109   ElementImporter (source, session),
110   xml_region (node),
111   handler (handler),
112   old_id ("0"),
113   region_prepared (false),
114   sources_prepared (false)
115 {
116         if (!parse_xml_region () || !parse_source_xml ()) {
117                 throw failed_constructor();
118         }
119         handler.register_id (old_id, id);
120 }
121
122 AudioRegionImporter::~AudioRegionImporter ()
123 {
124 }
125
126 string
127 AudioRegionImporter::get_info () const
128 {
129         nframes_t length, position;
130         Timecode::Time length_time, position_time;
131         std::ostringstream oss;
132
133         // Get sample positions
134         std::istringstream iss_length(xml_region.property ("length")->value());
135         iss_length >> length;
136         std::istringstream iss_position(xml_region.property ("position")->value());
137         iss_position >> position;
138
139         // Convert to timecode
140         session.sample_to_timecode(length, length_time, true, false);
141         session.sample_to_timecode(position, position_time, true, false);
142
143         // return info
144         oss << _("Length: ") <<
145           timecode_to_string(length_time) <<
146           _("\nPosition: ") <<
147           timecode_to_string(position_time) <<
148           _("\nChannels: ") <<
149           xml_region.property ("channels")->value();
150
151
152         return oss.str();
153 }
154
155 bool
156 AudioRegionImporter::_prepare_move ()
157 {
158         return true;
159 }
160
161 void
162 AudioRegionImporter::_cancel_move ()
163 {
164 }
165
166 void
167 AudioRegionImporter::_move ()
168 {
169         if (!region_prepared) {
170                 prepare_region();
171                 if (!region_prepared) {
172                         return;
173                 }
174         }
175
176         if (broken()) {
177                 return;
178         }
179
180         session.add_regions (region);
181 }
182
183 bool
184 AudioRegionImporter::parse_xml_region ()
185 {
186         XMLPropertyList const & props = xml_region.properties();
187         bool id_ok = false;
188         bool name_ok = false;
189
190         for (XMLPropertyList::const_iterator it = props.begin(); it != props.end(); ++it) {
191                 string prop = (*it)->name();
192                 if (!prop.compare ("type") || !prop.compare ("stretch") ||
193                   !prop.compare ("shift") || !prop.compare ("first_edit") ||
194                   !prop.compare ("layer") || !prop.compare ("flags") ||
195                   !prop.compare ("scale-gain") || !prop.compare("channels") ||
196                   !prop.compare ("first-edit") ||
197                   prop.find ("master-source-") == 0 || prop.find ("source-") == 0) {
198                         // All ok
199                 } else if (!prop.compare ("start") || !prop.compare ("length") ||
200                   !prop.compare ("position") || !prop.compare ("ancestral-start") ||
201                   !prop.compare ("ancestral-length") || !prop.compare ("sync-position")) {
202                         // Sample rate conversion
203                         (*it)->set_value (rate_convert_samples ((*it)->value()));
204                 } else if (!prop.compare("id")) {
205                         // get old id and update id
206                         old_id = (*it)->value();
207                         (*it)->set_value (id.to_s());
208                         id_ok = true;
209                 } else if (!prop.compare("name")) {
210                         // rename region if necessary
211                         name = (*it)->value();
212                         name = session.new_region_name (name);
213                         (*it)->set_value (name);
214                         name_ok = true;
215                 } else {
216                         std::cerr << string_compose (X_("AudioRegionImporter (%1): did not recognise XML-property \"%2\""), name, prop) << endmsg;
217                 }
218         }
219
220         if (!id_ok) {
221                 error << string_compose (X_("AudioRegionImporter (%1): did not find necessary XML-property \"id\""), name) << endmsg;
222                 return false;
223         }
224
225         if (!name_ok) {
226                 error << X_("AudioRegionImporter: did not find necessary XML-property \"name\"") << endmsg;
227                 return false;
228         }
229
230         return true;
231 }
232
233 bool
234 AudioRegionImporter::parse_source_xml ()
235 {
236         uint32_t channels;
237         char buf[128];
238         PBD::sys::path source_dir = get_sound_dir (source);
239         PBD::sys::path source_path;
240         XMLNode * source_node;
241         XMLProperty *prop;
242
243         // Get XML for sources
244         if (!(source_node = source.root()->child (X_("Sources")))) {
245                 return false;
246         }
247         XMLNodeList const & sources = source_node->children();
248
249         // Get source for each channel
250         if (!(prop = xml_region.property ("channels"))) {
251                 error << string_compose (X_("AudioRegionImporter (%1): did not find necessary XML-property \"channels\""), name) << endmsg;
252                 return false;
253         }
254
255         channels = atoi (prop->value().c_str());
256         for (uint32_t i = 0; i < channels; ++i) {
257                 bool source_found = false;
258
259                 // Get id for source-n
260                 snprintf (buf, sizeof(buf), X_("source-%d"), i);
261                 prop = xml_region.property (buf);
262                 if (!prop) {
263                         error << string_compose (X_("AudioRegionImporter (%1): did not find necessary XML-property \"%2\""), name, buf) << endmsg;
264                         return false;
265                 }
266                 string source_id = prop->value();
267
268                 // Get source
269                 for (XMLNodeList::const_iterator it = sources.begin(); it != sources.end(); it++) {
270                         prop = (*it)->property ("id");
271                         if (prop && !source_id.compare (prop->value())) {
272                                 source_path = source_dir;
273                                 prop = (*it)->property ("name");
274                                 if (!prop) {
275                                         error << string_compose (X_("AudioRegionImporter (%1): source %2 has no \"name\" property"), name, source_id) << endmsg;
276                                         return false;
277                                 }
278                                 source_path /= prop->value();
279                                 filenames.push_back (source_path.to_string());
280
281                                 source_found = true;
282                                 break;
283                         }
284                 }
285
286                 if (!source_found) {
287                         error << string_compose (X_("AudioRegionImporter (%1): could not find all necessary sources"), name) << endmsg;
288                         return false;
289                 }
290         }
291
292         return true;
293 }
294
295 PBD::sys::path
296 AudioRegionImporter::get_sound_dir (XMLTree const & tree)
297 {
298         PBD::sys::path source_dir = tree.filename();
299         source_dir = source_dir.branch_path();
300         SessionDirectory session_dir(source_dir);
301         source_dir = session_dir.sound_path();
302
303         return source_dir;
304 }
305
306 void
307 AudioRegionImporter::prepare_region ()
308 {
309         if (region_prepared) {
310                 return;
311         }
312
313         SourceList source_list;
314         prepare_sources();
315
316         // Create source list
317         for (std::list<string>::iterator it = filenames.begin(); it != filenames.end(); ++it) {
318                 source_list.push_back (handler.get_source (*it));
319         }
320
321         // create region and update XML
322         region.push_back (RegionFactory::create (source_list, xml_region));
323         if (*region.begin()) {
324                 xml_region = (*region.begin())->get_state();
325         } else {
326                 error << string_compose (X_("AudioRegionImporter (%1): could not construct Region"), name) << endmsg;
327                 handler.set_errors();
328         }
329
330         region_prepared = true;
331 }
332
333 void
334 AudioRegionImporter::prepare_sources ()
335 {
336         if (sources_prepared) {
337                 return;
338         }
339
340         status.total = 0;
341         status.replace_existing_source = false;
342         status.done = false;
343         status.cancel = false;
344         status.freeze = false;
345         status.progress = 0.0;
346         status.quality = SrcBest; // TODO other qualities also
347
348         // Get sources that still need to be imported
349         for (std::list<string>::iterator it = filenames.begin(); it != filenames.end(); ++it) {
350                 if (!handler.check_source (*it)) {
351                         status.paths.push_back (*it);
352                         status.total++;
353                 }
354         }
355
356         // import files
357         // TODO: threading & exception handling
358         session.import_audiofiles (status);
359
360         // Add imported sources to handlers map
361         std::vector<Glib::ustring>::iterator file_it = status.paths.begin();
362         for (SourceList::iterator source_it = status.sources.begin(); source_it != status.sources.end(); ++source_it) {
363                 if (*source_it) {
364                         handler.add_source(*file_it, *source_it);
365                 } else {
366                         error << string_compose (X_("AudioRegionImporter (%1): could not import all necessary sources"), name) << endmsg;
367                         handler.set_errors();
368                         set_broken();
369                 }
370
371                 ++file_it;
372         }
373
374         sources_prepared = true;
375 }
376
377 void
378 AudioRegionImporter::add_sources_to_session ()
379 {
380         if (!sources_prepared) {
381                 prepare_sources();
382         }
383
384         if (broken()) {
385                 return;
386         }
387
388         for (std::list<string>::iterator it = filenames.begin(); it != filenames.end(); ++it) {
389                 session.add_source (handler.get_source (*it));
390         }
391 }
392
393 XMLNode const &
394 AudioRegionImporter::get_xml ()
395 {
396         if(!region_prepared) {
397                 prepare_region();
398         }
399
400         return xml_region;
401 }