From 597d66ee80ed519af799c24f653a5c27e0b105ed Mon Sep 17 00:00:00 2001 From: sonota88 Date: Sat, 25 Jan 2025 12:04:29 +0900 Subject: [PATCH] Add Sound#set_volume (except for 'time' support) --- lib/dxopal/sound.rb | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/dxopal/sound.rb b/lib/dxopal/sound.rb index 1102647..100fb00 100644 --- a/lib/dxopal/sound.rb +++ b/lib/dxopal/sound.rb @@ -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 @@ -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 @@ -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