private void ProcessChords_DetectionSettings_EventsCollection_WithoutPredicate(
            ContainerType containerType,
            ChordDetectionSettings settings,
            ICollection <MidiEvent> midiEvents,
            Action <Chord> action,
            ICollection <MidiEvent> expectedMidiEvents)
        {
            var chordsCount = midiEvents.GetChords(settings).Count;

            switch (containerType)
            {
            case ContainerType.EventsCollection:
            {
                var eventsCollection = new EventsCollection();
                eventsCollection.AddRange(midiEvents);

                Assert.AreEqual(
                    chordsCount,
                    eventsCollection.ProcessChords(action, settings),
                    "Invalid count of processed chords.");

                var expectedEventsCollection = new EventsCollection();
                expectedEventsCollection.AddRange(expectedMidiEvents);
                MidiAsserts.AreEqual(expectedEventsCollection, eventsCollection, true, "Events are invalid.");
                Assert.IsTrue(
                    eventsCollection.All(e => midiEvents.Any(ee => object.ReferenceEquals(e, ee))),
                    "There are new events references.");
            }
            break;

            case ContainerType.TrackChunk:
            {
                var trackChunk = new TrackChunk(midiEvents);

                Assert.AreEqual(
                    chordsCount,
                    trackChunk.ProcessChords(action, settings),
                    "Invalid count of processed chords.");

                var expectedTrackChunk = new TrackChunk(expectedMidiEvents);
                MidiAsserts.AreEqual(expectedTrackChunk, trackChunk, true, "Events are invalid.");
                Assert.IsTrue(
                    trackChunk.Events.All(e => midiEvents.Any(ee => object.ReferenceEquals(e, ee))),
                    "There are new events references.");
            }
            break;

            case ContainerType.TrackChunks:
            case ContainerType.File:
            {
                ProcessChords_DetectionSettings_TrackChunks_WithoutPredicate(
                    containerType == ContainerType.File,
                    settings,
                    new[] { midiEvents },
                    action,
                    new[] { expectedMidiEvents });
            }
            break;
            }
        }
