2 * Copyright (C) 2000-2017 Paul Davis <paul@linuxaudiosystems.com>
3 * Copyright (C) 2006-2016 David Robillard <d@drobilla.net>
4 * Copyright (C) 2007-2012 Tim Mayberry <mojofunk@gmail.com>
5 * Copyright (C) 2009-2012 Carl Hetherington <carl@carlh.net>
6 * Copyright (C) 2014-2018 Robin Gareus <robin@gareus.org>
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.
24 #include "libardour-config.h"
38 #include <samplerate.h>
40 #include "pbd/gstdio_compat.h"
43 #include <boost/scoped_array.hpp>
44 #include <boost/scoped_ptr.hpp>
45 #include <boost/shared_array.hpp>
47 #include "pbd/basename.h"
48 #include "pbd/convert.h"
50 #include "evoral/SMF.hpp"
52 #include "ardour/analyser.h"
53 #include "ardour/ardour.h"
54 #include "ardour/audioengine.h"
55 #include "ardour/audioregion.h"
56 #include "ardour/import_status.h"
57 #include "ardour/region_factory.h"
58 #include "ardour/resampled_source.h"
59 #include "ardour/runtime_functions.h"
60 #include "ardour/session.h"
61 #include "ardour/session_directory.h"
62 #include "ardour/smf_source.h"
63 #include "ardour/sndfile_helpers.h"
64 #include "ardour/sndfileimportable.h"
65 #include "ardour/sndfilesource.h"
66 #include "ardour/source_factory.h"
67 #include "ardour/tempo.h"
70 #include "ardour/caimportable.h"
76 using namespace ARDOUR;
79 static boost::shared_ptr<ImportableSource>
80 open_importable_source (const string& path, samplecnt_t samplerate, ARDOUR::SrcQuality quality)
82 /* try libsndfile first, because it can get BWF info from .wav, which ExtAudioFile cannot.
83 We don't necessarily need that information in an ImportableSource, but it keeps the
84 logic the same as in SourceFactory::create()
88 boost::shared_ptr<SndFileImportableSource> source(new SndFileImportableSource(path));
90 if (source->samplerate() == samplerate) {
94 /* rewrap as a resampled source */
96 return boost::shared_ptr<ImportableSource>(new ResampledImportableSource(source, samplerate, quality));
101 #ifdef HAVE_COREAUDIO
103 /* libsndfile failed, see if we can use CoreAudio to handle the IO */
105 CAImportableSource* src = new CAImportableSource(path);
106 boost::shared_ptr<CAImportableSource> source (src);
108 if (source->samplerate() == samplerate) {
112 /* rewrap as a resampled source */
114 return boost::shared_ptr<ImportableSource>(new ResampledImportableSource(source, samplerate, quality));
124 Session::get_paths_for_new_sources (bool /*allow_replacing*/, const string& import_file_path, uint32_t channels,
125 vector<string> const & smf_track_names)
128 vector<string> new_paths;
129 const string basename = basename_nosuffix (import_file_path);
131 for (uint32_t n = 0; n < channels; ++n) {
133 const DataType type = SMFSource::safe_midi_file_extension (import_file_path) ? DataType::MIDI : DataType::AUDIO;
138 assert (smf_track_names.empty() || smf_track_names.size() == channels);
141 if (smf_track_names.empty() || smf_track_names[n].empty()) {
142 mchn_name = string_compose ("%1-t%2", basename, n);
144 mchn_name = string_compose ("%1-%2", basename, smf_track_names[n]);
146 filepath = new_midi_source_path (mchn_name);
148 filepath = new_midi_source_path (basename);
151 case DataType::AUDIO:
152 filepath = new_audio_source_path (basename, channels, n, false, false);
156 if (filepath.empty()) {
157 error << string_compose (_("Cannot find new filename for imported file %1"), import_file_path) << endmsg;
158 return vector<string>();
161 new_paths.push_back (filepath);
168 map_existing_mono_sources (const vector<string>& new_paths, Session& /*sess*/,
169 uint32_t /*samplerate*/, vector<boost::shared_ptr<Source> >& newfiles, Session *session)
171 for (vector<string>::const_iterator i = new_paths.begin();
172 i != new_paths.end(); ++i)
174 boost::shared_ptr<Source> source = session->audio_source_by_path_and_channel(*i, 0);
177 error << string_compose(_("Could not find a source for %1 even though we are updating this file!"), (*i)) << endl;
181 newfiles.push_back(boost::dynamic_pointer_cast<Source>(source));
187 create_mono_sources_for_writing (const vector<string>& new_paths,
188 Session& sess, uint32_t samplerate,
189 vector<boost::shared_ptr<Source> >& newfiles,
190 samplepos_t natural_position)
192 for (vector<string>::const_iterator i = new_paths.begin(); i != new_paths.end(); ++i) {
194 boost::shared_ptr<Source> source;
197 const DataType type = SMFSource::safe_midi_file_extension (*i) ? DataType::MIDI : DataType::AUDIO;
199 source = SourceFactory::createWritable (type, sess,
201 false, // destructive
205 catch (const failed_constructor& err) {
206 error << string_compose (_("Unable to create file %1 during import"), *i) << endmsg;
210 newfiles.push_back(boost::dynamic_pointer_cast<Source>(source));
212 /* for audio files, reset the timeline position so that any BWF-ish
213 information in the original files we are importing from is maintained.
216 boost::shared_ptr<AudioFileSource> afs;
217 if ((afs = boost::dynamic_pointer_cast<AudioFileSource>(source)) != 0) {
218 afs->set_natural_position (natural_position);
225 compose_status_message (const string& path,
226 uint32_t file_samplerate,
227 uint32_t session_samplerate,
228 uint32_t /* current_file */,
229 uint32_t /* total_files */)
231 if (file_samplerate != session_samplerate) {
232 return string_compose (_("Resampling %1 from %2kHz to %3kHz"),
233 Glib::path_get_basename (path),
234 file_samplerate/1000.0f,
235 session_samplerate/1000.0f);
238 return string_compose (_("Copying %1"), Glib::path_get_basename (path));
242 write_audio_data_to_new_files (ImportableSource* source, ImportStatus& status,
243 vector<boost::shared_ptr<Source> >& newfiles)
245 const samplecnt_t nframes = ResampledImportableSource::blocksize;
246 boost::shared_ptr<AudioFileSource> afs;
247 uint32_t channels = source->channels();
252 boost::scoped_array<float> data(new float[nframes * channels]);
253 vector<boost::shared_array<Sample> > channel_data;
255 for (uint32_t n = 0; n < channels; ++n) {
256 channel_data.push_back(boost::shared_array<Sample>(new Sample[nframes]));
261 boost::shared_ptr<AudioSource> s = boost::dynamic_pointer_cast<AudioSource> (newfiles[0]);
264 status.progress = 0.0f;
265 float progress_multiplier = 1;
266 float progress_base = 0;
267 const float progress_length = source->ratio() * source->length();
269 if (!source->clamped_at_unity() && s->clamped_at_unity()) {
271 /* The source we are importing from can return sample values with a magnitude greater than 1,
272 and the file we are writing the imported data to cannot handle such values. Compute the gain
273 factor required to normalize the input sources to have a magnitude of less than 1.
277 uint32_t read_count = 0;
279 while (!status.cancel) {
280 samplecnt_t const nread = source->read (data.get(), nframes * channels);
285 peak = compute_peak (data.get(), nread, peak);
287 read_count += nread / channels;
288 status.progress = 0.5 * read_count / progress_length;
292 /* we are out of range: compute a gain to fix it */
293 gain = (1 - FLT_EPSILON) / peak;
297 progress_multiplier = 0.5;
301 samplecnt_t read_count = 0;
303 while (!status.cancel) {
305 samplecnt_t nread, nfread;
309 if ((nread = source->read (data.get(), nframes * channels)) == 0) {
310 #ifdef PLATFORM_WINDOWS
311 /* Flush the data once we've finished importing the file. Windows can */
312 /* cache the data for very long periods of time (perhaps not writing */
313 /* it to disk until Ardour closes). So let's force it to flush now. */
314 for (chn = 0; chn < channels; ++chn)
315 if ((afs = boost::dynamic_pointer_cast<AudioFileSource>(newfiles[chn])) != 0)
322 /* here is the gain fix for out-of-range sample values that we computed earlier */
323 apply_gain_to_buffer (data.get(), nread, gain);
326 nfread = nread / channels;
330 for (chn = 0; chn < channels; ++chn) {
333 for (x = chn, n = 0; n < nfread; x += channels, ++n) {
334 channel_data[chn][n] = (Sample) data[x];
340 for (chn = 0; chn < channels; ++chn) {
341 if ((afs = boost::dynamic_pointer_cast<AudioFileSource>(newfiles[chn])) != 0) {
342 afs->write (channel_data[chn].get(), nfread);
346 read_count += nfread;
347 status.progress = progress_base + progress_multiplier * read_count / progress_length;
352 write_midi_data_to_new_files (Evoral::SMF* source, ImportStatus& status,
353 vector<boost::shared_ptr<Source> >& newfiles,
356 uint32_t buf_size = 4;
357 uint8_t* buf = (uint8_t*) malloc (buf_size);
359 status.progress = 0.0f;
361 bool type0 = source->is_type0 () && split_type0;
362 const std::set<uint8_t>& chn = source->channels ();
365 num_tracks = source->channels().size();
367 num_tracks = source->num_tracks();
369 assert (newfiles.size() == num_tracks);
372 vector<boost::shared_ptr<Source> >::iterator s = newfiles.begin();
373 std::set<uint8_t>::const_iterator cur_chan = chn.begin();
375 for (unsigned i = 1; i <= num_tracks; ++i) {
377 boost::shared_ptr<SMFSource> smfs = boost::dynamic_pointer_cast<SMFSource> (*s);
379 Glib::Threads::Mutex::Lock source_lock(smfs->mutex());
381 smfs->drop_model (source_lock);
383 source->seek_to_start ();
385 source->seek_to_track (i);
389 uint32_t delta_t = 0;
393 while (!status.cancel) {
394 gint note_id_ignored; // imported files either don't have NoteID's or we ignore them.
398 int ret = source->read_event (&delta_t, &size, &buf, ¬e_id_ignored);
400 if (size > buf_size) {
404 if (ret < 0) { // EOT
410 if (ret == 0) { // Meta
414 // type-0 files separate by channel
416 uint8_t type = buf[0] & 0xf0;
417 uint8_t chan = buf[0] & 0x0f;
418 if (type >= 0x80 && type <= 0xE0) {
419 if (chan != *cur_chan) {
426 smfs->mark_streaming_write_started (source_lock);
430 smfs->append_event_beats(
432 Evoral::Event<Temporal::Beats>(
434 Temporal::Beats::ticks_at_rate(t, source->ppqn()),
438 if (status.progress < 0.99) {
439 status.progress += 0.01;
445 /* we wrote something */
447 const samplepos_t pos = 0;
448 const Temporal::Beats length_beats = Temporal::Beats::ticks_at_rate(t, source->ppqn());
449 BeatsSamplesConverter converter(smfs->session().tempo_map(), pos);
450 smfs->update_length(pos + converter.to(length_beats.round_up_to_beat()));
451 smfs->mark_streaming_write_completed (source_lock);
457 info << string_compose (_("Track %1 of %2 contained no usable MIDI data"), i, num_tracks) << endmsg;
466 } catch (exception& e) {
467 error << string_compose (_("MIDI file could not be written (best guess: %1)"), e.what()) << endmsg;
476 remove_file_source (boost::shared_ptr<Source> source)
478 boost::shared_ptr<FileSource> fs = boost::dynamic_pointer_cast<FileSource> (source);
480 fs->DropReferences ();
483 ::g_unlink (fs->path().c_str());
487 // This function is still unable to cleanly update an existing source, even though
488 // it is possible to set the ImportStatus flag accordingly. The functinality
489 // is disabled at the GUI until the Source implementations are able to provide
490 // the necessary API.
492 Session::import_files (ImportStatus& status)
494 typedef vector<boost::shared_ptr<Source> > Sources;
495 Sources all_new_sources;
496 boost::shared_ptr<AudioFileSource> afs;
497 boost::shared_ptr<SMFSource> smfs;
498 uint32_t channels = 0;
499 vector<string> smf_names;
501 status.sources.clear ();
503 for (vector<string>::const_iterator p = status.paths.begin();
504 p != status.paths.end() && !status.cancel;
507 boost::shared_ptr<ImportableSource> source;
509 const DataType type = SMFSource::safe_midi_file_extension (*p) ? DataType::MIDI : DataType::AUDIO;
510 boost::scoped_ptr<Evoral::SMF> smf_reader;
512 if (type == DataType::AUDIO) {
514 source = open_importable_source (*p, sample_rate(), status.quality);
515 channels = source->channels();
516 } catch (const failed_constructor& err) {
517 error << string_compose(_("Import: cannot open input sound file \"%1\""), (*p)) << endmsg;
518 status.done = status.cancel = true;
524 smf_reader.reset (new Evoral::SMF());
526 if (smf_reader->open(*p)) {
527 throw Evoral::SMF::FileError (*p);
530 if (smf_reader->is_type0 () && status.split_midi_channels) {
531 channels = smf_reader->channels().size();
533 channels = smf_reader->num_tracks();
534 switch (status.midi_track_name_source) {
538 smf_reader->track_names (smf_names);
540 case SMFInstrumentName:
541 smf_reader->instrument_names (smf_names);
546 error << _("Import: error opening MIDI file") << endmsg;
547 status.done = status.cancel = true;
553 error << _("Import: file contains no channels.") << endmsg;
557 vector<string> new_paths = get_paths_for_new_sources (status.replace_existing_source, *p, channels, smf_names);
559 samplepos_t natural_position = source ? source->natural_position() : 0;
562 if (status.replace_existing_source) {
563 fatal << "THIS IS NOT IMPLEMENTED YET, IT SHOULD NEVER GET CALLED!!! DYING!" << endmsg;
564 status.cancel = !map_existing_mono_sources (new_paths, *this, sample_rate(), newfiles, this);
566 status.cancel = !create_mono_sources_for_writing (new_paths, *this, sample_rate(), newfiles, natural_position);
569 // copy on cancel/failure so that any files that were created will be removed below
570 std::copy (newfiles.begin(), newfiles.end(), std::back_inserter(all_new_sources));
576 for (Sources::iterator i = newfiles.begin(); i != newfiles.end(); ++i) {
577 if ((afs = boost::dynamic_pointer_cast<AudioFileSource>(*i)) != 0) {
578 afs->prepare_for_peakfile_writes ();
582 if (source) { // audio
583 status.doing_what = compose_status_message (*p, source->samplerate(),
584 sample_rate(), status.current, status.total);
585 write_audio_data_to_new_files (source.get(), status, newfiles);
586 } else if (smf_reader) { // midi
587 status.doing_what = string_compose(_("Loading MIDI file %1"), *p);
588 write_midi_data_to_new_files (smf_reader.get(), status, newfiles, status.split_midi_channels);
595 if (!status.cancel) {
599 now = localtime (&xnow);
600 status.freeze = true;
602 /* flush the final length(s) to the header(s) */
604 for (Sources::iterator x = all_new_sources.begin(); x != all_new_sources.end(); ) {
606 if ((afs = boost::dynamic_pointer_cast<AudioFileSource>(*x)) != 0) {
607 afs->update_header((*x)->natural_position(), *now, xnow);
608 afs->done_with_peakfile_writes ();
610 /* now that there is data there, requeue the file for analysis */
612 if (Config->get_auto_analyse_audio()) {
613 Analyser::queue_source_for_analysis (boost::static_pointer_cast<Source>(*x), false);
617 /* imported, copied files cannot be written or removed
620 boost::shared_ptr<FileSource> fs = boost::dynamic_pointer_cast<FileSource>(*x);
622 /* Only audio files should be marked as
623 immutable - we may need to rewrite MIDI
626 if (boost::dynamic_pointer_cast<AudioFileSource> (fs)) {
627 fs->mark_immutable ();
629 fs->mark_immutable_except_write ();
631 fs->mark_nonremovable ();
634 /* don't create tracks for empty MIDI sources (channels) */
636 if ((smfs = boost::dynamic_pointer_cast<SMFSource>(*x)) != 0 && smfs->is_empty()) {
637 x = all_new_sources.erase(x);
643 /* save state so that we don't lose these new Sources */
647 std::copy (all_new_sources.begin(), all_new_sources.end(), std::back_inserter(status.sources));
650 std::for_each (all_new_sources.begin(), all_new_sources.end(), remove_file_source);
652 error << _("Failed to remove some files after failed/cancelled import operation") << endmsg;