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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
<script type="text/javascript">
</script>
<table>
<tr>
<th>
Not normalized
</th>
<th>
Normalized
</th>
<th>
Computed gain
</th>
</tr>
<tr>
<td>
<audio controls> <source src="sample-a.mp3" type="audio/mpeg">Audio element not supported</audio>
</td>
<td>
<audio id="sample-a-n" controls> <source src="sample-a.mp3" type="audio/mpeg">Audio element not supported</audio>
</td>
<td>
<div id="sample-a-d"></div>
</td>
</tr>
<tr>
<td>
<audio controls> <source src="sample-b.mp3" type="audio/mpeg">Audio element not supported</audio>
</td>
<td>
<audio id="sample-b-n" controls> <source src="sample-b.mp3" type="audio/mpeg">Audio element not supported</audio>
</td>
<td>
<div id="sample-b-d"></div>
</td>
</tr>
<tr>
<td>
<audio controls> <source src="sample-c.mp3" type="audio/mpeg">Audio element not supported</audio>
</td>
<td>
<audio id="sample-c-n" controls> <source src="sample-c.mp3" type="audio/mpeg">Audio element not supported</audio>
</td>
<td>
<div id="sample-c-d"></div>
</td>
</tr>
<tr>
<td>
<audio controls> <source src="sample-d.mp3" type="audio/mpeg">Audio element not supported</audio>
</td>
<td>
<audio id="sample-d-n" controls> <source src="sample-d.mp3" type="audio/mpeg">Audio element not supported</audio>
</td>
<td>
<div id="sample-d-d"></div>
</td>
</tr>
<tr>
<td>
<audio controls> <source src="sample-e.mp3" type="audio/mpeg">Audio element not supported</audio>
</td>
<td>
<audio id="sample-e-n" controls> <source src="sample-e.mp3" type="audio/mpeg">Audio element not supported</audio>
</td>
<td>
<div id="sample-e-d"></div>
</td>
</tr>
<tr>
<td>
<audio controls> <source src="sample-f.mp3" type="audio/mpeg">Audio element not supported</audio>
</td>
<td>
<audio id="sample-f-n" controls> <source src="sample-f.mp3" type="audio/mpeg">Audio element not supported</audio>
</td>
<td>
<div id="sample-f-d"></div>
</td>
</tr>
<tr>
<td>
<audio controls> <source src="sample-g.mp3" type="audio/mpeg">Audio element not supported</audio>
</td>
<td>
<audio id="sample-g-n" controls> <source src="sample-g.mp3" type="audio/mpeg">Audio element not supported</audio>
</td>
<td>
<div id="sample-g-d"></div>
</td>
</tr>
</table>
<script type="text/javascript">
function start() {
var audioCtx = new AudioContext();
// http://wiki.hydrogenaud.io/index.php?title=ReplayGain_specification
// TODO: do the loudness filtering (Butterworth, Yulewalk) IIR filters
function normalizedAudioElement(name) {
var audioElem = document.getElementById(name + "-n");
var src = audioCtx.createMediaElementSource(audioElem);
var gainNode = audioCtx.createGain();
gainNode.gain.value = 1.0;
audioElem.addEventListener("play", function() {
src.connect(gainNode);
gainNode.connect(audioCtx.destination);
}, true);
audioElem.addEventListener("pause", function() {
// disconnect the nodes on pause, otherwise all nodes always run
src.disconnect(gainNode);
gainNode.disconnect(audioCtx.destination);
}, true);
fetch(name + ".mp3")
.then(function(res) { return res.arrayBuffer(); })
.then(function(buf) {
return audioCtx.decodeAudioData(buf);
}).then(function(decodedData) {
var decodedBuffer = decodedData.getChannelData(0);
var sliceLen = Math.floor(decodedData.sampleRate * 0.05);
var averages = [];
var sum = 0.0;
for (var i = 0; i < decodedBuffer.length; i++) {
sum += decodedBuffer[i] ** 2;
if (i % sliceLen === 0) {
sum = Math.sqrt(sum / sliceLen);
averages.push(sum);
sum = 0;
}
}
// Ascending sort of the averages array
averages.sort(function(a, b) { return a - b; });
// Take the average at the 95th percentile
var a = averages[Math.floor(averages.length * 0.95)];
var gain = 1.0 / a;
// Perform some clamping
// gain = Math.max(gain, 0.02);
// gain = Math.min(gain, 100.0);
// ReplayGain uses pink noise for this one one but we just take
// some arbitrary value... we're no standard
// Important is only that we don't output on levels
// too different from other websites
gain = gain / 10.0;
console.log("gain determined", name, a, gain);
gainNode.gain.value = gain;
var gainTextElem = document.getElementById(name + "-d");
gainTextElem.textContent = gain.toPrecision(4);
});
}
normalizedAudioElement("sample-a");
normalizedAudioElement("sample-b");
normalizedAudioElement("sample-c");
normalizedAudioElement("sample-d");
normalizedAudioElement("sample-e");
normalizedAudioElement("sample-f");
normalizedAudioElement("sample-g");
}
</script>
|