-
Notifications
You must be signed in to change notification settings - Fork 6
/
analyser.html
87 lines (78 loc) · 2.77 KB
/
analyser.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0"/>
<meta name="format-detection" content="telephone=no"/>
<title>频谱图</title>
<style>
div.graph {
height: 4px;
margin: 1px 0;
font-size: 1px;
background-color: #4ba9e6;
}
</style>
</head>
<body>
<h1>频谱图(Analyser)</h1>
<p>
<input type="button" id="play" disabled value="加载中..."/>
</p>
<div id="graph">
</div>
<script type="text/javascript" src="sound.js"></script>
<script type="text/javascript">
var Ctx = window.webkitAudioContext ? window.webkitAudioContext : window.AudioContext;
ctx = new Ctx();
window.requestAnimationFrame = window.requestAnimationFrame
|| window.webkitRequestAnimationFrame
|| window.mozRequestAnimationFrame
|| function (f) {
setTimeout(f, 30);
};
(function () {
var analyser = ctx.createAnalyser();
analyser.fftSize = 128;
analyser.smoothingTimeConstant = 0.4;
analyser.connect(ctx.destination);
var freqByteData = new Uint8Array(analyser.frequencyBinCount);
var i;
var graphParentDom = document.getElementById('graph');
var graphDom;
for (i = 0; i < analyser.frequencyBinCount; i ++) {
graphDom = document.createElement('div');
graphDom.id = 'graph' + i;
graphDom.className = 'graph';
graphDom.style.width = '1px';
graphParentDom.appendChild(graphDom);
}
var renderFFT = function () {
var i;
analyser.getByteFrequencyData(freqByteData);
for (i = 0; i < analyser.frequencyBinCount; i ++) {
document.getElementById('graph' + i).style.width = freqByteData[i] + 1 + 'px';
}
window.requestAnimationFrame(renderFFT);
};
var loadedCallback = function(){
document.getElementById('play').removeAttribute('disabled');
document.getElementById('play').value = '播放';
};
var gangnam = new Sound(ctx);
gangnam.setOutput(analyser);
//gangnam.load('res/gangnam_style_clip.mp3', loadedCallback);
//gangnam.setLoop(5.106, 12.386);
//gangnam.load('res/beir_shuang_chip.mp3', loadedCallback);
//gangnam.setLoop(1.755, 12.813);
gangnam.load('res/mario.mp3', loadedCallback);
gangnam.setLoop(2.40, 88.8);
document.getElementById('play').onclick = function () {
gangnam.play();
document.getElementById('play').setAttribute('disabled', 'disabled');
renderFFT();
};
})();
</script>
</body>
</html>