Skip to content

Commit

Permalink
Default RestDetectionSettings
Browse files Browse the repository at this point in the history
  • Loading branch information
melanchall committed May 6, 2024
1 parent 19ff130 commit 4af77ae
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 29 deletions.
10 changes: 7 additions & 3 deletions DryWetMidi/Interaction/Rests/RestDetectionSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ public sealed class RestDetectionSettings
{
#region Constants

private static readonly Func<ITimedObject, object> NoNotesKeySelector = obj => obj is Note ? "Note" : null;

/// <summary>
/// Rests will be built only at spaces without notes at all.
/// </summary>
public static readonly RestDetectionSettings NoNotes = new RestDetectionSettings
{
KeySelector = obj => obj is Note ? "Note" : null
KeySelector = NoNotesKeySelector
};

/// <summary>
Expand Down Expand Up @@ -66,9 +68,11 @@ public sealed class RestDetectionSettings
/// <summary>
/// Gets or sets a function that returns the key of an object. Please read
/// <see href="xref:a_getting_objects#rests">Getting objects: Rests</see> article to
/// understand the key concept.
/// understand the key concept. The default key selector is
/// <c>obj => obj is Note ? "Note" : null</c> which means rests will be built
/// between notes where there are no notes at all.
/// </summary>
public Func<ITimedObject, object> KeySelector { get; set; }
public Func<ITimedObject, object> KeySelector { get; set; } = NoNotesKeySelector;

#endregion
}
Expand Down
34 changes: 8 additions & 26 deletions DryWetMidi/Interaction/Rests/RestsUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,14 @@ public static class RestsUtilities
/// <param name="settings">Settings according to which rests should be detected and built.</param>
/// <returns>Collection with objects from <paramref name="timedObjects"/> and rests
/// between them.</returns>
/// <exception cref="ArgumentNullException">
/// <para>One of the following errors occurred:</para>
/// <list type="bullet">
/// <item>
/// <description><paramref name="timedObjects"/> is <c>null</c>.</description>
/// </item>
/// <item>
/// <description><paramref name="settings"/> is <c>null</c>.</description>
/// </item>
/// </list>
/// </exception>
/// <exception cref="ArgumentNullException"><paramref name="timedObjects"/> is <c>null</c>.</exception>
public static IEnumerable<ITimedObject> WithRests(
this IEnumerable<ITimedObject> timedObjects,
RestDetectionSettings settings)
RestDetectionSettings settings = null)
{
ThrowIfArgument.IsNull(nameof(timedObjects), timedObjects);
ThrowIfArgument.IsNull(nameof(settings), settings);

settings = settings ?? new RestDetectionSettings();

timedObjects = GetSortedObjects(timedObjects);
var rests = GetSortedRestsFromObjects(timedObjects, settings);
Expand All @@ -51,23 +42,14 @@ public static IEnumerable<ITimedObject> WithRests(
/// <param name="timedObjects">The input objects collection.</param>
/// <param name="settings">Settings according to which rests should be detected and built.</param>
/// <returns>Collection of rests between objects within <paramref name="timedObjects"/>.</returns>
/// <exception cref="ArgumentNullException">
/// <para>One of the following errors occurred:</para>
/// <list type="bullet">
/// <item>
/// <description><paramref name="timedObjects"/> is <c>null</c>.</description>
/// </item>
/// <item>
/// <description><paramref name="settings"/> is <c>null</c>.</description>
/// </item>
/// </list>
/// </exception>
/// <exception cref="ArgumentNullException"><paramref name="timedObjects"/> is <c>null</c>.</exception>
public static ICollection<Rest> GetRests(
this IEnumerable<ITimedObject> timedObjects,
RestDetectionSettings settings)
RestDetectionSettings settings = null)
{
ThrowIfArgument.IsNull(nameof(timedObjects), timedObjects);
ThrowIfArgument.IsNull(nameof(settings), settings);

settings = settings ?? new RestDetectionSettings();

timedObjects = GetSortedObjects(timedObjects);
return GetSortedRestsFromObjects(timedObjects, settings);
Expand Down

0 comments on commit 4af77ae

Please sign in to comment.