forked from linoleum-js/equalizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
equalizer.min.js
1 lines (1 loc) · 2.47 KB
/
equalizer.min.js
1
(function(root,factory){"use strict";if(typeof define==="function"&&define.amd){define([],factory)}else if(typeof exports==="object"){module.exports=factory()}else{console.log("ok");root.equalizer=factory()}})(this,function(){"use strict";var context=null,audio=null,filters=[],$$=document.querySelectorAll.bind(document),$=document.querySelector.bind(document),createContext=function(){var previous=window&&window.equalizer;if(previous&&previous.context){context=previous.context}else{context=new AudioContext}},createInputs=function(className,container){var inputs=[],node,i;for(i=0;i<10;i++){node=document.createElement("input");node.className=className.slice(1);container.appendChild(node);inputs.push(node)}return inputs},initInputsData=function(inputs){[].forEach.call(inputs,function(item){item.setAttribute("min",-16);item.setAttribute("max",16);item.setAttribute("step",.1);item.setAttribute("value",0);item.setAttribute("type","range")})},initEvents=function(inputs){[].forEach.call(inputs,function(item,i){item.addEventListener("change",function(e){filters[i].gain.value=e.target.value},false)})},createFilter=function(frequency){var filter=context.createBiquadFilter();filter.type="peaking";filter.frequency.value=frequency;filter.gain.value=0;filter.Q.value=1;return filter},createFilters=function(){var frequencies=[60,170,310,600,1e3,3e3,6e3,12e3,14e3,16e3];filters=frequencies.map(function(frequency){return createFilter(frequency)});filters.reduce(function(prev,curr){prev.connect(curr);return curr})},validateParam=function(param){if(!param){throw new TypeError("equalizer: param required")}var container=$(param.container),inputs=$$(param.inputs);if(param.audio instanceof HTMLMediaElement){audio=param.audio}else if(typeof param.audio==="string"){audio=$(param.audio);if(!audio){throw new TypeError("equalizer: there's no element that match selector"+param.audio)}}else{throw new TypeError('equalizer: parameter "audio" must be string or an audio element')}if(!container&&!inputs.length){throw new TypeError("equalizer: there's no elements match \""+param.container+'" or "'+param.selector)}if(!inputs.length){inputs=createInputs(param.selector||"",container)}return inputs},bindEqualizer=function(){var source=context.createMediaElementSource(audio);source.connect(filters[0]);filters[9].connect(context.destination)},equalizer=function(param){var inputs=validateParam(param);createContext();createFilters();initInputsData(inputs);initEvents(inputs);bindEqualizer()};equalizer.context=context;return equalizer});