summaryrefslogtreecommitdiff
path: root/normalizer
diff options
context:
space:
mode:
Diffstat (limited to 'normalizer')
-rwxr-xr-xnormalizer/normalizer.html171
-rwxr-xr-xnormalizer/sample-a.mp3bin0 -> 37473 bytes
-rwxr-xr-xnormalizer/sample-b.mp3bin0 -> 25377 bytes
-rwxr-xr-xnormalizer/sample-c.mp3bin0 -> 26145 bytes
-rwxr-xr-xnormalizer/sample-d.mp3bin0 -> 36705 bytes
-rwxr-xr-xnormalizer/sample-e.mp3bin0 -> 49761 bytes
-rwxr-xr-xnormalizer/sample-f.mp3bin0 -> 32993 bytes
-rwxr-xr-xnormalizer/sample-g.mp3bin0 -> 53793 bytes
8 files changed, 171 insertions, 0 deletions
diff --git a/normalizer/normalizer.html b/normalizer/normalizer.html
new file mode 100755
index 0000000..d166c65
--- /dev/null
+++ b/normalizer/normalizer.html
@@ -0,0 +1,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>
diff --git a/normalizer/sample-a.mp3 b/normalizer/sample-a.mp3
new file mode 100755
index 0000000..6b6041e
--- /dev/null
+++ b/normalizer/sample-a.mp3
Binary files differ
diff --git a/normalizer/sample-b.mp3 b/normalizer/sample-b.mp3
new file mode 100755
index 0000000..bb8c766
--- /dev/null
+++ b/normalizer/sample-b.mp3
Binary files differ
diff --git a/normalizer/sample-c.mp3 b/normalizer/sample-c.mp3
new file mode 100755
index 0000000..bc488f1
--- /dev/null
+++ b/normalizer/sample-c.mp3
Binary files differ
diff --git a/normalizer/sample-d.mp3 b/normalizer/sample-d.mp3
new file mode 100755
index 0000000..b51df1c
--- /dev/null
+++ b/normalizer/sample-d.mp3
Binary files differ
diff --git a/normalizer/sample-e.mp3 b/normalizer/sample-e.mp3
new file mode 100755
index 0000000..85ef615
--- /dev/null
+++ b/normalizer/sample-e.mp3
Binary files differ
diff --git a/normalizer/sample-f.mp3 b/normalizer/sample-f.mp3
new file mode 100755
index 0000000..17e5e96
--- /dev/null
+++ b/normalizer/sample-f.mp3
Binary files differ
diff --git a/normalizer/sample-g.mp3 b/normalizer/sample-g.mp3
new file mode 100755
index 0000000..e610b33
--- /dev/null
+++ b/normalizer/sample-g.mp3
Binary files differ