Class: TextToNoise::Player

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/text_to_noise/player.rb

Defined Under Namespace

Classes: SoundNotFound

Constant Summary collapse

SOUND_DIR =
File.expand_path File.join( APP_ROOT, 'sounds' )

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger

Constructor Details

#initializePlayer

Returns a new instance of Player.



12
13
14
15
16
17
18
19
20
# File 'lib/text_to_noise/player.rb', line 12

def initialize()
  self.class.load_rubygame
  
  Rubygame::Sound.autoload_dirs << SOUND_DIR
  themes = Dir.new(SOUND_DIR).entries.reject { |e| e =~ /^[.]/ }
  Rubygame::Sound.autoload_dirs.concat themes.map { |t| File.join SOUND_DIR, t }
  
  @sounds = []
end

Class Method Details

.load_rubygameObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/text_to_noise/player.rb', line 48

def self.load_rubygame()
  old_verbose, $VERBOSE = $VERBOSE, nil
  old_stream, stream = $stdout.dup, $stdout

  return if defined? Rubygame::Sound
  
  stream.reopen '/dev/null'
  stream.sync = true
  
  begin
    require 'rubygame'
  rescue LoadError
    require 'rubygems'
    require 'rubygame'
  end
  
  raise "No Mixer found!  Make sure sdl_mixer is installed." unless defined? Rubygame::Sound
ensure
  $VERBOSE = old_verbose
  stream.reopen(old_stream)
end

Instance Method Details

#available_soundsObject



42
43
44
45
46
# File 'lib/text_to_noise/player.rb', line 42

def available_sounds()
  Rubygame::Sound.autoload_dirs.map do |dir|
    Dir[ "#{dir}/*.wav" ].map { |f| File.basename f }
  end.flatten
end

#play(sound_name) ⇒ Object

Raises:



22
23
24
25
26
27
28
29
30
31
# File 'lib/text_to_noise/player.rb', line 22

def play( sound_name )
  sound = Rubygame::Sound[sound_name]
  raise SoundNotFound, sound_name if sound.nil?

  debug "Playing #{sound_name}"
  sound.play :fade_out => 2
  @sounds << sound

  debug "#{@sounds.size} sounds in queue" if playing?
end

#playing?Boolean

Returns:

  • (Boolean)


33
34
35
36
# File 'lib/text_to_noise/player.rb', line 33

def playing?
  @sounds = @sounds.select &:playing?
  not @sounds.empty?
end

#sound_dirsObject



38
39
40
# File 'lib/text_to_noise/player.rb', line 38

def sound_dirs()
  Rubygame::Sound.autoload_dirs
end