Class: Tea::Sound

Inherits:
Object
  • Object
show all
Defined in:
lib/tea/c_sound.rb

Overview

The Sound class allows loading and playing of audio files.

Currently supported formats: OGG, WAV, AIFF, RIFF, VOC.

Constant Summary collapse

STOPPED =

Sound states returned by Tea::Sound#state.

:STOPPED
PLAYING =
:PLAYING
PAUSED =
:PAUSED
VOLUME_MAX =

As per Ruby/SDL’s mixer API.

128
@@volume =

Tracked because Ruby/SDL’s mixer API can’t get the channel volumes.

VOLUME_MAX
@@channel_sound_ids =

Tracks which Sound objects are playing in which channels, to report accurate playing state info.

[]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Sound

Load a sound file.

May raise Tea::Error on failure.



61
62
63
64
65
66
67
68
69
# File 'lib/tea/c_sound.rb', line 61

def initialize(path)
  @wave = SDL::Mixer::Wave.load(path)
  @channel = -1

  # Tracked because Ruby/SDL's mixer API can't get sound volumes.
  @volume = VOLUME_MAX
rescue SDL::Error => e
  raise Tea::Error, e.message, e.backtrace
end

Class Method Details

.pause_allObject

Pause all sound.



43
44
45
# File 'lib/tea/c_sound.rb', line 43

def Sound.pause_all
  SDL::Mixer.pause -1
end

.resume_allObject

Resume playing all paused sound.



48
49
50
# File 'lib/tea/c_sound.rb', line 48

def Sound.resume_all
  SDL::Mixer.resume -1
end

.stop_allObject

Stop all sounds, playing or paused.



53
54
55
# File 'lib/tea/c_sound.rb', line 53

def Sound.stop_all
  SDL::Mixer.halt -1
end

.volumeObject

Get the master volume. Volume ranges from 0..128 inclusive.



31
32
33
# File 'lib/tea/c_sound.rb', line 31

def Sound.volume
  @@volume
end

.volume=(new_volume) ⇒ Object

Set the master volume. new_volume should be between 0..128 inclusive.



36
37
38
39
40
# File 'lib/tea/c_sound.rb', line 36

def Sound.volume=(new_volume)
  v = (new_volume >= 0) ? (new_volume <= VOLUME_MAX ? new_volume : VOLUME_MAX) : 0
  SDL::Mixer.set_volume -1, new_volume
  @@volume = v
end

Instance Method Details

#pauseObject

Pause the sound if it’s playing, otherwise do nothing.



96
97
98
# File 'lib/tea/c_sound.rb', line 96

def pause
  SDL::Mixer.pause(@channel) if channel_valid?
end

#play(loops = 0) ⇒ Object

Play the sound. If this sound is still playing, cut it off and start playing it from the start.

loops should be the number of times to repeat playing the sound. Use -1 to loop the sound forever.



88
89
90
91
92
93
# File 'lib/tea/c_sound.rb', line 88

def play(loops=0)
  SDL::Mixer.halt(@channel) if channel_valid?

  @channel = SDL::Mixer.play_channel(-1, @wave, loops)
  @@channel_sound_ids[@channel] = object_id
end

#resumeObject

Resume the sound if it’s paused, otherwise do nothing.



101
102
103
# File 'lib/tea/c_sound.rb', line 101

def resume
  SDL::Mixer.resume(@channel) if channel_valid?
end

#stateObject

Check if the sound is stopped, playing or paused. Returns one of Tea::Sound::STOPPED, Tea::Sound::PLAYING or Tea::Sound::PAUSED. A sound that isn’t playing or paused is considered stopped.



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/tea/c_sound.rb', line 113

def state
  if channel_valid?
    if SDL::Mixer.play?(@channel)
      # For some reason, pause? returns 0 and 1 instead of true and false.
      if SDL::Mixer.pause?(@channel) != 0
        PAUSED
      else
        PLAYING
      end
    else
      STOPPED
    end
  else
    STOPPED
  end
end

#stopObject

Stop the sound if it’s playing or paused, otherwise do nothing.



106
107
108
# File 'lib/tea/c_sound.rb', line 106

def stop
  SDL::Mixer.halt(@channel) if channel_valid?
end

#volumeObject

Get the sound’s volume. Volume ranges from 0..128 inclusive.



72
73
74
# File 'lib/tea/c_sound.rb', line 72

def volume
  @volume
end

#volume=(new_volume) ⇒ Object

Set the sound’s volume. new_volume should be between 0..128 inclusive.



77
78
79
80
81
# File 'lib/tea/c_sound.rb', line 77

def volume=(new_volume)
  v = (new_volume >= 0) ? (new_volume <= VOLUME_MAX ? new_volume : VOLUME_MAX) : 0
  @wave.set_volume v
  @volume = v
end