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

allow damaged files to be displayed #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions Data/MidiData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,14 @@ protected static int ReadVLQ(byte[] bytes, int index)
/* A variable-length quantity is in big-endian order, and only the lowest seven bits of each byte are part of the
* quantity. The Most Significant Bit (MSB) is set in all bytes except the last. It can be up to four bytes long.
*/

for (int i = 0, n = 0; i < 4; ++i)
{
if ((index + i) >= bytes.Length)
{
throw new ApplicationException(Properties.Resources.InvalidVLQ + string.Format(" offset {0}", index + i));
}

/* As appropriate, initialize the result (or shift it seven bits
* to the left) and add to it the lowest seven bits of this byte.
*/
Expand Down
14 changes: 13 additions & 1 deletion Data/Sets/MidiFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ namespace JeffBourdier
/// <summary>Represents a standard MIDI file (essentially, a collection of MidiChunk objects).</summary>
public class MidiFile : MidiSet
{


/****************
* Constructors *
****************/
Expand All @@ -36,7 +38,14 @@ public class MidiFile : MidiSet
public MidiFile(string filePath) : base(new List<MidiChunk>())
{
byte[] bytes = File.ReadAllBytes(filePath);
this.Replace(bytes);
try
{
this.Replace(bytes);
}
catch (Exception ex)
{
CreationException = ex;
}
}

/// <summary>Initializes a new instance of the MidiFile class with a header (MThd) chunk.</summary>
Expand All @@ -55,6 +64,9 @@ public MidiFile(MidiHeaderChunk header) : base(new List<MidiChunk>(1))

#region Public Methods

public Exception CreationException { get; set; }


/// <summary>Gets the chunk at the specified index.</summary>
/// <param name="index">The zero-based index of the chunk to get.</param>
/// <returns>The chunk at the specified index.</returns>
Expand Down
5 changes: 5 additions & 0 deletions MIDIopsyWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,11 @@ protected override bool OpenFile(string filePath)
return false;
}

if (!(this.MidiFile.CreationException==null)){
MessageBox.Show(String.Format("warning: {0}", this.MidiFile.CreationException.Message), Meta.Name, MessageBoxButton.OK, MessageBoxImage.Error);
}


/* Load the file object into the UI. */
this.LoadFile();
return true;
Expand Down