MidiSheetMusic.ClefMeasures.ClefMeasures C# (CSharp) Method

ClefMeasures() public method

public ClefMeasures ( List notes, int measurelen ) : System
notes List
measurelen int
return System
        public ClefMeasures(List<MidiNote> notes, int measurelen)
        {
            measure = measurelen;
            Clef mainclef = MainClef(notes);
            int nextmeasure = measurelen;
            int pos = 0;
            Clef clef = mainclef;

            clefs = new List<Clef>();

            while (pos < notes.Count) {
            /* Sum all the notes in the current measure */
            int sumnotes = 0;
            int notecount = 0;
            while (pos < notes.Count && notes[pos].StartTime < nextmeasure) {
                sumnotes += notes[pos].Number;
                notecount++;
                pos++;
            }
            if (notecount == 0)
                notecount = 1;

            /* Calculate the "average" note in the measure */
            int avgnote = sumnotes / notecount;
            if (avgnote == 0) {
                /* This measure doesn't contain any notes.
                 * Keep the previous clef.
                 */
            }
            else if (avgnote >= WhiteNote.BottomTreble.Number()) {
                clef = Clef.Treble;
            }
            else if (avgnote <= WhiteNote.TopBass.Number()) {
                clef = Clef.Bass;
            }
            else {
                /* The average note is between G3 and F4. We can use either
                 * the treble or bass clef.  Use the "main" clef, the clef
                 * that appears most for this track.
                 */
                clef = mainclef;
            }

            clefs.Add(clef);
            nextmeasure += measurelen;
            }
            clefs.Add(clef);
        }