Manjunath Kudlur
commited on
Commit
·
115eadd
1
Parent(s):
23932e3
Decode called only on the last post message
Browse files- decoder_worker.js +48 -34
decoder_worker.js
CHANGED
|
@@ -326,6 +326,43 @@ async function decodeAccumulated() {
|
|
| 326 |
}
|
| 327 |
}
|
| 328 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 329 |
// Message queue for sequential processing
|
| 330 |
const messageQueue = [];
|
| 331 |
let isProcessingQueue = false;
|
|
@@ -420,43 +457,21 @@ async function processMessage(e) {
|
|
| 420 |
case 'features': {
|
| 421 |
if (data.segmentId !== currentSegmentId) break;
|
| 422 |
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
dims: data.dims
|
| 426 |
-
};
|
| 427 |
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
const numFrames = accumulatedFeatures.dims[1];
|
| 435 |
-
const keepFrames = Math.max(0, numFrames - tailLatency);
|
| 436 |
-
|
| 437 |
-
if (keepFrames > 0) {
|
| 438 |
-
const totalFrames = keepFrames + newFeatures.dims[1];
|
| 439 |
-
const combined = new Float32Array(totalFrames * cfg.dim);
|
| 440 |
-
|
| 441 |
-
// Copy kept frames
|
| 442 |
-
for (let f = 0; f < keepFrames; f++) {
|
| 443 |
-
for (let d = 0; d < cfg.dim; d++) {
|
| 444 |
-
combined[f * cfg.dim + d] = accumulatedFeatures.data[f * cfg.dim + d];
|
| 445 |
-
}
|
| 446 |
-
}
|
| 447 |
-
// Copy new frames
|
| 448 |
-
combined.set(newFeatures.data, keepFrames * cfg.dim);
|
| 449 |
-
|
| 450 |
-
accumulatedFeatures = {
|
| 451 |
-
data: combined,
|
| 452 |
-
dims: [1, totalFrames, cfg.dim]
|
| 453 |
-
};
|
| 454 |
-
} else {
|
| 455 |
-
accumulatedFeatures = newFeatures;
|
| 456 |
}
|
| 457 |
}
|
| 458 |
|
| 459 |
-
|
|
|
|
|
|
|
| 460 |
const now = Date.now();
|
| 461 |
const timeSinceLastDecode = now - lastDecodeTime;
|
| 462 |
|
|
@@ -477,7 +492,6 @@ async function processMessage(e) {
|
|
| 477 |
// If there was a pending decode request, schedule it
|
| 478 |
if (pendingDecode) {
|
| 479 |
pendingDecode = false;
|
| 480 |
-
// Use setTimeout to avoid blocking - decode will happen on next message or timeout
|
| 481 |
setTimeout(async () => {
|
| 482 |
if (!isDecoding && currentSegmentId !== null) {
|
| 483 |
isDecoding = true;
|
|
|
|
| 326 |
}
|
| 327 |
}
|
| 328 |
|
| 329 |
+
// Helper to accumulate features data
|
| 330 |
+
function accumulateFeaturesData(data) {
|
| 331 |
+
const newFeatures = {
|
| 332 |
+
data: new Float32Array(data.features),
|
| 333 |
+
dims: data.dims
|
| 334 |
+
};
|
| 335 |
+
|
| 336 |
+
if (accumulatedFeatures === null) {
|
| 337 |
+
accumulatedFeatures = newFeatures;
|
| 338 |
+
} else {
|
| 339 |
+
// Trim last tailLatency frames from accumulated
|
| 340 |
+
const numFrames = accumulatedFeatures.dims[1];
|
| 341 |
+
const keepFrames = Math.max(0, numFrames - tailLatency);
|
| 342 |
+
|
| 343 |
+
if (keepFrames > 0) {
|
| 344 |
+
const totalFrames = keepFrames + newFeatures.dims[1];
|
| 345 |
+
const combined = new Float32Array(totalFrames * cfg.dim);
|
| 346 |
+
|
| 347 |
+
// Copy kept frames
|
| 348 |
+
for (let f = 0; f < keepFrames; f++) {
|
| 349 |
+
for (let d = 0; d < cfg.dim; d++) {
|
| 350 |
+
combined[f * cfg.dim + d] = accumulatedFeatures.data[f * cfg.dim + d];
|
| 351 |
+
}
|
| 352 |
+
}
|
| 353 |
+
// Copy new frames
|
| 354 |
+
combined.set(newFeatures.data, keepFrames * cfg.dim);
|
| 355 |
+
|
| 356 |
+
accumulatedFeatures = {
|
| 357 |
+
data: combined,
|
| 358 |
+
dims: [1, totalFrames, cfg.dim]
|
| 359 |
+
};
|
| 360 |
+
} else {
|
| 361 |
+
accumulatedFeatures = newFeatures;
|
| 362 |
+
}
|
| 363 |
+
}
|
| 364 |
+
}
|
| 365 |
+
|
| 366 |
// Message queue for sequential processing
|
| 367 |
const messageQueue = [];
|
| 368 |
let isProcessingQueue = false;
|
|
|
|
| 457 |
case 'features': {
|
| 458 |
if (data.segmentId !== currentSegmentId) break;
|
| 459 |
|
| 460 |
+
// Accumulate this message's features
|
| 461 |
+
accumulateFeaturesData(data);
|
|
|
|
|
|
|
| 462 |
|
| 463 |
+
// Drain all pending features messages from the queue and accumulate them too
|
| 464 |
+
while (messageQueue.length > 0 && messageQueue[0].data.type === 'features') {
|
| 465 |
+
const nextMsg = messageQueue.shift();
|
| 466 |
+
const nextData = nextMsg.data.data;
|
| 467 |
+
if (nextData.segmentId === currentSegmentId) {
|
| 468 |
+
accumulateFeaturesData(nextData);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 469 |
}
|
| 470 |
}
|
| 471 |
|
| 472 |
+
console.log(`Decoder accumulated features, total: ${accumulatedFeatures ? accumulatedFeatures.dims[1] : 0} frames`);
|
| 473 |
+
|
| 474 |
+
// Live caption with throttling
|
| 475 |
const now = Date.now();
|
| 476 |
const timeSinceLastDecode = now - lastDecodeTime;
|
| 477 |
|
|
|
|
| 492 |
// If there was a pending decode request, schedule it
|
| 493 |
if (pendingDecode) {
|
| 494 |
pendingDecode = false;
|
|
|
|
| 495 |
setTimeout(async () => {
|
| 496 |
if (!isDecoding && currentSegmentId !== null) {
|
| 497 |
isDecoding = true;
|