(function () {
"use strict";
/**
* Which elements are narration clips?
*
* The prompt tells an author to tag narration with data-narration; this writer tags its own clips
* with data-narration-clip. MEASURED: an author did exactly what the prompt said, and the driver
* selected none of it, so run 1 shipped silent with
*
* sitting in the document. Both spellings are therefore selected.
*
* Widening a selector is only safe with a bed guard, because data-narration is also a plausible
* spelling for an authored music bed. isNarrationClip is that guard: it refuses anything with no
* playable src, anything looped or explicitly bed-tagged, and anything whose id/src/class is named
* like a bed (master-bgm, bgm, music, ambient, soundtrack, sfx). A false rejection costs one
* scene's speech; a false acceptance plays a music bed as a voice for the whole film, so the guard
* errs toward rejecting.
*/
var BED = /(?:^|[\/_.-])(?:bgm|music|musicbed|bed|ambient|ambience|soundtrack|score|sfx|foley|whoosh|sting|chime)(?:[\/_.-]|$)/i;
function srcOf(el) {
var own = el.getAttribute("src");
if (own) return own;
var nested = el.querySelector("source[src]");
return nested ? nested.getAttribute("src") || "" : "";
}
function isNarrationClip(el) {
if (!el || el.tagName !== "AUDIO") return false;
var src = srcOf(el);
if (!src) return false;
// A data: URI carries inline bytes, never a TTS response: every voice this platform
// synthesises is an https URL. The master template's silent bed is exactly
// , so this closes the one
// shape that could be tagged data-narration (or lose its loop / bed-named id in an edit)
// while still being a bed. Costs no speech: no legitimate clip is ever a data URI.
if (/^data:/i.test(src)) return false;
if (el.loop) return false;
if (el.hasAttribute("data-bgm") || el.hasAttribute("data-music") || el.hasAttribute("data-ambient")) return false;
var clues = (el.getAttribute("id") || "") + " " + (el.getAttribute("class") || "") + " " + src;
if (BED.test(clues)) return false;
return true;
}
function collect() {
var nodes = document.querySelectorAll("audio[data-narration-clip], audio[data-narration]");
var out = [];
for (var i = 0; i < nodes.length; i++) if (isNarrationClip(nodes[i])) out.push(nodes[i]);
return out;
}
var CLIPS = collect();
if (!CLIPS.length) return;
var TOTAL = 40;
var current = null;
/**
* Give every clip a start time and a duration.
*
* Run 1's markup was .
* It carries data-scene and no data-start, because the prompt never mentions data-start. A driver
* that reads only data-start maps every such clip to 0, so clipAt() returns the LAST one and every
* earlier scene stays silent — the widening would select the clips and still play one sentence.
*
* So every clip gets a slot, decided per clip in order of how much we trust the evidence:
* 1. its own data-start, when it declares one (our own writer always does);
* 2. otherwise its position in data-scene / data-index order, else in document order.
* A clip that declares no duration gets an even share of the composition, which is the same
* honest default the extractor uses when it has no scene table.
*/
(function assignTimings() {
// Order by data-scene/data-index when the author numbered scenes; otherwise document order is
// already the right reading order.
var indexed = CLIPS.map(function (c, i) {
var raw = c.dataset.scene != null ? c.dataset.scene : c.dataset.index;
var num = raw != null ? parseFloat(raw) : NaN;
return { el: c, i: i, order: isFinite(num) ? num : i };
});
indexed.sort(function (a, b) { return a.order - b.order || a.i - b.i; });
CLIPS = indexed.map(function (x) { return x.el; });
var span = TOTAL;
if (!span) {
for (var k = 0; k < CLIPS.length; k++) {
var dk = parseFloat(CLIPS[k].dataset.duration || "0");
if (isFinite(dk) && dk > 0) span += dk;
}
}
var per = CLIPS.length ? (span || CLIPS.length * 5) / CLIPS.length : 0;
for (var j = 0; j < CLIPS.length; j++) {
var c = CLIPS[j];
// DECLARED start wins; otherwise the ordered position is the best available evidence. This is
// per-clip on purpose: an all-or-nothing rule collapsed every underived clip onto 0 as soon as
// ONE clip in the document declared data-start, so clipAt() returned only the last scene.
var declared = c.dataset.start != null ? parseFloat(c.dataset.start) : NaN;
var start = isFinite(declared) ? declared : j * per;
var dur = parseFloat(c.dataset.duration || "0");
if (!isFinite(dur) || dur <= 0) dur = per;
c.__hfStart = start;
c.__hfDur = dur;
}
})();
/**
* Resolve a clock reader.
*
* Measured failure this replaces: the driver took window.voltPlayer because it has seek(), but
* that object exposes NO time reader at the top level -- it hangs the real adapter off
* .adapter. The clock therefore read 0 forever, so a seek to 16.2s kept playing clip 1 and
* pausing left audio running. Resolution therefore prefers anything that can actually be READ,
* and unwraps .adapter when it must.
*/
function readable(obj) {
if (!obj) return null;
if (typeof obj.time === "function") return obj;
if (typeof obj.getTime === "function") return obj;
if (obj.adapter && obj.adapter !== obj && readable(obj.adapter)) return readable(obj.adapter);
return null;
}
function adapter() {
// 1. Whatever the platform player resolved (it may wrap the real adapter).
var viaPlayer = readable(window.voltPlayer) || readable(window.__player);
if (viaPlayer) return viaPlayer;
// 2. The composition's own controller.
var viaHf = readable(window.hyperframesPlayer);
if (viaHf) return viaHf;
// 3. The GSAP timeline registry — the single source of truth if anything else is absent.
var tls = window.__timelines || {};
var names = Object.keys(tls);
if (names.length) {
var tl = names
.map(function (n) { return tls[n]; })
.filter(function (t) { return t && typeof t.time === "function"; })
.sort(function (a, b) { return (b.duration() || 0) - (a.duration() || 0); })[0];
if (tl) {
return {
time: function () { return tl.time(); },
duration: function () { return tl.duration() || TOTAL || 0; },
isPaused: function () { return tl.paused(); },
};
}
}
return null;
}
/**
* Is playback stopped?
*
* This is what makes pause silence the narration. Every reader is tried, and when a controller
* genuinely cannot answer, false is NOT assumed — a driver that guesses "playing" leaves
* audio running under a paused frame, which is the defect this replaced.
*/
function isPaused(clock) {
var candidates = [clock, window.voltPlayer, window.__player, window.hyperframesPlayer];
for (var i = 0; i < candidates.length; i++) {
var c = candidates[i];
if (!c) continue;
if (typeof c.isPaused === "function") { try { return !!c.isPaused(); } catch (e) {} }
if (typeof c.paused === "function") { try { return !!c.paused(); } catch (e) {} }
if (typeof c.paused === "boolean") return c.paused;
if (c.adapter && c.adapter !== c) {
try {
if (typeof c.adapter.isPaused === "function") return !!c.adapter.isPaused();
if (typeof c.adapter.paused === "function") return !!c.adapter.paused();
} catch (e) {}
}
}
// Last resort: GSAP's own flag on the longest timeline.
var tls = window.__timelines || {};
var names = Object.keys(tls);
for (var j = 0; j < names.length; j++) {
var tl = tls[names[j]];
if (tl && typeof tl.paused === "function") { try { return !!tl.paused(); } catch (e) {} }
}
return false;
}
/**
* The clip owning time t.
*
* The window is the gap to the NEXT clip's start when one exists, not the clip's own audio
* length: a TTS clip is usually shorter than its scene, and a window sized from the audio would
* leave the tail of a scene silent while the frame is still showing it.
*/
function clipAt(t) {
var best = null;
for (var i = 0; i < CLIPS.length; i++) {
var c = CLIPS[i];
var start = c.__hfStart;
var nextStart = i + 1 < CLIPS.length ? CLIPS[i + 1].__hfStart : Infinity;
var end = isFinite(nextStart) ? nextStart : start + c.__hfDur;
if (t + 0.001 >= start && (t < end || (!isFinite(nextStart) && t <= end + 0.001))) best = c;
}
return best;
}
/**
* A clip that is not the active one is paused AND rewound, so the next arrival at its scene
* starts from its first word rather than from wherever it was left.
*/
function silence(except) {
for (var i = 0; i < CLIPS.length; i++) {
var c = CLIPS[i];
if (c === except) continue;
if (!c.paused) { try { c.pause(); } catch (e) {} }
}
}
function step() {
var clock = adapter();
var t = clock && typeof clock.time === "function" ? clock.time() || 0 : 0;
var paused = isPaused(clock);
var want = paused ? null : clipAt(t);
if (want !== current) {
current = want;
silence(want);
if (want) {
var localOffset = Math.max(0, t - want.__hfStart);
try {
if (Math.abs((want.currentTime || 0) - localOffset) > 0.3) want.currentTime = localOffset;
} catch (e) {}
if (paused) { try { want.pause(); } catch (e) {} }
else {
var pr = want.play();
if (pr && typeof pr.catch === "function") pr.catch(function () {});
}
}
} else if (want) {
// Same clip still active: correct slow drift without stepping on the clip's own playback.
var expected = Math.max(0, t - want.__hfStart);
if (!paused && Math.abs((want.currentTime || 0) - expected) > 0.85) {
try { want.currentTime = expected; } catch (e) {}
}
if (paused && !want.paused) { try { want.pause(); } catch (e) {} }
} else {
silence(null);
}
requestAnimationFrame(step);
}
/**
* A seek is a discontinuity: snap the clip's currentTime rather than waiting for the drift
* threshold, which would leave a wrong segment of speech playing for up to a second.
*/
/**
* A seek is a discontinuity, so the right clip must start immediately rather than waiting for
* the drift threshold in step(). Measured failure this replaces: seeking to 16.2s left clip 1
* playing from 0.31s, because the driver only compared against its own current clip and never
* mapped the clock onto the segment map.
*/
function syncNow() {
var clock = adapter();
var t = clock && typeof clock.time === "function" ? clock.time() || 0 : 0;
var paused = isPaused(clock);
var want = paused ? null : clipAt(t);
silence(want);
current = want;
if (want) {
var local = Math.max(0, t - want.__hfStart);
try {
if (Math.abs((want.currentTime || 0) - local) > 0.15) want.currentTime = local;
} catch (e) {}
if (paused) { try { want.pause(); } catch (e) {} }
else {
var pr = want.play();
if (pr && typeof pr.catch === "function") pr.catch(function () {});
}
}
}
// The platform player's seek, the composition's controller, and any direct GSAP seek all have
// to land on the right clip. Wrapping is safe: each wrap is installed once.
["seek"].forEach(function (method) {
[window.voltPlayer, window.__player, window.hyperframesPlayer].forEach(function (owner) {
if (!owner || typeof owner[method] !== "function" || owner.__hfNarrationWrapped) return;
var orig = owner[method];
owner[method] = function () {
var res = orig.apply(this, arguments);
setTimeout(syncNow, 0);
return res;
};
owner.__hfNarrationWrapped = true;
});
});
document.addEventListener("seeking", syncNow, true);
document.addEventListener("pointerup", function () { setTimeout(syncNow, 0); }, true);
// Pause must silence immediately, not on the next drift threshold.
var lastPaused = null;
function watchPause() {
var clock = adapter();
var nowPaused = isPaused(clock);
if (lastPaused === null || nowPaused !== lastPaused) {
lastPaused = nowPaused;
if (nowPaused) silence(null);
else syncNow();
}
setTimeout(watchPause, 120);
}
watchPause();
requestAnimationFrame(step);
window.hfNarration = {
voice: "christopher",
clips: CLIPS.length,
mute: function (on) { CLIPS.forEach(function (c) { c.muted = !!on; }); },
};
})();