-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.js
187 lines (164 loc) · 4.96 KB
/
input.js
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
import { parseText } from "./utils.js";
const canvas = document.getElementById("screen");
const ctx = canvas.getContext("2d");
let analyser;
const ac = new AudioContext();
let acResumed = null;
let currentVolume = 0;
let currentPitch = 0;
let smoothing = 4;
let volumeThreshold = 50;
let MIN_VOL_THRESHOLD = 100;
let PITCH_THRESHOLD = 450;
let above = false;
init();
async function init() {
analyser = ac.createAnalyser();
analyser.fftSize = 4096;
analyser.minDecibels = -100;
analyser.maxDecibels = -10;
analyser.smoothingTimeConstant = 0.05;
let source = await navigator.mediaDevices.getUserMedia({
audio: {
latency: 0.02,
echoCancellation: false,
mozNoiseSuppression: false,
noiseSuppression: false,
mozAutoGainControl: false,
autoGainControl: false,
sampleRate: 22500,
},
});
let lowPass = new BiquadFilterNode(ac, { frequency: 6000, type: "lowpass" });
let highPass = new BiquadFilterNode(ac, { frequency: 10, type: "highpass" });
let stream = ac.createMediaStreamSource(source);
stream.connect(lowPass);
lowPass.connect(highPass);
highPass.connect(analyser);
}
function smooth(current, next) {
return current + (next - current) / smoothing;
}
function analyseAudioTick() {
const bufferLength = analyser.frequencyBinCount;
const buffer = new Uint8Array(bufferLength);
analyser.getByteFrequencyData(buffer);
let maxFrequency = ac.sampleRate / 2;
let binWidth = maxFrequency / bufferLength;
let maxVol = 0;
let pitch = 0;
for (let i = 1; i < bufferLength - 1; i++) {
let slidingWindow =
(buffer[i - 2] +
buffer[i - 1] +
buffer[i] +
buffer[i + 1] +
buffer[i + 2]) /
5;
if (slidingWindow > volumeThreshold) {
if (slidingWindow > maxVol) {
maxVol = slidingWindow;
pitch = i * binWidth;
}
}
}
currentPitch = smooth(currentPitch, pitch);
currentVolume = smooth(currentVolume, maxVol);
if (currentVolume > MIN_VOL_THRESHOLD) {
if (!above)
window.dispatchEvent(
new CustomEvent("noiseStart", {
detail: { highPitch: currentPitch >= PITCH_THRESHOLD },
})
);
above = true;
} else {
if (currentVolume <= MIN_VOL_THRESHOLD) {
if (above) window.dispatchEvent(new Event("noiseStop"));
above = false;
}
}
if (currentVolume > 1) {
acResumed = true;
}
}
const handleX = canvas.width / 2 + 15;
const handleY = 100;
const handleHeight = 20;
const handleWidth = canvas.width / 2 - 30;
function drawAudioControl() {
ctx.textAlign = "start";
ctx.font = "20px slkscr";
ctx.fillStyle = "White";
ctx.fillText("Volume threshold", handleX, handleY - 40);
ctx.fillText("Click to set:", handleX, handleY - 20);
ctx.fillStyle = "grey";
ctx.fillRect(handleX, handleY, handleWidth, handleHeight);
ctx.fillStyle = "white";
ctx.fillRect(handleX, handleY, currentVolume, handleHeight);
ctx.fillStyle = "red";
ctx.fillRect(
handleX + MIN_VOL_THRESHOLD - 2,
handleY - 5,
4,
handleHeight + 10
);
const instr = acResumed
? getPostAudioContextInstruction()
: getWaitingAudioContextInstruction();
ctx.font = `${instr.fontSize}px slkscr`;
ctx.fillStyle = "red";
ctx.textAlign = "center";
instr.lines.forEach(({ y, text }) => {
ctx.fillText(text, (3 * canvas.width) / 4, y);
});
}
function audioCanvasTouchHandler({ x, y }) {
if (
x >= handleX &&
x <= handleX + handleWidth &&
y >= handleY &&
y <= handleY + handleHeight
) {
MIN_VOL_THRESHOLD = x - handleX;
}
}
function startAudioContext() {
if (acResumed === null) {
acResumed = false;
console.log("resuming attempts");
ac.resume().then((_) => {
acResumed = true;
});
}
if (acResumed) {
console.log("removing resumers");
document.removeEventListener("mousedown", startAudioContext);
document.removeEventListener("touchstart", startAudioContext);
}
}
document.addEventListener("mousedown", startAudioContext);
document.addEventListener("touchstart", startAudioContext);
function getWaitingAudioContextInstruction() {
const lines = [
"Waiting for audio startup, this can take a while. An external mic can be quicker/better",
"",
"It might help to close other apps/sites using your mic, including other instances of this site.",
"",
"After a couple of minutes if nothing happens reload the page"
];
const targetWidth = canvas.width / 2 - 20;
return parseText(lines, 20, targetWidth, 140, ctx);
}
function getPostAudioContextInstruction() {
const lines = [
"If your voice (white bar) crosses the threshold (red line), lasers will fire.",
"",
"External mics work better than built-in ones",
"",
"If audio isn't being picked up, close anything that could be using the mic, including other instances of this site. Refresh",
];
const targetWidth = canvas.width / 2 - 20;
return parseText(lines, 20, targetWidth, 140, ctx);
}
export { analyseAudioTick, drawAudioControl, audioCanvasTouchHandler };