forked from Okazari/Rythm.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnalyser.js
More file actions
60 lines (52 loc) · 1.34 KB
/
Analyser.js
File metadata and controls
60 lines (52 loc) · 1.34 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
class Analyser {
constructor () {
this.startingScale = 0
this.pulseRatio = 1
this.maxValueHistory = 100
this.hzHistory = []
}
initialise = (analyser) => {
this.analyser = analyser
this.analyser.fftSize = 2048
}
reset = () => {
this.hzHistory = []
this.frequences = new Uint8Array(this.analyser.frequencyBinCount)
}
analyse = () => {
this.analyser.getByteFrequencyData(this.frequences)
for(let i=0; i < this.frequences.length; i++){
if(!this.hzHistory[i]){
this.hzHistory[i] = []
}
if(this.hzHistory[i].length > this.maxValueHistory){
this.hzHistory[i].shift()
}
this.hzHistory[i].push(this.frequences[i])
}
}
getRangeAverageRatio = (startingValue, nbValue) => {
let total = 0
for(let i=startingValue; i<nbValue+startingValue; i++){
total += this.getFrequenceRatio(i)
}
return total/nbValue
}
getFrequenceRatio = (index) => {
let min = 255
let max = 0
this.hzHistory[index].forEach(value => {
if(value < min){
min = value
}
if(value > max){
max = value
}
})
const scale = max - min
const actualValue = this.frequences[index] -min
const percentage = (actualValue/scale)
return this.startingScale + (this.pulseRatio * percentage)
}
}
export default new Analyser