Пример #1
0
        private static ParsingResult TryParseChordIntervals(Match match, NoteName rootNoteName, out Chord chord)
        {
            chord = null;

            var intervalGroup           = match.Groups[IntervalGroupName];
            var intervalsParsingResults = intervalGroup
                                          .Captures
                                          .OfType <Capture>()
                                          .Select(c =>
            {
                Interval interval;
                var parsingResult = IntervalParser.TryParse(c.Value, out interval);

                return(new
                {
                    Interval = interval,
                    ParsingResult = parsingResult
                });
            })
                                          .ToArray();

            var notParsedResult = intervalsParsingResults.FirstOrDefault(r => r.ParsingResult.Status != ParsingStatus.Parsed);

            if (notParsedResult != null)
            {
                return(notParsedResult.ParsingResult);
            }

            var intervals = intervalsParsingResults.Select(r => r.Interval).ToArray();

            chord = new Chord(rootNoteName, intervals);
            return(ParsingResult.Parsed);
        }
Пример #2
0
        /// <summary>
        /// Converts the string representation of a musical interval to its <see cref="Scale"/> equivalent.
        /// </summary>
        /// <param name="input">A string containing an interval to convert.</param>
        /// <returns>A <see cref="Scale"/> equivalent to the musical interval contained in <paramref name="input"/>.</returns>
        /// <exception cref="ArgumentException"><paramref name="input"/> is null or contains white-spaces only.</exception>
        /// <exception cref="FormatException"><paramref name="input"/> has invalid format.</exception>
        public static Interval Parse(string input)
        {
            Interval interval;
            var      parsingResult = IntervalParser.TryParse(input, out interval);

            if (parsingResult.Status == ParsingStatus.Parsed)
            {
                return(interval);
            }

            throw parsingResult.Exception;
        }
Пример #3
0
 /// <summary>
 /// Converts the string representation of a musical interval to its <see cref="Interval"/> equivalent.
 /// A return value indicates whether the conversion succeeded.
 /// </summary>
 /// <param name="input">A string containing an interval to convert.</param>
 /// <param name="interval">When this method returns, contains the <see cref="Interval"/>
 /// equivalent of the musical interval contained in <paramref name="input"/>, if the conversion succeeded,
 /// or null if the conversion failed. The conversion fails if the <paramref name="input"/> is null or
 /// <see cref="String.Empty"/>, or is not of the correct format. This parameter is passed uninitialized;
 /// any value originally supplied in result will be overwritten.</param>
 /// <returns>true if <paramref name="input"/> was converted successfully; otherwise, false.</returns>
 public static bool TryParse(string input, out Interval interval)
 {
     return(IntervalParser.TryParse(input, out interval).Status == ParsingStatus.Parsed);
 }
Пример #4
0
        internal static ParsingResult TryParse(string input, out Scale scale)
        {
            scale = null;

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

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

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

            var rootNoteNameGroup = match.Groups[RootNoteNameGroupName];

            NoteName rootNoteName;
            var      rootNoteNameParsingResult = NoteNameParser.TryParse(rootNoteNameGroup.Value, out rootNoteName);

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

            //

            IEnumerable <Interval> intervals = null;

            var intervalGroup = match.Groups[IntervalGroupName];

            if (intervalGroup.Success)
            {
                var intervalsParsingResults = intervalGroup
                                              .Captures
                                              .OfType <Capture>()
                                              .Select(c =>
                {
                    Interval interval;
                    var parsingResult = IntervalParser.TryParse(c.Value, out interval);

                    return(new
                    {
                        Interval = interval,
                        ParsingResult = parsingResult
                    });
                })
                                              .ToArray();

                var notParsedResult =
                    intervalsParsingResults.FirstOrDefault(r => r.ParsingResult.Status != ParsingStatus.Parsed);
                if (notParsedResult != null)
                {
                    return(notParsedResult.ParsingResult);
                }

                intervals = intervalsParsingResults.Select(r => r.Interval).ToArray();
            }
            else
            {
                var intervalsMnemonicGroup = match.Groups[IntervalsMnemonicGroupName];
                var intervalsName          = intervalsMnemonicGroup.Value;

                intervals = ScaleIntervals.GetByName(intervalsName);
            }

            if (intervals == null)
            {
                return(ParsingResult.Error(ScaleIsUnknown));
            }

            //

            scale = new Scale(intervals, rootNoteName);
            return(ParsingResult.Parsed);
        }