Adjusting Pan

Most computers that have sound have two speakers, one on the left and the other on the right. You can adjust how much of a sound comes out of each speaker. If you are familiar with a car radio or home stereo, adjusting the pan is the same as moving the balance dial between left and right on your stereo (see Figure 12.2).

Figure 12.2. Pan allows you to adjust the level of sound coming out of each speaker.


By adjusting the pan level, you can create some pretty cool effects because you can enhance the illusion of movement by having a sound play from one speaker to another. Imagine a ship flying across the screen. You could have the sound of the ship move from one speaker to the next as the ship moves.

Pan is applied by using the SoundPan command and setting a pan number. The pan number can be anywhere between -1 and 1. Here’s how it works: A negative pan number will put more of the sound out of the left speaker, while a positive pan number puts more sound out of the right speaker. Here are some examples of pan numbers and how the sound would be divided:

  • 0.75—75% of the sound would come from the right speaker, and 25% would come from the left.

  • 0.25—25% of the sound would come from the left speaker, and 75% would come from the right.

Get the idea? Let’s put it to the test so that you can see how it works. Start by opening the file called demo12-03.bb. Follow along, but make sure that the sound file train.wav is in the same folder. We are now going to use the following code in bold to adjust the pan.

; Loading sound
train = LoadSound ( "train.wav" )
; Running the program
While Not KeyDown( 1 )
x# = 0

If KeyDown( 203 ) = True Then x# = -0.1
If KeyDown( 205 ) = True Then x# = 0.1
If x# > 0
						pan# = 1
						Else
						pan# = -1
						EndIf

MoveEntity sphere, x#,0,0

If x# <> 0
						SoundPan train, pan#
						PlaySound train
						EndIf
UpdateWorld

RenderWorld
Flip

Wend

End

When you run the program and press the right or left arrow keys, you’ll notice the sound “move” along with the sphere. Let’s take a close look at the code we added to understand how it works.

train = LoadSound ( "train.wav" )

This loads the sound that we have called train.

If x# > 0
pan# = 1
Else
pan# = -1
EndIf -

This code tells Blitz3D that if the x coordinate of the sphere is greater than 0 (in other words, if it moves to the right), then the music should play out of the right speaker and vice versa for the left.

If x# <> 0
SoundPan train, pan#
PlaySound train
EndIf

This says that when the sphere moves, the music should begin playing.

Note: Looping a Sound

You can use the code LoopSound ("soundfile.mp3") if you want to have a sound loop over and over. Obviously, you’d replace soundfile.mp3 with the actual file that you want to use.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.218.157