PageSourceSearch

https://www.tunemate.ai/midi-editor/processor.js

js tunemate.ai collected 2026-10-05 13:08:58 UTC 10,984 bytes, 1 lines download raw bytes

1!function(){"use strict";const e=new class{enabled=!0;log(...e){this.enabled&&console.log(...e)}warn(...e){this.enabled&&console.warn(...e)}error(...e){this.enabled&&console.error(...e)}};e.enabled=!1;class t{samples={};sampleParameters={};addSample(e,t){this.samples[t]=e}addSampleParameter(e,t){const{bank:s,instrument:a,keyRange:n,velRange:i}=t;for(let t=n[0];t<=n[1];t++)void 0===this.sampleParameters[s]&&(this.sampleParameters[s]={}),void 0===this.sampleParameters[s][a]&&(this.sampleParameters[s][a]={}),void 0===this.sampleParameters[s][a][t]&&(this.sampleParameters[s][a][t]=[]),this.sampleParameters[s][a][t].push({...e,velRange:i})}getSamples(e,t,s,a){const n=this.sampleParameters[e]?.[t]??this.sampleParameters[0]?.[t]??null,i=n?.[s]?.filter(e=>a>=e.velRange[0]&&a<=e.velRange[1])??[],l=[];for(const e of i){const t=this.samples[e.sampleID];void 0!==t?l.push({...e,buffer:t}):console.warn(`sample not found: ${e.sampleID}`)}return l}}var s;!function(){function e(){this.data=[],this.position=0}Object.defineProperty(e.prototype,"length",{get:function(){return this.data.length},enumerable:!1,configurable:!0}),e.prototype.writeByte=function(e){this.data.push(e),this.position++},e.prototype.writeStr=function(e){this.writeBytes(function(e){for(var t=[],s=0;s<e.length;s++)t.push(e.charCodeAt(s));return t}(e))},e.prototype.writeInt32=function(e){this.writeByte(e>>24&255),this.writeByte(e>>16&255),this.writeByte(e>>8&255),this.writeByte(255&e)},e.prototype.writeInt16=function(e){this.writeByte(e>>8&255),this.writeByte(255&e)},e.prototype.writeBytes=function(e){var t=this;e.forEach(function(e){return t.writeByte(e)})},e.prototype.writeChunk=function(t,s){this.writeStr(t);var a=new e;s(a),this.writeInt32(a.length),this.writeBytes(a.data)},e.prototype.toBytes=function(){return new Uint8Array(this.data)}}();class a{processor;rpnEvents={};bankSelectMSB={};constructor(e){this.processor=e}handleImmediateEvent(e){switch(e.type){case"sampleParameter":this.processor.addSampleParameter(e.parameter,e.range);break;case"loadSample":this.processor.addSample(e.data,e.sampleID)}}handleDelayableEvent(t){if(e.log("handle delayable event",t),"channel"===t.type)switch(t.subtype){case"noteOn":this.processor.noteOn(t.channel,t.noteNumber,t.velocity);break;case"noteOff":this.processor.noteOff(t.channel,t.noteNumber);break;case"pitchBend":this.processor.pitchBend(t.channel,t.value);break;case"programChange":this.processor.programChange(t.channel,t.value);break;case"controller":switch(t.controllerType){case 99:case 98:delete this.rpnEvents[t.channel];break;case 101:127===t.value?delete this.rpnEvents[t.channel]:this.rpnEvents[t.channel]={...this.rpnEvents[t.channel],rpnMSB:t};break;case 100:127===t.value?delete this.rpnEvents[t.channel]:this.rpnEvents[t.channel]={...this.rpnEvents[t.channel],rpnLSB:t};break;case 6:{const e={...this.rpnEvents[t.channel],dataMSB:t};this.rpnEvents[t.channel]=e,0===e.rpnLSB?.value&&this.processor.setPitchBendSensitivity(t.channel,e.dataMSB.value);break}case 38:this.rpnEvents[t.channel]={...this.rpnEvents[t.channel],dataLSB:t};break;case 7:this.processor.setMainVolume(t.channel,t.value);break;case 11:this.processor.expression(t.channel,t.value);break;case 120:this.processor.allSoundsOff(t.channel);break;case 123:this.processor.allNotesOff(t.channel);break;case 64:this.processor.hold(t.channel,t.value);break;case 10:this.processor.setPan(t.channel,t.value);break;case 1:this.processor.modulation(t.channel,t.value);break;case 0:this.bankSelectMSB[t.channel]=t.value;break;case 32:{const e=this.bankSelectMSB[t.channel];if(void 0!==e){const s=(e<<7)+t.value;this.processor.bankSelect(t.channel,s)}break}case 121:this.processor.resetChannel(t.channel)}}}}class n{getCurrentFrame;onImmediateEvent;onDelayableEvent;scheduledEvents=[];currentEvents=[];constructor(e,t,s){this.getCurrentFrame=e,this.onImmediateEvent=t,this.onDelayableEvent=s}get currentFrame(){return this.getCurrentFrame()}addEvent(t){e.log(t),"delayTime"in t?function(e,t,s){let a,n=0,i=e.length;for(;n<i;)a=n+i>>>1,e[a][s]<t[s]?n=a+1:i=a;e.splice(n,0,t)}(this.scheduledEvents,{...t,scheduledFrame:this.currentFrame+t.delayTime},"scheduledFrame"):this.onImmediateEvent(t)}processScheduledEvents(){if(0!==this.scheduledEvents.length){for(;;){const e=this.scheduledEvents[0];if(void 0===e||e.scheduledFrame>this.currentFrame)break;this.scheduledEvents.shift(),this.currentEvents.push(e)}for(this.currentEvents.sort(i);;){const e=this.currentEvents.shift();if(void 0===e)break;this.onDelayableEvent(e.midi)}}}removeScheduledEvents(e){this.scheduledEvents=this.scheduledEvents.filter(t=>t.midi.channel!==e),this.currentEvents=this.currentEvents.filter(t=>t.midi.channel!==e)}}function i(e,t){return e.scheduledFrame<t.scheduledFrame?-1:e.scheduledFrame>t.scheduledFrame?1:e.sequenceNumber<t.sequenceNumber?-1:e.sequenceNumber>t.sequenceNumber?1:0}!function(e){e[e.attack=0]="attack",e[e.decay=1]="decay",e[e.sustain=2]="sustain",e[e.release=3]="release",e[e.forceStop=4]="forceStop",e[e.stopped=5]="stopped"}(s||(s={}));class l{parameter;phase=s.attack;lastAmplitude=0;sampleRate;constructor(e,t){this.parameter=e,this.sampleRate=t}noteOn(){this.phase=s.attack}noteOff(){this.phase!==s.forceStop&&(this.phase=s.release)}forceStop(){this.phase=s.forceStop}getAmplitude(e){const{attackTime:t,decayTime:a,sustainLevel:n,releaseTime:i}=this.parameter,{sampleRate:l}=this;switch(this.phase){case s.attack:{const a=1/(t*l)*e,n=this.lastAmplitude+a;return n>=1?(this.phase=s.decay,this.lastAmplitude=1,1):(this.lastAmplitude=n,n)}case s.decay:{const t=1/(a*l)*e,i=this.lastAmplitude-t;return i<=n?n<=0?(this.phase=s.stopped,this.lastAmplitude=0,0):(this.phase=s.sustain,this.lastAmplitude=n,n):(this.lastAmplitude=i,i)}case s.sustain:return n;case s.release:{const t=1/(i*l)*e,a=this.lastAmplitude-t;return a<=0?(this.phase=s.stopped,this.lastAmplitude=0,0):(this.lastAmplitude=a,a)}
1case s.forceStop:{const t=1/(.1*l)*e,a=this.lastAmplitude-t;return a<=0?(this.phase=s.stopped,this.lastAmplitude=0,0):(this.lastAmplitude=a,a)}case s.stopped:return 0}}get isPlaying(){return this.phase!==s.stopped}}class r{frequency=5;phase=0;sampleRate;constructor(e){this.sampleRate=e}getValue(e){const t=this.phase;return this.phase+=2*Math.PI*this.frequency/this.sampleRate*e,Math.sin(t)}}class o{sample;sampleIndex=0;_isPlaying=!1;_isNoteOff=!1;baseSpeed=1;envelope;pitchLFO;sampleRate;speed=1;velocity=1;volume=1;modulation=0;modulationDepthRange=50;pan=0;isHold=!1;constructor(e,t){this.sample=e,this.sampleRate=t,this.envelope=new l(e.amplitudeEnvelope,t),this.pitchLFO=new r(t)}noteOn(e,t){this.velocity=t,this._isPlaying=!0,this.sampleIndex=this.sample.sampleStart,this.baseSpeed=Math.pow(2,(e-this.sample.pitch)/12*this.sample.scaleTuning),this.pitchLFO.frequency=5,this.envelope.noteOn()}noteOff(){this.envelope.noteOff(),this._isNoteOff=!0}forceStop(){this.envelope.forceStop()}process(e){if(!this._isPlaying)return;const t=this.baseSpeed*this.speed*this.sample.sampleRate/this.sampleRate,s=this.velocity*this.volume*this.sample.volume,a=(Math.min(1,Math.max(-1,this.pan+this.sample.pan))+1)*Math.PI/4,n=Math.cos(a),i=Math.sin(a),l=this.envelope.getAmplitude(e[0].length),r=l*s*n,o=l*s*i,h=t*(1+this.pitchLFO.getValue(e[0].length)*this.modulation*(this.modulationDepthRange/1200));for(let t=0;t<e[0].length;++t){const s=Math.floor(this.sampleIndex),a=this.sampleIndex+h;let n=null;null!==this.sample.loop&&a>=this.sample.loop.end&&(n=this.sample.loop.start+(a-Math.floor(a)));const i=null!==n?Math.floor(n):Math.min(s+1,this.sample.sampleEnd-1),l=this.sample.buffer[s],c=l+(this.sample.buffer[i]-l)*(this.sampleIndex-s);if(e[0][t]+=c*r,e[1][t]+=c*o,this.sampleIndex=n??a,this.sampleIndex>=this.sample.sampleEnd){this._isPlaying=!1;break}}}get isPlaying(){return this._isPlaying&&this.envelope.isPlaying}get isNoteOff(){return this._isNoteOff}get exclusiveClass(){return this.sample.exclusiveClass}}class h{sampleRate;getCurrentFrame;sampleTable=new t;channels={};eventScheduler;constructor(e,t){this.sampleRate=e,this.getCurrentFrame=t;const s=new a(this);this.eventScheduler=new n(t,e=>s.handleImmediateEvent(e),e=>s.handleDelayableEvent(e)),this.sampleRate=e,this.getCurrentFrame=t}get currentFrame(){return this.getCurrentFrame()}getSamples(e,t,s){const a=this.getChannelState(e),n=9===e?128:a.bank;return this.sampleTable.getSamples(n,a.instrument,t,s)}addSample(e,t){this.sampleTable.addSample(new Float32Array(e),t)}addSampleParameter(e,t){this.sampleTable.addSampleParameter(e,t)}addEvent(e){this.eventScheduler.addEvent(e)}noteOn(t,s,a){const n=this.getChannelState(t),i=this.getSamples(t,s,a);if(0!==i.length)for(const e of i){const t=new o(e,this.sampleRate),i=a/128;if(t.noteOn(s,i),void 0===n.oscillators[s]&&(n.oscillators[s]=[]),void 0!==e.exclusiveClass)for(const t in n.oscillators)for(const s of n.oscillators[t])s.exclusiveClass===e.exclusiveClass&&s.forceStop();n.oscillators[s].push(t)}else e.warn(`There is no sample for noteNumber ${s} in instrument ${n.instrument} in bank ${n.bank}`)}noteOff(e,t){const s=this.getChannelState(e);if(void 0!==s.oscillators[t])for(const e of s.oscillators[t])e.isNoteOff||(s.hold?e.isHold=!0:e.noteOff())}pitchBend(e,t){const s=this.getChannelState(e);s.pitchBend=(t/8192-1)*s.pitchBendSensitivity}programChange(e,t){this.getChannelState(e).instrument=t}setPitchBendSensitivity(e,t){this.getChannelState(e).pitchBendSensitivity=t}setMainVolume(e,t){this.getChannelState(e).volume=t/128}expression(e,t){this.getChannelState(e).expression=t/128}allSoundsOff(e){this.eventScheduler.removeScheduledEvents(e);const t=this.getChannelState(e);for(const e in t.oscillators)for(const s of t.oscillators[e])s.forceStop()}allNotesOff(e){const t=this.getChannelState(e);for(const e in t.oscillators)for(const s of t.oscillators[e])s.noteOff()}hold(e,t){const s=t>=64,a=this.getChannelState(e);if(a.hold=s,!s)for(const e in a.oscillators)for(const t of a.oscillators[e])t.isHold&&t.noteOff()}setPan(e,t){this.getChannelState(e).pan=2*(t/127-.5)}bankSelect(e,t){this.getChannelState(e).bank=t}modulation(e,t){this.getChannelState(e).modulation=t/128}resetChannel(e){delete this.channels[e]}getChannelState(e){const t=this.channels[e];if(void 0!==t)return t;const s={volume:1,bank:0,instrument:0,pitchBend:0,pitchBendSensitivity:2,oscillators:{},expression:1,pan:0,modulation:0,hold:!1};return this.channels[e]=s,s}process(e){this.eventScheduler.processScheduledEvents();for(const t in this.channels){const s=this.channels[t];for(let t in s.oscillators)s.oscillators[t]=s.oscillators[t].filter(t=>(t.speed=Math.pow(2,s.pitchBend/12),t.volume=s.volume*s.expression,t.pan=s.pan,t.modulation=s.modulation,t.process([e[0],e[1]]),!!t.isPlaying))}for(let t=0;t<e[0].length;++t)e[0][t]*=.3,e[1][t]*=.3}}class c extends AudioWorkletProcessor{synth=new h(sampleRate,()=>currentFrame);constructor(){super(),this.port.onmessage=e=>{this.synth.addEvent(e.data)}}process(e,t){return this.synth.process(t[0]),!0}}registerProcessor("synth-processor",c)}();

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.