-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo-eng.html
120 lines (110 loc) · 3.74 KB
/
demo-eng.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0"/>
<meta name="format-detection" content="telephone=no"/>
<title>8-bit noise demo</title>
</head>
<body>
<h1>8-bit noise demo</h1>
<button id="boom">Big Boom - Contra</button>
<button id="bullet">Bullet - Contra</button>
<script src="8bitNoise.js"></script>
<script>
var ctx = new (window.AudioContext || window.webkitAudioContext || window.mozAudioContext)();
var compressor = ctx.createDynamicsCompressor();
compressor.connect(ctx.destination);
(function () {
// [frequency, duration]
var boomNoise = [
[10, 0.16],
[14, 0.03],
[7, 0.03],
[12, 0.16],
[13, 0.80],
[10, 0.16],
[12, 0.10],
[13, 0.80],
[10, 0.16],
[13, 0.80]
];
var generate = function () {
var volumeNode = ctx.createGain();
volumeNode.gain.value = 1.0;
volumeNode.gain.linearRampToValueAtTime(1.0, ctx.currentTime);
volumeNode.gain.linearRampToValueAtTime(0.0, ctx.currentTime + 3.2);
volumeNode.connect(compressor);
var innerCompressor = ctx.createDynamicsCompressor();
innerCompressor.connect(volumeNode);
var i = 0;
var createOneNoise = function () {
if (i >= boomNoise.length) {
innerCompressor.disconnect(volumeNode);
volumeNode.disconnect(compressor);
innerCompressor = null;
volumeNode = null;
return;
}
var noiseNode = ctx.create8bitNoise(boomNoise[i][0]);
noiseNode.connect(innerCompressor);
noiseNode.start(0);
noiseNode.stop(ctx.currentTime + boomNoise[i][1]);
noiseNode.onended = createOneNoise;
i ++;
};
createOneNoise();
};
document.getElementById('boom').onclick = function () {
generate();
};
})();
(function () {
// [frequency, duration, volume]
var bulletNoise = [
[14, 0.016, 1],
[10, 0.017, 1],
[10, 0.017, 1],
[14, 0.033, 0.2],
[0, 0.017],
[14, 0.016, 1],
[10, 0.017, 1],
[10, 0.017, 1],
[14, 0.033, 0.2],
[0, 0.017],
[13, 0.05, 0.07],
[10, 0.10, 0.03],
[13, 0.05, 0.03],
[0, 0.05],
[13, 0.05, 0.03],
[12, 0.05, 0.015],
[13, 0.05, 0.015]
];
var generate = function () {
var i = 0;
var createOneNoise = function () {
if (i >= bulletNoise.length) {return;}
if (bulletNoise[i][0] === 0) {
setTimeout(createOneNoise, bulletNoise[i][1] * 1000);
i ++;
return;
}
var volumeNode = ctx.createGain();
volumeNode.connect(compressor);
volumeNode.gain.value = bulletNoise[i][2];
var noiseNode = ctx.create8bitNoise(bulletNoise[i][0]);
noiseNode.connect(volumeNode);
noiseNode.start(0);
noiseNode.stop(ctx.currentTime + bulletNoise[i][1]);
noiseNode.onended = createOneNoise;
i ++;
};
createOneNoise();
};
document.getElementById('bullet').onclick = function () {
generate();
};
})();
</script>
</body>
</html>