Results 1 to 4 of 4

Thread: Get Length of MIDI File

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2018
    Posts
    303

    Get Length of MIDI File

    Hello,

    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
    Attached Files Attached Files
    Last edited by mms_; Apr 10th, 2021 at 11:26 PM.

  2. #2
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    2,430

    Re: Get Length of MIDI File

    Found on SO (albeit 10 years old): https://stackoverflow.com/questions/...of-a-midi-file

    Bottom Line:
    You have to calculate it yourself, keeping track of tempo-changes

    Maybe helpful:
    http://midi.teragonaudio.com/tech/midifile/ppqn.htm
    http://midi.teragonaudio.com/tech/midifile/tempo.htm

    In that last link there is something interesting:
    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"
    One System to rule them all, One IDE to find them,
    One Code 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.
    ---------------------------------------------------------------------------------
    For health reasons i try to avoid reading unformatted Code

  3. #3
    Member
    Join Date
    Nov 2020
    Posts
    32

    Re: Get Length of MIDI File

    One of the best MCI websites:

    Visual Basic MCI Command Post
    http://web.archive.org/web/201112092...o/mci/mci.html

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2018
    Posts
    303

    Re: Get Length of MIDI File

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width