Do you know what the difference is between a tenor and a soprano? Tenors have voices that are low, while sopranos have voices that are higher. The level of a person’s voice, high or low, is called the pitch. I’m sure you’ve heard sound effects in which the pitch of someone’s voice is set so high that it sounds like they are talking like a chipmunk. Or, perhaps you’ve heard the pitch of someone’s voice lowered to a point that they sound like the devil or a monster.
Pitch is measured in a unit called a Hertz (Hz). In Blitz3D, you can adjust the pitch of a sound by adjusting its Hertz. The range goes from 1 Hz to 44,000 Hz.
Adjusting the pitch is simply a matter of using the SoundPitch code. For this program, I did not create a demo on the CD—I want you to try and follow along!
Type in the following code. Before you start entering the code, save the program to a folder that contains the file hello.wav.
; Changing Pitch ;______________ Graphics3D 640,480 SetBuffer BackBuffer() ; Creating a light light=CreateLight() ; Create camera camera=CreateCamera() PositionEntity camera, 0,1,0 ;Loading the sound hello = LoadSound ( "hello.wav" ) ; Running the program While Not KeyDown( 1 ) pitch#=12000 If KeyDown( 200 )=True Then pitch#= pitch#+500 If KeyDown( 208 )=True Then pitch#= pitch#-500 If pitch# < 1 Then pitch#=2000 If KeyDown( 57 )=True Then SoundPitch hello, pitch# PlaySound hello RenderWorld Flip Wend End
We’ll look at the code in a moment, but for now, just run the program. Use the space bar to play the sound and then press the up or down arrows to adjust the pitch, and then press the space bar again.
Now let’s look at the code we used to create the sound and adjust the pitch.
hello = LoadSound ( "hello.wav" )—This loads the sound into the program. We called our sound hello.
pitch#=12000—Here we created a variable and called it pitch#. We could have given the variable any name. We made the initial pitch level 12000 Hz.
If KeyDown( 200 )=True Then pitch#= pitch#+500—When the up arrow is pressed, the pitch will go up by 500 Hz.
If KeyDown( 208 )=True Then pitch#= pitch#-500—When the down arrow is pressed, the pitch will go down by 500 Hz.
If pitch# < 2000 Then pitch#=2000—If the pitch gets too low, you won’t be able to hear it, so we will create a safety net that prevents the pitch from falling below 2000 Hz. The code says that if the pitch level (pitch#) falls below 2000 then just set it at 2000.
If KeyDown( 57 )=True Then SoundPitch hello, pitch# PlaySound hello—This code says that if the space bar is pressed, then set the pitch level and play the sound called hello.
Note: Experiment with Pitch
Unless you created the sound itself, you won’t know what its pitch level is, so you’ll have to experiment with different pitch levels if you want to make an adjustment.
18.117.229.33