This repository has been archived by the owner on Dec 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkandinsky.js
168 lines (129 loc) · 4.3 KB
/
kandinsky.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
let skew = "blue";
let analysisInterval;
let setupComplete;
let audioContext;
let analyser;
let audioElement;
let audioSource;
function setupAnalysis() {
audioContext = new (window.AudioContext || window.webkitAudioContext)();
analyser = audioContext.createAnalyser();
analyser.fftSize = 256;
audioElement = document.getElementById('audio-element');
audioSource = audioContext.createMediaElementSource(audioElement);
audioSource.connect(analyser);
audioSource.connect(audioContext.destination);
setupComplete = true;
}
function analyse() {
if (!setupComplete) {
setupAnalysis();
}
analysisInterval = setInterval(() => {
const frequencyData = new Uint8Array(analyser.frequencyBinCount);
function getAverageValue(startIndex, endIndex) {
analyser.getByteFrequencyData(frequencyData);
let sum = 0;
for (let i = startIndex; i <= endIndex; i++) {
sum += frequencyData[i];
}
return sum / (endIndex - startIndex + 1);
}
var lowFrequencyVolume = getAverageValue(0, 30);
var midFrequencyVolume = getAverageValue(31, 120);
var highFrequencyVolume = getAverageValue(121, 255);
let rms = 0;
for (let i = 0; i < frequencyData.length; i++) {
rms += frequencyData[i] * frequencyData[i];
}
rms = Math.sqrt(rms / frequencyData.length);
const generalVolume = rms;
const alpha = 0.1 + (generalVolume / 255) * 0.9;
const mapTo255 = (value) => (value / 255) * 255;
let lowVolume = mapTo255(lowFrequencyVolume);
let midVolume = mapTo255(midFrequencyVolume);
let highVolume = mapTo255(highFrequencyVolume);
if (isNaN(lowVolume)) {
lowVolume = 0;
}
if (isNaN(midVolume)) {
midVolume = 0;
}
if (isNaN(highVolume)) {
highVolume = 0;
}
lowVolume = Math.min(255, lowVolume);
midVolume = Math.min(255, midVolume);
highVolume = Math.min(255, highVolume);
if (lowVolume == 0 && midVolume == 0 && highVolume == 0) {
lowVolume = 0;
midVolume = 89;
highVolume = 255;
}
var rgba;
if (skew == "blue") {
rgba = `rgba(${highVolume}, ${midVolume}, ${lowVolume}, ${alpha})`;
}
else if (skew == "green") {
rgba = `rgba(${midVolume}, ${lowVolume}, ${highVolume}, ${alpha})`;
}
else if (skew == "red") {
rgba = `rgba(${lowVolume}, ${midVolume}, ${highVolume}, ${alpha})`;
}
document.getElementById("navbar").style.backgroundColor = rgba;
document.getElementById("skew-controls").style.backgroundColor = rgba;
document.getElementById("skew-button-1").style.backgroundColor = rgba;
document.getElementById("skew-button-2").style.backgroundColor = rgba;
document.getElementById("skew-button-3").style.backgroundColor = rgba;
document.getElementById("upload-button-1").style.backgroundColor = rgba;
document.getElementById("upload-button-2").style.backgroundColor = rgba;
document.getElementById("output").innerText = rgbaToHex(rgba);
}, 0);
}
function stopAnalysis() {
clearInterval(analysisInterval);
}
function rgbaToHex(rgba) {
const rgbaRegex = /^rgba\((\d+\.?\d*),\s*(\d+\.?\d*),\s*(\d+\.?\d*),\s*([\d.]+)\)$/;
if (!rgbaRegex.test(rgba)) {
return null;
}
const [, r, g, b, a] = rgba.match(rgbaRegex);
const toHex = (value) => {
const intValue = Math.round(parseFloat(value));
const hex = intValue.toString(16);
return hex.length === 1 ? '0' + hex : hex;
};
const hexR = toHex(r);
const hexG = toHex(g);
const hexB = toHex(b);
const alpha = Math.round(parseFloat(a) * 255);
const hexA = toHex(alpha);
const hexColor = `#${hexR}${hexG}${hexB}${hexA}`;
return hexColor;
}
function changeSkew(newSkew) {
skew = newSkew;
}
function uploadFile() {
let input = document.createElement('input');
input.type = 'file';
input.onchange = () => {
let file = input.files[0];
if (file) {
let reader = new FileReader();
reader.onload = function (e) {
let fileURL = e.target.result;
document.getElementById("audio-element").src = fileURL;
document.getElementById("audio-element").play();
};
reader.readAsDataURL(file);
}
};
input.click();
}
function uploadLink() {
var url = prompt("URL:");
document.getElementById("audio-element").src = url;
document.getElementById("audio-element").play();
}