UNPKG

audio-inspect

Version:

Lightweight yet powerful audio analysis library

2 lines (1 loc) 10.7 kB
"use strict";var S=Object.defineProperty;var L=Object.getOwnPropertyDescriptor;var U=Object.getOwnPropertyNames;var z=Object.prototype.hasOwnProperty;var V=(e,n)=>{for(var t in n)S(e,t,{get:n[t],enumerable:!0})},$=(e,n,t,a)=>{if(n&&typeof n=="object"||typeof n=="function")for(let r of U(n))!z.call(e,r)&&r!==t&&S(e,r,{get:()=>n[r],enumerable:!(a=L(n,r))||a.enumerable});return e};var j=e=>$(S({},"__esModule",{value:!0}),e);var re={};V(re,{getPeak:()=>J,getPeakAmplitude:()=>J,getPeaks:()=>Q,getPeaksAnalysis:()=>ne,getRMS:()=>Z,getRMSAnalysis:()=>te,getWaveform:()=>Y,getWaveformAnalysis:()=>ee,getZeroCrossing:()=>X});module.exports=j(re);var A=class e extends Error{constructor(t,a,r,o){super(a,{cause:r});this.code=t;this.details=o;this.cause=r,Error.captureStackTrace&&Error.captureStackTrace(this,e)}name="AudioInspectError";timestamp=new Date;cause;toJSON(){return{name:this.name,code:this.code,message:this.message,details:this.details,timestamp:this.timestamp,stack:this.stack,cause:this.cause}}};function x(){if(typeof performance<"u"&&performance.now)return performance.now();if(typeof process<"u"&&process.hrtime){let[e,n]=process.hrtime();return e*1e3+n/1e6}return Date.now()}function M(e,n=0){if(n===-1){if(e.numberOfChannels===0)throw new A("INVALID_INPUT","No channels available");for(let r=0;r<e.numberOfChannels;r++)if(!e.channelData[r])throw new A("INVALID_INPUT",`Channel ${r} data does not exist for averaging`);let a=new Float32Array(e.length);for(let r=0;r<e.length;r++){let o=0;for(let s=0;s<e.numberOfChannels;s++){let i=e.channelData[s];i&&r<i.length&&(o+=i[r]??0)}a[r]=o/e.numberOfChannels}return a}if(n<-1||n>=e.numberOfChannels)throw new A("INVALID_INPUT",`Invalid channel number: ${n}`);let t=e.channelData[n];if(!t)throw new A("INVALID_INPUT",`Channel ${n} data does not exist`);return t}function N(e,n,t){return n<0||n>=e.length?t:e[n]??t}function O(e){return e==null?!1:!isNaN(e)&&isFinite(e)}function f(e){return e==null||isNaN(e)||!isFinite(e)?0:e}function I(e,n=1){return e<=0||n<=0?-1/0:20*Math.log10(e/n)}function T(e,n){let t=(e.length-1)*n+1,a=new Float32Array(t);for(let r=0;r<e.length-1;r++){let o=f(e[r]??0),i=(f(e[r+1]??0)-o)/n;for(let l=0;l<n;l++)a[r*n+l]=o+i*l}return e.length>0&&(a[t-1]=f(e[e.length-1]??0)),a}function R(e,n){if(e.length<4)return T(e,n);let t=(e.length-1)*n+1,a=new Float32Array(t);for(let r=0;r<e.length-1;r++){let o=f(e[Math.max(0,r-1)]??0),s=f(e[r]??0),i=f(e[Math.min(e.length-1,r+1)]??0),l=f(e[Math.min(e.length-1,r+2)]??0);for(let m=0;m<n;m++){let u=m/n,h=u*u,p=h*u,b=.5*(2*s+(-o+i)*u+(2*o-5*s+4*i-l)*h+(-o+3*s-3*i+l)*p);a[r*n+m]=f(b)}}return e.length>0&&(a[t-1]=f(e[e.length-1]??0)),a}function G(e,n){let t=(e.length-1)*n+1,a=new Float32Array(t),r=o=>{if(Math.abs(o)<1e-10)return 1;let s=Math.PI*o;return Math.abs(o)<3?3*Math.sin(s/3)*Math.sin(s)/(s*s):0};for(let o=0;o<t;o++){let s=o/n,i=0;for(let l=-3;l<=3;l++){let m=Math.floor(s)+l;if(m>=0&&m<e.length){let u=f(e[m]??0),h=r(s-m);i+=u*h}}a[o]=f(i)}return a}function H(e,n={}){let{factor:t=4,interpolation:a="cubic"}=n;if(t<=1)return e;if(e.length===0)return new Float32Array(0);switch(a){case"linear":return T(e,t);case"cubic":return R(e,t);case"sinc":return G(e,t);default:return R(e,t)}}function E(e,n={}){if(e.length===0)return 0;let t=H(e,n),a=0;for(let r=0;r<t.length;r++){let o=Math.abs(f(t[r]??0));a=Math.max(a,o)}return a}function _(e,n,t=!1){let a=[],r=e.length;if(r<3)return a;for(let o=1;o<r-1;o++){let s=Math.abs(f(e[o]??0)),i=Math.abs(f(e[o-1]??0)),l=Math.abs(f(e[o+1]??0));if(s>i&&s>l&&s>n){let m={position:o,amplitude:s};t&&(m.prominence=K(e,o,s)),a.push(m)}}return a}function K(e,n,t){let a=t;for(let o=n-1;o>=0;o--){let s=Math.abs(f(e[o]??0));if(s>t)break;a=Math.min(a,s)}let r=t;for(let o=n+1;o<e.length;o++){let s=Math.abs(f(e[o]??0));if(s>t)break;r=Math.min(r,s)}return t-Math.max(a,r)}function Q(e,n={}){let{count:t=100,threshold:a=.1,channel:r=0,minDistance:o=Math.floor(e.sampleRate/100)}=n;if(t<=0)throw new A("INVALID_INPUT","\u30D4\u30FC\u30AF\u6570\u306F\u6B63\u306E\u6574\u6570\u3067\u3042\u308B\u5FC5\u8981\u304C\u3042\u308A\u307E\u3059");if(a<0||a>1)throw new A("INVALID_INPUT","\u95BE\u5024\u306F0\u304B\u30891\u306E\u7BC4\u56F2\u3067\u3042\u308B\u5FC5\u8981\u304C\u3042\u308A\u307E\u3059");let s=M(e,r);if(s.length===0)return{peaks:[],maxAmplitude:0,averageAmplitude:0};let i=_(s,a);if(i.length===0)return{peaks:[],maxAmplitude:0,averageAmplitude:0};i.sort((p,b)=>b.amplitude-p.amplitude);let l=[],m=[];for(let p of i){if(l.length>=t)break;let b=p.position-o,g=p.position+o;m.some(([F,w])=>!(g<F||b>w))||(l.push({position:p.position,time:p.position/e.sampleRate,amplitude:p.amplitude}),m.push([b,g]))}l.sort((p,b)=>p.position-b.position);let u=i.length>0?i[0]?.amplitude??0:0,h=i.length>0?i.reduce((p,b)=>p+b.amplitude,0)/i.length:0;return{peaks:l,maxAmplitude:u,averageAmplitude:h}}var C=-1/0;function Z(e,n={}){let t=typeof n=="number"?{channel:n,asDB:!1,reference:1,truePeak:!1,oversamplingFactor:4,interpolation:"cubic"}:{channel:n.channel??0,asDB:n.asDB??!1,reference:n.reference??1,truePeak:n.truePeak??!1,oversamplingFactor:n.oversamplingFactor??4,interpolation:n.interpolation??"cubic"},a=M(e,t.channel);if(a.length===0)return t.asDB?C:0;let r=0,o=0;for(let i=0;i<a.length;i++){let l=a[i]??0;O(l)&&(r+=l*l,o++)}if(o===0)return t.asDB?C:0;let s=Math.sqrt(r/o);return t.asDB?I(s,t.reference):s}function J(e,n={}){let t={channel:n.channel??0,asDB:n.asDB??!1,reference:n.reference??1,truePeak:n.truePeak??!1,oversamplingFactor:n.oversamplingFactor??4,interpolation:n.interpolation??"cubic"},a=M(e,t.channel);if(a.length===0)return t.asDB?C:0;let r;if(t.truePeak)r=E(a,{factor:t.oversamplingFactor,interpolation:t.interpolation});else{r=0;for(let o=0;o<a.length;o++){let s=a[o]??0;O(s)&&(r=Math.max(r,Math.abs(s)))}}return t.asDB?I(r,t.reference):r}function X(e,n=0){let t=M(e,n);if(t.length<2)return 0;let a=0;for(let r=1;r<t.length;r++){let o=f(t[r-1]??0),s=f(t[r]??0);(o>=0&&s<0||o<0&&s>=0)&&a++}return a/(t.length-1)}function Y(e,n={}){let{framesPerSecond:t=60,channel:a=0,method:r="rms"}=n,o=M(e,a),s=Math.ceil(e.duration*t),i=e.length>0?e.length:s>0?1:0,l=Math.min(s,i),m=l>0?Math.max(1,Math.floor(e.length/l)):0,u=[],h=0,p=0;for(let g=0;g<l;g++){let y=g*m,F=Math.min(y+m,o.length);if(F<=y){let D=u.length>0?N(u,u.length-1,{time:0,amplitude:0}).amplitude:0;u.push({time:(y+m/2)/e.sampleRate,amplitude:D});continue}let w=o.subarray(y,F),c;switch(r){case"peak":c=B(w);break;case"average":c=W(w);break;case"rms":default:c=q(w);break}let d=(y+(F-y)/2)/e.sampleRate;u.push({time:d,amplitude:c}),h=Math.max(h,c),p+=c}let b=l>0?p/l:0;return{waveform:u,maxAmplitude:h,averageAmplitude:b,frameCount:l,samplesPerFrame:m}}function q(e){if(e.length===0)return 0;let n=0;for(let t=0;t<e.length;t++){let a=f(e[t]??0);n+=a*a}return Math.sqrt(n/e.length)}function B(e){let n=0;for(let t=0;t<e.length;t++){let a=Math.abs(f(e[t]??0));n=Math.max(n,a)}return n}function W(e){if(e.length===0)return 0;let n=0;for(let t=0;t<e.length;t++)n+=Math.abs(f(e[t]??0));return n/e.length}function ee(e,n={}){let t=x(),{framesPerSecond:a=60,channel:r=0,method:o="rms",onProgress:s}=n;s?.(0,"Waveform\u89E3\u6790\u3092\u958B\u59CB");let i=M(e,r),l=Math.ceil(e.duration*a),m=e.length>0?e.length:l>0?1:0,u=Math.min(l,m),h=u>0?Math.max(1,Math.floor(e.length/u)):0;s?.(25,"\u30D5\u30EC\u30FC\u30E0\u8A2D\u5B9A\u5B8C\u4E86");let p=new Float32Array(u),b=new Float32Array(u),g=0,y=0;s?.(50,"\u632F\u5E45\u8A08\u7B97\u3092\u958B\u59CB");for(let c=0;c<u;c++){let d=c*h,D=Math.min(d+h,i.length);if(D<=d){let v=c>0?p[c-1]??0:0;p[c]=v,b[c]=(d+h/2)/e.sampleRate;continue}let k=i.subarray(d,D),P;switch(o){case"peak":P=B(k);break;case"average":P=W(k);break;case"rms":default:P=q(k);break}if(p[c]=P,b[c]=(d+(D-d)/2)/e.sampleRate,g=Math.max(g,P),y+=P,c%Math.max(1,Math.floor(u/20))===0){let v=50+c/u*45;s?.(Math.round(v),`\u30D5\u30EC\u30FC\u30E0 ${c+1}/${u} \u51E6\u7406\u4E2D`)}}let F=u>0?y/u:0,w=x()-t;return s?.(100,"\u51E6\u7406\u5B8C\u4E86"),{amplitudes:p,timestamps:b,frameCount:u,samplesPerFrame:h,framesPerSecond:a,maxAmplitude:g,averageAmplitude:F,sampleRate:e.sampleRate,duration:e.duration,processingTime:w}}function ne(e,n={}){let t=x(),{count:a=100,threshold:r=.1,channel:o=0,minDistance:s=Math.floor(e.sampleRate/100),onProgress:i}=n;if(i?.(0,"Peaks\u89E3\u6790\u3092\u958B\u59CB"),a<=0)throw new A("INVALID_INPUT","\u30D4\u30FC\u30AF\u6570\u306F\u6B63\u306E\u6574\u6570\u3067\u3042\u308B\u5FC5\u8981\u304C\u3042\u308A\u307E\u3059");if(r<0||r>1)throw new A("INVALID_INPUT","\u95BE\u5024\u306F0\u304B\u30891\u306E\u7BC4\u56F2\u3067\u3042\u308B\u5FC5\u8981\u304C\u3042\u308A\u307E\u3059");let l=M(e,o);if(l.length===0)return{positions:new Float32Array(0),amplitudes:new Float32Array(0),times:new Float32Array(0),maxAmplitude:0,averageAmplitude:0,count:0,sampleRate:e.sampleRate,duration:e.duration,processingTime:x()-t};i?.(25,"\u30D4\u30FC\u30AF\u5019\u88DC\u3092\u691C\u51FA\u4E2D");let m=_(l,r);if(m.length===0)return{positions:new Float32Array(0),amplitudes:new Float32Array(0),times:new Float32Array(0),maxAmplitude:0,averageAmplitude:0,count:0,sampleRate:e.sampleRate,duration:e.duration,processingTime:x()-t};i?.(50,"\u30D4\u30FC\u30AF\u3092\u30BD\u30FC\u30C8\u4E2D"),m.sort((c,d)=>d.amplitude-c.amplitude),i?.(75,"\u30D4\u30FC\u30AF\u3092\u9078\u629E\u4E2D");let u=[],h=[];for(let c of m){if(u.length>=a)break;let d=c.position-s,D=c.position+s;h.some(([P,v])=>!(D<P||d>v))||(u.push({position:c.position,time:c.position/e.sampleRate,amplitude:c.amplitude}),h.push([d,D]))}u.sort((c,d)=>c.position-d.position);let p=new Float32Array(u.length),b=new Float32Array(u.length),g=new Float32Array(u.length);for(let c=0;c<u.length;c++){let d=u[c];d&&(p[c]=d.position,b[c]=d.amplitude,g[c]=d.time)}let y=m.length>0?m[0]?.amplitude??0:0,F=m.length>0?m.reduce((c,d)=>c+d.amplitude,0)/m.length:0,w=x()-t;return i?.(100,"\u51E6\u7406\u5B8C\u4E86"),{positions:p,amplitudes:b,times:g,maxAmplitude:y,averageAmplitude:F,count:u.length,sampleRate:e.sampleRate,duration:e.duration,processingTime:w}}function te(e,n={}){let t=x(),{channel:a=0,asDB:r=!1,reference:o=1,onProgress:s}=n;s?.(0,"RMS\u89E3\u6790\u3092\u958B\u59CB");let i=M(e,a);if(i.length===0)return{value:r?-1/0:0,channel:a,sampleRate:e.sampleRate,duration:e.duration,processingTime:x()-t};s?.(50,"RMS\u5024\u3092\u8A08\u7B97\u4E2D");let l=0;for(let h=0;h<i.length;h++){let p=i[h]??0;if(O(p)){let b=f(p);l+=b*b}}let m=Math.sqrt(l/i.length),u=x()-t;return s?.(100,"\u51E6\u7406\u5B8C\u4E86"),r?{value:I(m,o),channel:a,sampleRate:e.sampleRate,duration:e.duration,processingTime:u}:{value:m,valueDB:I(m,o),channel:a,sampleRate:e.sampleRate,duration:e.duration,processingTime:u}}