Skip to content

armgit / hypit

Public Imported from GitHub
0

Clone any viral video with AI agents. Not just a script, the whole workflow: swap the face, the words, the B-roll, ship 100 variants in one command, and get your 100M views.

feat: support speech estimate padding

kashorinefbd9e992a11
diff --git a/packages/estimate/src/program.ts b/packages/estimate/src/program.ts
index f5ce44ef..36019e6d 100644
--- a/packages/estimate/src/program.ts
+++ b/packages/estimate/src/program.ts
@@ -148,6 +148,7 @@ export function assertSpeechEstimatePolicy(value: SpeechEstimatePolicy): void {
|| !Number.isFinite(value.maximumSec)
|| value.maximumSec <= 0
|| value.minimumSec > value.maximumSec
+ || (value.paddingSec !== undefined && (!Number.isFinite(value.paddingSec) || value.paddingSec < 0))
) {
throw new Error("SpeechEstimatePolicy is invalid");
}
@@ -184,7 +185,7 @@ export function estimateSpeechDuration(
const language = policy.language === "auto" ? detectSpeechEstimateLanguage(text.value) : policy.language;
const units = countSpeechEstimateUnits(text.value, language);
if (units < 1) throw new Error("Speech Text contains no countable speech units");
- const raw = units / resolveSpeechEstimateRate(policy, language);
+ const raw = units / resolveSpeechEstimateRate(policy, language) + (policy.paddingSec ?? 0);
const firstClamp = Math.min(policy.maximumSec, Math.max(policy.minimumSec, raw));
const durationSec = Math.min(policy.maximumSec, Math.max(policy.minimumSec, rounded(firstClamp, policy.rounding)));
const duration = sealSpeechDuration(durationSec);
diff --git a/packages/estimate/src/surface.ts b/packages/estimate/src/surface.ts
index af6a1a18..5b8772fe 100644
--- a/packages/estimate/src/surface.ts
+++ b/packages/estimate/src/surface.ts
@@ -24,7 +24,7 @@ function sameType(left: SurfaceResolvedReference["type"], right: SurfaceResolved
function attributes(element: StructuredElement): void {
const required = ["id", "source"];
- const direct = ["language", "pace", "rate", "min", "max", "rounding"];
+ const direct = ["language", "pace", "rate", "min", "max", "rounding", "padding"];
const optional = ["policy", ...direct];
const allowed = new Set([...required, ...optional]);
if (
@@ -84,7 +84,7 @@ export function speechEstimatePolicyFromRecipe(
recipe: SvsRecipe,
subject = `SVS Recipe ${recipe.path}`,
) {
- const allowed = new Set(["language", "pace", "rate", "min", "max", "rounding"]);
+ const allowed = new Set(["language", "pace", "rate", "min", "max", "rounding", "padding"]);
const unknown = Object.keys(recipe.properties).filter((name) => !allowed.has(name));
if (unknown.length > 0) throw new Error(`${subject} contains unknown property ${unknown[0]}`);
const string = (name: string): string => {
@@ -110,11 +110,11 @@ export function speechEstimatePolicyFromRecipe(
throw new Error(`${subject}.rate must be a finite number`);
}
const common = {
-
language: string("language") as SpeechEstimateLanguage,
minimumSec: finite("min"),
maximumSec: finite("max"),
rounding: string("rounding") as SpeechEstimateRounding,
+ ...(recipe.properties.padding !== undefined ? { paddingSec: finite("padding") } : {}),
} as const;
return rate === undefined
? sealSpeechEstimatePolicy({ ...common, pace: string("pace") as SpeechEstimatePace })
@@ -164,6 +164,7 @@ export const decodeSpeechEstimateSurface: StructuredSurfaceHandler = ({ element,
minimumSec: number(element, "min"),
maximumSec: number(element, "max"),
rounding: text(element, "rounding") as SpeechEstimateRounding,
+ ...(element.attributes.padding !== undefined ? { paddingSec: number(element, "padding") } : {}),
})
: recipePolicy(element, resolveReference);
const policyId = `${id}.policy`;
diff --git a/packages/estimate/src/types.ts b/packages/estimate/src/types.ts
index 2180f5dc..8b4e69a5 100644
--- a/packages/estimate/src/types.ts
+++ b/packages/estimate/src/types.ts
@@ -8,6 +8,7 @@ type SpeechEstimatePolicyBase = {
readonly minimumSec: number;
readonly maximumSec: number;
readonly rounding: SpeechEstimateRounding;
+ readonly paddingSec?: number;
};
export type SpeechEstimatePolicy = SpeechEstimatePolicyBase & (
diff --git a/packages/estimate/test/estimate.test.ts b/packages/estimate/test/estimate.test.ts
index 5ee5c18b..a688f03e 100644
--- a/packages/estimate/test/estimate.test.ts
+++ b/packages/estimate/test/estimate.test.ts
@@ -74,6 +74,37 @@ test("a numeric rate gives SVS a continuous author-controlled pace", () => {
assert.equal(result, 10);
});
+test("optional padding extends speech before bounds and rounding", async () => {
+ const source = sealText(Array.from({ length: 23 }, () => "day").join(" "));
+ const policy = sealSpeechEstimatePolicy({
+ language: "en",
+ pace: "normal",
+ minimumSec: 1,
+ maximumSec: 30,
+ rounding: "none",
+ paddingSec: 1,
+ });
+ assert.equal(estimateSpeechDuration(source, policy), 6);
+
+ const output = await estimateSurface('<estimate:Speech id="duration" source={speech} language="en" pace="normal" min="1" max="30" rounding="none" padding="1"/>');
+ const value = output.records[0]!.value;
+ assert.equal(value.kind === "inline" && (value.value as { readonly paddingSec?: number }).paddingSec, 1);
+
+ const recipe = speechEstimatePolicyFromRecipe({
+ path: "speech.padded",
+ properties: {
+ language: "en",
+ pace: "normal",
+ min: 1,
+ max: 30,
+ rounding: "none",
+ padding: 1,
+ },
+ });
+ assert.equal(recipe.paddingSec, 1);
+ assert.throws(() => sealSpeechEstimatePolicy({ ...policy, paddingSec: -1 }), /invalid/u);
+});
+
test("the author Surface requires one complete explicit inline policy", async () => {
const output = await estimateSurface('<estimate:Speech id="duration" source={speech} language="en" rate="4.75" min="4" max="15" rounding="round"/>');
const value = output.records[0]!.value;