-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusic.html
180 lines (169 loc) · 5.85 KB
/
music.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>HTML5音频可视化频谱跳动代码</title>
<style>
* {
margin: 0;
padding: 0;
}
#canvas {
position: absolute;
left: 0px;
width:100%;
margin: auto;
/*background: linear-gradient(135deg, rgb(206, 68, 201) 0%, rgb(255, 255, 255) 100%);*/
z-index:-1;
}
#myaudio{
position: absolute;
left: 0px;
width:100%;
margin: auto;
height: auto;
z-index:-1;
}
</style>
</head>
<body>
<input type="file" onchange="selectFile(this)" id="file"/>
<video id="myaudio" crossOrigin="anonymous">
<source id="src" src="">
</video>
<canvas id="canvas"></canvas>
<script>
// 音频图的条数
var count = 200;
var base = 5;
var type = 'rect'; //line, rect, circle
var oAudio = document.getElementById('myaudio');
var canvas = document.getElementById("canvas");
var oCtx, analyser, audioSrc, ctx, oW, oH, color1, voiceHeight;
var step, di;
var flag = true;
function resize() {
oAudio.width = window.innerWidth;
canvas.width = oAudio.width;
canvas.height = oAudio.width*9/16;
oW = canvas.width;
oH = canvas.height;
color1 = '#9e0099';
ctx.strokeStyle='white';
ctx.fillStyle = color1; // 绘制向上的线条
ctx.globalAlpha = 0.5;
di = ~~(voiceHeight.length/count);
step = ~~(oW/count);
if (step < 2) step=2;
window.scrollTo(0, 1080);
}
var drawMethod = {
'line': function() {
// 将当前的频率数据复制到传入的无符号字节数组中,做到实时连接
analyser.getByteFrequencyData(voiceHeight);
//console.log(voiceHeight.length);
// 自定义获取数组里边数据的频步
ctx.clearRect(0, 0, oW, oH);
ctx.beginPath();
ctx.moveTo(0,oH);
var max = 0;
var i = 0, j = 0;
for (; i < voiceHeight.length&&j * step<oW; i+=di) {
ctx.lineTo(j * step, oH -voiceHeight[i]-base);
max = max + voiceHeight[i]+base;
j ++;
}
max = max/j;
ctx.lineTo(oW, oH);
ctx.closePath();
ctx.fill();
ctx.stroke();
ctx.beginPath();
ctx.moveTo(0, oH - max);
ctx.lineTo(oW, oH - max);
ctx.closePath();
ctx.stroke();
},
'rect': function() {
// 将当前的频率数据复制到传入的无符号字节数组中,做到实时连接
analyser.getByteFrequencyData(voiceHeight);
//console.log(voiceHeight.length);
ctx.clearRect(0, 0, oW, oH);
var i = 0, j = 0;
for (; i < voiceHeight.length&&j * step<oW; i+=di) {
ctx.fillRect(j * step, oH/2, step-1, -voiceHeight[i]-base);
ctx.fillRect(j * step, oH/2, step-1, voiceHeight[i]+base);
j++;
}
},
'circle': function() {
// 将当前的频率数据复制到传入的无符号字节数组中,做到实时连接
analyser.getByteFrequencyData(voiceHeight);
//console.log(voiceHeight.length);
ctx.clearRect(0, 0, oW, oH);
var i = 0;
for (; i < voiceHeight.length&&i * step<oW; i++) {
ctx.fillRect(i * step, oH/2, 1, -voiceHeight[i]-base);
}
}
};
function init() {
if (flag) {
// 创建音频上下文对象
oCtx = new AudioContext();
// 创建媒体源,除了audio本身可以获取,也可以通过oCtx对象提供的api进行媒体源操作
audioSrc = oCtx.createMediaElementSource(oAudio);
flag = false;
// 创建分析机
analyser = oCtx.createAnalyser();
// 媒体源与分析机连接
audioSrc.connect(analyser);
// 输出的目标:将分析机分析出来的处理结果与目标点(耳机/扬声器)连接
analyser.connect(oCtx.destination);
/*
根据分析音频的数据去获取音频频次界定音频图的高度
放在与音频频次等长的8位无符号字节数组
Uint8Array:初始化默认值为1024
*/
// 缓冲区:进行数据的缓冲处理,转换成二进制数据
voiceHeight = new Uint8Array(analyser.frequencyBinCount);
// 效果(实现的具体方法)
// 利用cancas渐变进行音频绘制
ctx = canvas.getContext('2d');
resize();
}
(function draw(){
drawMethod[type]();
window.requestAnimationFrame(draw);
})();
oAudio.play();
window.onresize = resize;
window.scrollTo(0, 1080);
/*
analyserNode 提供了时时频率以及时间域的分析信息
允许你获取实时的数据,并进行音频可视化
analyserNode接口的fftSize属性
fftSize:无符号长整型值,用于确定频域的FFT(快速傅里叶变换)
ffiSize属性值是从32位到32768范围内的2的非零幂,默认值是2048
*/
}
function selectFile(t) {
var file = t.files[0];
var url = URL.createObjectURL(file);
if (file) {
oAudio.src = url;
init();
}
}
canvas.onclick = function () {
if (oAudio.paused) {
oAudio.play();
} else {
oAudio.pause();
}
}
</script>
</body>
</html>