public void ReadWriteCustomChunk()
        {
            const int    expectedA = 1234567;
            const string expectedB = "Test";
            const byte   expectedC = 45;

            var customChunkTypes = new ChunkTypesCollection
            {
                { typeof(CustomChunk), CustomChunk.Id }
            };

            var readingSettings = new ReadingSettings {
                CustomChunkTypes = customChunkTypes
            };

            var midiFile = MidiFileTestUtilities.Read(
                new MidiFile(
                    new TrackChunk(
                        new TextEvent("foo"),
                        new MarkerEvent("bar")),
                    new CustomChunk(expectedA, expectedB, expectedC)),
                null,
                readingSettings);

            var customChunks = midiFile.Chunks.OfType <CustomChunk>().ToArray();

            Assert.AreEqual(1, customChunks.Length, "Custom chunks count is invalid.");

            var customChunk = customChunks.First();

            Assert.AreEqual(expectedA, customChunk.A, "A value is invalid");
            Assert.AreEqual(expectedB, customChunk.B, "B value is invalid");
            Assert.AreEqual(expectedC, customChunk.C, "C value is invalid");
        }
예제 #2
0
        public void CompareOriginalFormat_Read_DifferentFormats()
        {
            var midiFile1 = MidiFileTestUtilities.Read(
                new MidiFile(new TrackChunk(new NoteOnEvent {
                DeltaTime = 100
            })),
                null,
                null,
                MidiFileFormat.MultiTrack);
            var midiFile2 = MidiFileTestUtilities.Read(
                new MidiFile(new TrackChunk(new NoteOnEvent {
                DeltaTime = 100
            })),
                null,
                null,
                MidiFileFormat.SingleTrack);

            var areEqual = MidiFile.Equals(
                midiFile1,
                midiFile2,
                new MidiFileEqualityCheckSettings {
                CompareOriginalFormat = true
            },
                out var message);

            Assert.IsFalse(areEqual, "Files are equal.");
            Assert.IsNotNull(message, "Message is null.");
            Assert.IsNotEmpty(message, "Message is empty.");
        }
예제 #3
0
        public void DontCompareOriginalFormat_Read(MidiFileFormat firstFormat, MidiFileFormat secondFormat)
        {
            var midiFile1 = MidiFileTestUtilities.Read(
                new MidiFile(new TrackChunk(new NoteOnEvent {
                DeltaTime = 100
            })),
                null,
                null,
                firstFormat);
            var midiFile2 = MidiFileTestUtilities.Read(
                new MidiFile(new TrackChunk(new NoteOnEvent {
                DeltaTime = 100
            })),
                null,
                null,
                secondFormat);

            var areEqual = MidiFile.Equals(
                midiFile1,
                midiFile2,
                new MidiFileEqualityCheckSettings {
                CompareOriginalFormat = false
            },
                out var message);

            Assert.IsTrue(areEqual, "Files aren't equal.");
            Assert.IsNull(message, "Message isn't null.");
        }
예제 #4
0
        public void AllEventTypesAreReadCorrectly(Func <Type, MidiEvent> createMidiEvent)
        {
            var events = TypesProvider.GetAllEventTypes()
                         .Where(t => !typeof(SystemCommonEvent).IsAssignableFrom(t) &&
                                !typeof(SystemRealTimeEvent).IsAssignableFrom(t) &&
                                t != typeof(EndOfTrackEvent) &&
                                t != typeof(UnknownMetaEvent))
                         .Select(createMidiEvent)
                         .ToArray();

            var midiFile = MidiFileTestUtilities.Read(
                new MidiFile(new TrackChunk(events)),
                null,
                new ReadingSettings {
                SilentNoteOnPolicy = SilentNoteOnPolicy.NoteOn
            },
                MidiFileFormat.SingleTrack);

            var readEvents = midiFile.GetEvents().ToArray();

            for (var i = 0; i < events.Length; i++)
            {
                var expectedEvent = events[i];
                var actualEvent   = readEvents[i];

                MidiAsserts.AreEventsEqual(expectedEvent, actualEvent, true, $"Event {i} is invalid.");
            }
        }
