-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathYouTube-Volume-Booster.js
95 lines (78 loc) · 3.51 KB
/
YouTube-Volume-Booster.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// ==UserScript==
// @name Youtube Volume Booster
// @author emerysteele
// @namespace namespace_emerysteele
// @description Automatically boosts gain of quiet YouTube videos
// @version 2022.03.20
// @match https://www.youtube.com/*
// @noframes
// @grant none
// @compatible chrome
// @compatible firefox
// @compatible opera
// @compatible safari
// @compatible edge
// @connect youtube.com
// @run-at document-end
// ==/UserScript==
// The gain will only be boosted if the content loudness is below 0dB, if above 0dB gain is reset to 1x (YouTube handles normalization in this case)
// Volume boost is calculated as a gain multipler based on how quiet it is.
// 2022.03.20
// - First iteration
(function() {
window.addEventListener("yt-navigate-finish", main, true);
var gainAdjust = -0.0; //Use this to decrease or increase final gain boost by a set value
var video = undefined;
var audioCtx = undefined;
var mediaSource = undefined;
var gainNode = (gainNode === undefined) ? undefined : gainNode;
var player = undefined;
var volumeLevel = undefined;
var volumeGain = undefined;
var volumeGainPwr = undefined;
function main(){
console.log("New page loaded, running content loudness analysis and gain boost if needed.");
player = document.getElementById('movie_player');
volumeLevel = player.getStatsForNerds(0).volume.match(/(-?[0-9]\d*(\.\d+)?dB)/)[0];
volumeGain = volumeLevel.match(/[0-9]\d*(\.\d+)?/)[0];
volumeGainPwr = Math.pow(10,volumeGain/20);
console.log("Content loudness: "+volumeLevel);
if(volumeLevel.indexOf("-") == 0){
if(typeof gainNode == 'object'){
gainNode.gain.value = parseFloat(parseFloat(volumeGainPwr).toFixed(4)) + parseFloat(gainAdjust);
console.log("Needed gain: "+volumeGainPwr.toFixed(4)+"x");
console.log("Set gain (with gain adjust): "+gainNode.gain.value.toFixed(4)+"x");
}
else{
video = document.querySelector('video');
audioCtx = new AudioContext();
mediaSource = audioCtx.createMediaElementSource(video);
gainNode = audioCtx.createGain();
mediaSource.connect(gainNode);
gainNode.connect(audioCtx.destination);
gainNode.gain.value = parseFloat(parseFloat(volumeGainPwr).toFixed(4)) + parseFloat(gainAdjust);
console.log("Needed gain: "+volumeGainPwr.toFixed(4)+"x");
console.log("Set gain (with gain adjust): "+gainNode.gain.value.toFixed(4)+"x");
}
}
else{
if(typeof gainNode == 'object'){
gainNode.gain.value = parseFloat(1) + parseFloat(gainAdjust);
console.log("Needed gain: 1x");
console.log("Set gain (with gain adjust): "+gainNode.gain.value.toFixed(4)+"x");
}
else{
video = document.querySelector('video');
audioCtx = new AudioContext();
mediaSource = audioCtx.createMediaElementSource(video);
gainNode = audioCtx.createGain();
mediaSource.connect(gainNode);
gainNode.connect(audioCtx.destination);
gainNode.gain.value = parseFloat(1) + parseFloat(gainAdjust);
console.log("Needed gain: 1x");
console.log("Set gain (with gain adjust): "+gainNode.gain.value.toFixed(4)+"x");
}
}
}
main();
})();