MidiSheetMusic.MidiFile.RoundDurations C# (CSharp) Method

RoundDurations() public static method

public static RoundDurations ( List tracks, int quarternote ) : void
tracks List
quarternote int
return void
        public static void RoundDurations(List<MidiTrack> tracks, int quarternote)
        {
            foreach (MidiTrack track in tracks ) {
            MidiNote prevNote = null;
            for (int i = 0; i < track.Notes.Count-1; i++) {
                MidiNote note1 = track.Notes[i];
                if (prevNote == null) {
                    prevNote = note1;
                }

                /* Get the next note that has a different start time */
                MidiNote note2 = note1;
                for (int j = i+1; j < track.Notes.Count; j++) {
                    note2 = track.Notes[j];
                    if (note1.StartTime < note2.StartTime) {
                        break;
                    }
                }
                int maxduration = note2.StartTime - note1.StartTime;

                int dur = 0;
                if (quarternote <= maxduration)
                    dur = quarternote;
                else if (quarternote/2 <= maxduration)
                    dur = quarternote/2;
                else if (quarternote/3 <= maxduration)
                    dur = quarternote/3;
                else if (quarternote/4 <= maxduration)
                    dur = quarternote/4;

                if (dur < note1.Duration) {
                    dur = note1.Duration;
                }

                /* Special case: If the previous note's duration
                 * matches this note's duration, we can make a notepair.
                 * So don't expand the duration in that case.
                 */
                if ((prevNote.StartTime + prevNote.Duration == note1.StartTime) &&
                    (prevNote.Duration == note1.Duration)) {

                    dur = note1.Duration;
                }
                note1.Duration = dur;
                if (track.Notes[i+1].StartTime != note1.StartTime) {
                    prevNote = note1;
                }
            }
            }
        }