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 Dim dx As New DirectX7
Initialize DirectMusic (in Sub Form_Load) Set loader = dx.DirectMusicLoaderCreate()
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 Call
seg.SetStartPoint(0)
' Begin to the
beginning.
Obtain MIDI information : you just have to copy the
following code after the loading procedure Sylvain
Dupont - The
corner of the developer of games - DirectX
1) Declare
objects
2) Initialize
DirectMusic
3)
Load MIDI-files
4) Play the MIDI
segment
5) Obtain Midi information
(lenght, tempo, beat description)
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
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)
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
Set segstate = perf.PlaySegment(seg, 0, 0) ' Play the segment.
'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
A question ? See
F.A.Q