Esempio n. 2
0
        private void RemoveTimedEvents_EventsCollection_WithPredicate(
            ContainerType containerType,
            ICollection <MidiEvent> midiEvents,
            Predicate <TimedEvent> match,
            ICollection <MidiEvent> expectedMidiEvents,
            int expectedRemovedCount)
        {
            var eventsCollection = new EventsCollection();

            eventsCollection.AddRange(midiEvents);

            switch (containerType)
            {
            case ContainerType.EventsCollection:
            {
                Assert.AreEqual(
                    expectedRemovedCount,
                    eventsCollection.RemoveTimedEvents(match),
                    "Invalid count of removed timed events.");

                var expectedEventsCollection = new EventsCollection();
                expectedEventsCollection.AddRange(expectedMidiEvents);
                MidiAsserts.AreEqual(expectedEventsCollection, eventsCollection, true, "Events are invalid.");
                Assert.IsTrue(
                    eventsCollection.All(e => midiEvents.Any(ee => object.ReferenceEquals(e, ee))),
                    "There are new events references.");
            }
            break;

            case ContainerType.TrackChunk:
            {
                var trackChunk = new TrackChunk(eventsCollection);

                Assert.AreEqual(
                    expectedRemovedCount,
                    trackChunk.RemoveTimedEvents(match),
                    "Invalid count of removed timed events.");

                var expectedTrackChunk = new TrackChunk(expectedMidiEvents);
                MidiAsserts.AreEqual(expectedTrackChunk, trackChunk, true, "Events are invalid.");
                Assert.IsTrue(
                    trackChunk.Events.All(e => midiEvents.Any(ee => object.ReferenceEquals(e, ee))),
                    "There are new events references.");
            }
            break;

            case ContainerType.TrackChunks:
            case ContainerType.File:
            {
                RemoveTimedEvents_TrackChunks_WithPredicate(
                    containerType == ContainerType.File,
                    new[] { midiEvents },
                    match,
                    new[] { expectedMidiEvents },
                    expectedRemovedCount);
            }
            break;
            }
        }
        public static IEnumerable <TimedEvent> GetTimedEvents(this IEnumerable <MidiEvent> events)
        {
            ThrowIfArgument.IsNull(nameof(events), events);

            var eventsCollection = new EventsCollection();

            eventsCollection.AddRange(events);

            return(eventsCollection.ManageTimedEvents().Events.ToList());
        }
        /// <summary>
        /// Gets chords contained in the specified collection of <see cref="MidiEvent"/>.
        /// </summary>
        /// <param name="events">Collection of<see cref="MidiFile"/> to search for chords.</param>
        /// <param name="notesTolerance">Notes tolerance that defines maximum distance of notes from the
        /// start of the first note of a chord. Notes within this tolerance will be considered as a chord.</param>
        /// <returns>Collection of chords contained in <paramref name="events"/> ordered by time.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="events"/> is <c>null</c>.</exception>
        public static IEnumerable <Chord> GetChords(this IEnumerable <MidiEvent> events, long notesTolerance = 0)
        {
            ThrowIfArgument.IsNull(nameof(events), events);
            ThrowIfNotesTolerance.IsNegative(nameof(notesTolerance), notesTolerance);

            var eventsCollection = new EventsCollection();

            eventsCollection.AddRange(events);

            return(eventsCollection.ManageChords(notesTolerance).Chords.ToList());
        }
        private void GetNotes_DetectionSettings_EventsCollection(
            ContainerType containerType,
            NoteDetectionSettings settings,
            ICollection <MidiEvent> midiEvents,
            ICollection <Note> expectedNotes)
        {
            switch (containerType)
            {
            case ContainerType.EventsCollection:
            {
                var eventsCollection = new EventsCollection();
                eventsCollection.AddRange(midiEvents);

                var notes = eventsCollection.GetNotes(settings);
                MidiAsserts.AreEqual(expectedNotes, notes, "Notes are invalid.");

                var timedObjects = eventsCollection.GetObjects(ObjectType.Note, new ObjectDetectionSettings {
                        NoteDetectionSettings = settings
                    });
                MidiAsserts.AreEqual(expectedNotes, timedObjects, "Notes are invalid from GetObjects.");
            }
            break;

            case ContainerType.TrackChunk:
            {
                var trackChunk = new TrackChunk(midiEvents);

                var notes = trackChunk.GetNotes(settings);
                MidiAsserts.AreEqual(expectedNotes, notes, "Notes are invalid.");

                var timedObjects = trackChunk.GetObjects(ObjectType.Note, new ObjectDetectionSettings {
                        NoteDetectionSettings = settings
                    });
                MidiAsserts.AreEqual(expectedNotes, timedObjects, "Notes are invalid from GetObjects.");
            }
            break;

            case ContainerType.TrackChunks:
            case ContainerType.File:
            {
                GetNotes_DetectionSettings_TrackChunks(
                    containerType == ContainerType.File,
                    settings,
                    new[] { midiEvents },
                    expectedNotes);
            }
            break;
            }
        }
        private void GetChords_EventsCollection(
            ContainerType containerType,
            ICollection <MidiEvent> midiEvents,
            ICollection <Chord> expectedChords)
        {
            switch (containerType)
            {
            case ContainerType.EventsCollection:
            {
                var eventsCollection = new EventsCollection();
                eventsCollection.AddRange(midiEvents);

                var chords = eventsCollection.GetChords();
                MidiAsserts.AreEqual(expectedChords, chords, "Chords are invalid.");

                var timedObjects = eventsCollection.GetObjects(ObjectType.Chord);
                MidiAsserts.AreEqual(expectedChords, timedObjects, "Chords are invalid from GetObjects.");
            }
            break;

            case ContainerType.TrackChunk:
            {
                var trackChunk = new TrackChunk(midiEvents);

                var chords = trackChunk.GetChords();
                MidiAsserts.AreEqual(expectedChords, chords, "Chords are invalid.");

                var timedObjects = trackChunk.GetObjects(ObjectType.Chord);
                MidiAsserts.AreEqual(expectedChords, timedObjects, "Chords are invalid from GetObjects.");
            }
            break;

            case ContainerType.TrackChunks:
            case ContainerType.File:
            {
                GetChords_TrackChunks(
                    containerType == ContainerType.File,
                    new[] { midiEvents },
                    expectedChords);
            }
            break;
            }
        }