This repository has been archived by the owner on Jul 29, 2024. It is now read-only.
forked from Vap0r1ze/audioviz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
93 lines (84 loc) · 2.86 KB
/
index.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const { Plugin } = require('powercord/entities')
module.exports = class AudioViz extends Plugin {
startPlugin() {
setTimeout(() => {
this.intervals = []
this.startVisualizer()
this.loadStylesheet('style.scss')
}, 0)
}
reload() {
this.stopVisualizer()
this.startVisualizer()
}
pluginWillUnload() {
this.stopVisualizer()
}
stopVisualizer() {
clearInterval(this.interval)
cancelAnimationFrame(this.frame)
const viz = document.getElementById('vp-audioviz-visualizer');
viz.parentNode.removeChild(viz);
}
startVisualizer() {
const { desktopCapturer } = require('electron')
desktopCapturer.getSources({ types: ['window', 'screen'] }).then(async () => {
const stream = await navigator.mediaDevices.getUserMedia({
audio: {
mandatory: {
chromeMediaSource: 'desktop'
}
},
video: {
mandatory: {
chromeMediaSource: 'desktop'
}
}
})
const audioCtx = new AudioContext()
const audio = audioCtx.createMediaStreamSource(stream)
const easeInOutCubic = t => t < .5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1
const barCount = 20
const analyser = audioCtx.createAnalyser()
audio.connect(analyser)
analyser.fftSize = 1024
let accountContainer
let visualizer = document.createElement('div')
visualizer.classList.add('vp-audioviz-visualizer')
visualizer.id = 'vp-audioviz-visualizer'
for (let i = 0; i < barCount; i++) {
let bar = document.createElement('div')
bar.classList.add('vp-audioviz-bar')
visualizer.appendChild(bar)
}
const findElement = setInterval(() => {
if (accountContainer) {
visualizer = document.querySelector('.vp-audioviz-visualizer')
} else {
accountContainer = document.querySelector('.panels-j1Uci_ > .container-3baos1:last-child')
if (accountContainer) {
accountContainer.prepend(visualizer)
}
}
}, 1000)
const func = () => {
if (!visualizer) return
const bufferLength = analyser.frequencyBinCount
const dataArray = new Uint8Array(bufferLength)
analyser.getByteFrequencyData(dataArray)
for (let i = 0; i < barCount; i++) {
const y = dataArray[i * 2]
const height = easeInOutCubic(Math.min(1, y / 255)) * 90
const bar = visualizer.children[i]
bar.style.height = `${height}%`;
}
requestAnimationFrame(func)
}
const style = requestAnimationFrame(func)
this.interval = findElement;
this.frame = style
}).catch(error => {
console.error('An error occurred getting media sources', error)
})
}
}