Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions lib/dxruby_sdl/sound.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ module DXRubySDL
class Sound
extend Forwardable

module Common
MAX_DXRUBY_VOLUME = 255
private_constant :MAX_DXRUBY_VOLUME
MAX_SDL_VOLUME = 128
private_constant :MAX_SDL_VOLUME

def dxruby_volume_to_sdl_volume(volume)
(volume * MAX_SDL_VOLUME.to_f / MAX_DXRUBY_VOLUME).round
end
end

@sdl_mixer_openend = false

def initialize(filename)
Expand All @@ -28,6 +39,8 @@ def initialize(filename)
private

class Music
include Common

def initialize(filename)
@music = SDL::Mixer::Music.load(filename)
end
Expand All @@ -37,12 +50,16 @@ def play
end

def set_volume(volume, time = 0)
raise NotImplementedError, 'Sound#set_volume(volume, time) with MIDI'
if time > 0
raise NotImplementedError, 'Sound#set_volume(volume, time != 0)'
end
@music.set_volume_music(dxruby_volume_to_sdl_volume(volume))
end
end
private_constant :Music

class Wave
include Common
extend Forwardable

def initialize(filename)
Expand All @@ -63,7 +80,7 @@ def set_volume(volume, time = 0)
if time > 0
raise NotImplementedError, 'Sound#set_volume(volume, time != 0)'
end
@wave.set_volume(volume)
@wave.set_volume(dxruby_volume_to_sdl_volume(volume))
end
end
private_constant :Wave
Expand Down