Example #1
0
 /// <summary>
 /// Transposes the key by a given amount of semitones.
 /// </summary>
 /// <param name="amount">The number of semitones.</param>
 /// <returns>The transposed key.</returns>
 public Key Transpose(int amount)
 {
     return(new Key {
         Note = Note.Transpose(amount), IsMinor = IsMinor
     });
 }
Example #2
0
 /// <summary>
 /// Returns a <see cref="System.String"/> that represents this instance.
 /// </summary>
 /// <returns>
 /// The name of the base note with an 'm' appended if it's a minor key.
 /// </returns>
 public override string ToString()
 {
     return(Note.ToString(this) + (IsMinor ? "m" : String.Empty));
 }
Example #3
0
        /// <summary>
        /// Parses a string in order to find the name of a note.
        /// </summary>
        /// <param name="str">The string to parse.</param>
        /// <param name="rest">Will be assigned the rest of the string after the parsed note.</param>
        /// <param name="position">Will be assigned the position at which the note name starts in the original string.</param>
        /// <returns>The parsed note or <c>null</c> if no note was found in the string.</returns>
        public static Note Parse(string str, out string rest, out int position)
        {
            if (string.IsNullOrEmpty(str))
            {
                rest = null;
                position = 0;
                return null;
            }

            char? next = str.Length > 1 ? str[1] : (char?)null;

            int? semitones = null;
            bool wasFlat = false;
            int i = 0;

            while (!semitones.HasValue && i < str.Length)
            {
                switch (str[i])
                {
                    case 'C':
                        semitones = 0; break;
                    case 'D':
                        semitones = 2; break;
                    case 'E':
                        semitones = 4; break;
                    case 'F':
                        semitones = 5; break;
                    case 'G':
                        semitones = 7; break;
                    case 'A':
                        semitones = 9; break;
                    case 'H':
                        semitones = 11; break;
                    case 'B':
                        semitones = (Chords.GermanNotation && next != 'b' && next != '♭') ? 10 : 11; break;
                }

                i++;
            }

            if (!semitones.HasValue)
            {
                rest = null;
                position = 0;
                return null;
            }
            else
            {
                position = i - 1;
            }

            if (str.Length > i)
            {
                if (str[i] == '#' || str[i] == '♯')
                {
                    semitones++;
                    i++;
                }
                else if (str[i] == 'b' || str[i] == '♭')
                {
                    wasFlat = true;
                    semitones--;
                    i++;
                }
                else if (str[i] == 's' && (semitones == 4 || semitones == 9)) // Es and As
                {
                    semitones--;
                    i++;
                }
                else if (str.Length > i + 1)
                {
                    var germanSign = str.Substring(i, 2);
                    if (germanSign == "is")
                    {
                        semitones++;
                        i += 2;
                    }
                    else if (germanSign == "es")
                    {
                        semitones--;
                        i += 2;
                    }
                }
            }

            Note n = new Note(semitones.Value) { WasFlat = wasFlat };

            rest = str.Substring(i);

            return n;
        }