Class: SoundManager

Inherits:
Object show all
Defined in:
lib/gamebox/core/sound_manager.rb

Overview

SoundManager auto loads sounds/music from data/sounds and data/music. It makes them accessible via name. The name is a symbol of the filename without extension. This means that foo.wav and foo.ogg in the sound directory will generate indeterminent behavior.

Constant Summary collapse

SUPPORTED_AUDIO_EXTS =
%w(wav ogg mp3 au aiff caf)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSoundManager

checks to see if sdl_mixer is availalbe and preloads the sounds and music directories.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/gamebox/core/sound_manager.rb', line 11

def initialize

  puts 'CHANGE TO LOG:Warning, sound disabled' unless
  @enabled = (config_manager.settings[:sound].nil? or config_manager.settings[:sound] == true)

  if @enabled
    @music = {}
    music_path = Gamebox.configuration.music_path
    files = Dir.glob "#{music_path}**.{#{SUPPORTED_AUDIO_EXTS.join(',')}}"
    for f in files
      name = File.basename(f)
      begin
        sym = name.gsub(" ","_").split(".")[0..-2].join(".").to_sym
        ext = name.gsub(" ","_").split(".").last
        unless ext == "txt"
          @music[sym] = resource_manager.load_music(f)
        end
      rescue;end
    end if files

    @sounds = {}
    @playing_sounds = {}
    sound_path = Gamebox.configuration.sound_path
    files = Dir.glob "#{sound_path}**.{#{SUPPORTED_AUDIO_EXTS.join(',')}}"
    for f in files
      name = File.basename(f)
      begin
        sym = name.gsub(" ","_").split(".")[0..-2].join(".").to_sym
        ext = name.gsub(" ","_").split(".").last
        unless ext == "txt"
          @sounds[sym] = resource_manager.load_sound(f)
        end
      rescue;end
    end if files
  end
end

Instance Attribute Details

#musicObject

Returns the value of attribute music.



5
6
7
# File 'lib/gamebox/core/sound_manager.rb', line 5

def music
  @music
end

#soundsObject

Returns the value of attribute sounds.



5
6
7
# File 'lib/gamebox/core/sound_manager.rb', line 5

def sounds
  @sounds
end

Instance Method Details

#enabled?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/gamebox/core/sound_manager.rb', line 48

def enabled?
  @enabled
end

#pause_music(what) ⇒ Object

pause the music file that is passed in. pause_music :foo



84
85
86
87
88
# File 'lib/gamebox/core/sound_manager.rb', line 84

def pause_music(what)
  if @enabled
    @music[what].pause if @music[what]
  end
end

#play_music(what, opts = {}) ⇒ Object

plays the music based on the name with the specified volume level. will loop until SoundManager#stop_music is called. play_music :foo, :volume => 0.8 # play music at 80% volumne



64
65
66
67
68
69
70
71
72
# File 'lib/gamebox/core/sound_manager.rb', line 64

def play_music(what, opts={})
  if @enabled && @music[what]
    volume = opts.delete :volume
    repeat = opts[:repeat]
    repeat ||= false
    @music[what].volume = volume if volume
    @music[what].play repeat
  end
end

#play_sound(what, opts = {}) ⇒ Object

plays the sound based on the name with the specified volume level. play_sound :foo # play sound at 100% volume



54
55
56
57
58
59
# File 'lib/gamebox/core/sound_manager.rb', line 54

def play_sound(what, opts={})
  if @enabled && @sounds[what]
    merged_opts = {volume:1, speed:1, looping:false}.merge opts
    @playing_sounds[what] = @sounds[what].play merged_opts[:volume], merged_opts[:speed], merged_opts[:looping]
  end
end

#stop_music(what) ⇒ Object

stops the music file that is passed in. stop_music :foo



76
77
78
79
80
# File 'lib/gamebox/core/sound_manager.rb', line 76

def stop_music(what)
  if @enabled
    @music[what].stop if @music[what]
  end
end

#stop_sound(what) ⇒ Object

stops the sound that is passed in. stop_sound :foo



92
93
94
95
96
# File 'lib/gamebox/core/sound_manager.rb', line 92

def stop_sound(what)
  if @enabled
    @playing_sounds[what].stop if @playing_sounds[what]
  end
end