TUTORIAL SEVEN
SOUND AND MUSIC
The purpose of this program is to demonstrate how to load sound and music files and play them. Additionally, the program shows you how to detect for physical keyboard strikes using the SCANCODE command and provides some audio insight into how Dark Basic handles multi-channel sound.
This tutorial program can be loaded directly into the editor by clicking
SAMPLE07.DBA.
rem Load music file
LOAD MUSIC "sample.mid",1
rem Load sound file
LOAD SOUND "sample.wav",1
LOAD SOUND "sample.wav",2
LOAD SOUND "sample.wav",3
rem Start Music
LOOP MUSIC 1
rem Begin loop
DO
rem Pressing key '1' will play sound 1
IF SCANCODE()=2 AND SOUND PLAYING(1)=0 THEN PLAY SOUND 1
rem Pressing key '2' will play sound 2
IF SCANCODE()=3 AND SOUND PLAYING(2)=0 THEN PLAY SOUND 2
rem Pressing key '3' will play sound 3
IF SCANCODE()=4 AND SOUND PLAYING(3)=0 THEN PLAY SOUND 3
rem End loop
LOOP
The LOAD MUSIC command takes a file in the MIDI format, and loads it into music number 1. Many music files can be loaded, but only one score of music can be played at any one time. The LOAD SOUND commands load the same WAV format sound file into sound numbers 1, 2 and 3. The reason for this is to provide multi-channel sound in the program, which allows the sounds to overlap each other for best effect. The LOOP MUSIC command plays the music and instructs it to repeat continuously. The SCANCODE() command returns the value of the key currently being pressed. The SOUND PLAYING() command returns a 1 when the sound is playing, and zero at all other times.
Final Step : Things for you to do
1. Change the program to stop the music if a sound is played
2. Change the program to play the music if the 'm' key is pressed
3. Change the program to increase the number of explosions from 3 to 5
You can skip to the next tutorial by selecting TUTORIAL EIGHT.