-
Notifications
You must be signed in to change notification settings - Fork 0
/
v1.txt
117 lines (85 loc) · 3.14 KB
/
v1.txt
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Audio Spectrum Analyzer</title>
<style>
canvas {
width: 100%;
height: 200px;
background-color: #f2f2f2;
}
</style>
</head>
<body>
<input type="file" id="audioFileInput" accept="audio/*" />
<canvas id="spectrumAnalyzer"></canvas>
<button id="exportButton" disabled>Export Sound Data</button>
<script>
let capturedData = [];
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 source = audioContext.createBufferSource();
source.buffer = buffer;
const analyser = audioContext.createAnalyser();
analyser.fftSize = 64;
source.connect(analyser);
analyser.connect(audioContext.destination);
const canvas = document.getElementById('spectrumAnalyzer');
const canvasCtx = canvas.getContext('2d');
const bufferLength = 32;
const dataArray = new Uint8Array(bufferLength);
function captureData() {
analyser.getByteFrequencyData(dataArray);
capturedData.push(...dataArray);
}
function draw() {
canvasCtx.clearRect(0, 0, canvas.width, canvas.height);
analyser.getByteFrequencyData(dataArray);
const barWidth = (canvas.width / bufferLength) * 2.5;
let barHeight;
let x = 0;
for (let i = 0; i < bufferLength; i++) {
barHeight = dataArray[i];
canvasCtx.fillStyle = 'rgb(' + (barHeight + 100) + ',50,50)';
canvasCtx.fillRect(x, canvas.height - barHeight / 2, barWidth, barHeight / 2);
x += barWidth + 1;
}
setTimeout(function() {
requestAnimationFrame(draw);
}, 1000 / 30); // 30 FPS
}
source.start(0);
setInterval(captureData, 1000 / 30); // 30 FPS
draw();
setTimeout(function() {
document.getElementById('exportButton').disabled = false;
}, bufferLength / audioContext.sampleRate * 1000);
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();
});
});
};
reader.readAsArrayBuffer(file);
});
</script>
</body>
</html>