Comparing version 5.1.8 to 5.2.0
@@ -1189,2 +1189,13 @@ 'use strict'; | ||
/** | ||
* List available audio feature extractors. Return format provides the key to | ||
* be used in selecting the extractor in the extract methods | ||
* | ||
* @returns {Array.<string>} featureExtractors - a list of the keys of | ||
* available audio feature extractors | ||
*/ | ||
listAvailableFeatureExtractors: function () { | ||
return Object.keys(this.featureExtractors); | ||
}, | ||
/** | ||
* Extract an audio feature from a buffer | ||
@@ -1191,0 +1202,0 @@ * |
@@ -1193,2 +1193,13 @@ (function (global, factory) { | ||
/** | ||
* List available audio feature extractors. Return format provides the key to | ||
* be used in selecting the extractor in the extract methods | ||
* | ||
* @returns {Array.<string>} featureExtractors - a list of the keys of | ||
* available audio feature extractors | ||
*/ | ||
listAvailableFeatureExtractors: function () { | ||
return Object.keys(this.featureExtractors); | ||
}, | ||
/** | ||
* Extract an audio feature from a buffer | ||
@@ -1195,0 +1206,0 @@ * |
@@ -1,2 +0,2 @@ | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).Meyda=e()}(this,(function(){"use strict";var t=Object.freeze({__proto__:null,blackman:function(t){let e=new Float32Array(t),r=2*Math.PI/(t-1),a=2*r;for(let i=0;i<t/2;i++)e[i]=.42-.5*Math.cos(i*r)+.08*Math.cos(i*a);for(let r=Math.ceil(t/2);r>0;r--)e[t-r]=e[r-1];return e},sine:function(t){let e=Math.PI/(t-1),r=new Float32Array(t);for(let a=0;a<t;a++)r[a]=Math.sin(e*a);return r},hanning:function(t){let e=new Float32Array(t);for(let r=0;r<t;r++)e[r]=.5-.5*Math.cos(2*Math.PI*r/(t-1));return e},hamming:function(t){let e=new Float32Array(t);for(let r=0;r<t;r++)e[r]=.54-.46*Math.cos(2*Math.PI*(r/t-1));return e}});let e={};function r(t){for(;t%2==0&&t>1;)t/=2;return 1===t}function a(r,a){if("rect"!==a){if(""!==a&&a||(a="hanning"),e[a]||(e[a]={}),!e[a][r.length])try{e[a][r.length]=t[a](r.length)}catch(t){throw new Error("Invalid windowing function")}r=function(t,e){let r=[];for(let a=0;a<Math.min(t.length,e.length);a++)r[a]=t[a]*e[a];return r}(r,e[a][r.length])}return r}function i(t,e,r){let a=new Float32Array(t);for(var i=0;i<a.length;i++)a[i]=i*e/r,a[i]=13*Math.atan(a[i]/1315.8)+3.5*Math.atan(Math.pow(a[i]/7518,2));return a}function n(t){return Float32Array.from(t)}function o(t){return 1125*Math.log(1+t/700)}function s(t,e,r){let a=new Float32Array(t+2),i=new Float32Array(t+2),n=e/2,s=o(0),l=(o(n)-s)/(t+1),u=Array(t+2);for(let t=0;t<a.length;t++)a[t]=t*l,i[t]=(m=a[t],700*(Math.exp(m/1125)-1)),u[t]=Math.floor((r+1)*i[t]/e);var m,h=Array(t);for(let t=0;t<h.length;t++){h[t]=Array.apply(null,new Array(r/2+1)).map(Number.prototype.valueOf,0);for(let e=u[t];e<u[t+1];e++)h[t][e]=(e-u[t])/(u[t+1]-u[t]);for(let e=u[t+1];e<u[t+2];e++)h[t][e]=(u[t+2]-e)/(u[t+2]-u[t+1])}return h}function l(t,e,r,a=5,i=2,n=!0,o=440){var s=Math.floor(r/2)+1,l=new Array(r).fill(0).map(((a,i)=>t*function(t,e){return Math.log2(16*t/e)}(e*i/r,o)));l[0]=l[1]-1.5*t;var u,m,h,p=l.slice(1).map(((t,e)=>Math.max(t-l[e])),1).concat([1]),f=Math.round(t/2),c=new Array(t).fill(0).map(((e,r)=>l.map((e=>(10*t+f+e-r)%t-f)))),g=c.map(((t,e)=>t.map(((t,r)=>Math.exp(-.5*Math.pow(2*c[e][r]/p[r],2))))));if(m=(u=g)[0].map((()=>0)),h=u.reduce(((t,e)=>(e.forEach(((e,r)=>{t[r]+=Math.pow(e,2)})),t)),m).map(Math.sqrt),g=u.map(((t,e)=>t.map(((t,e)=>t/(h[e]||1))))),i){var w=l.map((e=>Math.exp(-.5*Math.pow((e/t-a)/i,2))));g=g.map((t=>t.map(((t,e)=>t*w[e]))))}return n&&(g=[...g.slice(3),...g.slice(0,3)]),g.map((t=>t.slice(0,s)))}function u(t,e){for(var r=0,a=0,i=0;i<e.length;i++)r+=Math.pow(i,t)*Math.abs(e[i]),a+=e[i];return r/a}function m(t){if("object"!=typeof t.ampSpectrum||"object"!=typeof t.barkScale)throw new TypeError;var e=24,r=new Float32Array(e),a=0,i=t.ampSpectrum,n=new Int32Array(25);n[0]=0;var o=t.barkScale[i.length-1]/e,s=1;for(let r=0;r<i.length;r++)for(;t.barkScale[r]>o;)n[s++]=r,o=s*t.barkScale[i.length-1]/e;n[24]=i.length-1;for(let t=0;t<e;t++){let e=0;for(let r=n[t];r<n[t+1];r++)e+=i[r];r[t]=Math.pow(e,.23)}for(let t=0;t<r.length;t++)a+=r[t];return{specific:r,total:a}}function h(){if("object"!=typeof arguments[0].ampSpectrum)throw new TypeError;for(var t=new Float32Array(arguments[0].ampSpectrum.length),e=0;e<t.length;e++)t[e]=Math.pow(arguments[0].ampSpectrum[e],2);return t}var p=null;var f=function(t,e){var r=t.length;return e=e||2,p&&p[r]||function(t){(p=p||{})[t]=new Array(t*t);for(var e=Math.PI/t,r=0;r<t;r++)for(var a=0;a<t;a++)p[t][a+r*t]=Math.cos(e*(a+.5)*r)}(r),t.map((function(){return 0})).map((function(a,i){return e*t.reduce((function(t,e,a,n){return t+e*p[r][a+i*r]}),0)}))};var c=Object.freeze({__proto__:null,buffer:function(t){return t.signal},rms:function(t){if("object"!=typeof t.signal)throw new TypeError;for(var e=0,r=0;r<t.signal.length;r++)e+=Math.pow(t.signal[r],2);return e/=t.signal.length,e=Math.sqrt(e)},energy:function(){if("object"!=typeof arguments[0].signal)throw new TypeError;for(var t=0,e=0;e<arguments[0].signal.length;e++)t+=Math.pow(Math.abs(arguments[0].signal[e]),2);return t},complexSpectrum:function(t){return t.complexSpectrum},spectralSlope:function(t){if("object"!=typeof t.ampSpectrum)throw new TypeError;let e=0,r=0,a=new Float32Array(t.ampSpectrum.length),i=0,n=0;for(var o=0;o<t.ampSpectrum.length;o++){e+=t.ampSpectrum[o];let s=o*t.sampleRate/t.bufferSize;a[o]=s,i+=s*s,r+=s,n+=s*t.ampSpectrum[o]}return(t.ampSpectrum.length*n-r*e)/(e*(i-Math.pow(r,2)))},spectralCentroid:function(){if("object"!=typeof arguments[0].ampSpectrum)throw new TypeError;return u(1,arguments[0].ampSpectrum)},spectralRolloff:function(){if("object"!=typeof arguments[0].ampSpectrum)throw new TypeError;for(var t=arguments[0].ampSpectrum,e=arguments[0].sampleRate/(2*(t.length-1)),r=0,a=0;a<t.length;a++)r+=t[a];for(var i=.99*r,n=t.length-1;r>i&&n>=0;)r-=t[n],--n;return(n+1)*e},spectralFlatness:function(){if("object"!=typeof arguments[0].ampSpectrum)throw new TypeError;for(var t=0,e=0,r=0;r<arguments[0].ampSpectrum.length;r++)t+=Math.log(arguments[0].ampSpectrum[r]),e+=arguments[0].ampSpectrum[r];return Math.exp(t/arguments[0].ampSpectrum.length)*arguments[0].ampSpectrum.length/e},spectralSpread:function(t){if("object"!=typeof t.ampSpectrum)throw new TypeError;return Math.sqrt(u(2,t.ampSpectrum)-Math.pow(u(1,t.ampSpectrum),2))},spectralSkewness:function(t){if("object"!=typeof t.ampSpectrum)throw new TypeError;var e=u(1,t.ampSpectrum),r=u(2,t.ampSpectrum),a=u(3,t.ampSpectrum);return(2*Math.pow(e,3)-3*e*r+a)/Math.pow(Math.sqrt(r-Math.pow(e,2)),3)},spectralKurtosis:function(){if("object"!=typeof arguments[0].ampSpectrum)throw new TypeError;var t=arguments[0].ampSpectrum,e=u(1,t),r=u(2,t),a=u(3,t),i=u(4,t),n=-3*Math.pow(e,4)+6*e*r-4*e*a+i,o=Math.pow(Math.sqrt(r-Math.pow(e,2)),4);return n/o},amplitudeSpectrum:function(t){return t.ampSpectrum},zcr:function(){if("object"!=typeof arguments[0].signal)throw new TypeError;for(var t=0,e=1;e<arguments[0].signal.length;e++)(arguments[0].signal[e-1]>=0&&arguments[0].signal[e]<0||arguments[0].signal[e-1]<0&&arguments[0].signal[e]>=0)&&t++;return t},loudness:m,perceptualSpread:function(){if("object"!=typeof arguments[0].signal)throw new TypeError;for(var t=m(arguments[0]),e=0,r=0;r<t.specific.length;r++)t.specific[r]>e&&(e=t.specific[r]);var a=Math.pow((t.total-e)/t.total,2);return a},perceptualSharpness:function(){if("object"!=typeof arguments[0].signal)throw new TypeError;for(var t=m(arguments[0]),e=t.specific,r=0,a=0;a<e.length;a++)r+=a<15?(a+1)*e[a+1]:.066*Math.exp(.171*(a+1));return r*=.11/t.total},powerSpectrum:h,mfcc:function(t){if("object"!=typeof t.ampSpectrum)throw new TypeError("Valid ampSpectrum is required to generate MFCC");if("object"!=typeof t.melFilterBank)throw new TypeError("Valid melFilterBank is required to generate MFCC");let e=Math.min(40,Math.max(1,t.numberOfMFCCCoefficients||13)),r=h(t),a=t.melFilterBank.length,i=Array(a);if(a<e)throw new Error("Insufficient filter bank for requested number of coefficients");let n=new Float32Array(a);for(let e=0;e<n.length;e++){i[e]=new Float32Array(t.bufferSize/2),n[e]=0;for(let a=0;a<t.bufferSize/2;a++)i[e][a]=t.melFilterBank[e][a]*r[a],n[e]+=i[e][a];n[e]=Math.log(n[e]+1)}let o=Array.prototype.slice.call(n);return f(o).slice(0,e)},chroma:function(t){if("object"!=typeof t.ampSpectrum)throw new TypeError("Valid ampSpectrum is required to generate chroma");if("object"!=typeof t.chromaFilterBank)throw new TypeError("Valid chromaFilterBank is required to generate chroma");var e=t.chromaFilterBank.map(((e,r)=>t.ampSpectrum.reduce(((t,r,a)=>t+r*e[a]),0))),r=Math.max(...e);return r?e.map((t=>t/r)):e},spectralFlux:function(t){if("object"!=typeof t.signal||"object"!=typeof t.previousSignal)throw new TypeError;let e=0;for(let r=-t.bufferSize/2;r<signal.length/2-1;r++)x=Math.abs(t.signal[r])-Math.abs(t.previousSignal[r]),e+=(x+Math.abs(x))/2;return e}});function g(t){if(Array.isArray(t)){for(var e=0,r=Array(t.length);e<t.length;e++)r[e]=t[e];return r}return Array.from(t)}var w={},S={},_={bitReverseArray:function(t){if(void 0===w[t]){for(var e=(t-1).toString(2).length,r="0".repeat(e),a={},i=0;i<t;i++){var n=i.toString(2);n=r.substr(n.length)+n,n=[].concat(g(n)).reverse().join(""),a[i]=parseInt(n,2)}w[t]=a}return w[t]},multiply:function(t,e){return{real:t.real*e.real-t.imag*e.imag,imag:t.real*e.imag+t.imag*e.real}},add:function(t,e){return{real:t.real+e.real,imag:t.imag+e.imag}},subtract:function(t,e){return{real:t.real-e.real,imag:t.imag-e.imag}},euler:function(t,e){var r=-2*Math.PI*t/e;return{real:Math.cos(r),imag:Math.sin(r)}},conj:function(t){return t.imag*=-1,t},constructComplexArray:function(t){var e={};e.real=void 0===t.real?t.slice():t.real.slice();var r=e.real.length;return void 0===S[r]&&(S[r]=Array.apply(null,Array(r)).map(Number.prototype.valueOf,0)),e.imag=S[r].slice(),e}},d=function(t){var e={};void 0===t.real||void 0===t.imag?e=_.constructComplexArray(t):(e.real=t.real.slice(),e.imag=t.imag.slice());var r=e.real.length,a=Math.log2(r);if(Math.round(a)!=a)throw new Error("Input size must be a power of 2.");if(e.real.length!=e.imag.length)throw new Error("Real and imaginary components must have the same length.");for(var i=_.bitReverseArray(r),n={real:[],imag:[]},o=0;o<r;o++)n.real[i[o]]=e.real[o],n.imag[i[o]]=e.imag[o];for(var s=0;s<r;s++)e.real[s]=n.real[s],e.imag[s]=n.imag[s];for(var l=1;l<=a;l++)for(var u=Math.pow(2,l),m=0;m<u/2;m++)for(var h=_.euler(m,u),p=0;p<r/u;p++){var f=u*p+m,c=u*p+m+u/2,g={real:e.real[f],imag:e.imag[f]},w={real:e.real[c],imag:e.imag[c]},S=_.multiply(h,w),d=_.subtract(g,S);e.real[c]=d.real,e.imag[c]=d.imag;var y=_.add(S,g);e.real[f]=y.real,e.imag[f]=y.imag}return e},y=d;class b{constructor(t,e){if(this._m=e,!t.audioContext)throw this._m.errors.noAC;if(t.bufferSize&&!r(t.bufferSize))throw this._m._errors.notPow2;if(!t.source)throw this._m._errors.noSource;this._m.audioContext=t.audioContext,this._m.bufferSize=t.bufferSize||this._m.bufferSize||256,this._m.hopSize=t.hopSize||this._m.hopSize||this._m.bufferSize,this._m.sampleRate=t.sampleRate||this._m.audioContext.sampleRate||44100,this._m.callback=t.callback,this._m.windowingFunction=t.windowingFunction||"hanning",this._m.featureExtractors=c,this._m.EXTRACTION_STARTED=t.startImmediately||!1,this._m.channel="number"==typeof t.channel?t.channel:0,this._m.inputs=t.inputs||1,this._m.outputs=t.outputs||1,this._m.numberOfMFCCCoefficients=t.numberOfMFCCCoefficients||this._m.numberOfMFCCCoefficients||13,this._m.spn=this._m.audioContext.createScriptProcessor(this._m.bufferSize,this._m.inputs,this._m.outputs),this._m.spn.connect(this._m.audioContext.destination),this._m._featuresToExtract=t.featureExtractors||[],this._m.barkScale=i(this._m.bufferSize,this._m.sampleRate,this._m.bufferSize),this._m.melFilterBank=s(Math.max(this._m.melBands,this._m.numberOfMFCCCoefficients),this._m.sampleRate,this._m.bufferSize),this._m.inputData=null,this._m.previousInputData=null,this._m.frame=null,this._m.previousFrame=null,this.setSource(t.source),this._m.spn.onaudioprocess=t=>{if(null!==this._m.inputData&&(this._m.previousInputData=this._m.inputData),this._m.inputData=t.inputBuffer.getChannelData(this._m.channel),this._m.previousInputData)(e=new Float32Array(this._m.previousInputData.length+this._m.inputData.length-this._m.hopSize)).set(this._m.previousInputData.slice(this._m.hopSize)),e.set(this._m.inputData,this._m.previousInputData.length-this._m.hopSize);else var e=this._m.inputData;(function(t,e,r){if(t.length<e)throw new Error("Buffer is too short for frame length");if(r<1)throw new Error("Hop length cannot be less that 1");if(e<1)throw new Error("Frame length cannot be less that 1");const a=1+Math.floor((t.length-e)/r);return new Array(a).fill(0).map(((a,i)=>t.slice(i*r,i*r+e)))})(e,this._m.bufferSize,this._m.hopSize).forEach((t=>{this._m.frame=t;var e=this._m.extract(this._m._featuresToExtract,this._m.frame,this._m.previousFrame);"function"==typeof this._m.callback&&this._m.EXTRACTION_STARTED&&this._m.callback(e),this._m.previousFrame=this._m.frame}))}}start(t){this._m._featuresToExtract=t||this._m._featuresToExtract,this._m.EXTRACTION_STARTED=!0}stop(){this._m.EXTRACTION_STARTED=!1}setSource(t){this._m.source&&this._m.source.disconnect(this._m.spn),this._m.source=t,this._m.source.connect(this._m.spn)}setChannel(t){t<=this._m.inputs?this._m.channel=t:console.error(`Channel ${t} does not exist. Make sure you've provided a value for 'inputs' that is greater than ${t} when instantiating the MeydaAnalyzer`)}get(t){return this._m.inputData?this._m.extract(t||this._m._featuresToExtract,this._m.inputData,this._m.previousInputData):null}}var v={audioContext:null,spn:null,bufferSize:512,sampleRate:44100,melBands:26,chromaBands:12,callback:null,windowingFunction:"hanning",featureExtractors:c,EXTRACTION_STARTED:!1,numberOfMFCCCoefficients:13,_featuresToExtract:[],windowing:a,_errors:{notPow2:new Error("Meyda: Buffer size must be a power of 2, e.g. 64 or 512"),featureUndef:new Error("Meyda: No features defined."),invalidFeatureFmt:new Error("Meyda: Invalid feature format"),invalidInput:new Error("Meyda: Invalid input."),noAC:new Error("Meyda: No AudioContext specified."),noSource:new Error("Meyda: No source node specified.")},createMeydaAnalyzer:function(t){return new b(t,Object.assign({},v))},extract:function(t,e,a){if(!e)throw this._errors.invalidInput;if("object"!=typeof e)throw this._errors.invalidInput;if(!t)throw this._errors.featureUndef;if(!r(e.length))throw this._errors.notPow2;void 0!==this.barkScale&&this.barkScale.length==this.bufferSize||(this.barkScale=i(this.bufferSize,this.sampleRate,this.bufferSize)),void 0!==this.melFilterBank&&this.barkScale.length==this.bufferSize&&this.melFilterBank.length==this.melBands||(this.melFilterBank=s(Math.max(this.melBands,this.numberOfMFCCCoefficients),this.sampleRate,this.bufferSize)),void 0!==this.chromaFilterBank&&this.chromaFilterBank.length==this.chromaBands||(this.chromaFilterBank=l(this.chromaBands,this.sampleRate,this.bufferSize)),void 0===e.buffer?this.signal=n(e):this.signal=e;let o=M(e,this.windowingFunction,this.bufferSize);if(this.signal=o.windowedSignal,this.complexSpectrum=o.complexSpectrum,this.ampSpectrum=o.ampSpectrum,a){let t=M(a,this.windowingFunction,this.bufferSize);this.previousSignal=t.windowedSignal,this.previousComplexSpectrum=t.complexSpectrum,this.previousAmpSpectrum=t.ampSpectrum}const u=t=>this.featureExtractors[t]({ampSpectrum:this.ampSpectrum,chromaFilterBank:this.chromaFilterBank,complexSpectrum:this.complexSpectrum,signal:this.signal,bufferSize:this.bufferSize,sampleRate:this.sampleRate,barkScale:this.barkScale,melFilterBank:this.melFilterBank,previousSignal:this.previousSignal,previousAmpSpectrum:this.previousAmpSpectrum,previousComplexSpectrum:this.previousComplexSpectrum,numberOfMFCCCoefficients:this.numberOfMFCCCoefficients});if("object"==typeof t)return t.reduce(((t,e)=>Object.assign({},t,{[e]:u(e)})),{});if("string"==typeof t)return u(t);throw this._errors.invalidFeatureFmt}},M=function(t,e,r){var i={};void 0===t.buffer?i.signal=n(t):i.signal=t,i.windowedSignal=a(i.signal,e),i.complexSpectrum=y(i.windowedSignal),i.ampSpectrum=new Float32Array(r/2);for(var o=0;o<r/2;o++)i.ampSpectrum[o]=Math.sqrt(Math.pow(i.complexSpectrum.real[o],2)+Math.pow(i.complexSpectrum.imag[o],2));return i};return"undefined"!=typeof window&&(window.Meyda=v),v})); | ||
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).Meyda=e()}(this,(function(){"use strict";var t=Object.freeze({__proto__:null,blackman:function(t){let e=new Float32Array(t),r=2*Math.PI/(t-1),a=2*r;for(let i=0;i<t/2;i++)e[i]=.42-.5*Math.cos(i*r)+.08*Math.cos(i*a);for(let r=Math.ceil(t/2);r>0;r--)e[t-r]=e[r-1];return e},sine:function(t){let e=Math.PI/(t-1),r=new Float32Array(t);for(let a=0;a<t;a++)r[a]=Math.sin(e*a);return r},hanning:function(t){let e=new Float32Array(t);for(let r=0;r<t;r++)e[r]=.5-.5*Math.cos(2*Math.PI*r/(t-1));return e},hamming:function(t){let e=new Float32Array(t);for(let r=0;r<t;r++)e[r]=.54-.46*Math.cos(2*Math.PI*(r/t-1));return e}});let e={};function r(t){for(;t%2==0&&t>1;)t/=2;return 1===t}function a(r,a){if("rect"!==a){if(""!==a&&a||(a="hanning"),e[a]||(e[a]={}),!e[a][r.length])try{e[a][r.length]=t[a](r.length)}catch(t){throw new Error("Invalid windowing function")}r=function(t,e){let r=[];for(let a=0;a<Math.min(t.length,e.length);a++)r[a]=t[a]*e[a];return r}(r,e[a][r.length])}return r}function i(t,e,r){let a=new Float32Array(t);for(var i=0;i<a.length;i++)a[i]=i*e/r,a[i]=13*Math.atan(a[i]/1315.8)+3.5*Math.atan(Math.pow(a[i]/7518,2));return a}function n(t){return Float32Array.from(t)}function o(t){return 1125*Math.log(1+t/700)}function s(t,e,r){let a=new Float32Array(t+2),i=new Float32Array(t+2),n=e/2,s=o(0),l=(o(n)-s)/(t+1),u=Array(t+2);for(let t=0;t<a.length;t++)a[t]=t*l,i[t]=(h=a[t],700*(Math.exp(h/1125)-1)),u[t]=Math.floor((r+1)*i[t]/e);var h,m=Array(t);for(let t=0;t<m.length;t++){m[t]=Array.apply(null,new Array(r/2+1)).map(Number.prototype.valueOf,0);for(let e=u[t];e<u[t+1];e++)m[t][e]=(e-u[t])/(u[t+1]-u[t]);for(let e=u[t+1];e<u[t+2];e++)m[t][e]=(u[t+2]-e)/(u[t+2]-u[t+1])}return m}function l(t,e,r,a=5,i=2,n=!0,o=440){var s=Math.floor(r/2)+1,l=new Array(r).fill(0).map(((a,i)=>t*function(t,e){return Math.log2(16*t/e)}(e*i/r,o)));l[0]=l[1]-1.5*t;var u,h,m,p=l.slice(1).map(((t,e)=>Math.max(t-l[e])),1).concat([1]),c=Math.round(t/2),f=new Array(t).fill(0).map(((e,r)=>l.map((e=>(10*t+c+e-r)%t-c)))),g=f.map(((t,e)=>t.map(((t,r)=>Math.exp(-.5*Math.pow(2*f[e][r]/p[r],2))))));if(h=(u=g)[0].map((()=>0)),m=u.reduce(((t,e)=>(e.forEach(((e,r)=>{t[r]+=Math.pow(e,2)})),t)),h).map(Math.sqrt),g=u.map(((t,e)=>t.map(((t,e)=>t/(m[e]||1))))),i){var w=l.map((e=>Math.exp(-.5*Math.pow((e/t-a)/i,2))));g=g.map((t=>t.map(((t,e)=>t*w[e]))))}return n&&(g=[...g.slice(3),...g.slice(0,3)]),g.map((t=>t.slice(0,s)))}function u(t,e){for(var r=0,a=0,i=0;i<e.length;i++)r+=Math.pow(i,t)*Math.abs(e[i]),a+=e[i];return r/a}function h(t){if("object"!=typeof t.ampSpectrum||"object"!=typeof t.barkScale)throw new TypeError;var e=24,r=new Float32Array(e),a=0,i=t.ampSpectrum,n=new Int32Array(25);n[0]=0;var o=t.barkScale[i.length-1]/e,s=1;for(let r=0;r<i.length;r++)for(;t.barkScale[r]>o;)n[s++]=r,o=s*t.barkScale[i.length-1]/e;n[24]=i.length-1;for(let t=0;t<e;t++){let e=0;for(let r=n[t];r<n[t+1];r++)e+=i[r];r[t]=Math.pow(e,.23)}for(let t=0;t<r.length;t++)a+=r[t];return{specific:r,total:a}}function m(){if("object"!=typeof arguments[0].ampSpectrum)throw new TypeError;for(var t=new Float32Array(arguments[0].ampSpectrum.length),e=0;e<t.length;e++)t[e]=Math.pow(arguments[0].ampSpectrum[e],2);return t}var p=null;var c=function(t,e){var r=t.length;return e=e||2,p&&p[r]||function(t){(p=p||{})[t]=new Array(t*t);for(var e=Math.PI/t,r=0;r<t;r++)for(var a=0;a<t;a++)p[t][a+r*t]=Math.cos(e*(a+.5)*r)}(r),t.map((function(){return 0})).map((function(a,i){return e*t.reduce((function(t,e,a,n){return t+e*p[r][a+i*r]}),0)}))};var f=Object.freeze({__proto__:null,buffer:function(t){return t.signal},rms:function(t){if("object"!=typeof t.signal)throw new TypeError;for(var e=0,r=0;r<t.signal.length;r++)e+=Math.pow(t.signal[r],2);return e/=t.signal.length,e=Math.sqrt(e)},energy:function(){if("object"!=typeof arguments[0].signal)throw new TypeError;for(var t=0,e=0;e<arguments[0].signal.length;e++)t+=Math.pow(Math.abs(arguments[0].signal[e]),2);return t},complexSpectrum:function(t){return t.complexSpectrum},spectralSlope:function(t){if("object"!=typeof t.ampSpectrum)throw new TypeError;let e=0,r=0,a=new Float32Array(t.ampSpectrum.length),i=0,n=0;for(var o=0;o<t.ampSpectrum.length;o++){e+=t.ampSpectrum[o];let s=o*t.sampleRate/t.bufferSize;a[o]=s,i+=s*s,r+=s,n+=s*t.ampSpectrum[o]}return(t.ampSpectrum.length*n-r*e)/(e*(i-Math.pow(r,2)))},spectralCentroid:function(){if("object"!=typeof arguments[0].ampSpectrum)throw new TypeError;return u(1,arguments[0].ampSpectrum)},spectralRolloff:function(){if("object"!=typeof arguments[0].ampSpectrum)throw new TypeError;for(var t=arguments[0].ampSpectrum,e=arguments[0].sampleRate/(2*(t.length-1)),r=0,a=0;a<t.length;a++)r+=t[a];for(var i=.99*r,n=t.length-1;r>i&&n>=0;)r-=t[n],--n;return(n+1)*e},spectralFlatness:function(){if("object"!=typeof arguments[0].ampSpectrum)throw new TypeError;for(var t=0,e=0,r=0;r<arguments[0].ampSpectrum.length;r++)t+=Math.log(arguments[0].ampSpectrum[r]),e+=arguments[0].ampSpectrum[r];return Math.exp(t/arguments[0].ampSpectrum.length)*arguments[0].ampSpectrum.length/e},spectralSpread:function(t){if("object"!=typeof t.ampSpectrum)throw new TypeError;return Math.sqrt(u(2,t.ampSpectrum)-Math.pow(u(1,t.ampSpectrum),2))},spectralSkewness:function(t){if("object"!=typeof t.ampSpectrum)throw new TypeError;var e=u(1,t.ampSpectrum),r=u(2,t.ampSpectrum),a=u(3,t.ampSpectrum);return(2*Math.pow(e,3)-3*e*r+a)/Math.pow(Math.sqrt(r-Math.pow(e,2)),3)},spectralKurtosis:function(){if("object"!=typeof arguments[0].ampSpectrum)throw new TypeError;var t=arguments[0].ampSpectrum,e=u(1,t),r=u(2,t),a=u(3,t),i=u(4,t),n=-3*Math.pow(e,4)+6*e*r-4*e*a+i,o=Math.pow(Math.sqrt(r-Math.pow(e,2)),4);return n/o},amplitudeSpectrum:function(t){return t.ampSpectrum},zcr:function(){if("object"!=typeof arguments[0].signal)throw new TypeError;for(var t=0,e=1;e<arguments[0].signal.length;e++)(arguments[0].signal[e-1]>=0&&arguments[0].signal[e]<0||arguments[0].signal[e-1]<0&&arguments[0].signal[e]>=0)&&t++;return t},loudness:h,perceptualSpread:function(){if("object"!=typeof arguments[0].signal)throw new TypeError;for(var t=h(arguments[0]),e=0,r=0;r<t.specific.length;r++)t.specific[r]>e&&(e=t.specific[r]);var a=Math.pow((t.total-e)/t.total,2);return a},perceptualSharpness:function(){if("object"!=typeof arguments[0].signal)throw new TypeError;for(var t=h(arguments[0]),e=t.specific,r=0,a=0;a<e.length;a++)r+=a<15?(a+1)*e[a+1]:.066*Math.exp(.171*(a+1));return r*=.11/t.total},powerSpectrum:m,mfcc:function(t){if("object"!=typeof t.ampSpectrum)throw new TypeError("Valid ampSpectrum is required to generate MFCC");if("object"!=typeof t.melFilterBank)throw new TypeError("Valid melFilterBank is required to generate MFCC");let e=Math.min(40,Math.max(1,t.numberOfMFCCCoefficients||13)),r=m(t),a=t.melFilterBank.length,i=Array(a);if(a<e)throw new Error("Insufficient filter bank for requested number of coefficients");let n=new Float32Array(a);for(let e=0;e<n.length;e++){i[e]=new Float32Array(t.bufferSize/2),n[e]=0;for(let a=0;a<t.bufferSize/2;a++)i[e][a]=t.melFilterBank[e][a]*r[a],n[e]+=i[e][a];n[e]=Math.log(n[e]+1)}let o=Array.prototype.slice.call(n);return c(o).slice(0,e)},chroma:function(t){if("object"!=typeof t.ampSpectrum)throw new TypeError("Valid ampSpectrum is required to generate chroma");if("object"!=typeof t.chromaFilterBank)throw new TypeError("Valid chromaFilterBank is required to generate chroma");var e=t.chromaFilterBank.map(((e,r)=>t.ampSpectrum.reduce(((t,r,a)=>t+r*e[a]),0))),r=Math.max(...e);return r?e.map((t=>t/r)):e},spectralFlux:function(t){if("object"!=typeof t.signal||"object"!=typeof t.previousSignal)throw new TypeError;let e=0;for(let r=-t.bufferSize/2;r<signal.length/2-1;r++)x=Math.abs(t.signal[r])-Math.abs(t.previousSignal[r]),e+=(x+Math.abs(x))/2;return e}});function g(t){if(Array.isArray(t)){for(var e=0,r=Array(t.length);e<t.length;e++)r[e]=t[e];return r}return Array.from(t)}var w={},S={},_={bitReverseArray:function(t){if(void 0===w[t]){for(var e=(t-1).toString(2).length,r="0".repeat(e),a={},i=0;i<t;i++){var n=i.toString(2);n=r.substr(n.length)+n,n=[].concat(g(n)).reverse().join(""),a[i]=parseInt(n,2)}w[t]=a}return w[t]},multiply:function(t,e){return{real:t.real*e.real-t.imag*e.imag,imag:t.real*e.imag+t.imag*e.real}},add:function(t,e){return{real:t.real+e.real,imag:t.imag+e.imag}},subtract:function(t,e){return{real:t.real-e.real,imag:t.imag-e.imag}},euler:function(t,e){var r=-2*Math.PI*t/e;return{real:Math.cos(r),imag:Math.sin(r)}},conj:function(t){return t.imag*=-1,t},constructComplexArray:function(t){var e={};e.real=void 0===t.real?t.slice():t.real.slice();var r=e.real.length;return void 0===S[r]&&(S[r]=Array.apply(null,Array(r)).map(Number.prototype.valueOf,0)),e.imag=S[r].slice(),e}},d=function(t){var e={};void 0===t.real||void 0===t.imag?e=_.constructComplexArray(t):(e.real=t.real.slice(),e.imag=t.imag.slice());var r=e.real.length,a=Math.log2(r);if(Math.round(a)!=a)throw new Error("Input size must be a power of 2.");if(e.real.length!=e.imag.length)throw new Error("Real and imaginary components must have the same length.");for(var i=_.bitReverseArray(r),n={real:[],imag:[]},o=0;o<r;o++)n.real[i[o]]=e.real[o],n.imag[i[o]]=e.imag[o];for(var s=0;s<r;s++)e.real[s]=n.real[s],e.imag[s]=n.imag[s];for(var l=1;l<=a;l++)for(var u=Math.pow(2,l),h=0;h<u/2;h++)for(var m=_.euler(h,u),p=0;p<r/u;p++){var c=u*p+h,f=u*p+h+u/2,g={real:e.real[c],imag:e.imag[c]},w={real:e.real[f],imag:e.imag[f]},S=_.multiply(m,w),d=_.subtract(g,S);e.real[f]=d.real,e.imag[f]=d.imag;var y=_.add(S,g);e.real[c]=y.real,e.imag[c]=y.imag}return e},y=d;class b{constructor(t,e){if(this._m=e,!t.audioContext)throw this._m.errors.noAC;if(t.bufferSize&&!r(t.bufferSize))throw this._m._errors.notPow2;if(!t.source)throw this._m._errors.noSource;this._m.audioContext=t.audioContext,this._m.bufferSize=t.bufferSize||this._m.bufferSize||256,this._m.hopSize=t.hopSize||this._m.hopSize||this._m.bufferSize,this._m.sampleRate=t.sampleRate||this._m.audioContext.sampleRate||44100,this._m.callback=t.callback,this._m.windowingFunction=t.windowingFunction||"hanning",this._m.featureExtractors=f,this._m.EXTRACTION_STARTED=t.startImmediately||!1,this._m.channel="number"==typeof t.channel?t.channel:0,this._m.inputs=t.inputs||1,this._m.outputs=t.outputs||1,this._m.numberOfMFCCCoefficients=t.numberOfMFCCCoefficients||this._m.numberOfMFCCCoefficients||13,this._m.spn=this._m.audioContext.createScriptProcessor(this._m.bufferSize,this._m.inputs,this._m.outputs),this._m.spn.connect(this._m.audioContext.destination),this._m._featuresToExtract=t.featureExtractors||[],this._m.barkScale=i(this._m.bufferSize,this._m.sampleRate,this._m.bufferSize),this._m.melFilterBank=s(Math.max(this._m.melBands,this._m.numberOfMFCCCoefficients),this._m.sampleRate,this._m.bufferSize),this._m.inputData=null,this._m.previousInputData=null,this._m.frame=null,this._m.previousFrame=null,this.setSource(t.source),this._m.spn.onaudioprocess=t=>{if(null!==this._m.inputData&&(this._m.previousInputData=this._m.inputData),this._m.inputData=t.inputBuffer.getChannelData(this._m.channel),this._m.previousInputData)(e=new Float32Array(this._m.previousInputData.length+this._m.inputData.length-this._m.hopSize)).set(this._m.previousInputData.slice(this._m.hopSize)),e.set(this._m.inputData,this._m.previousInputData.length-this._m.hopSize);else var e=this._m.inputData;(function(t,e,r){if(t.length<e)throw new Error("Buffer is too short for frame length");if(r<1)throw new Error("Hop length cannot be less that 1");if(e<1)throw new Error("Frame length cannot be less that 1");const a=1+Math.floor((t.length-e)/r);return new Array(a).fill(0).map(((a,i)=>t.slice(i*r,i*r+e)))})(e,this._m.bufferSize,this._m.hopSize).forEach((t=>{this._m.frame=t;var e=this._m.extract(this._m._featuresToExtract,this._m.frame,this._m.previousFrame);"function"==typeof this._m.callback&&this._m.EXTRACTION_STARTED&&this._m.callback(e),this._m.previousFrame=this._m.frame}))}}start(t){this._m._featuresToExtract=t||this._m._featuresToExtract,this._m.EXTRACTION_STARTED=!0}stop(){this._m.EXTRACTION_STARTED=!1}setSource(t){this._m.source&&this._m.source.disconnect(this._m.spn),this._m.source=t,this._m.source.connect(this._m.spn)}setChannel(t){t<=this._m.inputs?this._m.channel=t:console.error(`Channel ${t} does not exist. Make sure you've provided a value for 'inputs' that is greater than ${t} when instantiating the MeydaAnalyzer`)}get(t){return this._m.inputData?this._m.extract(t||this._m._featuresToExtract,this._m.inputData,this._m.previousInputData):null}}var v={audioContext:null,spn:null,bufferSize:512,sampleRate:44100,melBands:26,chromaBands:12,callback:null,windowingFunction:"hanning",featureExtractors:f,EXTRACTION_STARTED:!1,numberOfMFCCCoefficients:13,_featuresToExtract:[],windowing:a,_errors:{notPow2:new Error("Meyda: Buffer size must be a power of 2, e.g. 64 or 512"),featureUndef:new Error("Meyda: No features defined."),invalidFeatureFmt:new Error("Meyda: Invalid feature format"),invalidInput:new Error("Meyda: Invalid input."),noAC:new Error("Meyda: No AudioContext specified."),noSource:new Error("Meyda: No source node specified.")},createMeydaAnalyzer:function(t){return new b(t,Object.assign({},v))},listAvailableFeatureExtractors:function(){return Object.keys(this.featureExtractors)},extract:function(t,e,a){if(!e)throw this._errors.invalidInput;if("object"!=typeof e)throw this._errors.invalidInput;if(!t)throw this._errors.featureUndef;if(!r(e.length))throw this._errors.notPow2;void 0!==this.barkScale&&this.barkScale.length==this.bufferSize||(this.barkScale=i(this.bufferSize,this.sampleRate,this.bufferSize)),void 0!==this.melFilterBank&&this.barkScale.length==this.bufferSize&&this.melFilterBank.length==this.melBands||(this.melFilterBank=s(Math.max(this.melBands,this.numberOfMFCCCoefficients),this.sampleRate,this.bufferSize)),void 0!==this.chromaFilterBank&&this.chromaFilterBank.length==this.chromaBands||(this.chromaFilterBank=l(this.chromaBands,this.sampleRate,this.bufferSize)),void 0===e.buffer?this.signal=n(e):this.signal=e;let o=M(e,this.windowingFunction,this.bufferSize);if(this.signal=o.windowedSignal,this.complexSpectrum=o.complexSpectrum,this.ampSpectrum=o.ampSpectrum,a){let t=M(a,this.windowingFunction,this.bufferSize);this.previousSignal=t.windowedSignal,this.previousComplexSpectrum=t.complexSpectrum,this.previousAmpSpectrum=t.ampSpectrum}const u=t=>this.featureExtractors[t]({ampSpectrum:this.ampSpectrum,chromaFilterBank:this.chromaFilterBank,complexSpectrum:this.complexSpectrum,signal:this.signal,bufferSize:this.bufferSize,sampleRate:this.sampleRate,barkScale:this.barkScale,melFilterBank:this.melFilterBank,previousSignal:this.previousSignal,previousAmpSpectrum:this.previousAmpSpectrum,previousComplexSpectrum:this.previousComplexSpectrum,numberOfMFCCCoefficients:this.numberOfMFCCCoefficients});if("object"==typeof t)return t.reduce(((t,e)=>Object.assign({},t,{[e]:u(e)})),{});if("string"==typeof t)return u(t);throw this._errors.invalidFeatureFmt}},M=function(t,e,r){var i={};void 0===t.buffer?i.signal=n(t):i.signal=t,i.windowedSignal=a(i.signal,e),i.complexSpectrum=y(i.windowedSignal),i.ampSpectrum=new Float32Array(r/2);for(var o=0;o<r/2;o++)i.ampSpectrum[o]=Math.sqrt(Math.pow(i.complexSpectrum.real[o],2)+Math.pow(i.complexSpectrum.imag[o],2));return i};return"undefined"!=typeof window&&(window.Meyda=v),v})); | ||
//# sourceMappingURL=meyda.min.js.map |
{ | ||
"name": "meyda", | ||
"version": "5.1.8", | ||
"version": "5.2.0", | ||
"description": "Real-time feature extraction for the web audio api", | ||
@@ -5,0 +5,0 @@ "main": "./dist/node/main.js", |
@@ -7,2 +7,4 @@ # meyda | ||
Would you like your Meyda project to be featured in our [user showcase]? [Please let us know!](https://github.com/meyda/meyda/issues/new?assignees=&labels=Showcase+Request&template=showcase-application.md&title=) | ||
Want to ask questions, or chat about Meyda? Check out our [slack channel] on the Web Audio Slack Team. You'll need to [sign up here][web-audio-slack-team-signup] before you can join. | ||
@@ -32,1 +34,2 @@ | ||
[web-audio-slack-team-signup]: https://web-audio-slackin.herokuapp.com/ | ||
[user-showcase]: https://meyda.js.org/showcase |
@@ -174,2 +174,14 @@ import * as utilities from "./utilities"; | ||
/** | ||
* List available audio feature extractors. Return format provides the key to | ||
* be used in selecting the extractor in the extract methods | ||
* | ||
* @returns {Array.<string>} featureExtractors - a list of the keys of | ||
* available audio feature extractors | ||
*/ | ||
listAvailableFeatureExtractors: function () { | ||
return Object.keys(this.featureExtractors); | ||
}, | ||
/** | ||
* Extract an audio feature from a buffer | ||
@@ -176,0 +188,0 @@ * |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
360944
5171
34