-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
145 lines (123 loc) · 3.42 KB
/
Copy pathutil.js
File metadata and controls
145 lines (123 loc) · 3.42 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
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
// Returns freq for a given index
function getFrequencyAtIndex(index) {
return index * audioCtx.sampleRate / (analyser.frequencyBinCount * 2);
}
// Returns array index for a given freq
function getIndexForFrequency(freq, analyser) {
const nyquist = analyser.context.sampleRate / 2.0;
let index = Math.round(freq / nyquist * analyser.frequencyBinCount);
if (index < 0) index = 0;
if (index >= analyser.frequencyBinCount) index = analyser.frequencyBinCount - 1;
return index;
}
function getMinMax(data, start = 0, end = data.length) {
if (end > data.length) throw new Error('end is past size of array');
if (start < 0) throw new Error('start should be at least 0');
if (end <= start) throw new Error('end should be greater than start');
let max = Number.MIN_SAFE_INTEGER;
let min = Number.MAX_SAFE_INTEGER;
for (var i = start; i < end; i++) {
max = Math.max(data[i], max);
min = Math.min(data[i], min);
}
if (!Number.isFinite(max)) max = 0;
if (!Number.isFinite(min)) min = 0;
return { max: max, min: min };
}
function getAvg(data, start = 0, end = data.length) {
if (end > data.length) throw new Error('end is past size of array');
if (start < 0) throw new Error('start should be at least 0');
if (end <= start) throw new Error('end should be greater than start');
let total = 0;
for (var i = start; i < end; i++) {
total += data[i];
}
return total / end - start;
}
class Tracker {
constructor(id = null) {
this.reset();
this.id = id;
}
add(sample) {
if (!Number.isFinite(sample)) return;
if (Number.isNaN(sample)) return;
this._samples++;
this._total += sample;
this._min = Math.min(sample, this._min);
this._max = Math.max(sample, this._max);
}
avg() {
return this._total / this._samples;
}
min() {
return this._min;
}
max() {
return this._max;
}
reset(newId = null) {
if (newId !== null) this.id = newId;
this._min = Number.MAX_SAFE_INTEGER;
this._max = Number.MIN_SAFE_INTEGER;
this._total = 0;
this._samples = 0;
}
}
class SlidingWindow {
constructor(max = 100) {
this.store = [];
this.max = max;
}
clear() {
this.store = [];
}
add(sample) {
this.store.push(sample);
if (this.store.length >= this.max) {
this.store = this.store.slice(1);
}
}
avg() {
let total = 0;
for (var i = 0; i < this.store.length; i++) {
total++;
}
return total / this.store.length;
}
}
class IntervalMeter {
constructor(max = 100, pulseLengthMs = 0) {
this.max = max;
this.pulseLengthMs = pulseLengthMs;
this.store = [];
this.next = 0;
}
pulse() {
let now = performance.now();
if (now < this.next) return false; // Pulse happened too soon, ignoring
this.next = now + this.pulseLengthMs;
this.store.push(now);
if (this.store.length >= this.max) {
this.store = this.store.slice(1);
}
return true;
}
// Return average interval between all samples in milliseconds
calculate() {
if (this.store.length == 0) return 0;
let intervals = [];
let total = 0;
for (var i = 1; i < this.store.length; i++) {
let interval = this.store[i] - this.store[i - 1];
intervals.push(interval);
total += interval;
}
if (this.store[0] < performance.now() - 5000) {
// Haven't received any pulses for a while, reset
this.store = [];
return 0;
}
return total / (this.store.length - 1);
}
}