HTML5音频解析与可视化

HTML5音频解析与可视化

基本结构

使用express 框架搭建后台基本结构

加载音乐并用ajax请求资源

加载音乐文件:

1
2
3
4
5
6
7
8
9
10
11
var lis = $("#list li");
for (var i = 0; i < list.length; i++) {
lis[i].onclick = function() {
for (var j = 0; j < lis.length; j++) {
lis[j].className = "";
}
this.className = "selected";
load("/media/" + this.title);
}
}

ajax加载资源

1
2
3
4
5
6
var xhr = new XMLHttpRequest();
xhr.abort();
xhr.open("GET", url);
xhr.responseType = "arraybuffer";
xhr.onload = function() {};
xhr.send();

Web Audio Api 关系图

1.webAudio-API

使用AudioContext解码

2.AudioContext

实现:

1
2
3
4
5
6
7
8
9
10
var ac = new(window.AudioContext || window.webkitAudioContext)();
ac.decodeAudioData(xhr.response, function(buffer) {
if (n != count) return;
var bufferSource = ac.createBufferSource();
bufferSource.buffer = buffer;
bufferSource.connect(gainNode);
//bufferSource.connect(ac.destination);
bufferSource[bufferSource.start ? "start" : "noteOn"](0);
suurce = bufferSource;
}

使用gainNode调节音量

3.GainNode

实现:

1
2
var gainNode = ac[ac.createGain ? "createGain" : "createGainNode"]();
gainNode.connect(ac.destination);

bufferSource连接gainNode

4.AudioBufferSourceNode

实现:

1
2
3
4
5
6
7
8
9
bufferSource.connect(gainNode);
function changeVolume(percent) {
gainNode.gain.value = percent * percent;
}
$("#volume")[0].onchange = function() {
changeVolume(this.value / this.max);
}

音乐播放逻辑问题

实现:

1
2
3
4
5
6
7
8
9
10
11
//音乐资源加载
var source = null;
//计数器
var count = 0;
var n = ++count;
source && source[source.stop ? "stop" : "noteOff"](); //默认为0
if (n != count) return;

分析音频资源

参考链接

5.AnalyserNode

实现:

1
2
3
4
5
6
var analyser=ac.createAnalyser();
analyser.fftSize=512;
analyser.connect(gainNode);
//bufferSource连接analyser
bufferSource.connect(analyser);

时时分析

1
2
3
4
5
6
7
8
9
10
11
function visualizer(){
var arr=new Uint8Array(analyser.frequencyBinCount);
requestAnimationFrame=window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame;
function v(){
analyser.getByteFrequencyData(arr);
}
requestAnimationFrame(v);
}
visualizer();

音乐数据可视化

使用canvas画图进行可视化

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
//canvas画图(柱状)
var box = $("#box")[0];
var height, width;
var canvas = document.createElement("canvas");
var ctx = canvas.getContext("2d");
box.appendChild(canvas);
function resize() {
height = box.clientHeight;
width = box.clientWidth;
canvas.height = height;
canvas.width = width;
var line = ctx.createLinearGradient(0, 0, 0, height);
line.addColorStop(0, "red");
line.addColorStop(0.5, "yello");
line.addColorStop(1, "green");
ctx.fillstyle = line;
}
resize();
window.onresize = resize;
function draw(arr) {
ctx.clearRect(0,0,width,height);
var w = width / size;
for (var i = 0; i < size; i++) {
var h = arr[i] / 256 * height;
ctx.fillRect(w * i, height - h, w * 0.6, h);
}
}
-------------本文结束感谢您的阅读-------------

本文标题:HTML5音频解析与可视化

文章作者:Awebone

发布时间:2017年10月25日 - 00:10

最后更新:2020年03月21日 - 14:03

原始链接:https://www.awebone.com/posts/fd5cbc55/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。