+ vector<string> new_paths;
+ const string basename = basename_nosuffix (import_file_path);
+
+ SessionDirectory sdir(session_dir);
+
+ for (uint n = 0; n < channels; ++n) {
+
+ const DataType type = SMFSource::safe_midi_file_extension (import_file_path) ? DataType::MIDI : DataType::AUDIO;
+
+ std::string filepath = (type == DataType::MIDI)
+ ? sdir.midi_path() : sdir.sound_path();
+
+ filepath = Glib::build_filename (filepath,
+ get_non_existent_filename (hf, type, allow_replacing, filepath, basename, n, channels));
+ new_paths.push_back (filepath);
+ }
+
+ return new_paths;
+}
+
+static bool
+map_existing_mono_sources (const vector<string>& new_paths, Session& /*sess*/,
+ uint /*samplerate*/, vector<boost::shared_ptr<Source> >& newfiles, Session *session)
+{
+ for (vector<string>::const_iterator i = new_paths.begin();
+ i != new_paths.end(); ++i)
+ {
+ boost::shared_ptr<Source> source = session->source_by_path_and_channel(*i, 0);
+
+ if (source == 0) {
+ error << string_compose(_("Could not find a source for %1 even though we are updating this file!"), (*i)) << endl;
+ return false;
+ }
+
+ newfiles.push_back(boost::dynamic_pointer_cast<Source>(source));
+ }
+ return true;
+}
+
+static bool
+create_mono_sources_for_writing (const vector<string>& new_paths,
+ Session& sess, uint samplerate,
+ vector<boost::shared_ptr<Source> >& newfiles,
+ framepos_t timeline_position)
+{
+ for (vector<string>::const_iterator i = new_paths.begin(); i != new_paths.end(); ++i) {
+
+ boost::shared_ptr<Source> source;
+
+ try {
+ const DataType type = SMFSource::safe_midi_file_extension (*i) ? DataType::MIDI : DataType::AUDIO;
+
+ source = SourceFactory::createWritable (type, sess,
+ i->c_str(),
+ false, // destructive
+ samplerate);
+ }
+
+ catch (const failed_constructor& err) {
+ error << string_compose (_("Unable to create file %1 during import"), *i) << endmsg;
+ return false;
+ }
+
+ 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;
+}
+
+static string
+compose_status_message (const string& path,
+ uint file_samplerate,
+ uint session_samplerate,
+ uint /* current_file */,
+ uint /* total_files */)
+{
+ if (file_samplerate != session_samplerate) {
+ return string_compose (_("Resampling %1 from %2kHz to %3kHz"),
+ Glib::path_get_basename (path),
+ file_samplerate/1000.0f,
+ session_samplerate/1000.0f);
+ }
+
+ return string_compose (_("Copying %1"), Glib::path_get_basename (path));
+}
+
+static void
+write_audio_data_to_new_files (ImportableSource* source, ImportStatus& status,
+ vector<boost::shared_ptr<Source> >& newfiles)
+{
+ const framecnt_t nframes = ResampledImportableSource::blocksize;
+ boost::shared_ptr<AudioFileSource> afs;