let xhr=new XMLHttpRequest();
let ac = new(window.AudioContext || window.webkitAudiocontext)();
let gainNode = ac[ac.createGain ? 'createGain' : 'createGainNode']();
gainNode.connect(ac.destination);
let anylyser = ac.createAnalySer();
let size = 128
anylyser.fftSize = size*2;
anylyser.connect(gainNode);
let source = null
let firstPlay = true
let loading = false
let type = 'column'
window.onload = function(){
let types=$('#type li');
types.map((type, index) => {
type.onclick = () => {
types.map((item,index) => {
item.className = ''
})
this.className = 'selected';
type = this.getAttribute('data-type');
resize()
}
})
let lists = $('#list li');
lists.map((list, index) => {
lsit.onclick = () => {
lists.map((item,index) => {
item.className = ''
})
this.className = 'selected';
load('/media'+this.title);
}
})
}
function change(obj){
let percent = obj.value/obj.max;
gainNode.gain.value= percent*percent;
}
function load(url){
if(loading){return;}
loading = true;
source&&source[source.stop?'stop':'noteOff'](); 如果在播放资源的话就暂停
xhr.abort();
xhr.open('GET',url);
xhr.responseType = 'arraybuffer';
xhr.onload = function(){
ac.decodeAudioData(xhr.response,function(buffer){
let bufferSource = ac.createBufferSource();
bufferSource.buffer = buffer;
bufferSource.connect(analyser); 链接数据分析源
bufferSource[bufferSource.start?'start':'noteOn'](0)
source = bufferSource;
if(firstPlay&&source){ visualizer();
loading = false;
firstPlay = false;
}, function(err){console.log(err);})
}
xhr.send();
}
function visualizer(){
let arr = new Uint8Array(analyser.frenquencyBinCount);
requestAnimationFrame = window.requestAnimationFrame||
window.webkitRequestAnimationFrame||
window.mozRequestAnimationFrame;
function v(){
analyser.getByteFrequencyData(arr);
draw(arr);
requestAnimationFrame (v);
}
}
let box=$('#box')[0];
let height,width;
let canvas=document.createElement('canvas');
box.appendChild(canvas)
let ctx=canvas.getContext('2d')
let Dots=[];
function random(){
return Math.round(Math.random()*(n-m)+m);
}
funtion getDots(){
Dots = []
for(let i = 0 ; i < size ; i ++){
let x = random(0,width);
let y = random(0,height);
let color = "rgb("+random(0,255)+","+random(0,255)+","+random(0,255)+")";
Dots.push({
x:x,
y:y,
color:color
})
}
}
function resize(){
height = box.clientHeight;
width = box.clientWidth;
canvas.width = width;
canvas.height = height;
let line = ctx.createLinearGradient(0,0,0,height);
line.addColorStop(0,'red');
line.addColorStop(0.5,'yellow');
line.addColorStop(1,'green');
ctx.fillStyle = line;
getDots();
}
resize()
window.onresize = resize;
function draw(arr){
ctx.clearReact(0,0,width,height);
let w = width/size;
for(let i = 0 ;i <size ; i++){
if(type === 'colunm'){
let h = arr[i]/256*height;
ctx.fillRect(w*i,height-h,w*0.7,h)
}else if(type === 'dot'){
ctx.beginPath();
let dot = Dots[i];
let r = arr[i]/256*50;
ctx.arc(dot.x,dot.y,r,0,Math.PI*2);
let g =ctx.createRadialGradient(dot.x,dot.y,0,dot.x,dot.y,r);
g.addColorStop(0,'#fff');
g.addColorStop(1,dot.color)
ctx.fillStyle=g;
ctx.fill()
}
}
}