@omnimedia/omnitool 1.1.0-75 → 1.1.0-76
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/s/driver/parts/compositor.ts +3 -1
- package/x/demo/demo.bundle.min.js +1 -1
- package/x/demo/demo.bundle.min.js.map +3 -3
- package/x/driver/parts/compositor.js +3 -0
- package/x/driver/parts/compositor.js.map +1 -1
- package/x/index.html +2 -2
- package/x/tests.bundle.min.js +1 -1
- package/x/tests.bundle.min.js.map +3 -3
- package/x/tests.html +1 -1
package/package.json
CHANGED
|
@@ -164,7 +164,10 @@ export class Compositor {
|
|
|
164
164
|
return
|
|
165
165
|
|
|
166
166
|
const [top, right, bottom, left] = crop
|
|
167
|
+
const activeMask = target.mask
|
|
168
|
+
target.mask = null
|
|
167
169
|
const bounds = target.getLocalBounds()
|
|
170
|
+
target.mask = activeMask
|
|
168
171
|
const x = bounds.x + bounds.width * left
|
|
169
172
|
const y = bounds.y + bounds.height * top
|
|
170
173
|
const width = bounds.width * Math.max(0, 1 - left - right)
|
|
@@ -261,4 +264,3 @@ export class Compositor {
|
|
|
261
264
|
}
|
|
262
265
|
}
|
|
263
266
|
}
|
|
264
|
-
|
|
@@ -2412,7 +2412,7 @@ vec4 transition(vec2 uv) {
|
|
|
2412
2412
|
vec2 uv = vTextureCoord.xy;
|
|
2413
2413
|
gl_FragColor = transition(vTextureCoord);
|
|
2414
2414
|
}
|
|
2415
|
-
`;function Ea({name:r,renderer:e}){let t=_a.default.find(c=>c.name===r),n=new xt,o=new gt,i=new Cr({}),s=new Cr({}),a=new xo({glProgram:new po({vertex:Ca,fragment:Aa(t.glsl)}),resources:{from:i,to:s,uniforms:{...ki.basics,...ki.custom(t)}}});return n.filters=[a],{render({width:c,height:l,from:d,to:u,progress:f}){return(n.width!==c||n.height!==l)&&(n.setSize({width:c,height:l}),o.source.resize(c,l)),i.resource=d,s.resource=u,i.update(),s.update(),a.resources.uniforms.uniforms.progress=f,e.render({container:n,target:o,clear:!1,width:c,height:l}),o}}}var jn=class{pixi;onPointerDown=le();onPointerMove=le();onPointerUp=le();onDispose=le();static async setup(){let e=await wi({width:1920,height:1080,preference:"webgl",background:"black",preferWebGLVersion:2}),t=new mo;return t.interactive=!0,new this({renderer:e,stage:t})}constructor(e){this.pixi=e}#e=new Map;#t=new Map;#r=new WeakMap;async composite(e){let{stage:t,renderer:n}=this.pixi;this.#d(this.#u(e));let{dispose:o}=await this.#n(e,t);n.render(t);let i=new VideoFrame(n.canvas,{timestamp:0,duration:0});return o(),i}getActiveObject(e){return this.#t.get(e)?.sprite}async#n(e,t){if(Array.isArray(e)){let n=[];for(let o of[...e].reverse()){let i=await this.#n(o,t);n.push(i.dispose)}return{dispose:()=>n.forEach(o=>o())}}switch(e.kind){case"text":return this.#o(e,t);case"image":return this.#s(e,t);case"transition":return this.#i(e,t);case"gap":return this.pixi?.renderer.clear(),{dispose:()=>{}};default:return console.warn("Unknown layer kind",e.kind),{dispose:()=>{}}}}#o(e,t){let n=this.#l(e);return this.#c(n,e.matrix),this.#a(n,e.crop),t.addChild(n),{dispose:()=>{}}}#s(e,t){let n=this.#l(e);n.texture&&n.texture!==gt.EMPTY&&n.texture.destroy(!0);let o=gt.from(e.frame);return n.texture=o,this.#c(n,e.matrix),this.#a(n,e.crop),t.addChild(n),{dispose:()=>{e.frame.close()}}}#i({from:e,to:t,progress:n,name:o},i){let a=(this.#e.get(o)??(this.#e.set(o,Ea({name:"circle",renderer:this.pixi.renderer})),this.#e.get(o))).render({from:e,to:t,progress:n,width:e.displayWidth,height:e.displayHeight}),c=new xt(a);return i.addChild(c),{dispose:()=>c.destroy(!1)}}#c(e,t){if(!t)return;let n=ka(t);e.setFromMatrix(n)}#a(e,t){let n=this.#r.get(e);if(n&&(e.mask===n&&(e.mask=null),n.removeFromParent(),n.clear()),!t||t.every(p=>p===0))return;let[o,i,s,a]=t,c=e.getLocalBounds(),l=c.x+c.width*a,d=c.y+c.height*o,u=c.width*Math.max(0,1-a-i),f=c.height*Math.max(0,1-o-s),m=n??new go;m.clear(),m.beginFill(16777215),m.drawRect(l,d,u,f),m.endFill(),e.addChild(m),e.mask=m,this.#r.set(e,m)}#l(e){let t=this.#t.get(e.id);if(t)return t.sprite;switch(e.kind){case"text":{let n=new Sr({text:e.content,style:e.style});n.eventMode="static";let o=a=>this.onPointerDown.publish({event:a,id:e.id,object:n}),i=a=>this.onPointerUp.publish({event:a,id:e.id,object:n}),s=a=>this.onPointerUp.publish({event:a,id:e.id,object:n});return n.on("pointerdown",o),n.on("pointerup",i),n.on("pointermove",s),this.#t.set(e.id,{sprite:n,dispose:()=>{this.onDispose.publish({id:e.id,object:n}),n.off("pointermove",s),n.off("pointerdown",o),n.off("pointerup",i)}}).get(e.id)?.sprite}case"image":{let n=new xt;n.eventMode="static";let o=a=>this.onPointerDown.publish({event:a,id:e.id,object:n}),i=a=>this.onPointerUp.publish({event:a,id:e.id,object:n}),s=a=>this.onPointerUp.publish({event:a,id:e.id,object:n});return n.on("pointerdown",o),n.on("pointermove",s),n.on("pointerup",i),this.#t.set(e.id,{sprite:n,dispose:()=>{this.onDispose.publish({id:e.id,object:n}),n.off("pointerdown",o),n.off("pointermove",s),n.off("pointerup",i)}}).get(e.id)?.sprite}}}#u(e){let t=new Set,n=o=>{if(Array.isArray(o))for(let i of o)n(i);else t.add(o.id)};return n(e),t}#d(e){for(let t of this.#t.keys())if(!e.has(t)){let{sprite:n,dispose:o}=this.#t.get(t);o(),n.destroy(!0),this.#t.delete(t)}}};var Fa=r=>jr.host(e=>({async world(){r.count++}}));async function ce(r){return r instanceof Blob?new kr(r):new vr(r)}var $n=class{machina;thread;compositor;static async setup(e){let t=new Hn,n=await jr.thread({label:"OmnitoolDriver",workerUrl:e?.workerUrl??"/node_modules/@omnimedia/omnitool/x/driver/driver.worker.bundle.min.js",setupHost:Fa(t)}),o=await jn.setup();return new this(t,n,o)}constructor(e,t,n){this.machina=e,this.thread=t,this.compositor=n}async hello(){return this.thread.work.hello()}async getAudioDuration(e){let n=await new ee({source:await ce(e),formats:ie}).getPrimaryAudioTrack();if(!n)throw new Error("primary audio track not found");return await n.computeDuration()}async getVideoDuration(e){return await(await new ee({source:await ce(e),formats:ie}).getPrimaryVideoTrack())?.computeDuration()}decodeVideo(e){let t=null,{port1:n,port2:o}=new MessageChannel,i=new TransformStream({async transform(s,a){let c=await e.onFrame?.(s)??s;t?.close(),a.enqueue(c),t=c}});return this.thread.work.decodeVideo[Tt]({transfer:[i.writable,o]})({source:e.source,cancel:o,video:i.writable,start:e.start,end:e.end}),{readable:i.readable,cancel(){n.postMessage("close"),n.close()}}}decodeAudio(e){let t=new TransformStream,{port1:n,port2:o}=new MessageChannel;return this.thread.work.decodeAudio[Tt]({transfer:[t.writable,o]})({source:e.source,cancel:o,audio:t.writable,start:e.start,end:e.end}),{readable:t.readable,cancel(){n.postMessage("close"),n.close()}}}encode({audio:e,video:t,config:n}){let{readable:o,writable:i}=new TransformStream,s=[e,t,i].filter(Er.happy),a=this.thread.work.encode[Tt]({transfer:s})({audio:e,video:t,config:n,writable:i});return{readable:o,done:a}}async composite(e){return await this.compositor.composite(e)}};var M;(function(r){r[r.Sequence=0]="Sequence",r[r.Stack=1]="Stack",r[r.Video=2]="Video",r[r.Audio=3]="Audio",r[r.Text=4]="Text",r[r.Gap=5]="Gap",r[r.Spatial=6]="Spatial",r[r.Transition=7]="Transition",r[r.TextStyle=8]="TextStyle"})(M||(M={}));var Kn;(function(r){r[r.Crossfade=0]="Crossfade"})(Kn||(Kn={}));var Xn=class{datafile;duration=0;hasVideo=!1;hasAudio=!1;constructor(e){this.datafile=e}static async analyze(e){let t=new this(e),n=await this.duration(e.url)*1e3;t.duration=n;let{video:o,audio:i}=await this.#e(e.url);return t.hasAudio=i,t.hasVideo=o,t}static async duration(e){let n=await new ee({formats:ie,source:await ce(e)}).computeDuration();return Number(n.toFixed(5))}static async#e(e){let t=new ee({formats:ie,source:await ce(e)});return{audio:!!await t.getPrimaryAudioTrack(),video:!!await t.getPrimaryVideoTrack()}}};var Qn=class{#e=new Fr;async store(e){let t=await Xn.analyze(e),{hash:n}=t.datafile.checksum,{filename:o,bytes:i,url:s,blob:a}=t.datafile;if(this.#e.has(n)){let c=this.#e.require(n);c.filename=o}else this.#e.set(n,{kind:"media",filename:o,bytes:i,url:s,blob:a,duration:t.duration});return t}require(e){return this.#e.require(e)}};var Yn=class r{videoTrack;options;#e;#t=new Map;#r=[0,0];constructor(e,t){this.videoTrack=e,this.options=t,this.#e=new fr(e,t.canvasSinkOptions)}static async init(e,t){let o=await new ee({formats:ie,source:await ce(e)}).getPrimaryVideoTrack();if(o)return new r(o,{frequency:t.frequency??1,canvasSinkOptions:t.canvasSinkOptions??{width:80,height:50,fit:"fill"},onChange:t.onChange,onPlaceholders:t.onPlaceholders});throw new Error("Source has no video track")}set frequency(e){e!==this.options.frequency&&(this.options.frequency=e,this.#a())}get frequency(){return this.options.frequency}#n([e,t],n=1){let o=t-e;return[e-o*n,t+o*n]}async#o(){let[e,t]=this.#r,n=new Set,o=await this.videoTrack.computeDuration();for(let i=Math.max(0,e);i<=t;i+=this.options.frequency)i>=0&&i<=o&&n.add(i);this.options.onPlaceholders?.([...n])}async#s(){let[e,t]=this.#r,n=new Set,o=await this.videoTrack.computeDuration();for(let c=Math.max(0,e);c<=t;c+=this.options.frequency)c>=0&&c<=o&&n.add(c);let i=[...n].filter(c=>!this.#t.has(c)),s=0;for await(let c of this.#e.canvasesAtTimestamps(i))if(c){let l=i[s++];this.#t.set(l,c)}for(let c of this.#t.keys())n.has(c)||this.#t.delete(c);let a=[...this.#t.entries()].map(([c,l])=>({time:c,canvas:l}));this.options.onChange(a)}set range(e){let[t,n]=e,o=n-t,[i,s]=this.#r,a=i+o,c=s-o,l=t<a,d=n>c;!l&&!d||(this.#r=this.#n(e,2),this.#a())}#i=null;#c=!1;async#a(){if(this.#o(),this.#i){this.#c=!0;return}this.#i=this.#s(),await this.#i,this.#i=null,this.#c&&(this.#c=!1,await this.#a())}getThumbnail(e){return this.#t.get(e)}};function Da(r,e){let t=typeof window<"u"&&window.devicePixelRatio||1,n=document.createElement("canvas");n.width=e.width*t,n.height=e.height*t,n.style.width=`${e.width}px`,n.style.height=`${e.height}px`;let o=n.getContext("2d");if(!o)return n;o.scale(t,t),o.fillStyle=e.color;let i=e.height/2,s=Math.max(1,e.width),a=r.length/s;for(let c=0;c<s;c++){let l=Math.floor(c*a),d=Math.max(l+1,Math.floor((c+1)*a)),u=0;for(let m=l;m<d&&m<r.length;m++)r[m]>u&&(u=r[m]);let f=u*e.height;o.fillRect(c,i-f/2,1,f)}return n}var vi=[2048,1024,512,256,128,64,32];async function Ra(r,e){let t=await r.getAudioDuration(e)??0,n=r.decodeAudio({source:e}).readable,o=vi[vi.length-1],i=[],s=0,a=0,c=0;for await(let u of n){c||=u.sampleRate;let f=u.numberOfFrames,m=new Float32Array(f);u.copyTo(m,{planeIndex:0});for(let p=0;p<m.length;p++){let g=Math.abs(m[p]);g>s&&(s=g),a++,a>=o&&(i.push(s),s=0,a=0)}u.close()}a>0&&i.push(s);let l=new Float32Array(i),d=vi.map(u=>{let f=Math.max(1,Math.round(u/o)),m=f===1?l:Xc(l,f);return{samplesPerPeak:u,peaks:m,peaksPerSecond:c>0?c/u:0}});return{duration:t,levels:d}}function Xc(r,e){let t=new Float32Array(Math.ceil(r.length/e));for(let n=0;n<t.length;n++){let o=0,i=n*e,s=Math.min(i+e,r.length);for(let a=i;a<s;a++)r[a]>o&&(o=r[a]);t[n]=o}return t}var Qc=4096,Zn=class r{#e=new Map;#t=[0,0];#r=[0,0];#n;#o;#s;#i=!1;color;duration;tileSize;tileHeight;preloadMargin;constructor(e,t,n){this.#o=e,this.duration=t,this.tileSize=n.tileSize??1,this.#n=n.zoom??(n.tileWidth??256)/this.tileSize,this.tileHeight=n.tileHeight??96,this.preloadMargin=n.preloadMargin??2,this.color=n.color??"rgb(3, 148, 129)",this.#s=n.onChange}static async init(e,t,n={}){let{duration:o,levels:i}=await Ra(e,t);return new r(i,o,n)}set zoom(e){let t=Math.max(1,e);t!==this.#n&&(this.#n=t,this.#e.clear(),this.#a())}get zoom(){return this.#n}get range(){return this.#r}#c([e,t],n=1){let o=t-e;return[Math.max(0,e-o*n),Math.min(this.duration,t+o*n)]}set range(e){this.#r=e;let[t,n]=e,o=n-t,[i,s]=this.#t,a=i>0,c=s<this.duration,l=!a||t>=i+o,d=!c||n<=s-o;l&&d||(this.#t=this.#c(e,this.preloadMargin),this.#a())}#a(){this.#i||(this.#i=!0,queueMicrotask(()=>{this.#i=!1,this.#l()}))}#l(){let[e,t]=this.#t,n=new Set,o=this.#d(),i=Math.max(0,Math.floor(e/this.tileSize)*this.tileSize),s=Math.min(this.duration,t);for(let a=i;a<=s;a+=this.tileSize)n.add(a);for(let a of n)if(!this.#e.has(a)){let c=Math.min(a+this.tileSize,this.duration);this.#e.set(a,this.#u(a,c,o))}for(let a of this.#e.keys())n.has(a)||this.#e.delete(a);this.#p()}#u(e,t,n){let o=this.#f(n,e,t);return{startTime:e,endTime:t,peaks:o,canvas:Da(o,{width:this.#m(e,t),height:this.tileHeight,color:this.color})}}#d(){return this.#o.find(e=>e.peaksPerSecond>=this.#n)??this.#o[this.#o.length-1]}#f(e,t,n){if(!e.peaksPerSecond)return new Float32Array;let o=Math.max(0,Math.floor(t*e.peaksPerSecond)),i=Math.max(o+1,Math.min(e.peaks.length,Math.ceil(n*e.peaksPerSecond)));return e.peaks.slice(o,i)}#m(e,t){return Math.min(Qc,Math.max(1,Math.ceil((t-e)*this.#n)))}#p(){this.#s&&this.#s([...this.#e.values()].sort((e,t)=>e.startTime-t.startTime))}getTiles(){return this.#e}};var U=r=>r;var Gt=r=>r;var Ma=()=>{let r=!1,e=Gt(60),t=1e3/e,n=0,o=0,i=le(),s=null,a=l=>{if(requestAnimationFrame(a),!!r)for(n=l;l-o>=t;)o+=t,s?.(),s=null,i()};async function*c(){for(n=performance.now(),o=n,requestAnimationFrame(a);;)await new Promise(l=>s=l),yield}return{play(){r||(r=!0,n=performance.now(),o=n)},pause(){r=!1},setFPS(l){e=l,t=1e3/e},isPlaying(){return r},ticks:c,onTick:i}},Ba=async(r,e)=>{let t=U(1e3/r.fps),n=r.duration/1e3,o=Math.ceil(n*r.fps);for(let i=0;i<o;i++){let s=U(i*t);await e(s,i)}};function za(r){return"duration"in r}function Ua(r){let e=[],t=new Map(r.timeline.items.map(n=>[n.id,n]));return Si(r.timeline.rootId,t,r.from,{sequence:()=>{},stack:()=>{},video:(n,o,i)=>e.push({item:n,localTime:o,ancestors:i}),text:(n,o,i)=>e.push({item:n,localTime:o,ancestors:i}),audio:(n,o,i)=>e.push({item:n,localTime:o,ancestors:i})}),e}function Na(r,e,t){let n=Sa;for(let o of e)n=Oa(r,o,n);return Oa(r,t,n)}function Oa(r,e,t){if("spatialId"in e&&e.spatialId){let n=r.get(e.spatialId);if(n?.enabled){let o=wa(n.transform);return va(o,t)}}return t}function Si(r,e,t,n,o=[]){let i=e.get(r);if(i)switch(i.kind){case M.Stack:n.stack(i,t,o);for(let s of i.childrenIds)Si(s,e,t,n,[...o,i]);break;case M.Sequence:{n.sequence(i,t,o);let s=U(0);for(let a of i.childrenIds){let c=e.get(a);if(!c||!za(c))continue;let l=U(s+c.duration);if(t>=l){s=l;continue}let d=U(Math.max(0,t-s));Si(a,e,d,n,[...o,i]),s=l}break}case M.Video:n.video(i,t,o);break;case M.Text:n.text(i,t,o);break;case M.Audio:n.audio(i,t,o);break}}function he(r,e){let t=e.items.find(n=>n.id===r);if(!t)return U(0);switch(t.kind){case M.Sequence:{let n=t.childrenIds.map(i=>e.items.find(s=>s.id===i)).filter(Boolean),o=U(0);for(let i=0;i<n.length;i++){let s=n[i];if(s.kind===M.Transition){let a=n[i-1],c=n[i+1];if(a&&c&&a.kind!==M.Transition&&c.kind!==M.Transition){let l=he(a.id,e),d=he(c.id,e),u=Math.max(0,Math.min(s.duration,l,d));o=U(o-u)}continue}o=U(o+he(s.id,e))}return o}case M.Stack:{let n=U(0);for(let o of t.childrenIds){let i=he(o,e);i>n&&(n=i)}return n}default:return za(t)?t.duration:U(0)}}var Pr=r=>r;var Jn=class{resolveMedia;#e=new Map;constructor(e){this.resolveMedia=e}async getSink(e){let t=this.#e.get(e);if(t)return t.sink;let n=new ee({formats:ie,source:await ce(this.resolveMedia(e))}),o=await n.getPrimaryVideoTrack(),s=!!o&&await o.canDecode()&&o?new Ct(o):null;return this.#e.set(e,{input:n,sink:s}),s}};async function Va(r,e,t,n){let[o,i]=await Promise.all([t,n]),s=o.find(l=>l.kind==="image")?.frame,a=i.find(l=>l.kind==="image")?.frame,c=[...o.filter(l=>l.kind!=="image"),...i.filter(l=>l.kind!=="image")];return s&&a?[{id:r.id,kind:"transition",name:"circle",progress:e,from:s,to:a},...c]:c}async function La(r,e,t,n){let o=Yc(r,e,t);if(!o)return[];let i=[...n,e];return o.isTransitioning?Va(o.transition,o.progress,mt(r,o.outgoing,o.outgoingTime,i),mt(r,o.incoming,o.incomingTime,i)):mt(r,o.item,o.localTime,i)}function Yc(r,e,t){let n=e.childrenIds.map(i=>r.items.get(i)).filter(i=>!!i),o=U(0);for(let i=0;i<n.length;i++){let s=n[i];if(s.kind===M.Transition)continue;let a=o,c=he(s.id,r.timeline),l=U(a+c),d=n[i+1];if(!(d?.kind===M.Transition)){if(t<l)return{isTransitioning:!1,item:s,localTime:U(t-a)};o=l;continue}let f=d,m=n[i+2];if(!m||m.kind===M.Transition){o=l;continue}let p=he(m.id,r.timeline),g=Math.max(0,Math.min(f.duration,c,p)),x=U(l-g);if(t<x)return{isTransitioning:!1,item:s,localTime:U(t-a)};if(t<l){let y=U(t-x),b=U(t-a);return{isTransitioning:!0,incoming:m,outgoing:s,outgoingTime:b,incomingTime:y,progress:g>0?y/g:1,transition:f}}o=x,i++}return null}async function mt(r,e,t,n){let o=Na(r.items,n,e),i="spatialId"in e&&e.spatialId?r.items.get(e.spatialId)?.crop:void 0;switch(e.kind){case M.Stack:{let s=[...n,e];return(await Promise.all(e.childrenIds.map(c=>r.items.get(c)).filter(c=>!!c).map(c=>mt(r,c,t,s)))).flat()}case M.Sequence:return La(r,e,t,n);case M.Video:{if(t<0||t>=e.duration)return[];let s=await r.videoSampler(e,t);return s?[{kind:"image",frame:s,matrix:o,crop:i,id:e.id}]:[]}case M.Text:{if(t<0||t>=e.duration)return[];let s=e.styleId?r.items.get(e.styleId)?.style:void 0;return[{id:e.id,kind:"text",content:e.content,style:s,matrix:o,crop:i}]}case M.Gap:return[{id:e.id,kind:"gap"}];default:return[]}}function Ha(r){return async(e,t)=>{let o=await(await r.getSink(e.mediaHash))?.getSample(t/1e3),i=o?.toVideoFrame();return o?.close(),i??void 0}}function eo(r,e){let t=new Jn(r),n=e??Ha(t);return{async sample(o,i){let s=new Map(o.items.map(c=>[c.id,c])),a=s.get(o.rootId);return a?mt({videoSampler:n,timeline:o,items:s},a,i,[]):[]}}}var Wt=class{driver;resolveMedia;timeline;#e=-1/0;#t=new Map;#r;constructor(e,t,n){this.driver=e,this.resolveMedia=t,this.timeline=n,this.#r=eo(this.resolveMedia,(o,i)=>{let s=qa(i),a=this.#t.get(o.id);if(!a){let c=this.resolveMedia(o.mediaHash),l=qa(U(o.start+o.duration));a=this.#n(c,s,l),this.#t.set(o.id,a)}return a.next(s)})}next(e){if(e<this.#e)throw new Error(`Forward-only cursor regression: ${e}ms < ${this.#e}ms`);return this.#e=e,this.#r.sample(this.timeline,e)}async cancel(){await Promise.all([...this.#t.values()].map(e=>e.cancel())),this.#t.clear()}#n(e,t,n){let o=this.driver.decodeVideo({source:e,start:t/1e6,end:n/1e6}),i=o.readable.getReader(),s=null,a=null,c=!1,l=async()=>{if(c)return null;let{done:d,value:u}=await i.read();if(d)return c=!0,null;let f=new VideoFrame(u);return u.close(),f};return{async next(d){if(s??=await l(),!!s)for(;;){a??=l();let u=await a;if(!u)return new VideoFrame(s);let f=s.timestamp??-1/0,m=u.timestamp??f;if(m<d){s.close(),s=u,a=null;continue}if(Math.abs(m-d)<Math.abs(f-d)){s.close(),s=u,a=null;continue}return new VideoFrame(s)}},async cancel(){let d=a;a=null,c=!0,(await d?.catch(()=>null))?.close(),s?.close(),s=null,o.cancel()}}}},qa=r=>Math.round(r*1e3);async function Ga(r,e,t){return(await Promise.all(e.map(async({item:o,localTime:i})=>{if(o.kind!==M.Audio)return;let s=await r.getSink(o.mediaHash);if(!s)return;let a=o.start+i,c=Pr((t-a)/1e3),l=s.samples(a/1e3),d=await l.next();if(d.done)return;let u=d.value,f=l.next();return{offset:c,gain:o.gain??1,get currentSample(){return u},timelineTime:()=>Pr(c+u.timestamp),output:()=>({sample:u,timestamp:c+u.timestamp,gain:o.gain??1}),advance:async()=>{let m=await f;return m.done?!1:(u=m.value,f=l.next(),!0)}}}))).filter(o=>!!o)}var to=class{resolveMedia;#e=new Map;constructor(e){this.resolveMedia=e}async getSink(e){let t=this.#e.get(e);if(t)return t.sink;let n=new ee({formats:ie,source:await ce(this.resolveMedia(e))}),o=await n.getPrimaryAudioTrack(),s=!!o&&await o.canDecode()&&o?new mr(o):null;return this.#e.set(e,{input:n,sink:s}),s}};function Wa(r){let e={index:0,stream:r[0],time:r[0].timelineTime()};for(let[t,n]of r.entries()){let o=n.timelineTime();o<e.time&&(e={time:o,stream:n,index:t})}return e}function ro(r){let e=new to(r);return{async*sampleAudio(t,n){let o=Ua({timeline:t,from:n}),i=await Ga(e,o,n);for(;i.length>0;){let{stream:s,index:a}=Wa(i);yield s.output(),await s.advance()||i.splice(a,1)}}}}var no=class{driver;timeline;resolveMedia;audioSampler;seekVisualSampler;playVisualSampler=null;#e=U(0);#t=null;#r=Ma();onTick=this.#r.onTick;audioContext=new AudioContext({sampleRate:48e3});audioGain=this.audioContext.createGain();audioNodes=new Set;#n=null;constructor(e,t,n){this.driver=e,this.timeline=t,this.resolveMedia=n,this.audioGain.connect(this.audioContext.destination),this.audioGain.gain.value=.7**2,this.seekVisualSampler=eo(this.resolveMedia),this.audioSampler=ro(this.resolveMedia),this.#o()}update(e){this.timeline=e}get isPlaying(){return this.#r.isPlaying()}async#o(){for await(let e of this.#r.ticks()){let t=await this.playVisualSampler?.next(this.currentTime)??[];(await this.driver.composite(t)).close(),this.currentTime>=this.duration&&this.pause()}}async seek(e){return this.pause(),this.#e=e,await this.seekVisualSampler.sample(this.timeline,e)}async start(){if(!this.#r.isPlaying()){await this.audioContext.resume(),this.#e=this.currentTime,this.#t=this.audioContext.currentTime,this.#n?.abort(),this.#n=new AbortController;for(let e of this.audioNodes)e.stop();this.audioNodes.clear(),this.playVisualSampler=new Wt(this.driver,this.resolveMedia,this.timeline),this.#r.play(),this.#s(this.#n.signal,Pr(this.#e/1e3))}}pause(){this.#e=this.currentTime,this.#r.pause(),this.#n?.abort();for(let e of this.audioNodes)e.stop();this.audioNodes.clear(),this.playVisualSampler&&(this.playVisualSampler.cancel(),this.playVisualSampler=null)}get duration(){return he(this.timeline.rootId,this.timeline)}get currentTime(){if(!this.#r.isPlaying()||this.#t===null)return this.#e;let e=(this.audioContext.currentTime-this.#t)*1e3;return U(this.#e+e)}setFps(e){this.#r.setFPS(e)}async#s(e,t){let n=this.audioContext;if(this.#t!==null)for await(let{sample:o,timestamp:i}of this.audioSampler.sampleAudio(this.timeline,U(t*1e3))){if(e.aborted||!this.#r.isPlaying())return;for(;i-(n.currentTime-this.#t+t)>.75;)await new Promise(c=>setTimeout(c,25));let s=n.createBufferSource();s.buffer=o.toAudioBuffer(),s.connect(this.audioGain),s.onended=()=>this.audioNodes.delete(s),this.audioNodes.add(s);let a=this.#t+i-t;a>=n.currentTime?s.start(a):s.start(n.currentTime,n.currentTime-a)}}};var oo=class{driver;canvas;playback;#e=null;#t=null;constructor(e,t,n){this.driver=e,this.playback=new no(e,n,t),this.canvas=e.compositor.pixi.renderer.canvas}async play(){await this.playback.start()}pause(){this.playback.pause()}seek(e){return this.#e=e,this.#t??=this.#r().finally(()=>this.#t=null)}setFPS(e){this.playback.setFps(Gt(e))}get isSeeking(){return this.#t!==null}get isPlaying(){return this.playback.isPlaying}get duration(){return this.playback.duration}get currentTime(){return this.playback.currentTime}update(e){this.playback.update(e)}async#r(){for(;this.#e!==null;){let e=this.#e;this.#e=null;let t=await this.playback.seek(U(e));(await this.driver.composite(t)).close()}}};var pt=class{state;constructor(e){this.state=e}require(e){return e===void 0?void 0:this.state.timeline.items.find(n=>n.id===e)}get timeline(){return this.state.timeline}getId(){return ge.toInteger(ge.random())}#e(e){this.state.timeline=e(this.state.timeline)}register(e){this.#e(t=>({...t,items:[...t.items,e]}))}textStyle=e=>{let t={id:this.getId(),kind:M.TextStyle,style:e};return this.register(t),t};spatial=(e,t)=>{let n={id:this.getId(),kind:M.Spatial,transform:e??this.transform(),crop:t,enabled:!0};return this.register(n),n};sequence=(...e)=>{let t={id:this.getId(),kind:M.Sequence,childrenIds:e.map(n=>n.id)};return this.register(t),t};stack=(...e)=>{let t={kind:M.Stack,id:this.getId(),childrenIds:e.map(n=>n.id)};return this.register(t),t};video=(e,t)=>{if(!e.hasVideo)throw new Error(`Video clip error: media "${e.datafile.filename}" has no video track.`);let n={kind:M.Video,id:this.getId(),mediaHash:e.datafile.checksum.hash,start:t?.start??0,duration:t?.duration??e.duration};return this.register(n),n};audio=(e,t)=>{if(!e.hasAudio)throw new Error(`Audio clip error: media "${e.datafile.filename}" has no audio track.`);let n={kind:M.Audio,id:this.getId(),mediaHash:e.datafile.checksum.hash,start:t?.start??0,duration:t?.duration??e.duration,gain:t?.gain??1};return this.register(n),n};text=(e,t)=>{let n={id:this.getId(),content:e,kind:M.Text,duration:t?.duration??2e3};return t?.styles&&(n.styleId=this.textStyle(t.styles).id),this.register(n),n};gap=e=>{let t={id:this.getId(),kind:M.Gap,duration:e};return this.register(t),t};transition={crossfade:e=>{let t={id:this.getId(),kind:M.Transition,effect:Kn.Crossfade,duration:e};return this.register(t),t}};transform=e=>{let t=[e?.position?.[0]??0,e?.position?.[1]??0],n=[e?.scale?.[0]??1,e?.scale?.[1]??1],o=e?.rotation??0;return[t,n,o]};addChildren(e,...t){this.#e(n=>(n.items.find(({id:i})=>i===e.id).childrenIds.push(...t.map(i=>i.id)),n))}set=(e,t)=>{this.#e(n=>({...n,items:n.items.map(o=>o.id===e?{...o,...t}:o)}))}};var io=class{#e;#t;constructor(e={}){this.#e=e.chunkFrames??1024,this.#t=e.clamp??!0}async*mix(e){let t=this.#e,n=null,o=null,i=[],s=0,a=0;for await(let c of e){if(o===null)o=c.planes.length,n=c.sampleRate;else{if(c.planes.length!==o)throw new Error("Channel count changed");if(c.sampleRate!==n)throw new Error("Sample rate changed")}let l=Math.round(c.timestamp*n),d=c.planes[0]?.length??0;for(;s+t<=l;)yield this.#r(i,s,o,n),s+=t;i.push({startFrame:l,endFrame:l+d,data:c.planes}),a=Math.max(a,l+d)}if(o!==null&&n!==null)for(;s<a;)yield this.#r(i,s,o,n),s+=t}#r(e,t,n,o){let i=this.#e,s=new Float32Array(n*i),a=t+i;for(let c=0;c<n;c++){let l=c*i,d=s.subarray(l,l+i);for(let u of e){let f=u.data[c];if(!f)continue;let m=Math.max(t,u.startFrame),p=Math.min(a,u.endFrame);if(m>=p)continue;let g=m-t,x=m-u.startFrame,y=p-m;for(let b=0;b<y;b++)d[g+b]+=f[x+b]}if(this.#t)for(let u=0;u<i;u++){let f=d[u];d[u]=f<-1?-1:f>1?1:f}}for(let c=e.length-1;c>=0;c--)e[c].endFrame<=a&&e.splice(c,1);return{planar:s,sampleRate:o,channels:n,frames:i,startFrame:t}}};var Zc=(r,e,t)=>{if(e===t)return r;let n=t/e,o=Math.max(1,Math.round(r.length*n)),i=new Float32Array(o);for(let s=0;s<o;s++){let a=s/n,c=Math.floor(a),l=Math.min(c+1,r.length-1),d=a-c;i[s]=r[c]*(1-d)+r[l]*d}return i},ja=(r,e)=>{let t=r.numberOfChannels,n=new Array(t),o=0;for(let i=0;i<t;i++){let s=new Float32Array(r.numberOfFrames);r.copyTo(s,{planeIndex:i,format:"f32-planar"});let a=Zc(s,r.sampleRate,e);n[i]=a,o=a.length}return{data:n,frames:o}};var $a=(r,e)=>{if(e!==1)for(let t of r)for(let n=0;n<t.length;n++)t[n]*=e};function Ka({timeline:r,resolveMedia:e}){let t=new io,n=Jc(r,e),o=new TransformStream,i=o.writable.getWriter();async function s(){for await(let a of t.mix(n)){let c=new AudioData({format:"f32-planar",sampleRate:a.sampleRate,numberOfFrames:a.frames,numberOfChannels:a.channels,timestamp:Math.round(a.startFrame/a.sampleRate*1e6),data:new Float32Array(a.planar)});await i.write(c)}await i.close()}return s(),o.readable}async function*Jc(r,e){let t=ro(e);for await(let{sample:n,timestamp:o,gain:i}of t.sampleAudio(r,U(0))){let{data:s}=ja(n,48e3);$a(s,i),yield{planes:s,sampleRate:48e3,timestamp:o},n.close()}}function Xa({timeline:r,fps:e,driver:t,resolveMedia:n}){let o=new TransformStream,i=o.writable.getWriter(),s=new Wt(t,n,r),a=1/e,c=he(r.rootId,r);async function l(){await Ba({fps:e,duration:c},async(d,u)=>{let f=await s.next(d),m=await t.composite(f),p=new VideoFrame(m,{timestamp:Math.round(u*a*1e6),duration:Math.round(a*1e6)});await i.write(p),m.close()}),await i.close()}return l(),o.readable}function Qa(r){let e=Ka({...r}),t=Xa({...r});return r.driver.encode({video:t,audio:e,config:{audio:{codec:"opus",bitrate:128e3},video:{codec:"vp9",bitrate:1e6}}})}var so=class{driver;resources=new Qn;constructor(e){this.driver=e}load=async e=>Object.fromEntries(await Promise.all(Object.entries(e).map(async([t,n])=>[t,await this.resources.store(await n)])));timeline=e=>{let t=new pt({timeline:{format:"timeline",info:"https://omniclip.app/",version:0,items:[],rootId:0}}),n=e(t);return t.timeline.rootId=n.id,t.timeline};playback=async e=>new oo(this.driver,t=>this.resources.require(t).url,e);render=async(e,t=30)=>Qa({timeline:e,fps:Gt(t),driver:this.driver,resolveMedia:n=>this.resources.require(n).url})};var ao=class{data;bytes;hash;nickname;constructor(e,t,n,o){this.data=e,this.bytes=t,this.hash=n,this.nickname=o}static async make(e){let t=new Uint8Array(e),n=new Uint8Array(await crypto.subtle.digest("SHA-256",t)),o=_r.fromBytes(n),i=Mi.sigil.fromBytes(n);return new this(e,n,o,i)}};var co=class{url;bytes;blob;filename;checksum;constructor(e,t,n,o,i){this.url=e,this.bytes=t,this.blob=n,this.filename=o,this.checksum=i}static async make(e,t){let n=await e.arrayBuffer(),o=new Uint8Array(n),i=await ao.make(o),s=t??i.nickname,a=URL.createObjectURL(e);return new this(a,o,e,s,i)}static async load(e){}};async function Za(r,e,t){let n=t.querySelector(".play"),o=t.querySelector(".stop"),i=t.querySelector(".scrub"),s=t.querySelector(".playhead"),a=t.querySelector(".timecode"),c=t.querySelector(".player-canvas"),l=new pt({timeline:r}),d=await e.playback(r);c.replaceChildren(d.canvas),n.disabled=!1,o.disabled=!1,n.addEventListener("click",()=>d.play()),o.addEventListener("click",()=>d.pause()),i.max=String(Math.ceil(d.duration));let u=!1;d.playback.onTick.on(()=>m(d.currentTime,d.duration));let f=(p,g)=>{a.textContent=`${Ya(p)} / ${Ya(g)}`};i.addEventListener("input",async()=>{u=!0;let p=Math.max(0,Math.min(+i.value,d.duration));f(p,d.duration),await d.seek(p)}),i.addEventListener("change",async()=>{u=!1;let p=Math.max(0,Math.min(+i.value,d.duration));await d.seek(p)});let m=(p,g)=>{let x=Math.max(0,Math.min(p,g));u||(i.value=String(Math.round(x)));let y=g?x/g*100:0;s.style.left=`${y}%`,f(x,g)};d.update(l.timeline)}function Ya(r){let e=Math.max(0,r),t=Math.floor(e/1e3),n=Math.floor(t/60),o=t%60,i=Math.floor(e%1e3);return`${n}:${String(o).padStart(2,"0")}.${String(i).padStart(3,"0")}`}async function ec(r,e,t){let n=t.querySelector(".waveform-canvas"),o=t.querySelector(".width");n.replaceChildren(),n.style.position="relative",n.style.height="96px",n.style.overflow="hidden";let i=await Zn.init(r,e,{tileHeight:96,onChange:()=>s()}),s=()=>{let a=+o.value,c=Ja(a,i.duration);i.zoom=c,n.style.width=`${a}px`,n.replaceChildren(...[...i.getTiles().values()].map(l=>(l.canvas.style.position="absolute",l.canvas.style.top="0",l.canvas.style.left=`${l.startTime*Ja(a,i.duration)}px`,l.canvas.style.height="100%",l.canvas)))};o.oninput=s,i.range=[0,i.duration]}function Ja(r,e){return e>0?r/e:0}async function Pi(r,e){let t=new so(r),{videoA:n}=await t.load({videoA:co.make(e)});return{timeline:t.timeline(i=>{let s=i.text("content",{duration:1e3}),a=i.textStyle({fill:"green",fontSize:100}),c=i.spatial(i.transform({position:[240,160],scale:[1.4,1.4],rotation:.08}),[.15,.1,.05,.2]),l=i.video(n,{duration:3e3,start:1e3});return i.set(s.id,{styleId:a.id}),i.set(l.id,{spatialId:c.id}),i.sequence(i.stack(s,l,i.audio(n,{duration:1e3})),i.gap(500),i.video(n,{duration:7e3,start:5e3}))}),omni:t}}async function tc(r,e){let t=e.querySelector(".range"),n=e.querySelector(".range-view"),o=e.querySelector(".range-size"),i=e.querySelector(".frequency"),s=e.querySelector(".frequency-view"),a=e.querySelector(".filmstrip"),c=1/10,l=.5;a.replaceChildren();let d=await Yn.init(r,{onChange(u){let f=u.sort((m,p)=>m.time-p.time);a.replaceChildren(...f.map(({time:m,canvas:p})=>el(m,p.canvas)))},frequency:c,canvasSinkOptions:{width:80,height:50,fit:"fill"}});o.oninput=()=>{l=+o.value;let u=+t.value,f=u+l;d.range=[u,f],n.textContent=`visible time range: [${u}, ${f}]`},t.oninput=()=>{let u=+t.value,f=u+l;d.range=[u,f],n.textContent=`visible time range: [${u}, ${f}]`},i.oninput=()=>{d.frequency=1/+i.value,s.textContent=`frame every ${d.frequency.toFixed(3)} second (${i.value} frames per second)`},d.range=[10,10.5]}function el(r,e){let t=document.createElement("div");t.style.position="relative",t.style.display="inline-block",t.style.marginRight="4px",t.appendChild(e);let n=document.createElement("div");return n.textContent=`${r.toFixed(2)}s`,n.style.position="absolute",n.style.top="2px",n.style.right="4px",n.style.fontSize="10px",n.style.color="white",n.style.background="rgba(0,0,0,0.6)",n.style.padding="2px 4px",n.style.borderRadius="4px",n.style.pointerEvents="none",t.appendChild(n),t}function rc(r,e){let t={width:1920,height:1080},n=document.createElement("canvas");n.width=t.width,n.height=t.height;let o=n.getContext("2d");async function i(){let s=r.decodeVideo({source:e,async onFrame(f){let m=await r.composite([{id:0,kind:"image",frame:f},{id:1,kind:"text",content:"omnitool",style:{fontSize:50,fill:"green"}}]);return f.close(),o?.drawImage(m,0,0),m}}),a=r.decodeAudio({source:e}),{readable:c,done:l}=r.encode({video:s.readable,audio:a.readable,config:{audio:{codec:"opus",bitrate:128e3},video:{codec:"vp9",bitrate:1e6}}}),u=await(await window.showSaveFilePicker()).createWritable();await Promise.all([c.pipeTo(u),l])}return{canvas:n,run:i}}var jt=await $n.setup({workerUrl:new URL("../driver/driver.worker.bundle.min.js",import.meta.url)}),tl=document.querySelector("[data-demo='transcode']"),rl=document.querySelector("[data-demo='filmstrip']"),nl=document.querySelector("[data-demo='waveform']"),nc=document.querySelector("[data-demo='playback']"),ht=document.querySelector("[data-demo='export']"),oc=ht.querySelector("[data-action='export']"),lo=null;await jt.thread.work.hello(),jt.machina.count===1?console.log("\u2705 driver works"):console.error("\u274C FAIL driver call didn't work");var $t=(r,e)=>{let t=r.querySelector(".progress"),n=r.querySelector(".status");!t||!n||(e==="running"?(t.removeAttribute("value"),n.textContent="running"):e==="done"?(t.value=1,n.textContent="done"):(t.value=0,n.textContent="idle"))},Ii=(r,e)=>{let t=r.querySelector("input[type='file']"),n=r.querySelector("[data-action='run']");n.disabled=!0,t.addEventListener("input",()=>{n.disabled=!t.files?.length}),n.addEventListener("click",async()=>{let o=t.files?.[0];if(o){n.disabled=!0,$t(r,"running");try{await e(o,r),$t(r,"done")}finally{n.disabled=!1}}})};Ii(tl,async(r,e)=>{let t=e.querySelector(".demo-preview"),n=rc(jt,r);t.replaceChildren(n.canvas),await n.run()});Ii(rl,async(r,e)=>{await tc(r,e)});Ii(nl,async(r,e)=>{await ec(jt,r,e)});{let r=nc.querySelector("input[type='file']");r.addEventListener("input",async()=>{let e=r.files?.[0];if(!e)return;let{timeline:t,omni:n}=await Pi(jt,e);await Za(t,n,nc)})}{let r=ht.querySelector("input[type='file']");r.addEventListener("input",async()=>{let e=r.files?.[0];if(!e)return;$t(ht,"running");let{timeline:t,omni:n}=await Pi(jt,e);lo={timeline:t,omni:n},oc.disabled=!1;let o=ht.querySelector(".demo-preview"),i=await n.playback(t);await i.seek(0),o.replaceChildren(i.canvas),$t(ht,"done")})}oc.addEventListener("click",async()=>{lo&&($t(ht,"running"),await lo.omni.render(lo.timeline),$t(ht,"done"))});
|
|
2415
|
+
`;function Ea({name:r,renderer:e}){let t=_a.default.find(c=>c.name===r),n=new xt,o=new gt,i=new Cr({}),s=new Cr({}),a=new xo({glProgram:new po({vertex:Ca,fragment:Aa(t.glsl)}),resources:{from:i,to:s,uniforms:{...ki.basics,...ki.custom(t)}}});return n.filters=[a],{render({width:c,height:l,from:d,to:u,progress:f}){return(n.width!==c||n.height!==l)&&(n.setSize({width:c,height:l}),o.source.resize(c,l)),i.resource=d,s.resource=u,i.update(),s.update(),a.resources.uniforms.uniforms.progress=f,e.render({container:n,target:o,clear:!1,width:c,height:l}),o}}}var jn=class{pixi;onPointerDown=le();onPointerMove=le();onPointerUp=le();onDispose=le();static async setup(){let e=await wi({width:1920,height:1080,preference:"webgl",background:"black",preferWebGLVersion:2}),t=new mo;return t.interactive=!0,new this({renderer:e,stage:t})}constructor(e){this.pixi=e}#e=new Map;#t=new Map;#r=new WeakMap;async composite(e){let{stage:t,renderer:n}=this.pixi;this.#d(this.#u(e));let{dispose:o}=await this.#n(e,t);n.render(t);let i=new VideoFrame(n.canvas,{timestamp:0,duration:0});return o(),i}getActiveObject(e){return this.#t.get(e)?.sprite}async#n(e,t){if(Array.isArray(e)){let n=[];for(let o of[...e].reverse()){let i=await this.#n(o,t);n.push(i.dispose)}return{dispose:()=>n.forEach(o=>o())}}switch(e.kind){case"text":return this.#o(e,t);case"image":return this.#s(e,t);case"transition":return this.#i(e,t);case"gap":return this.pixi?.renderer.clear(),{dispose:()=>{}};default:return console.warn("Unknown layer kind",e.kind),{dispose:()=>{}}}}#o(e,t){let n=this.#l(e);return this.#c(n,e.matrix),this.#a(n,e.crop),t.addChild(n),{dispose:()=>{}}}#s(e,t){let n=this.#l(e);n.texture&&n.texture!==gt.EMPTY&&n.texture.destroy(!0);let o=gt.from(e.frame);return n.texture=o,this.#c(n,e.matrix),this.#a(n,e.crop),t.addChild(n),{dispose:()=>{e.frame.close()}}}#i({from:e,to:t,progress:n,name:o},i){let a=(this.#e.get(o)??(this.#e.set(o,Ea({name:"circle",renderer:this.pixi.renderer})),this.#e.get(o))).render({from:e,to:t,progress:n,width:e.displayWidth,height:e.displayHeight}),c=new xt(a);return i.addChild(c),{dispose:()=>c.destroy(!1)}}#c(e,t){if(!t)return;let n=ka(t);e.setFromMatrix(n)}#a(e,t){let n=this.#r.get(e);if(n&&(e.mask===n&&(e.mask=null),n.removeFromParent(),n.clear()),!t||t.every(g=>g===0))return;let[o,i,s,a]=t,c=e.mask;e.mask=null;let l=e.getLocalBounds();e.mask=c;let d=l.x+l.width*a,u=l.y+l.height*o,f=l.width*Math.max(0,1-a-i),m=l.height*Math.max(0,1-o-s),p=n??new go;p.clear(),p.beginFill(16777215),p.drawRect(d,u,f,m),p.endFill(),e.addChild(p),e.mask=p,this.#r.set(e,p)}#l(e){let t=this.#t.get(e.id);if(t)return t.sprite;switch(e.kind){case"text":{let n=new Sr({text:e.content,style:e.style});n.eventMode="static";let o=a=>this.onPointerDown.publish({event:a,id:e.id,object:n}),i=a=>this.onPointerUp.publish({event:a,id:e.id,object:n}),s=a=>this.onPointerUp.publish({event:a,id:e.id,object:n});return n.on("pointerdown",o),n.on("pointerup",i),n.on("pointermove",s),this.#t.set(e.id,{sprite:n,dispose:()=>{this.onDispose.publish({id:e.id,object:n}),n.off("pointermove",s),n.off("pointerdown",o),n.off("pointerup",i)}}).get(e.id)?.sprite}case"image":{let n=new xt;n.eventMode="static";let o=a=>this.onPointerDown.publish({event:a,id:e.id,object:n}),i=a=>this.onPointerUp.publish({event:a,id:e.id,object:n}),s=a=>this.onPointerUp.publish({event:a,id:e.id,object:n});return n.on("pointerdown",o),n.on("pointermove",s),n.on("pointerup",i),this.#t.set(e.id,{sprite:n,dispose:()=>{this.onDispose.publish({id:e.id,object:n}),n.off("pointerdown",o),n.off("pointermove",s),n.off("pointerup",i)}}).get(e.id)?.sprite}}}#u(e){let t=new Set,n=o=>{if(Array.isArray(o))for(let i of o)n(i);else t.add(o.id)};return n(e),t}#d(e){for(let t of this.#t.keys())if(!e.has(t)){let{sprite:n,dispose:o}=this.#t.get(t);o(),n.destroy(!0),this.#t.delete(t)}}};var Fa=r=>jr.host(e=>({async world(){r.count++}}));async function ce(r){return r instanceof Blob?new kr(r):new vr(r)}var $n=class{machina;thread;compositor;static async setup(e){let t=new Hn,n=await jr.thread({label:"OmnitoolDriver",workerUrl:e?.workerUrl??"/node_modules/@omnimedia/omnitool/x/driver/driver.worker.bundle.min.js",setupHost:Fa(t)}),o=await jn.setup();return new this(t,n,o)}constructor(e,t,n){this.machina=e,this.thread=t,this.compositor=n}async hello(){return this.thread.work.hello()}async getAudioDuration(e){let n=await new ee({source:await ce(e),formats:ie}).getPrimaryAudioTrack();if(!n)throw new Error("primary audio track not found");return await n.computeDuration()}async getVideoDuration(e){return await(await new ee({source:await ce(e),formats:ie}).getPrimaryVideoTrack())?.computeDuration()}decodeVideo(e){let t=null,{port1:n,port2:o}=new MessageChannel,i=new TransformStream({async transform(s,a){let c=await e.onFrame?.(s)??s;t?.close(),a.enqueue(c),t=c}});return this.thread.work.decodeVideo[Tt]({transfer:[i.writable,o]})({source:e.source,cancel:o,video:i.writable,start:e.start,end:e.end}),{readable:i.readable,cancel(){n.postMessage("close"),n.close()}}}decodeAudio(e){let t=new TransformStream,{port1:n,port2:o}=new MessageChannel;return this.thread.work.decodeAudio[Tt]({transfer:[t.writable,o]})({source:e.source,cancel:o,audio:t.writable,start:e.start,end:e.end}),{readable:t.readable,cancel(){n.postMessage("close"),n.close()}}}encode({audio:e,video:t,config:n}){let{readable:o,writable:i}=new TransformStream,s=[e,t,i].filter(Er.happy),a=this.thread.work.encode[Tt]({transfer:s})({audio:e,video:t,config:n,writable:i});return{readable:o,done:a}}async composite(e){return await this.compositor.composite(e)}};var M;(function(r){r[r.Sequence=0]="Sequence",r[r.Stack=1]="Stack",r[r.Video=2]="Video",r[r.Audio=3]="Audio",r[r.Text=4]="Text",r[r.Gap=5]="Gap",r[r.Spatial=6]="Spatial",r[r.Transition=7]="Transition",r[r.TextStyle=8]="TextStyle"})(M||(M={}));var Kn;(function(r){r[r.Crossfade=0]="Crossfade"})(Kn||(Kn={}));var Xn=class{datafile;duration=0;hasVideo=!1;hasAudio=!1;constructor(e){this.datafile=e}static async analyze(e){let t=new this(e),n=await this.duration(e.url)*1e3;t.duration=n;let{video:o,audio:i}=await this.#e(e.url);return t.hasAudio=i,t.hasVideo=o,t}static async duration(e){let n=await new ee({formats:ie,source:await ce(e)}).computeDuration();return Number(n.toFixed(5))}static async#e(e){let t=new ee({formats:ie,source:await ce(e)});return{audio:!!await t.getPrimaryAudioTrack(),video:!!await t.getPrimaryVideoTrack()}}};var Qn=class{#e=new Fr;async store(e){let t=await Xn.analyze(e),{hash:n}=t.datafile.checksum,{filename:o,bytes:i,url:s,blob:a}=t.datafile;if(this.#e.has(n)){let c=this.#e.require(n);c.filename=o}else this.#e.set(n,{kind:"media",filename:o,bytes:i,url:s,blob:a,duration:t.duration});return t}require(e){return this.#e.require(e)}};var Yn=class r{videoTrack;options;#e;#t=new Map;#r=[0,0];constructor(e,t){this.videoTrack=e,this.options=t,this.#e=new fr(e,t.canvasSinkOptions)}static async init(e,t){let o=await new ee({formats:ie,source:await ce(e)}).getPrimaryVideoTrack();if(o)return new r(o,{frequency:t.frequency??1,canvasSinkOptions:t.canvasSinkOptions??{width:80,height:50,fit:"fill"},onChange:t.onChange,onPlaceholders:t.onPlaceholders});throw new Error("Source has no video track")}set frequency(e){e!==this.options.frequency&&(this.options.frequency=e,this.#a())}get frequency(){return this.options.frequency}#n([e,t],n=1){let o=t-e;return[e-o*n,t+o*n]}async#o(){let[e,t]=this.#r,n=new Set,o=await this.videoTrack.computeDuration();for(let i=Math.max(0,e);i<=t;i+=this.options.frequency)i>=0&&i<=o&&n.add(i);this.options.onPlaceholders?.([...n])}async#s(){let[e,t]=this.#r,n=new Set,o=await this.videoTrack.computeDuration();for(let c=Math.max(0,e);c<=t;c+=this.options.frequency)c>=0&&c<=o&&n.add(c);let i=[...n].filter(c=>!this.#t.has(c)),s=0;for await(let c of this.#e.canvasesAtTimestamps(i))if(c){let l=i[s++];this.#t.set(l,c)}for(let c of this.#t.keys())n.has(c)||this.#t.delete(c);let a=[...this.#t.entries()].map(([c,l])=>({time:c,canvas:l}));this.options.onChange(a)}set range(e){let[t,n]=e,o=n-t,[i,s]=this.#r,a=i+o,c=s-o,l=t<a,d=n>c;!l&&!d||(this.#r=this.#n(e,2),this.#a())}#i=null;#c=!1;async#a(){if(this.#o(),this.#i){this.#c=!0;return}this.#i=this.#s(),await this.#i,this.#i=null,this.#c&&(this.#c=!1,await this.#a())}getThumbnail(e){return this.#t.get(e)}};function Da(r,e){let t=typeof window<"u"&&window.devicePixelRatio||1,n=document.createElement("canvas");n.width=e.width*t,n.height=e.height*t,n.style.width=`${e.width}px`,n.style.height=`${e.height}px`;let o=n.getContext("2d");if(!o)return n;o.scale(t,t),o.fillStyle=e.color;let i=e.height/2,s=Math.max(1,e.width),a=r.length/s;for(let c=0;c<s;c++){let l=Math.floor(c*a),d=Math.max(l+1,Math.floor((c+1)*a)),u=0;for(let m=l;m<d&&m<r.length;m++)r[m]>u&&(u=r[m]);let f=u*e.height;o.fillRect(c,i-f/2,1,f)}return n}var vi=[2048,1024,512,256,128,64,32];async function Ra(r,e){let t=await r.getAudioDuration(e)??0,n=r.decodeAudio({source:e}).readable,o=vi[vi.length-1],i=[],s=0,a=0,c=0;for await(let u of n){c||=u.sampleRate;let f=u.numberOfFrames,m=new Float32Array(f);u.copyTo(m,{planeIndex:0});for(let p=0;p<m.length;p++){let g=Math.abs(m[p]);g>s&&(s=g),a++,a>=o&&(i.push(s),s=0,a=0)}u.close()}a>0&&i.push(s);let l=new Float32Array(i),d=vi.map(u=>{let f=Math.max(1,Math.round(u/o)),m=f===1?l:Xc(l,f);return{samplesPerPeak:u,peaks:m,peaksPerSecond:c>0?c/u:0}});return{duration:t,levels:d}}function Xc(r,e){let t=new Float32Array(Math.ceil(r.length/e));for(let n=0;n<t.length;n++){let o=0,i=n*e,s=Math.min(i+e,r.length);for(let a=i;a<s;a++)r[a]>o&&(o=r[a]);t[n]=o}return t}var Qc=4096,Zn=class r{#e=new Map;#t=[0,0];#r=[0,0];#n;#o;#s;#i=!1;color;duration;tileSize;tileHeight;preloadMargin;constructor(e,t,n){this.#o=e,this.duration=t,this.tileSize=n.tileSize??1,this.#n=n.zoom??(n.tileWidth??256)/this.tileSize,this.tileHeight=n.tileHeight??96,this.preloadMargin=n.preloadMargin??2,this.color=n.color??"rgb(3, 148, 129)",this.#s=n.onChange}static async init(e,t,n={}){let{duration:o,levels:i}=await Ra(e,t);return new r(i,o,n)}set zoom(e){let t=Math.max(1,e);t!==this.#n&&(this.#n=t,this.#e.clear(),this.#a())}get zoom(){return this.#n}get range(){return this.#r}#c([e,t],n=1){let o=t-e;return[Math.max(0,e-o*n),Math.min(this.duration,t+o*n)]}set range(e){this.#r=e;let[t,n]=e,o=n-t,[i,s]=this.#t,a=i>0,c=s<this.duration,l=!a||t>=i+o,d=!c||n<=s-o;l&&d||(this.#t=this.#c(e,this.preloadMargin),this.#a())}#a(){this.#i||(this.#i=!0,queueMicrotask(()=>{this.#i=!1,this.#l()}))}#l(){let[e,t]=this.#t,n=new Set,o=this.#d(),i=Math.max(0,Math.floor(e/this.tileSize)*this.tileSize),s=Math.min(this.duration,t);for(let a=i;a<=s;a+=this.tileSize)n.add(a);for(let a of n)if(!this.#e.has(a)){let c=Math.min(a+this.tileSize,this.duration);this.#e.set(a,this.#u(a,c,o))}for(let a of this.#e.keys())n.has(a)||this.#e.delete(a);this.#p()}#u(e,t,n){let o=this.#f(n,e,t);return{startTime:e,endTime:t,peaks:o,canvas:Da(o,{width:this.#m(e,t),height:this.tileHeight,color:this.color})}}#d(){return this.#o.find(e=>e.peaksPerSecond>=this.#n)??this.#o[this.#o.length-1]}#f(e,t,n){if(!e.peaksPerSecond)return new Float32Array;let o=Math.max(0,Math.floor(t*e.peaksPerSecond)),i=Math.max(o+1,Math.min(e.peaks.length,Math.ceil(n*e.peaksPerSecond)));return e.peaks.slice(o,i)}#m(e,t){return Math.min(Qc,Math.max(1,Math.ceil((t-e)*this.#n)))}#p(){this.#s&&this.#s([...this.#e.values()].sort((e,t)=>e.startTime-t.startTime))}getTiles(){return this.#e}};var U=r=>r;var Gt=r=>r;var Ma=()=>{let r=!1,e=Gt(60),t=1e3/e,n=0,o=0,i=le(),s=null,a=l=>{if(requestAnimationFrame(a),!!r)for(n=l;l-o>=t;)o+=t,s?.(),s=null,i()};async function*c(){for(n=performance.now(),o=n,requestAnimationFrame(a);;)await new Promise(l=>s=l),yield}return{play(){r||(r=!0,n=performance.now(),o=n)},pause(){r=!1},setFPS(l){e=l,t=1e3/e},isPlaying(){return r},ticks:c,onTick:i}},Ba=async(r,e)=>{let t=U(1e3/r.fps),n=r.duration/1e3,o=Math.ceil(n*r.fps);for(let i=0;i<o;i++){let s=U(i*t);await e(s,i)}};function za(r){return"duration"in r}function Ua(r){let e=[],t=new Map(r.timeline.items.map(n=>[n.id,n]));return Si(r.timeline.rootId,t,r.from,{sequence:()=>{},stack:()=>{},video:(n,o,i)=>e.push({item:n,localTime:o,ancestors:i}),text:(n,o,i)=>e.push({item:n,localTime:o,ancestors:i}),audio:(n,o,i)=>e.push({item:n,localTime:o,ancestors:i})}),e}function Na(r,e,t){let n=Sa;for(let o of e)n=Oa(r,o,n);return Oa(r,t,n)}function Oa(r,e,t){if("spatialId"in e&&e.spatialId){let n=r.get(e.spatialId);if(n?.enabled){let o=wa(n.transform);return va(o,t)}}return t}function Si(r,e,t,n,o=[]){let i=e.get(r);if(i)switch(i.kind){case M.Stack:n.stack(i,t,o);for(let s of i.childrenIds)Si(s,e,t,n,[...o,i]);break;case M.Sequence:{n.sequence(i,t,o);let s=U(0);for(let a of i.childrenIds){let c=e.get(a);if(!c||!za(c))continue;let l=U(s+c.duration);if(t>=l){s=l;continue}let d=U(Math.max(0,t-s));Si(a,e,d,n,[...o,i]),s=l}break}case M.Video:n.video(i,t,o);break;case M.Text:n.text(i,t,o);break;case M.Audio:n.audio(i,t,o);break}}function he(r,e){let t=e.items.find(n=>n.id===r);if(!t)return U(0);switch(t.kind){case M.Sequence:{let n=t.childrenIds.map(i=>e.items.find(s=>s.id===i)).filter(Boolean),o=U(0);for(let i=0;i<n.length;i++){let s=n[i];if(s.kind===M.Transition){let a=n[i-1],c=n[i+1];if(a&&c&&a.kind!==M.Transition&&c.kind!==M.Transition){let l=he(a.id,e),d=he(c.id,e),u=Math.max(0,Math.min(s.duration,l,d));o=U(o-u)}continue}o=U(o+he(s.id,e))}return o}case M.Stack:{let n=U(0);for(let o of t.childrenIds){let i=he(o,e);i>n&&(n=i)}return n}default:return za(t)?t.duration:U(0)}}var Pr=r=>r;var Jn=class{resolveMedia;#e=new Map;constructor(e){this.resolveMedia=e}async getSink(e){let t=this.#e.get(e);if(t)return t.sink;let n=new ee({formats:ie,source:await ce(this.resolveMedia(e))}),o=await n.getPrimaryVideoTrack(),s=!!o&&await o.canDecode()&&o?new Ct(o):null;return this.#e.set(e,{input:n,sink:s}),s}};async function Va(r,e,t,n){let[o,i]=await Promise.all([t,n]),s=o.find(l=>l.kind==="image")?.frame,a=i.find(l=>l.kind==="image")?.frame,c=[...o.filter(l=>l.kind!=="image"),...i.filter(l=>l.kind!=="image")];return s&&a?[{id:r.id,kind:"transition",name:"circle",progress:e,from:s,to:a},...c]:c}async function La(r,e,t,n){let o=Yc(r,e,t);if(!o)return[];let i=[...n,e];return o.isTransitioning?Va(o.transition,o.progress,mt(r,o.outgoing,o.outgoingTime,i),mt(r,o.incoming,o.incomingTime,i)):mt(r,o.item,o.localTime,i)}function Yc(r,e,t){let n=e.childrenIds.map(i=>r.items.get(i)).filter(i=>!!i),o=U(0);for(let i=0;i<n.length;i++){let s=n[i];if(s.kind===M.Transition)continue;let a=o,c=he(s.id,r.timeline),l=U(a+c),d=n[i+1];if(!(d?.kind===M.Transition)){if(t<l)return{isTransitioning:!1,item:s,localTime:U(t-a)};o=l;continue}let f=d,m=n[i+2];if(!m||m.kind===M.Transition){o=l;continue}let p=he(m.id,r.timeline),g=Math.max(0,Math.min(f.duration,c,p)),x=U(l-g);if(t<x)return{isTransitioning:!1,item:s,localTime:U(t-a)};if(t<l){let y=U(t-x),b=U(t-a);return{isTransitioning:!0,incoming:m,outgoing:s,outgoingTime:b,incomingTime:y,progress:g>0?y/g:1,transition:f}}o=x,i++}return null}async function mt(r,e,t,n){let o=Na(r.items,n,e),i="spatialId"in e&&e.spatialId?r.items.get(e.spatialId)?.crop:void 0;switch(e.kind){case M.Stack:{let s=[...n,e];return(await Promise.all(e.childrenIds.map(c=>r.items.get(c)).filter(c=>!!c).map(c=>mt(r,c,t,s)))).flat()}case M.Sequence:return La(r,e,t,n);case M.Video:{if(t<0||t>=e.duration)return[];let s=await r.videoSampler(e,t);return s?[{kind:"image",frame:s,matrix:o,crop:i,id:e.id}]:[]}case M.Text:{if(t<0||t>=e.duration)return[];let s=e.styleId?r.items.get(e.styleId)?.style:void 0;return[{id:e.id,kind:"text",content:e.content,style:s,matrix:o,crop:i}]}case M.Gap:return[{id:e.id,kind:"gap"}];default:return[]}}function Ha(r){return async(e,t)=>{let o=await(await r.getSink(e.mediaHash))?.getSample(t/1e3),i=o?.toVideoFrame();return o?.close(),i??void 0}}function eo(r,e){let t=new Jn(r),n=e??Ha(t);return{async sample(o,i){let s=new Map(o.items.map(c=>[c.id,c])),a=s.get(o.rootId);return a?mt({videoSampler:n,timeline:o,items:s},a,i,[]):[]}}}var Wt=class{driver;resolveMedia;timeline;#e=-1/0;#t=new Map;#r;constructor(e,t,n){this.driver=e,this.resolveMedia=t,this.timeline=n,this.#r=eo(this.resolveMedia,(o,i)=>{let s=qa(i),a=this.#t.get(o.id);if(!a){let c=this.resolveMedia(o.mediaHash),l=qa(U(o.start+o.duration));a=this.#n(c,s,l),this.#t.set(o.id,a)}return a.next(s)})}next(e){if(e<this.#e)throw new Error(`Forward-only cursor regression: ${e}ms < ${this.#e}ms`);return this.#e=e,this.#r.sample(this.timeline,e)}async cancel(){await Promise.all([...this.#t.values()].map(e=>e.cancel())),this.#t.clear()}#n(e,t,n){let o=this.driver.decodeVideo({source:e,start:t/1e6,end:n/1e6}),i=o.readable.getReader(),s=null,a=null,c=!1,l=async()=>{if(c)return null;let{done:d,value:u}=await i.read();if(d)return c=!0,null;let f=new VideoFrame(u);return u.close(),f};return{async next(d){if(s??=await l(),!!s)for(;;){a??=l();let u=await a;if(!u)return new VideoFrame(s);let f=s.timestamp??-1/0,m=u.timestamp??f;if(m<d){s.close(),s=u,a=null;continue}if(Math.abs(m-d)<Math.abs(f-d)){s.close(),s=u,a=null;continue}return new VideoFrame(s)}},async cancel(){let d=a;a=null,c=!0,(await d?.catch(()=>null))?.close(),s?.close(),s=null,o.cancel()}}}},qa=r=>Math.round(r*1e3);async function Ga(r,e,t){return(await Promise.all(e.map(async({item:o,localTime:i})=>{if(o.kind!==M.Audio)return;let s=await r.getSink(o.mediaHash);if(!s)return;let a=o.start+i,c=Pr((t-a)/1e3),l=s.samples(a/1e3),d=await l.next();if(d.done)return;let u=d.value,f=l.next();return{offset:c,gain:o.gain??1,get currentSample(){return u},timelineTime:()=>Pr(c+u.timestamp),output:()=>({sample:u,timestamp:c+u.timestamp,gain:o.gain??1}),advance:async()=>{let m=await f;return m.done?!1:(u=m.value,f=l.next(),!0)}}}))).filter(o=>!!o)}var to=class{resolveMedia;#e=new Map;constructor(e){this.resolveMedia=e}async getSink(e){let t=this.#e.get(e);if(t)return t.sink;let n=new ee({formats:ie,source:await ce(this.resolveMedia(e))}),o=await n.getPrimaryAudioTrack(),s=!!o&&await o.canDecode()&&o?new mr(o):null;return this.#e.set(e,{input:n,sink:s}),s}};function Wa(r){let e={index:0,stream:r[0],time:r[0].timelineTime()};for(let[t,n]of r.entries()){let o=n.timelineTime();o<e.time&&(e={time:o,stream:n,index:t})}return e}function ro(r){let e=new to(r);return{async*sampleAudio(t,n){let o=Ua({timeline:t,from:n}),i=await Ga(e,o,n);for(;i.length>0;){let{stream:s,index:a}=Wa(i);yield s.output(),await s.advance()||i.splice(a,1)}}}}var no=class{driver;timeline;resolveMedia;audioSampler;seekVisualSampler;playVisualSampler=null;#e=U(0);#t=null;#r=Ma();onTick=this.#r.onTick;audioContext=new AudioContext({sampleRate:48e3});audioGain=this.audioContext.createGain();audioNodes=new Set;#n=null;constructor(e,t,n){this.driver=e,this.timeline=t,this.resolveMedia=n,this.audioGain.connect(this.audioContext.destination),this.audioGain.gain.value=.7**2,this.seekVisualSampler=eo(this.resolveMedia),this.audioSampler=ro(this.resolveMedia),this.#o()}update(e){this.timeline=e}get isPlaying(){return this.#r.isPlaying()}async#o(){for await(let e of this.#r.ticks()){let t=await this.playVisualSampler?.next(this.currentTime)??[];(await this.driver.composite(t)).close(),this.currentTime>=this.duration&&this.pause()}}async seek(e){return this.pause(),this.#e=e,await this.seekVisualSampler.sample(this.timeline,e)}async start(){if(!this.#r.isPlaying()){await this.audioContext.resume(),this.#e=this.currentTime,this.#t=this.audioContext.currentTime,this.#n?.abort(),this.#n=new AbortController;for(let e of this.audioNodes)e.stop();this.audioNodes.clear(),this.playVisualSampler=new Wt(this.driver,this.resolveMedia,this.timeline),this.#r.play(),this.#s(this.#n.signal,Pr(this.#e/1e3))}}pause(){this.#e=this.currentTime,this.#r.pause(),this.#n?.abort();for(let e of this.audioNodes)e.stop();this.audioNodes.clear(),this.playVisualSampler&&(this.playVisualSampler.cancel(),this.playVisualSampler=null)}get duration(){return he(this.timeline.rootId,this.timeline)}get currentTime(){if(!this.#r.isPlaying()||this.#t===null)return this.#e;let e=(this.audioContext.currentTime-this.#t)*1e3;return U(this.#e+e)}setFps(e){this.#r.setFPS(e)}async#s(e,t){let n=this.audioContext;if(this.#t!==null)for await(let{sample:o,timestamp:i}of this.audioSampler.sampleAudio(this.timeline,U(t*1e3))){if(e.aborted||!this.#r.isPlaying())return;for(;i-(n.currentTime-this.#t+t)>.75;)await new Promise(c=>setTimeout(c,25));let s=n.createBufferSource();s.buffer=o.toAudioBuffer(),s.connect(this.audioGain),s.onended=()=>this.audioNodes.delete(s),this.audioNodes.add(s);let a=this.#t+i-t;a>=n.currentTime?s.start(a):s.start(n.currentTime,n.currentTime-a)}}};var oo=class{driver;canvas;playback;#e=null;#t=null;constructor(e,t,n){this.driver=e,this.playback=new no(e,n,t),this.canvas=e.compositor.pixi.renderer.canvas}async play(){await this.playback.start()}pause(){this.playback.pause()}seek(e){return this.#e=e,this.#t??=this.#r().finally(()=>this.#t=null)}setFPS(e){this.playback.setFps(Gt(e))}get isSeeking(){return this.#t!==null}get isPlaying(){return this.playback.isPlaying}get duration(){return this.playback.duration}get currentTime(){return this.playback.currentTime}update(e){this.playback.update(e)}async#r(){for(;this.#e!==null;){let e=this.#e;this.#e=null;let t=await this.playback.seek(U(e));(await this.driver.composite(t)).close()}}};var pt=class{state;constructor(e){this.state=e}require(e){return e===void 0?void 0:this.state.timeline.items.find(n=>n.id===e)}get timeline(){return this.state.timeline}getId(){return ge.toInteger(ge.random())}#e(e){this.state.timeline=e(this.state.timeline)}register(e){this.#e(t=>({...t,items:[...t.items,e]}))}textStyle=e=>{let t={id:this.getId(),kind:M.TextStyle,style:e};return this.register(t),t};spatial=(e,t)=>{let n={id:this.getId(),kind:M.Spatial,transform:e??this.transform(),crop:t,enabled:!0};return this.register(n),n};sequence=(...e)=>{let t={id:this.getId(),kind:M.Sequence,childrenIds:e.map(n=>n.id)};return this.register(t),t};stack=(...e)=>{let t={kind:M.Stack,id:this.getId(),childrenIds:e.map(n=>n.id)};return this.register(t),t};video=(e,t)=>{if(!e.hasVideo)throw new Error(`Video clip error: media "${e.datafile.filename}" has no video track.`);let n={kind:M.Video,id:this.getId(),mediaHash:e.datafile.checksum.hash,start:t?.start??0,duration:t?.duration??e.duration};return this.register(n),n};audio=(e,t)=>{if(!e.hasAudio)throw new Error(`Audio clip error: media "${e.datafile.filename}" has no audio track.`);let n={kind:M.Audio,id:this.getId(),mediaHash:e.datafile.checksum.hash,start:t?.start??0,duration:t?.duration??e.duration,gain:t?.gain??1};return this.register(n),n};text=(e,t)=>{let n={id:this.getId(),content:e,kind:M.Text,duration:t?.duration??2e3};return t?.styles&&(n.styleId=this.textStyle(t.styles).id),this.register(n),n};gap=e=>{let t={id:this.getId(),kind:M.Gap,duration:e};return this.register(t),t};transition={crossfade:e=>{let t={id:this.getId(),kind:M.Transition,effect:Kn.Crossfade,duration:e};return this.register(t),t}};transform=e=>{let t=[e?.position?.[0]??0,e?.position?.[1]??0],n=[e?.scale?.[0]??1,e?.scale?.[1]??1],o=e?.rotation??0;return[t,n,o]};addChildren(e,...t){this.#e(n=>(n.items.find(({id:i})=>i===e.id).childrenIds.push(...t.map(i=>i.id)),n))}set=(e,t)=>{this.#e(n=>({...n,items:n.items.map(o=>o.id===e?{...o,...t}:o)}))}};var io=class{#e;#t;constructor(e={}){this.#e=e.chunkFrames??1024,this.#t=e.clamp??!0}async*mix(e){let t=this.#e,n=null,o=null,i=[],s=0,a=0;for await(let c of e){if(o===null)o=c.planes.length,n=c.sampleRate;else{if(c.planes.length!==o)throw new Error("Channel count changed");if(c.sampleRate!==n)throw new Error("Sample rate changed")}let l=Math.round(c.timestamp*n),d=c.planes[0]?.length??0;for(;s+t<=l;)yield this.#r(i,s,o,n),s+=t;i.push({startFrame:l,endFrame:l+d,data:c.planes}),a=Math.max(a,l+d)}if(o!==null&&n!==null)for(;s<a;)yield this.#r(i,s,o,n),s+=t}#r(e,t,n,o){let i=this.#e,s=new Float32Array(n*i),a=t+i;for(let c=0;c<n;c++){let l=c*i,d=s.subarray(l,l+i);for(let u of e){let f=u.data[c];if(!f)continue;let m=Math.max(t,u.startFrame),p=Math.min(a,u.endFrame);if(m>=p)continue;let g=m-t,x=m-u.startFrame,y=p-m;for(let b=0;b<y;b++)d[g+b]+=f[x+b]}if(this.#t)for(let u=0;u<i;u++){let f=d[u];d[u]=f<-1?-1:f>1?1:f}}for(let c=e.length-1;c>=0;c--)e[c].endFrame<=a&&e.splice(c,1);return{planar:s,sampleRate:o,channels:n,frames:i,startFrame:t}}};var Zc=(r,e,t)=>{if(e===t)return r;let n=t/e,o=Math.max(1,Math.round(r.length*n)),i=new Float32Array(o);for(let s=0;s<o;s++){let a=s/n,c=Math.floor(a),l=Math.min(c+1,r.length-1),d=a-c;i[s]=r[c]*(1-d)+r[l]*d}return i},ja=(r,e)=>{let t=r.numberOfChannels,n=new Array(t),o=0;for(let i=0;i<t;i++){let s=new Float32Array(r.numberOfFrames);r.copyTo(s,{planeIndex:i,format:"f32-planar"});let a=Zc(s,r.sampleRate,e);n[i]=a,o=a.length}return{data:n,frames:o}};var $a=(r,e)=>{if(e!==1)for(let t of r)for(let n=0;n<t.length;n++)t[n]*=e};function Ka({timeline:r,resolveMedia:e}){let t=new io,n=Jc(r,e),o=new TransformStream,i=o.writable.getWriter();async function s(){for await(let a of t.mix(n)){let c=new AudioData({format:"f32-planar",sampleRate:a.sampleRate,numberOfFrames:a.frames,numberOfChannels:a.channels,timestamp:Math.round(a.startFrame/a.sampleRate*1e6),data:new Float32Array(a.planar)});await i.write(c)}await i.close()}return s(),o.readable}async function*Jc(r,e){let t=ro(e);for await(let{sample:n,timestamp:o,gain:i}of t.sampleAudio(r,U(0))){let{data:s}=ja(n,48e3);$a(s,i),yield{planes:s,sampleRate:48e3,timestamp:o},n.close()}}function Xa({timeline:r,fps:e,driver:t,resolveMedia:n}){let o=new TransformStream,i=o.writable.getWriter(),s=new Wt(t,n,r),a=1/e,c=he(r.rootId,r);async function l(){await Ba({fps:e,duration:c},async(d,u)=>{let f=await s.next(d),m=await t.composite(f),p=new VideoFrame(m,{timestamp:Math.round(u*a*1e6),duration:Math.round(a*1e6)});await i.write(p),m.close()}),await i.close()}return l(),o.readable}function Qa(r){let e=Ka({...r}),t=Xa({...r});return r.driver.encode({video:t,audio:e,config:{audio:{codec:"opus",bitrate:128e3},video:{codec:"vp9",bitrate:1e6}}})}var so=class{driver;resources=new Qn;constructor(e){this.driver=e}load=async e=>Object.fromEntries(await Promise.all(Object.entries(e).map(async([t,n])=>[t,await this.resources.store(await n)])));timeline=e=>{let t=new pt({timeline:{format:"timeline",info:"https://omniclip.app/",version:0,items:[],rootId:0}}),n=e(t);return t.timeline.rootId=n.id,t.timeline};playback=async e=>new oo(this.driver,t=>this.resources.require(t).url,e);render=async(e,t=30)=>Qa({timeline:e,fps:Gt(t),driver:this.driver,resolveMedia:n=>this.resources.require(n).url})};var ao=class{data;bytes;hash;nickname;constructor(e,t,n,o){this.data=e,this.bytes=t,this.hash=n,this.nickname=o}static async make(e){let t=new Uint8Array(e),n=new Uint8Array(await crypto.subtle.digest("SHA-256",t)),o=_r.fromBytes(n),i=Mi.sigil.fromBytes(n);return new this(e,n,o,i)}};var co=class{url;bytes;blob;filename;checksum;constructor(e,t,n,o,i){this.url=e,this.bytes=t,this.blob=n,this.filename=o,this.checksum=i}static async make(e,t){let n=await e.arrayBuffer(),o=new Uint8Array(n),i=await ao.make(o),s=t??i.nickname,a=URL.createObjectURL(e);return new this(a,o,e,s,i)}static async load(e){}};async function Za(r,e,t){let n=t.querySelector(".play"),o=t.querySelector(".stop"),i=t.querySelector(".scrub"),s=t.querySelector(".playhead"),a=t.querySelector(".timecode"),c=t.querySelector(".player-canvas"),l=new pt({timeline:r}),d=await e.playback(r);c.replaceChildren(d.canvas),n.disabled=!1,o.disabled=!1,n.addEventListener("click",()=>d.play()),o.addEventListener("click",()=>d.pause()),i.max=String(Math.ceil(d.duration));let u=!1;d.playback.onTick.on(()=>m(d.currentTime,d.duration));let f=(p,g)=>{a.textContent=`${Ya(p)} / ${Ya(g)}`};i.addEventListener("input",async()=>{u=!0;let p=Math.max(0,Math.min(+i.value,d.duration));f(p,d.duration),await d.seek(p)}),i.addEventListener("change",async()=>{u=!1;let p=Math.max(0,Math.min(+i.value,d.duration));await d.seek(p)});let m=(p,g)=>{let x=Math.max(0,Math.min(p,g));u||(i.value=String(Math.round(x)));let y=g?x/g*100:0;s.style.left=`${y}%`,f(x,g)};d.update(l.timeline)}function Ya(r){let e=Math.max(0,r),t=Math.floor(e/1e3),n=Math.floor(t/60),o=t%60,i=Math.floor(e%1e3);return`${n}:${String(o).padStart(2,"0")}.${String(i).padStart(3,"0")}`}async function ec(r,e,t){let n=t.querySelector(".waveform-canvas"),o=t.querySelector(".width");n.replaceChildren(),n.style.position="relative",n.style.height="96px",n.style.overflow="hidden";let i=await Zn.init(r,e,{tileHeight:96,onChange:()=>s()}),s=()=>{let a=+o.value,c=Ja(a,i.duration);i.zoom=c,n.style.width=`${a}px`,n.replaceChildren(...[...i.getTiles().values()].map(l=>(l.canvas.style.position="absolute",l.canvas.style.top="0",l.canvas.style.left=`${l.startTime*Ja(a,i.duration)}px`,l.canvas.style.height="100%",l.canvas)))};o.oninput=s,i.range=[0,i.duration]}function Ja(r,e){return e>0?r/e:0}async function Pi(r,e){let t=new so(r),{videoA:n}=await t.load({videoA:co.make(e)});return{timeline:t.timeline(i=>{let s=i.text("content",{duration:1e3}),a=i.textStyle({fill:"green",fontSize:100}),c=i.spatial(i.transform({position:[240,160],scale:[1.4,1.4],rotation:.08}),[.15,.1,.05,.2]),l=i.video(n,{duration:3e3,start:1e3});return i.set(s.id,{styleId:a.id}),i.set(l.id,{spatialId:c.id}),i.sequence(i.stack(s,l,i.audio(n,{duration:1e3})),i.gap(500),i.video(n,{duration:7e3,start:5e3}))}),omni:t}}async function tc(r,e){let t=e.querySelector(".range"),n=e.querySelector(".range-view"),o=e.querySelector(".range-size"),i=e.querySelector(".frequency"),s=e.querySelector(".frequency-view"),a=e.querySelector(".filmstrip"),c=1/10,l=.5;a.replaceChildren();let d=await Yn.init(r,{onChange(u){let f=u.sort((m,p)=>m.time-p.time);a.replaceChildren(...f.map(({time:m,canvas:p})=>el(m,p.canvas)))},frequency:c,canvasSinkOptions:{width:80,height:50,fit:"fill"}});o.oninput=()=>{l=+o.value;let u=+t.value,f=u+l;d.range=[u,f],n.textContent=`visible time range: [${u}, ${f}]`},t.oninput=()=>{let u=+t.value,f=u+l;d.range=[u,f],n.textContent=`visible time range: [${u}, ${f}]`},i.oninput=()=>{d.frequency=1/+i.value,s.textContent=`frame every ${d.frequency.toFixed(3)} second (${i.value} frames per second)`},d.range=[10,10.5]}function el(r,e){let t=document.createElement("div");t.style.position="relative",t.style.display="inline-block",t.style.marginRight="4px",t.appendChild(e);let n=document.createElement("div");return n.textContent=`${r.toFixed(2)}s`,n.style.position="absolute",n.style.top="2px",n.style.right="4px",n.style.fontSize="10px",n.style.color="white",n.style.background="rgba(0,0,0,0.6)",n.style.padding="2px 4px",n.style.borderRadius="4px",n.style.pointerEvents="none",t.appendChild(n),t}function rc(r,e){let t={width:1920,height:1080},n=document.createElement("canvas");n.width=t.width,n.height=t.height;let o=n.getContext("2d");async function i(){let s=r.decodeVideo({source:e,async onFrame(f){let m=await r.composite([{id:0,kind:"image",frame:f},{id:1,kind:"text",content:"omnitool",style:{fontSize:50,fill:"green"}}]);return f.close(),o?.drawImage(m,0,0),m}}),a=r.decodeAudio({source:e}),{readable:c,done:l}=r.encode({video:s.readable,audio:a.readable,config:{audio:{codec:"opus",bitrate:128e3},video:{codec:"vp9",bitrate:1e6}}}),u=await(await window.showSaveFilePicker()).createWritable();await Promise.all([c.pipeTo(u),l])}return{canvas:n,run:i}}var jt=await $n.setup({workerUrl:new URL("../driver/driver.worker.bundle.min.js",import.meta.url)}),tl=document.querySelector("[data-demo='transcode']"),rl=document.querySelector("[data-demo='filmstrip']"),nl=document.querySelector("[data-demo='waveform']"),nc=document.querySelector("[data-demo='playback']"),ht=document.querySelector("[data-demo='export']"),oc=ht.querySelector("[data-action='export']"),lo=null;await jt.thread.work.hello(),jt.machina.count===1?console.log("\u2705 driver works"):console.error("\u274C FAIL driver call didn't work");var $t=(r,e)=>{let t=r.querySelector(".progress"),n=r.querySelector(".status");!t||!n||(e==="running"?(t.removeAttribute("value"),n.textContent="running"):e==="done"?(t.value=1,n.textContent="done"):(t.value=0,n.textContent="idle"))},Ii=(r,e)=>{let t=r.querySelector("input[type='file']"),n=r.querySelector("[data-action='run']");n.disabled=!0,t.addEventListener("input",()=>{n.disabled=!t.files?.length}),n.addEventListener("click",async()=>{let o=t.files?.[0];if(o){n.disabled=!0,$t(r,"running");try{await e(o,r),$t(r,"done")}finally{n.disabled=!1}}})};Ii(tl,async(r,e)=>{let t=e.querySelector(".demo-preview"),n=rc(jt,r);t.replaceChildren(n.canvas),await n.run()});Ii(rl,async(r,e)=>{await tc(r,e)});Ii(nl,async(r,e)=>{await ec(jt,r,e)});{let r=nc.querySelector("input[type='file']");r.addEventListener("input",async()=>{let e=r.files?.[0];if(!e)return;let{timeline:t,omni:n}=await Pi(jt,e);await Za(t,n,nc)})}{let r=ht.querySelector("input[type='file']");r.addEventListener("input",async()=>{let e=r.files?.[0];if(!e)return;$t(ht,"running");let{timeline:t,omni:n}=await Pi(jt,e);lo={timeline:t,omni:n},oc.disabled=!1;let o=ht.querySelector(".demo-preview"),i=await n.playback(t);await i.seek(0),o.replaceChildren(i.canvas),$t(ht,"done")})}oc.addEventListener("click",async()=>{lo&&($t(ht,"running"),await lo.omni.render(lo.timeline),$t(ht,"done"))});
|
|
2416
2416
|
/*! Bundled license information:
|
|
2417
2417
|
|
|
2418
2418
|
mediabunny/dist/modules/src/misc.js:
|