Class: SFML::Sound
- Inherits:
-
Object
- Object
- SFML::Sound
- 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
-
#buffer ⇒ Object
Returns the value of attribute buffer.
Instance Method Summary collapse
- #attenuation ⇒ Object
- #attenuation=(value) ⇒ Object
-
#initialize(buffer, volume: 100.0, pitch: 1.0, looping: false) ⇒ Sound
constructor
A new instance of Sound.
- #looping=(value) ⇒ Object
- #looping? ⇒ Boolean
- #min_distance ⇒ Object
- #min_distance=(value) ⇒ Object
- #pause ⇒ Object
- #paused? ⇒ Boolean
- #pitch ⇒ Object
- #pitch=(value) ⇒ Object
- #play ⇒ Object
- #playing? ⇒ Boolean
-
#position ⇒ Object
—- 3D positional audio —-.
- #position=(value) ⇒ Object
- #relative_to_listener=(value) ⇒ Object
- #relative_to_listener? ⇒ Boolean
- #status ⇒ Object
- #stop ⇒ Object
- #stopped? ⇒ Boolean
- #volume ⇒ Object
- #volume=(value) ⇒ Object
Constructor Details
#initialize(buffer, volume: 100.0, pitch: 1.0, looping: false) ⇒ Sound
Returns a new instance of Sound.
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
#buffer ⇒ Object
Returns the value of attribute buffer.
27 28 29 |
# File 'lib/sfml/audio/sound.rb', line 27 def buffer @buffer end |
Instance Method Details
#attenuation ⇒ Object
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
44 45 46 |
# File 'lib/sfml/audio/sound.rb', line 44 def looping? @looping end |
#min_distance ⇒ Object
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 |
#pause ⇒ Object
36 |
# File 'lib/sfml/audio/sound.rb', line 36 def pause = C::Audio.sfSound_pause(@handle) |
#paused? ⇒ Boolean
41 |
# File 'lib/sfml/audio/sound.rb', line 41 def paused? = status == :paused |
#pitch ⇒ Object
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 |
#play ⇒ Object
35 |
# File 'lib/sfml/audio/sound.rb', line 35 def play = C::Audio.sfSound_play(@handle) |
#playing? ⇒ Boolean
40 |
# File 'lib/sfml/audio/sound.rb', line 40 def = status == :playing |
#position ⇒ Object
—- 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
96 |
# File 'lib/sfml/audio/sound.rb', line 96 def relative_to_listener? = C::Audio.sfSound_isRelativeToListener(@handle) |
#status ⇒ Object
39 |
# File 'lib/sfml/audio/sound.rb', line 39 def status = C::Audio::STATUSES[C::Audio.sfSound_getStatus(@handle)] |
#stop ⇒ Object
37 |
# File 'lib/sfml/audio/sound.rb', line 37 def stop = C::Audio.sfSound_stop(@handle) |
#stopped? ⇒ Boolean
42 |
# File 'lib/sfml/audio/sound.rb', line 42 def stopped? = status == :stopped |