Expose virtual-keyboard port as async-port
[ardour.git] / libs / ardour / audio_region_importer.cc
1 /*
2  * Copyright (C) 2008-2009 Sakari Bergen <sakari.bergen@beatwaves.net>
3  * Copyright (C) 2009-2012 Carl Hetherington <carl@carlh.net>
4  * Copyright (C) 2009-2012 David Robillard <d@drobilla.net>
5  * Copyright (C) 2009-2017 Paul Davis <paul@linuxaudiosystems.com>
6  * Copyright (C) 2012 Tim Mayberry <mojofunk@gmail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #include "ardour/audio_region_importer.h"
24
25 #include <sstream>
26
27 #include <glibmm/miscutils.h>
28
29 #include "pbd/failed_constructor.h"
30 #include "pbd/compose.h"
31 #include "pbd/error.h"
32
33 #include "ardour/session.h"
34 #include "ardour/region.h"
35 #include "ardour/region_factory.h"
36 #include "ardour/session_directory.h"
37
38 #include "pbd/i18n.h"
39
40 using namespace std;
41 using namespace PBD;
42 using namespace ARDOUR;
43
44 /**** Handler ***/
45 AudioRegionImportHandler::AudioRegionImportHandler (XMLTree const & source, Session & session) :
46   ElementImportHandler (source, session)
47 {
48         XMLNode const * root = source.root();
49         XMLNode const * regions;
50
51         if (!(regions = root->child (X_("Regions")))) {
52                 throw failed_constructor();
53         }
54
55         create_regions_from_children (*regions, elements);
56 }
57
58 void
59 AudioRegionImportHandler::create_regions_from_children (XMLNode const & node, ElementList & list)
60 {
61         XMLNodeList const & children = node.children();
62         for (XMLNodeList::const_iterator it = children.begin(); it != children.end(); ++it) {
63                 XMLProperty const * type = (*it)->property("type");
64                 if (!(*it)->name().compare ("Region") && (!type || type->value() == "audio") ) {
65                         try {
66                                 list.push_back (ElementPtr ( new AudioRegionImporter (source, session, *this, **it)));
67                         } catch (failed_constructor const&) {
68                                 set_dirty();
69                         }
70                 }
71         }
72 }
73
74 string
75 AudioRegionImportHandler::get_info () const
76 {
77         return _("Audio Regions");
78 }
79
80 bool
81 AudioRegionImportHandler::check_source (string const & filename) const
82 {
83         return (sources.find (filename) != sources.end());
84 }
85
86 void
87 AudioRegionImportHandler::add_source (string const & filename, boost::shared_ptr<Source> const & source)
88 {
89         sources.insert (SourcePair (filename, source));
90 }
91
92 boost::shared_ptr<Source> const &
93 AudioRegionImportHandler::get_source (string const & filename) const
94 {
95         return (sources.find (filename))->second;
96 }
97
98 void
99 AudioRegionImportHandler::register_id (PBD::ID & old_id, PBD::ID & new_id)
100 {
101         id_map.insert (IdPair (old_id, new_id));
102 }
103
104 PBD::ID const &
105 AudioRegionImportHandler::get_new_id (PBD::ID & old_id) const
106 {
107         return (id_map.find (old_id))->second;
108 }
109
110 /*** AudioRegionImporter ***/
111 AudioRegionImporter::AudioRegionImporter (XMLTree const & source, Session & session, AudioRegionImportHandler & handler, XMLNode const & node) :
112   ElementImporter (source, session),
113   xml_region (node),
114   handler (handler),
115   old_id ("0"),
116   region_prepared (false),
117   sources_prepared (false)
118 {
119         if (!parse_xml_region () || !parse_source_xml ()) {
120                 throw failed_constructor();
121         }
122         handler.register_id (old_id, id);
123 }
124
125 AudioRegionImporter::~AudioRegionImporter ()
126 {
127 }
128
129 string
130 AudioRegionImporter::get_info () const
131 {
132         samplecnt_t length, position;
133         Timecode::Time length_time, position_time;
134         std::ostringstream oss;
135
136         // Get sample positions
137         std::istringstream iss_length(xml_region.property ("length")->value());
138         iss_length >> length;
139         std::istringstream iss_position(xml_region.property ("position")->value());
140         iss_position >> position;
141
142         // Convert to timecode
143         session.sample_to_timecode(length, length_time, true, false);
144         session.sample_to_timecode(position, position_time, true, false);
145
146         // return info
147         oss << _("Length: ") <<
148           timecode_to_string(length_time) <<
149           _("\nPosition: ") <<
150           timecode_to_string(position_time) <<
151           _("\nChannels: ") <<
152           xml_region.property ("channels")->value();
153
154
155         return oss.str();
156 }
157
158 bool
159 AudioRegionImporter::_prepare_move ()
160 {
161         return true;
162 }
163
164 void
165 AudioRegionImporter::_cancel_move ()
166 {
167 }
168
169 void
170 AudioRegionImporter::_move ()
171 {
172         if (!region_prepared) {
173                 prepare_region();
174                 if (!region_prepared) {
175                         return;
176                 }
177         }
178
179         if (broken()) {
180                 return;
181         }
182 }
183
184 bool
185 AudioRegionImporter::parse_xml_region ()
186 {
187         XMLPropertyList const & props = xml_region.properties();
188         bool id_ok = false;
189         bool name_ok = false;
190
191         for (XMLPropertyList::const_iterator it = props.begin(); it != props.end(); ++it) {
192                 string prop = (*it)->name();
193                 if (!prop.compare ("type") || !prop.compare ("stretch") ||
194                   !prop.compare ("shift") || !prop.compare ("first_edit") ||
195                   !prop.compare ("layer") || !prop.compare ("flags") ||
196                   !prop.compare ("scale-gain") || !prop.compare("channels") ||
197                   !prop.compare ("first-edit") ||
198                   prop.find ("master-source-") == 0 || prop.find ("source-") == 0) {
199                         // All ok
200                 } else if (!prop.compare ("start") || !prop.compare ("length") ||
201                   !prop.compare ("position") || !prop.compare ("ancestral-start") ||
202                   !prop.compare ("ancestral-length") || !prop.compare ("sync-position")) {
203                         // Sample rate conversion
204                         (*it)->set_value (rate_convert_samples ((*it)->value()));
205                 } else if (!prop.compare("id")) {
206                         // get old id and update id
207                         old_id = (*it)->value();
208                         (*it)->set_value (id.to_s());
209                         id_ok = true;
210                 } else if (!prop.compare("name")) {
211                         // rename region if necessary
212                         name = (*it)->value();
213                         name = RegionFactory::new_region_name (name);
214                         (*it)->set_value (name);
215                         name_ok = true;
216                 } else {
217                         std::cerr << string_compose (X_("AudioRegionImporter (%1): did not recognise XML-property \"%2\""), name, prop) << endmsg;
218                 }
219         }
220
221         if (!id_ok) {
222                 error << string_compose (X_("AudioRegionImporter (%1): did not find necessary XML-property \"id\""), name) << endmsg;
223                 return false;
224         }
225
226         if (!name_ok) {
227                 error << X_("AudioRegionImporter: did not find necessary XML-property \"name\"") << endmsg;
228                 return false;
229         }
230
231         return true;
232 }
233
234 bool
235 AudioRegionImporter::parse_source_xml ()
236 {
237         uint32_t channels;
238         char buf[128];
239         std::string source_dir(get_sound_dir (source));
240         XMLNode * source_node;
241         XMLProperty const * 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                                 prop = (*it)->property ("name");
273                                 if (!prop) {
274                                         error << string_compose (X_("AudioRegionImporter (%1): source %2 has no \"name\" property"), name, source_id) << endmsg;
275                                         return false;
276                                 }
277                                 filenames.push_back (Glib::build_filename (source_dir, prop->value()));
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 std::string
293 AudioRegionImporter::get_sound_dir (XMLTree const & tree)
294 {
295         SessionDirectory session_dir(Glib::path_get_dirname (tree.filename()));
296         return session_dir.sound_path();
297 }
298
299 void
300 AudioRegionImporter::prepare_region ()
301 {
302         if (region_prepared) {
303                 return;
304         }
305
306         SourceList source_list;
307         prepare_sources();
308
309         // Create source list
310         for (std::list<string>::iterator it = filenames.begin(); it != filenames.end(); ++it) {
311                 source_list.push_back (handler.get_source (*it));
312         }
313
314         // create region and update XML
315         boost::shared_ptr<Region> r = RegionFactory::create (source_list, xml_region);
316         if (session.config.get_glue_new_regions_to_bars_and_beats ()) {
317                 r->set_position_lock_style (MusicTime);
318         }
319         region.push_back (r);
320         if (*region.begin()) {
321                 xml_region = (*region.begin())->get_state();
322         } else {
323                 error << string_compose (X_("AudioRegionImporter (%1): could not construct Region"), name) << endmsg;
324                 handler.set_errors();
325         }
326
327         region_prepared = true;
328 }
329
330 void
331 AudioRegionImporter::prepare_sources ()
332 {
333         if (sources_prepared) {
334                 return;
335         }
336
337         status.total = 0;
338         status.replace_existing_source = false;
339         status.done = false;
340         status.cancel = false;
341         status.freeze = false;
342         status.progress = 0.0;
343         status.quality = SrcBest; // TODO other qualities also
344
345         // Get sources that still need to be imported
346         for (std::list<string>::iterator it = filenames.begin(); it != filenames.end(); ++it) {
347                 if (!handler.check_source (*it)) {
348                         status.paths.push_back (*it);
349                         status.total++;
350                 }
351         }
352
353         // import files
354         // TODO: threading & exception handling
355         session.import_files (status);
356
357         // Add imported sources to handlers map
358         std::vector<string>::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 }