fix import of MIDI files
[ardour.git] / libs / ardour / import.cc
index d621afe2971ff9067d0336c77b5fcf33af67dc88..e05d162a568a6880f188dd36c67b7a51af944039 100644 (file)
 #include "ardour/tempo.h"
 
 #ifdef HAVE_COREAUDIO
-#ifdef USE_COREAUDIO_FOR_FILE_IO
 #include "ardour/caimportable.h"
 #endif
-#endif
 
 #include "i18n.h"
 
@@ -77,51 +75,45 @@ using namespace PBD;
 static boost::shared_ptr<ImportableSource>
 open_importable_source (const string& path, nframes_t samplerate, ARDOUR::SrcQuality quality)
 {
-#ifdef HAVE_COREAUDIO
-#ifdef USE_COREAUDIO_FOR_FILE_IO
-
-       /* see if we can use CoreAudio to handle the IO */
-
-       try {
-               CAImportableSource* src = new CAImportableSource(path);
-               boost::shared_ptr<CAImportableSource> source (src);
-
+       /* try libsndfile first, because it can get BWF info from .wav, which ExtAudioFile cannot.
+          We don't necessarily need that information in an ImportableSource, but it keeps the 
+          logic the same as in SourceFactory::create() 
+        */
+
+       try { 
+               boost::shared_ptr<SndFileImportableSource> source(new SndFileImportableSource(path));
+               
                if (source->samplerate() == samplerate) {
                        return source;
                }
-
+               
                /* rewrap as a resampled source */
-
+               
                return boost::shared_ptr<ImportableSource>(new ResampledImportableSource(source, samplerate, quality));
        }
 
        catch (...) {
 
-               /* fall back to SndFile */
-#endif
-#endif
-
-               try {
-                       boost::shared_ptr<SndFileImportableSource> source(new SndFileImportableSource(path));
-
-                       if (source->samplerate() == samplerate) {
-                               return source;
-                       }
-
-                       /* rewrap as a resampled source */
+#ifdef HAVE_COREAUDIO
 
-                       return boost::shared_ptr<ImportableSource>(new ResampledImportableSource(source, samplerate, quality));
+               /* libsndfile failed, see if we can use CoreAudio to handle the IO */
+               
+               CAImportableSource* src = new CAImportableSource(path);
+               boost::shared_ptr<CAImportableSource> source (src);
+               
+               if (source->samplerate() == samplerate) {
+                       return source;
                }
+               
+               /* rewrap as a resampled source */
+               
+               return boost::shared_ptr<ImportableSource>(new ResampledImportableSource(source, samplerate, quality));
 
-               catch (...) {
-                       throw; // rethrow
-               }
+#else
+               throw; // rethrow
+#endif
 
-#ifdef HAVE_COREAUDIO
-#ifdef USE_COREAUDIO_FOR_FILE_IO
        }
-#endif
-#endif
 }
 
 static std::string
@@ -213,7 +205,7 @@ map_existing_mono_sources (const vector<string>& new_paths, Session& /*sess*/,
 static bool
 create_mono_sources_for_writing (const vector<string>& new_paths, Session& sess,
                                 uint samplerate, vector<boost::shared_ptr<Source> >& newfiles,
-                                nframes64_t /*timeline_position*/)
+                                framepos_t timeline_position)
 {
        for (vector<string>::const_iterator i = new_paths.begin(); i != new_paths.end(); ++i)
        {
@@ -237,6 +229,15 @@ create_mono_sources_for_writing (const vector<string>& new_paths, Session& sess,
                }
 
                newfiles.push_back(boost::dynamic_pointer_cast<Source>(source));
+
+                /* for audio files, reset the timeline position so that any BWF-ish
+                   information in the original files we are importing from is maintained.
+                */
+
+                boost::shared_ptr<AudioFileSource> afs;
+                if ((afs = boost::dynamic_pointer_cast<AudioFileSource>(source)) != 0) {
+                        afs->set_timeline_position(timeline_position);
+                }
        }
        return true;
 }
@@ -315,7 +316,7 @@ write_audio_data_to_new_files (ImportableSource* source, ImportStatus& status,
 
 static void
 write_midi_data_to_new_files (Evoral::SMF* source, ImportStatus& status,
-               vector<boost::shared_ptr<Source> >& newfiles)
+                              vector<boost::shared_ptr<Source> >& newfiles)
 {
        uint32_t buf_size = 4;
        uint8_t* buf      = (uint8_t*)malloc(buf_size);
@@ -333,6 +334,7 @@ write_midi_data_to_new_files (Evoral::SMF* source, ImportStatus& status,
                uint64_t t       = 0;
                uint32_t delta_t = 0;
                uint32_t size    = 0;
+                bool first = true;
 
                while (!status.cancel) {
                        size = buf_size;
@@ -350,6 +352,11 @@ write_midi_data_to_new_files (Evoral::SMF* source, ImportStatus& status,
                        if (ret == 0) { // Meta
                                continue;
                        }
+                        
+                        if (first) {
+                                smfs->mark_streaming_write_started ();
+                                first = false;
+                        }
 
                        smfs->append_event_unlocked_beats(Evoral::Event<double>(0,
                                        (double)t / (double)source->ppqn(),
@@ -364,7 +371,7 @@ write_midi_data_to_new_files (Evoral::SMF* source, ImportStatus& status,
                const double length_beats = ceil(t / (double)source->ppqn());
                BeatsFramesConverter converter(smfs->session().tempo_map(), pos);
                smfs->update_length(pos, converter.to(length_beats));
-               smfs->end_write();
+               smfs->mark_streaming_write_completed ();
 
                if (status.cancel) {
                        break;
@@ -433,13 +440,13 @@ Session::import_audiofiles (ImportStatus& status)
                                                                      get_best_session_directory_for_new_source (),
                                                                      channels);
                Sources newfiles;
-               nframes64_t natural_position = source ? source->natural_position() : 0;
+               framepos_t natural_position = source ? source->natural_position() : 0;
 
                if (status.replace_existing_source) {
                        fatal << "THIS IS NOT IMPLEMENTED YET, IT SHOULD NEVER GET CALLED!!! DYING!" << endmsg;
                        status.cancel = !map_existing_mono_sources (new_paths, *this, frame_rate(), newfiles, this);
                } else {
-                       status.cancel = !create_mono_sources_for_writing (new_paths, *this, frame_rate(), newfiles, natural_position);
+                        status.cancel = !create_mono_sources_for_writing (new_paths, *this, frame_rate(), newfiles, natural_position);
                }
 
                // copy on cancel/failure so that any files that were created will be removed below
@@ -476,7 +483,7 @@ Session::import_audiofiles (ImportStatus& status)
 
                for (Sources::iterator x = all_new_sources.begin(); x != all_new_sources.end(); ) {
                        if ((afs = boost::dynamic_pointer_cast<AudioFileSource>(*x)) != 0) {
-                               afs->update_header(0, *now, xnow);
+                                afs->update_header((*x)->natural_position(), *now, xnow);
                                afs->done_with_peakfile_writes ();
 
                                /* now that there is data there, requeue the file for analysis */