DirectMusic:
Gathering Information
By: Jack Hoxley
Written: June 2000
Download:
DM_Info.Zip
(10kb)
Gathering information on a piece
of music is only really useful to multimedia programs; rarely is it any use
to games developers. When a game is written it is quite likely that the user
wont want to know much about the music settings, and the developer could just
open up the file in another program and copy the details from there - rather
than program it.
However, when you are writing
multimedia applications you probably wont know anything about the music being
created, or being loaded - which is why you use these methods to work out whats
been loaded. There are three main things that you can check for:
- Tempo
- Time Signature
- Length
General Notice
To gather information from a piece of music you need to have it playing - at
least for the information listed below. This can be quite annoying, as you'll
end up hearing the first 1/2 a second of music whilst it works things out. This
can easily be overcome by setting the volume to 0 (mute) this way you won't
hear anything. You should be able to play the music by now; so it's not included
on this particular page.
1. Tempo
Tempo is basically how fast it goes; it is useful to know this if you want to
speed it up or slow it down to a certain speed. Use this code to work out the
tempo:
'Variables to hold our info
dim mtLength as long,dtempo as double
'Get Length
mtTime = perf.GetMusicTime()
'<Start playing music here>
'GetTempo
dtempo = perf.GetTempo(mtTime + 2000, 0)
'You don't need this next line; only if you intend
to output the result
lblTempo.Caption = "Tempo: " & Format(dtempo, "00.00")
'You may want to use the rounding feature though |
|
|
|
Note; you'll need the tempo to
work out the length
2. Time Signature
The time signature is only really useful if you want the user to know; or you
are doing something that needs it.
'Required to hold the data
Dim timesig As DMUS_TIMESIGNATURE
'GetTimeSig
Call perf.GetTimeSig(mtTime + 2000, 0, timesig)
'You only need the second half of the next line if
you aren't outputting it to the user.
lblTimeSig.Caption = "Time Signature: " & timesig.beatsPerMeasure & "/"
& timesig.beat |
|
|
|
3. Length
This is the most useful of the three; and is used to greater effect in the next
tutorial (looping music). In that tutorial we will be working out how far through
a piece we are. You will need to have worked out the tempo before you can use
this code:
'Actual Length in seconds
(Rounded)
'You can extract the formula from this next line
lblLength.Caption = "Length: " & Int((((seg.GetLength() / 768) * 60) / dtempo))
& "s"
'This will round it to the nearest second; so you
may well be half a second short. You will have
'to write your own code to convert this into hh:mm:ss format. |
|
|
|
You can download a complete working
example from the top of this page; or from the downloads page. See the next
tutorial for putting this code to practical use.
|