/// <summary> /// Sets the state of chords within the specified pattern. /// </summary> /// <param name="pattern">Pattern to set chords state within.</param> /// <param name="chordSelection">Predicate to select chords to set state.</param> /// <param name="state">State of chords selected with <paramref name="chordSelection"/>.</param> /// <param name="recursive">A value indicating whether nested patterns should be processed or not. The /// default value is <c>true</c>.</param> /// <exception cref="ArgumentNullException"> /// <para>One of the following errors occured:</para> /// <list type="bullet"> /// <item> /// <description><paramref name="pattern"/> is <c>null</c>.</description> /// </item> /// <item> /// <description><paramref name="chordSelection"/> is <c>null</c>.</description> /// </item> /// </list> /// </exception> /// <exception cref="InvalidEnumArgumentException"><paramref name="state"/> specified an invalid value.</exception> public static void SetChordsState(this Pattern pattern, ChordSelection chordSelection, PatternActionState state, bool recursive = true) { ThrowIfArgument.IsNull(nameof(pattern), pattern); ThrowIfArgument.IsNull(nameof(chordSelection), chordSelection); ThrowIfArgument.IsInvalidEnumValue(nameof(state), state); var chordIndexWrapper = new ObjectWrapper <int>(); SetChordsState(pattern, chordIndexWrapper, chordSelection, state, recursive); }
/// <summary> /// Creates a new <see cref="Pattern"/> by transforming chords in the specified pattern using predicate /// to select chords to transform.. /// </summary> /// <param name="pattern">Pattern to transform notes of.</param> /// <param name="chordSelection">Predicate to select chords to transform.</param> /// <param name="chordTransformation">Transformation to apply to chords of the <paramref name="pattern"/>.</param> /// <param name="recursive">A value indicating whether nested patterns should be processed or not. The /// default value is <c>true</c>.</param> /// <returns><see cref="Pattern"/> that created by transforming chords of the <paramref name="pattern"/>.</returns> /// <exception cref="ArgumentNullException"> /// <para>One of the following errors occured:</para> /// <list type="bullet"> /// <item> /// <description><paramref name="pattern"/> is <c>null</c>.</description> /// </item> /// <item> /// <description><paramref name="chordSelection"/> is <c>null</c>.</description> /// </item> /// <item> /// <description><paramref name="chordTransformation"/> is <c>null</c>.</description> /// </item> /// </list> /// </exception> public static Pattern TransformChords(this Pattern pattern, ChordSelection chordSelection, ChordTransformation chordTransformation, bool recursive = true) { ThrowIfArgument.IsNull(nameof(pattern), pattern); ThrowIfArgument.IsNull(nameof(chordSelection), chordSelection); ThrowIfArgument.IsNull(nameof(chordTransformation), chordTransformation); var chordIndexWrapper = new ObjectWrapper <int>(); return(TransformChords(pattern, chordIndexWrapper, chordSelection, chordTransformation, recursive)); }
private static void SetChordsState(Pattern pattern, ObjectWrapper <int> chordIndexWrapper, ChordSelection chordSelection, PatternActionState state, bool recursive) { foreach (var a in pattern.Actions) { var addChordAction = a as AddChordAction; if (addChordAction != null && chordSelection(chordIndexWrapper.Object++, addChordAction.ChordDescriptor)) { addChordAction.State = state; } var addPatternAction = a as AddPatternAction; if (addPatternAction != null && recursive) { SetChordsState(addPatternAction.Pattern, chordIndexWrapper, chordSelection, state, recursive); } } }
private static Pattern TransformChords(Pattern pattern, ObjectWrapper <int> chordIndexWrapper, ChordSelection chordSelection, ChordTransformation chordTransformation, bool recursive) { return(new Pattern(pattern.Actions.Select(a => { var addChordAction = a as AddChordAction; if (addChordAction != null && chordSelection(chordIndexWrapper.Object++, addChordAction.ChordDescriptor)) { var chordDescriptor = chordTransformation(addChordAction.ChordDescriptor); return new AddChordAction(chordDescriptor); } var addPatternAction = a as AddPatternAction; if (addPatternAction != null && recursive) { return new AddPatternAction(TransformChords(addPatternAction.Pattern, chordIndexWrapper, chordSelection, chordTransformation, recursive)); } return a.Clone(); }) .ToList())); }