Class: SFML::Sound

Inherits:
Object
  • Object
show all
Defined in:
lib/sfml/audio/sound.rb

Overview

A short sound that plays from a SoundBuffer held entirely in memory. Cheap to create, suitable for game SFX (blips, hits, footsteps).

buffer = SFML::SoundBuffer.load("blip.wav")
sound  = SFML::Sound.new(buffer, volume: 80, pitch: 1.2, looping: true)
sound.play

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(buffer, volume: 100.0, pitch: 1.0, looping: false) ⇒ Sound

Returns a new instance of Sound.

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/sfml/audio/sound.rb', line 9

def initialize(buffer, volume: 100.0, pitch: 1.0, looping: false)
  raise ArgumentError, "Sound requires a SFML::SoundBuffer" unless buffer.is_a?(SoundBuffer)

  ptr = C::Audio.sfSound_create(buffer.handle)
  raise Error, "sfSound_create returned NULL" if ptr.null?
  @handle = FFI::AutoPointer.new(ptr, C::Audio.method(:sfSound_destroy))
  @buffer  = buffer # keep alive
  # @looping mirrors the loop flag because SFML 3's isLooping reads
  # through an OpenAL source that may be unallocated on systems
  # without an audio device (some CI runners). Caching on the Ruby
  # side keeps observable behaviour deterministic regardless.
  @looping = false

  self.volume  = volume
  self.pitch   = pitch
  self.looping = looping
end

Instance Attribute Details

#bufferObject

Returns the value of attribute buffer.



27
28
29
# File 'lib/sfml/audio/sound.rb', line 27

def buffer
  @buffer
end

Instance Method Details

#attenuationObject



84
# File 'lib/sfml/audio/sound.rb', line 84

def attenuation = C::Audio.sfSound_getAttenuation(@handle)

#attenuation=(value) ⇒ Object



86
87
88
# File 'lib/sfml/audio/sound.rb', line 86

def attenuation=(value)
  C::Audio.sfSound_setAttenuation(@handle, value.to_f)
end

#looping=(value) ⇒ Object



48
49
50
51
# File 'lib/sfml/audio/sound.rb', line 48

def looping=(value)
  @looping = value ? true : false
  C::Audio.sfSound_setLooping(@handle, @looping)
end

#looping?Boolean

Returns:

  • (Boolean)


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

def looping?
  @looping
end

#min_distanceObject



90
# File 'lib/sfml/audio/sound.rb', line 90

def min_distance = C::Audio.sfSound_getMinDistance(@handle)

#min_distance=(value) ⇒ Object



92
93
94
# File 'lib/sfml/audio/sound.rb', line 92

def min_distance=(value)
  C::Audio.sfSound_setMinDistance(@handle, value.to_f)
end

#pauseObject



36
# File 'lib/sfml/audio/sound.rb', line 36

def pause  = C::Audio.sfSound_pause(@handle)

#paused?Boolean

Returns:

  • (Boolean)


41
# File 'lib/sfml/audio/sound.rb', line 41

def paused?     = status == :paused

#pitchObject



59
# File 'lib/sfml/audio/sound.rb', line 59

def pitch       = C::Audio.sfSound_getPitch(@handle)

#pitch=(value) ⇒ Object



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

def pitch=(value)
  C::Audio.sfSound_setPitch(@handle, value.to_f)
end

#playObject



35
# File 'lib/sfml/audio/sound.rb', line 35

def play   = C::Audio.sfSound_play(@handle)

#playing?Boolean

Returns:

  • (Boolean)


40
# File 'lib/sfml/audio/sound.rb', line 40

def playing?    = status == :playing

#positionObject

—- 3D positional audio —-

Sounds have a 3D position; the SFML::Listener acts as the “ear”. Volume falls off with distance from min_distance outward, scaled by attenuation (0 = no falloff, 1 = realistic, higher = sharper). By default a Sound’s position is in world coordinates; flip ‘relative_to_listener = true` and the position becomes relative to the listener — useful for “stuck to the camera” UI sounds.

For 2D games, set z = 0 and listener.position to your camera.



75
76
77
# File 'lib/sfml/audio/sound.rb', line 75

def position
  Vector3.from_native(C::Audio.sfSound_getPosition(@handle))
end

#position=(value) ⇒ Object



79
80
81
82
# File 'lib/sfml/audio/sound.rb', line 79

def position=(value)
  vec = value.is_a?(Vector3) ? value : Vector3.new(*value)
  C::Audio.sfSound_setPosition(@handle, vec.to_native_f)
end

#relative_to_listener=(value) ⇒ Object



98
99
100
# File 'lib/sfml/audio/sound.rb', line 98

def relative_to_listener=(value)
  C::Audio.sfSound_setRelativeToListener(@handle, value ? true : false)
end

#relative_to_listener?Boolean

Returns:

  • (Boolean)


96
# File 'lib/sfml/audio/sound.rb', line 96

def relative_to_listener? = C::Audio.sfSound_isRelativeToListener(@handle)

#statusObject



39
# File 'lib/sfml/audio/sound.rb', line 39

def status      = C::Audio::STATUSES[C::Audio.sfSound_getStatus(@handle)]

#stopObject



37
# File 'lib/sfml/audio/sound.rb', line 37

def stop   = C::Audio.sfSound_stop(@handle)

#stopped?Boolean

Returns:

  • (Boolean)


42
# File 'lib/sfml/audio/sound.rb', line 42

def stopped?    = status == :stopped

#volumeObject



53
# File 'lib/sfml/audio/sound.rb', line 53

def volume      = C::Audio.sfSound_getVolume(@handle)

#volume=(value) ⇒ Object



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

def volume=(value)
  C::Audio.sfSound_setVolume(@handle, value.to_f)
end