Example #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Octave"/> with the
        /// specified octave number.
        /// </summary>
        /// <param name="octave">The number of an octave.</param>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="octave"/> is out of valid range.</exception>
        private Octave(int octave)
        {
            Debug.Assert(octave >= MinOctaveNumber && octave <= MaxOctaveNumber,
                         "An octave's number is out of range.");

            Number = octave;

            _notes = Enum.GetValues(typeof(NoteName))
                     .Cast <NoteName>()
                     .Where(n => NoteUtilities.IsNoteValid(n, octave))
                     .ToDictionary(n => n,
                                   n => Note.Get(n, octave));
        }
Example #2
0
        internal static ParsingResult TryParse(string input, out Note note)
        {
            note = null;

            if (string.IsNullOrWhiteSpace(input))
            {
                return(ParsingResult.EmptyInputString);
            }

            var match = ParsingUtilities.Match(input, Patterns);

            if (match == null)
            {
                return(ParsingResult.NotMatched);
            }

            var noteNameGroup = match.Groups[NoteNameGroupName];

            NoteName noteName;
            var      noteNameParsingResult = NoteNameParser.TryParse(noteNameGroup.Value, out noteName);

            if (noteNameParsingResult.Status != ParsingStatus.Parsed)
            {
                return(noteNameParsingResult);
            }

            int octaveNumber;

            if (!ParsingUtilities.ParseInt(match, OctaveGroupName, Octave.Middle.Number, out octaveNumber))
            {
                return(ParsingResult.Error(OctaveIsOutOfRange));
            }

            if (!NoteUtilities.IsNoteValid(noteName, octaveNumber))
            {
                return(ParsingResult.Error(NoteIsOutOfRange));
            }

            note = Note.Get(noteName, octaveNumber);
            return(ParsingResult.Parsed);
        }
Example #3
0
        /// <summary>
        /// Gets all notes that belong to a musical scale.
        /// </summary>
        /// <param name="scale"><see cref="Scale"/> to get notes of.</param>
        /// <returns>Notes that belong to the <paramref name="scale"/>.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="scale"/> is null.</exception>
        public static IEnumerable <Note> GetNotes(this Scale scale)
        {
            ThrowIfArgument.IsNull(nameof(scale), scale);

            int noteNumber = SevenBitNumber.Values
                             .SkipWhile(number => NoteUtilities.GetNoteName(number) != scale.RootNote)
                             .First();

            yield return(Note.Get((SevenBitNumber)noteNumber));

            while (true)
            {
                foreach (var interval in scale.Intervals)
                {
                    noteNumber += interval;
                    if (!NoteUtilities.IsNoteNumberValid(noteNumber))
                    {
                        yield break;
                    }

                    yield return(Note.Get((SevenBitNumber)noteNumber));
                }
            }
        }