This repository was archived by the owner on Nov 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathaudioVisualizer.js
70 lines (60 loc) · 1.91 KB
/
audioVisualizer.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
import { h, createRef, Component } from 'preact';
export default class AudioVisualizer extends Component {
constructor(props) {
super(props)
const audioCtx = new (window.AudioContext || window.webkitAudioContext)()
this.analyser = audioCtx.createAnalyser()
const source = audioCtx.createMediaStreamSource(props.stream)
source.connect(this.analyser)
this.analyser.fftSize = 2048
this.bufferLength = this.analyser.frequencyBinCount
this.dataArray = new Uint8Array(this.bufferLength)
this.draw = this.draw.bind(this)
}
componentDidMount () {
this.width = this.theCanvas.current.width
this.height = this.theCanvas.current.height
this.canvasCtx = this.theCanvas.current.getContext('2d')
this.canvasCtx.clearRect(0, 0, this.width, this.height)
this.draw()
}
componentWillUnmount () {
this.paused = true
}
play () {
this.paused = false
this.draw()
}
pause () {
this.paused = true
}
theCanvas = createRef()
draw () {
this.analyser.getByteTimeDomainData(this.dataArray)
this.canvasCtx.fillStyle = 'rgb(0, 0, 0)'
this.canvasCtx.fillRect(0, 0, this.width, this.height)
this.canvasCtx.lineWidth = 2
this.canvasCtx.strokeStyle = 'rgb(200, 200, 200)'
this.canvasCtx.beginPath()
const sliceWidth = this.width * 1.0 / this.bufferLength
let x = 0
for (let i = 0; i < this.bufferLength; i++) {
const v = this.dataArray[i] / 128.0
const y = v * this.height / 2
if (i === 0) this.canvasCtx.moveTo(x, y)
else this.canvasCtx.lineTo(x, y)
x += sliceWidth
}
this.canvasCtx.lineTo(this.width, this.height / 2)
this.canvasCtx.stroke()
if (this.paused) return
requestAnimationFrame(this.draw)
}
render(props) {
return <canvas class={`audioVisualizer ${props.class}`}
onclick={props.onclick}
height={props.height}
width={props.width}
ref={this.theCanvas} />
}
}