-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
61 lines (47 loc) · 1.48 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
<!DOCTYPE html>
<html>
<head>
<script>
function decode(arrayBuffer, audioContext, callback)
{
if (window.Worker) {
var w = new Worker("mp3worker.min.js");
w.onmessage = function(e) {
var data = e.data;
var channelData = data.channelData;
var audioBuffer = null;
if (channelData && data.sampleRate) {
audioBuffer = audioContext.createBuffer(channelData.length, channelData[0].length, data.sampleRate);
for (var i = 0; i < data.channelData.length; i++) {
audioBuffer.getChannelData(i).set(data.channelData[i]);
}
}
callback(audioBuffer);
};
w.postMessage(arrayBuffer);
} else {
callback(null);
}
}
function handleLoad()
{
var context = window.webkitAudioContext ? new webkitAudioContext() : new AudioContext();
var request = new XMLHttpRequest();
request.responseType = "arraybuffer";
request.open("GET", "sounds/test.mp3", true);
request.addEventListener("load", function(e) {
decode(request.response, context, function(audioBuffer) {
console.log(audioBuffer);
var source = context.createBufferSource();
source.buffer = audioBuffer;
source.connect(context.destination);
source.start();
});
}, false);
request.send(null);
}
</script>
</head>
<body onload="handleLoad()">
</body>
</html>