Class: Quake::Sound::Mixer
- Inherits:
-
Object
- Object
- Quake::Sound::Mixer
- Defined in:
- lib/quake/sound/mixer.rb
Overview
High-level sound manager: loads WAVs from PAK, plays them by name.
Constant Summary collapse
- MAX_CHANNELS =
16
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(pak) ⇒ Mixer
constructor
A new instance of Mixer.
- #loaded? ⇒ Boolean
- #open ⇒ Object
-
#play(path, volume: 128, loop_count: 0) ⇒ Object
Play a sound from the PAK.
Constructor Details
#initialize(pak) ⇒ Mixer
Returns a new instance of Mixer.
28 29 30 31 32 |
# File 'lib/quake/sound/mixer.rb', line 28 def initialize(pak) @pak = pak @chunks = {} # sound_path -> FFI pointer @open = false end |
Instance Method Details
#close ⇒ Object
45 46 47 48 49 50 |
# File 'lib/quake/sound/mixer.rb', line 45 def close @chunks.each_value { |chunk| MixerLib.Mix_FreeChunk(chunk) } @chunks.clear MixerLib.Mix_CloseAudio if @open @open = false end |
#loaded? ⇒ Boolean
65 66 67 |
# File 'lib/quake/sound/mixer.rb', line 65 def loaded? @open end |
#open ⇒ Object
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/quake/sound/mixer.rb', line 34 def open result = MixerLib.Mix_OpenAudio(22050, MixerLib::MIX_DEFAULT_FORMAT, 2, 1024) if result < 0 puts "Warning: Could not open audio" return end MixerLib.Mix_AllocateChannels(MAX_CHANNELS) @open = true puts "Audio initialized (#{MAX_CHANNELS} channels)" end |
#play(path, volume: 128, loop_count: 0) ⇒ Object
Play a sound from the PAK. path is relative (e.g. “items/health1.wav”). Returns channel number or -1 on failure.
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/quake/sound/mixer.rb', line 54 def play(path, volume: 128, loop_count: 0) return -1 unless @open full_path = path.start_with?("sound/") ? path : "sound/#{path}" chunk = load_chunk(full_path) return -1 unless chunk MixerLib.Mix_Volume(-1, volume) MixerLib.Mix_PlayChannelTimed(-1, chunk, loop_count, -1) end |