// speech-voice-demo2.el - LIVE VOICE LOOP on Will's richer 30s read, with a
// GEOMETRIC SET-REPLACE of the voice_will manifold (supersede the coarse 10s
// region, insert the 30s region — no duplicate node, no per-node CRUD; Will's
// standing rule f999c5ff). HONEST: 30s steadies the 11-number average over more
// of his vowels, but it is still one formant triple with no coarticulation or
// prosody — closer but still synthetic, not a clone.

fn main() {
    let outdir: String = "/Users/will/Development/neuron-technologies/foundation/el/.claude/worktrees/agent-acc02900ef4ade35e/elp/tests/examples/out/"
    let vp: String = "/private/tmp/claude-501/-Users-will/6531446d-bc27-4095-930b-e04777c3db4f/scratchpad/will30-voiceprint.json"
    let manifest: String = "elp/data/will-voice.engram.json"

    // --- SET-REPLACE step 1: read the PRIOR region (text read of the manifold
    //     file — no engram_load, so the store stays clean) and report what is
    //     being superseded. ---
    let prior: String = fs_read(manifest)
    let pp: Int = str_index_of(prior, "voice will ")
    if pp >= 0 {
        let pw: String = str_slice(prior, pp, pp + 200)
        println("[set-replace] superseding PRIOR voice region: f0=" + int_to_str(parse_uint_from(pw, "f0=")) + " kf=" + int_to_str(parse_uint_from(pw, "kf=")) + " f1=" + int_to_str(parse_uint_from(pw, "f1=")))
    }

    // --- step 2: reshape the 30s voiceprint -> organ voice-signature source ---
    let sig: [Int] = reshape_voiceprint(vp, "elp/data/will-voice.json")

    // --- step 3: INSERT the fresh 30s region into an EMPTY engram and save ->
    //     wholesale replaces the manifold file (old region dropped, not edited,
    //     not duplicated). This is the geometric set-replace. ---
    let ig: Int = ingest_voice(sig, manifest)

    // --- step 4: READ the new target BACK from geometry ---
    let g: [Int] = load_voice(manifest)
    println("[voice-geometry] new region read from manifold: f0=" + int_to_str(native_list_get(g, 0)) + " f0_end=" + int_to_str(native_list_get(g, 1)) + " kf=" + int_to_str(native_list_get(g, 2)) + " f1=" + int_to_str(native_list_get(g, 3)) + " f2=" + int_to_str(native_list_get(g, 4)) + " f3=" + int_to_str(native_list_get(g, 5)) + " (measured 30s, COARSE — not a clone)")

    // phoneme + lexicon geometry from the organ (loaded after the voice sig is
    // cached in EL, since engram_load replaces the store)
    let pmap: [String] = organ_pmap("elp/data/phonetics-formants.engram.json")
    let lmap: [String] = organ_lex("elp/data/lexicon.psv")

    // --- step 5: render a fresh reply FROM MEANING in the 30s Will voice ---
    let vw: [String] = voice_will(native_list_get(g, 0), native_list_get(g, 1), native_list_get(g, 2))
    let t: String = sem_realize(sem_frame("greet", "Will", "", ""))
    let codes: [String] = text_phonemes(lmap, t)
    println("[render] \"" + t + "\" :: " + list_join(codes, " ") + "  in voice=will f0=" + int_to_str(voice_get_int(vw, "f0")) + " kf=" + int_to_str(voice_get_int(vw, "kf")))
    let samples: [Int] = synth_codes(codes, vw, pmap)
    let ok: Bool = write_wav(samples, 16000, outdir + "will-reply2.wav")
    println("[done] will-reply2.wav=" + bool_to_str(ok))
}
