1a83bd39d7492e919e3dc36927a8984f4531abf4
[ardour.git] / libs / ardour / import.cc
1 /*
2   Copyright (C) 2000 Paul Davis
3
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.
8
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.
13
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.
17
18 */
19
20 #ifdef WAF_BUILD
21 #include "libardour-config.h"
22 #endif
23
24 #include <cstdio>
25 #include <cstdlib>
26 #include <string>
27 #include <climits>
28 #include <cerrno>
29 #include <unistd.h>
30 #include <sys/stat.h>
31 #include <time.h>
32 #include <stdint.h>
33
34 #include <sndfile.h>
35 #include <samplerate.h>
36
37 #include "pbd/gstdio_compat.h"
38 #include <glibmm.h>
39
40 #include <boost/scoped_array.hpp>
41 #include <boost/shared_array.hpp>
42
43 #include "pbd/basename.h"
44 #include "pbd/convert.h"
45
46 #include "evoral/SMF.hpp"
47
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"
64
65 #ifdef HAVE_COREAUDIO
66 #include "ardour/caimportable.h"
67 #endif
68
69 #include "pbd/i18n.h"
70
71 using namespace std;
72 using namespace ARDOUR;
73 using namespace PBD;
74
75 static boost::shared_ptr<ImportableSource>
76 open_importable_source (const string& path, samplecnt_t samplerate, ARDOUR::SrcQuality quality)
77 {
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()
81         */
82
83         try {
84                 boost::shared_ptr<SndFileImportableSource> source(new SndFileImportableSource(path));
85
86                 if (source->samplerate() == samplerate) {
87                         return source;
88                 }
89
90                 /* rewrap as a resampled source */
91
92                 return boost::shared_ptr<ImportableSource>(new ResampledImportableSource(source, samplerate, quality));
93         }
94
95         catch (...) {
96
97 #ifdef HAVE_COREAUDIO
98
99                 /* libsndfile failed, see if we can use CoreAudio to handle the IO */
100
101                 CAImportableSource* src = new CAImportableSource(path);
102                 boost::shared_ptr<CAImportableSource> source (src);
103
104                 if (source->samplerate() == samplerate) {
105                         return source;
106                 }
107
108                 /* rewrap as a resampled source */
109
110                 return boost::shared_ptr<ImportableSource>(new ResampledImportableSource(source, samplerate, quality));
111
112 #else
113                 throw; // rethrow
114 #endif
115
116         }
117 }
118
119 vector<string>
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)
122
123 {
124         vector<string> new_paths;
125         const string basename = basename_nosuffix (import_file_path);
126
127         for (uint32_t n = 0; n < channels; ++n) {
128
129                 const DataType type = SMFSource::safe_midi_file_extension (import_file_path) ? DataType::MIDI : DataType::AUDIO;
130                 string filepath;
131
132                 switch (type) {
133                 case DataType::MIDI:
134                         assert (smf_track_names.empty() || smf_track_names.size() == channels);
135                         if (channels > 1) {
136                                 string mchn_name;
137                                 if (smf_track_names.empty() || smf_track_names[n].empty()) {
138                                         mchn_name = string_compose ("%1-t%2", basename, n);
139                                 } else {
140                                         mchn_name = string_compose ("%1-%2", basename, smf_track_names[n]);
141                                 }
142                                 filepath = new_midi_source_path (mchn_name);
143                         } else {
144                                 filepath = new_midi_source_path (basename);
145                         }
146                         break;
147                 case DataType::AUDIO:
148                         filepath = new_audio_source_path (basename, channels, n, false, false);
149                         break;
150                 }
151
152                 if (filepath.empty()) {
153                         error << string_compose (_("Cannot find new filename for imported file %1"), import_file_path) << endmsg;
154                         return vector<string>();
155                 }
156
157                 new_paths.push_back (filepath);
158         }
159
160         return new_paths;
161 }
162
163 static bool
164 map_existing_mono_sources (const vector<string>& new_paths, Session& /*sess*/,
165                            uint32_t /*samplerate*/, vector<boost::shared_ptr<Source> >& newfiles, Session *session)
166 {
167         for (vector<string>::const_iterator i = new_paths.begin();
168              i != new_paths.end(); ++i)
169         {
170                 boost::shared_ptr<Source> source = session->audio_source_by_path_and_channel(*i, 0);
171
172                 if (source == 0) {
173                         error << string_compose(_("Could not find a source for %1 even though we are updating this file!"), (*i)) << endl;
174                         return false;
175                 }
176
177                 newfiles.push_back(boost::dynamic_pointer_cast<Source>(source));
178         }
179         return true;
180 }
181
182 static bool
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                                  samplepos_t timeline_position)
187 {
188         for (vector<string>::const_iterator i = new_paths.begin(); i != new_paths.end(); ++i) {
189
190                 boost::shared_ptr<Source> source;
191
192                 try {
193                         const DataType type = SMFSource::safe_midi_file_extension (*i) ? DataType::MIDI : DataType::AUDIO;
194
195                         source = SourceFactory::createWritable (type, sess,
196                                                                 i->c_str(),
197                                                                 false, // destructive
198                                                                 samplerate);
199                 }
200
201                 catch (const failed_constructor& err) {
202                         error << string_compose (_("Unable to create file %1 during import"), *i) << endmsg;
203                         return false;
204                 }
205
206                 newfiles.push_back(boost::dynamic_pointer_cast<Source>(source));
207
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.
210                 */
211
212                 boost::shared_ptr<AudioFileSource> afs;
213                 if ((afs = boost::dynamic_pointer_cast<AudioFileSource>(source)) != 0) {
214                         afs->set_timeline_position(timeline_position);
215                 }
216         }
217         return true;
218 }
219
220 static string
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 */)
226 {
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);
232         }
233
234         return string_compose (_("Copying %1"), Glib::path_get_basename (path));
235 }
236
237 static void
238 write_audio_data_to_new_files (ImportableSource* source, ImportStatus& status,
239                                vector<boost::shared_ptr<Source> >& newfiles)
240 {
241         const samplecnt_t nframes = ResampledImportableSource::blocksize;
242         boost::shared_ptr<AudioFileSource> afs;
243         uint32_t channels = source->channels();
244         if (channels == 0) {
245                 return;
246         }
247
248         boost::scoped_array<float> data(new float[nframes * channels]);
249         vector<boost::shared_array<Sample> > channel_data;
250
251         for (uint32_t n = 0; n < channels; ++n) {
252                 channel_data.push_back(boost::shared_array<Sample>(new Sample[nframes]));
253         }
254
255         float gain = 1;
256
257         boost::shared_ptr<AudioSource> s = boost::dynamic_pointer_cast<AudioSource> (newfiles[0]);
258         assert (s);
259
260         status.progress = 0.0f;
261         float progress_multiplier = 1;
262         float progress_base = 0;
263
264         if (!source->clamped_at_unity() && s->clamped_at_unity()) {
265
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.
269                 */
270
271                 float peak = 0;
272                 uint32_t read_count = 0;
273
274                 while (!status.cancel) {
275                         samplecnt_t const nread = source->read (data.get(), nframes * channels);
276                         if (nread == 0) {
277                                 break;
278                         }
279
280                         peak = compute_peak (data.get(), nread * channels, peak);
281
282                         read_count += nread / channels;
283                         status.progress = 0.5 * read_count / (source->ratio() * source->length() * channels);
284                 }
285
286                 if (peak >= 1) {
287                         /* we are out of range: compute a gain to fix it */
288                         gain = (1 - FLT_EPSILON) / peak;
289                 }
290
291                 source->seek (0);
292                 progress_multiplier = 0.5;
293                 progress_base = 0.5;
294         }
295
296         samplecnt_t read_count = 0;
297
298         while (!status.cancel) {
299
300                 samplecnt_t nread, nfread;
301                 uint32_t x;
302                 uint32_t chn;
303
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)
311                                         afs->flush ();
312 #endif
313                         break;
314                 }
315
316                 if (gain != 1) {
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);
319                 }
320
321                 nfread = nread / channels;
322
323                 /* de-interleave */
324
325                 for (chn = 0; chn < channels; ++chn) {
326
327                         samplecnt_t n;
328                         for (x = chn, n = 0; n < nfread; x += channels, ++n) {
329                                 channel_data[chn][n] = (Sample) data[x];
330                         }
331                 }
332
333                 /* flush to disk */
334
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);
338                         }
339                 }
340
341                 read_count += nread;
342                 status.progress = progress_base + progress_multiplier * read_count / (source->ratio () * source->length() * channels);
343         }
344 }
345
346 static void
347 write_midi_data_to_new_files (Evoral::SMF* source, ImportStatus& status,
348                               vector<boost::shared_ptr<Source> >& newfiles,
349                               bool split_type0)
350 {
351         uint32_t buf_size = 4;
352         uint8_t* buf      = (uint8_t*) malloc (buf_size);
353
354         status.progress = 0.0f;
355         uint16_t num_tracks;
356         bool type0 = source->is_type0 () && split_type0;
357         const std::set<uint8_t>& chn = source->channels ();
358
359         if (type0) {
360                 num_tracks = source->channels().size();
361         } else {
362                 num_tracks = source->num_tracks();
363         }
364         assert (newfiles.size() == num_tracks);
365
366         try {
367                 vector<boost::shared_ptr<Source> >::iterator s = newfiles.begin();
368                 std::set<uint8_t>::const_iterator cur_chan = chn.begin();
369
370                 for (unsigned i = 1; i <= num_tracks; ++i) {
371
372                         boost::shared_ptr<SMFSource> smfs = boost::dynamic_pointer_cast<SMFSource> (*s);
373
374                         Glib::Threads::Mutex::Lock source_lock(smfs->mutex());
375
376                         smfs->drop_model (source_lock);
377                         if (type0) {
378                                 source->seek_to_start ();
379                         } else {
380                                 source->seek_to_track (i);
381                         }
382
383                         uint64_t t       = 0;
384                         uint32_t delta_t = 0;
385                         uint32_t size    = 0;
386                         bool first = true;
387
388                         while (!status.cancel) {
389                                 gint note_id_ignored; // imported files either don't have NoteID's or we ignore them.
390
391                                 size = buf_size;
392
393                                 int ret = source->read_event (&delta_t, &size, &buf, &note_id_ignored);
394
395                                 if (size > buf_size) {
396                                         buf_size = size;
397                                 }
398
399                                 if (ret < 0) { // EOT
400                                         break;
401                                 }
402
403                                 t += delta_t;
404
405                                 if (ret == 0) { // Meta
406                                         continue;
407                                 }
408
409                                 // type-0 files separate by channel
410                                 if (type0) {
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) {
415                                                         continue;
416                                                 }
417                                         }
418                                 }
419
420                                 if (first) {
421                                         smfs->mark_streaming_write_started (source_lock);
422                                         first = false;
423                                 }
424
425                                 smfs->append_event_beats(
426                                         source_lock,
427                                         Evoral::Event<Evoral::Beats>(
428                                                 Evoral::MIDI_EVENT,
429                                                 Evoral::Beats::ticks_at_rate(t, source->ppqn()),
430                                                 size,
431                                                 buf));
432
433                                 if (status.progress < 0.99) {
434                                         status.progress += 0.01;
435                                 }
436                         }
437
438                         if (!first) {
439
440                                 /* we wrote something */
441
442                                 const samplepos_t     pos          = 0;
443                                 const Evoral::Beats  length_beats = Evoral::Beats::ticks_at_rate(t, source->ppqn());
444                                 BeatsSamplesConverter 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);
447
448                                 if (status.cancel) {
449                                         break;
450                                 }
451                         } else {
452                                 info << string_compose (_("Track %1 of %2 contained no usable MIDI data"), i, num_tracks) << endmsg;
453                         }
454
455                         ++s; // next source
456                         if (type0) {
457                                 ++cur_chan;
458                         }
459                 }
460
461         } catch (exception& e) {
462                 error << string_compose (_("MIDI file could not be written (best guess: %1)"), e.what()) << endmsg;
463         }
464
465         if (buf) {
466                 free (buf);
467         }
468 }
469
470 static void
471 remove_file_source (boost::shared_ptr<Source> source)
472 {
473         boost::shared_ptr<FileSource> fs = boost::dynamic_pointer_cast<FileSource> (source);
474
475         fs->DropReferences ();
476
477         if (fs) {
478                 ::g_unlink (fs->path().c_str());
479         }
480 }
481
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.
486 void
487 Session::import_files (ImportStatus& status)
488 {
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;
495
496         status.sources.clear ();
497
498         for (vector<string>::const_iterator p = status.paths.begin();
499              p != status.paths.end() && !status.cancel;
500              ++p)
501         {
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;
505
506                 if (type == DataType::AUDIO) {
507                         try {
508                                 source = open_importable_source (*p, sample_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;
513                                 return;
514                         }
515
516                 } else {
517                         try {
518                                 smf_reader = std::auto_ptr<Evoral::SMF>(new Evoral::SMF());
519
520                                 if (smf_reader->open(*p)) {
521                                         throw Evoral::SMF::FileError (*p);
522                                 }
523
524                                 if (smf_reader->is_type0 () && status.split_midi_channels) {
525                                         channels = smf_reader->channels().size();
526                                 } else {
527                                         channels = smf_reader->num_tracks();
528                                         switch (status.midi_track_name_source) {
529                                         case SMFTrackNumber:
530                                                 break;
531                                         case SMFTrackName:
532                                                 smf_reader->track_names (smf_names);
533                                                 break;
534                                         case SMFInstrumentName:
535                                                 smf_reader->instrument_names (smf_names);
536                                                 break;
537                                         }
538                                 }
539                         } catch (...) {
540                                 error << _("Import: error opening MIDI file") << endmsg;
541                                 status.done = status.cancel = true;
542                                 return;
543                         }
544                 }
545
546                 if (channels == 0) {
547                         error << _("Import: file contains no channels.") << endmsg;
548                         continue;
549                 }
550
551                 vector<string> new_paths = get_paths_for_new_sources (status.replace_existing_source, *p, channels, smf_names);
552                 Sources newfiles;
553                 samplepos_t natural_position = source ? source->natural_position() : 0;
554
555
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, sample_rate(), newfiles, this);
559                 } else {
560                         status.cancel = !create_mono_sources_for_writing (new_paths, *this, sample_rate(), newfiles, natural_position);
561                 }
562
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));
565
566                 if (status.cancel) {
567                         break;
568                 }
569
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 ();
573                         }
574                 }
575
576                 if (source) { // audio
577                         status.doing_what = compose_status_message (*p, source->samplerate(),
578                                                                     sample_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);
583                 }
584
585                 ++status.current;
586                 status.progress = 0;
587         }
588
589         if (!status.cancel) {
590                 struct tm* now;
591                 time_t xnow;
592                 time (&xnow);
593                 now = localtime (&xnow);
594                 status.freeze = true;
595
596                 /* flush the final length(s) to the header(s) */
597
598                 for (Sources::iterator x = all_new_sources.begin(); x != all_new_sources.end(); ) {
599
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 ();
603
604                                 /* now that there is data there, requeue the file for analysis */
605
606                                 if (Config->get_auto_analyse_audio()) {
607                                         Analyser::queue_source_for_analysis (boost::static_pointer_cast<Source>(*x), false);
608                                 }
609                         }
610
611                         /* imported, copied files cannot be written or removed
612                          */
613
614                         boost::shared_ptr<FileSource> fs = boost::dynamic_pointer_cast<FileSource>(*x);
615                         if (fs) {
616                                 /* Only audio files should be marked as
617                                    immutable - we may need to rewrite MIDI
618                                    files at any time.
619                                 */
620                                 if (boost::dynamic_pointer_cast<AudioFileSource> (fs)) {
621                                         fs->mark_immutable ();
622                                 } else {
623                                         fs->mark_immutable_except_write ();
624                                 }
625                                 fs->mark_nonremovable ();
626                         }
627
628                         /* don't create tracks for empty MIDI sources (channels) */
629
630                         if ((smfs = boost::dynamic_pointer_cast<SMFSource>(*x)) != 0 && smfs->is_empty()) {
631                                 x = all_new_sources.erase(x);
632                         } else {
633                                 ++x;
634                         }
635                 }
636
637                 /* save state so that we don't lose these new Sources */
638
639                 save_state (_name);
640
641                 std::copy (all_new_sources.begin(), all_new_sources.end(), std::back_inserter(status.sources));
642         } else {
643                 try {
644                         std::for_each (all_new_sources.begin(), all_new_sources.end(), remove_file_source);
645                 } catch (...) {
646                         error << _("Failed to remove some files after failed/cancelled import operation") << endmsg;
647                 }
648
649         }
650
651         status.done = true;
652 }
653