-
Notifications
You must be signed in to change notification settings - Fork 5
/
binaural-modeled.min.js
1 lines (1 loc) · 16.2 KB
/
binaural-modeled.min.js
1
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var t;t="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,t.BinauralModeled=e()}}(function(){return function e(t,i,n){function s(r,a){if(!i[r]){if(!t[r]){var u="function"==typeof require&&require;if(!a&&u)return u(r,!0);if(o)return o(r,!0);var l=new Error("Cannot find module '"+r+"'");throw l.code="MODULE_NOT_FOUND",l}var c=i[r]={exports:{}};t[r][0].call(c.exports,function(e){var i=t[r][1][e];return s(i?i:e)},c,c.exports,e,t,i,n)}return i[r].exports}for(var o="function"==typeof require&&require,r=0;r<n.length;r++)s(n[r]);return s}({1:[function(e,t,i){t.exports=e("./dist/binaural-modeled")},{"./dist/binaural-modeled":2}],2:[function(e,t,i){"use strict";var n=e("babel-runtime/helpers/create-class")["default"],s=e("babel-runtime/helpers/class-call-check")["default"],o=e("babel-runtime/helpers/interop-require-default")["default"];Object.defineProperty(i,"__esModule",{value:!0});var r=e("kdt"),a=o(r),u=e("biquad-filter"),l=o(u),c=e("fractional-delay"),h=o(c),f=function(){function e(t){s(this,e),this.audioContext=t.audioContext,this.hrtfDataset=void 0,this.hrtfDatasetLength=void 0,this.nextPosition=[],this.changeWhenFinishCrossfading=!1,this.position=[],this.crossfadeDuration=.02,this.bufferSize=1024,this.tree=-1,this.input=this.audioContext.createGain(),this.state="A",this.target=void 0,this.pendingPosition=void 0,this.convolverA=new d({audioContext:this.audioContext}),this.convolverA.gain.value=1,this.input.connect(this.convolverA.input),this.convolverB=new d({audioContext:this.audioContext}),this.convolverB.gain.value=0,this.input.connect(this.convolverB.input),this.sampleRate=this.audioContext.sampleRate}return n(e,[{key:"connect",value:function(e){return this.convolverA.connect(e),this.convolverB.connect(e),this}},{key:"disconnect",value:function(e){return this.convolverA.disconnect(e),this.convolverB.disconnect(e),this}},{key:"distance",value:function(e,t){return Math.pow(e.x-t.x,2)+Math.pow(e.y-t.y,2)+Math.pow(e.z-t.z,2)}},{key:"setPosition",value:function(e,t,i){var n=this.getRealCoordinates(e,t,i);if(n.azimuth!==this.position.azimuth||n.elevation!==this.position.elevation||n.distance!==this.position.distance)switch(this.state){case"A":this.state="A2B",this.pendingPosition=void 0,this._crossfadeTo("B",n);break;case"B":this.state="B2A",this.pendingPosition=void 0,this._crossfadeTo("A",n);break;case"A2B":this.pendingPosition=n;break;case"B2A":this.pendingPosition=n}}},{key:"_crossfadeTo",value:function(e,t){function i(e){e.audioContext.currentTime>o&&(window.clearInterval(r),e.state=e.target,e.target=void 0,e.pendingPosition&&e.setPosition(e.pendingPosition.azimuth,e.pendingPosition.elevation,e.pendingPosition.distance))}this.position=t,this.target=e;var n=this.getHRTF(this.position.azimuth,this.position.elevation,this.position.distance),s=this.audioContext.currentTime,o=s+this.crossfadeDuration;switch(this.target){case"A":this.convolverA.setCoefficients(n.iir_coeffs_left,n.iir_coeffs_right),this.convolverA.setDelay(n.itd/1e3),this.convolverB.gain.linearRampToValueAtTime(0,o),this.convolverA.gain.linearRampToValueAtTime(1,o);break;case"B":this.convolverB.setCoefficients(n.iir_coeffs_left,n.iir_coeffs_right),this.convolverB.setDelay(n.itd/1e3),this.convolverA.gain.linearRampToValueAtTime(0,o),this.convolverB.gain.linearRampToValueAtTime(1,o)}var r=window.setInterval(i,10,this)}},{key:"getPosition",value:function(){return this.position}},{key:"setCrossfadeDuration",value:function(e){this.crossfadeDuration=e/1e3}},{key:"getCrossfadeDuration",value:function(){return 1e3*this.crossfadeDuration}},{key:"getHRTF",value:function(e,t,i){var n=this.getNearestPoint(e,t,i),s=[];return s.iir_coeffs_left=n.iir_coeffs_left,s.iir_coeffs_right=n.iir_coeffs_right,s.itd=n.itd,s}},{key:"sphericalToCartesian",value:function(e,t,i){return{x:i*Math.sin(e),y:i*Math.cos(e),z:i*Math.sin(t)}}},{key:"getRealCoordinates",value:function(e,t,i){var n=this.getNearestPoint(e,t,i);return{azimuth:n.azimuth,elevation:n.elevation,distance:n.distance}}},{key:"getNearestPoint",value:function(e,t,i){var n=e*Math.PI/180,s=t*Math.PI/180,o=this.sphericalToCartesian(n,s,i),r=this.tree.nearest(o,1)[0];return r[0]}},{key:"HRTFDataset",set:function(e){this.hrtfDataset=e,this.hrtfDatasetLength=this.hrtfDataset.length;for(var t=0;t<this.hrtfDatasetLength;t++){var i=this.hrtfDataset[t],n=i.azimuth*Math.PI/180,s=i.elevation*Math.PI/180,o=this.sphericalToCartesian(n,s,i.distance);i.x=o.x,i.y=o.y,i.z=o.z}this.tree=a["default"].createKdTree(this.hrtfDataset,this.distance,["x","y","z"]);var r=this.getHRTF(0,0,1);this.convolverB.setCoefficients(r.iir_coeffs_left,r.iir_coeffs_right),this.convolverB.setDelay(r.itd/1e3),this.convolverA.setCoefficients(r.iir_coeffs_left,r.iir_coeffs_right),this.convolverA.setDelay(r.itd/1e3)},get:function(){return this.hrtfDataset}}]),e}();i["default"]=f;var d=function(){function e(t){s(this,e),this.audioContext=t.audioContext,this.bufferSize=1024,this.input=this.audioContext.createGain(),this.gainNode=this.audioContext.createGain(),this.biquadFilterLeft=new l["default"],this.biquadFilterRight=new l["default"],this.fractionalDelayLeft=new h["default"](44100),this.fractionalDelayRight=new h["default"](44100),this.processorNode=this.audioContext.createScriptProcessor(this.bufferSize),this.input.connect(this.processorNode),this.processorNode.connect(this.gainNode),this.processorNodeFunction()}return n(e,[{key:"setCoefficients",value:function(e,t){this.biquadFilterLeft.setCoefficients(e),this.biquadFilterRight.setCoefficients(t)}},{key:"setDelay",value:function(e){var t=.001+e/2,i=.001-e/2;this.fractionalDelayLeft.setDelay(t),this.fractionalDelayRight.setDelay(i)}},{key:"processorNodeFunction",value:function(){var e=this;this.processorNode.onaudioprocess=function(t){var i=t.inputBuffer.getChannelData(0),n=t.outputBuffer.getChannelData(0),s=t.outputBuffer.getChannelData(1),o=new Float32Array(e.fractionalDelayLeft.process(i)),r=new Float32Array(e.fractionalDelayRight.process(i));e.biquadFilterLeft.process(o,n),e.biquadFilterRight.process(r,s)}}},{key:"connect",value:function(e){return this.gainNode.connect(e),this}},{key:"disconnect",value:function(e){return this.gainNode.disconnect(e),this}},{key:"gain",get:function(){return this.gainNode.gain}}]),e}();t.exports=i["default"]},{"babel-runtime/helpers/class-call-check":4,"babel-runtime/helpers/create-class":5,"babel-runtime/helpers/interop-require-default":6,"biquad-filter":10,"fractional-delay":18,kdt:9}],3:[function(e,t,i){t.exports={"default":e("core-js/library/fn/object/define-property"),__esModule:!0}},{"core-js/library/fn/object/define-property":7}],4:[function(e,t,i){"use strict";i["default"]=function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")},i.__esModule=!0},{}],5:[function(e,t,i){"use strict";var n=e("babel-runtime/core-js/object/define-property")["default"];i["default"]=function(){function e(e,t){for(var i=0;i<t.length;i++){var s=t[i];s.enumerable=s.enumerable||!1,s.configurable=!0,"value"in s&&(s.writable=!0),n(e,s.key,s)}}return function(t,i,n){return i&&e(t.prototype,i),n&&e(t,n),t}}(),i.__esModule=!0},{"babel-runtime/core-js/object/define-property":3}],6:[function(e,t,i){"use strict";i["default"]=function(e){return e&&e.__esModule?e:{"default":e}},i.__esModule=!0},{}],7:[function(e,t,i){var n=e("../../modules/$");t.exports=function(e,t,i){return n.setDesc(e,t,i)}},{"../../modules/$":8}],8:[function(e,t,i){var n=Object;t.exports={create:n.create,getProto:n.getPrototypeOf,isEnum:{}.propertyIsEnumerable,getDesc:n.getOwnPropertyDescriptor,setDesc:n.defineProperty,setDescs:n.defineProperties,getKeys:n.keys,getNames:n.getOwnPropertyNames,getSymbols:n.getOwnPropertySymbols,each:[].forEach}},{}],9:[function(e,t,i){function n(e,t,i){this.obj=e,this.left=null,this.right=null,this.parent=i,this.dimension=t}function s(e,t,i){function s(e,t,o){var r,a,u=t%i.length;return 0===e.length?null:1===e.length?new n(e[0],u,o):(e.sort(function(e,t){return e[i[u]]-t[i[u]]}),r=Math.floor(e.length/2),a=new n(e[r],u,o),a.left=s(e.slice(0,r),t+1,a),a.right=s(e.slice(r+1),t+1,a),a)}var r=this;this.root=s(e,0,null),this.insert=function(e){function t(n,s){if(null===n)return s;var o=i[n.dimension];return e[o]<n.obj[o]?t(n.left,n):t(n.right,n)}var s,o,r=t(this.root,null);return null===r?void(this.root=new n(e,0,null)):(s=new n(e,(r.dimension+1)%i.length,r),o=i[r.dimension],void(e[o]<r.obj[o]?r.left=s:r.right=s))},this.remove=function(e){function t(n){if(null===n)return null;if(n.obj===e)return n;var s=i[n.dimension];return e[s]<n.obj[s]?t(n.left,n):t(n.right,n)}function n(e){function t(e,n){var s,o,r,a,u;return null===e?null:(s=i[n],e.dimension===n?null!==e.right?t(e.right,n):e:(o=e.obj[s],r=t(e.left,n),a=t(e.right,n),u=e,null!==r&&r.obj[s]>o&&(u=r),null!==a&&a.obj[s]>u.obj[s]&&(u=a),u))}function s(e,t){var n,o,r,a,u;return null===e?null:(n=i[t],e.dimension===t?null!==e.left?s(e.left,t):e:(o=e.obj[n],r=s(e.left,t),a=s(e.right,t),u=e,null!==r&&r.obj[n]<o&&(u=r),null!==a&&a.obj[n]<u.obj[n]&&(u=a),u))}var o,a,u;return null===e.left&&null===e.right?null===e.parent?void(r.root=null):(u=i[e.parent.dimension],void(e.obj[u]<e.parent.obj[u]?e.parent.left=null:e.parent.right=null)):(o=null!==e.left?t(e.left,e.dimension):s(e.right,e.dimension),a=o.obj,n(o),void(e.obj=a))}var s;s=t(r.root),null!==s&&n(s)},this.nearest=function(e,n,s){function a(s){function o(e,t){c.push([e,t]),c.size()>n&&c.pop()}var r,u,l,h,f=i[s.dimension],d=t(e,s.obj),p={};for(h=0;h<i.length;h+=1)h===s.dimension?p[i[h]]=e[i[h]]:p[i[h]]=s.obj[i[h]];return u=t(p,s.obj),null===s.right&&null===s.left?void((c.size()<n||d<c.peek()[1])&&o(s,d)):(r=null===s.right?s.left:null===s.left?s.right:e[f]<s.obj[f]?s.left:s.right,a(r),(c.size()<n||d<c.peek()[1])&&o(s,d),void((c.size()<n||Math.abs(u)<c.peek()[1])&&(l=r===s.left?s.right:s.left,null!==l&&a(l))))}var u,l,c;if(c=new o(function(e){return-e[1]}),s)for(u=0;n>u;u+=1)c.push([null,s]);for(a(r.root),l=[],u=0;n>u;u+=1)c.content[u][0]&&l.push([c.content[u][0].obj,c.content[u][1]]);return l},this.balanceFactor=function(){function e(t){return null===t?0:Math.max(e(t.left),e(t.right))+1}function t(e){return null===e?0:t(e.left)+t(e.right)+1}return e(r.root)/(Math.log(t(r.root))/Math.log(2))}}function o(e){this.content=[],this.scoreFunction=e}o.prototype={push:function(e){this.content.push(e),this.bubbleUp(this.content.length-1)},pop:function(){var e=this.content[0],t=this.content.pop();return this.content.length>0&&(this.content[0]=t,this.sinkDown(0)),e},peek:function(){return this.content[0]},remove:function(e){for(var t=this.content.length,i=0;t>i;i++)if(this.content[i]==e){var n=this.content.pop();return void(i!=t-1&&(this.content[i]=n,this.scoreFunction(n)<this.scoreFunction(e)?this.bubbleUp(i):this.sinkDown(i)))}throw new Error("Node not found.")},size:function(){return this.content.length},bubbleUp:function(e){for(var t=this.content[e];e>0;){var i=Math.floor((e+1)/2)-1,n=this.content[i];if(!(this.scoreFunction(t)<this.scoreFunction(n)))break;this.content[i]=t,this.content[e]=n,e=i}},sinkDown:function(e){for(var t=this.content.length,i=this.content[e],n=this.scoreFunction(i);;){var s=2*(e+1),o=s-1,r=null;if(t>o){var a=this.content[o],u=this.scoreFunction(a);n>u&&(r=o)}if(t>s){var l=this.content[s],c=this.scoreFunction(l);(null==r?n:u)>c&&(r=s)}if(null==r)break;this.content[e]=this.content[r],this.content[r]=i,e=r}}},t.exports={createKdTree:function(e,t,i){return new s(e,t,i)}}},{}],10:[function(e,t,i){t.exports=e("./dist/biquad-filter")},{"./dist/biquad-filter":11}],11:[function(e,t,i){"use strict";var n=e("babel-runtime/helpers/create-class")["default"],s=e("babel-runtime/helpers/class-call-check")["default"];Object.defineProperty(i,"__esModule",{value:!0});var o=function(){function e(){s(this,e),this.coefficients=[],this.numberOfCascade=1,this.resetMemories()}return n(e,[{key:"setCoefficients",value:function(e){if(e){this.numberOfCascade=this.getNumberOfCascadeFilters(e),this.coefficients=[],this.coefficients.g=e[0];for(var t=0;t<this.numberOfCascade;t++)this.coefficients[t]={b1:e[1+4*t],b2:e[2+4*t],a1:e[3+4*t],a2:e[4+4*t]};return this.resetMemories(),!0}throw new Error("No coefficients are set")}},{key:"getNumberOfCascadeFilters",value:function(e){return(e.length-1)/4}},{key:"resetMemories",value:function(){this.memories=[{xi1:0,xi2:0,yi1:0,yi2:0}];for(var e=1;e<this.numberOfCascade;e++)this.memories[e]={yi1:0,yi2:0}}},{key:"process",value:function(e,t){for(var i,n,s,o,r,a,u,l,c,h,f,d=[],p=0;p<e.length;p++){i=e[p],n=this.coefficients[0].b1,s=this.coefficients[0].b2,o=this.coefficients[0].a1,r=this.coefficients[0].a2,a=this.memories[0].xi1,u=this.memories[0].xi2,l=this.memories[0].yi1,c=this.memories[0].yi2,d[0]=i+n*a+s*u-o*l-r*c;for(var v=1;v<this.numberOfCascade;v++)n=this.coefficients[v].b1,s=this.coefficients[v].b2,o=this.coefficients[v].a1,r=this.coefficients[v].a2,h=this.memories[v-1].yi1,f=this.memories[v-1].yi2,l=this.memories[v].yi1,c=this.memories[v].yi2,d[v]=d[v-1]+n*h+s*f-o*l-r*c;t[p]=d[this.numberOfCascade-1]*this.coefficients.g,this.memories[0].xi2=this.memories[0].xi1,this.memories[0].xi1=i;for(var m=0;m<this.numberOfCascade;m++)this.memories[m].yi2=this.memories[m].yi1,this.memories[m].yi1=d[m]}}}]),e}();i["default"]=o,t.exports=i["default"]},{"babel-runtime/helpers/class-call-check":13,"babel-runtime/helpers/create-class":14}],12:[function(e,t,i){arguments[4][3][0].apply(i,arguments)},{"core-js/library/fn/object/define-property":15,dup:3}],13:[function(e,t,i){arguments[4][4][0].apply(i,arguments)},{dup:4}],14:[function(e,t,i){arguments[4][5][0].apply(i,arguments)},{"babel-runtime/core-js/object/define-property":12,dup:5}],15:[function(e,t,i){arguments[4][7][0].apply(i,arguments)},{"../../modules/$":16,dup:7}],16:[function(e,t,i){arguments[4][8][0].apply(i,arguments)},{dup:8}],17:[function(e,t,i){"use strict";var n=e("babel-runtime/helpers/create-class")["default"],s=e("babel-runtime/helpers/class-call-check")["default"];Object.defineProperty(i,"__esModule",{value:!0});var o=function(){function e(t,i){s(this,e),this.delayTime=0,this.posRead=0,this.posWrite=0,this.fracXi1=0,this.fracYi1=0,this.intDelay=0,this.fracDelay=0,this.a1=void 0,this.sampleRate=t,this.maxDelayTime=i||1,this.bufferSize=this.maxDelayTime*this.sampleRate,this.bufferSize%1!==0&&(this.bufferSize=parseInt(this.bufferSize)+1),this.buffer=new Float32Array(this.bufferSize)}return n(e,[{key:"setDelay",value:function(e){if(!(e<this.maxDelayTime))throw new Error("delayTime > maxDelayTime");this.delayTime=e;var t=e*this.sampleRate;this.intDelay=parseInt(t),this.fracDelay=t-this.intDelay,this.resample(),0!==this.fracDelay&&this.updateThiranCoefficient()}},{key:"getDelay",value:function(){return this.delayTime}},{key:"process",value:function(e){for(var t=new Float32Array(e.length),i=0;i<e.length;i+=1)this.buffer[this.posWrite]=e[i],t[i]=this.buffer[this.posRead],this.updatePointers();return 0===this.fracDelay?t:t=new Float32Array(this.fractionalThiranProcess(t))}},{key:"updatePointers",value:function(){this.posWrite===this.buffer.length-1?this.posWrite=0:this.posWrite=this.posWrite+1,this.posRead===this.buffer.length-1?this.posRead=0:this.posRead=this.posRead+1}},{key:"updateThiranCoefficient",value:function(){this.a1=(1-this.fracDelay)/(1+this.fracDelay)}},{key:"resample",value:function(){if(this.posWrite-this.intDelay<0){var e=this.intDelay-this.posWrite;this.posRead=this.buffer.length-e}else this.posRead=this.posWrite-this.intDelay}},{key:"fractionalThiranProcess",value:function(e){for(var t,i,n=new Float32Array(e.length),s=this.fracXi1,o=this.fracYi1,r=0;r<e.length;r+=1)t=e[r],i=this.a1*t+s-this.a1*o,s=t,o=i,n[r]=i;return this.fracXi1=s,this.fracYi1=o,n}}]),e}();i["default"]=o,t.exports=i["default"]},{"babel-runtime/helpers/class-call-check":20,"babel-runtime/helpers/create-class":21}],18:[function(e,t,i){t.exports=e("./dist/fractional-delay")},{"./dist/fractional-delay":17}],19:[function(e,t,i){arguments[4][3][0].apply(i,arguments)},{"core-js/library/fn/object/define-property":22,dup:3}],20:[function(e,t,i){arguments[4][4][0].apply(i,arguments)},{dup:4}],21:[function(e,t,i){arguments[4][5][0].apply(i,arguments)},{"babel-runtime/core-js/object/define-property":19,dup:5}],22:[function(e,t,i){arguments[4][7][0].apply(i,arguments)},{"../../modules/$":23,dup:7}],23:[function(e,t,i){arguments[4][8][0].apply(i,arguments)},{dup:8}]},{},[1])(1)});