Skip to content

Commit

Permalink
Decouple object detection settings
Browse files Browse the repository at this point in the history
  • Loading branch information
melanchall committed Jul 18, 2024
1 parent 54c33e7 commit ffd962e
Show file tree
Hide file tree
Showing 15 changed files with 525 additions and 278 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,6 @@ public void GetChords_DetectionSettings_EventsCollection_NotesTolerance_1_Custom
{
NotesTolerance = 0,
Constructor = CustomChordConstructor,
NoteDetectionSettings = new NoteDetectionSettings
{
Constructor = CustomNoteConstructor
}
},
midiEvents: new MidiEvent[]
{
Expand Down Expand Up @@ -119,6 +115,10 @@ public void GetChords_DetectionSettings_EventsCollection_NotesTolerance_1_Custom
Assert.IsNotInstanceOf<CustomTimedEvent>(n.GetTimedNoteOffEvent(), "Invalid Note Off timed event type.");
}
}
},
noteDetectionSettings: new NoteDetectionSettings
{
Constructor = CustomNoteConstructor
});

[Test]
Expand All @@ -128,11 +128,6 @@ public void GetChords_DetectionSettings_EventsCollection_NotesTolerance_1_Custom
{
NotesTolerance = 0,
Constructor = CustomChordConstructor,
NoteDetectionSettings = new NoteDetectionSettings
{
Constructor = CustomNoteConstructor,
TimedEventDetectionSettings = CustomEventSettings
}
},
midiEvents: new MidiEvent[]
{
Expand Down Expand Up @@ -163,7 +158,12 @@ public void GetChords_DetectionSettings_EventsCollection_NotesTolerance_1_Custom
Assert.IsInstanceOf<CustomTimedEvent>(n.GetTimedNoteOffEvent(), "Invalid Note Off timed event type.");
}
}
});
},
noteDetectionSettings: new NoteDetectionSettings
{
Constructor = CustomNoteConstructor,
},
timedEventDetectionSettings: CustomEventSettings);

