Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds Invalid event handling, for bulk conversion #39

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 79 additions & 9 deletions MIDIProcessor/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
Expand Down Expand Up @@ -41,12 +41,37 @@ 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)
{
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<SoundEvent> Song = new List<SoundEvent>();

// Extract notes from MIDI file
Expand All @@ -56,9 +81,16 @@ public static void ConvertMidiToText(string midiFilePath, string textFilePath, s
(n.TimeAs<MetricTimeSpan>(tempoMap).Seconds * 1000) +
(n.TimeAs<MetricTimeSpan>(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
{
Expand All @@ -73,15 +105,34 @@ 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))
{
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)
Expand Down Expand Up @@ -114,9 +165,28 @@ 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)}");
}

public class ShiftParameters
{
public double NoShiftBonus { get; set; } = 0.11;
Expand Down Expand Up @@ -165,6 +235,6 @@ private static int CalculateOptimalShift(List<byte> notes)

return bestShift;
}

}
}
}