Blitz3D+ Command Reference

ChannelPitch channel,pitch

Parameters

channel - channel handle returned by PlaySound or PlayMusic

pitch - playback rate in hertz; the sample's own rate plays it normally

Description

Changes the playback rate, and so the pitch, of a sound that is already playing.

The number is a sample rate in hertz measured against the rate the file was recorded at. A 22050 Hz sample played at 22050 sounds normal; at 44100 it runs twice as fast an octave higher, at 11025 half speed an octave lower. Speed and pitch always move together.

This is a Channel command, so it bends one live playback while you listen. That is what makes it worth having over SoundPitch, which only sets the rate future playbacks begin at. Start a looping engine sample and raise its pitch with the throttle and you have a convincing engine for the cost of one file; do the same with a rising whine for a charging weapon, or drop the pitch of everything at once for a slow-motion effect.

Each channel has its own pitch, so several copies of the same sound can be running at different rates at the same time.

Extreme ratios are clamped by the audio engine rather than climbing without limit. A pitched-up sound also finishes sooner, and a looping one loops faster. Once a playback has ended its channel handle is stale and this does nothing, and a MIDI channel from PlayMusic ignores it entirely.

See also: SoundPitch, ChannelVolume, ChannelPan, ChannelPlaying, PlaySound.

Example

; ChannelPitch Example
; --------------------

Graphics 640,480,0,2
SetBuffer BackBuffer()

; A short tone looped endlessly gives us a steady engine hum
snd=LoadSound("media/beep.wav")
LoopSound snd
channel=PlaySound(snd)

; beep.wav was recorded at 11025 hertz - its natural pitch
hertz=11025

While Not KeyDown(1)

    ; Hold Up/Down to rev the engine like a siren
    If KeyDown(200) And hertz<33000 Then hertz=hertz+150
    If KeyDown(208) And hertz>2000 Then hertz=hertz-150

    ; ChannelPitch retunes the channel WHILE it plays - no need to
    ; restart it. (SoundPitch instead changes the sound for future
    ; PlaySound calls.)
    ChannelPitch channel,hertz

    Cls

    ; A frequency meter: the bar grows with the hertz value
    Color 100,100,100
    Rect 100,240,441,20,False
    Color 255,180,80
    Rect 102,242,(hertz-2000)*437/31000,16

    Color 255,255,255
    Text 0,0,"Up/Down: rev the live channel   Esc: exit"
    Text 0,20,"ChannelPitch channel,"+hertz+"   (recorded at 11025 Hz)"

    Flip

Wend

StopChannel channel

End

Index