From eec36feb110234ff16534a2b0725524a273e172d Mon Sep 17 00:00:00 2001 From: Robin Gareus Date: Thu, 15 Dec 2022 00:29:50 +0100 Subject: [PATCH] Fix importing MIDI files with duplicate track names --- libs/ardour/import.cc | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/libs/ardour/import.cc b/libs/ardour/import.cc index 122149025a..ac774bbada 100644 --- a/libs/ardour/import.cc +++ b/libs/ardour/import.cc @@ -598,6 +598,29 @@ Session::deinterlace_midi_region (boost::shared_ptr mr) } } +static vector +unique_track_names (const vector& n) +{ + set uniq; + vector rv; + + for (auto tn : n) { + while (uniq.find (tn) != uniq.end()) { + if (tn.empty ()) { + tn = "MIDI"; + } + /* not not use '-' as separator because that is used by + * new_midi_source_path, new_audio_source_path + * when checking for existing files. + */ + tn = bump_name_once (tn, '.'); + } + uniq.insert (tn); + rv.push_back (tn); + } + return rv; +} + // This function is still unable to cleanly update an existing source, even though // it is possible to set the ImportStatus flag accordingly. The functionality // is disabled at the GUI until the Source implementations are able to provide @@ -669,11 +692,14 @@ Session::import_files (ImportStatus& status) if (status.split_midi_channels) { vector temp; smf_reader->track_names (temp); + temp = unique_track_names (temp); for (uint32_t i = 0; itrack_names (smf_names); + vector temp; + smf_reader->track_names (temp); + smf_names = unique_track_names (temp); } break; case SMFInstrumentName: