cw
Version:
A comprehensive Morse Code (CW) library
3 lines (2 loc) • 1.49 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):(t="undefined"!=typeof globalThis?globalThis:t||self).cw=e()}(this,(function(){"use strict";function t(t){return 60/(50*t)}function e(t,e){const n=void 0===e?t:e;return(300*t-186*n)/(95*t*n)}const n={A:".-",B:"-...",C:"-.-.",D:"-..",E:".",F:"..-.",G:"--.",H:"....",I:"..",J:".---",K:"-.-",L:".-..",M:"--",N:"-.",O:"---",P:".--.",Q:"--.-",R:".-.",S:"...",T:"-",U:"..-",V:"...-",W:".--",X:"-..-",Y:"-.--",Z:"--..",1:".----",2:"..---",3:"...--",4:"....-",5:".....",6:"-....",7:"--...",8:"---..",9:"----.",0:"-----","=":"-...-",".":".-.-.-",",":"--..--","/":"-..-.","?":"..--.."};function o(t={}){if("undefined"==typeof window)return;const e=t.tone||700,n=new(window.AudioContext||window.webkitAudioContext),o=n.createOscillator(),i=n.createGain();return i.connect(n.destination),i.gain.value=0,o.frequency.value=e,o.connect(i),o.start(),{actx:n,osc:o,gain:i}}return{tdit:t,tfdit:e,codes:n,initAudioContext:o,play:function(i,c={}){const r=c.actx||o(c);if(!r)return 0;const{actx:f,gain:u}=r,s=f.currentTime,a=c.wpm||20,d=c.fwpm||10,l=t(a),p=e(a,d);let g=s+.5;for(let t=0;t<i.length;t++){const e=i[t];if(" "===e){g+=4*p;continue}const o=n[e.toUpperCase()];if(o){for(let t=0;t<o.length;t++){const e="-"===o[t]?3*l:l;u.gain.setTargetAtTime(1,g,.002),u.gain.setTargetAtTime(0,g+e,.002),g+=e+l}g-=l,g+=3*p}}return 1e3*g}}}));
//# sourceMappingURL=cw.min.js.map