/// <summary> /// Creates a new <see cref="Pattern"/> by transforming notes in the specified pattern. /// </summary> /// <param name="pattern">Pattern to transform notes of.</param> /// <param name="noteTransformation">Transformation to apply to notes 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 notes 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="noteTransformation"/> is <c>null</c>.</description> /// </item> /// </list> /// </exception> public static Pattern TransformNotes(this Pattern pattern, NoteTransformation noteTransformation, bool recursive = true) { ThrowIfArgument.IsNull(nameof(pattern), pattern); ThrowIfArgument.IsNull(nameof(noteTransformation), noteTransformation); return(TransformNotes(pattern, AllNotesSelection, noteTransformation, recursive)); }
public static Pattern TransformNotes(this Pattern pattern, NoteTransformation noteTransformation, bool recursive = true) { ThrowIfArgument.IsNull(nameof(pattern), pattern); ThrowIfArgument.IsNull(nameof(noteTransformation), noteTransformation); return(new Pattern(pattern.Actions.Select(a => { var addNoteAction = a as AddNoteAction; if (addNoteAction != null) { var noteDescriptor = noteTransformation(addNoteAction.NoteDescriptor); return new AddNoteAction(noteDescriptor); } var addPatternAction = a as AddPatternAction; if (addPatternAction != null && recursive) { return new AddPatternAction(addPatternAction.Pattern.TransformNotes(noteTransformation)); } return a; }) .ToList())); }
/// <summary> /// Creates a new <see cref="Pattern"/> by transforming notes in the specified pattern using predicate /// to select notes to transform. /// </summary> /// <param name="pattern">Pattern to transform notes of.</param> /// <param name="noteSelection">Predicate to select notes to transform.</param> /// <param name="noteTransformation">Transformation to apply to notes 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 notes 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="noteSelection"/> is <c>null</c>.</description> /// </item> /// <item> /// <description><paramref name="noteTransformation"/> is <c>null</c>.</description> /// </item> /// </list> /// </exception> public static Pattern TransformNotes(this Pattern pattern, NoteSelection noteSelection, NoteTransformation noteTransformation, bool recursive = true) { ThrowIfArgument.IsNull(nameof(pattern), pattern); ThrowIfArgument.IsNull(nameof(noteSelection), noteSelection); ThrowIfArgument.IsNull(nameof(noteTransformation), noteTransformation); var noteIndexWrapper = new ObjectWrapper <int>(); return(TransformNotes(pattern, noteIndexWrapper, noteSelection, noteTransformation, recursive)); }
private static Pattern TransformNotes(Pattern pattern, ObjectWrapper <int> noteIndexWrapper, NoteSelection noteSelection, NoteTransformation noteTransformation, bool recursive) { return(new Pattern(pattern.Actions.Select(a => { var addNoteAction = a as AddNoteAction; if (addNoteAction != null && noteSelection(noteIndexWrapper.Object++, addNoteAction.NoteDescriptor)) { var noteDescriptor = noteTransformation(addNoteAction.NoteDescriptor); return new AddNoteAction(noteDescriptor); } var addPatternAction = a as AddPatternAction; if (addPatternAction != null && recursive) { return new AddPatternAction(TransformNotes(addPatternAction.Pattern, noteIndexWrapper, noteSelection, noteTransformation, recursive)); } return a.Clone(); }) .ToList())); }