From 75b3ffeeabdb1de5d9043c33a89b34691db7c34d Mon Sep 17 00:00:00 2001 From: ro5490 <17776299+ro5490@users.noreply.github.com> Date: Mon, 25 Nov 2024 00:14:03 +0000 Subject: [PATCH 1/3] Adds Invalid event handling, for bulk conversion --- MIDIProcessor/Program.cs | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/MIDIProcessor/Program.cs b/MIDIProcessor/Program.cs index 6f57f76..1f512d6 100644 --- a/MIDIProcessor/Program.cs +++ b/MIDIProcessor/Program.cs @@ -42,11 +42,37 @@ static void Main(string[] args) Console.Read(); } - public static void ConvertMidiToText(string midiFilePath, string textFilePath, string statsFilePath) + public static void ConvertMidiToText(string midiFilePath, string textFilePath, string statsFilePath) { - var midiFile = MidiFile.Read(midiFilePath); - var tempoMap = midiFile.GetTempoMap(); + // Configure ReadingSettings to handle invalid events + var readingSettings = new ReadingSettings + { + // Skip events with invalid parameter values + InvalidChannelEventParameterValuePolicy = InvalidChannelEventParameterValuePolicy.SnapToLimits + }; + + MidiFile midiFile; + + try + { + // Read the MIDI file with the custom settings + midiFile = MidiFile.Read(midiFilePath, readingSettings); + } + catch (Exception ex) + { + // Log the error to the console or stats file and skip the problematic file + Console.WriteLine($"Error reading file {Path.GetFileName(midiFilePath)}: {ex.Message}"); + + // Optionally log the error to the stats file + using (StreamWriter statsWriter = new StreamWriter(statsFilePath, true)) + { + statsWriter.WriteLine($"Error reading file {Path.GetFileName(midiFilePath)}: {ex.Message}"); + statsWriter.WriteLine(new string('-', 50)); + } + return; // Skip further processing for this file + } + var tempoMap = midiFile.GetTempoMap(); List Song = new List(); // Extract notes from MIDI file @@ -167,4 +193,4 @@ private static int CalculateOptimalShift(List notes) } } -} \ No newline at end of file +} From 118764c34ac33f12ce8c50a3c13c9d371a2394cd Mon Sep 17 00:00:00 2001 From: ro5490 <17776299+ro5490@users.noreply.github.com> Date: Mon, 25 Nov 2024 00:36:22 +0000 Subject: [PATCH 2/3] Update Program.cs --- MIDIProcessor/Program.cs | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/MIDIProcessor/Program.cs b/MIDIProcessor/Program.cs index 1f512d6..8cb26f8 100644 --- a/MIDIProcessor/Program.cs +++ b/MIDIProcessor/Program.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.IO; @@ -41,8 +41,7 @@ static void Main(string[] args) Console.WriteLine($"Conversion complete. Statistics written to {statsFilePath}"); Console.Read(); } - - public static void ConvertMidiToText(string midiFilePath, string textFilePath, string statsFilePath) + public static void ConvertMidiToText(string midiFilePath, string textFilePath, string statsFilePath) { // Configure ReadingSettings to handle invalid events var readingSettings = new ReadingSettings @@ -82,9 +81,16 @@ public static void ConvertMidiToText(string midiFilePath, string textFilePath, s (n.TimeAs(tempoMap).Seconds * 1000) + (n.TimeAs(tempoMap).Milliseconds); - if (Song.Count > 0 && timestampNote <= Song[Song.Count - 1].timestamp + 30) + if (Song.Count > 0) { - Song[Song.Count - 1].notes.Add(n.NoteNumber); + if (timestampNote <= Song[Song.Count - 1].timestamp + 30) + { + Song[Song.Count - 1].notes.Add(n.NoteNumber); + } + else + { + Song.Add(new SoundEvent(timestampNote, n.NoteNumber)); + } } else { @@ -106,8 +112,25 @@ public static void ConvertMidiToText(string midiFilePath, string textFilePath, s Directory.CreateDirectory(directory); } - // Apply shift and write to file + // Ensure the Song list is not empty + if (Song.Count == 0) + { + Console.WriteLine($"No valid notes found in {Path.GetFileName(midiFilePath)}. Skipping file."); + + // Optionally log to stats file + using (StreamWriter statsWriter = new StreamWriter(statsFilePath, true)) + { + statsWriter.WriteLine($"File: {Path.GetFileName(midiFilePath)} - No valid notes found. Skipped."); + statsWriter.WriteLine(new string('-', 50)); + } + + return; // Skip further processing + } + + // Safely access the first timestamp int offsetNotes = Song[0].timestamp; + + // Apply shift and write to file using (StreamWriter writer = new StreamWriter(textFilePath)) { foreach (var soundEvent in Song) @@ -143,6 +166,7 @@ public static void ConvertMidiToText(string midiFilePath, string textFilePath, s // Print a simple progress message to console Console.WriteLine($"Converted: {Path.GetFileName(midiFilePath)}"); } + public class ShiftParameters { public double NoShiftBonus { get; set; } = 0.11; From a59c11f7fe73991a5cc0f25f5d700abd90a99b08 Mon Sep 17 00:00:00 2001 From: ro5490 <17776299+ro5490@users.noreply.github.com> Date: Mon, 25 Nov 2024 20:06:36 +0000 Subject: [PATCH 3/3] Update Program.cs --- MIDIProcessor/Program.cs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/MIDIProcessor/Program.cs b/MIDIProcessor/Program.cs index 8cb26f8..77e4f82 100644 --- a/MIDIProcessor/Program.cs +++ b/MIDIProcessor/Program.cs @@ -105,6 +105,8 @@ public static void ConvertMidiToText(string midiFilePath, string textFilePath, s int totalNotes = allNotes.Count; int omittedNotes = allNotes.Count(n => n + optimalShift < 40 || n + optimalShift > 79); + float notesInRangePercentage = (float)(totalNotes - omittedNotes) / totalNotes * 100; + // Ensure the directory exists string directory = Path.GetDirectoryName(textFilePath); if (!string.IsNullOrEmpty(directory)) @@ -163,6 +165,24 @@ public static void ConvertMidiToText(string midiFilePath, string textFilePath, s statsWriter.WriteLine(new string('-', 50)); } + // Copy files based on criteria + string oneHundredFolder = "OneHundredPercent"; + string perfectFolder = "Perfect"; + + if (notesInRangePercentage == 100.00f) + { + Directory.CreateDirectory(oneHundredFolder); + string destinationFile = Path.Combine(oneHundredFolder, Path.GetFileName(textFilePath)); + File.Copy(textFilePath, destinationFile, true); + + if (optimalShift == 0 && omittedNotes == 0) + { + Directory.CreateDirectory(perfectFolder); + string perfectDestinationFile = Path.Combine(perfectFolder, Path.GetFileName(textFilePath)); + File.Copy(textFilePath, perfectDestinationFile, true); + } + } + // Print a simple progress message to console Console.WriteLine($"Converted: {Path.GetFileName(midiFilePath)}"); } @@ -215,6 +235,6 @@ private static int CalculateOptimalShift(List notes) return bestShift; } - + } }