예제 #5
0
        public void AllEventTypesAreReadCorrectly()
        {
            var events = TypesProvider.GetAllEventTypes()
                         .Where(t => !typeof(SystemCommonEvent).IsAssignableFrom(t) &&
                                !typeof(SystemRealTimeEvent).IsAssignableFrom(t) &&
                                t != typeof(EndOfTrackEvent) &&
                                t != typeof(UnknownMetaEvent))
                         .Select(t =>
            {
                var instance = (MidiEvent)Activator.CreateInstance(t);

                if (instance is SysExEvent sysExEvent)
                {
                    sysExEvent.Data = new byte[] { 1, 2, 3 }
                }
                ;

                if (instance is SequencerSpecificEvent sequencerSpecificEvent)
                {
                    sequencerSpecificEvent.Data = new byte[] { 1, 2, 3 }
                }
                ;

                if (instance is NoteOnEvent noteOnEvent)
                {
                    noteOnEvent.Velocity = (SevenBitNumber)100;
                }

                if (instance is BaseTextEvent baseTextEvent)
                {
                    baseTextEvent.Text = Guid.NewGuid().ToString();
                }

                return(instance);
            })
                         .ToArray();

            var midiFile   = MidiFileTestUtilities.Read(new MidiFile(new TrackChunk(events)), null, null, MidiFileFormat.SingleTrack);
            var readEvents = midiFile.GetEvents().ToArray();

            for (var i = 0; i < events.Length; i++)
            {
                var expectedEvent = events[i];
                var actualEvent   = readEvents[i];

                MidiAsserts.AreEventsEqual(expectedEvent, actualEvent, true, $"Event {i} is invalid.");
            }
        }
예제 #6
0
        public void ReadWriteRead()
        {
            foreach (var filePath in TestFilesProvider.GetValidFilesPaths())
            {
                MidiFile midiFile  = null;
                MidiFile midiFile2 = null;

                Assert.DoesNotThrow(() =>
                {
                    midiFile  = MidiFile.Read(filePath);
                    midiFile2 = MidiFileTestUtilities.Read(midiFile, null, null);
                },
                                    $"Read/Write/Read failed for '{filePath}'.");

                Assert.IsNotNull(midiFile, "MIDI file is null.");
                MidiAsserts.AreFilesEqual(midiFile, midiFile2, true, $"Reread failed for '{filePath}'.");
            }
        }
        public void ReadWriteCustomMetaEvent()
        {
            const int    expectedA = 1234567;
            const string expectedB = "Test";
            const byte   expectedC = 45;

            var customMetaEventTypes = new EventTypesCollection
            {
                { typeof(CustomMetaEvent), 0x5A }
            };

            var writingSettings = new WritingSettings {
                CustomMetaEventTypes = customMetaEventTypes
            };
            var readingSettings = new ReadingSettings {
                CustomMetaEventTypes = customMetaEventTypes
            };

            var midiFile = MidiFileTestUtilities.Read(
                new MidiFile(
                    new TrackChunk(
                        new CustomMetaEvent(expectedA, expectedB, expectedC)
            {
                DeltaTime = 100
            },
                        new TextEvent("foo"),
                        new MarkerEvent("bar"))),
                writingSettings,
                readingSettings);

            var customMetaEvents = midiFile.GetEvents().OfType <CustomMetaEvent>().ToArray();

            Assert.AreEqual(1, customMetaEvents.Length, "Custom meta events count is invalid.");

            var customMetaEvent = customMetaEvents.First();

            Assert.AreEqual(100, customMetaEvent.DeltaTime, "Delta-time is invalid.");
            Assert.AreEqual(expectedA, customMetaEvent.A, "A value is invalid");
            Assert.AreEqual(expectedB, customMetaEvent.B, "B value is invalid");
            Assert.AreEqual(expectedC, customMetaEvent.C, "C value is invalid");
        }