diff options
author | RaindropsSys <raindrops@equestria.dev> | 2023-10-30 23:08:45 +0100 |
---|---|---|
committer | RaindropsSys <raindrops@equestria.dev> | 2023-10-30 23:08:45 +0100 |
commit | 41c51b8bdb9c8e9fa4a7d56f260d594739d4107e (patch) | |
tree | 4bb3e824d636c7cf8cb39fd0e1aa25c49c339164 /assets/js/normalizer.js | |
parent | 4d4308c46d4f7801c657cc79d2243e1a81831334 (diff) | |
download | mist-41c51b8bdb9c8e9fa4a7d56f260d594739d4107e.tar.gz mist-41c51b8bdb9c8e9fa4a7d56f260d594739d4107e.tar.bz2 mist-41c51b8bdb9c8e9fa4a7d56f260d594739d4107e.zip |
Updated 35 files and added 28 files (automated)
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 |