-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
158 lines (128 loc) · 5.13 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Audio Spectrum Analyzer To TXT</title>
<style>
body {
background-color: black;
color: white;
}
canvas {
width: 100%;
height: 200px;
background-color: black;
}
</style>
</head>
<body>
<input type="file" id="audioFileInput" accept="audio/*" />
<canvas id="leftAnalyzer"></canvas>
<canvas id="rightAnalyzer"></canvas>
<p id="renderMessage">Rendering audio, please wait until the song has finished playing...</p>
<button id="exportButton" disabled>Export Sound Data</button>
<script>
let capturedData = [];
let isLeftChannel = true;
function convertToText(soundData) {
let text = '';
for (let i = 0; i < soundData.length; i++) {
text += soundData[i] + '\n';
}
return text;
}
document.getElementById('audioFileInput').addEventListener('change', function(event) {
const file = event.target.files[0];
const reader = new FileReader();
reader.onload = function(e) {
const audioContext = new (window.AudioContext || window.webkitAudioContext)();
const audioData = e.target.result;
audioContext.decodeAudioData(audioData, function(buffer) {
const channels = buffer.numberOfChannels;
const audioSources = [];
const analysers = [];
const bufferLength = buffer.length;
let audioCompletedCount = 0;
for (let i = 0; i < channels; i++) {
const audioSource = audioContext.createBufferSource();
const channelBuffer = audioContext.createBuffer(1, bufferLength, buffer.sampleRate);
channelBuffer.copyToChannel(buffer.getChannelData(i), 0);
audioSource.buffer = channelBuffer;
audioSources.push(audioSource);
const analyser = audioContext.createAnalyser();
analyser.fftSize = 64;
analysers.push(analyser);
audioSource.connect(analyser);
audioSource.onended = function() {
audioCompletedCount++;
if (audioCompletedCount === channels) {
audioSources.forEach(source => (source.paused = true));
document.getElementById('exportButton').disabled = false;
document.getElementById('renderMessage').style.display = 'none';
}
};
}
const canvasLeft = document.getElementById('leftAnalyzer');
const canvasCtxLeft = canvasLeft.getContext('2d');
const canvasRight = document.getElementById('rightAnalyzer');
const canvasCtxRight = canvasRight.getContext('2d');
function captureData() {
const dataArrayLeft = new Uint8Array(32);
const dataArrayRight = new Uint8Array(32);
analysers[0].getByteFrequencyData(dataArrayLeft);
capturedData.push(...dataArrayLeft);
if (channels > 1) {
analysers[1].getByteFrequencyData(dataArrayRight);
capturedData.push(...dataArrayRight);
}
}
function draw() {
canvasCtxLeft.clearRect(0, 0, canvasLeft.width, canvasLeft.height);
canvasCtxRight.clearRect(0, 0, canvasRight.width, canvasRight.height);
const dataArrayLeft = new Uint8Array(analysers[0].frequencyBinCount);
analysers[0].getByteFrequencyData(dataArrayLeft);
const barWidth = (canvasLeft.width / dataArrayLeft.length) * 0.95;
let barHeight;
let x = 0;
for (let j = 0; j < dataArrayLeft.length; j++) {
barHeight = dataArrayLeft[j];
canvasCtxLeft.fillStyle = 'rgb(' + (barHeight + 100) + ',50,50)';
canvasCtxLeft.fillRect(x, canvasLeft.height - barHeight / 2, barWidth, barHeight / 2);
x += barWidth + 1;
}
if (channels > 1) {
const dataArrayRight = new Uint8Array(analysers[1].frequencyBinCount);
analysers[1].getByteFrequencyData(dataArrayRight);
x = 0;
for (let j = 0; j < dataArrayRight.length; j++) {
barHeight = dataArrayRight[j];
canvasCtxRight.fillStyle = 'blue';
canvasCtxRight.fillRect(x, canvasRight.height - barHeight / 2, barWidth, barHeight / 2);
x += barWidth + 1;
}
}
if (!audioSources[0].paused) {
requestAnimationFrame(draw);
}
}
for (let i = 0; i < channels; i++) {
audioSources[i].start(0);
}
draw();
setInterval(captureData, 1000 / 30); // Adjust the interval for stereo
});
};
reader.readAsArrayBuffer(file);
});
document.getElementById('exportButton').addEventListener('click', function() {
const textData = convertToText(capturedData);
const blob = new Blob([textData], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = 'sound_data.txt';
link.click();
});
</script>
</body>
</html>