2 Copyright (C) 2000 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "libardour-config.h"
35 #include <samplerate.h>
37 #include "pbd/gstdio_compat.h"
40 #include <boost/scoped_array.hpp>
41 #include <boost/shared_array.hpp>
43 #include "pbd/basename.h"
44 #include "pbd/convert.h"
46 #include "evoral/SMF.hpp"
48 #include "ardour/analyser.h"
49 #include "ardour/ardour.h"
50 #include "ardour/audioengine.h"
51 #include "ardour/audioregion.h"
52 #include "ardour/import_status.h"
53 #include "ardour/region_factory.h"
54 #include "ardour/resampled_source.h"
55 #include "ardour/runtime_functions.h"
56 #include "ardour/session.h"
57 #include "ardour/session_directory.h"
58 #include "ardour/smf_source.h"
59 #include "ardour/sndfile_helpers.h"
60 #include "ardour/sndfileimportable.h"
61 #include "ardour/sndfilesource.h"
62 #include "ardour/source_factory.h"
63 #include "ardour/tempo.h"
66 #include "ardour/caimportable.h"
72 using namespace ARDOUR;
75 static boost::shared_ptr<ImportableSource>
76 open_importable_source (const string& path, framecnt_t samplerate, ARDOUR::SrcQuality quality)
78 /* try libsndfile first, because it can get BWF info from .wav, which ExtAudioFile cannot.
79 We don't necessarily need that information in an ImportableSource, but it keeps the
80 logic the same as in SourceFactory::create()
84 boost::shared_ptr<SndFileImportableSource> source(new SndFileImportableSource(path));
86 if (source->samplerate() == samplerate) {
90 /* rewrap as a resampled source */
92 return boost::shared_ptr<ImportableSource>(new ResampledImportableSource(source, samplerate, quality));
99 /* libsndfile failed, see if we can use CoreAudio to handle the IO */
101 CAImportableSource* src = new CAImportableSource(path);
102 boost::shared_ptr<CAImportableSource> source (src);
104 if (source->samplerate() == samplerate) {
108 /* rewrap as a resampled source */
110 return boost::shared_ptr<ImportableSource>(new ResampledImportableSource(source, samplerate, quality));
120 Session::get_paths_for_new_sources (bool /*allow_replacing*/, const string& import_file_path, uint32_t channels,
121 vector<string> const & smf_track_names)
124 vector<string> new_paths;
125 const string basename = basename_nosuffix (import_file_path);
127 for (uint32_t n = 0; n < channels; ++n) {
129 const DataType type = SMFSource::safe_midi_file_extension (import_file_path) ? DataType::MIDI : DataType::AUDIO;
134 assert (smf_track_names.empty() || smf_track_names.size() == channels);
137 if (smf_track_names.empty() || smf_track_names[n].empty()) {
138 mchn_name = string_compose ("%1-t%2", basename, n);
140 mchn_name = string_compose ("%1-%2", basename, smf_track_names[n]);
142 filepath = new_midi_source_path (mchn_name);
144 filepath = new_midi_source_path (basename);
147 case DataType::AUDIO:
148 filepath = new_audio_source_path (basename, channels, n, false, false);
152 if (filepath.empty()) {
153 error << string_compose (_("Cannot find new filename for imported file %1"), import_file_path) << endmsg;
154 return vector<string>();
157 new_paths.push_back (filepath);
164 map_existing_mono_sources (const vector<string>& new_paths, Session& /*sess*/,
165 uint32_t /*samplerate*/, vector<boost::shared_ptr<Source> >& newfiles, Session *session)
167 for (vector<string>::const_iterator i = new_paths.begin();
168 i != new_paths.end(); ++i)
170 boost::shared_ptr<Source> source = session->audio_source_by_path_and_channel(*i, 0);
173 error << string_compose(_("Could not find a source for %1 even though we are updating this file!"), (*i)) << endl;
177 newfiles.push_back(boost::dynamic_pointer_cast<Source>(source));
183 create_mono_sources_for_writing (const vector<string>& new_paths,
184 Session& sess, uint32_t samplerate,
185 vector<boost::shared_ptr<Source> >& newfiles,
186 framepos_t timeline_position)
188 for (vector<string>::const_iterator i = new_paths.begin(); i != new_paths.end(); ++i) {
190 boost::shared_ptr<Source> source;
193 const DataType type = SMFSource::safe_midi_file_extension (*i) ? DataType::MIDI : DataType::AUDIO;
195 source = SourceFactory::createWritable (type, sess,
197 false, // destructive
201 catch (const failed_constructor& err) {
202 error << string_compose (_("Unable to create file %1 during import"), *i) << endmsg;
206 newfiles.push_back(boost::dynamic_pointer_cast<Source>(source));
208 /* for audio files, reset the timeline position so that any BWF-ish
209 information in the original files we are importing from is maintained.
212 boost::shared_ptr<AudioFileSource> afs;
213 if ((afs = boost::dynamic_pointer_cast<AudioFileSource>(source)) != 0) {
214 afs->set_timeline_position(timeline_position);
221 compose_status_message (const string& path,
222 uint32_t file_samplerate,
223 uint32_t session_samplerate,
224 uint32_t /* current_file */,
225 uint32_t /* total_files */)
227 if (file_samplerate != session_samplerate) {
228 return string_compose (_("Resampling %1 from %2kHz to %3kHz"),
229 Glib::path_get_basename (path),
230 file_samplerate/1000.0f,
231 session_samplerate/1000.0f);
234 return string_compose (_("Copying %1"), Glib::path_get_basename (path));
238 write_audio_data_to_new_files (ImportableSource* source, ImportStatus& status,
239 vector<boost::shared_ptr<Source> >& newfiles)
241 const framecnt_t nframes = ResampledImportableSource::blocksize;
242 boost::shared_ptr<AudioFileSource> afs;
243 uint32_t channels = source->channels();
248 boost::scoped_array<float> data(new float[nframes * channels]);
249 vector<boost::shared_array<Sample> > channel_data;
251 for (uint32_t n = 0; n < channels; ++n) {
252 channel_data.push_back(boost::shared_array<Sample>(new Sample[nframes]));
257 boost::shared_ptr<AudioSource> s = boost::dynamic_pointer_cast<AudioSource> (newfiles[0]);
260 status.progress = 0.0f;
261 float progress_multiplier = 1;
262 float progress_base = 0;
264 if (!source->clamped_at_unity() && s->clamped_at_unity()) {
266 /* The source we are importing from can return sample values with a magnitude greater than 1,
267 and the file we are writing the imported data to cannot handle such values. Compute the gain
268 factor required to normalize the input sources to have a magnitude of less than 1.
272 uint32_t read_count = 0;
274 while (!status.cancel) {
275 framecnt_t const nread = source->read (data.get(), nframes * channels);
280 peak = compute_peak (data.get(), nread * channels, peak);
282 read_count += nread / channels;
283 status.progress = 0.5 * read_count / (source->ratio() * source->length() * channels);
287 /* we are out of range: compute a gain to fix it */
288 gain = (1 - FLT_EPSILON) / peak;
292 progress_multiplier = 0.5;
296 framecnt_t read_count = 0;
298 while (!status.cancel) {
300 framecnt_t nread, nfread;
304 if ((nread = source->read (data.get(), nframes * channels)) == 0) {
305 #ifdef PLATFORM_WINDOWS
306 /* Flush the data once we've finished importing the file. Windows can */
307 /* cache the data for very long periods of time (perhaps not writing */
308 /* it to disk until Ardour closes). So let's force it to flush now. */
309 for (chn = 0; chn < channels; ++chn)
310 if ((afs = boost::dynamic_pointer_cast<AudioFileSource>(newfiles[chn])) != 0)
317 /* here is the gain fix for out-of-range sample values that we computed earlier */
318 apply_gain_to_buffer (data.get(), nread, gain);
321 nfread = nread / channels;
325 for (chn = 0; chn < channels; ++chn) {
328 for (x = chn, n = 0; n < nfread; x += channels, ++n) {
329 channel_data[chn][n] = (Sample) data[x];
335 for (chn = 0; chn < channels; ++chn) {
336 if ((afs = boost::dynamic_pointer_cast<AudioFileSource>(newfiles[chn])) != 0) {
337 afs->write (channel_data[chn].get(), nfread);
342 status.progress = progress_base + progress_multiplier * read_count / (source->ratio () * source->length() * channels);
347 write_midi_data_to_new_files (Evoral::SMF* source, ImportStatus& status,
348 vector<boost::shared_ptr<Source> >& newfiles,
351 uint32_t buf_size = 4;
352 uint8_t* buf = (uint8_t*) malloc (buf_size);
354 status.progress = 0.0f;
356 bool type0 = source->is_type0 () && split_type0;
357 const std::set<uint8_t>& chn = source->channels ();
360 num_tracks = source->channels().size();
362 num_tracks = source->num_tracks();
364 assert (newfiles.size() == num_tracks);
367 vector<boost::shared_ptr<Source> >::iterator s = newfiles.begin();
368 std::set<uint8_t>::const_iterator cur_chan = chn.begin();
370 for (unsigned i = 1; i <= num_tracks; ++i) {
372 boost::shared_ptr<SMFSource> smfs = boost::dynamic_pointer_cast<SMFSource> (*s);
374 Glib::Threads::Mutex::Lock source_lock(smfs->mutex());
376 smfs->drop_model (source_lock);
378 source->seek_to_start ();
380 source->seek_to_track (i);
384 uint32_t delta_t = 0;
388 while (!status.cancel) {
389 gint note_id_ignored; // imported files either don't have NoteID's or we ignore them.
393 int ret = source->read_event (&delta_t, &size, &buf, ¬e_id_ignored);
395 if (size > buf_size) {
399 if (ret < 0) { // EOT
405 if (ret == 0) { // Meta
409 // type-0 files separate by channel
411 uint8_t type = buf[0] & 0xf0;
412 uint8_t chan = buf[0] & 0x0f;
413 if (type >= 0x80 && type <= 0xE0) {
414 if (chan != *cur_chan) {
421 smfs->mark_streaming_write_started (source_lock);
425 smfs->append_event_beats(
427 Evoral::Event<Evoral::Beats>(
429 Evoral::Beats::ticks_at_rate(t, source->ppqn()),
433 if (status.progress < 0.99) {
434 status.progress += 0.01;
440 /* we wrote something */
442 const framepos_t pos = 0;
443 const Evoral::Beats length_beats = Evoral::Beats::ticks_at_rate(t, source->ppqn());
444 BeatsFramesConverter converter(smfs->session().tempo_map(), pos);
445 smfs->update_length(pos + converter.to(length_beats.round_up_to_beat()));
446 smfs->mark_streaming_write_completed (source_lock);
452 info << string_compose (_("Track %1 of %2 contained no usable MIDI data"), i, num_tracks) << endmsg;
461 } catch (exception& e) {
462 error << string_compose (_("MIDI file could not be written (best guess: %1)"), e.what()) << endmsg;
471 remove_file_source (boost::shared_ptr<Source> source)
473 boost::shared_ptr<FileSource> fs = boost::dynamic_pointer_cast<FileSource> (source);
475 fs->DropReferences ();
478 ::g_unlink (fs->path().c_str());
482 // This function is still unable to cleanly update an existing source, even though
483 // it is possible to set the ImportStatus flag accordingly. The functinality
484 // is disabled at the GUI until the Source implementations are able to provide
485 // the necessary API.
487 Session::import_files (ImportStatus& status)
489 typedef vector<boost::shared_ptr<Source> > Sources;
490 Sources all_new_sources;
491 boost::shared_ptr<AudioFileSource> afs;
492 boost::shared_ptr<SMFSource> smfs;
493 uint32_t channels = 0;
494 vector<string> smf_names;
496 status.sources.clear ();
498 for (vector<string>::const_iterator p = status.paths.begin();
499 p != status.paths.end() && !status.cancel;
502 boost::shared_ptr<ImportableSource> source;
503 std::auto_ptr<Evoral::SMF> smf_reader;
504 const DataType type = SMFSource::safe_midi_file_extension (*p) ? DataType::MIDI : DataType::AUDIO;
506 if (type == DataType::AUDIO) {
508 source = open_importable_source (*p, frame_rate(), status.quality);
509 channels = source->channels();
510 } catch (const failed_constructor& err) {
511 error << string_compose(_("Import: cannot open input sound file \"%1\""), (*p)) << endmsg;
512 status.done = status.cancel = true;
518 smf_reader = std::auto_ptr<Evoral::SMF>(new Evoral::SMF());
520 if (smf_reader->open(*p)) {
521 throw Evoral::SMF::FileError (*p);
524 if (smf_reader->is_type0 () && status.split_midi_channels) {
525 channels = smf_reader->channels().size();
527 channels = smf_reader->num_tracks();
528 switch (status.midi_track_name_source) {
532 smf_reader->track_names (smf_names);
534 case SMFInstrumentName:
535 smf_reader->instrument_names (smf_names);
540 error << _("Import: error opening MIDI file") << endmsg;
541 status.done = status.cancel = true;
547 error << _("Import: file contains no channels.") << endmsg;
551 vector<string> new_paths = get_paths_for_new_sources (status.replace_existing_source, *p, channels, smf_names);
553 framepos_t natural_position = source ? source->natural_position() : 0;
556 if (status.replace_existing_source) {
557 fatal << "THIS IS NOT IMPLEMENTED YET, IT SHOULD NEVER GET CALLED!!! DYING!" << endmsg;
558 status.cancel = !map_existing_mono_sources (new_paths, *this, frame_rate(), newfiles, this);
560 status.cancel = !create_mono_sources_for_writing (new_paths, *this, frame_rate(), newfiles, natural_position);
563 // copy on cancel/failure so that any files that were created will be removed below
564 std::copy (newfiles.begin(), newfiles.end(), std::back_inserter(all_new_sources));
570 for (Sources::iterator i = newfiles.begin(); i != newfiles.end(); ++i) {
571 if ((afs = boost::dynamic_pointer_cast<AudioFileSource>(*i)) != 0) {
572 afs->prepare_for_peakfile_writes ();
576 if (source) { // audio
577 status.doing_what = compose_status_message (*p, source->samplerate(),
578 frame_rate(), status.current, status.total);
579 write_audio_data_to_new_files (source.get(), status, newfiles);
580 } else if (smf_reader.get()) { // midi
581 status.doing_what = string_compose(_("Loading MIDI file %1"), *p);
582 write_midi_data_to_new_files (smf_reader.get(), status, newfiles, status.split_midi_channels);
589 if (!status.cancel) {
593 now = localtime (&xnow);
594 status.freeze = true;
596 /* flush the final length(s) to the header(s) */
598 for (Sources::iterator x = all_new_sources.begin(); x != all_new_sources.end(); ) {
600 if ((afs = boost::dynamic_pointer_cast<AudioFileSource>(*x)) != 0) {
601 afs->update_header((*x)->natural_position(), *now, xnow);
602 afs->done_with_peakfile_writes ();
604 /* now that there is data there, requeue the file for analysis */
606 if (Config->get_auto_analyse_audio()) {
607 Analyser::queue_source_for_analysis (boost::static_pointer_cast<Source>(*x), false);
611 /* imported, copied files cannot be written or removed
614 boost::shared_ptr<FileSource> fs = boost::dynamic_pointer_cast<FileSource>(*x);
616 /* Only audio files should be marked as
617 immutable - we may need to rewrite MIDI
620 if (boost::dynamic_pointer_cast<AudioFileSource> (fs)) {
621 fs->mark_immutable ();
623 fs->mark_immutable_except_write ();
625 fs->mark_nonremovable ();
628 /* don't create tracks for empty MIDI sources (channels) */
630 if ((smfs = boost::dynamic_pointer_cast<SMFSource>(*x)) != 0 && smfs->is_empty()) {
631 x = all_new_sources.erase(x);
637 /* save state so that we don't lose these new Sources */
641 std::copy (all_new_sources.begin(), all_new_sources.end(), std::back_inserter(status.sources));
644 std::for_each (all_new_sources.begin(), all_new_sources.end(), remove_file_source);
646 error << _("Failed to remove some files after failed/cancelled import operation") << endmsg;