// speech-voicegeom-demo.el - THE JUMP: render Will's VOWEL SPACE + PROSODY
// (measured over 30s), not the single 11-number average. His vowels land at HIS
// targets; pitch follows HIS melody. All read back FROM the ingested geometry.
// INTERIM: the geometry was Python-measured (measure_voice.py, numpy LPC/F0) —
// to be superseded by the engram-measures-audio path. No source layer.

fn main() {
    let outdir: String = "/Users/will/Development/neuron-technologies/foundation/el/.claude/worktrees/agent-acc02900ef4ade35e/elp/tests/examples/out/"

    // 1: ingest vowel space + prosody as geometry (empty store -> save; set-replace)
    let ig: Int = ingest_voicegeom("elp/data/will-vowelspace.psv", "elp/data/will-prosody.psv", "elp/data/will-voicegeom.engram.json")
    // kf (vocal-tract scale for consonants) from the earlier will-voice manifold
    let sigv: [Int] = load_voice("elp/data/will-voice.engram.json")
    let kf: Int = native_list_get(sigv, 2)
    // 2: read vowel space + prosody back FROM geometry
    let vmap: [String] = load_voicegeom("elp/data/will-voicegeom.engram.json")
    let pros: [Int] = prosody_from(vmap)
    println("[geometry] vowels=" + int_to_str((native_list_len(vmap) - 2) / 2) + " prosody f0_median=" + int_to_str(native_list_get(pros, 0)) + " f0_min=" + int_to_str(native_list_get(pros, 1)) + " f0_max=" + int_to_str(native_list_get(pros, 2)) + " kf=" + int_to_str(kf))
    let ehv: [Int] = vmap_get(vmap, "EH")
    let ihv: [Int] = vmap_get(vmap, "IH")
    println("[his-vowels] EH=" + int_to_str(native_list_get(ehv, 0)) + "/" + int_to_str(native_list_get(ehv, 1)) + " IH=" + int_to_str(native_list_get(ihv, 0)) + "/" + int_to_str(native_list_get(ihv, 1)))

    // phoneme geometry from the organ (loaded AFTER caches are in EL)
    let pmap: [String] = organ_pmap("elp/data/phonetics-formants.engram.json")
    let lmap: [String] = organ_lex("elp/data/lexicon.psv")

    // 3+4: render FROM MEANING in his-vowels + his-prosody voice
    let vw: [String] = voice_will(native_list_get(pros, 0), native_list_get(pros, 1), kf)
    let noacc: [String] = native_list_empty()
    let novset: [String] = native_list_empty()
    let t: String = sem_realize(sem_frame("greet", "Will", "", ""))
    let codes: [String] = text_phonemes(lmap, t)
    println("[render] \"" + t + "\" :: " + list_join(codes, " "))
    let samples: [Int] = synth_codes_accent(codes, vw, pmap, noacc, novset, vmap, pros)
    let ok: Bool = write_wav(samples, 16000, outdir + "will-reply3.wav")
    println("[done] will-reply3.wav=" + bool_to_str(ok))
}
