internal static ParsingResult TryParse(string input, out Interval interval) { interval = null; if (string.IsNullOrWhiteSpace(input)) { return(ParsingResult.EmptyInputString); } var match = ParsingUtilities.Match(input, Patterns); if (match == null) { return(ParsingResult.NotMatched); } int halfSteps; if (!ParsingUtilities.ParseInt(match, HalfStepsGroupName, 0, out halfSteps) || !IntervalUtilities.IsIntervalValid(halfSteps)) { return(ParsingResult.Error(HalfStepsNumberIsOutOfRange)); } interval = Interval.FromHalfSteps(halfSteps); return(ParsingResult.Parsed); }
internal static ParsingResult TryParse(string input, out MidiTimeSpan timeSpan) { timeSpan = null; if (string.IsNullOrWhiteSpace(input)) { return(ParsingResult.EmptyInputString); } var match = ParsingUtilities.Match(input, Patterns); if (match == null) { return(ParsingResult.NotMatched); } long midiTimeSpan; if (!ParsingUtilities.ParseNonnegativeLong(match, TimeSpanGroupName, 0, out midiTimeSpan)) { return(ParsingResult.Error(OutOfRange)); } timeSpan = new MidiTimeSpan(midiTimeSpan); return(ParsingResult.Parsed); }
internal static ParsingResult TryParse(string input, out Octave octave) { octave = null; if (string.IsNullOrWhiteSpace(input)) { return(ParsingResult.EmptyInputString); } var match = ParsingUtilities.Match(input, Patterns); if (match == null) { return(ParsingResult.NotMatched); } int octaveNumber; if (!ParsingUtilities.ParseInt(match, OctaveNumberGroupName, Octave.Middle.Number, out octaveNumber) || octaveNumber < Octave.MinOctaveNumber || octaveNumber > Octave.MaxOctaveNumber) { return(ParsingResult.Error(OctaveIsOutOfRange)); } octave = Octave.Get(octaveNumber); return(ParsingResult.Parsed); }
internal static ParsingResult TryParse(string input, out BeatTimeSpan timeSpan) { timeSpan = null; if (string.IsNullOrWhiteSpace(input)) { return(ParsingResult.EmptyInputString); } var match = ParsingUtilities.Match(input, Patterns); if (match == null) { return(ParsingResult.NotMatched); } long beats; if (!ParsingUtilities.ParseNonnegativeLong(match, BeatsGroupName, 0, out beats)) { return(ParsingResult.Error(BeatsIsOutOfRange)); } long ticks; if (!ParsingUtilities.ParseNonnegativeLong(match, TicksGroupName, 0, out ticks)) { return(ParsingResult.Error(TicksIsOutOfRange)); } timeSpan = new BeatTimeSpan(beats, ticks); return(ParsingResult.Parsed); }
internal static ParsingResult TryParse(string input, out BarBeatFractionTimeSpan timeSpan) { timeSpan = null; if (string.IsNullOrWhiteSpace(input)) { return(ParsingResult.EmptyInputString); } var match = ParsingUtilities.Match(input, Patterns); if (match == null) { return(ParsingResult.NotMatched); } long bars; if (!ParsingUtilities.ParseNonnegativeLong(match, BarsGroupName, 0, out bars)) { return(ParsingResult.Error(BarsIsOutOfRange)); } double beats; if (!ParsingUtilities.ParseNonnegativeDouble(match, BeatsGroupName, 0, out beats)) { return(ParsingResult.Error(BeatsIsOutOfRange)); } timeSpan = new BarBeatFractionTimeSpan(bars, beats); return(ParsingResult.Parsed); }
internal static ParsingResult TryParse(string input, Scale scale, out ChordProgression chordProgression) { chordProgression = null; if (string.IsNullOrWhiteSpace(input)) { return(ParsingResult.EmptyInputString); } var parts = input.Split(new[] { PartsDelimiter }, System.StringSplitOptions.RemoveEmptyEntries); var chords = new List <Chord>(); foreach (var part in parts) { var match = ParsingUtilities.Match(part, Patterns, ignoreCase: false); if (match == null) { return(ParsingResult.NotMatched); } 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); }
internal static ParsingResult TryParse(string input, out NoteName noteName) { noteName = default(NoteName); if (string.IsNullOrWhiteSpace(input)) { return(ParsingResult.EmptyInputString); } var match = ParsingUtilities.Match(input, Patterns); if (match == null) { return(ParsingResult.NotMatched); } var noteLetterGroup = match.Groups[NoteLetterGroupName]; var noteBaseNumber = (int)(NoteName)Enum.Parse(typeof(NoteName), noteLetterGroup.Value); var accidentalGroup = match.Groups[AccidentalGroupName]; if (accidentalGroup.Success) { foreach (Capture capture in accidentalGroup.Captures) { var accidental = capture.Value; if (string.Equals(accidental, Note.SharpShortString, StringComparison.OrdinalIgnoreCase) || string.Equals(accidental, Note.SharpLongString, StringComparison.OrdinalIgnoreCase)) { noteBaseNumber++; } else if (string.Equals(accidental, Note.FlatShortString, StringComparison.OrdinalIgnoreCase) || string.Equals(accidental, Note.FlatLongString, StringComparison.OrdinalIgnoreCase)) { noteBaseNumber--; } } } noteBaseNumber %= Octave.OctaveSize; if (noteBaseNumber < 0) { noteBaseNumber = Octave.OctaveSize + noteBaseNumber; } noteName = (NoteName)noteBaseNumber; return(ParsingResult.Parsed); }
internal static ParsingResult TryParse(string input, out MetricTimeSpan timeSpan) { timeSpan = null; if (string.IsNullOrWhiteSpace(input)) { return(ParsingResult.EmptyInputString); } var match = ParsingUtilities.Match(input, Patterns); if (match == null) { return(ParsingResult.NotMatched); } int hours; if (!ParsingUtilities.ParseInt(match, HoursGroupName, 0, out hours)) { return(new ParsingResult(HoursIsOutOfRange)); } int minutes; if (!ParsingUtilities.ParseInt(match, MinutesGroupName, 0, out minutes)) { return(new ParsingResult(MinutesIsOutOfRange)); } int seconds; if (!ParsingUtilities.ParseInt(match, SecondsGroupName, 0, out seconds)) { return(new ParsingResult(SecondsIsOutOfRange)); } int milliseconds; if (!ParsingUtilities.ParseInt(match, MillisecondsGroupName, 0, out milliseconds)) { return(new ParsingResult(MillisecondsIsOutOfRange)); } timeSpan = new MetricTimeSpan(hours, minutes, seconds, milliseconds); return(ParsingResult.Parsed); }
internal static ParsingResult TryParse(string input, out Interval interval) { interval = null; if (string.IsNullOrWhiteSpace(input)) { return(ParsingResult.EmptyInputString); } var match = ParsingUtilities.Match(input, Patterns, ignoreCase: false); if (match == null) { return(ParsingResult.NotMatched); } var intervalQualityGroup = match.Groups[IntervalQualityGroupName]; if (!intervalQualityGroup.Success) { int halfSteps; if (!ParsingUtilities.ParseInt(match, HalfStepsGroupName, 0, out halfSteps) || !IntervalUtilities.IsIntervalValid(halfSteps)) { return(ParsingResult.Error(HalfStepsNumberIsOutOfRange)); } interval = Interval.FromHalfSteps(halfSteps); return(ParsingResult.Parsed); } var intervalQuality = IntervalQualitiesByLetters[intervalQualityGroup.Value]; int intervalNumber; if (!ParsingUtilities.ParseInt(match, IntervalNumberGroupName, 0, out intervalNumber) || intervalNumber < 1) { return(ParsingResult.Error(IntervalNumberIsOutOfRange)); } interval = Interval.Get(intervalQuality, intervalNumber); return(ParsingResult.Parsed); }
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); }
internal static ParsingResult TryParse(string input, out Chord chord) { chord = null; if (string.IsNullOrWhiteSpace(input)) return ParsingResult.EmptyInputString; var match = ParsingUtilities.Match(input, Patterns, ignoreCase: false); 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; // NoteName? bassNoteName = null; var bassNoteNameGroup = match.Groups[BassNoteNameGroupName]; if (bassNoteNameGroup.Success) { NoteName actualBassNoteName; var bassNoteNameParsingResult = NoteNameParser.TryParse(bassNoteNameGroup.Value, out actualBassNoteName); if (bassNoteNameParsingResult.Status != ParsingStatus.Parsed) return bassNoteNameParsingResult; bassNoteName = actualBassNoteName; } var notesNames = ChordsNamesTable.GetChordNotesNames(rootNoteName, match.Groups[ChordCharacteristicsGroupName].Value, bassNoteName); if (!notesNames.Any()) return ParsingResult.Error(ChordCharacteristicIsUnknown); chord = new Chord(notesNames); return ParsingResult.Parsed; }
internal static ParsingResult TryParse(string input, out Chord chord) { chord = null; if (string.IsNullOrWhiteSpace(input)) { return(ParsingResult.EmptyInputString); } var match = ParsingUtilities.Match(input, Patterns, ignoreCase: false); 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); } if (match.Groups[ChordNameGroupName].Success) { return(TryParseChordName(match, rootNoteName, out chord)); } else if (match.Groups[ChordIntervalsGroupName].Success) { return(TryParseChordIntervals(match, rootNoteName, out chord)); } return(ParsingResult.Parsed); }
internal static ParsingResult TryParse(string input, out NoteName noteName) { noteName = default(NoteName); if (string.IsNullOrWhiteSpace(input)) { return(ParsingResult.EmptyInputString); } var match = ParsingUtilities.Match(input, Patterns); if (match == null) { return(ParsingResult.NotMatched); } var noteNameGroup = match.Groups[NoteNameGroupName]; var noteNameString = noteNameGroup.Value; var accidentalGroup = match.Groups[AccidentalGroupName]; if (accidentalGroup.Success) { var accidental = accidentalGroup.Value; accidental = accidental.Replace(Note.SharpShortString, Note.SharpLongString); accidental = char.ToUpper(accidental[0]) + accidental.Substring(1); noteNameString += accidental; } if (!Enum.TryParse(noteNameString, out noteName)) { return(ParsingResult.Error(NoteNameIsInvalid)); } return(ParsingResult.Parsed); }
internal static ParsingResult TryParse(string input, out MusicalTimeSpan timeSpan) { timeSpan = null; if (string.IsNullOrWhiteSpace(input)) { return(ParsingResult.EmptyInputString); } var match = ParsingUtilities.Match(input, Patterns); if (match == null) { return(ParsingResult.NotMatched); } // Fraction long numerator; if (!ParsingUtilities.ParseLong(match, NumeratorGroupName, 1, out numerator)) { return(new ParsingResult(NumeratorIsOutOfRange)); } long denominator; if (!ParsingUtilities.ParseLong(match, DenominatorGroupName, 1, out denominator)) { return(new ParsingResult(DenominatorIsOutOfRange)); } var fractionMnemonicGroup = match.Groups[FractionMnemonicGroupName]; if (fractionMnemonicGroup.Success) { var fractionDefinition = Fractions[fractionMnemonicGroup.Value]; numerator = fractionDefinition.Item1; denominator = fractionDefinition.Item2; } // Tuplet int tupletNotesCount; if (!ParsingUtilities.ParseInt(match, TupletNotesCountGroupName, 1, out tupletNotesCount)) { return(new ParsingResult(TupletNotesCountIsOutOfRange)); } int tupletSpaceSize; if (!ParsingUtilities.ParseInt(match, TupletSpaceSizeGroupName, 1, out tupletSpaceSize)) { return(new ParsingResult(TupletSpaceSizeIsOutOfRange)); } var tupletMnemonicGroup = match.Groups[TupletMnemonicGroupName]; if (tupletMnemonicGroup.Success) { var tupletDefinition = Tuplets[tupletMnemonicGroup.Value]; tupletNotesCount = tupletDefinition.Item1; tupletSpaceSize = tupletDefinition.Item2; } // Dots var dotsGroup = match.Groups[DotsGroupName]; var dots = dotsGroup.Success ? dotsGroup.Value.Length : 0; // timeSpan = new MusicalTimeSpan(numerator, denominator).Dotted(dots).Tuplet(tupletNotesCount, tupletSpaceSize); return(ParsingResult.Parsed); }
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); }