-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdebugger.html
97 lines (88 loc) · 2.23 KB
/
debugger.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
<!doctype html>
<html lang="en">
<!--
(c) Copyright 2018, Sean Connelly (@voidqk), http://sean.cm
MIT License
Project Home: https://github.com/voidqk/adsrnode
-->
<head>
<title>ADSRNode Debugger</title>
<script src="adsrnode.js"></script>
<style>
body {
background-color: #eef;
font-family: sans-serif;
}
</style>
</head>
<body>
<p>Move mouse to interact with interruption logic.</p>
<p>Click mouse to change mode.</p>
<canvas id="cnv" width="1600" height="800" style="width: 800px; height: 400px;"></canvas>
<script>
var cnv = document.getElementById('cnv');
var ctx = cnv.getContext('2d');
var debugMode = 0;
function redraw(offset){
var curve = (debugMode == 0 || debugMode == 2) ? 6 : 0;
var opts = {
base: -0.5,
attack: 0.4,
attackCurve: curve,
peak: 1,
hold: 0.3,
decay: 0.5,
decayCurve: curve,
sustain: 0.5,
release: 0.3,
releaseCurve: curve
};
var rate = 44100;
var sustime = 1; // seconds to sustain
var downtime = opts.attack + opts.hold + opts.decay + sustime;
var duration = downtime + opts.release + 0.5;
var actx = new OfflineAudioContext(1, Math.ceil(rate * duration), rate);
var env = ADSRNode(actx, opts);
env.connect(actx.destination);
env.start();
env.trigger(0);
if (debugMode == 0 || debugMode == 1){
if (offset * duration > downtime)
env.release(downtime);
env.trigger(offset * duration);
}
else
env.release(offset * duration);
actx.startRendering().then(function(buf){
ctx.fillStyle = '#fff';
ctx.fillRect(0, 0, cnv.width, cnv.height);
ctx.fillStyle = '#000';
ctx.font = '20px sans-serif';
ctx.fillText((['Two Curves', 'Two Linear', 'Single Curves', 'Single Linear'])[debugMode],
10, 30);
ctx.beginPath();
var data = buf.getChannelData(0);
for (var sx = 0; sx < cnv.width; sx++){
var x = sx / (cnv.width - 1);
var y = data[(x * rate * duration) | 0];
var sy = cnv.height * 0.5 - y * cnv.height * 0.4;
if (sx == 0)
ctx.moveTo(sx, sy);
else
ctx.lineTo(sx, sy);
}
ctx.strokeStyle = '#000';
ctx.stroke();
});
}
cnv.addEventListener('mousemove', function(e){
redraw(e.offsetX * 2 / cnv.width);
});
cnv.addEventListener('click', function(e){
debugMode = (debugMode + 1) % 4;
redraw(e.offsetX * 2 / cnv.width);
});
redraw(1);
</script>
</body>
</html>