public static void RoundStartTimes(List<MidiTrack> tracks, int millisec, TimeSignature time)
{
/* Get all the starttimes in all tracks, in sorted order */
List<int> starttimes = new List<int>();
foreach (MidiTrack track in tracks) {
foreach (MidiNote note in track.Notes) {
starttimes.Add( note.StartTime );
}
}
starttimes.Sort();
/* Notes within "millisec" milliseconds apart will be combined. */
int interval = time.Quarter * millisec * 1000 / time.Tempo;
/* If two starttimes are within interval millisec, make them the same */
for (int i = 0; i < starttimes.Count - 1; i++) {
if (starttimes[i+1] - starttimes[i] <= interval) {
starttimes[i+1] = starttimes[i];
}
}
CheckStartTimes(tracks);
/* Adjust the note starttimes, so that it matches one of the starttimes values */
foreach (MidiTrack track in tracks) {
int i = 0;
foreach (MidiNote note in track.Notes) {
while (i < starttimes.Count &&
note.StartTime - interval > starttimes[i]) {
i++;
}
if (note.StartTime > starttimes[i] &&
note.StartTime - starttimes[i] <= interval) {
note.StartTime = starttimes[i];
}
}
track.Notes.Sort(track.Notes[0]);
}
}