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