Music

[ LiB ]

You've gotten this far, but you might not quite understand the difference between music and sound, at least on a computer. Here's the thing, you have used sound so far to produce gunfire and explosion sounds. You can see that these sounds are played only at the time of the actual explosion or at the time when the bullet is actually fired . Music, however, is played in the background while your game is running. Therefore, it is much easier to use, because you can rig it up to play at the beginning of the game, and not worry about the music from then on.

For the music examples in this book, I use the *.mp3 format. Blitz Basic also allows you to use the *.wav, *.ogg, *.xms, and *.mid formats for your games , but I won't be covering them to keep things simple. These are all special formats that are like the .WAV format, but have small differences that won't be explained here. There's a lot of information out there if you want to learn about the other formats. Special thanks to Thomas Stenb ck and everyone else at Interim Nation for letting me use their music on the CD. You can visit Interim Nation at http://www.interimnation.cin.

To begin understanding music in Blitz Basic, we first need to discuss channels.

Channels and PlayMusic()

What is a channel? Well, imagine you have a sibling who is talking to you. At that point, the only thing you can hear is your sibling. Now, imagine that a phone located near you begins to ring. Suddenly, you can hear two things at once, right? Well, at this moment, there are two channels playing: the sibling channel and the telephone ringing channel.

Now, the cool thing about channels is that you can edit each channel independently. What does this mean? Well, for instance, say your sibling, who is currently on the sibling channel, begins to whisper to you. The volume of this voice has decreased. Using channels, you can change the volume of one channel while leaving another channel the same. If you take a look at Figure 11.5, you can see an example of how channels might work. There are two boxes, both emitting sounds. One is on the right, and one is on the left; thus, using the magic of channels, the one on the left is panned far to the left and the one on the right is panned to the right.

Figure 11.5. Channels and panning.


All we need now is to learn how to get control of a channel. Unlike handles, which you retrieve by loading sounds, you must play a sound in order to get access to a channel.

The most common way to get a channel is to use the function PlayMusic() . This function is declared as follows :

 PlayMusic(filename$) 

When playing music, you don't need to load the sound first. You just call PlayMusic() with the proper filename, and your sound is good to go!

Say you wanted to load a techno song named "technosong.mp3." This is what you would do:

 technosong = PlayMusic("technosong.mp3") 

As you can see, the function uses the PlayMusic() function and assigns the song to a channel variable. This variable can be used later for sound editing. Notice that this line of code actually plays the music. That means that at the time of using this line of code, "technosong.mp3" will begin to play. If you want to load the sound before using the sound file in the program, use the LoadSound()/PlaySound() functions. By the way, PlaySound() also returns a channel variable, which you can use just as you can with channel variables from music files.

Okay, now that we know how to load a music file, let's find out what we can do with those channels.

Messing With Da Channels

The last section taught you how to play music files and load channels, and this section teaches you how to use them. Following is a list of all the functions and their declarations that can be used with channels.

  • StopChannel channel_handle

  • PauseChannel channel_handle

  • ResumeChannel channel_handle

  • ChannelVolume channel_handle, volume#

  • ChannelPan channel_handle, pan#

  • ChannelPitch channel_handle, hertz

That's quite a few, but they are very easy to understand. Most of them don't even require parameters beyond the obligatory channel variable, and those that do aren't tough. Anyway, let me help you understand what these functions do.

The first half of the list ( Stop , Pause , and ResumeChannel ) can be separated into one group , and the second half ( ChannelVolume , Pan , and Pitch ) into another group. All of the functions within each group are related . The first group's functions work much like the Stop, Pause, and Resume buttons on a CD player. The StopChannel function stops a song immediately. The song is shut down and can only be restarted from the beginning. PauseChannel and ResumeChannel , however, allow you to pause and begin playing a music file anywhere within the song. PauseChannel pauses the song, and ResumeChannel picks up the song from the same point where it left off.

You can use these functions in numerous situations. Say you have a game with a monster alien at the end. The music plays in the background, and when you get to the monster, you want the music to stop playing while the monster says or does something. What you do is call PauseChannel right when the monster appears onscreen, and after he finishes his speech or video or whatever, you call ResumeChannel to begin the music right from the starting point.

Alright, cool. Next up is the second group, which consists of the functions ChannelVolume , ChannelPan , and ChannelPitch .

Do you remember the SoundVolume , SoundPan , and SoundPitch functions? Well, these work in the same way. ChannelVolume adjusts the volume of the music playing from the given channel. The volume# variable can be anywhere between 0 and 1.000, with 0 being the lowest and 1.000 the highest.

ChannelPan allows you to adjust the direction the sound is coming from. The value of the sound can be anywhere from -1.00 to 1.00. -1.00 is on the far left and 1.00 is on the far right, and of course, 0.00 is directly in the center.

The last function that can be used with music is ChannelPitch . ChannelPitch uses a hertz value between 0 and 44,000, with 44,000 being the highest-pitched sound and 0 being the lowest-pitched sound. (Actually, 0 means no sound!)

The cool thing about these functions is that every change you make to them happens in real-timeyou do not have to replay the music files every time you want to hear the changes. For example, if you have a program with music playing from the right and music playing from the left, and the player turns his character around 180 degrees, all you have to do is call ChannelPan and you're done, rather than playing the sound again.

Okay, I included a program, demo11-05.bb, that allows you to play with a music file. You can change its volume, pan it, and change its pitch. You can also pause, stop, and resume the song. Table 11.2 details all of the keys for demo11-05.bb.

Table 11.2. Demo11-05.bb's Keys

Key

Action

Up Arrow

Increases pitch by 100 hertz

Down Arrow

Decreases pitch by 100 hertz

Left Arrow

Pans music to left by -.1

Right Arrow

Pans Music to right by .1

'A' key

Increases volume by .1

'Z' key

Decreases volume by .1

'P' key

Pauses sound

'R' Key

Resumes sound

'S'

Key Stops sound


The source for demo11-05.bb is very long, so it isn't included in the book. Feel free to check it out on the CD. By the way, listen to the song included in the demoit's very cool.

There are a few more channels that you might want to know a bit more about. The first function, ChannelPlaying() , tests to see if a channel is currently playing. If the music file is playing, ChannelPlaying() will return 1; if it is not playing, ChannelPlaying() returns 0.

You would use this function if you wanted your background music to play more than once, back to back. This is called looping, which is the action of playing the same music file over and over without a break in the middle. How would you do it? Maybe like this:

 coolsong = PlayMusic("song.mp3") ;MAIN LOOP While Not KeyDown(1) If Not ChannelPlaying(coolsong)         coolsong = PlayMusic ("song.mp3") Endif 

Sound files are easier to loop. Blitz Basic provides a function, LoopSound , which can be used to play a sound file over and over again. LoopSound is declared like this:

 LoopSound sound_variable 

All you do is pass the handle of the sound file to this function. After you do that, call PlaySound with the handle of the sound, and the file will play over and over.

[ LiB ]


Game Programming for Teens
Game Programming for Teens
ISBN: 1598635182
EAN: 2147483647
Year: 2004
Pages: 94
Authors: Maneesh Sethi

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net