public List<int> GuessMeasureLength()
{
List<int> result = new List<int>();
int pulses_per_second = (int) (1000000.0 / timesig.Tempo * timesig.Quarter);
int minmeasure = pulses_per_second / 2; /* The minimum measure length in pulses */
int maxmeasure = pulses_per_second * 4; /* The maximum measure length in pulses */
/* Get the start time of the first note in the midi file. */
int firstnote = timesig.Measure * 5;
foreach (MidiTrack track in tracks) {
if (firstnote > track.Notes[0].StartTime) {
firstnote = track.Notes[0].StartTime;
}
}
/* interval = 0.06 seconds, converted into pulses */
int interval = timesig.Quarter * 60000 / timesig.Tempo;
foreach (MidiTrack track in tracks) {
int prevtime = 0;
foreach (MidiNote note in track.Notes) {
if (note.StartTime - prevtime <= interval)
continue;
prevtime = note.StartTime;
int time_from_firstnote = note.StartTime - firstnote;
/* Round the time down to a multiple of 4 */
time_from_firstnote = time_from_firstnote / 4 * 4;
if (time_from_firstnote < minmeasure)
continue;
if (time_from_firstnote > maxmeasure)
break;
if (!result.Contains(time_from_firstnote)) {
result.Add(time_from_firstnote);
}
}
}
result.Sort();
return result;
}