使用 BinaryReader 读取 MIDI 文件。 (。网)
如何使用 BinaryReader 读取 midi 文件(格式规范位于此处)
我正在使用 vb.net,但我愿意看到其他代码(主要是 C#,我可以转换它)。我正在开展一个大型项目,这有点阻碍。
这是我当前的代码:
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
直到我开始阅读实际的标题数据时,一切都正常。我完全不知道该如何继续。任何帮助或代码示例都会很好!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在类中编写解码逻辑,而不是直接在 GUI 中。
为 MIDI 标头定义一个结构,然后使用类似 this 的内容来设置它。
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.