UNPKG

web-audio-beat-detector

Version:

A beat detection utility which is using the Web Audio API.

43 lines (37 loc) 18.2 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('web-audio-beat-detector-broker')) : typeof define === 'function' && define.amd ? define(['exports', 'web-audio-beat-detector-broker'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.webAudioBeatDetector = {}, global.webAudioBeatDetectorBroker)); })(this, (function (exports, webAudioBeatDetectorBroker) { 'use strict'; var createLoadOrReturnBroker = function createLoadOrReturnBroker(loadBroker, worker) { var broker = null; return function () { if (broker !== null) { return broker; } var blob = new Blob([worker], { type: 'application/javascript; charset=utf-8' }); var url = URL.createObjectURL(blob); broker = loadBroker(url); // Bug #1: Edge up until v18 didn't like the URL to be revoked directly. setTimeout(function () { return URL.revokeObjectURL(url); }); return broker; }; }; // This is the minified and stringified code of the web-audio-beat-detector-worker package. var worker = "(()=>{var t={10:function(t,e,r){!function(t,e){\"use strict\";var r=function(t){var e=[];return t.forEach((function(r,n){for(var o=Math.min(t.length-n,10),a=function(){var o=t[n+i]-r;e.some((function(t){return t.interval===o&&(t.peaks.push(r),!0)}))||e.push({interval:o,peaks:[r]})},i=1;i<o;i+=1)a()})),e},n=function(t){for(var e=0,r=t.length,n=0;n<r;n+=1)t[n]>e&&(e=t[n]);return e},o=function(t,e,r){for(var n=t.length,o=[],a=!1,i=0;i<n;i+=1)t[i]>e?a=!0:a&&(a=!1,o.push(i-1),i+=r/4-1);return a&&o.push(n-1),o},a=function(t,r){var n,o,a=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},i=Math.max(0,null!==(n=a.maxTempo)&&void 0!==n?n:180),u=Math.max(0,null!==(o=a.minTempo)&&void 0!==o?o:90),s=[];return t.forEach((function(t){for(var n=60/(t.interval/r);n<u;)n*=2;for(;n>i;)n/=2;if(!(n<u)){var o=!1,a=t.peaks.length;s.forEach((function(r){if(r.tempo===n&&(r.score+=t.peaks.length,r.peaks=[].concat(e(r.peaks),e(t.peaks)),o=!0),r.tempo>n-.5&&r.tempo<n+.5){var i=2*Math.abs(r.tempo-n);a+=(1-i)*r.peaks.length,r.score+=(1-i)*t.peaks.length}})),o||s.push({peaks:t.peaks,score:a,tempo:n})}})),s},i=30,u=function(t,e,u){var s=n(t),c=.3*s,f=[],l=s-.05*s;if(s>.25)for(;f.length<i&&l>=c;)f=o(t,l,e),l-=.05*s;var p=r(f),h=a(p,e,u);return h.sort((function(t,e){return e.score-t.score})),h},s=function(t,e,r){var n=u(t,e,r);if(0===n.length)throw new Error(\"The given channelData does not contain any detectable beats.\");return n[0].tempo},c=function(t,e,r){var n=u(t,e,r);if(0===n.length)throw new Error(\"The given channelData does not contain any detectable beats.\");var o=n[0],a=o.peaks,i=o.tempo,s=Math.round(i),c=60/s;a.sort((function(t,e){return t-e}));for(var f=a[0]/e;f>c;)f-=c;return{bpm:s,offset:f,tempo:i}};t.createWorker(self,{analyze:function(t){var e=t.channelData,r=t.sampleRate,n=t.tempoSettings;return{result:s(e,r,n)}},guess:function(t){var e=t.channelData,r=t.sampleRate,n=t.tempoSettings;return{result:c(e,r,n)}}})}(r(623),r(132))},45:(t,e,r)=>{var n=r(738).default;t.exports=function(t,e){if(\"object\"!=n(t)||!t)return t;var r=t[Symbol.toPrimitive];if(void 0!==r){var o=r.call(t,e||\"default\");if(\"object\"!=n(o))return o;throw new TypeError(\"@@toPrimitive must return a primitive value.\")}return(\"string\"===e?String:Number)(t)},t.exports.__esModule=!0,t.exports.default=t.exports},79:t=>{t.exports=function(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=Array(e);r<e;r++)n[r]=t[r];return n},t.exports.__esModule=!0,t.exports.default=t.exports},122:(t,e,r)=>{var n=r(79);t.exports=function(t,e){if(t){if(\"string\"==typeof t)return n(t,e);var r={}.toString.call(t).slice(8,-1);return\"Object\"===r&&t.constructor&&(r=t.constructor.name),\"Map\"===r||\"Set\"===r?Array.from(t):\"Arguments\"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?n(t,e):void 0}},t.exports.__esModule=!0,t.exports.default=t.exports},132:(t,e,r)=>{var n=r(901),o=r(291),a=r(122),i=r(869);t.exports=function(t){return n(t)||o(t)||a(t)||i()},t.exports.__esModule=!0,t.exports.default=t.exports},291:t=>{t.exports=function(t){if(\"undefined\"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t[\"@@iterator\"])return Array.from(t)},t.exports.__esModule=!0,t.exports.default=t.exports},293:t=>{function e(t,e,r,n,o,a,i){try{var u=t[a](i),s=u.value}catch(t){return void r(t)}u.done?e(s):Promise.resolve(s).then(n,o)}t.exports=function(t){return function(){var r=this,n=arguments;return new Promise((function(o,a){var i=t.apply(r,n);function u(t){e(i,o,a,u,s,\"next\",t)}function s(t){e(i,o,a,u,s,\"throw\",t)}u(void 0)}))}},t.exports.__esModule=!0,t.exports.default=t.exports},389:function(t,e){!function(t){\"use strict\";var e=function(t){return function(e){var r=t(e);return e.add(r),r}},r=function(t){return function(e,r){return t.set(e,r),r}},n=void 0===Number.MAX_SAFE_INTEGER?9007199254740991:Number.MAX_SAFE_INTEGER,o=536870912,a=2*o,i=function(t,e){return function(r){var i=e.get(r),u=void 0===i?r.size:i<a?i+1:0;if(!r.has(u))return t(r,u);if(r.size<o){for(;r.has(u);)u=Math.floor(Math.random()*a);return t(r,u)}if(r.size>n)throw new Error(\"Congratulations, you created a collection of unique numbers which uses all available integers!\");for(;r.has(u);)u=Math.floor(Math.random()*n);return t(r,u)}},u=new WeakMap,s=r(u),c=i(s,u),f=e(c);t.addUniqueNumber=f,t.generateUniqueNumber=c}(e)},623:function(t,e,r){!function(t,e,r,n,o){\"use strict\";var a={INTERNAL_ERROR:-32603,INVALID_PARAMS:-32602,METHOD_NOT_FOUND:-32601},i=function(t,e){return Object.assign(new Error(t),{status:e})},u=function(t){return i('The requested method called \"'.concat(t,'\" is not supported.'),a.METHOD_NOT_FOUND)},s=function(t){return i('The handler of the method called \"'.concat(t,'\" returned no required result.'),a.INTERNAL_ERROR)},c=function(t){return i('The handler of the method called \"'.concat(t,'\" returned an unexpected result.'),a.INTERNAL_ERROR)},f=function(t){return i('The specified parameter called \"portId\" with the given value \"'.concat(t,'\" does not identify a port connected to this worker.'),a.INVALID_PARAMS)},l=function(t,n){return function(){var o=e(r.mark((function e(o){var a,i,f,l,p,h,v,d,y,m,g,x,b;return r.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(a=o.data,i=a.id,f=a.method,l=a.params,p=n[f],e.prev=2,void 0!==p){e.next=5;break}throw u(f);case 5:if(void 0!==(h=void 0===l?p():p(l))){e.next=8;break}throw s(f);case 8:if(!(h instanceof Promise)){e.next=14;break}return e.next=11,h;case 11:e.t0=e.sent,e.next=15;break;case 14:e.t0=h;case 15:if(v=e.t0,null!==i){e.next=21;break}if(void 0===v.result){e.next=19;break}throw c(f);case 19:e.next=25;break;case 21:if(void 0!==v.result){e.next=23;break}throw c(f);case 23:d=v.result,y=v.transferables,m=void 0===y?[]:y,t.postMessage({id:i,result:d},m);case 25:e.next=31;break;case 27:e.prev=27,e.t1=e.catch(2),g=e.t1.message,x=e.t1.status,b=void 0===x?-32603:x,t.postMessage({error:{code:b,message:g},id:i});case 31:case\"end\":return e.stop()}}),e,null,[[2,27]])})));return function(t){return o.apply(this,arguments)}}()},p=function(){return new Promise((function(t){var e=new ArrayBuffer(0),r=new MessageChannel,n=r.port1,o=r.port2;n.onmessage=function(e){var r=e.data;return t(null!==r)},o.postMessage(e,[e])}))};function h(t,e){var r=Object.keys(t);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(t);e&&(n=n.filter((function(e){return Object.getOwnPropertyDescriptor(t,e).enumerable}))),r.push.apply(r,n)}return r}function v(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{};e%2?h(Object(r),!0).forEach((function(e){n(t,e,r[e])})):Object.getOwnPropertyDescriptors?Object.defineProperties(t,Object.getOwnPropertyDescriptors(r)):h(Object(r)).forEach((function(e){Object.defineProperty(t,e,Object.getOwnPropertyDescriptor(r,e))}))}return t}var d=new Map,y=function(t,n,a){return v(v({},n),{},{connect:function(e){var r=e.port;r.start();var a=t(r,n),i=o.generateUniqueNumber(d);return d.set(i,(function(){a(),r.close(),d.delete(i)})),{result:i}},disconnect:function(t){var e=t.portId,r=d.get(e);if(void 0===r)throw f(e);return r(),{result:null}},isSupported:function(){var t=e(r.mark((function t(){var e,n;return r.wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,p();case 2:if(!t.sent){t.next=14;break}if(!((e=a())instanceof Promise)){t.next=11;break}return t.next=8,e;case 8:t.t0=t.sent,t.next=12;break;case 11:t.t0=e;case 12:return n=t.t0,t.abrupt(\"return\",{result:n});case 14:return t.abrupt(\"return\",{result:!1});case 15:case\"end\":return t.stop()}}),t)})));function n(){return t.apply(this,arguments)}return n}()})},m=function(t,e){var r=y(m,e,arguments.length>2&&void 0!==arguments[2]?arguments[2]:function(){return!0}),n=l(t,r);return t.addEventListener(\"message\",n),function(){return t.removeEventListener(\"message\",n)}};t.createWorker=m,t.isSupported=p}(e,r(293),r(756),r(693),r(389))},633:(t,e,r)=>{var n=r(738).default;function o(){\"use strict\";t.exports=o=function(){return r},t.exports.__esModule=!0,t.exports.default=t.exports;var e,r={},a=Object.prototype,i=a.hasOwnProperty,u=Object.defineProperty||function(t,e,r){t[e]=r.value},s=\"function\"==typeof Symbol?Symbol:{},c=s.iterator||\"@@iterator\",f=s.asyncIterator||\"@@asyncIterator\",l=s.toStringTag||\"@@toStringTag\";function p(t,e,r){return Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{p({},\"\")}catch(e){p=function(t,e,r){return t[e]=r}}function h(t,e,r,n){var o=e&&e.prototype instanceof b?e:b,a=Object.create(o.prototype),i=new A(n||[]);return u(a,\"_invoke\",{value:N(t,r,i)}),a}function v(t,e,r){try{return{type:\"normal\",arg:t.call(e,r)}}catch(t){return{type:\"throw\",arg:t}}}r.wrap=h;var d=\"suspendedStart\",y=\"suspendedYield\",m=\"executing\",g=\"completed\",x={};function b(){}function w(){}function _(){}var E={};p(E,c,(function(){return this}));var O=Object.getPrototypeOf,k=O&&O(O(R([])));k&&k!==a&&i.call(k,c)&&(E=k);var M=_.prototype=b.prototype=Object.create(E);function L(t){[\"next\",\"throw\",\"return\"].forEach((function(e){p(t,e,(function(t){return this._invoke(e,t)}))}))}function j(t,e){function r(o,a,u,s){var c=v(t[o],t,a);if(\"throw\"!==c.type){var f=c.arg,l=f.value;return l&&\"object\"==n(l)&&i.call(l,\"__await\")?e.resolve(l.__await).then((function(t){r(\"next\",t,u,s)}),(function(t){r(\"throw\",t,u,s)})):e.resolve(l).then((function(t){f.value=t,u(f)}),(function(t){return r(\"throw\",t,u,s)}))}s(c.arg)}var o;u(this,\"_invoke\",{value:function(t,n){function a(){return new e((function(e,o){r(t,n,e,o)}))}return o=o?o.then(a,a):a()}})}function N(t,r,n){var o=d;return function(a,i){if(o===m)throw Error(\"Generator is already running\");if(o===g){if(\"throw\"===a)throw i;return{value:e,done:!0}}for(n.method=a,n.arg=i;;){var u=n.delegate;if(u){var s=S(u,n);if(s){if(s===x)continue;return s}}if(\"next\"===n.method)n.sent=n._sent=n.arg;else if(\"throw\"===n.method){if(o===d)throw o=g,n.arg;n.dispatchException(n.arg)}else\"return\"===n.method&&n.abrupt(\"return\",n.arg);o=m;var c=v(t,r,n);if(\"normal\"===c.type){if(o=n.done?g:y,c.arg===x)continue;return{value:c.arg,done:n.done}}\"throw\"===c.type&&(o=g,n.method=\"throw\",n.arg=c.arg)}}}function S(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,S(t,r),\"throw\"===r.method)||\"return\"!==n&&(r.method=\"throw\",r.arg=new TypeError(\"The iterator does not provide a '\"+n+\"' method\")),x;var a=v(o,t.iterator,r.arg);if(\"throw\"===a.type)return r.method=\"throw\",r.arg=a.arg,r.delegate=null,x;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,x):i:(r.method=\"throw\",r.arg=new TypeError(\"iterator result is not an object\"),r.delegate=null,x)}function P(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 T(t){var e=t.completion||{};e.type=\"normal\",delete e.arg,t.completion=e}function A(t){this.tryEntries=[{tryLoc:\"root\"}],t.forEach(P,this),this.reset(!0)}function R(t){if(t||\"\"===t){var r=t[c];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(i.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(n(t)+\" is not iterable\")}return w.prototype=_,u(M,\"constructor\",{value:_,configurable:!0}),u(_,\"constructor\",{value:w,configurable:!0}),w.displayName=p(_,l,\"GeneratorFunction\"),r.isGeneratorFunction=function(t){var e=\"function\"==typeof t&&t.constructor;return!!e&&(e===w||\"GeneratorFunction\"===(e.displayName||e.name))},r.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,_):(t.__proto__=_,p(t,l,\"GeneratorFunction\")),t.prototype=Object.create(M),t},r.awrap=function(t){return{__await:t}},L(j.prototype),p(j.prototype,f,(function(){return this})),r.AsyncIterator=j,r.async=function(t,e,n,o,a){void 0===a&&(a=Promise);var i=new j(h(t,e,n,o),a);return r.isGeneratorFunction(e)?i:i.next().then((function(t){return t.done?t.value:i.next()}))},L(M),p(M,l,\"Generator\"),p(M,c,(function(){return this})),p(M,\"toString\",(function(){return\"[object Generator]\"})),r.keys=function(t){var e=Object(t),r=[];for(var n in e)r.push(n);return r.reverse(),function t(){for(;r.length;){var n=r.pop();if(n in e)return t.value=n,t.done=!1,t}return t.done=!0,t}},r.values=R,A.prototype={constructor:A,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(T),!t)for(var r in this)\"t\"===r.charAt(0)&&i.call(this,r)&&!isNaN(+r.slice(1))&&(this[r]=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 r=this;function n(n,o){return u.type=\"throw\",u.arg=t,r.next=n,o&&(r.method=\"next\",r.arg=e),!!o}for(var o=this.tryEntries.length-1;o>=0;--o){var a=this.tryEntries[o],u=a.completion;if(\"root\"===a.tryLoc)return n(\"end\");if(a.tryLoc<=this.prev){var s=i.call(a,\"catchLoc\"),c=i.call(a,\"finallyLoc\");if(s&&c){if(this.prev<a.catchLoc)return n(a.catchLoc,!0);if(this.prev<a.finallyLoc)return n(a.finallyLoc)}else if(s){if(this.prev<a.catchLoc)return n(a.catchLoc,!0)}else{if(!c)throw Error(\"try statement without catch or finally\");if(this.prev<a.finallyLoc)return n(a.finallyLoc)}}}},abrupt:function(t,e){for(var r=this.tryEntries.length-1;r>=0;--r){var n=this.tryEntries[r];if(n.tryLoc<=this.prev&&i.call(n,\"finallyLoc\")&&this.prev<n.finallyLoc){var o=n;break}}o&&(\"break\"===t||\"continue\"===t)&&o.tryLoc<=e&&e<=o.finallyLoc&&(o=null);var a=o?o.completion:{};return a.type=t,a.arg=e,o?(this.method=\"next\",this.next=o.finallyLoc,x):this.complete(a)},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),x},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.finallyLoc===t)return this.complete(r.completion,r.afterLoc),T(r),x}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var r=this.tryEntries[e];if(r.tryLoc===t){var n=r.completion;if(\"throw\"===n.type){var o=n.arg;T(r)}return o}}throw Error(\"illegal catch attempt\")},delegateYield:function(t,r,n){return this.delegate={iterator:R(t),resultName:r,nextLoc:n},\"next\"===this.method&&(this.arg=e),x}},r}t.exports=o,t.exports.__esModule=!0,t.exports.default=t.exports},693:(t,e,r)=>{var n=r(736);t.exports=function(t,e,r){return(e=n(e))in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t},t.exports.__esModule=!0,t.exports.default=t.exports},736:(t,e,r)=>{var n=r(738).default,o=r(45);t.exports=function(t){var e=o(t,\"string\");return\"symbol\"==n(e)?e:e+\"\"},t.exports.__esModule=!0,t.exports.default=t.exports},738:t=>{function e(r){return t.exports=e=\"function\"==typeof Symbol&&\"symbol\"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&\"function\"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?\"symbol\":typeof t},t.exports.__esModule=!0,t.exports.default=t.exports,e(r)}t.exports=e,t.exports.__esModule=!0,t.exports.default=t.exports},756:(t,e,r)=>{var n=r(633)();t.exports=n;try{regeneratorRuntime=n}catch(t){\"object\"==typeof globalThis?globalThis.regeneratorRuntime=n:Function(\"r\",\"regeneratorRuntime = r\")(n)}},869:t=>{t.exports=function(){throw new TypeError(\"Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.\")},t.exports.__esModule=!0,t.exports.default=t.exports},901:(t,e,r)=>{var n=r(79);t.exports=function(t){if(Array.isArray(t))return n(t)},t.exports.__esModule=!0,t.exports.default=t.exports}},e={};function r(n){var o=e[n];if(void 0!==o)return o.exports;var a=e[n]={exports:{}};return t[n].call(a.exports,a,a.exports,r),a.exports}r.n=t=>{var e=t&&t.__esModule?()=>t.default:()=>t;return r.d(e,{a:e}),e},r.d=(t,e)=>{for(var n in e)r.o(e,n)&&!r.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:e[n]})},r.o=(t,e)=>Object.prototype.hasOwnProperty.call(t,e),(()=>{\"use strict\";r(10)})()})();"; // tslint:disable-line:max-line-length var loadOrReturnBroker = createLoadOrReturnBroker(webAudioBeatDetectorBroker.load, worker); var analyze = function analyze() { var _loadOrReturnBroker; return (_loadOrReturnBroker = loadOrReturnBroker()).analyze.apply(_loadOrReturnBroker, arguments); }; var guess = function guess() { var _loadOrReturnBroker2; return (_loadOrReturnBroker2 = loadOrReturnBroker()).guess.apply(_loadOrReturnBroker2, arguments); }; exports.analyze = analyze; exports.guess = guess; }));