diff --git a/lib/dxruby_sdl/sound.rb b/lib/dxruby_sdl/sound.rb index c019e05..4b6cd6f 100644 --- a/lib/dxruby_sdl/sound.rb +++ b/lib/dxruby_sdl/sound.rb @@ -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) @@ -28,6 +39,8 @@ def initialize(filename) private class Music + include Common + def initialize(filename) @music = SDL::Mixer::Music.load(filename) end @@ -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) @@ -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