/// <summary> /// Resolves root note of the specified chord. /// </summary> /// <param name="chord">Chord to resolve root note.</param> /// <param name="octave">Octave to resolve root note of the <paramref name="chord"/>.</param> /// <returns>Root note of the chord regarding to <paramref name="octave"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="chord"/> is null. -or- /// <paramref name="octave"/> is null.</exception> public static Note ResolveRootNote(this Chord chord, Octave octave) { ThrowIfArgument.IsNull(nameof(chord), chord); ThrowIfArgument.IsNull(nameof(octave), octave); return(octave.GetNote(chord.RootNoteName)); }
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); }
/// <summary> /// Resolves notes of the specified chord. /// </summary> /// <param name="chord">Chord to resolve notes.</param> /// <param name="octave">Octave to resolve notes of the <paramref name="chord"/>.</param> /// <returns>Notes of the chord regarding to <paramref name="octave"/>.</returns> /// <exception cref="ArgumentNullException"><paramref name="chord"/> is null. -or- /// <paramref name="octave"/> is null.</exception> public static IEnumerable <Note> ResolveNotes(this Chord chord, Octave octave) { ThrowIfArgument.IsNull(nameof(chord), chord); ThrowIfArgument.IsNull(nameof(octave), octave); var rootNote = chord.ResolveRootNote(octave); var result = new List <Note> { rootNote }; result.AddRange(chord.GetIntervalsFromRootNote().Select(i => rootNote + i)); return(result); }
/// <summary> /// Gets an octave by the specified octave number. /// </summary> /// <param name="octaveNumber">The number of an octave.</param> /// <returns>An octave with the specified number.</returns> /// <exception cref="ArgumentOutOfRangeException"><paramref name="octaveNumber"/> is out of valid range.</exception> public static Octave Get(int octaveNumber) { ThrowIfArgument.IsOutOfRange(nameof(octaveNumber), octaveNumber, MinOctaveNumber, MaxOctaveNumber, $"Octave number is out of [{MinOctaveNumber}, {MaxOctaveNumber}] range."); Octave octave; if (!_cache.TryGetValue(octaveNumber, out octave)) { _cache.Add(octaveNumber, octave = new Octave(octaveNumber)); } return(octave); }
/// <summary> /// Converts the string representation of a musical octave to its <see cref="Octave"/> equivalent. /// A return value indicates whether the conversion succeeded. /// </summary> /// <param name="input">A string containing an octave to convert.</param> /// <param name="octave">When this method returns, contains the <see cref="Octave"/> /// equivalent of the musical octave 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, out Octave octave) { return(ParsingUtilities.TryParse(input, OctaveParser.TryParse, out octave)); }