Class: Ruby2D::Sound

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

Overview

Sounds are intended to be short samples, played without interruption, like an effect.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, loop: false) ⇒ Sound

Load a sound from a file

Parameters:

  • path (String)

    File to load the sound from

  • loop (true, false) (defaults to: false)

    If true playback will loop automatically, default is false

Raises:

  • (Error)

    if file cannot be found or music could not be successfully loaded.



16
17
18
19
20
21
22
# File 'lib/ruby2d/sound.rb', line 16

def initialize(path, loop: false)
  raise Error, "Cannot find audio file `#{path}`" unless File.exist? path

  @path = path
  @loop = loop
  raise Error, "Sound `#{@path}` cannot be created" unless ext_init(@path)
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



9
10
11
# File 'lib/ruby2d/sound.rb', line 9

def data
  @data
end

#loopObject

Returns the value of attribute loop.



9
10
11
# File 'lib/ruby2d/sound.rb', line 9

def loop
  @loop
end

#pathObject (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/ruby2d/sound.rb', line 8

def path
  @path
end

Class Method Details

.mix_volumeObject

Get the volume of the sound mixer



51
52
53
# File 'lib/ruby2d/sound.rb', line 51

def self.mix_volume
  ext_get_mix_volume
end

.mix_volume=(volume) ⇒ Object

Set the volume of the sound mixer



56
57
58
59
# File 'lib/ruby2d/sound.rb', line 56

def self.mix_volume=(volume)
  # Clamp value to between 0-100
  ext_set_mix_volume(volume.clamp(0, 100))
end

Instance Method Details

#lengthObject

Returns the length in seconds



35
36
37
# File 'lib/ruby2d/sound.rb', line 35

def length
  ext_length
end

#playObject

Play the sound



25
26
27
# File 'lib/ruby2d/sound.rb', line 25

def play
  ext_play
end

#stopObject

Stop the sound



30
31
32
# File 'lib/ruby2d/sound.rb', line 30

def stop
  ext_stop
end

#volumeObject

Get the volume of the sound



40
41
42
# File 'lib/ruby2d/sound.rb', line 40

def volume
  ext_get_volume
end

#volume=(volume) ⇒ Object

Set the volume of the sound



45
46
47
48
# File 'lib/ruby2d/sound.rb', line 45

def volume=(volume)
  # Clamp value to between 0-100
  ext_set_volume(volume.clamp(0, 100))
end