Class: Rubydraw::Sound

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

Overview

Lets you access sound files and play them! A bit different then Gosu in that you don’t have to specify the window here; just a matter of taste. Otherwise it’s mostly the same:

Rubydraw::Sound#new: Creates a new sound with the given file path

Rubydraw::Sound#play: Plays the sound. Note: this method exits immediately; it doesn’t wait until the sound is finished.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Sound

Create a new sound with the given file path. Raise an SDLError if for some reason it couln’t load the sound.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rubydraw/sound.rb', line 16

def initialize(path)
  # In case program is being run from a different directory,
  # provide the _full_ path. Nothing relative here.
  full_path = File.expand_path path
  SDL::Mixer.OpenAudio(22050, SDL::AUDIO_S16SYS, 2, 1024)
  @sdl_sound = SDL::Mixer::LoadWAV(full_path)
  # Usually happens because the file doesn't exist.
  if @sdl_sound.pointer.null?
    raise SDLError, "Failed to load sound from: #{full_path} because '#{SDL.GetError}'"
  end
  # The default volume. Can be changed with Sound's volume attribute.
  @volume = 1
  # Allocate a new SDL channel all for this sound to use.
  @channel = SDL::Mixer.AllocateChannels(SDL::Mixer.AllocateChannels(-1) + 1) - 1
end

Instance Attribute Details

#volumeObject

Used to specify the volume (from 0 to 1) when this sound is played. Set at 1 by default



13
14
15
# File 'lib/rubydraw/sound.rb', line 13

def volume
  @volume
end

Instance Method Details

#pauseObject

Pause the sound so it can be resumed later.



41
42
43
44
# File 'lib/rubydraw/sound.rb', line 41

def pause
  SDL::Mixer.Pause(@channel)
  self
end

#paused?Boolean

Returns whether or not this sound is paused.

Returns:

  • (Boolean)


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

def paused?
  SDL::Mixer.Paused(@channel) == 1
end

#playObject

Play this sound, but don’t restart it if it’s already playing.

Raises:



33
34
35
36
37
38
# File 'lib/rubydraw/sound.rb', line 33

def play
  SDL::Mixer.Volume(@channel, (SDL::Mixer::MAX_VOLUME * @volume).to_i)
  result = SDL::Mixer.PlayChannelTimed(@channel, @sdl_sound, 0, -1)
  raise(SDLError, "Failed to play sound: #{SDL.GetError}") if result == -1
  self
end

#playing?Boolean

Returns whether or not this sound is playing.

Returns:

  • (Boolean)


66
67
68
# File 'lib/rubydraw/sound.rb', line 66

def playing?
  (SDL::Mixer.Playing(@channel) == 1) and not paused?
end

#resumeObject

Resume the sound from where it was paused.



54
55
56
57
# File 'lib/rubydraw/sound.rb', line 54

def resume
  SDL::Mixer.Resume(@channel)
  self
end

#stopObject

Stop the sound. Unlike pause because Sound#play will start from the beginning afterward.



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

def stop
  SDL::Mixer.HaltChannel(@channel)
  self
end

#stopped?Boolean

Returns whether or not this sound is stopped.

Returns:

  • (Boolean)


71
72
73
# File 'lib/rubydraw/sound.rb', line 71

def stopped?
  not playing?
end