diff options
Diffstat (limited to 'assets/js/normalizer.js')
-rw-r--r-- | assets/js/normalizer.js | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/assets/js/normalizer.js b/assets/js/normalizer.js new file mode 100644 index 0000000..c18d242 --- /dev/null +++ b/assets/js/normalizer.js @@ -0,0 +1,40 @@ +window.currentNormalizationContext = null; +window.currentNormalizationContext2 = null; +window.currentNormalizationContext3 = null; + +function normalizeAudio(ab, gainBoost) { + ab = ab.slice(0); + + return new Promise((res) => { + let currentNormalizationProfile = currentNormalizationContext.createGain(); + currentNormalizationProfile.gain.value = (1.0 + gainBoost) / 10.0; + + if (localStorage.getItem("normalize") === "false") res(currentNormalizationProfile); + + currentNormalizationContext.decodeAudioData(ab).then(function(decodedData) { + let decodedBuffer = decodedData.getChannelData(0); + let sliceLen = Math.floor(decodedData.sampleRate * 0.05); + let averages = []; + let sum = 0.0; + + for (let i = 0; i < decodedBuffer.length; i++) { + sum += decodedBuffer[i] ** 2; + if (i % sliceLen === 0) { + sum = Math.sqrt(sum / sliceLen); + averages.push(sum); + sum = 0; + } + } + + averages.sort(function(a, b) { return a - b; }); + let a = averages[Math.floor(averages.length * 0.95)]; + + let gain = (1.0 + gainBoost) / a; + gain = gain / 10.0; + + console.log("Calculated gain:", gain); + currentNormalizationProfile.gain.value = gain; + res(currentNormalizationProfile); + }); + }); +}
\ No newline at end of file |