forked from hyperbeam/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
175 lines (159 loc) · 4.95 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<title>Hyperbeam AudioContext example</title>
<style>
body {
font-family: sans-serif;
}
/* #cloudComputerDiv {
height: 720px;
width: 1280px;
} */
#cloudComputerDiv {
position: fixed;
inset: 0;
top: 50px;
}
.toolbar {
display: flex;
align-items: center;
justify-content: center;
}
.toolbar > .group {
display: flex;
align-items: center;
justify-content: center;
margin: 0 10px;
}
</style>
</head>
<body>
<div>
<div class="toolbar">
<div class="group">
Volume:
<input
type="range"
id="volumeSlider"
min="0"
max="2"
value="1"
step="0.1"
/>
</div>
<div class="group">
<input type="checkbox" id="reverbToggle" />
<label for="reverbToggle">Reverb</label>
</div>
<div class="group">
<input type="checkbox" id="noiseToggle" />
<label for="noiseToggle">Noise</label>
</div>
</div>
<div id="cloudComputerDiv"></div>
</div>
<script type="module">
import Hyperbeam from "https://unpkg.com/@hyperbeam/web@latest/dist/index.js";
const audioCtx = new (window.AudioContext || window.webkitAudioContext)();
let source;
const gainNode = audioCtx.createGain();
const noiseNode = brownNoise(audioCtx);
const reverbNode = reverb(audioCtx, 4, 4);
async function main() {
const resp = await fetch("/computer");
const data = await resp.json();
const hb = await Hyperbeam(cloudComputerDiv, data.embed_url, {
audioTrackCb: tryAudio,
});
let source;
volumeSlider.addEventListener("input", (e) => {
gainNode.gain.value = e.target.value;
});
reverbToggle.addEventListener("change", (e) => {
if (e.target.checked) {
enableReverb();
} else {
disableReverb();
}
});
noiseToggle.addEventListener("change", (e) => {
if (e.target.checked) {
enableNoise();
} else {
disableNoise();
}
});
window.addEventListener("mouseup", async () => {
// AudioContext will start in a suspended state if no user gestures
// have been received on the page: need to check if suspended and resume
if (audioCtx.state === "suspended") {
try {
await audioCtx.resume();
console.log("audioCtx state:", audioCtx.state);
} catch (e) {
console.error(e);
}
}
});
}
main();
function tryAudio(track) {
// audioCtx.createMediaStreamTrackSource(track) is only supported by Firefox
const stream = new MediaStream([track]);
source = audioCtx.createMediaStreamSource(stream);
source.connect(gainNode);
gainNode.connect(audioCtx.destination);
console.log("connected audio track:", track);
}
function enableReverb() {
source.connect(reverbNode);
reverbNode.connect(gainNode);
}
function disableReverb() {
reverbNode.disconnect();
}
function enableNoise() {
noiseNode.connect(gainNode);
}
function disableNoise() {
noiseNode.disconnect();
}
// Inspired by https://github.com/web-audio-components/simple-reverb/blob/master/index.js
function reverb(ctx, seconds, decay, reverse = false) {
const rate = ctx.sampleRate;
const length = rate * seconds;
const impulse = ctx.createBuffer(2, length, rate);
const impulseL = impulse.getChannelData(0);
const impulseR = impulse.getChannelData(1);
for (let i = 0; i < length; i++) {
const n = reverse ? length - i : i;
impulseL[i] =
(Math.random() * 2 - 1) * Math.pow(1 - n / length, decay);
impulseR[i] =
(Math.random() * 2 - 1) * Math.pow(1 - n / length, decay);
}
const reverbNode = ctx.createConvolver();
reverbNode.buffer = impulse;
return reverbNode;
}
// https://noisehack.com/generate-noise-web-audio-api/
// Noise is used for debugging
function brownNoise(ctx) {
const bufferSize = 4096;
let lastOut = 0.0;
const node = ctx.createScriptProcessor(bufferSize, 1, 1);
node.onaudioprocess = function (e) {
let output = e.outputBuffer.getChannelData(0);
for (let i = 0; i < bufferSize; i++) {
const white = Math.random() * 2 - 1;
output[i] = (lastOut + 0.02 * white) / 1.02;
lastOut = output[i];
output[i] *= 3.5; // (roughly) compensate for gain
}
};
return node;
}
</script>
</body>
</html>