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

.cone ⇒ Object

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).



77
78
79
# File 'lib/sfml/audio/listener.rb', line 77

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

.cone=(value) ⇒ Object

Set the cone. Accepts a SoundCone or a Hash.



82
83
84
85
86
87
88
89
90
91
# File 'lib/sfml/audio/listener.rb', line 82

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

.direction ⇒ Object

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



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

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

.direction=(value) ⇒ Object

Set the forward direction — accepts Vector3 or [x, y, z].



43
44
45
46
# File 'lib/sfml/audio/listener.rb', line 43

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

.global_volume ⇒ Object

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

Set the master volume in [0, 100].



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

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

.position ⇒ Object

The listener's 3D position in world space.



26
27
28
# File 'lib/sfml/audio/listener.rb', line 26

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

.position=(value) ⇒ Object

Set the position — accepts Vector3 or [x, y, z].



31
32
33
34
# File 'lib/sfml/audio/listener.rb', line 31

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

.up_vector ⇒ Object

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



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

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

.up_vector=(value) ⇒ Object

Set the up vector — accepts Vector3 or [x, y, z].



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

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

.velocity ⇒ Object

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



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

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

.velocity=(value) ⇒ Object

Set the velocity — accepts Vector3 or [x, y, z].



67
68
69
70
# File 'lib/sfml/audio/listener.rb', line 67

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