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); }
/// <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 <c>null</c> if the conversion failed. The conversion fails if the <paramref name="input"/> is <c>null</c> 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><c>true</c> if <paramref name="input"/> was converted successfully; otherwise, <c>false</c>.</returns> public static bool TryParse(string input, Scale scale, out ChordProgression chordProgression) { return(ParsingUtilities.TryParse(input, GetParsing(input, scale), out chordProgression)); }