pixelbox
Version:
A framework to fast-prototype pixel-based games
1 lines • 7.68 kB
JavaScript
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.bleeper=e():t.bleeper=e()}(window,(function(){return function(t){var e={};function s(i){if(e[i])return e[i].exports;var o=e[i]={i:i,l:!1,exports:{}};return t[i].call(o.exports,o,o.exports,s),o.l=!0,o.exports}return s.m=t,s.c=e,s.d=function(t,e,i){s.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:i})},s.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},s.t=function(t,e){if(1&e&&(t=s(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var i=Object.create(null);if(s.r(i),Object.defineProperty(i,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var o in t)s.d(i,o,function(e){return t[e]}.bind(null,o));return i},s.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return s.d(e,"a",e),e},s.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},s.p="",s(s.s=2)}([function(t,e,s){var i=s(4);function o(){this.name="",this.speed=20,this.pmin=0,this.pmax=127,this.snap=!1,this.steps=[],this.audio=null}t.exports=o,o.prototype.serialize=function(){for(var t="",e=0;e<this.steps.length;e++)t+=this.steps[e].serialize();return{n:this.name,i:[this.speed,this.pmin,this.pmax,~~this.snap],s:t}},o.prototype.deserialize=function(t){this.name=t.n,this.speed=t.i[0],this.pmin=t.i[1],this.pmax=t.i[2],this.snap=!!t.i[3],this.audio=null,this.steps=[];for(var e=t.s,s=0;s<e.length;s+=3){var o=new i;o.deserialize(e,s),this.steps.push(o)}return this},o.prototype.check=function(){this.name.length>11&&(this.name=this.name.substr(0,11)),this.speed=Math.max(0,Math.min(53,~~this.speed)),this.pmin=Math.max(0,Math.min(101,~~this.pmin)),this.pmax=Math.max(26,Math.min(127,~~this.pmax));for(var t=0;t<this.steps.length;t++)this.steps[t].check()}},function(t,e){e.SAMPLE_RATE=44100,e.CHUNK_LENGTH=50},function(t,e,s){var i=s(3),o=s(0),n=new(s(6)),r=audioManager.getEmptySound().__proto__.constructor;if(!r.prototype._createAudioNodes)throw new Error("webAudio required");function h(t){r.call(this),this._data=t}function a(t){var e=new h(t);return t.audio=e,e}inherits(h,r),h.prototype.load=function(t){if(this._loaded)return t&&t(null,this);t&&this._onLoadQueuedCallback.push(t),this.buffer=n.generateSound(this._data),this.usedMemory=this.buffer.duration,this.audioManager.usedMemory+=this.buffer.duration,this._finalizeLoad(null)},o.prototype.play=function(t,e,s){this.audio?this.audio.play(t,e,s):a(this).play(t,e,s)};var u=new i,c=[];e.loadProgram=function(t){u.deserialize(t),assets.bleeper={},c.length=0;for(var e=0;e<u.sounds.length;e++){var s=u.sounds[e],i=a(s);c.push(i);var o=s.name;o&&(assets.bleeper[o]=i,audioManager.permanentSounds[o]=i)}return u},e.sounds=c},function(t,e,s){var i=s(0);function o(){this.version=1,this.sounds=[]}t.exports=o,o.prototype.serialize=function(){for(var t=[],e=0;e<this.sounds.length;e++)t.push(this.sounds[e].serialize());return{_type:"Bleeper program",v:this.version,s:t}},o.prototype.deserialize=function(t){if("Bleeper program"!==t._type)throw new Error("Incorrect data type");this.version=t.v,this.sounds.length=0;for(var e=t.s,s=0;s<e.length;s++){var o=new i;o.deserialize(e[s]),this.sounds.push(o)}return this},o.prototype.check=function(){if(0!==this.sounds.length)for(var t=0;t<this.sounds.length;t++)this.sounds[t].check();else this.sounds.push(new i)}},function(t,e,s){var i=s(5);function o(){this.wave=0,this.pitch=0,this.volume=0}t.exports=o,o.prototype.serialize=function(){var t=this.wave,e=this.pitch;return this.pitch>92&&(t+=8,e-=92),i.encodeUint6(t)+i.encodeUint6(e)+i.encodeUint6(this.volume)},o.prototype.deserialize=function(t,e){var s=i.decodeUint6(t,e),o=i.decodeUint6(t,e+1);return this.volume=i.decodeUint6(t,e+2),s>=8&&(s-=8,o+=92),this.wave=s,this.pitch=o,this},o.prototype.check=function(){this.wave=Math.max(0,Math.min(7,~~this.wave)),this.pitch=Math.max(0,Math.min(93,~~this.pitch)),this.volume=Math.max(0,Math.min(62,~~this.volume))}},function(t,e){for(var s="#$%&'()*+,-~/0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}. !",i=s.length,o={},n=0;n<s.length;n++)o[s[n]]=n;e.encodeUint13=function(t){return s[~~(t/i)]+s[t%i]},e.encodeUint6=function(t){return s[t]},e.decodeUint13=function(t,e){var s=t[e],n=t[e+1];return o[s]*i+o[n]},e.decodeUint6=function(t,e){return o[t[e]]}},function(t,e,s){var i=s(7),o=s(8),n=s(1),r=n.SAMPLE_RATE,h=n.CHUNK_LENGTH;function a(){this.oscillator=new o}t.exports=a,a.prototype.generateSound=function(t){var e=t.steps;if(0===e.length)return null;var s=(t.speed+1)*h,o=e.length*s+100,n=i.createBuffer(1,o,r),a=n.getChannelData(0);this.oscillator.reset();for(var u=0,c=0;u<e.length;u++){var p=e[u];this.oscillator.setStep(t,p),this.oscillator.generate(a,c,c+s),c+=s}return this.oscillator.generateEndDamping(a,c,c+100),n}},function(t,e){var s=new(0,window.AudioContext);t.exports=s},function(t,e,s){var i=s(9),o=s(1).SAMPLE_RATE,n=2*Math.PI,r=[0,.12,.25,.37,.5,.62,.75,.87,.87,.75,.62,.5,.37,.25,.12,0,-.12,-.25,-.37,-.5,-.62,-.75,-.87,-1,-1,-.87,-.75,-.62,-.5,-.37,-.25,-.12];function h(){this._amp=1,this._oscPos=0,this._oscInc=0,this._oscState=1,this._oscOut=0,this._duty=0,this._prevOut=0,this.generate=this._generatePulse}t.exports=h,h.prototype.reset=function(){this._oscPos=0,this._oscState=1,this._oscOut=0,this._prevOut=0},h.prototype.setStep=function(t,e){var s=t.pmin+(t.pmax-t.pmin-20)*e.pitch/93;switch(s=20+90*s/107,t.snap&&(s=Math.round(s)),this._oscInc=i.noteToFreq(s)/o,this._amp=e.volume/63,e.wave){case 0:this.generate=this._generatePulse,this._duty=.06;break;case 1:this.generate=this._generatePulse,this._duty=.12;break;case 2:this.generate=this._generatePulse,this._duty=.25;break;case 3:this.generate=this._generatePulse,this._duty=.5;break;case 4:this.generate=this._generateTriangle;break;case 5:this.generate=this._generateSine;break;case 6:this.generate=this._generateSaw,this._amp*=2;break;case 7:this.generate=this._generateNoise,this._oscInc=2.9*this._oscInc+.01}},h.prototype._generatePulse=function(t,e,s){for(var i=this._prevOut,o=e;o<s;o++)this._oscPos+=this._oscInc,this._oscPos>1&&(this._oscPos=this._oscPos%1),i=this._oscPos>this._duty?this._amp:-this._amp,t[o]=i;this._prevOut=i},h.prototype._generateTriangle=function(t,e,s){for(var i=this._prevOut,o=e;o<s;o++)this._oscPos+=this._oscInc,this._oscPos>1&&(this._oscPos=this._oscPos%1),i=r[~~(32*this._oscPos)]*this._amp,t[o]=i;this._prevOut=i},h.prototype._generateSine=function(t,e,s){for(var i=this._prevOut,o=e;o<s;o++)this._oscPos+=this._oscInc,this._oscPos>1&&(this._oscPos=this._oscPos%1),i=Math.sin(this._oscPos*n)*this._amp,t[o]=i;this._prevOut=i},h.prototype._generateSaw=function(t,e,s){for(var i=this._prevOut,o=e;o<s;o++)this._oscPos+=this._oscInc,this._oscPos>1&&(this._oscPos=this._oscPos%1),i=(this._oscPos-.5)*this._amp,t[o]=i;this._prevOut=i},h.prototype._generateNoise=function(t,e,s){for(var i=this._prevOut,o=e;o<s;o++){if(this._oscPos+=this._oscInc,this._oscPos>1){this._oscPos=this._oscPos%1;var n=!(16384&this._oscState)^!(8192&this._oscState);this._oscState=32767&(this._oscState<<1|n),this._oscOut=(16384&this._oscState)>>14}i=(this._oscOut-.5)*this._amp,t[o]=i}this._prevOut=i},h.prototype.generateEndDamping=function(t,e,s){for(var i=this._prevOut,o=e;o<s;o++)i*=.98,t[o]=i}},function(t,e){for(var s,i=[],o=0;o<128;o++)i.push((s=o,440*Math.pow(2,(s-69)/12)));e.noteToFreqTable=i,e.noteToFreq=function(t){t<0&&(t=0),t>127&&(t=127);var e=~~t,s=t-e,o=i[e];return o+s*(i[e+1]-o)}}])}));