예제 #1
0
        public Note(int scaleDegree, int octave, MidiOctaveFormat format)
        {
            if (octave < -2)
            {
                throw new OctaveValueException(octave);
            }

            int octaveModifier = format == MidiOctaveFormat.Standard ? 2 : 1;

            this.value    = PitchScaler.Scale(scaleDegree) + (Interval.Octave.Semitones * (octaveModifier + octave));
            this.Duration = 1;
        }
예제 #2
0
        public static Note FromName(string note, MidiOctaveFormat format)
        {
            const int MinimumTextLength = 2;

            if (String.IsNullOrEmpty(note) || note.Length < MinimumTextLength)
            {
                throw new NoteFormatException("Note text is too short");
            }

            if (!Char.IsLetter(note.First()))
            {
                throw new NoteFormatException("Note must start with a letter");
            }

            Accidental accidental = Accidental.FromString(note.Substring(1, 1));

            int NamePartLength = 1;

            if (accidental != null)
            {
                NamePartLength = 2;
            }

            string scaleDegreeText = note.Substring(0, NamePartLength).ToUpper();

            int scaleDegree = 0;

            try
            {
                scaleDegree = NoteValues[scaleDegreeText];
            }
            catch (KeyNotFoundException e)
            {
                throw new NoteFormatException(scaleDegreeText, e);
            }

            string octaveText  = note.Substring(NamePartLength);
            int    octaveValue = Convert.ToInt32(octaveText, CultureInfo.CurrentCulture);

            return(new Note(scaleDegree, octaveValue, format));
        }
예제 #3
0
        /// <summary>
        /// Calculates which octave a value belongs to.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="format"></param>
        /// <returns></returns>
        public static int OctaveOf(int value, MidiOctaveFormat format)
        {
            int octaveModifier = format == MidiOctaveFormat.Standard ? 2 : 1;

            return((value / 12) - octaveModifier);
        }