Freesnöw
Freesnöw

Reputation: 32143

Using BinaryReader to read a midi file. (.net)

How would I use a BinaryReader to read a midi file (specifications for the format are here)

I'm using vb.net, but I'm willing to see other code (mostly just C#, I can convert it). I'm working on a large project and this comes as a bit of a speedbump.

Here is my current code:

Private Function convertCharArrayToString(ByVal chars() As Char) As String
    Dim tReturn As String = ""
    For Each v As Char In chars
        tReturn &= v
    Next
    Return tReturn
End Function

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    midiStatus = "Reading..."
    Dim midiStream As New StreamReader(midiFile)
    Dim nBR As New BinaryReader(midiStream.BaseStream)
    midiStatus = "Validating Midi File..."
    Dim HeaderA As String = convertCharArrayToString(nBR.ReadChars(4))
    If Not HeaderA = "MThd" Then Return
    Dim HeaderB() As Byte = nBR.ReadBytes(4)
    'Get Track Type
    midiStatus = "Reading Header Data..."
    Dim TrackType1 As Integer = nBR.ReadInt16()
    Dim TrackType2 As Integer = nBR.ReadInt16()
    MsgBox(TrackType1 & TrackType2)

End Sub

Everything works find up to when I start reading the actual Header Data. I'm absoultly lost as to how I'm to continue. Any help or code examples would be nice!

Upvotes: 1

Views: 1404

Answers (1)

MPelletier
MPelletier

Reputation: 16687

Write your decoding logic in a class, not directly in the GUI.

Define a struct for the MIDI header, then use something like this to set it.

Upvotes: 1

Related Questions