UNPKG

@alexanderolsen/silence-listener-node

Version:

Pseudo-AudioNode to notify when an audio stream becomes silent.

1 lines 16 kB
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.SilenceListenerNode=e():t.SilenceListenerNode=e()}(this,(function(){return(()=>{var t={506:t=>{t.exports=function(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}},926:t=>{function e(t,e,n,r,o,i,c){try{var u=t[i](c),a=u.value}catch(t){return void n(t)}u.done?e(a):Promise.resolve(a).then(r,o)}t.exports=function(t){return function(){var n=this,r=arguments;return new Promise((function(o,i){var c=t.apply(n,r);function u(t){e(c,o,i,u,a,"next",t)}function a(t){e(c,o,i,u,a,"throw",t)}u(void 0)}))}}},575:t=>{t.exports=function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}},100:(t,e,n)=>{var r=n(489),o=n(67);function i(e,n,c){return o()?t.exports=i=Reflect.construct:t.exports=i=function(t,e,n){var o=[null];o.push.apply(o,e);var i=new(Function.bind.apply(t,o));return n&&r(i,n.prototype),i},i.apply(null,arguments)}t.exports=i},913:t=>{function e(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,r.key,r)}}t.exports=function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}},754:t=>{function e(n){return t.exports=e=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)},e(n)}t.exports=e},205:(t,e,n)=>{var r=n(489);t.exports=function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function");t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,writable:!0,configurable:!0}}),e&&r(t,e)}},430:t=>{t.exports=function(t){return-1!==Function.toString.call(t).indexOf("[native code]")}},67:t=>{t.exports=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(t){return!1}}},585:(t,e,n)=>{var r=n(8),o=n(506);t.exports=function(t,e){return!e||"object"!==r(e)&&"function"!=typeof e?o(t):e}},489:t=>{function e(n,r){return t.exports=e=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t},e(n,r)}t.exports=e},8:t=>{function e(n){return"function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?t.exports=e=function(t){return typeof t}:t.exports=e=function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},e(n)}t.exports=e},957:(t,e,n)=>{var r=n(754),o=n(489),i=n(430),c=n(100);function u(e){var n="function"==typeof Map?new Map:void 0;return t.exports=u=function(t){if(null===t||!i(t))return t;if("function"!=typeof t)throw new TypeError("Super expression must either be null or a function");if(void 0!==n){if(n.has(t))return n.get(t);n.set(t,e)}function e(){return c(t,arguments,r(this).constructor)}return e.prototype=Object.create(t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),o(e,t)},u(e)}t.exports=u},757:(t,e,n)=>{t.exports=n(666)},666:t=>{var e=function(t){"use strict";var e,n=Object.prototype,r=n.hasOwnProperty,o="function"==typeof Symbol?Symbol:{},i=o.iterator||"@@iterator",c=o.asyncIterator||"@@asyncIterator",u=o.toStringTag||"@@toStringTag";function a(t,e,n){return Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{a({},"")}catch(t){a=function(t,e,n){return t[e]=n}}function s(t,e,n,r){var o=e&&e.prototype instanceof v?e:v,i=Object.create(o.prototype),c=new j(r||[]);return i._invoke=function(t,e,n){var r=f;return function(o,i){if(r===p)throw new Error("Generator is already running");if(r===d){if("throw"===o)throw i;return E()}for(n.method=o,n.arg=i;;){var c=n.delegate;if(c){var u=S(c,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(r===f)throw r=d,n.arg;n.dispatchException(n.arg)}else"return"===n.method&&n.abrupt("return",n.arg);r=p;var a=l(t,e,n);if("normal"===a.type){if(r=n.done?d:h,a.arg===y)continue;return{value:a.arg,done:n.done}}"throw"===a.type&&(r=d,n.method="throw",n.arg=a.arg)}}}(t,n,c),i}function l(t,e,n){try{return{type:"normal",arg:t.call(e,n)}}catch(t){return{type:"throw",arg:t}}}t.wrap=s;var f="suspendedStart",h="suspendedYield",p="executing",d="completed",y={};function v(){}function b(){}function m(){}var g={};g[i]=function(){return this};var w=Object.getPrototypeOf,x=w&&w(w(C([])));x&&x!==n&&r.call(x,i)&&(g=x);var k=m.prototype=v.prototype=Object.create(g);function _(t){["next","throw","return"].forEach((function(e){a(t,e,(function(t){return this._invoke(e,t)}))}))}function O(t,e){function n(o,i,c,u){var a=l(t[o],t,i);if("throw"!==a.type){var s=a.arg,f=s.value;return f&&"object"==typeof f&&r.call(f,"__await")?e.resolve(f.__await).then((function(t){n("next",t,c,u)}),(function(t){n("throw",t,c,u)})):e.resolve(f).then((function(t){s.value=t,c(s)}),(function(t){return n("throw",t,c,u)}))}u(a.arg)}var o;this._invoke=function(t,r){function i(){return new e((function(e,o){n(t,r,e,o)}))}return o=o?o.then(i,i):i()}}function S(t,n){var r=t.iterator[n.method];if(r===e){if(n.delegate=null,"throw"===n.method){if(t.iterator.return&&(n.method="return",n.arg=e,S(t,n),"throw"===n.method))return y;n.method="throw",n.arg=new TypeError("The iterator does not provide a 'throw' method")}return y}var o=l(r,t.iterator,n.arg);if("throw"===o.type)return n.method="throw",n.arg=o.arg,n.delegate=null,y;var i=o.arg;return i?i.done?(n[t.resultName]=i.value,n.next=t.nextLoc,"return"!==n.method&&(n.method="next",n.arg=e),n.delegate=null,y):i:(n.method="throw",n.arg=new TypeError("iterator result is not an object"),n.delegate=null,y)}function N(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function L(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function j(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(N,this),this.reset(!0)}function C(t){if(t){var n=t[i];if(n)return n.call(t);if("function"==typeof t.next)return t;if(!isNaN(t.length)){var o=-1,c=function n(){for(;++o<t.length;)if(r.call(t,o))return n.value=t[o],n.done=!1,n;return n.value=e,n.done=!0,n};return c.next=c}}return{next:E}}function E(){return{value:e,done:!0}}return b.prototype=k.constructor=m,m.constructor=b,b.displayName=a(m,u,"GeneratorFunction"),t.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===b||"GeneratorFunction"===(e.displayName||e.name))},t.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,m):(t.__proto__=m,a(t,u,"GeneratorFunction")),t.prototype=Object.create(k),t},t.awrap=function(t){return{__await:t}},_(O.prototype),O.prototype[c]=function(){return this},t.AsyncIterator=O,t.async=function(e,n,r,o,i){void 0===i&&(i=Promise);var c=new O(s(e,n,r,o),i);return t.isGeneratorFunction(n)?c:c.next().then((function(t){return t.done?t.value:c.next()}))},_(k),a(k,u,"Generator"),k[i]=function(){return this},k.toString=function(){return"[object Generator]"},t.keys=function(t){var e=[];for(var n in t)e.push(n);return e.reverse(),function n(){for(;e.length;){var r=e.pop();if(r in t)return n.value=r,n.done=!1,n}return n.done=!0,n}},t.values=C,j.prototype={constructor:j,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(L),!t)for(var n in this)"t"===n.charAt(0)&&r.call(this,n)&&!isNaN(+n.slice(1))&&(this[n]=e)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(t){if(this.done)throw t;var n=this;function o(r,o){return u.type="throw",u.arg=t,n.next=r,o&&(n.method="next",n.arg=e),!!o}for(var i=this.tryEntries.length-1;i>=0;--i){var c=this.tryEntries[i],u=c.completion;if("root"===c.tryLoc)return o("end");if(c.tryLoc<=this.prev){var a=r.call(c,"catchLoc"),s=r.call(c,"finallyLoc");if(a&&s){if(this.prev<c.catchLoc)return o(c.catchLoc,!0);if(this.prev<c.finallyLoc)return o(c.finallyLoc)}else if(a){if(this.prev<c.catchLoc)return o(c.catchLoc,!0)}else{if(!s)throw new Error("try statement without catch or finally");if(this.prev<c.finallyLoc)return o(c.finallyLoc)}}}},abrupt:function(t,e){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&r.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===t||"continue"===t)&&i.tryLoc<=e&&e<=i.finallyLoc&&(i=null);var c=i?i.completion:{};return c.type=t,c.arg=e,i?(this.method="next",this.next=i.finallyLoc,y):this.complete(c)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),y},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),L(n),y}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var o=r.arg;L(n)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(t,n,r){return this.delegate={iterator:C(t),resultName:n,nextLoc:r},"next"===this.method&&(this.arg=e),y}},t}(t.exports);try{regeneratorRuntime=e}catch(t){Function("r","regeneratorRuntime = r")(e)}}},e={};function n(r){if(e[r])return e[r].exports;var o=e[r]={exports:{}};return t[r](o,o.exports,n),o.exports}n.n=t=>{var e=t&&t.__esModule?()=>t.default:()=>t;return n.d(e,{a:e}),e},n.d=(t,e)=>{for(var r in e)n.o(e,r)&&!n.o(t,r)&&Object.defineProperty(t,r,{enumerable:!0,get:e[r]})},n.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),n.r=t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})};var r={};return(()=>{"use strict";n.r(r),n.d(r,{createSilenceListenerNode:()=>L,polyfill:()=>N});var t=n(757),e=n.n(t),o=n(926),i=n.n(o),c=n(575),u=n.n(c),a=n(913),s=n.n(a),l=n(506),f=n.n(l),h=n(205),p=n.n(h),d=n(585),y=n.n(d),v=n(754),b=n.n(v),m=function(){function t(){u()(this,t),this.silenceCallback=null}return s()(t,[{key:"connect",value:function(t){throw"connect() must be implemented"}},{key:"disconnect",value:function(){throw"disconnect() must be implemented"}},{key:"setSilenceCallback",value:function(t){this.silenceCallback=t}}]),t}();var g=function(t){p()(o,t);var e,n,r=(e=o,n=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return!1}}(),function(){var t,r=b()(e);if(n){var o=b()(this).constructor;t=Reflect.construct(r,arguments,o)}else t=r.apply(this,arguments);return y()(this,t)});function o(t,e,n,i,c){var a;return u()(this,o),(a=r.call(this)).audioNode=t.createScriptProcessor(e,n,i),a.audioNode.onaudioprocess=a._processNext.bind(f()(a)),a.silent=!1,a.silentRuns=0,a.silenceThreshold=c,a}return s()(o,[{key:"connect",value:function(t){this.audioNode.connect(t)}},{key:"disconnect",value:function(){this.audioNode.disconnect()}},{key:"_processNext",value:function(t){for(var e=!0,n=t.inputBuffer,r=t.outputBuffer,o=0;o<r.numberOfChannels;o++)for(var i=n.getChannelData(o),c=r.getChannelData(o),u=0;u<n.length;u++)c[u]=i[u],0!==c[u]&&(e=!1);this.silent&&!e?(this.silenceCallback(!1),this.silent=!1,this.silentRuns=0):!this.silent&&e&&(this.silentRuns++,this.silentRuns===this.silenceThreshold&&(this.silenceCallback(!0),this.silent=!0))}}]),o}(m),w=n(957),x=n.n(w);function k(t){var e=function(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return!1}}();return function(){var n,r=b()(t);if(e){var o=b()(this).constructor;n=Reflect.construct(r,arguments,o)}else n=r.apply(this,arguments);return y()(this,n)}}function _(t,e,n,r,o,i){for(var c=o,a=[],s=e,l=n,f=0;f<n;f++)a.push(r);var h=function(t){p()(n,t);var e=k(n);function n(t){return u()(this,n),e.call(this,t,"SilenceListenerNode",{numberOfInputs:s,numberOfOutputs:l,outputChannelCount:a})}return n}(x()(AudioWorkletNode));return new Promise((function(e,n){t.audioWorklet.addModule(i).then((function(){var n=new h(t);n.port.postMessage({command:"init",silenceThreshold:c});var r=new O(n);e(r)}))}))}var O=function(t){p()(n,t);var e=k(n);function n(t){var r;return u()(this,n),(r=e.call(this)).audioNode=t,t.port.onmessage=r._onMessage.bind(f()(r)),r}return s()(n,[{key:"connect",value:function(t){this.audioNode.connect(t)}},{key:"disconnect",value:function(){this.audioNode.disconnect()}},{key:"_onMessage",value:function(t){if("stateChange"!==t.data.command)throw"command ".concat(t.data.command," unrecognized");this.silenceCallback(t.data.silent)}}]),n}(m),S=function(){function t(e){u()(this,t),this._backend=e,this._backend.setSilenceCallback(this.onSilenceStateChange.bind(this)),this._subscriptions=new Map,this.lastStateChangeTime=Date.now()}return s()(t,[{key:"onSilenceStateChange",value:function(t){var e=Date.now()-this.lastStateChangeTime;this.lastStateChangeTime=Date.now(),this._subscriptions.forEach((function(n,r){n(t,e)}))}},{key:"connect",value:function(t){this._backend.connect(t)}},{key:"disconnect",value:function(){this._backend.disconnect()}},{key:"subscribeToSilence",value:function(t){var e=(7,Math.random().toString(36).substring(0,7));return this._subscriptions.set(e,t),e}},{key:"unsubscribeFromSilence",value:function(t){this._subscriptions.delete(t)}},{key:"_isSilenceListenerNode",get:function(){return!0}},{key:"numberOfInputs",get:function(){return this._backend.audioNode.numberOfInputs}},{key:"numberOfOutputs",get:function(){return this._backend.audioNode.numberOfOutputs}},{key:"channelCount",get:function(){return this._backend.audioNode.channelCount},set:function(t){this._backend.audioNode.channelCount=t}},{key:"channelCountMode",get:function(){return this._backend.audioNode.channelCountMode},set:function(t){this._backend.audioNode.channelCountMode=t}},{key:"channelInterpretation",get:function(){return this._backend.audioNode.channelInterpretation},set:function(t){this._backend.audioNode.channelInterpretation=t}},{key:"silent",get:function(){return this._backend.silent}},{key:"silence",get:function(){return this._backend.silent}}]),t}();function N(){AudioNode.prototype._isPolyfilled||(AudioNode.prototype._isPolyfilled=!0,AudioNode.prototype._connect=AudioNode.prototype.connect,AudioNode.prototype.connect=function(){var t=Array.prototype.slice.call(arguments);!0===t[0]._isSilenceListenerNode&&(t[0]=t[0]._backend.audioNode),this._connect.apply(this,t)})}function L(t,e){return j.apply(this,arguments)}function j(){return(j=i()(e().mark((function t(n,r){var o,i,c,u,a,s,l,f=arguments;return e().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(o=f.length>2&&void 0!==f[2]?f[2]:{},i=o.nInputs||1,c=o.nOutputs||1,u=o.batchSize||512,a=o.silenceThreshold||Math.floor(44100/u),s=o.pathToWorklet||"/sln.worklet.js",C(n,i,c,r,u),N(),void 0===window.AudioWorklet){t.next=14;break}return t.next=11,_(n,i,c,r,a,s);case 11:l=t.sent,t.next=15;break;case 14:l=new g(n,u,i,c,a);case 15:return t.abrupt("return",new S(l));case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function C(t,e,n,r,o){if(!t)throw"must provide a valid AudioContext";if(e!=n)throw"SilenceListenerNode only support nInputs === nOutputs";if(e<1)throw"nInputs must be >= 1";if(r<1)throw"nChannels must be >= 1";if(![256,512,1024,2048,4096,8192,16384].includes(o))throw"batch size must be one of [256, 512, 1024, 2048, 4096, 8192, 16384]"}})(),r})()}));