[Test]
public void GetChords_DetectionSettings_EventsCollection_NotesTolerance_2([Values] ContainerType containerType, [Values(1, 10)] int notesTolerance) => GetChords_DetectionSettings_EventsCollection(
Expand Down Expand Up @@ -473,10 +473,6 @@ public void GetChords_DetectionSettings_TrackChunks_NotesTolerance_1_Custom_2([V
{
NotesTolerance = 0,
Constructor = CustomChordConstructor,
NoteDetectionSettings = new NoteDetectionSettings
{
Constructor = CustomNoteConstructor
}
},
midiEvents: new[]
{
Expand Down Expand Up @@ -524,6 +520,10 @@ public void GetChords_DetectionSettings_TrackChunks_NotesTolerance_1_Custom_2([V
Assert.IsNotInstanceOf<CustomTimedEvent>(n.GetTimedNoteOffEvent(), "Invalid Note Off timed event type.");
}
}
},
noteDetectionSettings: new NoteDetectionSettings
{
Constructor = CustomNoteConstructor
});

[Test]
Expand All @@ -533,11 +533,6 @@ public void GetChords_DetectionSettings_TrackChunks_NotesTolerance_1_Custom_3([V
{
NotesTolerance = 0,
Constructor = CustomChordConstructor,
NoteDetectionSettings = new NoteDetectionSettings
{
Constructor = CustomNoteConstructor,
TimedEventDetectionSettings = CustomEventSettings
}
},
midiEvents: new[]
{
Expand Down Expand Up @@ -585,7 +580,12 @@ public void GetChords_DetectionSettings_TrackChunks_NotesTolerance_1_Custom_3([V
Assert.IsInstanceOf<CustomTimedEvent>(n.GetTimedNoteOffEvent(), "Invalid Note Off timed event type.");
}
}
});
},
noteDetectionSettings: new NoteDetectionSettings
{
Constructor = CustomNoteConstructor,
},
timedEventDetectionSettings: CustomEventSettings);

[Test]
public void GetChords_DetectionSettings_TrackChunks_NotesTolerance_2([Values] bool wrapToFile, [Values(1, 10)] int notesTolerance) => GetChords_DetectionSettings_TrackChunks(
Expand Down Expand Up @@ -830,7 +830,9 @@ private void GetChords_DetectionSettings_EventsCollection(
ChordDetectionSettings settings,
ICollection<MidiEvent> midiEvents,
ICollection<Chord> expectedChords,
Action<ICollection<Chord>> additionalChecks = null)
Action<ICollection<Chord>> additionalChecks = null,
NoteDetectionSettings noteDetectionSettings = null,
TimedEventDetectionSettings timedEventDetectionSettings = null)
{
switch (containerType)
{
Expand All @@ -839,11 +841,16 @@ private void GetChords_DetectionSettings_EventsCollection(
var eventsCollection = new EventsCollection();
eventsCollection.AddRange(midiEvents);

var chords = eventsCollection.GetChords(settings);
var chords = eventsCollection.GetChords(settings, noteDetectionSettings, timedEventDetectionSettings);
MidiAsserts.AreEqual(expectedChords, chords, "Chords are invalid.");
additionalChecks?.Invoke(chords);

var timedObjects = eventsCollection.GetObjects(ObjectType.Chord, new ObjectDetectionSettings { ChordDetectionSettings = settings });
var timedObjects = eventsCollection.GetObjects(ObjectType.Chord, new ObjectDetectionSettings
{
ChordDetectionSettings = settings,
NoteDetectionSettings = noteDetectionSettings,
TimedEventDetectionSettings = timedEventDetectionSettings,
});
MidiAsserts.AreEqual(expectedChords, timedObjects, "Chords are invalid from GetObjects.");
additionalChecks?.Invoke(timedObjects.Cast<Chord>().ToArray());
}
Expand All @@ -852,11 +859,16 @@ private void GetChords_DetectionSettings_EventsCollection(
{
var trackChunk = new TrackChunk(midiEvents);

var chords = trackChunk.GetChords(settings);
var chords = trackChunk.GetChords(settings, noteDetectionSettings, timedEventDetectionSettings);
MidiAsserts.AreEqual(expectedChords, chords, "Chords are invalid.");
additionalChecks?.Invoke(chords);

var timedObjects = trackChunk.GetObjects(ObjectType.Chord, new ObjectDetectionSettings { ChordDetectionSettings = settings });
var timedObjects = trackChunk.GetObjects(ObjectType.Chord, new ObjectDetectionSettings
{
ChordDetectionSettings = settings,
NoteDetectionSettings = noteDetectionSettings,
TimedEventDetectionSettings = timedEventDetectionSettings,
});
MidiAsserts.AreEqual(expectedChords, timedObjects, "Chords are invalid from GetObjects.");
additionalChecks?.Invoke(timedObjects.Cast<Chord>().ToArray());
}
Expand All @@ -869,7 +881,9 @@ private void GetChords_DetectionSettings_EventsCollection(
settings,
new[] { midiEvents },
expectedChords,
additionalChecks);
additionalChecks,
noteDetectionSettings,
timedEventDetectionSettings);
}
break;
}
Expand All @@ -880,16 +894,18 @@ private void GetChords_DetectionSettings_TrackChunks(
ChordDetectionSettings settings,
ICollection<ICollection<MidiEvent>> midiEvents,
IEnumerable<Chord> expectedChords,
Action<ICollection<Chord>> additionalChecks = null)
Action<ICollection<Chord>> additionalChecks = null,
NoteDetectionSettings noteDetectionSettings = null,
TimedEventDetectionSettings timedEventDetectionSettings = null)
{
ICollection<Chord> chords;

var trackChunks = midiEvents.Select(e => new TrackChunk(e)).ToArray();

if (wrapToFile)
chords = new MidiFile(trackChunks).GetChords(settings);
chords = new MidiFile(trackChunks).GetChords(settings, noteDetectionSettings, timedEventDetectionSettings);
else
chords = trackChunks.GetChords(settings);
chords = trackChunks.GetChords(settings, noteDetectionSettings, timedEventDetectionSettings);

MidiAsserts.AreEqual(expectedChords, chords, "Chords are invalid.");
additionalChecks?.Invoke(chords);
Expand All @@ -899,9 +915,19 @@ private void GetChords_DetectionSettings_TrackChunks(
IEnumerable<ITimedObject> timedObjects;

if (wrapToFile)
timedObjects = new MidiFile(trackChunks).GetObjects(ObjectType.Chord, new ObjectDetectionSettings { ChordDetectionSettings = settings });
timedObjects = new MidiFile(trackChunks).GetObjects(ObjectType.Chord, new ObjectDetectionSettings
{
ChordDetectionSettings = settings,
NoteDetectionSettings = noteDetectionSettings,
TimedEventDetectionSettings = timedEventDetectionSettings,
});
else
timedObjects = trackChunks.GetObjects(ObjectType.Chord, new ObjectDetectionSettings { ChordDetectionSettings = settings });
timedObjects = trackChunks.GetObjects(ObjectType.Chord, new ObjectDetectionSettings
{
ChordDetectionSettings = settings,
NoteDetectionSettings = noteDetectionSettings,
TimedEventDetectionSettings = timedEventDetectionSettings,
});

MidiAsserts.AreEqual(expectedChords, timedObjects, "Chords are invalid from GetObjects.");
additionalChecks?.Invoke(timedObjects.Cast<Chord>().ToArray());
Expand Down
Loading

0 comments on commit ffd962e

Please sign in to comment.