예제 #1
0
        internal static ParsingResult TryParse(string input, Scale scale, out ChordProgression chordProgression)
        {
            chordProgression = null;

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

            var matches = ParsingUtilities.Matches(input, Patterns, ignoreCase: false);

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

            var chords = new List <Chord>();

            foreach (var match in matches)
            {
                var degreeGroup = match.Groups[ScaleDegreeGroupName];
                var degreeRoman = degreeGroup.Value.ToLower();
                if (string.IsNullOrWhiteSpace(degreeRoman))
                {
                    continue;
                }

                var degree       = RomanToInteger(degreeRoman);
                var rootNoteName = scale.GetStep(degree - 1);

                var fullString       = match.Value;
                var matchIndex       = match.Index;
                var degreeGroupIndex = degreeGroup.Index;
                var chordString      =
                    fullString.Substring(0, degreeGroupIndex - matchIndex) +
                    rootNoteName +
                    fullString.Substring(degreeGroupIndex - matchIndex + degreeGroup.Length);

                Chord chord;
                var   chordParsingResult = ChordParser.TryParse(chordString, out chord);
                if (chordParsingResult.Status != ParsingStatus.Parsed)
                {
                    return(chordParsingResult);
                }

                chords.Add(chord);
            }

            chordProgression = new ChordProgression(chords);
            return(ParsingResult.Parsed);
        }