Squirm
09-30-2001, 10:13 AM
Another lazy Sunday afternoon. Nothing to do, so I'll write this quick DirectMusic tutorial. Later, I might do some of the more advanced stuff, such as DirectDraw. DirectMusic is probably the easiest of all of the DirectX areas, so if you want a taste of DirectX, this is a good place to start. DirectMusic only deals with .MID files, not MP3 or anything like that, so if you want those, then you'll have to wait till I write something on DirectSound. Anyway......
First, we're gonna need to make a reference to DirectX so that VB knows what we're talking about, and how to handle all the complex code.
For this project, we can use a Standard EXE project with just 1 form, although it is likely that you would later want to encapsulate different DX functions into their own code modules. For now, a Form module is sufficient.
1) Click on the Project menu
2) Click on References
3) Scroll down until you find DirectX 7 for Visual Basic Type Library and check it
4) Click OK
Now, you are all set to include anything DirectX 7 in your VB project. Earlier version of DirectX did not come with a .TLB and so VBers had to rely on 3rd party stuff.
Now we have the power of DirectX, so let's use it. First, we will need an instance of DirectX with which to mess with. At the top of the form's code window, add the following:
Option Explicit
Dim DX As New DirectX7
Now, we have an object, DX, around which everything else will be based. We must declare further objects to enable us to use DirectMusic.
Dim perf As DirectMusicPerformance
Dim seg As DirectMusicSegment
Dim segstate As DirectMusicSegmentState
Dim loader As DirectMusicLoader
WOA! Loads of new objects there! Here's a simple breakdown:
DirectMusicPerformance - This controls the music, looks after sending data to varius ports, setting options, etc.
DirectMusicSegment - This is a 'segment' of music, which acts as a kind of buffer of what is to be played next.
DirectMusicSegmentState - Tells us what the music is doing (is it playing or stopped?)
DirectMusicLoader - This loads music data from the file into the segment.
Now, we can begin. Although these objects have been dimensioned, they are not ready for use. DirectX has to 'make' them into things. The code for setting everything up is this:
Set loader = DX.DirectMusicLoaderCreate()
Set perf = DX.DirectMusicPerformanceCreate()
Call perf.Init(Nothing, 0)
perf.SetPort -1, 80
Call perf.SetMasterAutoDownload(True)
perf.SetMasterVolume (iVol * 42 - 3000)
Fairly simple to follow, and you don't really need to know the specifics of it. The only thing you really need to look out for is SetMasterVolume. The variable I called iVol is an integer with any number between 0 and 100, depending on the volume you desire. Once all that is sorted, we can begin to load some music from a MID file. This requires two simple lines of code:
Set seg = loader.LoadSegment("c:\somemusic.mid")
seg.SetStandardMidiFile
Simple, eh? Of course, its unlikely you'll have a file called somemusic on you C drive, so change that obviously images/smilies/wink.gif. Now, at last, we have everything set up. All we need to do now is start playing it. This is the fun (and easy) part...
Set segstate = perf.PlaySegment(seg, 0, 0)
TADA! You should now hear some music coming through your PC speakers (unless you set volume to 0). It will continue to play until the end of the music, and then it will stop. However, if you want to stop it before the end, just put this:
Call perf.Stop(seg, segstate, 0, 0)
From here, the music will stop, and you can then start it again as you wish. Finally, once you are sure you've had enough of the music, you should close down DirectX cleanly. This means do not just put End where you fancy. Use this, to keep things tidy:
If Not (perf Is Nothing) Then perf.CloseDown
Also, if you have finished with DirectX altogether, then you will need to unload that too:
Set DX = Nothing
You would obviously not want to use that if you were still using DirectDraw, Direct3D, or some other part of DX. Unloading your DirectX object should really be the last thing you do, just before the whole application closes.
That's it. You can now use DirectX to play midi files in the background (so you can have your application do other stuff at the same time). As always, have fun, and good luck!
images/smilies/wink.gif
First, we're gonna need to make a reference to DirectX so that VB knows what we're talking about, and how to handle all the complex code.
For this project, we can use a Standard EXE project with just 1 form, although it is likely that you would later want to encapsulate different DX functions into their own code modules. For now, a Form module is sufficient.
1) Click on the Project menu
2) Click on References
3) Scroll down until you find DirectX 7 for Visual Basic Type Library and check it
4) Click OK
Now, you are all set to include anything DirectX 7 in your VB project. Earlier version of DirectX did not come with a .TLB and so VBers had to rely on 3rd party stuff.
Now we have the power of DirectX, so let's use it. First, we will need an instance of DirectX with which to mess with. At the top of the form's code window, add the following:
Option Explicit
Dim DX As New DirectX7
Now, we have an object, DX, around which everything else will be based. We must declare further objects to enable us to use DirectMusic.
Dim perf As DirectMusicPerformance
Dim seg As DirectMusicSegment
Dim segstate As DirectMusicSegmentState
Dim loader As DirectMusicLoader
WOA! Loads of new objects there! Here's a simple breakdown:
DirectMusicPerformance - This controls the music, looks after sending data to varius ports, setting options, etc.
DirectMusicSegment - This is a 'segment' of music, which acts as a kind of buffer of what is to be played next.
DirectMusicSegmentState - Tells us what the music is doing (is it playing or stopped?)
DirectMusicLoader - This loads music data from the file into the segment.
Now, we can begin. Although these objects have been dimensioned, they are not ready for use. DirectX has to 'make' them into things. The code for setting everything up is this:
Set loader = DX.DirectMusicLoaderCreate()
Set perf = DX.DirectMusicPerformanceCreate()
Call perf.Init(Nothing, 0)
perf.SetPort -1, 80
Call perf.SetMasterAutoDownload(True)
perf.SetMasterVolume (iVol * 42 - 3000)
Fairly simple to follow, and you don't really need to know the specifics of it. The only thing you really need to look out for is SetMasterVolume. The variable I called iVol is an integer with any number between 0 and 100, depending on the volume you desire. Once all that is sorted, we can begin to load some music from a MID file. This requires two simple lines of code:
Set seg = loader.LoadSegment("c:\somemusic.mid")
seg.SetStandardMidiFile
Simple, eh? Of course, its unlikely you'll have a file called somemusic on you C drive, so change that obviously images/smilies/wink.gif. Now, at last, we have everything set up. All we need to do now is start playing it. This is the fun (and easy) part...
Set segstate = perf.PlaySegment(seg, 0, 0)
TADA! You should now hear some music coming through your PC speakers (unless you set volume to 0). It will continue to play until the end of the music, and then it will stop. However, if you want to stop it before the end, just put this:
Call perf.Stop(seg, segstate, 0, 0)
From here, the music will stop, and you can then start it again as you wish. Finally, once you are sure you've had enough of the music, you should close down DirectX cleanly. This means do not just put End where you fancy. Use this, to keep things tidy:
If Not (perf Is Nothing) Then perf.CloseDown
Also, if you have finished with DirectX altogether, then you will need to unload that too:
Set DX = Nothing
You would obviously not want to use that if you were still using DirectDraw, Direct3D, or some other part of DX. Unloading your DirectX object should really be the last thing you do, just before the whole application closes.
That's it. You can now use DirectX to play midi files in the background (so you can have your application do other stuff at the same time). As always, have fun, and good luck!
images/smilies/wink.gif