+ uint32_t buf_size = 4;
+ uint8_t* buf = (uint8_t*) malloc (buf_size);
+
+ status.progress = 0.0f;
+
+ assert (newfiles.size() == source->num_tracks());
+
+ try {
+ vector<boost::shared_ptr<Source> >::iterator s = newfiles.begin();
+
+ for (unsigned i = 1; i <= source->num_tracks(); ++i) {
+
+ boost::shared_ptr<SMFSource> smfs = boost::dynamic_pointer_cast<SMFSource> (*s);
+
+ smfs->drop_model ();
+ source->seek_to_track (i);
+
+ uint64_t t = 0;
+ uint32_t delta_t = 0;
+ uint32_t size = 0;
+ bool first = true;
+
+ while (!status.cancel) {
+ gint note_id_ignored; // imported files either don't have NoteID's or we ignore them.
+
+ size = buf_size;
+
+ int ret = source->read_event (&delta_t, &size, &buf, ¬e_id_ignored);
+
+ if (size > buf_size) {
+ buf_size = size;
+ }
+
+ if (ret < 0) { // EOT
+ break;
+ }
+
+ t += delta_t;
+
+ 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(),
+ size,
+ buf));
+
+ if (status.progress < 0.99) {
+ status.progress += 0.01;
+ }
+ }
+
+ if (!first) {
+
+ /* we wrote something */
+
+ const framepos_t pos = 0;
+ 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->mark_streaming_write_completed ();
+
+ if (status.cancel) {
+ break;
+ }
+ } else {
+ warning << string_compose (_("Track %1 of %2 contained no usable MIDI data"), i, source->file_path()) << endmsg;
+ }
+
+ ++s; // next source
+ }
+
+ } catch (...) {
+ error << string_compose (_("MIDI file %1 was not readable (no reason available"), source->file_path()) << endmsg;
+ }
+
+ if (buf) {
+ free (buf);
+ }