Spaces:
Running

File size: 1,448 Bytes
1b72d36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// The clip encoder's home: the `encoder/` bundle (the codec's encoder and its w2v-BERT teacher) in
// a module Worker with its own runtime instance and memory. The page starts it when a clip is first
// cloned by its codes and ends it once the codes are out.
//
// Encode: one `step()` per macrotask (a lane's front, each of its stages, its teacher, its codes),
// each reported to the page, then the clip's codes, a frame of 10 after another.

import { engine, load, post, serve, yieldTask } from './dist/host/worker.js';

serve({
  load: ({ modelUrl, threads, build }) =>
    load({
      modelUrl,
      threads,
      build,
      glueName: 'zonos3_mini_runtime',
      className: 'EncoderRuntime',
      cacheName: 'zonos3-mini-encoder',
      distUrl: new URL('./dist/', import.meta.url),
      onRuntime: (started) => post({ type: 'runtime', build: started.build.name, threads: started.threads }),
      onReady: (progress) => post({ type: 'ready', ...progress }),
    }),
  encode,
});

async function encode({ job, clip, clipRate }) {
  const { rt } = engine;
  const t0 = performance.now();
  rt.start(clip, clipRate);
  for (;;) {
    const progress = JSON.parse(rt.step());
    post({ type: 'encoding', job, steps: progress.steps, total: progress.total });
    if (progress.done) break;
    await yieldTask();
  }
  const codes = rt.codes();
  post({ type: 'encoded', job, codes, encodeMs: performance.now() - t0 }, [codes.buffer]);
}