// AN ORPHEUS TUNING AND A READING (lane orpheus-tuning, S1 of the re-tune // plan): the knobs a synthesis may carry so the same words are read again // differently, and their ranges. Shared by the editor's tts op // (controller/ttsFile.ts, lib/ttsApps.ts) and umtool (its post bench, its CLI, // a card's narration), so a value is refused in the same words everywhere. // // tuning: { temperature?, top_p?, top_k?, min_p?, repeat_penalty?, takes? } // -- the sampler's knobs (llama-server's names) and the takes a chunk // gets; a missing one is the default (ttsApps.ts ORPHEUS_SAMPLER, the // settings' tts.orpheus.takes). // reading: a whole number, 0 = today's seeds; reading r reads with the seed // base ORPHEUS_SEED_BASE + 9973 x r ("another reading" = r + 1). // // Pure: no I/O, no defaults (those are the engine's). /** Each field's range: [lowest, highest], and whether it is a whole number. */ export const TTS_TUNING_RANGES = Object.freeze({ // At or below 0 llama.cpp samples greedily, and the seed would mean nothing. temperature: Object.freeze({ min: 0.1, max: 1.5, integer: false }), top_p: Object.freeze({ min: 0.05, max: 1, integer: false }), // 0 switches top-k off. top_k: Object.freeze({ min: 0, max: 200, integer: true }), // 0 switches min-p off. min_p: Object.freeze({ min: 0, max: 0.5, integer: false }), // Below 1 a repeat would be rewarded. repeat_penalty: Object.freeze({ min: 1, max: 2, integer: false }), // tts.orpheus.takes' own range (ttsSettings.ts). takes: Object.freeze({ min: 1, max: 6, integer: true }), }); export const TTS_TUNING_FIELDS = Object.freeze(Object.keys(TTS_TUNING_RANGES)); /** * Orpheus's defaults for the tunable fields: common/lib/ttsApps.ts * ORPHEUS_SAMPLER's (a test holds the two equal) and the settings' default * takes (ttsSettings.ts DEFAULT_ORPHEUS_TTS_SETTINGS.takes). Here so a caller in * bare node (umtool) normalises a tuning as the editor does. */ export const ORPHEUS_TUNING_DEFAULTS = Object.freeze({ temperature: 0.55, top_p: 0.85, top_k: 50, min_p: 0, repeat_penalty: 1.2 }); export const ORPHEUS_DEFAULT_TAKES = 3; /** The highest reading (the seed base stays under 2^31). */ export const TTS_READING_MAX = 99_999; /** The step between two readings' seed bases: a prime well past one reading's own seeds (101 x takes + chunks). */ export const TTS_READING_SEED_STEP = 9973; const isObj = (v) => v !== null && typeof v === "object" && !Array.isArray(v); const shown = (v) => (typeof v === "number" ? String(v) : JSON.stringify(v) ?? String(v)); /** * One field's value as it is checked, sent and keyed: a decimal field rounded * to 3 places (0.550000001 is 0.55, 0.7000000000000001 is 0.7), -0 as 0. A * value that is not a finite number is returned as it is (and refused). */ export function quantizeTtsTuningValue(field, value) { if (typeof value !== "number" || !Number.isFinite(value)) return value; const r = TTS_TUNING_RANGES[field]; const q = r && !r.integer ? Math.round(value * 1000) / 1000 : value; return q === 0 ? 0 : q; } /** * A tuning with every value that EQUALS its default left out (after * quantising): the sampler's against ORPHEUS_TUNING_DEFAULTS, `takes` against * `opts.takes` (the settings' takes; omitted: `takes` is kept as given). `{}` is * "no tuning". Unknown and non-numeric fields are dropped. */ export function normalizeTtsTuning(tuning, { takes } = {}) { const out = {}; if (!isObj(tuning)) return out; for (const k of TTS_TUNING_FIELDS) { const v = quantizeTtsTuningValue(k, tuning[k]); if (typeof v !== "number" || !Number.isFinite(v)) continue; if (k === "takes" ? takes !== undefined && v === takes : v === ORPHEUS_TUNING_DEFAULTS[k]) continue; out[k] = v; } return out; } /** "a number from 0.1 to 1.5" / "a whole number from 1 to 6". */ export function ttsTuningRangeText(field) { const r = TTS_TUNING_RANGES[field]; return `${r.integer ? "a whole number" : "a number"} from ${r.min} to ${r.max}`; } /** The refusal for one field's value, or null. `name` is how the caller names it ("tuning.top_p", "--top-p"). */ export function ttsTuningValueError(field, value, name = `"tuning.${field}"`) { const r = TTS_TUNING_RANGES[field]; const ok = typeof value === "number" && Number.isFinite(value) && value >= r.min && value <= r.max && (!r.integer || Number.isInteger(value)); return ok ? null : `${name} must be ${ttsTuningRangeText(field)} (got ${shown(value)})`; } /** * A tuning as asked: an object of the known fields, each quantised * (quantizeTtsTuningValue) and then in range. Undefined and null fields are * left out; an empty result is `{}`. * @returns {{ ok: true, value: Record } | { ok: false, error: string }} */ export function parseTtsTuning(raw, { name = "tuning" } = {}) { if (!isObj(raw)) return { ok: false, error: `"${name}" must be an object of ${TTS_TUNING_FIELDS.join(", ")}` }; const value = {}; for (const [k, v] of Object.entries(raw)) { if (!Object.hasOwn(TTS_TUNING_RANGES, k)) { return { ok: false, error: `"${name}.${k}" is not a tuning field (${TTS_TUNING_FIELDS.join(", ")})` }; } if (v === undefined || v === null) continue; const q = quantizeTtsTuningValue(k, v); const err = ttsTuningValueError(k, q, `"${name}.${k}"`); if (err) return { ok: false, error: err }; value[k] = q; } return { ok: true, value }; } /** The refusal for a reading, or null. */ export function ttsReadingError(value, name = '"reading"') { const ok = typeof value === "number" && Number.isInteger(value) && value >= 0 && value <= TTS_READING_MAX; return ok ? null : `${name} must be a whole number from 0 to ${TTS_READING_MAX} (0 = the first reading; got ${shown(value)})`; } /** A reading's seed base: `base + 9973 x reading`. */ export function ttsReadingSeedBase(base, reading = 0) { return base + TTS_READING_SEED_STEP * (reading || 0); } // --------------------------------------------------------------------------- // A CHARACTER's TUNING (lane character-tuning, S2 of the re-tune plan): the fields a character's synthetic preset // may carry, and three NAMED STARTING POINTS the operator picks from by ear -- the numbers always shown under them. // The names cover the sampler only; `takes` is a field of its own. // --------------------------------------------------------------------------- /** The tuning fields a character's synthetic preset may carry (umtool cast set --temperature …). */ export const CHARACTER_TUNING_FIELDS = Object.freeze(["temperature", "top_p", "repeat_penalty", "takes"]); /** The sampler fields a named starting point sets. */ export const ORPHEUS_TUNING_NAMED_FIELDS = Object.freeze(["temperature", "top_p", "repeat_penalty"]); /** * The named starting points: Steady (cooler and narrower -- steadier pitch and pace; the repeat penalty held, since a * cooler sampler loops more readily), Default (ORPHEUS_SAMPLER), Lively (warmer and wider -- more varied intonation, * a touch quicker; kept under 0.8, where words start to slur). */ export const ORPHEUS_TUNING_NAMED = Object.freeze({ steady: Object.freeze({ temperature: 0.4, top_p: 0.8, repeat_penalty: 1.2 }), default: Object.freeze({ temperature: ORPHEUS_TUNING_DEFAULTS.temperature, top_p: ORPHEUS_TUNING_DEFAULTS.top_p, repeat_penalty: ORPHEUS_TUNING_DEFAULTS.repeat_penalty }), lively: Object.freeze({ temperature: 0.75, top_p: 0.92, repeat_penalty: 1.25 }), }); export const ORPHEUS_TUNING_NAMES = Object.freeze(Object.keys(ORPHEUS_TUNING_NAMED)); /** * The named starting point a tuning's sampler is ("steady" | "default" | "lively"), or "custom": its temperature, * top_p and repeat_penalty (a missing one is the default) equal to one, and top_k and min_p the defaults. `takes` is * not looked at. */ export function ttsTuningNameOf(tuning) { const t = isObj(tuning) ? tuning : {}; const v = (k) => { const q = quantizeTtsTuningValue(k, t[k]); return typeof q === "number" && Number.isFinite(q) ? q : ORPHEUS_TUNING_DEFAULTS[k]; }; if (v("top_k") !== ORPHEUS_TUNING_DEFAULTS.top_k || v("min_p") !== ORPHEUS_TUNING_DEFAULTS.min_p) return "custom"; for (const name of ORPHEUS_TUNING_NAMES) { if (ORPHEUS_TUNING_NAMED_FIELDS.every((k) => v(k) === ORPHEUS_TUNING_NAMED[name][k])) return name; } return "custom"; }