stork-live 0.1.1 → 0.1.2

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/README.md CHANGED
@@ -97,6 +97,7 @@ The renderer is a web page, so on iOS and Android it runs inside the system web
97
97
  | Lip movement against FlashHead on 18 unseen clips | 0.71 for the in-page model; FlashHead agrees with itself at about 0.90 |
98
98
  | Draw rate, iPhone 15 Pro Max, Safari | 60 frames a second |
99
99
  | Draw rate, Pixel 6 Pro, Chrome | 60 frames a second |
100
+ | Firefox 161 on a Mac (from 0.1.2) | Draws at the screen's rate; every lip frame on time |
100
101
  | Picture against the reference GPU renderer | 45 dB (compact file), 52 dB (full file) |
101
102
 
102
103
  ## How it is built
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "stork-live",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "A talking avatar drawn in the browser. Lips follow any audio. No server, no API key.",
5
5
  "type": "module",
6
6
  "license": "Apache-2.0",
package/src/gpu.js CHANGED
@@ -182,7 +182,11 @@ export class GpuAvatar {
182
182
  if (opt.worker) {
183
183
  const wu = new URL('./orderWorker.js', import.meta.url)
184
184
  // a page may not start a worker from another site (a CDN, say), so in that case start one that imports it
185
- try { this.worker = new Worker(wu, { type: 'module' }) } catch (e) { this.worker = new Worker(URL.createObjectURL(new Blob([`import ${JSON.stringify(wu.href)}`], { type: 'text/javascript' })), { type: 'module' }) }
185
+ // Chrome refuses at once and Firefox only reports it later, after the arrays below have been handed over, so decide up front.
186
+ const viaImport = () => new Worker(URL.createObjectURL(new Blob([`import ${JSON.stringify(wu.href)}`], { type: 'text/javascript' })), { type: 'module' })
187
+ if (wu.origin !== self.location.origin) this.worker = viaImport()
188
+ else try { this.worker = new Worker(wu, { type: 'module' }) } catch (e) { this.worker = viaImport() }
189
+ this.worker.onerror = (e) => { const err = new Error('Stork Live could not start its worker' + (e && e.message ? ': ' + e.message : '')); if (this.onWorkerFailed) this.onWorkerFailed(err); else this.workerFailed = err }
186
190
  this.worker.postMessage({ init: ord }, [ord.tid.buffer, ord.bary.buffer, ord.f_off.buffer, ord.r_pos.buffer, ord.r_w.buffer, ord.act.buffer, ord.Bz.buffer])
187
191
  this.worker.onmessage = (e) => {
188
192
  const m = e.data; if (m.ready) { this.workerReady = true; if (this.onWorkerReady) this.onWorkerReady(); return }
@@ -282,7 +286,7 @@ export class GpuAvatar {
282
286
  // resolves once the order for the most recently requested frame has been applied
283
287
  settle() { return new Promise((res) => { if (!this.worker || !this.busy) return res(); (this.waiters = this.waiters || []).push(res) }) }
284
288
  // resolves when the background worker is up (the first frame drawn after this requests the first order)
285
- workerUp() { return new Promise((res) => { if (!this.worker || this.workerReady) return res(); this.onWorkerReady = res }) }
289
+ workerUp() { return new Promise((res, rej) => { if (!this.worker || this.workerReady) return res(); if (this.workerFailed) return rej(this.workerFailed); this.onWorkerReady = res; this.onWorkerFailed = rej }) }
286
290
 
287
291
  // The mark a free avatar carries: a "Powered by stork.ai" lockup, bottom right, and Stork's bird, which flies in over the picture,
288
292
  // lands on the lockup's orange disc, and every so often takes off and comes back. Both images come from the avatar file.
package/src/stork-live.js CHANGED
@@ -114,7 +114,11 @@ export class StorkLive {
114
114
  motion.onFrames = (f0, v) => { if (this.speaking !== sp) return; for (let i = 0; i < v.length / KM; i++) this.frames.set(f0 + i, v.slice(i * KM, (i + 1) * KM)) }
115
115
  proc.onaudioprocess = (e) => {
116
116
  const d = e.inputBuffer.getChannelData(0); e.outputBuffer.getChannelData(0).set(d) // pass the sound through; it is heard `delay` later, in step with the lips
117
- if (sp.tPlay === Infinity) sp.tPlay = e.playbackTime + delay // sample 0 of the stream is heard at this audio-clock time
117
+ if (sp.tPlay === Infinity) {
118
+ // Chrome hands over each block 64 ms ahead of the audio clock, Firefox about 8 ms. Hold the sound back by the difference, so the lips have the same head start everywhere.
119
+ const d = delay + Math.min(0.1, Math.max(0, 0.064 - (e.playbackTime - ctx.currentTime)))
120
+ dl.delayTime.value = d; sp.tPlay = e.playbackTime + d // sample 0 of the stream is heard at this audio-clock time
121
+ }
118
122
  motion.push(d); sp.sent += d.length
119
123
  }
120
124
  node.connect(proc); proc.connect(dl); dl.connect(ctx.destination)