// THE VOICE A POST ITEM's OWN READ IS SAID IN (a single post's take: `umtool posts narrate
- --take`, the
// bench's Re-take), through the SAME function a thread read asks for his piece (post-say.mjs resolvePieceVoice): the
// read's own voice (--voice, the bench's voice field), the item's own character (`narration.character`), the
// project's characters and voice map, the CAST, the narrator's (the item's own `narration.voice`, else the cut's). So
// a post read alone and the same post read with its thread agree.
//
// Plain ESM.
import { postNarrationKnobs } from "umtool-report-to-video/post-narration";
import { resolvePieceVoice, voiceRefOf } from "umtool-report-to-video/post-say";
/**
* `{ engine, voice, from, speed?, character? }` for post item `e`'s own read -- `from` "override", "item", "project",
* "cast", "narrator"; `speed` and `character` the preset's that answered (a character's `synthetic.speed`, its id).
* `map`, `cast` and `item` (voiceSourceOf's layers) are read from the project when not given (characterLayers.mjs; a
* broken voice map reads as none here: the read is not refused for it; `umtool posts voices` says what is wrong).
*
* @param {{ dir: string }} project
* @param {Record} e
* @param {{ render?: Record, engine?: string | null, voice?: string | null, map?: Record, cast?: Record, item?: any }} [o]
*/
export async function itemReadVoice(project, e, { render = {}, engine = null, voice = null, map = undefined, cast = undefined, item = undefined } = {}) {
const k = postNarrationKnobs(e, render);
const eng = engine ?? e.narration?.engine ?? k.engine;
const narrator = e.narration?.voice ?? k.voice;
if (voice) {
const ref = voiceRefOf(voice, eng) ?? { engine: eng, voice: String(voice) };
return { engine: engine ?? ref.engine ?? eng, voice: ref.voice, from: "override" };
}
const L = map !== undefined && cast !== undefined && item !== undefined ? null : await (await import("./characterLayers.mjs")).readLayers(project.dir).catch(() => null);
const m = map !== undefined ? map : L?.map ?? {};
const c = cast !== undefined ? cast : L?.castMap ?? {};
const it = item !== undefined ? item : L?.itemOf(e) ?? null;
const r = resolvePieceVoice({ handle: e.handle ?? null, role: "post" }, { map: m, cast: c, item: it, self: e.handle ?? null, narrator, engine: eng });
// An engine asked for outright that the resolved voice is not of: the narrator's voice in it.
if (engine && r.engine !== engine) return { engine, voice: narrator, from: "narrator" };
return r;
}