Example #1
0
        private static void AreEqual(TimedEvent expectedTimedEvent, TimedEvent actualTimedEvent, bool compareDeltaTimes, long timesEpsilon, string message)
        {
            Assert.IsTrue(Math.Abs(expectedTimedEvent.Time - actualTimedEvent.Time) <= timesEpsilon, $"{message} Time is invalid.");

            string eventsEqualityCheckMessage;

            Assert.IsTrue(
                MidiEvent.Equals(expectedTimedEvent.Event, actualTimedEvent.Event, new MidiEventEqualityCheckSettings {
                CompareDeltaTimes = compareDeltaTimes
            }, out eventsEqualityCheckMessage),
                $"{message} {eventsEqualityCheckMessage}");
        }
Example #2
0
        public static void AreEventsEqual(MidiEvent midiEvent1, MidiEvent midiEvent2, bool compareDeltaTimes, string message = null)
        {
            string eventsComparingMessage;
            var    areEqual = MidiEvent.Equals(
                midiEvent1,
                midiEvent2,
                new MidiEventEqualityCheckSettings {
                CompareDeltaTimes = compareDeltaTimes
            },
                out eventsComparingMessage);

            Assert.IsTrue(areEqual, $"{message} {eventsComparingMessage}");
        }
        private static void CheckProgramEvents(IReadOnlyList <SentEvent> sentEvents, IReadOnlyList <MidiEvent> expectedProgramEvents)
        {
            foreach (var programEvent in expectedProgramEvents)
            {
                string message;
                var    sentEvent = sentEvents.FirstOrDefault(e => MidiEvent.Equals(e.Event, programEvent, new MidiEventEqualityCheckSettings {
                    CompareDeltaTimes = false
                }, out message));
                Assert.IsNotNull(sentEvent, $"Program event {programEvent} was not sent.");

                Assert.LessOrEqual(
                    sentEvent.Time,
                    SendReceiveUtilities.MaximumEventSendReceiveDelay,
                    $"Program event was sent at wrong time (at {sentEvent.Time} instead of zero).");
            }
        }
Example #4
0
        public void TextComparison_Ordinal_SameTexts()
        {
            var midiEvent1 = new TextEvent {
                Text = "abc"
            };
            var midiEvent2 = new TextEvent {
                Text = "abc"
            };

            var settings = new MidiEventEqualityCheckSettings {
                TextComparison = System.StringComparison.Ordinal
            };
            var areEqual = MidiEvent.Equals(midiEvent1, midiEvent2, settings, out var message);

            Assert.IsTrue(areEqual, "Events aren't equal.");
            Assert.IsNull(message, "Message isn't null.");
        }
Example #5
0
        public void DontCompareDeltaTimes(long firstDeltaTime, long secondDeltaTime)
        {
            var midiEvent1 = new NoteOnEvent {
                DeltaTime = firstDeltaTime
            };
            var midiEvent2 = new NoteOnEvent {
                DeltaTime = secondDeltaTime
            };

            var settings = new MidiEventEqualityCheckSettings {
                CompareDeltaTimes = false
            };
            var areEqual = MidiEvent.Equals(midiEvent1, midiEvent2, settings, out var message);

            Assert.IsTrue(areEqual, "Events aren't equal.");
            Assert.IsNull(message, "Message isn't null.");
        }
Example #6
0
        public void TextComparison_OrdinalIgnoreCase(string firstText, string secondText)
        {
            var midiEvent1 = new TextEvent {
                Text = firstText
            };
            var midiEvent2 = new TextEvent {
                Text = secondText
            };

            var settings = new MidiEventEqualityCheckSettings {
                TextComparison = System.StringComparison.OrdinalIgnoreCase
            };
            var areEqual = MidiEvent.Equals(midiEvent1, midiEvent2, settings, out var message);

            Assert.IsTrue(areEqual, "Events aren't equal.");
            Assert.IsNull(message, "Message isn't null.");
        }
Example #7
0
        public void CompareDeltaTimes_SameDeltaTimes()
        {
            var midiEvent1 = new NoteOnEvent {
                DeltaTime = 100
            };
            var midiEvent2 = new NoteOnEvent {
                DeltaTime = 100
            };

            var settings = new MidiEventEqualityCheckSettings {
                CompareDeltaTimes = true
            };
            var areEqual = MidiEvent.Equals(midiEvent1, midiEvent2, settings, out var message);

            Assert.IsTrue(areEqual, "Events aren't equal.");
            Assert.IsNull(message, "Message isn't null.");
        }
