Exemple #1
0
        public Note parse(string note, Duration duration)
        {
            if (note == "z")
            {
                return(new Note(Note.REST, duration * unitNoteLength * 4));
            }

            Accidental accidental   = Accidental.NONE;
            int        selectedNote = 0;
            int        pitch        = 0;

            //Parse accidental and remove it from the string
            if (note.Length > 1 && note.Substring(0, 2) == "__")
            {
                accidental = Accidental.DOUBLE_FLAT;
                note       = note.Substring(2);
            }
            else if (note.Length > 1 && note.Substring(0, 2) == "^^")
            {
                accidental = Accidental.DOUBLE_SHARP;
                note       = note.Substring(2);
            }
            else if (note[0] == '_')
            {
                accidental = Accidental.FLAT;
                note       = note.Substring(1);
            }
            else if (note[0] == '=')
            {
                accidental = Accidental.NATURAL;
                note       = note.Substring(1);
            }
            else if (note[0] == '^')
            {
                accidental = Accidental.SHARP;
                note       = note.Substring(1);
            }

            //Parse note
            char charnote = note[0];

            note = note.Substring(1);

            if (char.IsUpper(charnote))
            {
                charnote = char.ToLower(charnote);
                pitch   -= Note.PITCH_OCTAVE;
            }

            switch (charnote)
            {
            case 'f': pitch += (int)NoteFigure.F + C_5; selectedNote = 0; break;

            case 'c': pitch += (int)NoteFigure.C + C_5; selectedNote = 1; break;

            case 'g': pitch += (int)NoteFigure.G + C_5; selectedNote = 2; break;

            case 'd': pitch += (int)NoteFigure.D + C_5; selectedNote = 3; break;

            case 'a': pitch += (int)NoteFigure.A + C_5; selectedNote = 4; break;

            case 'e': pitch += (int)NoteFigure.E + C_5; selectedNote = 5; break;

            case 'b': pitch += (int)NoteFigure.B + C_5; selectedNote = 6; break;
            }

            //Update accidental
            if (accidental != Accidental.NONE)
            {
                currentAccidentals[selectedNote] = accidental;
            }

            //Update pitch
            switch (currentAccidentals[selectedNote])
            {
            case Accidental.DOUBLE_SHARP: pitch += 2; break;

            case Accidental.SHARP: pitch += 1; break;

            case Accidental.FLAT: pitch -= 1; break;

            case Accidental.DOUBLE_FLAT: pitch -= 2; break;
            }

            //Check tonality if no accidental applied
            if (currentAccidentals[selectedNote] == Accidental.NONE)
            {
                int tonalityAccidentals = tonality.getAccidentals();
                if (tonalityAccidentals > 0)
                {
                    if (tonalityAccidentals > selectedNote)
                    {
                        pitch++;
                    }
                }
                else if (tonalityAccidentals < 0)
                {
                    if (tonalityAccidentals + 7 <= selectedNote)
                    {
                        pitch--;
                    }
                }
            }

            //Octave
            for (int i = 0; i < note.Length; i++)
            {
                switch (note[i])
                {
                case ',': pitch -= Note.PITCH_OCTAVE; break;

                case '\'': pitch += Note.PITCH_OCTAVE; break;
                }
            }

            return(new Note(pitch, duration * unitNoteLength * 4));
        }
 public void setTonality(Tonality t)
 {
     this.accidentals = t.getAccidentals();
 }