+ if ((fs = boost::dynamic_pointer_cast<FileSource> (siter->second)) == 0) {
+ continue;
+ }
+
+ if (!fs->destructive()) {
+ if (fs->empty() && !fs->used()) {
+ continue;
+ }
+ }
+
+ if (snapshot_type != NormalSave && fs->within_session ()) {
+ /* copy MIDI sources to new file
+ *
+ * We cannot replace the midi-source and MidiRegion::clobber_sources,
+ * because the GUI (midi_region) has a direct pointer to the midi-model
+ * of the source, as does UndoTransaction.
+ *
+ * On the upside, .mid files are not kept open. The file is only open
+ * when reading the model initially and when flushing the model to disk:
+ * source->session_saved () or export.
+ *
+ * We can change the _path of the existing source under the hood, keeping
+ * all IDs, references and pointers intact.
+ * */
+ boost::shared_ptr<SMFSource> ms;
+ if ((ms = boost::dynamic_pointer_cast<SMFSource> (siter->second)) != 0) {
+ const std::string ancestor_name = ms->ancestor_name();
+ const std::string base = PBD::basename_nosuffix(ancestor_name);
+ const string path = new_midi_source_path (base, false);
+
+ /* use SMF-API to clone data (use the midi_model, not data on disk) */
+ boost::shared_ptr<SMFSource> newsrc (new SMFSource (*this, path, SndFileSource::default_writable_flags));
+ Source::Lock lm (ms->mutex());
+
+ // TODO special-case empty, removable() files: just create a new removable.
+ // (load + write flushes the model and creates the file)
+ if (!ms->model()) {
+ ms->load_model (lm);
+ }
+ if (ms->write_to (lm, newsrc, Evoral::MinBeats, Evoral::MaxBeats)) {
+ error << string_compose (_("Session-Save: Failed to copy MIDI Source '%1' for snapshot"), ancestor_name) << endmsg;
+ } else {
+ if (snapshot_type == SnapshotKeep) {
+ /* keep working on current session.
+ *
+ * Save snapshot-state with the original filename.
+ * Switch to use new path for future saves of the main session.
+ */
+ child->add_child_nocopy (ms->get_state());
+ }