beepody
Version:
Beep melody utilities.
8 lines (7 loc) • 3.2 kB
JavaScript
/*!
* beepody v0.3.33333333
* (c) Dylan Ferris
* Released under the GPL-3.0 License.
*/
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).beepody={})}(this,(function(e){"use strict";var t="0.3.33333333";class s{ctx;gainNode;outNode;constructor(){if("undefined"==typeof window||void 0===window.AudioContext)throw"AudioContext is required";this.ctx=new window.AudioContext,this.gainNode=this.ctx.createGain(),this.gainNode.gain.value=1/8,this.outNode=this.ctx.destination,this.gainNode.connect(this.outNode)}beepOnBeepOff(e,t,s){const n=this.ctx.createOscillator();n.type="square",n.frequency.value=e,n.connect(this.gainNode),n.start(s),n.stop(s+t)}}class n{frequency;length;repeats;constructor(e=440,t=200,s=1){this.frequency=e,this.length=t,this.repeats=s}toString(){return`Beep(${this.frequency} ${this.length} ${this.repeats})`}}class o{beeps;tempo;constructor(e){this.beeps=e,this.tempo=600}toHash(){const e=[];for(const t of this.beeps)e.push(`${t.frequency}^${t.length}`);return e.join("|")}toBeepCommand(){const e=[];for(const t of this.beeps){let s=`-f ${t.frequency} -l ${t.length}`;1!==t.repeats&&(s+=` -r ${t.repeats}`),e.push(s)}return`beep ${e.join(" -n ")}`}toGRUBInitTune(){const e=[];let t=`play ${this.tempo}`;for(const t of this.beeps)e.push(`${t.frequency} ${t.length/100}`);return e.length&&(t+=` ${e.join(" ")}`),t}toString(){return`${this.constructor.name}(${this.toHash()})`}lengthInSeconds(){let e=0;for(const t of this.beeps)for(let s=-1;s<t.repeats;s++)e+=t.length;return.001*e}}const r=e=>{if("undefined"==typeof window)return;let t=0;const n=new s;for(const s of e.beeps){const e=.001*s.length;n.beepOnBeepOff(s.frequency,e,t),t+=e}},i=[["f","frequency","FREQ"],["l","length","LEN"],["r","repeats","REPEATS"],["d","delay","DELAY"]],c=[["n","new","NEW"]];e.Beep=n,e.BeepSequence=o,e.default=class{static get version(){return t}},e.parseBeepCommand=e=>{const t=new o([]),s=e.split(/\s+/);console.assert("beep"===s.shift());let r=new n;const p=(e,t)=>{switch(e){case"frequency":r.frequency=parseFloat(t);break;case"length":r.length=parseFloat(t);break;case"repeats":r.repeats=parseInt(t,10)}},f=e=>{if("new"===e)t.beeps.push(r),r=new n};let a;for(const e of s)if("-"===e[0])if("-"===e[1]){const t=e.substring(2);for(const e of i)e[1]===t&&(a=e[1]);for(const e of c)e[1]===t&&f(e[1])}else{const t=e[1];for(const e of i)e[0]===t&&(a=e[1]);for(const e of c)e[0]===t&&"string"==typeof e[1]&&f(e[1])}else a&&p(a,e);return t.beeps.push(r),t},e.parseBeepHash=e=>{const t=new o([]);for(const s of e.split("|")){const e=s.split("^").map((e=>parseFloat(e))),o=new n(...e);t.beeps.push(o)}return t},e.parseGRUBInitTune=e=>{const t=new o([]),s=e.split(/\s+/);let r;console.assert("play"===s.shift()),t.tempo=parseFloat(s.shift()||"60");for(const e of s)if(r){const s=100*parseFloat(e),o=new n;o.frequency=r,o.length=s,t.beeps.push(o),r=null}else r=parseFloat(e);return t},e.playBeepSequence=r,e.playDefaultBeep=()=>{r(new o([new n]))},e.version=t,Object.defineProperty(e,"__esModule",{value:!0})}));
//# sourceMappingURL=index.umd.min.js.map