DirectMusic Tutorials :

Tutorials : Direct3D - DirectSound - DirectDraw - DirectInput

DirectMusic is a DirectX module. It allows you to read MIDI files with a quality of synthetiser (Direct Music use synthetiser sample)

Read a MIDI file

These are the different steps to play a MIDI-files

1)  Declare objects
2)  Initialize DirectMusic
3)  Load MIDI-files
4)  Play the MIDI segment
5) Obtain Midi information (lenght, tempo, beat description)

Declare the Objets:

Dim dx As New DirectX7
Dim perf As DirectMusicPerformance 'Two music class for obtaining tempo, measure
Dim perf2 As DirectMusicPerformance ' information.
Dim seg As DirectMusicSegment
Dim segstate As DirectMusicSegmentState
Dim loader As DirectMusicLoader

Initialize DirectMusic (in Sub Form_Load)

Set loader = dx.DirectMusicLoaderCreate()

Set perf2 = dx.DirectMusicPerformanceCreate()
Call perf2.Init(Nothing, 0)
perf2.SetPort -1, 80
Call perf2.GetMasterAutoDownload

Set perf = dx.DirectMusicPerformanceCreate()
Call perf.Init(Nothing, 0)
perf.SetPort -1, 80
Call perf.SetMasterAutoDownload(True)

perf.SetMasterVolume (-150
)

This is the method for loading a MIDI-files (replace Filename with the MIDI-file name or with a call to a CommonDialog OpenDialog)

Set loader = Nothing
Set loader = dx.DirectMusicLoaderCreate
Set seg = loader.LoadSegment(FileName)

Dim path As String

Do While path <> "\"
  path = Mid(FileName, length, 1)
  length = length - 1
Loop

Dim SearchDir As String
SearchDir = Left(FileName, length)
loader.SetSearchDirectory (Left(FileName, length + 1))
perf2.SetMasterAutoDownload True
seg.SetStandardMidiFile

Play a MIDI segment:

Call seg.SetStartPoint(0)              ' Begin to the beginning.
Set segstate = perf.PlaySegment(seg, 0, 0) ' Play the segment.

Obtain MIDI information : you just have to copy the following code after the loading procedure

     
    'Play the MIDI segment , just enough for obtaining information
    mtTime = perf2.GetMusicTime()
    Call perf2.PlaySegment(seg, 0, mtTime + 2000)
    
    'get the tempo
   
dTempo = perf2.GetTempo(mtTime + 2000, 0)
    lblTempo.Caption = "Tempo: " & Format(dTempo, "00.00") 
     
    ' get the measure 
   
Call perf2.GetTimeSig(mtTime + 2000, 0, timesig)
    lblTimeSig.Caption = "Mesure
: " & timesig.beatsPerMeasure & "/" & timesig.beat
    
    'get lenght of segment (in seconds)
    mtLength = (((seg.GetLength() / 768) * 60) / dTempo)
 
   
    Total_Time = mtLength
 
    Minutes = 0
    a = mtLength - 60
    Do While a > 0
        Minutes = Minutes + 1
        a = a - 60
    Loop
    lblLength.Caption = "Length: " & Format(Minutes, "00") & ":" & Format((mtLength - (Minutes * 60)), "00.0")
    
    Now we must stop the music.
    Call perf2.Stop(seg, Nothing, 0, 0) 

         
There is a example :DirectMusic Player 1.   You can also launch this program on-line . DirectMusic on line

Sylvain Dupont - The corner of the developer of games - DirectX
A question ? See F.A.Q

jaffiche.net - le réseau d'échange de bannières 1 pour 1jaffiche.net - le réseau d'échange de bannières 1 pour 
1
jaffiche.net - le réseau d'échange de bannières 1 pour 1

Click Here!