Skip to content

Conversation

junichi-ishikura
Copy link

Sound#set_volume( volume, time=0 ) を修正し、 0〜255 で指定されたボリュームを Ruby/SDL のAPIに合わせて 0〜128 に変換するようにしました。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

あらためて見てみると volume * 128 / 255 が半端な値になることがあるため、四捨五入しましょうかね (volume * 128.0 / 255).round

また、下部でも同じ処理をしているので、メソッドにくくりだしましょうかね。

def dxruby_volume_to_sdl_volume(volume)
  (volume * 128.0 / 255).round
end

そして、 128 と 255 を定数にするとより良いでしょうね。

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

なお、 MAX_DXRUBY_VOLUMEMAX_SDL_VOLUMEをprivateスコープにしているのは、DXRubyではそれらを定義していないからです。dxruby_sdl では、極力、DXRubyが提供していないものは、提供しないようにしています。

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ボリュームの変換処理をくくりだす dxruby_volume_to_sdl_volume(volume)メソッドは、Soundクラスに定義してMusic#set_volumeWave#set_volumeから使用するイメージですか:question:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dxruby_volume_to_sdl_volume(volume)Common モジュールに定義して、 MusicWaveCommoninclude することにしましょうかね。

@takaokouji
Copy link

LGTM

takaokouji added a commit that referenced this pull request Mar 2, 2016
allow volume argument to be 0-255 and convert 0-128 for Ruby/SDL API.
@takaokouji takaokouji merged commit 10bd165 into smalruby:master Mar 2, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants