Module: SFML::Listener

Defined in:
lib/sfml/audio/listener.rb

Overview

The “ear” — a global, single-instance object that defines from where the player hears the world. Sounds positioned via Sound#position= attenuate based on their distance from this point.

SFML::Listener.position = [400, 300, 0]   # follow your camera
SFML::Listener.global_volume = 80         # 0..100, master gain

For 2D games keep z = 0 and treat x, y as world coords. For first-person stuff also set Listener.direction to point along the camera forward.

Class Method Summary collapse

Class Method Details

.coneObject

Directional cone for the listener — same shape as the cone on individual sound sources (see SFML::SoundCone). Used for cone-shaped attenuation when the listener faces a particular direction (think headphones turning to follow a character’s head).



71
72
73
# File 'lib/sfml/audio/listener.rb', line 71

def cone
  SoundCone.from_native(C::Audio.sfListener_getCone)
end

.cone=(value) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/sfml/audio/listener.rb', line 75

def cone=(value)
  cone =
    case value
    when SoundCone then value
    when Hash      then SoundCone.new(**value)
    else
      raise ArgumentError, "Listener.cone= expects SoundCone or Hash; got #{value.class}"
    end
  C::Audio.sfListener_setCone(cone.to_native)
end

.directionObject

The forward direction of the listener (unit-length, doesn’t have to be). Default: (0, 0, -1) — looking into the screen.



35
36
37
# File 'lib/sfml/audio/listener.rb', line 35

def direction
  Vector3.from_native(C::Audio.sfListener_getDirection)
end

.direction=(value) ⇒ Object



39
40
41
42
# File 'lib/sfml/audio/listener.rb', line 39

def direction=(value)
  vec = value.is_a?(Vector3) ? value : Vector3.new(*value)
  C::Audio.sfListener_setDirection(vec.to_native_f)
end

.global_volumeObject

Master volume in the range [0, 100]. Affects all sounds, music, etc.



16
17
18
# File 'lib/sfml/audio/listener.rb', line 16

def global_volume
  C::Audio.sfListener_getGlobalVolume
end

.global_volume=(value) ⇒ Object



20
21
22
# File 'lib/sfml/audio/listener.rb', line 20

def global_volume=(value)
  C::Audio.sfListener_setGlobalVolume(value.to_f)
end

.positionObject



24
25
26
# File 'lib/sfml/audio/listener.rb', line 24

def position
  Vector3.from_native(C::Audio.sfListener_getPosition)
end

.position=(value) ⇒ Object



28
29
30
31
# File 'lib/sfml/audio/listener.rb', line 28

def position=(value)
  vec = value.is_a?(Vector3) ? value : Vector3.new(*value)
  C::Audio.sfListener_setPosition(vec.to_native_f)
end

.up_vectorObject

The “up” direction. Default: (0, 1, 0). Together with ‘direction` it determines listener orientation for stereo panning.



46
47
48
# File 'lib/sfml/audio/listener.rb', line 46

def up_vector
  Vector3.from_native(C::Audio.sfListener_getUpVector)
end

.up_vector=(value) ⇒ Object



50
51
52
53
# File 'lib/sfml/audio/listener.rb', line 50

def up_vector=(value)
  vec = value.is_a?(Vector3) ? value : Vector3.new(*value)
  C::Audio.sfListener_setUpVector(vec.to_native_f)
end

.velocityObject

Listener velocity in world units / second — used by the Doppler effect on sources whose ‘doppler_factor` is non-zero.



57
58
59
# File 'lib/sfml/audio/listener.rb', line 57

def velocity
  Vector3.from_native(C::Audio.sfListener_getVelocity)
end

.velocity=(value) ⇒ Object



61
62
63
64
# File 'lib/sfml/audio/listener.rb', line 61

def velocity=(value)
  vec = value.is_a?(Vector3) ? value : Vector3.new(*value)
  C::Audio.sfListener_setVelocity(vec.to_native_f)
end