CreateListener ( parent[,rolloff_factor#][,doppler_scale#][,distance_scale#] )

Parameters

parent - parent entity of listener, typically the camera; may be 0 for a free-standing listener

rolloff_factor# (optional) - the rate at which volume diminishes with distance; 1 (default)

doppler_scale# (optional) - the severity of the doppler effect; 1 (default)

distance_scale# (optional) - artificially scales distances, i.e. how many world units count as one metre; 1 (default)

Description

Creates a listener entity - the 'ears' of your 3D world - and returns its handle.

A listener must exist before EmitSound can play 3D sounds; emitting without one is a runtime error. Parent the listener to your camera so that what the player hears always matches what they see: sounds pan between speakers as their sources move around the listener, get quieter with distance, and pitch-shift with relative speed (the doppler effect).

Only a single listener is supported per world.

The three scale factors tune the whole 3D sound stage. Raise rolloff_factor to make sounds die away faster with distance (a claustrophobic cave), lower it for open air. Raise doppler_scale for exaggerated racing-game whooshes, or set it to 0 to disable doppler entirely. Set distance_scale to match your world's sense of scale - if one world unit is a centimetre, sounds would otherwise fade over absurdly short distances.

See also: EmitSound, Load3DSound, CreateCamera.

Example

; CreateListener Example
; ----------------------

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

camera=CreateCamera()
PositionEntity camera,0,2,0

light=CreateLight()
RotateEntity light,45,45,0

; Textured ground plane
plane=CreatePlane()
ground_tex=LoadTexture("media/MossyGround.BMP")
ScaleTexture ground_tex,4,4
EntityTexture plane,ground_tex

; A siren drone circles the camera, beeping as it goes
drone=CreateSphere()
EntityColor drone,255,100,100
PositionEntity drone,8,2,0

; The listener rides on the camera - 3D sound is heard from its position
listener=CreateListener(camera)

; Load the beep as a 3D sound so it can be positioned in space
beep=Load3DSound("../2d_examples/media/beep.wav")

angle#=0
next_beep=MilliSecs()

While Not KeyDown(1)

    ; Fly the drone in a circle around the listener
    angle=angle+1
    PositionEntity drone,Cos(angle)*8,2,Sin(angle)*8

    ; The listener created above makes EmitSound positional: emit a beep
    ; from the drone every 700 milliseconds
    If MilliSecs()>next_beep Then
        channel=EmitSound(beep,drone)
        next_beep=MilliSecs()+700
    EndIf

    ; Left / Right turn the camera - face the drone or turn away
    If KeyDown(203) Then TurnEntity camera,0,1,0
    If KeyDown(205) Then TurnEntity camera,0,-1,0

    RenderWorld

    Text 0,0,"Left / Right: turn camera   Esc: exit"
    Text 0,20,"Listen: the beep pans between speakers as the drone circles"

    Flip

Wend

End

Index