Platform SDK: DirectX

Step 2: Play Effects

[C++]

This topic pertains only to application development in DirectX for Visual Basic.

[Visual Basic]

In the DLSEffects sample, the user can hear a variety of sound effects, including laughter, fragments of speech in several languages, and an infant's heartbeat. All these sounds are contained in a single DLS instrument.

Completely different sounds can be played by a single instrument because different note ranges, or regions, can be associated with different wave samples. In Boids.dls, for example, the first speech fragment is assigned to the pitch range from C0 through B3. Any note in that range is based on the same wave sample. The root note is C3, meaning that when a C3 note is played, the wave is heard at its original frequency. All other notes cause the pitch of the wave to be modified.

The following procedure sends a note at the volume indicated by the global variable gVelocity:

Private Sub SendNote(chan As Integer, pitch As Byte, dur As Long)
  
  Dim noteMsg As DMUS_NOTE_PMSG
  noteMsg.velocity = gVelocity
  noteMsg.flags = DMUS_NOTEF_NOTEON
  noteMsg.midiValue = pitch
  noteMsg.mtDuration = dur
  Call perf.SendNotePMSG(0, DMUS_PMSGF_REFTIME, chan, noteMsg)
    
 End Sub

All but one of the waves in Boids.dls are one-shot samples. This means that when a note is played, the wave sample plays only once, regardless of the duration of the note. The exception is the heartbeat sample, which is looped so that it plays as long as the note is playing. This looping behavior was specified by the author of the instrument collection.

The following procedure sends a C3 note. C3 is the root note of the first speech sample, so it plays at the correct pitch. The constant NoteDur is 6000 milliseconds, allowing enough time for this or any of the other waves to play completely before DirectMusic generates a note-off MIDI message. (If you click the button and send an identical note before this time has elapsed, the note-off message might cancel the new note as well, so it might not play fully.)

Private Sub cmdC3_Click()
  SendNote channel, 36, NoteDur
End Sub

The DLSEffects application plays most of the samples at their root note. However, the heartbeat sample is treated a little differently, as an illustration of how the pitch of sound effects can be modified programmatically. This topic is covered in Step 3: Modify and Stop Effects.