// speech-organ-demo.el - PROOF: the render now reads its phoneme + accent
// GEOMETRY from the ingest ORGAN's saved engram files (engram_load +
// engram_scan_nodes_json + cache), not a same-run hand-load. The British accent
// is still a composed transform-geometry (voice (+) accent, separable). Numbers
// come from the organ manifold; the .psv supplies only categorical vowel-class.

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

    // engram-independent caches from source (survive engram_load replacement)
    let vset: [String] = organ_vset("elp/data/phonetics.psv")
    let lmap: [String] = organ_lex("elp/data/lexicon.psv")
    // ORGAN read: phonetics FIRST (cache), THEN accent (engram_load replaces store)
    let pmap: [String] = organ_pmap("elp/data/phonetics-formants.engram.json")
    let amap: [String] = organ_amap("elp/data/british-accent.engram.json")
    println("[organ] phon_syms=" + int_to_str(native_list_len(pmap) / 2) + " accent_syms=" + int_to_str(native_list_len(amap) / 2) + " vowels=" + int_to_str(native_list_len(vset)) + " words=" + int_to_str(native_list_len(lmap) / 2))

    // prove the numbers came from the organ node content
    let g: [Int] = phon_geo(pmap, "AA")
    println("[organ-read] phoneme AA f1=" + int_to_str(native_list_get(g, 0)) + " f2=" + int_to_str(native_list_get(g, 1)) + " f3=" + int_to_str(native_list_get(g, 2)) + " (P&B1952 MEASURED)")
    let ov: [Int] = accent_formants(amap, "AA")
    if native_list_len(ov) >= 3 {
        println("[organ-read] accent AA(LOT) f1=" + int_to_str(native_list_get(ov, 0)) + " f2=" + int_to_str(native_list_get(ov, 1)) + " (DERIVED RP, PROVISIONAL)")
    }
    println("[organ-read] non_rhotic=" + int_to_str(is_nonrhotic(amap)))

    let neuron: [String] = voice_neuron()
    let noacc: [String] = native_list_empty()

    // Sentence 1: "I am Neuron." from meaning; GA vs RP = separable toggle
    let t1: String = sem_realize(sem_frame("describe", "I", "Neuron", ""))
    let c1: [String] = text_phonemes(lmap, t1)
    println("[s1] " + t1 + " :: " + list_join(c1, " "))
    let ga: [Int] = synth_codes_accent(c1, neuron, pmap, noacc, vset)
    let okga: Bool = write_wav(ga, 16000, outdir + "ga-neuron-organ.wav")
    let br1: [Int] = synth_codes_accent(c1, neuron, pmap, amap, vset)
    let okb1: Bool = write_wav(br1, 16000, outdir + "british-neuron-organ.wav")

    // Sentence 2: non-rhoticity showcase
    let t2: String = sem_realize(sem_frame("describe", "I", "here", ""))
    let c2: [String] = text_phonemes(lmap, t2)
    let c2rp: [String] = apply_rhoticity(c2, vset)
    println("[s2] " + t2 + " :: GA=" + list_join(c2, " ") + "  RP=" + list_join(c2rp, " "))
    let br2: [Int] = synth_codes_accent(c2, neuron, pmap, amap, vset)
    let okb2: Bool = write_wav(br2, 16000, outdir + "british-2-organ.wav")

    println("[done] ga-organ=" + bool_to_str(okga) + " british-organ=" + bool_to_str(okb1) + " british-2-organ=" + bool_to_str(okb2))
}
