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>
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.
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.
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.
23 #include "ardour/audio_region_importer.h"
27 #include <glibmm/miscutils.h>
29 #include "pbd/failed_constructor.h"
30 #include "pbd/compose.h"
31 #include "pbd/error.h"
33 #include "ardour/session.h"
34 #include "ardour/region.h"
35 #include "ardour/region_factory.h"
36 #include "ardour/session_directory.h"
42 using namespace ARDOUR;
45 AudioRegionImportHandler::AudioRegionImportHandler (XMLTree const & source, Session & session) :
46 ElementImportHandler (source, session)
48 XMLNode const * root = source.root();
49 XMLNode const * regions;
51 if (!(regions = root->child (X_("Regions")))) {
52 throw failed_constructor();
55 create_regions_from_children (*regions, elements);
59 AudioRegionImportHandler::create_regions_from_children (XMLNode const & node, ElementList & list)
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") ) {
66 list.push_back (ElementPtr ( new AudioRegionImporter (source, session, *this, **it)));
67 } catch (failed_constructor const&) {
75 AudioRegionImportHandler::get_info () const
77 return _("Audio Regions");
81 AudioRegionImportHandler::check_source (string const & filename) const
83 return (sources.find (filename) != sources.end());
87 AudioRegionImportHandler::add_source (string const & filename, boost::shared_ptr<Source> const & source)
89 sources.insert (SourcePair (filename, source));
92 boost::shared_ptr<Source> const &
93 AudioRegionImportHandler::get_source (string const & filename) const
95 return (sources.find (filename))->second;
99 AudioRegionImportHandler::register_id (PBD::ID & old_id, PBD::ID & new_id)
101 id_map.insert (IdPair (old_id, new_id));
105 AudioRegionImportHandler::get_new_id (PBD::ID & old_id) const
107 return (id_map.find (old_id))->second;
110 /*** AudioRegionImporter ***/
111 AudioRegionImporter::AudioRegionImporter (XMLTree const & source, Session & session, AudioRegionImportHandler & handler, XMLNode const & node) :
112 ElementImporter (source, session),
116 region_prepared (false),
117 sources_prepared (false)
119 if (!parse_xml_region () || !parse_source_xml ()) {
120 throw failed_constructor();
122 handler.register_id (old_id, id);
125 AudioRegionImporter::~AudioRegionImporter ()
130 AudioRegionImporter::get_info () const
132 samplecnt_t length, position;
133 Timecode::Time length_time, position_time;
134 std::ostringstream oss;
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;
142 // Convert to timecode
143 session.sample_to_timecode(length, length_time, true, false);
144 session.sample_to_timecode(position, position_time, true, false);
147 oss << _("Length: ") <<
148 timecode_to_string(length_time) <<
150 timecode_to_string(position_time) <<
152 xml_region.property ("channels")->value();
159 AudioRegionImporter::_prepare_move ()
165 AudioRegionImporter::_cancel_move ()
170 AudioRegionImporter::_move ()
172 if (!region_prepared) {
174 if (!region_prepared) {
185 AudioRegionImporter::parse_xml_region ()
187 XMLPropertyList const & props = xml_region.properties();
189 bool name_ok = false;
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) {
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());
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);
217 std::cerr << string_compose (X_("AudioRegionImporter (%1): did not recognise XML-property \"%2\""), name, prop) << endmsg;
222 error << string_compose (X_("AudioRegionImporter (%1): did not find necessary XML-property \"id\""), name) << endmsg;
227 error << X_("AudioRegionImporter: did not find necessary XML-property \"name\"") << endmsg;
235 AudioRegionImporter::parse_source_xml ()
239 std::string source_dir(get_sound_dir (source));
240 XMLNode * source_node;
241 XMLProperty const * prop;
243 // Get XML for sources
244 if (!(source_node = source.root()->child (X_("Sources")))) {
247 XMLNodeList const & sources = source_node->children();
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;
255 channels = atoi (prop->value().c_str());
256 for (uint32_t i = 0; i < channels; ++i) {
257 bool source_found = false;
259 // Get id for source-n
260 snprintf (buf, sizeof(buf), X_("source-%d"), i);
261 prop = xml_region.property (buf);
263 error << string_compose (X_("AudioRegionImporter (%1): did not find necessary XML-property \"%2\""), name, buf) << endmsg;
266 string source_id = prop->value();
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");
274 error << string_compose (X_("AudioRegionImporter (%1): source %2 has no \"name\" property"), name, source_id) << endmsg;
277 filenames.push_back (Glib::build_filename (source_dir, prop->value()));
284 error << string_compose (X_("AudioRegionImporter (%1): could not find all necessary sources"), name) << endmsg;
293 AudioRegionImporter::get_sound_dir (XMLTree const & tree)
295 SessionDirectory session_dir(Glib::path_get_dirname (tree.filename()));
296 return session_dir.sound_path();
300 AudioRegionImporter::prepare_region ()
302 if (region_prepared) {
306 SourceList source_list;
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));
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);
319 region.push_back (r);
320 if (*region.begin()) {
321 xml_region = (*region.begin())->get_state();
323 error << string_compose (X_("AudioRegionImporter (%1): could not construct Region"), name) << endmsg;
324 handler.set_errors();
327 region_prepared = true;
331 AudioRegionImporter::prepare_sources ()
333 if (sources_prepared) {
338 status.replace_existing_source = false;
340 status.cancel = false;
341 status.freeze = false;
342 status.progress = 0.0;
343 status.quality = SrcBest; // TODO other qualities also
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);
354 // TODO: threading & exception handling
355 session.import_files (status);
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) {
361 handler.add_source(*file_it, *source_it);
363 error << string_compose (X_("AudioRegionImporter (%1): could not import all necessary sources"), name) << endmsg;
364 handler.set_errors();
371 sources_prepared = true;
375 AudioRegionImporter::add_sources_to_session ()
377 if (!sources_prepared) {
385 for (std::list<string>::iterator it = filenames.begin(); it != filenames.end(); ++it) {
386 session.add_source (handler.get_source (*it));
391 AudioRegionImporter::get_xml ()
393 if(!region_prepared) {