Skip to content

Commit

Permalink
Merge pull request #55 from sonota88/feat-sound-set-volume
Browse files Browse the repository at this point in the history
Add Sound#set_volume (except for 'time' support)
  • Loading branch information
yhara authored Jan 26, 2025
2 parents 45f1976 + 597d66e commit b9cb9d2
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/dxopal/sound.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ def self._load(path_or_url)

def initialize(path_or_url)
@path_or_url = path_or_url # Used in error message
@gain_node = Sound.audio_context.JS.createGain()
set_volume(230)
end
attr_accessor :decoded

Expand All @@ -49,7 +51,9 @@ def play(loop_ = false)
if (#{loop_}) {
source.loop = true;
}
source.connect(context.destination);
source
.connect(#{@gain_node})
.connect(context.destination);
source.start(0);
}
@source = source
Expand All @@ -61,5 +65,14 @@ def stop
return unless @source
@source.JS.stop()
end

# TODO: support for volume change using 'time' parameter
def set_volume(volume, time=0)
%x{
var normalized = (volume > 255 ? 255 : volume) / 255;
var db = (normalized - 1) * 96;
#{@gain_node}.gain.value = Math.pow(10, db / 20);
}
end
end
end

0 comments on commit b9cb9d2

Please sign in to comment.