Class: Quake::Sound::Mixer

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(pak) ⇒ Mixer

Returns a new instance of Mixer.



58
59
60
61
62
# File 'lib/quake/sound/mixer.rb', line 58

def initialize(pak)
  @pak = pak
  @chunks = {} # sound_path -> FFI pointer
  @open = false
end

Instance Method Details

#closeObject



80
81
82
83
84
85
# File 'lib/quake/sound/mixer.rb', line 80

def close
  @chunks.each_value { |chunk| MixerLib.Mix_FreeChunk(chunk) }
  @chunks.clear
  MixerLib.Mix_CloseAudio if @open
  @open = false
end

#loaded?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/quake/sound/mixer.rb', line 100

def loaded?
  @open
end

#openObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/quake/sound/mixer.rb', line 64

def open
  unless MixerLib.available?
    puts "Warning: SDL2_mixer not available (#{MixerLib::LOAD_ERROR.message})"
    return
  end

  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.



89
90
91
92
93
94
95
96
97
98
# File 'lib/quake/sound/mixer.rb', line 89

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