Example #8
0
        public void TextComparison_Ordinal_DifferentTexts()
        {
            var midiEvent1 = new TextEvent {
                Text = "abc"
            };
            var midiEvent2 = new TextEvent {
                Text = "Abc"
            };

            var settings = new MidiEventEqualityCheckSettings {
                TextComparison = System.StringComparison.Ordinal
            };
            var areEqual = MidiEvent.Equals(midiEvent1, midiEvent2, settings, out var message);

            Assert.IsFalse(areEqual, "Events are equal.");
            Assert.IsNotNull(message, "Message is null.");
            Assert.IsNotEmpty(message, "Message is empty.");
        }
Example #9
0
        public void CompareDeltaTimes_DifferentDeltaTimes()
        {
            var midiEvent1 = new NoteOnEvent {
                DeltaTime = 100
            };
            var midiEvent2 = new NoteOnEvent {
                DeltaTime = 1000
            };

            var settings = new MidiEventEqualityCheckSettings {
                CompareDeltaTimes = true
            };
            var areEqual = MidiEvent.Equals(midiEvent1, midiEvent2, settings, out var message);

            Assert.IsFalse(areEqual, "Events are equal.");
            Assert.IsNotNull(message, "Message is null.");
            Assert.IsNotEmpty(message, "Message is empty.");
        }
            public bool Equals(TimedEvent timedEvent1, TimedEvent timedEvent2)
            {
                if (ReferenceEquals(timedEvent1, timedEvent2))
                {
                    return(true);
                }

                if (ReferenceEquals(null, timedEvent1) || ReferenceEquals(null, timedEvent2))
                {
                    return(false);
                }

                string message;

                return(Math.Abs(timedEvent1.Time - timedEvent2.Time) <= _timesEpsilon &&
                       MidiEvent.Equals(timedEvent1.Event, timedEvent2.Event, new MidiEventEqualityCheckSettings {
                    CompareDeltaTimes = _compareDeltaTimes
                }, out message));
            }
Example #11
0
        public static bool Equals(EventsCollection eventsCollection1, EventsCollection eventsCollection2, MidiEventEqualityCheckSettings settings, out string message)
        {
            message = null;

            if (ReferenceEquals(eventsCollection1, eventsCollection2))
            {
                return(true);
            }

            if (ReferenceEquals(null, eventsCollection1) || ReferenceEquals(null, eventsCollection2))
            {
                message = "One of events collections is null.";
                return(false);
            }

            if (eventsCollection1.Count != eventsCollection2.Count)
            {
                message = $"Counts of events are different ({eventsCollection1.Count} vs {eventsCollection2.Count}).";
                return(false);
            }

            for (var i = 0; i < eventsCollection1.Count; i++)
            {
                var event1 = eventsCollection1[i];
                var event2 = eventsCollection2[i];

                string eventsComparingMessage;
                if (!MidiEvent.Equals(event1, event2, settings, out eventsComparingMessage))
                {
                    message = $"Events at position {i} are different. {eventsComparingMessage}";
                    return(false);
                }
            }

            return(true);
        }
Example #12
0
        public static bool Equals(MidiChunk midiChunk1, MidiChunk midiChunk2, MidiChunkEqualityCheckSettings settings, out string message)
        {
            message = null;

            if (ReferenceEquals(midiChunk1, midiChunk2))
            {
                return(true);
            }

            if (ReferenceEquals(null, midiChunk1) || ReferenceEquals(null, midiChunk2))
            {
                message = "One of chunks is null.";
                return(false);
            }

            var type1 = midiChunk1.GetType();
            var type2 = midiChunk2.GetType();

            if (type1 != type2)
            {
                message = $"Types of chunks are different ({type1} vs {type2}).";
                return(false);
            }

            var trackChunk1 = midiChunk1 as TrackChunk;

            if (trackChunk1 != null)
            {
                var trackChunk2 = (TrackChunk)midiChunk2;

                var events1 = trackChunk1.Events;
                var events2 = trackChunk2.Events;

                if (events1.Count != events2.Count)
                {
                    message = $"Counts of events in track chunks are different ({events1.Count} vs {events2.Count}).";
                    return(false);
                }

                for (var i = 0; i < events1.Count; i++)
                {
                    var event1 = events1[i];
                    var event2 = events2[i];

                    string eventsComparingMessage;
                    if (!MidiEvent.Equals(event1, event2, settings.EventEqualityCheckSettings, out eventsComparingMessage))
                    {
                        message = $"Events at position {i} in track chunks are different. {eventsComparingMessage}";
                        return(false);
                    }
                }

                return(true);
            }

            var unknownChunk1 = midiChunk1 as UnknownChunk;

            if (unknownChunk1 != null)
            {
                var unknownChunk2 = (UnknownChunk)midiChunk2;

                var chunkId1 = unknownChunk1.ChunkId;
                var chunkId2 = unknownChunk2.ChunkId;

                if (chunkId1 != chunkId2)
                {
                    message = $"IDs of unknown chunks are different ({chunkId1} vs {chunkId2}).";
                    return(false);
                }

                if (!ArrayUtilities.Equals(unknownChunk1.Data, unknownChunk2.Data))
                {
                    message = "Unknown chunks data are different.";
                    return(false);
                }

                return(true);
            }

            return(midiChunk1.Equals(midiChunk2));
        }
