Main Site Links Resources Tutorials
News VB Gaming Code Downloads DirectX 7
Contact Webmaster VB Programming Product Reviews DirectX 8
  General Multimedia Articles DirectX 9

DirectMusic: Using DLS Collections
By: Jack Hoxley
Written: June 2000

Download: DM_DLS.Zip (22kb)

DLS stands for DownLoadable Sounds; effectively all they are are instruments. Technicaly you dont actually play a DLS file, you use it along with another piece of music. Say, for example, you had a piece of music. In one level it was played with Heavy metal instruments, the other classical instruments, the last one with techno instruments. It wouldn't be the piece of music that was changing, it would be the DLS collection. The music is just a collection of notes, lengths, tones and flags saying which instrument to use - They then interact with the DLS, which, for example, has 16 instruments in it. The piece of music says "10 seconds of A flat on instrument 2" - So the DLS says instrument 2 is a drum, so it plays a drum. Therefore, if you change the DLS you'll be able to achieve many different styles and results with just one piece of music.

The program will still require music; we can do this either through MIDI files (see MIDI tutorial) or we can use segments. For this tutorial we will use a segment; on the basis that we've already covered MIDI files.

I used Micrsofts DirectMusic Producer to create/convert the files used in this examples; although quite complicated it is worth looking at. I believe you can get a copy off their web page (see Downloads for a link). This program allows you to make your own music (MIDI, DLS, SGT etc...) if you have the correct hardware and convert existing pieces if you dont.

The Code
The code is written in 5 steps; to make this easier for you, you'll have to trigger each step. Open up a Standard EXE project in VB and link the DirectX reference library to your project (????) and open up the form object. Create 5 command buttons on the form and name them: "CmdStep1", "CmdStep2", "CmdStep3", "CmdStep4", "CmdStep5". Then give them captions something along the lines of: "init","Load Music","Load DLS","Play","Stop". Now onto writing the code:

Step1: The Declarations
The declarations are fairly simple, and basically outline what variables we will need. For a simple project these will probably be fine.

Option Explicit

Dim dx As New DirectX7
'The performance regulates the interface
'between different segments and the hardware

Dim perf As DirectMusicPerformance
'The segment is what actually holds the data
Dim seg As DirectMusicSegment
'the segmentstate tells us what it's doing
Dim segstate As DirectMusicSegmentState
'The loader object is what changes the
'hard drive data into what is stored in memory (segment)

Dim loader As DirectMusicLoader
'A collection is what the DLS file is loaded into
Dim col As DirectMusicCollection

Step2: Initialising
Before you can do anything with the variables you just defined you must initialise them. This means that we create any objects that are need, link our program to the hardware and set any additional options.

'We must create the object with which
'we create segments

Set loader = dx.DirectMusicLoaderCreate()

'First we create the performance
Set perf = dx.DirectMusicPerformanceCreate()
'then we intialise it
Call perf.Init(Nothing, 0)
'Then we set it's options
perf.SetPort -1, 80
'These options should be standard for most projects
Call perf.SetMasterAutoDownload(True)
'The formula below just simplifies setting the volume
'Change the 75 for a value between 0 and 100 (like a percentage)

perf.SetMasterVolume (75 * 42 - 3000)

Step3: Loading our Segment
Now that all the objects have been initialised we can load some musical data. This comes in the form of one call:

'Use the loader object to create a segment from
'the data stored on the HDD
'Depending on how big the data is, this line may take
'several seconds to complete

Set seg = loader.LoadSegment(App.Path & "\mozart.sgt")

Step4: Connecting to our DLS file
Now that we have some data in memory we can link a DLS collection to it. We cannot do this before the data has been loaded.

'Load the DLS into our collection object; again, using the loader

Set col = loader.LoadCollection(App.Path & "\dlscollection.dls")
'Now we link the DLS collection to our music
Call seg.ConnectToCollection(col)

Step5: Playing and stopping the sound
Now we actually get to hear something! Playing and Stopping is extremely simple; just one line each.

'The Play Line:
Set segstate = perf.PlaySegment(seg, 0, 0)

'The Stop Line
Call perf.Stop(seg, segstate, 0, 0)

Note; the piece of music will automatically stop playing when it comes to a natural end.

As you can see; a simple project is extremely easy - less than 50 lines of code in there. However, the real art is in the making of the music. If you are to use Music in your game (and you should) then it is well worth spending a LOT of time writing and recording the music. If you have no musical experience/talent, go looking for someone who does.

If you're having problems with the above code you can download a working example from the top of the page, or you can get it from my Downloads page.

DirectX 4 VB 2000 Jack Hoxley. All rights reserved.
Reproduction of this site and it's contents, in whole or in part, is prohibited,
except where explicitly stated otherwise.
Design by Mateo
Contact Webmaster