2 Copyright (C) 2008 Paul Davis
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.
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.
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.
21 #include "ardour/audio_region_importer.h"
25 #include "pbd/failed_constructor.h"
26 #include "pbd/compose.h"
27 #include "pbd/error.h"
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"
38 using namespace ARDOUR;
41 AudioRegionImportHandler::AudioRegionImportHandler (XMLTree const & source, Session & session) :
42 ElementImportHandler (source, session)
44 XMLNode const * root = source.root();
45 XMLNode const * regions;
47 if (!(regions = root->child (X_("Regions")))) {
48 throw failed_constructor();
51 create_regions_from_children (*regions, elements);
55 AudioRegionImportHandler::create_regions_from_children (XMLNode const & node, ElementList & list)
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") ) {
62 list.push_back (ElementPtr ( new AudioRegionImporter (source, session, *this, **it)));
63 } catch (failed_constructor err) {
71 AudioRegionImportHandler::get_info () const
73 return _("Audio Regions");
77 AudioRegionImportHandler::check_source (string const & filename) const
79 return (sources.find (filename) != sources.end());
83 AudioRegionImportHandler::add_source (string const & filename, boost::shared_ptr<Source> const & source)
85 sources.insert (SourcePair (filename, source));
88 boost::shared_ptr<Source> const &
89 AudioRegionImportHandler::get_source (string const & filename) const
91 return (sources.find (filename))->second;
95 AudioRegionImportHandler::register_id (PBD::ID & old_id, PBD::ID & new_id)
97 id_map.insert (IdPair (old_id, new_id));
101 AudioRegionImportHandler::get_new_id (PBD::ID & old_id) const
103 return (id_map.find (old_id))->second;
106 /*** AudioRegionImporter ***/
107 AudioRegionImporter::AudioRegionImporter (XMLTree const & source, Session & session, AudioRegionImportHandler & handler, XMLNode const & node) :
108 ElementImporter (source, session),
112 region_prepared (false),
113 sources_prepared (false)
115 if (!parse_xml_region () || !parse_source_xml ()) {
116 throw failed_constructor();
118 handler.register_id (old_id, id);
121 AudioRegionImporter::~AudioRegionImporter ()
126 AudioRegionImporter::get_info () const
128 nframes_t length, position;
129 SMPTE::Time length_time, position_time;
130 std::ostringstream oss;
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;
139 session.sample_to_smpte(length, length_time, true, false);
140 session.sample_to_smpte(position, position_time, true, false);
143 oss << _("Length: ") <<
144 smpte_to_string(length_time) <<
146 smpte_to_string(position_time) <<
148 xml_region.property ("channels")->value();
155 AudioRegionImporter::_prepare_move ()
161 AudioRegionImporter::_cancel_move ()
166 AudioRegionImporter::_move ()
168 if (!region_prepared) {
170 if (!region_prepared) {
179 session.add_regions (region);
183 AudioRegionImporter::parse_xml_region ()
185 XMLPropertyList const & props = xml_region.properties();
187 bool name_ok = false;
189 for (XMLPropertyList::const_iterator it = props.begin(); it != props.end(); ++it) {
190 string prop = (*it)->name();
191 if (!prop.compare ("type") || !prop.compare ("stretch") ||
192 !prop.compare ("shift") || !prop.compare ("first_edit") ||
193 !prop.compare ("layer") || !prop.compare ("flags") ||
194 !prop.compare ("scale-gain") || !prop.compare("channels") ||
195 !prop.compare ("first-edit") ||
196 prop.find ("master-source-") == 0 || prop.find ("source-") == 0) {
198 } else if (!prop.compare ("start") || !prop.compare ("length") ||
199 !prop.compare ("position") || !prop.compare ("ancestral-start") ||
200 !prop.compare ("ancestral-length") || !prop.compare ("sync-position")) {
201 // Sample rate conversion
202 (*it)->set_value (rate_convert_samples ((*it)->value()));
203 } else if (!prop.compare("id")) {
204 // get old id and update id
205 old_id = (*it)->value();
206 (*it)->set_value (id.to_s());
208 } else if (!prop.compare("name")) {
209 // rename region if necessary
210 name = (*it)->value();
211 name = session.new_region_name (name);
212 (*it)->set_value (name);
215 std::cerr << string_compose (X_("AudioRegionImporter (%1): did not recognise XML-property \"%2\""), name, prop) << endmsg;
220 error << string_compose (X_("AudioRegionImporter (%1): did not find necessary XML-property \"id\""), name) << endmsg;
225 error << X_("AudioRegionImporter: did not find necessary XML-property \"name\"") << endmsg;
233 AudioRegionImporter::parse_source_xml ()
237 PBD::sys::path source_dir = get_sound_dir (source);
238 PBD::sys::path source_path;
239 XMLNode * source_node;
242 // Get XML for sources
243 if (!(source_node = source.root()->child (X_("Sources")))) {
246 XMLNodeList const & sources = source_node->children();
248 // Get source for each channel
249 if (!(prop = xml_region.property ("channels"))) {
250 error << string_compose (X_("AudioRegionImporter (%1): did not find necessary XML-property \"channels\""), name) << endmsg;
254 channels = atoi (prop->value());
255 for (uint32_t i = 0; i < channels; ++i) {
256 bool source_found = false;
258 // Get id for source-n
259 snprintf (buf, sizeof(buf), X_("source-%d"), i);
260 prop = xml_region.property (buf);
262 error << string_compose (X_("AudioRegionImporter (%1): did not find necessary XML-property \"%2\""), name, buf) << endmsg;
265 string source_id = prop->value();
268 for (XMLNodeList::const_iterator it = sources.begin(); it != sources.end(); it++) {
269 prop = (*it)->property ("id");
270 if (prop && !source_id.compare (prop->value())) {
271 source_path = source_dir;
272 prop = (*it)->property ("name");
274 error << string_compose (X_("AudioRegionImporter (%1): source %2 has no \"name\" property"), name, source_id) << endmsg;
277 source_path /= prop->value();
278 filenames.push_back (source_path.to_string());
286 error << string_compose (X_("AudioRegionImporter (%1): could not find all necessary sources"), name) << endmsg;
295 AudioRegionImporter::get_sound_dir (XMLTree const & tree)
297 PBD::sys::path source_dir = tree.filename();
298 source_dir = source_dir.branch_path();
299 SessionDirectory session_dir(source_dir);
300 source_dir = session_dir.sound_path();
306 AudioRegionImporter::prepare_region ()
308 if (region_prepared) {
312 SourceList source_list;
315 // Create source list
316 for (std::list<string>::iterator it = filenames.begin(); it != filenames.end(); ++it) {
317 source_list.push_back (handler.get_source (*it));
320 // create region and update XML
321 region.push_back (RegionFactory::create (source_list, xml_region));
322 if (*region.begin()) {
323 xml_region = (*region.begin())->get_state();
325 error << string_compose (X_("AudioRegionImporter (%1): could not construct Region"), name) << endmsg;
326 handler.set_errors();
329 region_prepared = true;
333 AudioRegionImporter::prepare_sources ()
335 if (sources_prepared) {
340 status.replace_existing_source = false;
342 status.cancel = false;
343 status.freeze = false;
344 status.progress = 0.0;
345 status.quality = SrcBest; // TODO other qualities also
347 // Get sources that still need to be imported
348 for (std::list<string>::iterator it = filenames.begin(); it != filenames.end(); ++it) {
349 if (!handler.check_source (*it)) {
350 status.paths.push_back (*it);
356 // TODO: threading & exception handling
357 session.import_audiofiles (status);
359 // Add imported sources to handlers map
360 std::vector<Glib::ustring>::iterator file_it = status.paths.begin();
361 for (SourceList::iterator source_it = status.sources.begin(); source_it != status.sources.end(); ++source_it) {
363 handler.add_source(*file_it, *source_it);
365 error << string_compose (X_("AudioRegionImporter (%1): could not import all necessary sources"), name) << endmsg;
366 handler.set_errors();
373 sources_prepared = true;
377 AudioRegionImporter::add_sources_to_session ()
379 if (!sources_prepared) {
387 for (std::list<string>::iterator it = filenames.begin(); it != filenames.end(); ++it) {
388 session.add_source (handler.get_source (*it));
393 AudioRegionImporter::get_xml ()
395 if(!region_prepared) {