From c2b015a15585d82487979ded6d3bea2104f7ca4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E5=A4=AA?= Date: Sun, 24 Jan 2016 00:34:06 +1100 Subject: [PATCH] Put volume control on a loudness scale rather than amplitude --- js/battle.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/js/battle.js b/js/battle.js index 284361548..953757594 100644 --- a/js/battle.js +++ b/js/battle.js @@ -139,16 +139,22 @@ var BattleSoundLibrary = (function () { if (this.bgm) this.bgm.play(); } }; + + function loudnessPercentToAmplitudePercent(loudnessPercent) { + // 10 dB is perceived as approximately twice as loud + var decibels = 10 * Math.log(loudnessPercent / 100) / Math.log(2); + return Math.pow(10, decibels / 20) * 100; + } BattleSoundLibrary.prototype.setBgmVolume = function (bgmVolume) { - this.bgmVolume = bgmVolume; + this.bgmVolume = loudnessPercentToAmplitudePercent(bgmVolume); if (this.bgm) { try { - this.bgm.setVolume(bgmVolume); + this.bgm.setVolume(this.bgmVolume); } catch (e) {} } }; BattleSoundLibrary.prototype.setEffectVolume = function (effectVolume) { - this.effectVolume = effectVolume; + this.effectVolume = loudnessPercentToAmplitudePercent(effectVolume); }; return BattleSoundLibrary;