Class: Ruby2D::Music

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

Overview

Music is for longer pieces which can be played, paused, stopped, resumed, and faded out, like a background soundtrack.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, loop: false) ⇒ Music

Load music from a file

Parameters:

  • path (String)

    File to load the music 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.



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

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

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

Instance Attribute Details

#dataObject

Returns the value of attribute data.



10
11
12
# File 'lib/ruby2d/music.rb', line 10

def data
  @data
end

#loopObject

Returns the value of attribute loop.



10
11
12
# File 'lib/ruby2d/music.rb', line 10

def loop
  @loop
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

Class Method Details

.volumeObject

Returns the volume, in percentage



47
48
49
# File 'lib/ruby2d/music.rb', line 47

def volume
  ext_get_volume
end

.volume=(volume) ⇒ Object

Set music volume, 0 to 100%



52
53
54
55
# File 'lib/ruby2d/music.rb', line 52

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

Instance Method Details

#fadeout(milliseconds) ⇒ Object

Fade out music over provided milliseconds



68
69
70
# File 'lib/ruby2d/music.rb', line 68

def fadeout(milliseconds)
  ext_fadeout(milliseconds)
end

#lengthObject

Returns the length in seconds



73
74
75
# File 'lib/ruby2d/music.rb', line 73

def length
  ext_length
end

#pauseObject

Pause the music



31
32
33
# File 'lib/ruby2d/music.rb', line 31

def pause
  ext_pause
end

#playObject

Play the music



26
27
28
# File 'lib/ruby2d/music.rb', line 26

def play
  ext_play
end

#resumeObject

Resume paused music



36
37
38
# File 'lib/ruby2d/music.rb', line 36

def resume
  ext_resume
end

#stopObject

Stop playing the music, start at beginning



41
42
43
# File 'lib/ruby2d/music.rb', line 41

def stop
  ext_stop
end

#volumeObject

Alias instance methods to class methods



59
60
61
# File 'lib/ruby2d/music.rb', line 59

def volume
  Music.volume
end

#volume=(volume) ⇒ Object



63
64
65
# File 'lib/ruby2d/music.rb', line 63

def volume=(volume)
  Music.volume = (volume)
end