-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
50 lines (42 loc) · 1.69 KB
/
Copy pathscript.js
File metadata and controls
50 lines (42 loc) · 1.69 KB
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
document.getElementById('convertButton').addEventListener('click', function() {
var fileInput = document.getElementById('fileInput');
var outputDiv = document.getElementById('output');
if (fileInput.files.length === 0) {
outputDiv.textContent = 'Please select an MP3 file.';
return;
}
var file = fileInput.files[0];
var reader = new FileReader();
reader.onload = function(event) {
var arrayBuffer = event.target.result;
// Decode the MP3 file using the Web Audio API
decodeMP3(arrayBuffer)
.then(binaryData => {
// Display binary data
outputDiv.textContent = binaryData;
})
.catch(error => {
console.error('Error decoding MP3:', error);
outputDiv.textContent = 'Error: Failed to decode MP3.';
});
};
reader.readAsArrayBuffer(file);
});
async function decodeMP3(arrayBuffer) {
// Create audio context
var audioContext = new (window.AudioContext || window.webkitAudioContext)();
// Decode audio data
var audioBuffer = await audioContext.decodeAudioData(arrayBuffer);
// Convert audio buffer to binary string
var binaryString = '';
for (var channel = 0; channel < audioBuffer.numberOfChannels; channel++) {
var channelData = audioBuffer.getChannelData(channel);
for (var i = 0; i < channelData.length; i++) {
// Scale float value to integer range [-32768, 32767]
var value = Math.round(channelData[i] * 32767);
// Convert integer value to binary string
binaryString += value.toString(2);
}
}
return binaryString;
}