コード例 #1
0
ファイル: ChordProgression.cs プロジェクト: Hyuan02/GGJ2020
 /// <summary>
 /// Converts the string representation of a chord progression to its <see cref="ChordProgression"/> equivalent.
 /// A return value indicates whether the conversion succeeded.
 /// </summary>
 /// <param name="input">A string containing a chord progression to convert.</param>
 /// <param name="scale">Scale to resolve chords.</param>
 /// <param name="chordProgression">When this method returns, contains the <see cref="ChordProgression"/>
 /// equivalent of the chord progression 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, Scale scale, out ChordProgression chordProgression)
 {
     return(ParsingUtilities.TryParse(input, GetParsing(input, scale), out chordProgression));
 }
コード例 #2
0
ファイル: ChordProgression.cs プロジェクト: Hyuan02/GGJ2020
 /// <summary>
 /// Converts the string representation of a chord progression to its <see cref="ChordProgression"/> equivalent.
 /// </summary>
 /// <param name="input">A string containing a chord progression to convert.</param>
 /// <param name="scale">Scale to resolve chords.</param>
 /// <returns>A <see cref="ChordProgression"/> equivalent to the chord progression 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 ChordProgression Parse(string input, Scale scale)
 {
     return(ParsingUtilities.Parse(input, GetParsing(input, scale)));
 }
コード例 #3
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);
        }