-
Notifications
You must be signed in to change notification settings - Fork 6
/
compression-curve2.html
384 lines (288 loc) · 9.92 KB
/
compression-curve2.html
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
<!--
Copyright 2011, Google Inc.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
* Neither the name of Google Inc. nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>
</title>
<!-- Slider stuff -->
<script type="text/javascript" src="lib/events.js"></script>
<style type="text/css">
#slider { margin: 10px; }
</style>
<link rel="stylesheet" type="text/css" href = "style/simple.css" />
<!-- Our javascript code -->
<script type="text/javascript">
// Events
// init() once the page has finished loading.
window.onload = init;
var canvas;
var canvasContext;
var canvasWidth = 0;
var canvasHeight = 0;
var backgroundColor = "rgb(0, 0, 0)";
var curveColor = "rgb(192,192,192)";
var gridColor = "rgb(200,200,200)";
var thresholdColor = "rgb(75,25,25)";
var sampleRate = 44100.0;
var nyquist = 0.5 * sampleRate;
var threshold = -24;
var knee = 30;
var ratio = 12;
var makeupGain = 0;
var kneeThreshold = 1; // computed at draw time
var kneeThresholdDb;
var maxOutputDb = 6;
var minOutputDb = -36;
function xpixelToDb(x) {
// This is right even though it looks like we should scale by width.
// We want the same pixel/dB scale for both.
var k = x / canvas.height;
var db = minOutputDb + k * (maxOutputDb - minOutputDb);
return db;
}
function dBToXPixel(db) {
var k = (db - minOutputDb) / (maxOutputDb - minOutputDb);
var x = k * canvas.height;
return x;
}
function ypixelToDb(y) {
var k = y / canvas.height;
var db = maxOutputDb - k * (maxOutputDb - minOutputDb);
return db;
}
function dBToYPixel(db) {
var k = (maxOutputDb - db) / (maxOutputDb - minOutputDb);
var y = k * canvas.height;
return y;
}
function decibelsToLinear(db) {
return Math.pow(10.0, 0.05 * db);
}
function linearToDecibels(x) {
return 20.0 * Math.log(x) / Math.LN10;
}
var m_ratio;
var m_slope;
var m_linearThreshold;
var m_thresholdDb;
var m_kneeDb;
var m_kneeThreshold;
var m_kneeThresholdDb;
var m_ykneeThresholdDb;
// Approximate 1st derivative with input and output expressed in dB.
// This slope is equal to the inverse of the compression "ratio".
// In other words, a compression ratio of 20 would be a slope of 1/20.
function slopeAt(x, k) {
if (x < m_linearThreshold)
return 1;
var x2 = x * 1.001;
var xDb = linearToDecibels(x);
var x2Db = linearToDecibels(x2);
var yDb = linearToDecibels(saturateBasic(x, k));
var y2Db = linearToDecibels(saturateBasic(x2, k));
var m = (y2Db - yDb) / (x2Db - xDb);
return m;
}
function kAtSlope(slope) {
var xDb = m_thresholdDb + m_kneeDb;
var x = decibelsToLinear(xDb);
var minK = 0.1;
var maxK = 10000;
var k = 5;
// Approximate.
for (var i = 0; i < 15; ++i) {
m = slopeAt(x, k);
// console.log("slope = " + slope + " : m = " + m + " : k = " + k);
if (m < slope) {
// k is too high.
maxK = k;
k = Math.sqrt(minK * maxK);
} else {
// k is not high enough.
minK = k;
k = Math.sqrt(minK * maxK);
}
}
return k;
}
// Exponential saturation curve.
function saturateBasic(x, k)
{
if (x < m_linearThreshold)
return x;
return m_linearThreshold + (1 - Math.exp(-k * (x - m_linearThreshold))) / k;
}
function saturate(x, k)
{
var y;
if (x < m_kneeThreshold) {
y = saturateBasic(x, k);
} else {
var xDb = linearToDecibels(x);
var yDb = m_ykneeThresholdDb + m_slope * (xDb - m_kneeThresholdDb);
y = decibelsToLinear(yDb);
}
return y;
}
function drawCurve() {
// Update curve state.
var dbThreshold = threshold;
var dbKnee = knee;
var linearThreshold = decibelsToLinear(dbThreshold);
var linearKnee = decibelsToLinear(dbKnee);
m_linearThreshold = linearThreshold;
m_thresholdDb = dbThreshold;
m_kneeDb = dbKnee;
// Makeup gain.
var maximum = 1.05 * linearKnee * linearThreshold;
// Compute knee threshold.
m_ratio = ratio;
m_slope = 1 / m_ratio;
var k = kAtSlope(1 / m_ratio);
// console.log("k = " + k);
m_kneeThresholdDb = dbThreshold + knee;
m_kneeThreshold = decibelsToLinear(m_kneeThresholdDb);
m_ykneeThresholdDb = linearToDecibels(saturateBasic(m_kneeThreshold, k));
var info = document.getElementById("info");
// draw center
var width = canvas.width;
var height = canvas.height;
canvasContext.fillStyle = backgroundColor;
canvasContext.fillRect(0, 0, width, height);
// Draw linear response.
canvasContext.strokeStyle = gridColor;
canvasContext.lineWidth = 1;
canvasContext.beginPath();
canvasContext.moveTo(dBToXPixel(minOutputDb), dBToYPixel(minOutputDb));
canvasContext.lineTo(dBToXPixel(maxOutputDb), dBToYPixel(maxOutputDb));
canvasContext.stroke();
// Draw 0dBFS output levels from 0dBFS down to -36dBFS
for (var dbFS = 0; dbFS >= -36; dbFS -= 6) {
canvasContext.beginPath();
var y = dBToYPixel(dbFS);
canvasContext.moveTo(0, y);
canvasContext.lineTo(width, y);
canvasContext.stroke();
canvasContext.textAlign = "center";
canvasContext.strokeText(dbFS.toFixed(0) + " dBFS", 25, y - 5);
}
// Draw 0dBFS input line
canvasContext.beginPath();
canvasContext.moveTo(dBToXPixel(0), 0);
canvasContext.lineTo(dBToXPixel(0), height);
canvasContext.stroke();
// Draw threshold input line
canvasContext.strokeStyle = thresholdColor;
canvasContext.beginPath();
canvasContext.moveTo(dBToXPixel(dbThreshold), 0);
canvasContext.lineTo(dBToXPixel(dbThreshold), height);
canvasContext.stroke();
// Draw knee input line
canvasContext.strokeStyle = thresholdColor;
canvasContext.beginPath();
canvasContext.moveTo(dBToXPixel(m_kneeThresholdDb), 0);
canvasContext.lineTo(dBToXPixel(m_kneeThresholdDb), height);
canvasContext.stroke();
canvasContext.strokeStyle = curveColor;
canvasContext.lineWidth = 3;
canvasContext.beginPath();
canvasContext.moveTo(0, 0);
var pixelsPerDb = (0.5 * height) / 40.0;
var noctaves = 8;
for (var x = 0; x < width; ++x) {
var inputDb = xpixelToDb(x);
var inputLinear = decibelsToLinear(inputDb);
// var outputLinear = shape(inputLinear);
var outputLinear = saturate(inputLinear, k);
var outputDb = linearToDecibels(outputLinear);
// Add makeup gain.
outputDb += makeupGain;
var y = dBToYPixel(outputDb);
canvasContext.lineTo(x, y);
// info.innerHTML += info.innerHTML + "<br>" + x + ", " + y;
}
canvasContext.stroke();
}
function thresholdHandler(event, ui) {
threshold = parseFloat(ui.value);
drawCurve();
var info = document.getElementById("threshold-value");
info.innerHTML = "threshold = " + threshold + " dB";
}
function kneeHandler(event, ui) {
knee = parseFloat(ui.value);
drawCurve();
var info = document.getElementById("knee-value");
info.innerHTML = "knee = " + knee + " dB";
}
function ratioHandler(event, ui) {
ratio = parseFloat(ui.value);
drawCurve();
var info = document.getElementById("ratio-value");
info.innerHTML = "ratio = " + ratio;
}
function makeupGainHandler(event, ui) {
makeupGain = parseFloat(ui.value);
drawCurve();
var info = document.getElementById("makeupGain-value");
info.innerHTML = "makeupGain = " + makeupGain + " dB";
}
function init() {
canvas = document.getElementById('canvasID');
canvasContext = canvas.getContext('2d');
canvasWidth = parseFloat(window.getComputedStyle(canvas, null).width);
canvasHeight = parseFloat(window.getComputedStyle(canvas, null).height);
addSlider("threshold");
addSlider("knee");
addSlider("ratio");
addSlider("makeupGain");
configureSlider("threshold", threshold, -36.0, 0, thresholdHandler);
configureSlider("knee", knee, 0.0, 40.0, kneeHandler);
configureSlider("ratio", ratio, 1, 50, ratioHandler);
configureSlider("makeupGain", makeupGain, 0, 20, makeupGainHandler);
drawCurve();
}
</script>
</head>
<body>
<h1>Static Compression Curve</h1>
<div id="info">
</div>
<canvas id="canvasID" width="1024" height="768" style="border: 2px inset blue;">
</canvas>
<br><br>
<!-- Sliders and other controls will be added here -->
<div id="controls"> </div>
<div id="info"> </div>
<div id="kits">
</div>
</body>
</html>