-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
204 lines (181 loc) · 9.64 KB
/
main.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import { latencyMeasurer } from "./latencyMeasurer.js";
class app {
static initialize(e) {
app.currentSection = app.currentAnchor = app.audioContext = app.audioNode = null;
let navItems = document.getElementById('nav').getElementsByTagName('a');
for (let a of navItems) a.addEventListener('click', function(e) {
e.preventDefault();
app.navClick(e.target);
});
app.navClick(navItems[0]);
app.content = document.getElementById('content');
app.content.innerHTML = '<h2>Initializing...</h2>';
let audioWorklet = (typeof AudioWorkletNode === 'function') ? 1 : 0;
app.data = {
device: '?',
os: '?',
browser: '?',
browserVersion: '?',
buffersize: audioWorklet ? 128 : 512,
samplerate: '?',
audioWorklet: audioWorklet
};
app.runningon = '?';
let xhr = new XMLHttpRequest();
xhr.timeout = 10000;
xhr.onload = function() {
if (xhr.status == 200) {
app.getDeviceData(xhr.responseText);
app.displayStart();
}
}
xhr.onerror = function() {
app.displayStart();
}
xhr.open('POST', 'https://api.whatismybrowser.com/api/v2/user_agent_parse', true);
xhr.setRequestHeader('X-API-KEY', '537c5c214028e36667cb0b8f8f2cb2c8');
xhr.send(JSON.stringify({ 'user_agent': navigator.userAgent }));
}
static navClick(a) {
let href = a.href;
let target = href.substring(href.indexOf('#') + 1);
if (app.currentSection != null) app.currentSection.style.display = 'none';
if (app.currentAnchor != null) app.currentAnchor.className = '';
app.currentSection = document.getElementById(target);
app.currentAnchor = a;
app.currentSection.style.display = 'block';
app.currentAnchor.className = 'selected';
}
static send(latencyMs) {
let xhr = new XMLHttpRequest();
xhr.open('POST', 'https://superpowered.com/latencydata/web.php', true);
xhr.setRequestHeader('Content-type', 'application/json');
xhr.send(JSON.stringify(app.data));
}
static getDeviceData(json) {
json = JSON.parse(json);
if (json == null) return;
if (json.parse.hardware_type == 'mobile') {
app.data.device = json.parse.operating_platform_vendor_name;
if (json.parse.hardware_sub_type) app.data.device += ' ' + json.parse.hardware_sub_type;
if (json.parse.operating_platform_code) app.data.device += ' ' + json.parse.operating_platform_code;
} else app.data.device = json.parse.hardware_type;
app.data.os = json.parse.operating_system_name + ' ' + json.parse.operating_system_version_full.toString().replace(/,/g,'.');
app.data.browser = json.parse.software_name;
app.data.browserVersion = json.parse.software_version;
app.runningon = app.data.device + ', ' + app.data.os + ', ' + app.data.browser + ' ' + app.data.browserVersion + ', ' + (app.data.audioWorklet ? 'Audio Worklet' : 'ScriptProcessor');
}
static displayStart() {
if (app.audioContext != null) app.audioContext.close();
app.audioContext = app.audioNode = null;
document.getElementById('prepare').style.display = 'block';
document.getElementById('thisdevice').innerText = 'Currently running on: ' + app.runningon;
app.content.innerHTML = '';
app.startbutton = document.createElement('button');
app.startbutton.innerText = 'START';
app.startbutton.addEventListener('click', app.start);
app.content.appendChild(app.startbutton);
}
static onAudioInputPermissionDenied(error) {
app.displayResult('<h3>Error: ' + error + '</h3><p>Please check the microphone permission.</p>');
}
static onMessageFromAudioScope(message) {
if (message.latencyMs < 0) {
app.displayResult('<h3>The environment is too loud!</h3><p>Please try it again in a quieter environment.</p>');
} else if (message.state == 11) {
if (message.latency < 1) app.displayResult('<h3>The variance is too big.</h3><p>Please try it again in a quieter environment.</p>');
else {
app.data.ms = message.latency;
app.send();
app.displayResult('<h3>Result: ' + message.latency + 'ms</h3><p>10 ms or lower allows for the best real-time interactive experience. Below 50 ms provides acceptable results for simple audio use-cases and maximum 100 ms is acceptable for gaming.</p>');
}
} else {
let percentage = ((parseInt(message.state) - 1) / 10) * 100;
if (percentage < 1) percentage = 1; else if (percentage > 100) percentage = 100;
document.getElementById('progressBar').style.width = percentage + '%';
}
}
static onAudioSetupFinished() {
let audioInput = app.audioContext.createMediaStreamSource(app.inputStream);
audioInput.connect(app.audioNode);
app.audioNode.connect(app.audioContext.destination);
app.content.innerHTML = '<h3>Please wait...</h3><p id="progressBody"><span id="progressBar"></span></p>';
let restartLink = document.createElement('a');
restartLink.innerText = 'CANCEL';
restartLink.addEventListener('click', app.displayStart);
app.content.appendChild(restartLink);
}
static displayResult(html) {
if (app.audioContext != null) app.audioContext.close();
app.audioContext = app.audioNode = null;
let div = document.createElement('div');
div.id = 'result';
div.innerHTML = html;
let restartLink = document.createElement('a');
restartLink.innerText = 'TRY AGAIN';
restartLink.addEventListener('click', app.displayStart);
div.appendChild(restartLink);
app.content.innerHTML = '';
app.content.appendChild(div);
}
static onAudioPermissionGranted(inputStream) {
app.inputStream = inputStream;
let audioTracks = inputStream.getAudioTracks();
for (let audioTrack of audioTracks) {
audioTrack.applyConstraints({ autoGainControl: false, echoCancellation: false, noiseSuppression: false });
}
if (!app.data.audioWorklet) {
app.latencyMeasurer = new latencyMeasurer();
app.latencyMeasurer.toggle();
app.lastState = 0;
app.audioNode = app.audioContext.createScriptProcessor(app.data.buffersize, 2, 2);
app.audioNode.onaudioprocess = function(e) {
app.latencyMeasurer.processInput(e.inputBuffer.getChannelData(0), e.inputBuffer.getChannelData(1), app.data.samplerate, e.inputBuffer.length);
app.latencyMeasurer.processOutput(e.outputBuffer.getChannelData(0), e.outputBuffer.getChannelData(1));
if (app.lastState != app.latencyMeasurer.state) {
app.lastState = app.latencyMeasurer.state;
app.onMessageFromAudioScope({ state: app.lastState, latency: app.latencyMeasurer.latencyMs });
}
}
app.onAudioSetupFinished();
} else {
let processorPath = window.location.pathname.substring(0, window.location.pathname.lastIndexOf('/')) + '/processor.js';
app.audioContext.audioWorklet.addModule(processorPath).then(() => {
class CustomAudioNode extends AudioWorkletNode {
constructor(audioContext, moduleInstance, name) {
super(audioContext, name, {
'processorOptions': {
'samplerate': app.data.samplerate
},
'outputChannelCount': [2]
});
}
sendMessageToAudioScope(message) { this.port.postMessage(message); }
}
app.audioNode = new CustomAudioNode(app.audioContext, app, 'MyProcessor');
app.audioNode.port.onmessage = (event) => {
if (event.data == '___ready___') app.onAudioSetupFinished(); else app.onMessageFromAudioScope(event.data);
};
});
}
}
static start(e) {
document.getElementById('prepare').style.display = 'none';
app.content.innerHTML = '<h2>Initializing...</h2>';
let AudioContext = window.AudioContext || window.webkitAudioContext || false;
app.audioContext = new AudioContext({ latencyHint: 0 });
app.data.samplerate = app.audioContext.sampleRate;
let constraints = {
'echoCancellation': false,
'disableLocalEcho': false,
'autoGainControl': false,
'audio': { mandatory: { googAutoGainControl: false, googAutoGainControl2: false, googEchoCancellation: false, googNoiseSuppression: false, googHighpassFilter: false, googEchoCancellation2: false, googNoiseSuppression2: false, googDAEchoCancellation: false, googNoiseReduction: false } },
'video': false
};
navigator.getUserMediaMethod = navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia;
if (navigator.getUserMediaMethod) navigator.getUserMediaMethod(constraints, app.onAudioPermissionGranted, app.onAudioInputPermissionDenied);
else if (navigator.mediaDevices.getUserMedia) navigator.mediaDevices.getUserMedia(constraints).then(app.onAudioPermissionGranted).catch(app.onAudioInputPermissionDenied);
else app.onAudioInputPermissionDenied("Can't access getUserMedia.");
}
}
window.addEventListener('load', app.initialize);