web-wake-word
Version:
A web package for keyword detection
2 lines • 19.1 kB
JavaScript
/*! For license information please see 4fa4e5a2931f19769a7a.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 _slicedToArray(e,t){return _arrayWithHoles(e)||_iterableToArrayLimit(e,t)||_unsupportedIterableToArray(e,t)||_nonIterableRest()}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure 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 _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 _iterableToArrayLimit(e,t){var r=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=r){var n,o,a,i,s=[],u=!0,c=!1;try{if(a=(r=r.call(e)).next,0===t){if(Object(r)!==r)return;u=!1}else for(;!(u=(n=a.call(r)).done)&&(s.push(n.value),s.length!==t);u=!0);}catch(e){c=!0,o=e}finally{try{if(!u&&null!=r.return&&(i=r.return(),Object(i)!==i))return}finally{if(c)throw o}}return s}}function _arrayWithHoles(e){if(Array.isArray(e))return e}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 c(e,t,r){return Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}),e[t]}try{c({},"")}catch(e){c=function(e,t,r){return e[t]=r}}function l(e,t,r,n){var a=t&&t.prototype instanceof v?t:v,i=Object.create(a.prototype),s=new F(n||[]);return o(i,"_invoke",{value:L(e,r,s)}),i}function f(e,t,r){try{return{type:"normal",arg:e.call(t,r)}}catch(e){return{type:"throw",arg:e}}}t.wrap=l;var h="suspendedStart",p="suspendedYield",d="executing",y="completed",m={};function v(){}function g(){}function w(){}var _={};c(_,i,(function(){return this}));var b=Object.getPrototypeOf,x=b&&b(b(O([])));x&&x!==r&&n.call(x,i)&&(_=x);var S=w.prototype=v.prototype=Object.create(_);function k(e){["next","throw","return"].forEach((function(t){c(e,t,(function(e){return this._invoke(t,e)}))}))}function T(e,t){function r(o,a,i,s){var u=f(e[o],e,a);if("throw"!==u.type){var c=u.arg,l=c.value;return l&&"object"==_typeof(l)&&n.call(l,"__await")?t.resolve(l.__await).then((function(e){r("next",e,i,s)}),(function(e){r("throw",e,i,s)})):t.resolve(l).then((function(e){c.value=e,i(c)}),(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 L(t,r,n){var o=h;return function(a,i){if(o===d)throw Error("Generator is already running");if(o===y){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=E(s,n);if(u){if(u===m)continue;return u}}if("next"===n.method)n.sent=n._sent=n.arg;else if("throw"===n.method){if(o===h)throw o=y,n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);o=d;var c=f(t,r,n);if("normal"===c.type){if(o=n.done?y:p,c.arg===m)continue;return{value:c.arg,done:n.done}}"throw"===c.type&&(o=y,n.method="throw",n.arg=c.arg)}}}function E(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,E(t,r),"throw"===r.method)||"return"!==n&&(r.method="throw",r.arg=new TypeError("The iterator does not provide a '"+n+"' method")),m;var a=f(o,t.iterator,r.arg);if("throw"===a.type)return r.method="throw",r.arg=a.arg,r.delegate=null,m;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,m):i:(r.method="throw",r.arg=new TypeError("iterator result is not an object"),r.delegate=null,m)}function P(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 A(e){var t=e.completion||{};t.type="normal",delete t.arg,e.completion=t}function F(e){this.tryEntries=[{tryLoc:"root"}],e.forEach(P,this),this.reset(!0)}function O(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 g.prototype=w,o(S,"constructor",{value:w,configurable:!0}),o(w,"constructor",{value:g,configurable:!0}),g.displayName=c(w,u,"GeneratorFunction"),t.isGeneratorFunction=function(e){var t="function"==typeof e&&e.constructor;return!!t&&(t===g||"GeneratorFunction"===(t.displayName||t.name))},t.mark=function(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,w):(e.__proto__=w,c(e,u,"GeneratorFunction")),e.prototype=Object.create(S),e},t.awrap=function(e){return{__await:e}},k(T.prototype),c(T.prototype,s,(function(){return this})),t.AsyncIterator=T,t.async=function(e,r,n,o,a){void 0===a&&(a=Promise);var i=new T(l(e,r,n,o),a);return t.isGeneratorFunction(r)?i:i.next().then((function(e){return e.done?e.value:i.next()}))},k(S),c(S,u,"Generator"),c(S,i,(function(){return this})),c(S,"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=O,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(A),!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"),c=n.call(i,"finallyLoc");if(u&&c){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(!c)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,m):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),m},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),A(r),m}},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;A(r)}return o}}throw Error("illegal catch attempt")},delegateYield:function(t,r,n){return this.delegate={iterator:O(t),resultName:r,nextLoc:n},"next"===this.method&&(this.arg=e),m}},t}function _defineProperty(e,t,r){return(t=_toPropertyKey(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}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{ort}from"./onnx-runtime-loader.js";import{generateMelSpectrogram,extractWindows,expandDims,flatten3D,flatten4D}from"./utils.js";import path from"path-browserify";var _require=require("./getSessionOptions"),getSessionOptions=_require.getSessionOptions,getWasmPaths=_require.getWasmPaths,logMemory=function(e){var t=(performance.memory.usedJSHeapSize/1024/1024).toFixed(1);console.log(e+": JS heap used: ".concat(t," MB"))},modelSuffix=".onnx";export var ModelLoader=function(){return _createClass((function e(t,r){var n,o,a;_classCallCheck(this,e),"undefined"!=typeof process&&null!==(n=process.env)&&void 0!==n&&n.MODEL_FOLDER_PATH&&process.env.MODEL_FOLDER_PATH;var i="undefined"!=typeof process&&null!==(o=process.env)&&void 0!==o&&o.WASM_BASE_PATH?process.env.WASM_BASE_PATH:"./",s=("undefined"!=typeof process&&null!==(a=process.env)&&void 0!==a&&a.AUDIO_WORKLET_PATH&&process.env.AUDIO_WORKLET_PATH,!1);try{new URL("modelLoader.js",import.meta.url),console.log("Dynamic path environment detected")}catch(e){s=!0,console.log("Static (such as next.js) path environment detected")}Array.isArray(t)?r&&(ort.env.wasm.wasmPaths=s?i+"ort-wasm-simd.wasm":getWasmPaths(r)):t&&(ort.env.wasm.wasmPaths=s?i+"ort-wasm-simd.wasm":getWasmPaths(r)),this.keywordSessions=null,this.melSpecSession=null,this.embeddingSession=null}),[{key:"getMelspectrogram",value:(s=_asyncToGenerator(_regeneratorRuntime().mark((function e(t){var r,n,o,a,i,s,u,c,l,f,h,p,d,y;return _regeneratorRuntime().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t instanceof Float32Array||(t=new Float32Array(t)),u=[1,t.length],c=new ort.Tensor("float32",t,u),l=_defineProperty({},this.melSpecSession.inputNames[0],c),e.prev=4,e.next=7,this.melSpecSession.run(l);case 7:f=e.sent,e.next=14;break;case 10:return e.prev=10,e.t0=e.catch(4),console.error("Failed to run MelSpectrogram ONNX session:",e.t0),e.abrupt("return",[]);case 14:return h=f.output.data,p=f.output.dims,h.length,void 0!==(void 0!==(null===(r=h[0])||void 0===r?void 0:r.length)?null===(n=h[0][0])||void 0===n?void 0:n.length:void 0)&&(null===(o=h[0][0][0])||void 0===o||o.length),(d=this.reshapeTo2D(h,p)).length,void 0!==(void 0!==(null===(a=d[0])||void 0===a?void 0:a.length)?null===(i=d[0][0])||void 0===i?void 0:i.length:void 0)&&(null===(s=d[0][0][0])||void 0===s||s.length),y=this.transform(d,(function(e){return e/10+2})),e.abrupt("return",y);case 27:case"end":return e.stop()}}),e,this,[[4,10]])}))),function(e){return s.apply(this,arguments)})},{key:"reshapeTo2D",value:function(e,t){for(var r=_slicedToArray(t,4),n=(r[0],r[1],r[2]),o=r[3],a=[],i=0,s=0;s<n;s++){for(var u=[],c=0;c<o;c++)u.push(e[i++]);a.push(u)}return a}},{key:"squeeze4D",value:function(e){var t=[e.length,e[0].length,e[0][0].length,e[0][0][0].length],r=t[1];if(1===t[0]&&1===t[3]){for(var n=[],o=0;o<r;o++)n.push(e[0][o].map((function(e){return e[0]})));return n}return e}},{key:"transform",value:function(e,t){return e.map((function(e){return e.map(t)}))}},{key:"___getMelspectrogram",value:(i=_asyncToGenerator(_regeneratorRuntime().mark((function e(t){var r,n;return _regeneratorRuntime().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=generateMelSpectrogram(t,this.sampleRate),n=r.map((function(e){return e.map((function(e){return e/10+2}))})),e.abrupt("return",n);case 3:case"end":return e.stop()}}),e,this)}))),function(e){return i.apply(this,arguments)})},{key:"getMelspectrogramShort",value:(a=_asyncToGenerator(_regeneratorRuntime().mark((function e(t){var r,n;return _regeneratorRuntime().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:for(t instanceof Float32Array||(t=new Float32Array(t)),r=0;r<t.length;r++)("number"!=typeof t[r]||isNaN(t[r]))&&console.error("Invalid number at position [".concat(r,"]:"),t[r]);return e.next=4,this.getMelspectrogram(t);case 4:return n=e.sent,e.abrupt("return",n);case 6:case"end":return e.stop()}}),e,this)}))),function(e){return a.apply(this,arguments)})},{key:"loadModels",value:(o=_asyncToGenerator(_regeneratorRuntime().mark((function e(t,r,n){var o,a,i;return _regeneratorRuntime().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return console.log("Loading models",r),this.sampleRate=n,e.next=4,getSessionOptions(ort.env.wasm.wasmPaths,o,1,!1,!1);case 4:return a=e.sent,o=path.join(t,"/embedding_model"+modelSuffix),e.next=8,ort.InferenceSession.create(o,a);case 8:return this.embeddingSession=e.sent,o=path.join(t,"/melspectrogram"+modelSuffix),e.next=12,getSessionOptions(ort.env.wasm.wasmPaths,o,1,!1,!1);case 12:return a=e.sent,e.next=15,ort.InferenceSession.create(o,a);case 15:if(this.melSpecSession=e.sent,!Array.isArray(r)){e.next=22;break}return e.next=19,Promise.all(r.map(function(){var e=_asyncToGenerator(_regeneratorRuntime().mark((function e(r){var n,o,a;return _regeneratorRuntime().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=path.join(t,r.modelToUse),e.next=3,getSessionOptions(ort.env.wasm.wasmPaths,n,1,!1,!1);case 3:return o=e.sent,e.next=6,ort.InferenceSession.create(n,o);case 6:return a=e.sent,e.abrupt("return",{session:a,modelIdentifier:r.modelToUse});case 8:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}()));case 19:this.keywordSessions=e.sent,e.next=27;break;case 22:return o=path.join(t+"/",r),e.next=25,ort.InferenceSession.create(o,{graphOptimizationLevel:"all"});case 25:i=e.sent,this.keywordSessions=[{session:i,modelIdentifier:r}];case 27:if(!(this.keywordSessions.length>0)){e.next=31;break}return e.next=30,this.runModelWithDifferentFeatures(this.keywordSessions[0].session);case 30:this.nFeatureFrames=e.sent;case 31:return e.abrupt("return",this.nFeatureFrames);case 32:case"end":return e.stop()}}),e,this)}))),function(e,t,r){return o.apply(this,arguments)})},{key:"getEmbeddings",value:(n=_asyncToGenerator(_regeneratorRuntime().mark((function e(t){var r,n,o,a,i,s,u,c,l,f,h,p,d,y,m;return _regeneratorRuntime().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,this.getMelspectrogramShort(t);case 2:return a=e.sent,i=extractWindows(a,76),s=expandDims(i),u=flatten3D(s),c=[i.length,76,32,1],l=new ort.Tensor("float32",u,c),f={input_1:l},e.next=12,this.embeddingSession.run(f);case 12:return h=e.sent,p=h[Object.keys(h)[0]].data,p.length,void 0!==(void 0!==(null===(r=p[0])||void 0===r?void 0:r.length)?null===(n=p[0][0])||void 0===n?void 0:n.length:void 0)&&(null===(o=p[0][0][0])||void 0===o||o.length),h.conv2d_19.dims[0],h.conv2d_19.dims[1],h.conv2d_19.dims[2],d=h.conv2d_19.dims[3],96!==(y=d)&&console.error("chunkSize!=96!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"),m=this.reshapeEmbeddings(p,y),e.abrupt("return",m);case 31:case"end":return e.stop()}}),e,this)}))),function(e){return n.apply(this,arguments)})},{key:"reshapeEmbeddings",value:function(e,t){for(var r=[],n=Math.floor(e.length/t),o=0;o<n;o++){var a=o*t,i=a+t;r.push(e.slice(a,i))}return r}},{key:"runModelWithDifferentFeatures",value:(r=_asyncToGenerator(_regeneratorRuntime().mark((function e(t,r){var n,o,a,i,s,u,c,l,f,h,p;return _regeneratorRuntime().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:n=null,o=new Float32Array(1536),a=new Float32Array(2688).fill(0),i=0,s=0,u=[{size:16,data:o},{size:28,data:a}];case 6:if(!(s<u.length)){e.next=27;break}return c=u[s],e.prev=8,(l=c.data)instanceof Float32Array||(l=new Float32Array(l)),i=c.size,f=new ort.Tensor("float32",l,[1,i,96]),h=_defineProperty({},t.inputNames[0],f),e.next=16,t.run(h);case 16:return(p=e.sent)[Object.keys(p)[0]],n=i,e.abrupt("break",27);case 22:e.prev=22,e.t0=e.catch(8);case 24:s++,e.next=6;break;case 27:if(null===n){e.next=31;break}return e.abrupt("return",n);case 31:return e.abrupt("return",null);case 32:case"end":return e.stop()}}),e,null,[[8,22]])}))),function(e,t){return r.apply(this,arguments)})},{key:"hasUndefined",value:function(e){for(var t=0;t<e.length;t++)if("number"!=typeof e[t]||isNaN(e[t]))throw new Error("hasUndefined() flattenedFeatures[i] ".concat(e[t]))}},{key:"predictKeyword",value:(t=_asyncToGenerator(_regeneratorRuntime().mark((function e(t,r){var n,o,a,i,s,u;return _regeneratorRuntime().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(this.hasUndefined(t),t.length===96*this.nFeatureFrames){e.next=3;break}throw new Error("Expected flattenedFeatures.length ".concat(96*this.nFeatureFrames,", got ").concat(t.length));case 3:if(o=null===(n=this.keywordSessions.find((function(e){return e.modelIdentifier===r})))||void 0===n?void 0:n.session){e.next=6;break}throw new Error('Model session for "'.concat(r,'" not found'));case 6:return a=new ort.Tensor("float32",t,[1,this.nFeatureFrames,96]),i=_defineProperty({},o.inputNames[0],a),e.next=10,o.run(i);case 10:return s=e.sent,u=s[Object.keys(s)[0]],e.abrupt("return",u.data[0]);case 13:case"end":return e.stop()}}),e,this)}))),function(e,r){return t.apply(this,arguments)})},{key:"predictKeyword_org",value:(e=_asyncToGenerator(_regeneratorRuntime().mark((function e(t){var r,n,o,a,i;return _regeneratorRuntime().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(this.hasUndefined(t),t.length===96*this.nFeatureFrames){e.next=3;break}throw new Error("Expected flattenedFeatures.length ".concat(96*this.nFeatureFrames," got ").concat(t.length));case 3:return r=new ort.Tensor("float32",t,[1,this.nFeatureFrames,96]),n=_defineProperty({},this.keywordSession.inputNames[0],r),this.nFeatureFrames,e.next=8,this.keywordSession.run(n);case 8:return o=e.sent,a=o[Object.keys(o)[0]],i=a.data[0],e.abrupt("return",i);case 12:case"end":return e.stop()}}),e,this)}))),function(t){return e.apply(this,arguments)})}]);var e,t,r,n,o,a,i,s}();