public void PartStartMarkerEventFactory_PartEndMarkerEventFactory_DontPreserveTimes(long firstEventTime) { var timedEvents = new[] { new TimedEvent(new TextEvent("A"), firstEventTime), new TimedEvent(new InstrumentNameEvent("Test instrument"), 10), new TimedEvent(new TextEvent("B"), 90), new TimedEvent(new TextEvent("C"), 210) }; var midiFile = timedEvents.ToFile(); var grid = new SteppedGrid((MidiTimeSpan)100); var settings = new SliceMidiFileSettings { SplitNotes = false, PreserveTimes = false, Markers = new SliceMidiFileMarkers { PartStartMarkerEventFactory = () => new MarkerEvent("S"), PartEndMarkerEventFactory = () => new MarkerEvent("E") } }; var newFiles = midiFile.SplitByGrid(grid, settings).ToList(); Assert.AreEqual(3, newFiles.Count, "New files count is invalid."); CompareTimedEvents( newFiles[0].GetTimedEvents(), new[] { new TimedEvent(new MarkerEvent("S"), 0), new TimedEvent(new TextEvent("A"), firstEventTime), new TimedEvent(new InstrumentNameEvent("Test instrument"), 10), new TimedEvent(new TextEvent("B"), 90), new TimedEvent(new MarkerEvent("E"), 100) }, "First file contains invalid events."); CompareTimedEvents( newFiles[1].GetTimedEvents(), new[] { new TimedEvent(new MarkerEvent("S"), 0), new TimedEvent(new InstrumentNameEvent("Test instrument"), 0), new TimedEvent(new MarkerEvent("E"), 100) }, "Second file contains invalid events."); CompareTimedEvents( newFiles[2].GetTimedEvents(), new[] { new TimedEvent(new MarkerEvent("S"), 0), new TimedEvent(new InstrumentNameEvent("Test instrument"), 0), new TimedEvent(new TextEvent("C"), 10), new TimedEvent(new MarkerEvent("E"), 100) }, "Third file contains invalid events."); }
public void Print(string nome, MidiFile midiFile) { var grid = new SteppedGrid(step: new BarBeatTimeSpan(bars: 1, beats: 0, ticks: 0)); var midiMeasures = midiFile.SplitByGrid(grid, new SplittingMidiFileByGridSettings { RemoveEmptyFiles = true }); Console.WriteLine("-----------------------inĂcio----------------------"); var i = 1; foreach (var midiMeasure in midiMeasures) { var tempoMapMeasure = midiMeasure.GetTempoMap(); Console.WriteLine($"Measure {i}"); var notesAndRests = midiMeasure.GetTrackChunks().Last().GetNotes().GetNotesAndRests(RestSeparationPolicy.NoSeparation); notesAndRests.ToList().ForEach(e => Console.WriteLine($"Event: {e.ToString()}, Time: {e.Time}, Length: {e.Length}")); Console.WriteLine("---------------------------------------------------"); if (File.Exists($@"Files\{nome}-{i}.mid")) { File.Delete($@"Files\{nome}-{i}.mid"); } midiMeasure.Write($@"Files\{nome}-{i}.mid"); i++; } }
public void SplitByGrid_DontSplitNotes_DontPreserveTimes() { var timedEvents = new[] { new TimedEvent(new SetTempoEvent(100000), 0), new TimedEvent(new InstrumentNameEvent("Test instrument"), 10), new TimedEvent(new SetTempoEvent(200000), 90), new TimedEvent(new NoteOnEvent((SevenBitNumber)50, (SevenBitNumber)100), 90), new TimedEvent(new NoteOnEvent((SevenBitNumber)20, (SevenBitNumber)100), 150), new TimedEvent(new NoteOffEvent((SevenBitNumber)50, (SevenBitNumber)100), 190), new TimedEvent(new NoteOffEvent((SevenBitNumber)20, (SevenBitNumber)100), 200), new TimedEvent(new TextEvent("Test"), 200) }; var midiFile = timedEvents.ToFile(); var grid = new SteppedGrid((MidiTimeSpan)100); var settings = new SliceMidiFileSettings { SplitNotes = false, PreserveTimes = false }; var newFiles = midiFile.SplitByGrid(grid, settings).ToList(); Assert.AreEqual(3, newFiles.Count, "New files count is invalid."); CompareTimedEvents( newFiles[0].GetTimedEvents(), new[] { new TimedEvent(new SetTempoEvent(100000), 0), new TimedEvent(new InstrumentNameEvent("Test instrument"), 10), new TimedEvent(new SetTempoEvent(200000), 90), new TimedEvent(new NoteOnEvent((SevenBitNumber)50, (SevenBitNumber)100), 90) }, "First file contains invalid events."); CompareTimedEvents( newFiles[1].GetTimedEvents(), new[] { new TimedEvent(new SetTempoEvent(200000), 0), new TimedEvent(new InstrumentNameEvent("Test instrument"), 0), new TimedEvent(new NoteOnEvent((SevenBitNumber)20, (SevenBitNumber)100), 50), new TimedEvent(new NoteOffEvent((SevenBitNumber)50, (SevenBitNumber)100), 90), new TimedEvent(new NoteOffEvent((SevenBitNumber)20, (SevenBitNumber)100), 100), }, "Second file contains invalid events."); CompareTimedEvents( newFiles[2].GetTimedEvents(), new[] { new TimedEvent(new SetTempoEvent(200000), 0), new TimedEvent(new InstrumentNameEvent("Test instrument"), 0), new TimedEvent(new TextEvent("Test"), 0) }, "Third file contains invalid events."); }
public void SplitByGrid_EmptyFile() { var midiFile = new MidiFile(); var grid = new SteppedGrid(MusicalTimeSpan.Eighth); Assert.IsFalse(midiFile.SplitByGrid(grid).Any(), "Empty file splitting produced non-empty result."); }
private ICollection <MidiFile> SplitMidiInMeasures(MidiFile midiFile) { var grid = new SteppedGrid(step: new BarBeatTimeSpan(bars: 1, beats: 0, ticks: 0)); return(midiFile.SplitByGrid(grid, new SplittingMidiFileByGridSettings { RemoveEmptyFiles = true }).ToList()); }
public void SplitByGrid_RemoveEmptyFiles(bool removeEmptyFiles) { var timedEvents = new[] { new TimedEvent(new NoteOnEvent((SevenBitNumber)50, (SevenBitNumber)100), 90), new TimedEvent(new NoteOffEvent((SevenBitNumber)50, (SevenBitNumber)70), 95), new TimedEvent(new NoteOnEvent((SevenBitNumber)20, (SevenBitNumber)100), 300), new TimedEvent(new NoteOffEvent((SevenBitNumber)20, (SevenBitNumber)100), 400) }; var midiFile = timedEvents.ToFile(); var grid = new SteppedGrid((MidiTimeSpan)100); var settings = new SplittingMidiFileByGridSettings { SplitNotes = false, PreserveTimes = false, RemoveEmptyFiles = removeEmptyFiles }; var newFiles = midiFile.SplitByGrid(grid, settings).ToList(); Assert.AreEqual(removeEmptyFiles ? 2 : 4, newFiles.Count, "New files count is invalid."); CompareTimedEvents( newFiles[0].GetTimedEvents(), new[] { new TimedEvent(new NoteOnEvent((SevenBitNumber)50, (SevenBitNumber)100), 90), new TimedEvent(new NoteOffEvent((SevenBitNumber)50, (SevenBitNumber)70), 95), }, "First file contains invalid events."); if (!removeEmptyFiles) { CompareTimedEvents( newFiles[1].GetTimedEvents(), Enumerable.Empty <TimedEvent>(), "Second file contains invalid events."); CompareTimedEvents( newFiles[2].GetTimedEvents(), Enumerable.Empty <TimedEvent>(), "Third file contains invalid events."); } CompareTimedEvents( newFiles[removeEmptyFiles ? 1 : 3].GetTimedEvents(), new[] { new TimedEvent(new NoteOnEvent((SevenBitNumber)20, (SevenBitNumber)100), 0), new TimedEvent(new NoteOffEvent((SevenBitNumber)20, (SevenBitNumber)100), 100) }, "Last file contains invalid events."); }
public void SplitByGrid_PreserveTrackChunks(bool preserveTrackChunks) { var timedEvents1 = new[] { new TimedEvent(new NoteOnEvent((SevenBitNumber)50, (SevenBitNumber)100), 90), new TimedEvent(new NoteOffEvent((SevenBitNumber)50, (SevenBitNumber)70), 95), new TimedEvent(new NoteOnEvent((SevenBitNumber)20, (SevenBitNumber)100), 100), new TimedEvent(new NoteOffEvent((SevenBitNumber)20, (SevenBitNumber)100), 200) }; var timedEvents2 = new[] { new TimedEvent(new NoteOnEvent((SevenBitNumber)21, (SevenBitNumber)100), 100), new TimedEvent(new NoteOffEvent((SevenBitNumber)21, (SevenBitNumber)100), 200) }; var midiFile = new MidiFile( timedEvents1.ToTrackChunk(), timedEvents2.ToTrackChunk()); var grid = new SteppedGrid((MidiTimeSpan)100); var settings = new SliceMidiFileSettings { SplitNotes = false, PreserveTimes = false, PreserveTrackChunks = preserveTrackChunks }; var newFiles = midiFile.SplitByGrid(grid, settings).ToList(); Assert.AreEqual(2, newFiles.Count, "New files count is invalid."); Assert.AreEqual(preserveTrackChunks ? 2 : 1, newFiles[0].GetTrackChunks().Count(), "Track chunks count of the first file is invalid."); CompareTimedEvents( newFiles[0].GetTrackChunks().First().GetTimedEvents(), new[] { new TimedEvent(new NoteOnEvent((SevenBitNumber)50, (SevenBitNumber)100), 90), new TimedEvent(new NoteOffEvent((SevenBitNumber)50, (SevenBitNumber)70), 95), }, "First track chunk of first file contains invalid events."); if (preserveTrackChunks) { CompareTimedEvents( newFiles[0].GetTrackChunks().Last().GetTimedEvents(), Enumerable.Empty <TimedEvent>(), "Second track chunk of first file contains invalid events."); } Assert.AreEqual(2, newFiles[1].GetTrackChunks().Count(), "Track chunks count of the second file is invalid."); CompareTimedEvents( newFiles[1].GetTrackChunks().First().GetTimedEvents(), new[] { new TimedEvent(new NoteOnEvent((SevenBitNumber)20, (SevenBitNumber)100), 0), new TimedEvent(new NoteOffEvent((SevenBitNumber)20, (SevenBitNumber)100), 100) }, "First track chunk of second file contains invalid events."); CompareTimedEvents( newFiles[1].GetTrackChunks().Last().GetTimedEvents(), new[] { new TimedEvent(new NoteOnEvent((SevenBitNumber)21, (SevenBitNumber)100), 0), new TimedEvent(new NoteOffEvent((SevenBitNumber)21, (SevenBitNumber)100), 100) }, "Second track chunk of second file contains invalid events."); }
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."); }
static void Main(string[] args) { // Gets fullpath of each file in directory string[] filePaths = Directory.GetFiles(@"C:\\Users\\Dave\\Music\\FYPDataset\\2018", "*.mid", SearchOption.TopDirectoryOnly); // 1. Edit directory here to change folder. Ensures only ".mid" files are returned int length = filePaths.Length; Console.WriteLine("Number of files: {0}", length); // Creates the grid to split midi files at (30 second intervals = 3E7) IGrid grid = new SteppedGrid(new MetricTimeSpan(30000000)); // Reading and Split settings var readingSettings = new ReadingSettings { NotEnoughBytesPolicy = NotEnoughBytesPolicy.Abort, InvalidChunkSizePolicy = InvalidChunkSizePolicy.Abort }; var splitSettings = new SliceMidiFileSettings { SplitNotes = true, PreserveTimes = false, PreserveTrackChunks = false }; // Creating new output directory string newDirectory = "C:\\Users\\Dave\\Music\\FYPDataset\\2018\\Trimmed18"; // 2. Edit here to create new output folder Directory.CreateDirectory(newDirectory); int counterFiles = 0; // Begin loop for each file in directory foreach (string file in filePaths) { string filePath = file; Console.WriteLine(filePath); // Reads a singular midi file MidiFile mf = MidiFile.Read(filePath, readingSettings); // Creates a IEnumerable object of class MidiFile (containing all smaller midifiles) IEnumerable <MidiFile> midi = MidiFileSplitter.SplitByGrid(mf, grid, splitSettings).ToList(); int counterSplits = 0; foreach (var i in midi) // For each split file { string fileWrite = String.Format("{0}\\SplitTrack{1}_{2}.MID", newDirectory, counterFiles, counterSplits); Console.WriteLine(fileWrite); i.Write(fileWrite, overwriteFile: true, format: MidiFileFormat.MultiTrack); ++counterSplits; // Increment split number } ++counterFiles; // Increment file number } Console.WriteLine("Midi File Splitting Complete!"); Console.Read(); }