Files

132 lines
6.1 KiB
Python

# -*- coding: utf-8 -*-
# Q2 fix: wrap-aware resume trong worklet sf-fx-realtime.js
# - KEEP 4 -> 16 (85ms tail che stall 68-82ms -> het gap wrap)
# - wrap underrun: bo 'flush' (engine da restart, block moi khong stale) +
# resumeFill 8 (~43ms) thay FILL 24 (~128ms)
import io
P = r'app/static/js/sf-fx-realtime.js'
s = io.open(P, 'r', encoding='utf-8').read()
# 1) constructor: wrapMode flag
old = " this.latencySamples = 0; // G3: latency chain thật (engine qua WS) — align wrap fade theo processed tail"
new = old + "\n this.wrapMode = false; // V45.3: dang trong wrap (clear_queue loop) — resume underrun bo flush + re-buffer nho"
assert s.count(old) == 1, 'ctor latency line'
s = s.replace(old, new)
# 2) clear_queue mode='loop': KEEP 4 -> 16 + set wrapMode
old = """ // V45.2 (option B): cắt bớt tail cũ giữ KEEP block (~21ms cushion) thay
// vì giữ nguyên FILL 24 (~128ms) → downbeat pass mới không trễ ~110ms.
// skipFade crossfade từ lastOut che jump cắt front; wrapFadeBlocks đếm
// lại từ KEEP để seam crossfade vẫn đúng lúc.
const KEEP = 4;
if (this.outQueue.length > KEEP) {
const head = this.outQueue.length - KEEP;
this.outQueue.splice(0, head);
this.outPos = 0;
if (this.playing) {
this.skipFade = this.WRAP_FADE;
this.skipFadeTotal = this.WRAP_FADE;
this.skipFromL = this.lastOutL;
this.skipFromR = this.lastOutR;
}
}
this.wrapFadeBlocks = this.outQueue.length + Math.ceil((this.latencySamples || 0) / this.BLOCK);
return;"""
new = """ // V45.2 (option B): cắt bớt tail cũ giữ KEEP block thay vì giữ nguyên
// FILL 24 (~128ms) → downbeat pass mới không trễ ~110ms. skipFade
// crossfade từ lastOut che jump cắt front; wrapFadeBlocks đếm lại từ
// KEEP để seam crossfade vẫn đúng lúc.
// V45.3: KEEP 4 -> 16 (~85ms): KEEP 4 (~21ms) engine (stall bridge
// 68-138ms sau transport play) khong refill kip -> queue can -> underrun
// -> flush + re-buffer FILL 24 = gap ~110-235ms moi wrap (WORSE hon
// truoc V45.2: downbeat tre 106-128ms nhung khong gap). KEEP 16 che
// stall dien hinh 68-82ms lien tuc (khong gap, downbeat ~85ms); stall
// dai hon roi vao nhanh resume wrap (bo flush + resumeFill 8).
const KEEP = 16;
this.wrapMode = true;
if (this.outQueue.length > KEEP) {
const head = this.outQueue.length - KEEP;
this.outQueue.splice(0, head);
this.outPos = 0;
if (this.playing) {
this.skipFade = this.WRAP_FADE;
this.skipFadeTotal = this.WRAP_FADE;
this.skipFromL = this.lastOutL;
this.skipFromR = this.lastOutR;
}
}
this.wrapFadeBlocks = this.outQueue.length + Math.ceil((this.latencySamples || 0) / this.BLOCK);
return;"""
assert s.count(old) == 1, 'loop clear block'
s = s.replace(old, new)
# 3) flush_for_stop: reset wrapMode
old = """ this.resumeFill = this.RESUME_FILL;
this.silenced = false;"""
new = """ this.resumeFill = this.RESUME_FILL;
this.silenced = false;
this.wrapMode = false;"""
assert s.count(old) == 1, 'flush_for_stop'
s = s.replace(old, new)
# 4) clear_queue wipe (play/ARM): reset wrapMode
old = """ this.playing = false;
this.wrapFadeBlocks = -1; // V42: wipe — hủy wrap crossfade chờ (không có tail cũ)
this.resumeFadeIn = true;"""
new = """ this.playing = false;
this.wrapFadeBlocks = -1; // V42: wipe — hủy wrap crossfade chờ (không có tail cũ)
this.wrapMode = false; // V45.3: play/seek — het context wrap
this.resumeFadeIn = true;"""
assert s.count(old) == 1, 'wipe branch'
s = s.replace(old, new)
# 5) wasInterrupted: wrap-aware
old = """ const wasInterrupted = !this.resumeFadeIn && (this.holdFadeLeft > 0 || this.silenced) && this.hasStarted;
if (wasInterrupted) {
this.outQueue.length = 0;
this.outPos = 0;
this.flushPending = true;
this.resumeFadeIn = true;
this.resumeFill = this.FILL; // underrun-stall: giữ cushion FILL đầy đủ (V25 wobble)
this.port.postMessage({ type: 'flush' });
i--;
continue;
}"""
new = """ const wasInterrupted = !this.resumeFadeIn && (this.holdFadeLeft > 0 || this.silenced) && this.hasStarted;
if (wasInterrupted) {
// V45.3: wrap (loop restart) — engine da transport('play') tu main
// (D17): block moi = audio pass moi, KHONG phai backlog stale (boi
// worklet flush gui 'flush' xuong engine ring = lam mat tac dung D17
// skip-flush o stopAllPlayback -> gap giua seam). Bo flush (round-trip)
// + re-buffer nho hon FILL 24 (~128ms) -> resume sau stall som hon
// (~43ms) -> gap wrap giam tu ~110-235ms xuong con vai chuc ms.
const _wrap = this.wrapMode;
this.outQueue.length = 0;
this.outPos = 0;
this.resumeFadeIn = true;
this.resumeFill = _wrap ? 8 : this.FILL;
this.flushPending = _wrap ? false : true;
if (!_wrap) this.port.postMessage({ type: 'flush' });
i--;
continue;
}"""
assert s.count(old) == 1, 'wasInterrupted block'
s = s.replace(old, new)
# 6) resume success: reset wrapMode
old = """ this.playing = true;
this.outPos = 0;
this.holdFadeLeft = 0;"""
new = """ this.playing = true;
this.outPos = 0;
this.holdFadeLeft = 0;
this.wrapMode = false; // V45.3: resume xong — ve che do thuong (underrun sau nay flush nhu cu)"""
assert s.count(old) == 1, 'resume success'
s = s.replace(old, new)
io.open(P, 'w', encoding='utf-8', newline='\r\n').write(s)
raw = open(P, 'rb').read()
print('CRLF:', raw.count(b'\r\n'), 'LF-only:', raw.count(b'\n') - raw.count(b'\r\n'))
print('patch ok')