-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
68 lines (54 loc) · 1.71 KB
/
index.ts
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
export default class Illustrator {
audioSrc: MediaElementAudioSourceNode
analyser: AnalyserNode
loopId: number
isWaveform: boolean
constructor(option?: { waveform: boolean }) {
if (option === undefined || option.waveform !== true) {
this.isWaveform = false
} else {
this.isWaveform = true
}
}
connect = (audio: HTMLAudioElement | HTMLVideoElement) => {
const ctx = new AudioContext()
this.audioSrc = ctx.createMediaElementSource(audio)
this.analyser = ctx.createAnalyser()
this.audioSrc.connect(this.analyser)
this.analyser.connect(ctx.destination)
this.analyser.fftSize = this.isWaveform ? 2048 : 256
}
disconnect = () => {
if (this.audioSrc) this.audioSrc.disconnect()
if (this.analyser) this.analyser.disconnect()
}
getData = (items?: number) => {
const bufferLength = this.analyser.frequencyBinCount
const dataArray = new Uint8Array(bufferLength)
const getAverage = (chunkSize: number) => {
const results: Uint8Array[] = new Array(
Math.ceil(dataArray.length / chunkSize)
)
.fill(0)
.map((_, i) =>
dataArray.slice(chunkSize * i, chunkSize * i + chunkSize)
)
return results.map(
result => result.reduce((acc, cur) => acc + cur, 0) / result.length
)
}
if (this.isWaveform) {
this.analyser.getByteTimeDomainData(dataArray)
} else {
this.analyser.getByteFrequencyData(dataArray)
}
if (items === undefined) return dataArray
return getAverage(bufferLength / items)
}
startLoop = (callback: FrameRequestCallback) => {
this.loopId = requestAnimationFrame(callback)
}
stopLoop = () => {
cancelAnimationFrame(this.loopId)
}
}