Manic EMU is an all-in-one retro game emulator for iOS. It packs powerful features while keeping a clean, sleek UI and delivering buttery-smooth gameplay.
Swift
let glue=null;function encodeUTF8(e){return encodeURIComponent(s).replace(/%([\dA-Fa-f]{2})/g,m,e=>String.fromCharCode(parseInt(e,16)))}function decodeUTF8(e){if(/[\u0100-\uffff]/.test(e))throw new Error("UTF8 decoding works on bytes only.");let t="";for(let s=0;s<e.length;s++)t+="%"+("0"+e.charCodeAt(s).toString(16)).slice(-2);return decodeURIComponent(t)}function readCString(e,t,s){for(var i=t+s,r=t;e[r]&&!(r>=i);)++r;for(var o="";t<r;)o+=String.fromCharCode(e[t++]);return o}function writeCString(e,t,s){for(var i=s,r=0;r<e.length;++r){var o=e.charCodeAt(r);t[s++]=255&o}return t[s]=0,s-i}class Glue{constructor(){this.instance=null,this.memoryBuffer=null,this.heapu8=null,this.importObject={env:{emscripten_notify_memory_growth:()=>{console.log("growing memory"),this.loadHeap()},log_msg:(e,t)=>{console.log("msg",this.readStringAt(e,t))}}}}async init(e){const t=await WebAssembly.instantiate(e,this.importObject);this.instance=t,this.instance.exports._initialize(),this.exports=this.instance.exports,this.loadHeap()}loadHeap(){this.memoryBuffer=this.exports.memory.buffer,this.heapu8=new Uint8Array(this.memoryBuffer)}malloc(e){return this.instance.exports.malloc(e)}free(e){return this.instance.exports.free(e)}writeInto(e,t){this.heapu8.set(new Uint8Array(t),e)}readStringAt(e,t){return e?readCString(this.heapu8,e,t):""}allocString(e){const t=this.malloc(e.length+1);return writeCString(e,this.heapu8,t),t}}class Bootstrap extends AudioWorkletProcessor{constructor(e){super(e),this.init(e.processorOptions.module)}process(){}async init(e){glue=new Glue,await glue.init(e),this.port.postMessage({ok:!0})}}function createMsgHandler(e,t,s){e.onmessage=async i=>{const r=i.data;if(r&&r.cmd&&r.msgId){const{cmd:s,msgId:i}=r;if(t)try{const o=await t(s,r);e.postMessage({replyFor:i,value:o})}catch(t){e.postMessage({replyFor:i,error:t.toString()})}}else s&&s(i)}}registerProcessor("bootstrap",Bootstrap);class MidiPlayerProcessor extends AudioWorkletProcessor{constructor(...e){super(...e),this.alive=!0,this.hasPlayer=!1,this.playingFrameOffset=-1,this.initBuffers(1024),createMsgHandler(this.port,(e,t)=>{if("setSequence"==e)return this.setSequence(t.buffer);if("stop"==e)this.stop();else if("play"==e)this.play();else if("loop"==e)this.loop(t.times);else{if("getPosition"==e)return this.getPosition();if("seek"==e)return this.seek(t.pos);if("shortEvent"==e)return this.shortEvent(t.status,t.data1,t.data2);"delete"==e&&this.delete()}},e=>{}),this.ps=glue.exports.midiplayer_create(globalThis.sampleRate),this.hasPlayer=!0,this.isPlaying=!1}initBuffers(e){this.leftBuf&&(glue.free(this.leftBuf),glue.free(this.rightBuf)),this.bufSize=e,this.leftBuf=glue.malloc(e),this.rightBuf=glue.malloc(e)}play(){this.playingFrameOffset=-1,glue.exports.midiplayer_play(this.ps),this.isPlaying=!0}loop(e){glue.exports.midiplayer_loop(this.ps,e)}stop(){glue.exports.midiplayer_stop(this.ps),this.isPlaying=!1}getPosition(){return glue.exports.midiplayer_get_position(this.ps)}seek(e){glue.exports.midiplayer_seek(e)}setSequence(e){const t=e.length||e.byteLength,s=glue.malloc(t);glue.writeInto(s,e);const i=glue.exports.midiplayer_set_sequence(this.ps,s,t);return glue.free(s),this.isPlaying=!1,{duration:i}}shortEvent(e,t,s){return glue.exports.midiplayer_short_event(this.ps,e,t,s)}process(e,t){if(this.hasPlayer){const e=-1===this.playingFrameOffset?0:currentFrame-this.playingFrameOffset|0,s=t[0][0].length;s>this.bufSize/4&&this.initBuffers(4*s);const i=glue.exports.midiplayer_write_data(this.ps,this.leftBuf,this.rightBuf,s,e);t[0][0].set(new Float32Array(glue.memoryBuffer,this.leftBuf,s)),t[0][1].set(new Float32Array(glue.memoryBuffer,this.rightBuf,s)),this.isPlaying&&!i&&(this.port.postMessage("end-of-media"),this.isPlaying=!1)}return this.playingFrameOffset=currentFrame+t[0][0].length|0,this.alive}delete(){glue.midiplayer_delete(this.ps),this.ps=null,this.alive=!1}}registerProcessor("midi-player",MidiPlayerProcessor);class FFAudioPlayerProcessor extends AudioWorkletProcessor{constructor(...e){super(...e),this.alive=!0,this.ps=null,this.playing=!1,this.initBuffers(1024),createMsgHandler(this.port,(e,t)=>{if("load"==e)return this.load(t.buffer,t.contentType);if("stop"==e)this.stop();else if("play"==e)this.play();else if("loop"==e)this.loop(t.times);else if("seek"==e)this.seek(t.pos);else{if("getPosition"==e)return this.getPosition();"close"==e&&this.close()}},e=>{}),this.loops=0,this.loopsLeft=0,this.playingFrameOffset=-1,this.inputPtr=0}load(e,t){if(this.ps)throw new Error("player was already loaded");const s=e.length||e.byteLength,i=glue.malloc(s);glue.writeInto(i,e);let r=t?glue.allocString(t):0;if(this.ps=glue.exports.ffplayer_open(i,s,globalThis.sampleRate,r),r&&glue.free(r),!this.ps)throw glue.free(i),new Error("failed to initialize player");return this.inputPtr=i,glue.exports.ffplayer_get_duration_ms(this.ps)}initBuffers(e){this.leftBuf&&(glue.free(this.leftBuf),glue.free(this.rightBuf)),this.bufSize=e,this.leftBuf=glue.malloc(e),this.rightBuf=glue.malloc(e)}play(){this.playingFrameOffset=-1,this.isPlaying=!0,this.loopsLeft=this.loops}loop(e){this.loops=e,this.loopsLeft=e}stop(){this.isPlaying=!1}getPosition(){return glue.exports.ffplayer_get_current_time_ms(this.ps)}seek(e){glue.exports.ffplayer_seek(this.ps,e)}process(e,t){if(this.ps&&this.isPlaying){let e=-1===this.playingFrameOffset?0:currentFrame-this.playingFrameOffset|0;const s=t[0][0].length;s>this.bufSize/4&&this.initBuffers(4*s);let i=0;for(;i<s;){const t=s-i,r=e>0?Math.min(e,s):t,o=e>0?0:i,l=glue.exports.ffplayer_read(this.ps,this.leftBuf+4*o,this.rightBuf+4*o,r);if(e>0?e-=l:i+=l,l<r){if(this.seek(0),!(-1==this.loopsLeft||this.loopsLeft>0)){this.port.postMessage("end-of-media"),this.isPlaying=!1;break}this.loopsLeft=-1==this.loopsLeft?-1:this.loopsLeft-1}}t[0][0].set(new Float32Array(glue.memoryBuffer,this.leftBuf,i)),t[0][1].set(new Float32Array(glue.memoryBuffer,this.rightBuf,i)),this.playingFrameOffset=currentFrame+s|0}return this.alive}close(){this.ps&&(glue.exports.ffplayer_close(this.ps),this.ps=null),this.inputPtr&&(glue.free(this.inputPtr),this.inputPtr=0),this.alive=!1}}registerProcessor("ff-player",FFAudioPlayerProcessor);