Skip to content

Commit

Permalink
Handle empty collections in ManageTempoMap (#321)
Browse files Browse the repository at this point in the history
  • Loading branch information
melanchall committed Feb 10, 2025
1 parent 769e48a commit 20e4547
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions DryWetMidi/Interaction/TempoMap/TempoMapManagingUtilities.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Melanchall.DryWetMidi.Common;
Expand Down Expand Up @@ -34,11 +35,18 @@ public static class TempoMapManagingUtilities
/// </item>
/// </list>
/// </exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="eventsCollections"/> is an empty collection.</exception>
public static TempoMapManager ManageTempoMap(this IEnumerable<EventsCollection> eventsCollections, TimeDivision timeDivision)
{
ThrowIfArgument.IsNull(nameof(eventsCollections), eventsCollections);
ThrowIfArgument.IsNull(nameof(timeDivision), timeDivision);

var eventsCollectionsCollection = eventsCollections as ICollection<EventsCollection>;
if (eventsCollectionsCollection != null && eventsCollectionsCollection.Count == 0 && !eventsCollectionsCollection.IsReadOnly)
eventsCollectionsCollection.Add(new EventsCollection());

ThrowIfArgument.DoesntSatisfyCondition(nameof(eventsCollections), eventsCollections, c => c.Any(), "Failed to manage tempo map on an empty events collections list.");

return new TempoMapManager(timeDivision, eventsCollections);
}

Expand All @@ -64,11 +72,18 @@ public static TempoMapManager ManageTempoMap(this IEnumerable<EventsCollection>
/// </item>
/// </list>
/// </exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="trackChunks"/> is an empty collection.</exception>
public static TempoMapManager ManageTempoMap(this IEnumerable<TrackChunk> trackChunks, TimeDivision timeDivision)
{
ThrowIfArgument.IsNull(nameof(trackChunks), trackChunks);
ThrowIfArgument.IsNull(nameof(timeDivision), timeDivision);

var trackChunksCollection = trackChunks as ICollection<TrackChunk>;
if (trackChunksCollection != null && trackChunksCollection.Count == 0 && !trackChunksCollection.IsReadOnly)
trackChunksCollection.Add(new TrackChunk());

ThrowIfArgument.DoesntSatisfyCondition(nameof(trackChunks), trackChunks, c => c.Any(), "Failed to manage tempo map on an empty track chunks collection.");

return trackChunks.Select(c => c.Events).ManageTempoMap(timeDivision);
}

Expand All @@ -84,6 +99,9 @@ public static TempoMapManager ManageTempoMap(this MidiFile file)
{
ThrowIfArgument.IsNull(nameof(file), file);

if (!file.GetTrackChunks().Any())
file.Chunks.Add(new TrackChunk());

return file.GetTrackChunks().ManageTempoMap(file.TimeDivision);
}

Expand Down

0 comments on commit 20e4547

Please sign in to comment.