Spaces:
Running on Zero
Running on Zero
Download assets/playback.js from fffiloni/fastframes-voice: direct link, hf CLI and curl.
- Browser
- Download file 5.11 kB
-
https://huggingface.co/spaces/fffiloni/fastframes-voice/resolve/main/assets/playback.js
- Command line
-
hf download hf://spaces/fffiloni/fastframes-voice/assets/playback.js
-
curl -L -o playback.js https://huggingface.co/spaces/fffiloni/fastframes-voice/resolve/main/assets/playback.js
5.11 kB
| /* Present the generated frames in the monitor. The full take uses native MP4 replay. */ | |
| function inlinePlayback(monitor, canvas, onStart, onEnd, onError) { | |
| let active=null, looping=null, cached=null, allowLoop=false; | |
| function inlineReplay() { | |
| const video = monitor.querySelector("#last-take video"); | |
| if (!video) return; | |
| if (!video.playsInline) video.playsInline = true; | |
| if (!video.hasAttribute("webkit-playsinline")) video.setAttribute("webkit-playsinline",""); | |
| } | |
| const observer = new MutationObserver(inlineReplay); | |
| observer.observe(monitor,{childList:true,subtree:true,attributes:true,attributeFilter:["playsinline","webkit-playsinline"]}); | |
| monitor.addEventListener("loadstart",inlineReplay,true); | |
| inlineReplay(); | |
| function stopActive() { | |
| if (active) { active.controller.abort(); active.wake?.(); } | |
| active = null; | |
| } | |
| function stopLoop(){ | |
| if(looping)looping.controller.abort(); | |
| looping=null; | |
| } | |
| function clear(){stopActive();stopLoop();cached=null;} | |
| function startLoop(){ | |
| if(looping || !allowLoop || !cached || active?.started)return; | |
| const state={...cached,repeat:true,controller:new AbortController()}; | |
| looping=state; | |
| play(state.cue,state); | |
| } | |
| function setLoop(value){allowLoop=value;if(value)startLoop();else stopLoop();} | |
| function repeatClip(cue){ | |
| stopLoop();cached={cue,path:cue.preview,ready:Math.ceil(cue.frames/8),blobs:[]}; | |
| startLoop(); | |
| } | |
| async function loadStrip(blob) { | |
| if (typeof createImageBitmap === "function") { | |
| const bitmap = await createImageBitmap(blob); | |
| return {source:bitmap,close:()=>bitmap.close()}; | |
| } | |
| const url=URL.createObjectURL(blob), img=new Image(); | |
| try { | |
| img.src=url;await img.decode(); | |
| return {source:img,close:()=>URL.revokeObjectURL(url)}; | |
| } catch(error) {URL.revokeObjectURL(url);throw error;} | |
| } | |
| function display(strip, firstFrame, count, signal, clock, state) { | |
| const ctx = canvas.getContext("2d",{alpha:false}); | |
| return new Promise(resolve=>{ | |
| let tick = 0, shown = -1; | |
| function finish(){cancelAnimationFrame(tick);signal.removeEventListener("abort",finish);resolve();} | |
| function frame(now){ | |
| if(signal.aborted){finish();return;} | |
| if(clock.start===undefined)clock.start=now; | |
| if(shown===-1 && now-clock.start-firstFrame*62.5>=62.5)clock.start=now-firstFrame*62.5; | |
| const position=Math.max(0,(now-clock.start)*16/1000-firstFrame); | |
| const index=Math.min(count-1,Math.floor(position)); | |
| if(index!==shown){ | |
| if(!state.repeat && !state.started){state.started=true;stopLoop();} | |
| ctx.drawImage(strip,0,index*480,832,480,0,0,832,480);shown=index; | |
| if(!state.repeat && firstFrame===0 && index===0)onStart(); | |
| } | |
| if(position>=count){finish();return;} | |
| tick=requestAnimationFrame(frame); | |
| } | |
| signal.addEventListener("abort",finish,{once:true}); | |
| if(signal.aborted)finish();else tick=requestAnimationFrame(frame); | |
| }); | |
| } | |
| function extend(cue, ready) { | |
| if(active?.path===cue.preview) { | |
| active.ready=Math.max(active.ready,ready); | |
| active.wake?.(); | |
| return active.playing; | |
| } | |
| stopActive(); | |
| const state={path:cue.preview,ready,controller:new AbortController(),wake:null,blobs:[]}; | |
| active=state; | |
| state.playing=play(cue,state); | |
| return state.playing; | |
| } | |
| async function play(cue,state) { | |
| const signal=state.controller.signal; | |
| async function strip(index){ | |
| while(index>=state.ready && !signal.aborted){ | |
| await new Promise(resolve=>{state.wake=resolve;}); | |
| state.wake=null; | |
| } | |
| if(signal.aborted)return null; | |
| if(state.blobs[index])return loadStrip(state.blobs[index]); | |
| const response=await media.strip(state.path+"/"+index+".jpg",signal); | |
| if(!response.ok)throw new Error("Could not load segment frames"); | |
| state.blobs[index]=await response.blob(); | |
| return loadStrip(state.blobs[index]); | |
| } | |
| let next=strip(0); | |
| try{ | |
| const count=Math.ceil(cue.frames/8); | |
| let clock={}; | |
| for(let step=0;(state.repeat||step<count) && !signal.aborted;step++){ | |
| const i=step%count; | |
| if(!i)clock={}; | |
| const item=await next;next=null; | |
| if(!item)break; | |
| try{ | |
| if(state.repeat || i+1<count){next=strip((i+1)%count);next.catch(()=>{});} | |
| await display(item.source,i*8,Math.min(8,cue.frames-8*i),signal,clock,state); | |
| }finally{item.close();} | |
| } | |
| if(!signal.aborted && !state.repeat){ | |
| active=null;cached={cue,path:state.path,ready:count,blobs:state.blobs}; | |
| startLoop();onEnd(); | |
| } | |
| }catch(error){ | |
| if(!signal.aborted){if(state.repeat){stopLoop();}else{stopActive();onError();}} | |
| }finally{ | |
| if(next){try{(await next)?.close();}catch{}} | |
| } | |
| } | |
| function segment(cue){stopActive();return extend(cue,Math.ceil(cue.frames/8));} | |
| return {segment,extend,clear,setLoop,repeatClip,isLooping:()=>Boolean(looping),dispose(){clear();observer.disconnect();monitor.removeEventListener("loadstart",inlineReplay,true);}}; | |
| } | |