// speech-demo.el - PROOF: Neuron speaks from MEANING, rendered through INGESTED
// phonetic geometry, own-core, plus voice-by-IMITATION. Built by concatenating
// the elp realizer + voice-profile + speech-ingest + speech, then this main.
//
//   LEARN   : ingest acoustic-phonetics + lexicon SOURCES -> phoneme manifold in
//             the engram (source -> manifold -> merge).
//   MEANING : sem_frame("describe","I","Neuron","") -> sem_realize -> "I am Neuron."
//   PHONES  : words -> phoneme codes, READ from the ingested lexicon geometry.
//   RENDER  : superpose formant resonances (read from engram) over a glottal
//             source -> own-core PCM/WAV, in Neuron's own voice.
//   IMITATE : HEAR a short sample of a different voice -> extract its signature
//             by ear (autocorrelation pitch + integer-DFT formant) -> render new
//             speech in that voice. An impression, not a corpus.

fn speak_report(tag: String, codes: [String], voice: [String], pmap: [String], path: String) -> [Int] {
    let s: [Int] = synth_codes(codes, voice, pmap)
    let ok: Bool = write_wav(s, 16000, path)
    println(tag + " samples=" + int_to_str(native_list_len(s)) + " ok=" + bool_to_str(ok) + " -> " + path)
    return s
}

fn main() {
    let outdir: String = "/private/tmp/claude-501/-Users-will/6531446d-bc27-4095-930b-e04777c3db4f/scratchpad/"

    // -- LEARN: ingest the speech primitives as geometry --------------------
    let pmap: [String] = ingest_phonetics("elp/data/phonetics.psv")
    let lmap: [String] = ingest_lexicon("elp/data/lexicon.psv")
    let saved: Bool = engram_save(outdir + "phoneme-manifold.json")
    println("[learn] phonemes=" + int_to_str(native_list_len(pmap) / 2) + " words=" + int_to_str(native_list_len(lmap) / 2) + " manifold_saved=" + bool_to_str(saved))

    // sanity: show that AA's formants came from ingested geometry, not code
    let aa: [Int] = phon_geo(pmap, "AA")
    let aaF1: Int = native_list_get(aa, 0)
    let aaF2: Int = native_list_get(aa, 1)
    println("[read-geometry] AA F1=" + int_to_str(aaF1) + " F2=" + int_to_str(aaF2) + " (parsed from engram node)")

    // -- MEANING -> WORDS via the realizer's language faculty ----------------
    let frame: [String] = sem_frame("describe", "I", "Neuron", "")
    let text: String = sem_realize(frame)
    println("[meaning->text] " + text)

    // -- WORDS -> PHONEMES (read from ingested lexicon geometry) --------------
    let codes: [String] = text_phonemes(lmap, text)
    println("[phonemes] " + list_join(codes, " "))

    // -- RENDER in Neuron's own voice ----------------------------------------
    let neuron: [String] = voice_neuron()
    let s1: [Int] = speak_report("[speak neuron]", codes, neuron, pmap, outdir + "neuron.wav")

    // -- IMITATION: hear a distinct voice, recover its signature, re-render ---
    let vA: [String] = voice_target_a()
    let hcodes: [String] = native_list_empty()
    hcodes = native_list_append(hcodes, "SIL")
    let z: Int = 0
    while z < 6 {
        hcodes = native_list_append(hcodes, "AA")
        z = z + 1
    }
    hcodes = native_list_append(hcodes, "SIL")
    let heard: [Int] = synth_codes(hcodes, vA, pmap)
    let okh: Bool = write_wav(heard, 16000, outdir + "heard.wav")

    let vB: [String] = voice_analyze(heard, 16000)
    println("[imitate] heard ACTUAL   f0=" + voice_get(vA, "f0") + " kf=" + voice_get(vA, "kf"))
    println("[imitate] heard RECOVERED f0=" + voice_get(vB, "f0") + " kf=" + voice_get(vB, "kf") + " (extracted by ear from PCM)")
    let s2: [Int] = speak_report("[speak imitation]", codes, vB, pmap, outdir + "imitation.wav")

    println("[done] rendered from meaning + ingested geometry; imitation from a heard sample.")
}