Example #13
0
        public void SplitByGridAndRemoveEmptyPartsAndMergeIntoNewFile()
        {
            var timedEvents1 = new[]
            {
                new TimedEvent(new TextEvent("A1"), 0),
                new TimedEvent(new SetTempoEvent(100000), 10),
                new TimedEvent(new TextEvent("B1"), 90),
                new TimedEvent(new TextEvent("C1"), 210),
                new TimedEvent(new NoteOnEvent(), 270),
                new TimedEvent(new NoteOffEvent(), 320)
            };

            var timedEvents2 = new[]
            {
                new TimedEvent(new TextEvent("A2"), 10),
                new TimedEvent(new TextEvent("B2"), 70),
                new TimedEvent(new TextEvent("C2"), 260)
            };

            var trackChunk1 = timedEvents1.ToTrackChunk();
            var trackChunk2 = timedEvents2.ToTrackChunk();
            var midiFile    = new MidiFile(trackChunk1, trackChunk2);

            var grid     = new SteppedGrid((MidiTimeSpan)100);
            var settings = new SliceMidiFileSettings
            {
                SplitNotes          = false,
                PreserveTimes       = false,
                PreserveTrackChunks = true,
                Markers             = new SliceMidiFileMarkers
                {
                    PartStartMarkerEventFactory = () => new MarkerEvent("S"),
                    PartEndMarkerEventFactory   = () => new MarkerEvent("F"),
                    EmptyPartMarkerEventFactory = () => new MarkerEvent("E")
                }
            };

            var newFiles = midiFile.SplitByGrid(grid, settings).ToList();

            Assert.AreEqual(4, newFiles.Count, "New files count is invalid.");

            //

            var nonEmptyParts = newFiles.Where(f =>
            {
                var newTimedEvents = f.GetTimedEvents().ToArray();
                return(newTimedEvents.Any() && !newTimedEvents.Any(e => MidiEvent.Equals(e.Event, new MarkerEvent("E"))));
            })
                                .ToArray();

            Assert.AreEqual(3, nonEmptyParts.Length, "Non-empty new files count is invalid.");

            //

            var trackChunksCount = midiFile.GetTrackChunks().Count();
            var newTrackChunks   = new List <TrackChunk>();

            for (var i = 0; i < trackChunksCount; i++)
            {
                var trackChunk = new TrackChunk();

                foreach (var part in nonEmptyParts)
                {
                    trackChunk.Events.AddRange(part.GetTrackChunks().ElementAt(i).Events);
                }

                newTrackChunks.Add(trackChunk);
            }

            var resultFile = new MidiFile(newTrackChunks)
            {
                TimeDivision = midiFile.TimeDivision
            };

            //

            var equalityCheckSettings = new MidiEventEqualityCheckSettings
            {
                CompareDeltaTimes = false
            };

            resultFile.RemoveTimedEvents(e =>
                                         MidiEvent.Equals(e.Event, new MarkerEvent("S"), equalityCheckSettings) ||
                                         MidiEvent.Equals(e.Event, new MarkerEvent("F"), equalityCheckSettings) ||
                                         MidiEvent.Equals(e.Event, new MarkerEvent("E"), equalityCheckSettings));

            //

            CompareTimedEvents(
                resultFile.GetTimedEvents(),
                new[]
            {
                new TimedEvent(new TextEvent("A1"), 0),
                new TimedEvent(new SetTempoEvent(100000), 10),
                new TimedEvent(new TextEvent("A2"), 10),
                new TimedEvent(new TextEvent("B2"), 70),
                new TimedEvent(new TextEvent("B1"), 90),
                new TimedEvent(new SetTempoEvent(100000), 100),
                new TimedEvent(new TextEvent("C1"), 110),
                new TimedEvent(new TextEvent("C2"), 160),
                new TimedEvent(new NoteOnEvent(), 170),
                new TimedEvent(new SetTempoEvent(100000), 200),
                new TimedEvent(new NoteOffEvent(), 220)
            },
                "Result file contains invalid events.");
        }