From 2ec4d259d3f80e818a29f478a049f59201baccee Mon Sep 17 00:00:00 2001 From: Junichi Ishikura Date: Fri, 10 Jul 2015 12:17:42 +0900 Subject: [PATCH 1/2] allow volume argument to be 0-255 and convert 0-128 for Ruby/SDL API. --- lib/dxruby_sdl/sound.rb | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/dxruby_sdl/sound.rb b/lib/dxruby_sdl/sound.rb index c019e05..15d27cf 100644 --- a/lib/dxruby_sdl/sound.rb +++ b/lib/dxruby_sdl/sound.rb @@ -37,7 +37,10 @@ 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((volume * 128 / 255).to_i) end end private_constant :Music @@ -63,7 +66,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((volume * 128 / 255).to_i) end end private_constant :Wave From 14db026d1f497488d16b511aead6f83d6245b243 Mon Sep 17 00:00:00 2001 From: Junichi Ishikura Date: Mon, 17 Aug 2015 11:19:09 +0900 Subject: [PATCH 2/2] refactored. --- lib/dxruby_sdl/sound.rb | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/dxruby_sdl/sound.rb b/lib/dxruby_sdl/sound.rb index 15d27cf..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 @@ -40,12 +53,13 @@ def set_volume(volume, time = 0) if time > 0 raise NotImplementedError, 'Sound#set_volume(volume, time != 0)' end - @music.set_volume_music((volume * 128 / 255).to_i) + @music.set_volume_music(dxruby_volume_to_sdl_volume(volume)) end end private_constant :Music class Wave + include Common extend Forwardable def initialize(filename) @@ -66,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 * 128 / 255).to_i) + @wave.set_volume(dxruby_volume_to_sdl_volume(volume)) end end private_constant :Wave