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 <glibmm/miscutils.h>
27 #include "pbd/failed_constructor.h"
28 #include "pbd/compose.h"
29 #include "pbd/error.h"
31 #include "ardour/session.h"
32 #include "ardour/region.h"
33 #include "ardour/region_factory.h"
34 #include "ardour/session_directory.h"
40 using namespace ARDOUR;
43 AudioRegionImportHandler::AudioRegionImportHandler (XMLTree const & source, Session & session) :
44 ElementImportHandler (source, session)
46 XMLNode const * root = source.root();
47 XMLNode const * regions;
49 if (!(regions = root->child (X_("Regions")))) {
50 throw failed_constructor();
53 create_regions_from_children (*regions, elements);
57 AudioRegionImportHandler::create_regions_from_children (XMLNode const & node, ElementList & list)
59 XMLNodeList const & children = node.children();
60 for (XMLNodeList::const_iterator it = children.begin(); it != children.end(); ++it) {
61 XMLProperty const * type = (*it)->property("type");
62 if (!(*it)->name().compare ("Region") && (!type || type->value() == "audio") ) {
64 list.push_back (ElementPtr ( new AudioRegionImporter (source, session, *this, **it)));
65 } catch (failed_constructor err) {
73 AudioRegionImportHandler::get_info () const
75 return _("Audio Regions");
79 AudioRegionImportHandler::check_source (string const & filename) const
81 return (sources.find (filename) != sources.end());
85 AudioRegionImportHandler::add_source (string const & filename, boost::shared_ptr<Source> const & source)
87 sources.insert (SourcePair (filename, source));
90 boost::shared_ptr<Source> const &
91 AudioRegionImportHandler::get_source (string const & filename) const
93 return (sources.find (filename))->second;
97 AudioRegionImportHandler::register_id (PBD::ID & old_id, PBD::ID & new_id)
99 id_map.insert (IdPair (old_id, new_id));
103 AudioRegionImportHandler::get_new_id (PBD::ID & old_id) const
105 return (id_map.find (old_id))->second;
108 /*** AudioRegionImporter ***/
109 AudioRegionImporter::AudioRegionImporter (XMLTree const & source, Session & session, AudioRegionImportHandler & handler, XMLNode const & node) :
110 ElementImporter (source, session),
114 region_prepared (false),
115 sources_prepared (false)
117 if (!parse_xml_region () || !parse_source_xml ()) {
118 throw failed_constructor();
120 handler.register_id (old_id, id);
123 AudioRegionImporter::~AudioRegionImporter ()
128 AudioRegionImporter::get_info () const
130 framecnt_t length, position;
131 Timecode::Time length_time, position_time;
132 std::ostringstream oss;
134 // Get sample positions
135 std::istringstream iss_length(xml_region.property ("length")->value());
136 iss_length >> length;
137 std::istringstream iss_position(xml_region.property ("position")->value());
138 iss_position >> position;
140 // Convert to timecode
141 session.sample_to_timecode(length, length_time, true, false);
142 session.sample_to_timecode(position, position_time, true, false);
145 oss << _("Length: ") <<
146 timecode_to_string(length_time) <<
148 timecode_to_string(position_time) <<
150 xml_region.property ("channels")->value();
157 AudioRegionImporter::_prepare_move ()
163 AudioRegionImporter::_cancel_move ()
168 AudioRegionImporter::_move ()
170 if (!region_prepared) {
172 if (!region_prepared) {
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 = RegionFactory::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 std::string source_dir(get_sound_dir (source));
238 XMLNode * source_node;
239 XMLProperty const * prop;
241 // Get XML for sources
242 if (!(source_node = source.root()->child (X_("Sources")))) {
245 XMLNodeList const & sources = source_node->children();
247 // Get source for each channel
248 if (!(prop = xml_region.property ("channels"))) {
249 error << string_compose (X_("AudioRegionImporter (%1): did not find necessary XML-property \"channels\""), name) << endmsg;
253 channels = atoi (prop->value().c_str());
254 for (uint32_t i = 0; i < channels; ++i) {
255 bool source_found = false;
257 // Get id for source-n
258 snprintf (buf, sizeof(buf), X_("source-%d"), i);
259 prop = xml_region.property (buf);
261 error << string_compose (X_("AudioRegionImporter (%1): did not find necessary XML-property \"%2\""), name, buf) << endmsg;
264 string source_id = prop->value();
267 for (XMLNodeList::const_iterator it = sources.begin(); it != sources.end(); ++it) {
268 prop = (*it)->property ("id");
269 if (prop && !source_id.compare (prop->value())) {
270 prop = (*it)->property ("name");
272 error << string_compose (X_("AudioRegionImporter (%1): source %2 has no \"name\" property"), name, source_id) << endmsg;
275 filenames.push_back (Glib::build_filename (source_dir, prop->value()));
282 error << string_compose (X_("AudioRegionImporter (%1): could not find all necessary sources"), name) << endmsg;
291 AudioRegionImporter::get_sound_dir (XMLTree const & tree)
293 SessionDirectory session_dir(Glib::path_get_dirname (tree.filename()));
294 return session_dir.sound_path();
298 AudioRegionImporter::prepare_region ()
300 if (region_prepared) {
304 SourceList source_list;
307 // Create source list
308 for (std::list<string>::iterator it = filenames.begin(); it != filenames.end(); ++it) {
309 source_list.push_back (handler.get_source (*it));
312 // create region and update XML
313 boost::shared_ptr<Region> r = RegionFactory::create (source_list, xml_region);
314 if (session.config.get_glue_new_regions_to_bars_and_beats ()) {
315 r->set_position_lock_style (MusicTime);
317 region.push_back (r);
318 if (*region.begin()) {
319 xml_region = (*region.begin())->get_state();
321 error << string_compose (X_("AudioRegionImporter (%1): could not construct Region"), name) << endmsg;
322 handler.set_errors();
325 region_prepared = true;
329 AudioRegionImporter::prepare_sources ()
331 if (sources_prepared) {
336 status.replace_existing_source = false;
338 status.cancel = false;
339 status.freeze = false;
340 status.progress = 0.0;
341 status.quality = SrcBest; // TODO other qualities also
343 // Get sources that still need to be imported
344 for (std::list<string>::iterator it = filenames.begin(); it != filenames.end(); ++it) {
345 if (!handler.check_source (*it)) {
346 status.paths.push_back (*it);
352 // TODO: threading & exception handling
353 session.import_files (status);
355 // Add imported sources to handlers map
356 std::vector<string>::iterator file_it = status.paths.begin();
357 for (SourceList::iterator source_it = status.sources.begin(); source_it != status.sources.end(); ++source_it) {
359 handler.add_source(*file_it, *source_it);
361 error << string_compose (X_("AudioRegionImporter (%1): could not import all necessary sources"), name) << endmsg;
362 handler.set_errors();
369 sources_prepared = true;
373 AudioRegionImporter::add_sources_to_session ()
375 if (!sources_prepared) {
383 for (std::list<string>::iterator it = filenames.begin(); it != filenames.end(); ++it) {
384 session.add_source (handler.get_source (*it));
389 AudioRegionImporter::get_xml ()
391 if(!region_prepared) {