-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.js
More file actions
48 lines (47 loc) · 1.57 KB
/
input.js
File metadata and controls
48 lines (47 loc) · 1.57 KB
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
class Input {
init(callback) {
this.callback = callback;
navigator.mediaDevices.getUserMedia({ audio: true, video: false })
.then(this.handleSuccess.bind(this));
}
handleSuccess(stream) {
const context = new AudioContext();
this.analyser = context.createAnalyser();
this.analyser.fftSize = 256;
this.analyser.minDecibels = -128;
this.analyser.maxDecibels = 0;
this.fftArray = new Uint8Array(this.analyser.frequencyBinCount);
this.fftArrayFloat = new Float32Array(this.analyser.frequencyBinCount);
this.frequencyBinCount = this.analyser.frequencyBinCount;
this.timeBinCount = this.frequencyBinCount;
this.timeArrayFloat = new Float32Array(this.timeBinCount);
this.analyserInit = true;
const source = context.createMediaStreamSource(stream);
source.connect(this.analyser);
this.callback.apply();
}
getFft() {
if (this.analyserInit) {
this.analyser.getByteFrequencyData(this.fftArray);
}
return this.fftArray;
}
getFftFloat() {
if (this.analyserInit) {
this.analyser.getFloatFrequencyData(this.fftArrayFloat);
}
return this.fftArrayFloat;
}
getTimeFloat() {
if (this.analyserInit) {
this.analyser.getFloatTimeDomainData(this.timeArrayFloat);
}
return this.timeArrayFloat;
}
getFrequencyBinCount() {
return this.frequencyBinCount;
}
getTimeBinCount() {
return this.timeBinCount;
}
}