forked from deskjet/chiptune2.js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
chiptune3.worklet.min.js
1 lines (1 loc) · 6.79 KB
/
chiptune3.worklet.min.js
1
import libopenmptPromise from"./libopenmpt.worklet.js";const OPENMPT_MODULE_RENDER_STEREOSEPARATION_PERCENT=2,OPENMPT_MODULE_RENDER_INTERPOLATIONFILTER_LENGTH=3;let libopenmpt;function asciiToStack(t){const e=libopenmpt.stackAlloc(t.length+1);return writeAsciiToMemory(t,e),e}function writeAsciiToMemory(t,e,o){for(let o=0;o<t.length;++o)libopenmpt.HEAP8[e++>>0]=t.charCodeAt(o);o||(libopenmpt.HEAP8[e>>0]=0)}libopenmptPromise().then((t=>{if(libopenmpt=t,!libopenmpt.stackSave)return;let e=libopenmpt.stackSave();libopenmpt.version=libopenmpt.UTF8ToString(libopenmpt._openmpt_get_string(asciiToStack("library_version"))),libopenmpt.build=libopenmpt.UTF8ToString(libopenmpt._openmpt_get_string(asciiToStack("build"))),libopenmpt.stackRestore(e)})).catch((t=>console.error(t)));class MPT extends AudioWorkletProcessor{constructor(){super(),this.port.onmessage=this.handleMessage_.bind(this),this.paused=!1,this.config={repeatCount:-1,stereoSeparation:100,interpolationFilter:0},this.channels=0}process(t,e,o){if(!this.modulePtr||!this.leftPtr||!this.rightPtr||this.paused)return!0;const p=e[0][0],s=e[0][1],n=libopenmpt._openmpt_module_read_float_stereo(this.modulePtr,sampleRate,p.length,this.leftPtr,this.rightPtr);if(0==n){return!this.modulePtr?this.port.postMessage({cmd:"err",val:"Process"}):this.port.postMessage({cmd:"end"}),!0}p.set(libopenmpt.HEAPF32.subarray(this.leftPtr/4,this.leftPtr/4+n)),s.set(libopenmpt.HEAPF32.subarray(this.rightPtr/4,this.rightPtr/4+n));let r={cmd:"pos",pos:libopenmpt._openmpt_module_get_position_seconds(this.modulePtr),order:libopenmpt._openmpt_module_get_current_order(this.modulePtr),pattern:libopenmpt._openmpt_module_get_current_pattern(this.modulePtr),row:libopenmpt._openmpt_module_get_current_row(this.modulePtr)};return this.port.postMessage(r),!0}handleMessage_(t){const e=t.data.val;switch(t.data.cmd){case"config":this.config=e;break;case"play":this.play(e);break;case"pause":this.paused=!0;break;case"unpause":this.paused=!1;break;case"togglePause":this.paused=!this.paused;break;case"stop":this.stop();break;case"meta":this.meta();break;case"repeatCount":if(this.config.repeatCount=e,!this.modulePtr)return;libopenmpt._openmpt_module_set_repeat_count(this.modulePtr,this.config.repeatCount);break;case"setPitch":if(!libopenmpt.stackSave||!this.modulePtr)return;libopenmpt._openmpt_module_ctl_set(this.modulePtr,asciiToStack("play.pitch_factor"),asciiToStack(e.toString()));break;case"setTempo":if(!libopenmpt.stackSave||!this.modulePtr)return;libopenmpt._openmpt_module_ctl_set(this.modulePtr,asciiToStack("play.tempo_factor"),asciiToStack(e.toString()));break;case"selectSubsong":if(!this.modulePtr)return;libopenmpt._openmpt_module_select_subsong(this.modulePtr,e);break;case"setPos":if(!this.modulePtr)return;libopenmpt._openmpt_module_set_position_seconds(this.modulePtr,e);break;case"setOrderRow":if(!this.modulePtr)return;libopenmpt._openmpt_module_set_position_order_row(this.modulePtr,e.o,e.r);break;default:console.log("Received unknown message",t.data)}}play(t){this.stop();const e=new Int8Array(t),o=libopenmpt._malloc(e.byteLength);if(libopenmpt.HEAPU8.set(e,o),this.modulePtr=libopenmpt._openmpt_module_create_from_memory(o,e.byteLength,0,0,0),0!==this.modulePtr){if(libopenmpt.stackSave){const t=libopenmpt.stackSave();libopenmpt._openmpt_module_ctl_set(this.modulePtr,asciiToStack("render.resampler.emulate_amiga"),asciiToStack("1")),libopenmpt._openmpt_module_ctl_set(this.modulePtr,asciiToStack("render.resampler.emulate_amiga_type"),asciiToStack("a1200")),libopenmpt.stackRestore(t)}this.paused=!1,this.leftPtr=libopenmpt._malloc(512),this.rightPtr=libopenmpt._malloc(512),libopenmpt._openmpt_module_set_repeat_count(this.modulePtr,this.config.repeatCount),libopenmpt._openmpt_module_set_render_param(this.modulePtr,2,this.config.stereoSeparation),libopenmpt._openmpt_module_set_render_param(this.modulePtr,3,this.config.interpolationFilter),this.meta()}else this.port.postMessage({cmd:"err",val:"ptr"})}stop(){this.modulePtr&&(0!=this.modulePtr&&(libopenmpt._openmpt_module_destroy(this.modulePtr),this.modulePtr=0),0!=this.leftBufferPtr&&(libopenmpt._free(this.leftBufferPtr),this.leftBufferPtr=0),0!=this.rightBufferPtr&&(libopenmpt._free(this.rightBufferPtr),this.rightBufferPtr=0),this.channels=0)}meta(){this.port.postMessage({cmd:"meta",meta:this.getMeta()})}getSong(){if(!libopenmpt.UTF8ToString||!this.modulePtr)return!1;let t={channels:[],instruments:[],samples:[],orders:[],numSubsongs:libopenmpt._openmpt_module_get_num_subsongs(this.modulePtr),patterns:[]};const e=libopenmpt._openmpt_module_get_num_channels(this.modulePtr);this.channel=e;for(let o=0;o<e;o++)t.channels.push(libopenmpt.UTF8ToString(libopenmpt._openmpt_module_get_channel_name(this.modulePtr,o)));for(let e=0,o=libopenmpt._openmpt_module_get_num_instruments(this.modulePtr);e<o;e++)t.instruments.push(libopenmpt.UTF8ToString(libopenmpt._openmpt_module_get_instrument_name(this.modulePtr,e)));for(let e=0,o=libopenmpt._openmpt_module_get_num_samples(this.modulePtr);e<o;e++)t.samples.push(libopenmpt.UTF8ToString(libopenmpt._openmpt_module_get_sample_name(this.modulePtr,e)));for(let e=0,o=libopenmpt._openmpt_module_get_num_orders(this.modulePtr);e<o;e++)t.orders.push({name:libopenmpt.UTF8ToString(libopenmpt._openmpt_module_get_order_name(this.modulePtr,e)),pat:libopenmpt._openmpt_module_get_order_pattern(this.modulePtr,e)});for(let o=0,p=libopenmpt._openmpt_module_get_num_patterns(this.modulePtr);o<p;o++){const p={name:libopenmpt.UTF8ToString(libopenmpt._openmpt_module_get_pattern_name(this.modulePtr,o)),rows:[]};for(let t=0,s=libopenmpt._openmpt_module_get_pattern_num_rows(this.modulePtr,o);t<s;t++){const s=[];for(let p=0;p<e;p++){const e=[];for(let s=0;s<6;s++)e.push(libopenmpt._openmpt_module_get_pattern_row_channel_command(this.modulePtr,o,t,p,s));s.push(e)}p.rows.push(s)}t.patterns.push(p)}return t}getMeta(){if(!libopenmpt.UTF8ToString||!this.modulePtr)return!1;const t={};t.dur=libopenmpt._openmpt_module_get_duration_seconds(this.modulePtr),0==t.dur&&this.port.postMessage({cmd:"err",val:"dur"});const e=libopenmpt.UTF8ToString(libopenmpt._openmpt_module_get_metadata_keys(this.modulePtr)).split(";");for(let o=0;o<e.length;o++){const p=libopenmpt._malloc(e[o].length+1);writeAsciiToMemory(e[o],p),t[e[o]]=libopenmpt.UTF8ToString(libopenmpt._openmpt_module_get_metadata(this.modulePtr,p)),libopenmpt._free(p)}return t.song=this.getSong(),t.totalOrders=t.song.orders.length,t.totalPatterns=t.song.patterns.length,t.songs=this.getSongs(),t.libopenmptVersion=libopenmpt.version,t.libopenmptBuild=libopenmpt.build,t}getSongs(){if(!libopenmpt.UTF8ToString)return"";const t=libopenmpt._openmpt_module_get_num_subsongs(this.modulePtr),e=[];for(let o=0;o<t;o++){const t=libopenmpt._openmpt_module_get_subsong_name(this.modulePtr,o),p=libopenmpt.UTF8ToString(t);""!=p?e.push(p):e.push("Subsong "+(o+1)),libopenmpt._openmpt_free_string(t)}return e}}registerProcessor("libopenmpt-processor",MPT);