Below is code I use to get the length (time in milliseconds) of a MIDI file.
It works perfect provided there is only one one Tempo setting in the sequence.
I've attached 2 MIDI files that demonstrate the problem
TimeTest1.mid
contains 60 notes and the Tempo is set at 60 bmp
60 x 1 sec = 60 seconds
TimeTest2.mid
contains 60 notes and the Tempo of first 20 notes is set at 60 bmp, the last 40 notes Tempo is set at 120 bpm
20 x 1 sec + 40 x 1/2 sec = 40 seconds
Windows Media Player shows correctly 60 seconds for the first, and 40 seconds for the second
My code correctly reports 60 seconds for the first, and incorrectly reports 30 seconds for the second
Any help to get this working correctly using VB would be appreciated.
Code:
Option Explicit
Private Declare Function mciSendStringW Lib "winmm.dll" _
(ByVal lpszCommand As Long, _
Optional ByVal lpszReturnString As Long, _
Optional ByVal cchReturn As Long, _
Optional ByVal hWndCallback As Long) As Long
Private Sub Form_Load()
Command1.Caption = "Get MIDI length"
End Sub
Private Sub Command1_Click()
Dim ret As Long
Dim buf As String
Dim midiLength As String
'ret = mciSendStringW(StrPtr("open """ & App.Path & "\TimeTest1.mid"" type sequencer alias myMIDI"))
ret = mciSendStringW(StrPtr("open """ & App.Path & "\TimeTest2.mid"" type sequencer alias myMIDI"))
ret = mciSendStringW(StrPtr("set myMIDI time format milliseconds"))
buf = Space$(255&)
ret = mciSendStringW(StrPtr("status myMIDI length"), StrPtr(buf), Len(buf))
midiLength = Val(buf)
MsgBox "MIDI length: " & midiLength & " milliseconds"
mciSendStringW StrPtr("close myMIDI")
End Sub
FF 51 03 tt tt tt
Indicates a tempo change. The 3 data bytes of tt tt tt are the tempo in microseconds per quarter note.
Look out for the Byte-sequence "FF 51 03"
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
True, in a SMF (Standard Midi File) the length (duration in seconds) is not written anywhere.
If you were to parse the file, you would have to calculate it yourself.
But when using the mci API, the docs state that if you set the time format to milliseconds, and then do a status length request, time in milliseconds would be returned.