web-wake-word
Version:
A web package for keyword detection
2 lines • 24.7 kB
JavaScript
/*! For license information please see 189023b75b9da6dffccf.js.LICENSE.txt */
function _typeof(e){return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},_typeof(e)}function _createForOfIteratorHelper(e,t){var r="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(!r){if(Array.isArray(e)||(r=_unsupportedIterableToArray(e))||t&&e&&"number"==typeof e.length){r&&(e=r);var n=0,o=function(){};return{s:o,n:function(){return n>=e.length?{done:!0}:{done:!1,value:e[n++]}},e:function(e){throw e},f:o}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var a,i=!0,s=!1;return{s:function(){r=r.call(e)},n:function(){var e=r.next();return i=e.done,e},e:function(e){s=!0,a=e},f:function(){try{i||null==r.return||r.return()}finally{if(s)throw a}}}}function _toConsumableArray(e){return _arrayWithoutHoles(e)||_iterableToArray(e)||_unsupportedIterableToArray(e)||_nonIterableSpread()}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _unsupportedIterableToArray(e,t){if(e){if("string"==typeof e)return _arrayLikeToArray(e,t);var r={}.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?_arrayLikeToArray(e,t):void 0}}function _iterableToArray(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}function _arrayWithoutHoles(e){if(Array.isArray(e))return _arrayLikeToArray(e)}function _arrayLikeToArray(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=Array(t);r<t;r++)n[r]=e[r];return n}function _regeneratorRuntime(){"use strict";_regeneratorRuntime=function(){return t};var e,t={},r=Object.prototype,n=r.hasOwnProperty,o=Object.defineProperty||function(e,t,r){e[t]=r.value},a="function"==typeof Symbol?Symbol:{},i=a.iterator||"@@iterator",s=a.asyncIterator||"@@asyncIterator",u=a.toStringTag||"@@toStringTag";function f(e,t,r){return Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}),e[t]}try{f({},"")}catch(e){f=function(e,t,r){return e[t]=r}}function c(e,t,r,n){var a=t&&t.prototype instanceof g?t:g,i=Object.create(a.prototype),s=new F(n||[]);return o(i,"_invoke",{value:x(e,r,s)}),i}function h(e,t,r){try{return{type:"normal",arg:e.call(t,r)}}catch(e){return{type:"throw",arg:e}}}t.wrap=c;var l="suspendedStart",d="suspendedYield",m="executing",p="completed",y={};function g(){}function v(){}function b(){}var w={};f(w,i,(function(){return this}));var B=Object.getPrototypeOf,_=B&&B(B(S([])));_&&_!==r&&n.call(_,i)&&(w=_);var k=b.prototype=g.prototype=Object.create(w);function L(e){["next","throw","return"].forEach((function(t){f(e,t,(function(e){return this._invoke(t,e)}))}))}function A(e,t){function r(o,a,i,s){var u=h(e[o],e,a);if("throw"!==u.type){var f=u.arg,c=f.value;return c&&"object"==_typeof(c)&&n.call(c,"__await")?t.resolve(c.__await).then((function(e){r("next",e,i,s)}),(function(e){r("throw",e,i,s)})):t.resolve(c).then((function(e){f.value=e,i(f)}),(function(e){return r("throw",e,i,s)}))}s(u.arg)}var a;o(this,"_invoke",{value:function(e,n){function o(){return new t((function(t,o){r(e,n,t,o)}))}return a=a?a.then(o,o):o()}})}function x(t,r,n){var o=l;return function(a,i){if(o===m)throw Error("Generator is already running");if(o===p){if("throw"===a)throw i;return{value:e,done:!0}}for(n.method=a,n.arg=i;;){var s=n.delegate;if(s){var u=T(s,n);if(u){if(u===y)continue;return u}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if(o===l)throw o=p,n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);o=m;var f=h(t,r,n);if("normal"===f.type){if(o=n.done?p:d,f.arg===y)continue;return{value:f.arg,done:n.done}}"throw"===f.type&&(o=p,n.method="throw",n.arg=f.arg)}}}function T(t,r){var n=r.method,o=t.iterator[n];if(o===e)return r.delegate=null,"throw"===n&&t.iterator.return&&(r.method="return",r.arg=e,T(t,r),"throw"===r.method)||"return"!==n&&(r.method="throw",r.arg=new TypeError("The iterator does not provide a '"+n+"' method")),y;var a=h(o,t.iterator,r.arg);if("throw"===a.type)return r.method="throw",r.arg=a.arg,r.delegate=null,y;var i=a.arg;return i?i.done?(r[t.resultName]=i.value,r.next=t.nextLoc,"return"!==r.method&&(r.method="next",r.arg=e),r.delegate=null,y):i:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,y)}function E(e){var t={tryLoc:e[0]};1 in e&&(t.catchLoc=e[1]),2 in e&&(t.finallyLoc=e[2],t.afterLoc=e[3]),this.tryEntries.push(t)}function P(e){var t=e.completion||{};t.type="normal",delete t.arg,e.completion=t}function F(e){this.tryEntries=[{tryLoc:"root"}],e.forEach(E,this),this.reset(!0)}function S(t){if(t||""===t){var r=t[i];if(r)return r.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var o=-1,a=function r(){for(;++o<t.length;)if(n.call(t,o))return r.value=t[o],r.done=!1,r;return r.value=e,r.done=!0,r};return a.next=a}}throw new TypeError(_typeof(t)+" is not iterable")}return v.prototype=b,o(k,"constructor",{value:b,configurable:!0}),o(b,"constructor",{value:v,configurable:!0}),v.displayName=f(b,u,"GeneratorFunction"),t.isGeneratorFunction=function(e){var t="function"==typeof e&&e.constructor;return!!t&&(t===v||"GeneratorFunction"===(t.displayName||t.name))},t.mark=function(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,b):(e.__proto__=b,f(e,u,"GeneratorFunction")),e.prototype=Object.create(k),e},t.awrap=function(e){return{__await:e}},L(A.prototype),f(A.prototype,s,(function(){return this})),t.AsyncIterator=A,t.async=function(e,r,n,o,a){void 0===a&&(a=Promise);var i=new A(c(e,r,n,o),a);return t.isGeneratorFunction(r)?i:i.next().then((function(e){return e.done?e.value:i.next()}))},L(k),f(k,u,"Generator"),f(k,i,(function(){return this})),f(k,"toString",(function(){return"[object Generator]"})),t.keys=function(e){var t=Object(e),r=[];for(var n in t)r.push(n);return r.reverse(),function e(){for(;r.length;){var n=r.pop();if(n in t)return e.value=n,e.done=!1,e}return e.done=!0,e}},t.values=S,F.prototype={constructor:F,reset:function(t){if(this.prev=0,this.next=0,this.sent=this._sent=e,this.done=!1,this.delegate=null,this.method="next",this.arg=e,this.tryEntries.forEach(P),!t)for(var r in this)"t"===r.charAt(0)&&n.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=e)},stop:function(){this.done=!0;var e=this.tryEntries[0].completion;if("throw"===e.type)throw e.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var r=this;function o(n,o){return s.type="throw",s.arg=t,r.next=n,o&&(r.method="next",r.arg=e),!!o}for(var a=this.tryEntries.length-1;a>=0;--a){var i=this.tryEntries[a],s=i.completion;if("root"===i.tryLoc)return o("end");if(i.tryLoc<=this.prev){var u=n.call(i,"catchLoc"),f=n.call(i,"finallyLoc");if(u&&f){if(this.prev<i.catchLoc)return o(i.catchLoc,!0);if(this.prev<i.finallyLoc)return o(i.finallyLoc)}else if(u){if(this.prev<i.catchLoc)return o(i.catchLoc,!0)}else{if(!f)throw Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return o(i.finallyLoc)}}}},abrupt:function(e,t){for(var r=this.tryEntries.length-1;r>=0;--r){var o=this.tryEntries[r];if(o.tryLoc<=this.prev&&n.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var a=o;break}}a&&("break"===e||"continue"===e)&&a.tryLoc<=t&&t<=a.finallyLoc&&(a=null);var i=a?a.completion:{};return i.type=e,i.arg=t,a?(this.method="next",this.next=a.finallyLoc,y):this.complete(i)},complete:function(e,t){if("throw"===e.type)throw e.arg;return"break"===e.type||"continue"===e.type?this.next=e.arg:"return"===e.type?(this.rval=this.arg=e.arg,this.method="return",this.next="end"):"normal"===e.type&&t&&(this.next=t),y},finish:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var r=this.tryEntries[t];if(r.finallyLoc===e)return this.complete(r.completion,r.afterLoc),P(r),y}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var r=this.tryEntries[t];if(r.tryLoc===e){var n=r.completion;if("throw"===n.type){var o=n.arg;P(r)}return o}}throw Error("illegal catch attempt")},delegateYield:function(t,r,n){return this.delegate={iterator:S(t),resultName:r,nextLoc:n},"next"===this.method&&(this.arg=e),y}},t}function asyncGeneratorStep(e,t,r,n,o,a,i){try{var s=e[a](i),u=s.value}catch(e){return void r(e)}s.done?t(u):Promise.resolve(u).then(n,o)}function _asyncToGenerator(e){return function(){var t=this,r=arguments;return new Promise((function(n,o){var a=e.apply(t,r);function i(e){asyncGeneratorStep(a,n,o,i,s,"next",e)}function s(e){asyncGeneratorStep(a,n,o,i,s,"throw",e)}i(void 0)}))}}function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function _defineProperties(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,_toPropertyKey(n.key),n)}}function _createClass(e,t,r){return t&&_defineProperties(e.prototype,t),r&&_defineProperties(e,r),Object.defineProperty(e,"prototype",{writable:!1}),e}function _toPropertyKey(e){var t=_toPrimitive(e,"string");return"symbol"==_typeof(t)?t:t+""}function _toPrimitive(e,t){if("object"!=_typeof(e)||!e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,t||"default");if("object"!=_typeof(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}import{AudioProcessor}from"./audioProcessor.js";import{ModelLoader}from"./modelLoader.js";import{LicenseManager}from"./licenseManager.js";import{getWasmPaths}from"./getSessionOptions.js";import{ort}from"./onnx-runtime-loader.js";var isStaticEnv=!1;export var KeywordDetector=function(){return _createClass((function e(t,r,n,o,a,i,s){var u,f,c;_classCallCheck(this,e);var h="undefined"!=typeof process&&null!==(u=process.env)&&void 0!==u&&u.MODEL_FOLDER_PATH?process.env.MODEL_FOLDER_PATH:"./models/",l="undefined"!=typeof process&&null!==(f=process.env)&&void 0!==f&&f.WASM_BASE_PATH?process.env.WASM_BASE_PATH:"./",d="undefined"!=typeof process&&null!==(c=process.env)&&void 0!==c&&c.AUDIO_WORKLET_PATH?process.env.AUDIO_WORKLET_PATH:"./";try{new URL("keywordDetection.js",import.meta.url),console.log("Dynamic path environment detected")}catch(e){isStaticEnv=!0,console.log("Static (such as next.js) path environment detected")}this.modelsFolderPath=isStaticEnv?h:t,this.licenseManager=new LicenseManager,Array.isArray(r)?(n&&(ort.env.wasm.wasmPaths=isStaticEnv?l+"ort-wasm-simd.wasm":getWasmPaths(n),ort.env.wasm.numThreads=1,ort.env.logLevel="verbose"),o&&(this.audioWorkletPath=isStaticEnv?d:o),this.modelParamsArr=r.map((function(e){return{modelToUse:e.modelToUse,threshold:e.threshold,bufferCount:e.bufferCount,onKeywordDetected:e.onKeywordDetected,keyThreshold:e.threshold,fakeThreshold:e.threshold-.01,concurentPrediction:0}})),this.modelLoader=n?new ModelLoader(this.modelParamsArr,isStaticEnv?d:n):new ModelLoader(this.modelParamsArr),this.modelToUse=this.modelParamsArr):(this.keyThreshold=this.threshold=n,this.bufferCount=o,this.onKeywordDetected=a,this.fakeThreshold=this.keyThreshold-.01,this.concurentPrediction=0,this.keyBufferCnt=o,this.modelsFolderPath=isStaticEnv?l:t,this.modelToUse=r,isStaticEnv&&(i=l,s=d),i&&(this.wasmBasePath=i),s&&(this.audioWorkletPath=s),this.modelLoader=i?new ModelLoader(i):new ModelLoader),this.rawDataBuffer=[],this.rawDataRemainder=[],this.featureBuffer=[],this.melspectrogramBuffer=[],this.MAX_MEL_ROWS=970,this.chunks=[],this.audioBuffer=[],this.bufferPosition=0,this.audioQueue=[],this.MAX_AUDIO_SAMPLES=32e3,this.sampleRate=16e3,this.frameLength=1280,this.nFeatureFrames=28,this.initialFramesToIgnore=4,this.featureBufferMaxLen=120,this.rawBufferMaxLen=10*this.sampleRate,this.randomDataSize=4*this.sampleRate,this.audioProcessor=null,this.isListening=!1,this.isLicensed=!1}),[{key:"init",value:(i=_asyncToGenerator(_regeneratorRuntime().mark((function e(){var t,r,n;return _regeneratorRuntime().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return console.log("Init()"),e.next=3,this.modelLoader.loadModels(this.modelsFolderPath,this.modelToUse,this.sampleRate);case 3:for(this.nFeatureFrames=e.sent,this.initialFramesToIgnore=4,this.resetBuffers(),t=new Float32Array(this.randomDataSize),r=0;r<this.randomDataSize;r++)t[r]=2e3*Math.random()-1e3;return e.next=10,this.modelLoader.getEmbeddings(t);case 10:n=e.sent,this.featureBuffer=n;case 12:case"end":return e.stop()}}),e,this)}))),function(){return i.apply(this,arguments)})},{key:"resetBuffers",value:function(){this.rawDataBuffer=[],this.melspectrogramBuffer=[];for(var e=0;e<76;e++){var t=new Float32Array(32).fill(1);this.melspectrogramBuffer.push(t)}this.printMelspectrogramBuffer(),this.rawDataRemainder=[]}},{key:"printMelspectrogramBuffer",value:function(){this.melspectrogramBuffer.length>0&&this.melspectrogramBuffer[0].length}},{key:"setLicense",value:(a=_asyncToGenerator(_regeneratorRuntime().mark((function e(t){return _regeneratorRuntime().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this.licenseManager.isLicenseValid(t);case 2:return this.isLicensed=e.sent,e.abrupt("return",this.isLicensed);case 4:case"end":return e.stop()}}),e,this)}))),function(e){return a.apply(this,arguments)})},{key:"startListening",value:function(){var e=this;if(this.isLicensed&&!this.isListening){this.resetBuffers();for(var t=new Float32Array(this.randomDataSize),r=0;r<this.randomDataSize;r++)t[r]=2e3*Math.random()-1e3;this.modelLoader&&this.modelLoader.embeddingSession?(this.modelLoader.getEmbeddings(t).then((function(t){e.featureBuffer=t,e.audioProcessor=new AudioProcessor((function(t){e.processAudioFrame(t)}),e.frameLength,e.sampleRate,e.audioWorkletPath),e.audioProcessor.start()})),this.isListening=!0):console.error("Embedding session not loaded!!!.\n Please check if you called 'await detector.init()'? make sure the call uses await!")}}},{key:"stopListening",value:function(){this.isListening&&(this.isListening=!1,this.audioProcessor&&this.audioProcessor.stop())}},{key:"bufferAudioFrames",value:function(e){var t;(t=this.audioBuffer).push.apply(t,_toConsumableArray(e))}},{key:"bufferAudioFramesSize",value:function(){return this.audioBuffer.length}},{key:"getBufferAudioFrames",value:function(){return this.audioBuffer}},{key:"bufferAudioFramesClear",value:function(){this.audioBuffer=[]}},{key:"updateFeatureBuffer",value:function(e){for(var t=this.featureBuffer.length,r=t+e.length,n=0;n<this.featureBuffer.length;n++)if(this.featureBuffer[0].length!=this.featureBuffer[n].length)throw new Error("this.featureBuffer[i].length wrong!!! ".concat(this.featureBuffer[n].length));if(96!==e[0].length)throw new Error("Expected embedding of size 96, but got ".concat(e[0].length));for(var o=new Array(r),a=0;a<this.featureBuffer.length;a++){o[a]=new Array(this.featureBuffer[0].length);for(var i=0;i<this.featureBuffer[0].length;i++)o[a][i]=this.featureBuffer[a][i]}o[t]=new Array(this.featureBuffer[0].length);for(var s=0;s<o.length-1;s++)if(o[0].length!=o[s].length)throw new Error("newFeatureBuffer length wrong!!! ".concat(o[s].length));for(var u=0;u<e.length;u++)for(var f=0;f<e[0].length;f++)o[t+u][f]=e[u][f];this.featureBuffer=o;for(var c=0;c<this.featureBuffer.length;c++)if(this.featureBuffer[0].length!=this.featureBuffer[c].length)throw new Error("update to new this.featureBuffer[i].length wrong!!! ".concat(this.featureBuffer[c].length));if(this.featureBuffer.length>this.featureBufferMaxLen){var h=this.featureBuffer.slice(this.featureBuffer.length-this.featureBufferMaxLen);this.featureBuffer=h}for(var l=0;l<this.featureBuffer.length;l++)if(this.featureBuffer[0].length!=this.featureBuffer[l].length)throw new Error("Trimmed this.featureBuffer[i].length wrong!!! ".concat(this.featureBuffer[l].length))}},{key:"processAudioFrame",value:(o=_asyncToGenerator(_regeneratorRuntime().mark((function e(t){var r,n,o,a,i,s,u;return _regeneratorRuntime().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(e.prev=0,this.initialFramesToIgnore>=0&&this.initialFramesToIgnore--,t instanceof Float32Array||(t=new Float32Array(t)),this.storeFrame(t),this.bufferAudioFrames(t),!(this.bufferAudioFramesSize()<this.frameLength)){e.next=7;break}return e.abrupt("return");case 7:return e.next=9,this.detectFromMicrophone(this.getBufferAudioFrames());case 9:return this.bufferAudioFramesClear(),e.next=12,this.prepareFlattenBuffer();case 12:r=e.sent,n=Array.isArray(this.modelToUse)?this.modelToUse:[{modelToUse:this.modelToUse,threshold:this.threshold,bufferCount:this.bufferCount,onKeywordDetected:this.onKeywordDetected,keyThreshold:this.threshold,fakeThreshold:this.threshold-.01,concurentPrediction:this.concurentPrediction}],o=_createForOfIteratorHelper(n),e.prev=15,o.s();case 17:if((a=o.n()).done){e.next=38;break}return i=a.value,e.next=21,this.modelLoader.predictKeyword(r,i.modelToUse);case 21:if(!((s=e.sent)>i.fakeThreshold)){e.next=35;break}if(!(s<i.keyThreshold)){e.next=27;break}i.concurentPrediction=0,e.next=33;break;case 27:if(i.concurentPrediction++,!(i.concurentPrediction>=i.bufferCount)){e.next=33;break}return u={prediction:s,cntBuf:i.concurentPrediction,model:i.modelToUse},e.next=32,i.onKeywordDetected(u);case 32:i.concurentPrediction=0;case 33:e.next=36;break;case 35:i.concurentPrediction=0;case 36:e.next=17;break;case 38:e.next=43;break;case 40:e.prev=40,e.t0=e.catch(15),o.e(e.t0);case 43:return e.prev=43,o.f(),e.finish(43);case 46:Array.isArray(this.modelToUse)||(this.concurentPrediction=n[0].concurentPrediction),e.next=53;break;case 49:e.prev=49,e.t1=e.catch(0),this.stopListening(),console.error("DaVoice WakeWord: Exception in processAudioFrame:",e.t1);case 53:case"end":return e.stop()}}),e,this,[[0,49],[15,40,43,46]])}))),function(e){return o.apply(this,arguments)})},{key:"storeFrame",value:function(e){var t,r=_createForOfIteratorHelper(e);try{for(r.s();!(t=r.n()).done;){var n=t.value;this.audioQueue.push(n)}}catch(e){r.e(e)}finally{r.f()}for(;this.audioQueue.length>this.MAX_AUDIO_SAMPLES;)this.audioQueue.shift()}},{key:"flushBufferToWav",value:function(e){var t=this.createWavBlob(this.audioBufferToSave),r=URL.createObjectURL(t),n=document.createElement("a");n.style.display="none",n.href=r,n.download=e,document.body.appendChild(n),n.click(),URL.revokeObjectURL(r)}},{key:"createWavBlob",value:function(e){return new Blob([],{type:"audio/wav"})}},{key:"detectFromMicrophone",value:(n=_asyncToGenerator(_regeneratorRuntime().mark((function e(t){var r,n,o;return _regeneratorRuntime().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return this.bufferRawData(t),e.next=3,this.streamingMelspectrogram(t.length);case 3:return r=this.getMelNumberOfRows(),("number"!=typeof(n=this.getMelspecSubArray(r-76,r))[0][0]||isNaN(n[0][0]))&&console.error("melspecSlice[0][0] Invalid number :",n[0][0]),e.next=8,this.getEmbeddingsFromMelspectrogram(n);case 8:o=e.sent,this.updateFeatureBuffer(o);case 10:case"end":return e.stop()}}),e,this)}))),function(e){return n.apply(this,arguments)})},{key:"bufferRawData",value:function(e){var t;(t=this.rawDataBuffer).push.apply(t,_toConsumableArray(e)),this.rawDataBuffer.length>this.rawBufferMaxLen&&this.rawDataBuffer.splice(0,this.rawDataBuffer.length-this.rawBufferMaxLen)}},{key:"streamingMelspectrogram",value:(r=_asyncToGenerator(_regeneratorRuntime().mark((function e(t){var r,n,o,a,i,s;return _regeneratorRuntime().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(this.rawDataBuffer.length<400)){e.next=3;break}throw r="The number of input frames must be at least 400 samples @ 16kHz (25 ms)!"+this.rawDataBuffer.length,new Error(r);case 3:return n=Math.max(0,this.rawDataBuffer.length-t-480),o=this.rawDataBuffer.slice(n),e.next=7,this.modelLoader.getMelspectrogramShort(o);case 7:for(a=e.sent,this.updateMelspectrogramBuffer(a),i=0;i<this.melspectrogramBuffer.length;i++)for(s=0;s<this.melspectrogramBuffer[i].length;s++)("number"!=typeof this.melspectrogramBuffer[i][s]||isNaN(this.melspectrogramBuffer[i][s]))&&console.error("this.melspectrogramBuffer Invalid number at position [".concat(i,"][").concat(s,"]:"),this.melspectrogramBuffer[i][s]);case 11:case"end":return e.stop()}}),e,this)}))),function(e){return r.apply(this,arguments)})},{key:"updateMelspectrogramBuffer",value:function(e){var t,r=_createForOfIteratorHelper(e);try{for(r.s();!(t=r.n()).done;){var n=t.value;this.melspectrogramBuffer.push(n),this.melspectrogramBuffer.length>this.MAX_MEL_ROWS&&this.melspectrogramBuffer.shift()}}catch(e){r.e(e)}finally{r.f()}}},{key:"appendMelspec",value:function(e){var t,r=_createForOfIteratorHelper(e);try{for(r.s();!(t=r.n()).done;){var n=t.value;this.melspectrogramBuffer.push(n)}}catch(e){r.e(e)}finally{r.f()}for(;this.melspectrogramBuffer.length>this.MAX_MEL_ROWS;)this.melspectrogramBuffer.shift()}},{key:"getMelNumberOfRows",value:function(){return this.melspectrogramBuffer.length}},{key:"flattenSubArray",value:function(e){return e.map((function(e){return e.map((function(e){return Array.isArray(e)?e[0]:("number"==typeof e||console.warn("Unexpected cell type:",e),e)}))}))}},{key:"getMelspecSubArray",value:function(e,t){("number"!=typeof this.melspectrogramBuffer[0][0]||isNaN(this.melspectrogramBuffer[0][0]))&&console.error("this.melspectrogramSlice[0][0] Invalid number :",this.melspectrogramBuffer[0][0]),e<0&&(console.error("startRow (".concat(e,") is less than 0. Adjusting to 0.")),e=0),t>this.melspectrogramBuffer.length&&(console.error("endRow (".concat(t,") exceeds buffer length. Adjusting to ").concat(this.melspectrogramBuffer.length,".")),t=this.melspectrogramBuffer.length);var r=this.melspectrogramBuffer.slice(e,t);return("number"!=typeof r[0][0]||isNaN(r[0][0]))&&console.error("subArray[0][0] Invalid number :",r[0][0]),r}},{key:"Myflatten4D",value:function(e){for(var t=[],r=0;r<e.length;r++)for(var n=0;n<e[r].length;n++)for(var o=0;o<e[r][n].length;o++)for(var a=0;a<e[r][n][o].length;a++)t.push(e[r][n][o][a]);return t}},{key:"getEmbeddingsFromMelspectrogram",value:(t=_asyncToGenerator(_regeneratorRuntime().mark((function e(t){var r,n,o,a,i,s,u,f,c,h,l,d,m,p;return _regeneratorRuntime().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:for(r=0;r<t.length;r++)for(n=0;n<t[r].length;n++)("number"!=typeof t[r][n]||isNaN(t[r][n]))&&console.error("Invalid number at position [".concat(r,"][").concat(n,"]:"),t[r][n]);for(o=t.length*t[0].length,a=new Float32Array(o),i=0,s=0;s<t.length;s++)for(u=0;u<t[s].length;u++)a[i++]=t[s][u];if(2432===a.length){e.next=8;break}throw new Error("Flattened melspectrogram length should be ".concat(2432,", but got ").concat(a.length));case 8:return f=[1,76,32,1],c=new ort.Tensor("float32",a,f),e.next=12,this.modelLoader.embeddingSession.run({input_1:c});case 12:for(h=e.sent,l=h[Object.keys(h)[0]].data,h[Object.keys(h)[0]].dims,d=[],m=0;m<l.length/96;m++)for(d[m]=[],p=0;p<96;p++)d[m][p]=l[96*m+p];return e.abrupt("return",d);case 18:case"end":return e.stop()}}),e,this)}))),function(e){return t.apply(this,arguments)})},{key:"hasUndefined",value:function(e,t){for(var r=0;r<e.length;r++)if("number"!=typeof e[r]||isNaN(e[r]))throw new Error("hasUndefined() buffer[i] ".concat(e[r]," error_message = ").concat(t))}},{key:"prepareFlattenBuffer",value:(e=_asyncToGenerator(_regeneratorRuntime().mark((function e(){var t,r,n,o,a;return _regeneratorRuntime().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(!(this.featureBuffer.length<this.nFeatureFrames)){e.next=3;break}return console.error("Insufficient feature buffer length: ".concat(this.featureBuffer.length)),e.abrupt("return",0);case 3:t=this.getFeatures(),r=new Float32Array(t.length*t[0].length),n=0,o=0;case 7:if(!(o<t.length)){e.next=20;break}this.hasUndefined(t[o],"feature buffer"),a=0;case 10:if(!(a<t[o].length)){e.next=17;break}if(t[o].length==t[0].length){e.next=13;break}throw new Error("features[i].length != features[0].length: ".concat(t[0].length," ").concat(t[o].length));case 13:r[n++]=t[o][a];case 14:a++,e.next=10;break;case 17:o++,e.next=7;break;case 20:return this.hasUndefined(r,"predictFromBuffer() flattenedFeatures"),e.abrupt("return",r);case 22:case"end":return e.stop()}}),e,this)}))),function(){return e.apply(this,arguments)})},{key:"getFeatures",value:function(){var e=this.featureBuffer.length-this.nFeatureFrames;return this.featureBuffer.slice(e,e+this.nFeatureFrames)}}]);var e,t,r,n,o,a,i}();