Class: Ruby2D::Music
- Inherits:
-
Object
- Object
- Ruby2D::Music
- 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
-
#data ⇒ Object
Returns the value of attribute data.
-
#loop ⇒ Object
Returns the value of attribute loop.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
-
.volume ⇒ Object
Returns the volume, in percentage.
-
.volume=(volume) ⇒ Object
Set music volume, 0 to 100%.
Instance Method Summary collapse
-
#fadeout(milliseconds) ⇒ Object
Fade out music over provided milliseconds.
-
#initialize(path, loop: false) ⇒ Music
constructor
Load music from a file.
-
#length ⇒ Object
Returns the length in seconds.
-
#pause ⇒ Object
Pause the music.
-
#play ⇒ Object
Play the music.
-
#resume ⇒ Object
Resume paused music.
-
#stop ⇒ Object
Stop playing the music, start at beginning.
-
#volume ⇒ Object
Alias instance methods to class methods.
- #volume=(volume) ⇒ Object
Constructor Details
#initialize(path, loop: false) ⇒ Music
Load music from a file
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
#data ⇒ Object
Returns the value of attribute data.
10 11 12 |
# File 'lib/ruby2d/music.rb', line 10 def data @data end |
#loop ⇒ Object
Returns the value of attribute loop.
10 11 12 |
# File 'lib/ruby2d/music.rb', line 10 def loop @loop end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
9 10 11 |
# File 'lib/ruby2d/music.rb', line 9 def path @path end |
Class Method Details
.volume ⇒ Object
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 |
#length ⇒ Object
Returns the length in seconds
73 74 75 |
# File 'lib/ruby2d/music.rb', line 73 def length ext_length end |
#pause ⇒ Object
Pause the music
31 32 33 |
# File 'lib/ruby2d/music.rb', line 31 def pause ext_pause end |
#play ⇒ Object
Play the music
26 27 28 |
# File 'lib/ruby2d/music.rb', line 26 def play ext_play end |
#resume ⇒ Object
Resume paused music
36 37 38 |
# File 'lib/ruby2d/music.rb', line 36 def resume ext_resume end |
#stop ⇒ Object
Stop playing the music, start at beginning
41 42 43 |
# File 'lib/ruby2d/music.rb', line 41 def stop ext_stop end |