Blitz3D+ Command Reference

ChannelPan channel,pan#

Parameters

channel - channel handle returned by PlaySound or PlayMusic

pan# - stereo position from -1.0 (full left) through 0.0 (centre) to 1.0 (full right)

Description

Moves a playing sound between the left and right speakers.

This is a Channel command: it steers one live playback and does not touch the loaded sound. SoundPan is the version that sets where future playbacks start; this one is for sounds that need to move while you can hear them.

That is where the effect really pays off. Update the pan every loop from an object's screen position and a passing car, a circling wasp or an enemy running across the play field genuinely travels across the stereo field. A helpful conversion is pan#=(x-GraphicsWidth()/2.0)/(GraphicsWidth()/2.0), clamped to the -1 to 1 range.

Each channel pans independently, so several copies of the same sound can sit in different places at once - which is exactly what you want when three enemies are firing from three positions.

Panning needs at least a stereo output to be audible. Avoid hard-panning anything the player must not miss, since a player with one headphone will lose it entirely. Once a sound has finished its channel handle is stale and this does nothing, and a MIDI channel from PlayMusic ignores panning altogether.

See also: SoundPan, ChannelVolume, ChannelPitch, ChannelPlaying, PlaySound.

Example

; ChannelPan Example
; ------------------

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

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

; Pan as a percentage: -100 = full left, 0 = centre, 100 = full right
pan_pct=0

While Not KeyDown(1)

    ; Left/Right arrows slide the hum between the speakers
    If KeyDown(203) And pan_pct>-100 Then pan_pct=pan_pct-2
    If KeyDown(205) And pan_pct<100 Then pan_pct=pan_pct+2

    ; ChannelPan pans the channel WHILE it plays - no need to restart it.
    ; (SoundPan instead changes the sound for future PlaySound calls.)
    ChannelPan channel,pan_pct/100.0

    Cls

    ; The pan slider with a moving marker
    Color 100,100,100
    Rect 120,240,401,3
    Color 80,220,255
    Rect 318+pan_pct*2,228,6,27

    Color 255,255,255
    Text 100,270,"L"
    Text 320,270,"C",True
    Text 534,270,"R"

    Text 0,0,"Left/Right: pan the live channel   Esc: exit"
    Text 0,20,"ChannelPan channel,pan#   pan = "+pan_pct+"% (-100 left ... +100 right)"

    Flip

Wend

StopChannel channel

End

Index