Apply MIDI looping patch from torbenh, with minor changes.
[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 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 string
122 AudioRegionImporter::get_info () const
123 {
124         nframes_t length, position;
125         SMPTE::Time length_time, position_time;
126         std::ostringstream oss;
127         
128         // Get sample positions
129         std::istringstream iss_length(xml_region.property ("length")->value());
130         iss_length >> length;
131         std::istringstream iss_position(xml_region.property ("position")->value());
132         iss_position >> position;
133         
134         // Convert to smpte
135         session.sample_to_smpte(length, length_time, true, false);
136         session.sample_to_smpte(position, position_time, true, false);
137         
138         // return info
139         oss << _("Length: ") <<
140           smpte_to_string(length_time) <<
141           _("\nPosition: ") << 
142           smpte_to_string(position_time) <<
143           _("\nChannels: ") <<
144           xml_region.property ("channels")->value();
145
146         
147         return oss.str();
148 }
149
150 bool
151 AudioRegionImporter::prepare_move ()
152 {
153         queued = true;
154         return true;
155 }
156
157 void
158 AudioRegionImporter::cancel_move ()
159 {
160         queued = false;
161 }
162
163 void
164 AudioRegionImporter::move ()
165 {
166         if (!region_prepared) {
167                 prepare_region();
168                 if (!region_prepared) {
169                         return;
170                 }
171         }
172         
173         if (broken()) {
174                 return;
175         }
176         
177         session.add_regions (region);
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.find ("master-source-") == 0 || prop.find ("source-") == 0) {
194                         // All ok
195                 } else if (!prop.compare ("start") || !prop.compare ("length") ||
196                   !prop.compare ("position") || !prop.compare ("ancestral-start") ||
197                   !prop.compare ("ancestral-length") || !prop.compare ("sync-position")) {
198                         // Sample rate conversion
199                         (*it)->set_value (rate_convert_samples ((*it)->value()));
200                 } else if (!prop.compare("id")) {
201                         // get old id and update id
202                         old_id = (*it)->value();
203                         (*it)->set_value (id.to_s());
204                         id_ok = true;
205                 } else if (!prop.compare("name")) {
206                         // rename region if necessary
207                         name = (*it)->value();
208                         name = session.new_region_name (name);
209                         (*it)->set_value (name);
210                         name_ok = true;
211                 } else {
212                         std::cerr << string_compose (X_("AudioRegionImporter (%1): did not recognise XML-property \"%1\""), name, prop) << endmsg;
213                 }
214         }
215         
216         if (!id_ok) {
217                 error << string_compose (X_("AudioRegionImporter (%1): did not find necessary XML-property \"id\""), name) << endmsg;
218                 return false;
219         }
220         
221         if (!name_ok) {
222                 error << X_("AudioRegionImporter: did not find necessary XML-property \"name\"") << endmsg;
223                 return false;
224         }
225         
226         return true;
227 }
228
229 bool
230 AudioRegionImporter::parse_source_xml ()
231 {
232         uint32_t channels;
233         char buf[128];
234         PBD::sys::path source_dir = get_sound_dir (source);
235         PBD::sys::path source_path;
236         XMLNode * source_node;
237         XMLProperty *prop;
238         
239         // Get XML for sources
240         if (!(source_node = source.root()->child (X_("Sources")))) {
241                 return false;
242         }
243         XMLNodeList const & sources = source_node->children();
244         
245         // Get source for each channel
246         if (!(prop = xml_region.property ("channels"))) {
247                 error << string_compose (X_("AudioRegionImporter (%1): did not find necessary XML-property \"channels\""), name) << endmsg;
248                 return false;
249         }
250         
251         channels = atoi (prop->value());
252         for (uint32_t i = 0; i < channels; ++i) {
253                 bool source_found = false;
254                 
255                 // Get id for source-n
256                 snprintf (buf, sizeof(buf), X_("source-%d"), i);
257                 prop = xml_region.property (buf);
258                 if (!prop) {
259                         error << string_compose (X_("AudioRegionImporter (%1): did not find necessary XML-property \"%3\""), name, buf) << endmsg;
260                         return false;
261                 }
262                 string source_id = prop->value();
263                 
264                 // Get source
265                 for (XMLNodeList::const_iterator it = sources.begin(); it != sources.end(); it++) {
266                         prop = (*it)->property ("id");
267                         if (prop && !source_id.compare (prop->value())) {
268                                 source_path = source_dir;
269                                 prop = (*it)->property ("name");
270                                 if (!prop) {
271                                         error << string_compose (X_("AudioRegionImporter (%1): source %2 has no \"name\" property"), name, source_id) << endmsg;
272                                         return false;
273                                 }
274                                 source_path /= prop->value();
275                                 filenames.push_back (source_path.to_string());
276                                 
277                                 source_found = true;
278                                 break;
279                         }
280                 }
281                 
282                 if (!source_found) {
283                         error << string_compose (X_("AudioRegionImporter (%1): could not find all necessary sources"), name) << endmsg;
284                         return false;
285                 }
286         }
287         
288         return true;
289 }
290
291 PBD::sys::path
292 AudioRegionImporter::get_sound_dir (XMLTree const & tree)
293 {
294         PBD::sys::path source_dir = tree.filename();
295         source_dir = source_dir.branch_path();
296         SessionDirectory session_dir(source_dir);
297         source_dir = session_dir.sound_path();
298         
299         return source_dir;
300 }
301
302 void
303 AudioRegionImporter::prepare_region ()
304 {
305         if (region_prepared) {
306                 return;
307         }
308         
309         SourceList source_list;
310         prepare_sources();
311         
312         // Create source list
313         for (std::list<string>::iterator it = filenames.begin(); it != filenames.end(); ++it) {
314                 source_list.push_back (handler.get_source (*it));
315         }
316         
317         // create region and update XML
318         region.push_back (RegionFactory::create (source_list, xml_region));
319         if (*region.begin()) {
320                 xml_region = (*region.begin())->get_state();
321         } else {
322                 error << string_compose (X_("AudioRegionImporter (%1): could not construct Region"), name) << endmsg;
323                 handler.set_errors();
324         }
325
326         region_prepared = true;
327 }
328
329 void
330 AudioRegionImporter::prepare_sources ()
331 {
332         if (sources_prepared) {
333                 return;
334         }
335         
336         Session::import_status status;
337         
338         // Get sources that still need to be imported
339         for (std::list<string>::iterator it = filenames.begin(); it != filenames.end(); ++it) {
340                 if (!handler.check_source (*it)) {
341                         status.paths.push_back (*it);
342                 }
343         }
344         
345         // Prepare rest of import struct TODO quality
346         status.replace_existing_source = false;
347         status.done = false;
348         status.cancel = false;
349         status.freeze = false;
350         status.progress = 0.0;
351         status.quality = SrcBest;
352         
353         // import files
354         // TODO: threading & exception handling
355         session.import_audiofiles (status);
356         
357         // Add imported sources to handlers map
358         std::vector<Glib::ustring>::iterator file_it = status.paths.begin();
359         for (SourceList::iterator source_it = status.sources.begin(); source_it != status.sources.end(); ++source_it) {
360                 if (*source_it) {
361                         handler.add_source(*file_it, *source_it);
362                 } else {
363                         error << string_compose (X_("AudioRegionImporter (%1): could not import all necessary sources"), name) << endmsg;
364                         handler.set_errors();
365                         set_broken();
366                 }
367                 
368                 ++file_it;
369         }
370         
371         sources_prepared = true;
372 }
373
374 void
375 AudioRegionImporter::add_sources_to_session ()
376 {
377         if (!sources_prepared) {
378                 prepare_sources();
379         }
380         
381         if (broken()) {
382                 return;
383         }
384         
385         for (std::list<string>::iterator it = filenames.begin(); it != filenames.end(); ++it) {
386                 session.add_source (handler.get_source (*it));
387         }
388 }
389
390 XMLNode const & 
391 AudioRegionImporter::get_xml ()
392 {
393         if(!region_prepared) {
394                 prepare_region();
395         }
396         
397         return xml_region;
398 }