code wiki / (root) / nx_phoneme_synth_gate.nx

nx_phoneme_synth_gate.nx source

↩ module page · 179 lines · 7906 B

1// nx_phoneme_synth_gate.nx -- REFEREE for VOICE-CLONE-001 rung 5a (articulation). 2// 3// Renders a 3-phoneme "word" /a/ -> /i/ -> /u/ and proves the engine ARTICULATES 4// (says a sequence) rather than sustaining one sound: 5// ARTICULATION : the 3 segments must be SPECTRALLY DISTINCT (large pairwise 6// voiceprint distance) -> the voice is moving through sounds. 7// NEG-CONTROL : a constant "word" /a/ /a/ /a/ must have the 3 segments NEARLY 8// IDENTICAL (small distance) -> the variation in the real word 9// comes from the PHONEMES, not from synthesis noise. 10// DETERMINISM : the same sequence renders byte-equal (segment dist == 0). 11// VOICED/UNVOICED : a mixed word /a/(voiced) /s/(unvoiced) /u/(voiced) must 12// read voiced, UNVOICED, voiced (vp frame-count, via nx_pitch). 13// SPEAKER : a second speaker's phoneme inventory (child formants) reading 14// the SAME word differs from speaker 1 AND still articulates. 15// 16// Every value PRINTED. Evidence -> stdout + knowledge/status/phoneme_synth_gate.log. 17// Exit 0 GREEN / 1 RED. Sovereign x86_64 throughout. 18// license_tier: ORIGINAL 19import "nx_syscalls_x86_64.nx" 20import "nx_phoneme_synth.nx" 21import "nx_voiceprint.nx" 22 23const DUR: i64 = 1200 24const CAP: i64 = 4096 25 26func gp(logfd: i64, s: *u8) -> i64 { 27 var n: i64 = 0 28 while s[n] != (0 as u8) { n = n + 1 } 29 sys_write(1, s, n) 30 if logfd > 0 { sys_write(logfd, s, n) } 31 return 0 32} 33func gn(logfd: i64, v: i64) -> i64 { 34 let bb: *u8 = sys_mmap(28) 35 var m: i64 = v 36 if m < 0 { sys_write(1, "-\x00" as *u8, 1); if logfd > 0 { sys_write(logfd, "-\x00" as *u8, 1) } m = 0 - m } 37 let t: *u8 = sys_mmap(28) 38 var k: i64 = 0 39 if m == 0 { t[0] = 48 as u8; k = 1 } 40 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 41 var i: i64 = 0 42 while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } 43 sys_write(1, bb, k) 44 if logfd > 0 { sys_write(logfd, bb, k) } 45 return 0 46} 47 48// fill one phoneme record 49func setph(seq: *i64, idx: i64, a1: i64, a2: i64, a3: i64, v: i64, dur: i64) -> i64 { 50 seq[idx * PH_REC + 0] = a1 51 seq[idx * PH_REC + 1] = a2 52 seq[idx * PH_REC + 2] = a3 53 seq[idx * PH_REC + 3] = v 54 seq[idx * PH_REC + 4] = dur 55 return 0 56} 57 58// voiceprint of a segment: skip the 400-sample warm-up/transition region (a 59// high-Q cascade rings for ~3 pitch periods at cold start), then average over a 60// 760-sample window (4 LPC frames) to damp single-window variance. 61func seg_vp(pcm: *u8, segstart: i64, vp: *i64) -> i64 { 62 let p: *u8 = (pcm as i64 + (segstart + 400) * 2) as *u8 63 return nx_voiceprint_extract(p, 760, vp) 64} 65 66func main() -> i64 { 67 let logfd: i64 = sys_openat_append("knowledge/status/phoneme_synth_gate.log\x00" as *u8, 0x1a4) 68 gp(logfd, "PHONEME-SYNTH-GATE VOICE-CLONE-001 rung5a dur=\x00" as *u8); gn(logfd, DUR); gp(logfd, "\n\x00" as *u8) 69 70 let seq: *i64 = sys_mmap(PH_REC * 8 * 8) as *i64 71 72 // ---- WORD /a/ /i/ /u/ (adult) ---- 73 setph(seq, 0, 52310, 40810, 0 - 21040, 1, DUR) 74 setph(seq, 1, 60863, 0 - 14059, 0 - 44298, 1, DUR) 75 setph(seq, 2, 60540, 48278, 0 - 21040, 1, DUR) 76 let pcmW: *u8 = sys_mmap(CAP * 2) 77 nx_phoneme_synth(seq, 3, 120, 8000, pcmW, CAP) 78 let w0: *i64 = sys_mmap(VP_WORDS * 8) as *i64 79 let w1: *i64 = sys_mmap(VP_WORDS * 8) as *i64 80 let w2: *i64 = sys_mmap(VP_WORDS * 8) as *i64 81 seg_vp(pcmW, 0, w0) 82 seg_vp(pcmW, 1200, w1) 83 seg_vp(pcmW, 2400, w2) 84 let d01: i64 = nx_voiceprint_distance(w0, w1) 85 let d12: i64 = nx_voiceprint_distance(w1, w2) 86 let d02: i64 = nx_voiceprint_distance(w0, w2) 87 gp(logfd, " WORD seg dist 0-1=\x00" as *u8); gn(logfd, d01) 88 gp(logfd, " 1-2=\x00" as *u8); gn(logfd, d12) 89 gp(logfd, " 0-2=\x00" as *u8); gn(logfd, d02); gp(logfd, "\n\x00" as *u8) 90 91 // ---- CONSTANT /a/ /a/ /a/ (neg-control) ---- 92 setph(seq, 0, 52310, 40810, 0 - 21040, 1, DUR) 93 setph(seq, 1, 52310, 40810, 0 - 21040, 1, DUR) 94 setph(seq, 2, 52310, 40810, 0 - 21040, 1, DUR) 95 let pcmC: *u8 = sys_mmap(CAP * 2) 96 nx_phoneme_synth(seq, 3, 120, 8000, pcmC, CAP) 97 let c0: *i64 = sys_mmap(VP_WORDS * 8) as *i64 98 let c1: *i64 = sys_mmap(VP_WORDS * 8) as *i64 99 let c2: *i64 = sys_mmap(VP_WORDS * 8) as *i64 100 seg_vp(pcmC, 0, c0) 101 seg_vp(pcmC, 1200, c1) 102 seg_vp(pcmC, 2400, c2) 103 let cc01: i64 = nx_voiceprint_distance(c0, c1) 104 let cc12: i64 = nx_voiceprint_distance(c1, c2) 105 gp(logfd, " CONST seg dist 0-1=\x00" as *u8); gn(logfd, cc01) 106 gp(logfd, " 1-2=\x00" as *u8); gn(logfd, cc12); gp(logfd, "\n\x00" as *u8) 107 108 // ---- DETERMINISM: re-render the WORD ---- 109 setph(seq, 0, 52310, 40810, 0 - 21040, 1, DUR) 110 setph(seq, 1, 60863, 0 - 14059, 0 - 44298, 1, DUR) 111 setph(seq, 2, 60540, 48278, 0 - 21040, 1, DUR) 112 let pcmW2: *u8 = sys_mmap(CAP * 2) 113 nx_phoneme_synth(seq, 3, 120, 8000, pcmW2, CAP) 114 let w0b: *i64 = sys_mmap(VP_WORDS * 8) as *i64 115 seg_vp(pcmW2, 0, w0b) 116 let dd0: i64 = nx_voiceprint_distance(w0, w0b) 117 gp(logfd, " DETERMINISM seg0 redo dist=\x00" as *u8); gn(logfd, dd0); gp(logfd, "\n\x00" as *u8) 118 119 // ---- VOICED/UNVOICED: /a/(v) /s/(uv) /u/(v) ---- 120 setph(seq, 0, 52310, 40810, 0 - 21040, 1, DUR) 121 setph(seq, 1, 0 - 9781, 0 - 55473, 0 - 59195, 0, DUR) 122 setph(seq, 2, 60540, 48278, 0 - 21040, 1, DUR) 123 let pcmM: *u8 = sys_mmap(CAP * 2) 124 nx_phoneme_synth(seq, 3, 120, 8000, pcmM, CAP) 125 let m0: *i64 = sys_mmap(VP_WORDS * 8) as *i64 126 let m1: *i64 = sys_mmap(VP_WORDS * 8) as *i64 127 let m2: *i64 = sys_mmap(VP_WORDS * 8) as *i64 128 seg_vp(pcmM, 0, m0) 129 seg_vp(pcmM, 1200, m1) 130 seg_vp(pcmM, 2400, m2) 131 gp(logfd, " MIXED voiced-frames seg0=\x00" as *u8); gn(logfd, m0[0]) 132 gp(logfd, " seg1=\x00" as *u8); gn(logfd, m1[0]) 133 gp(logfd, " seg2=\x00" as *u8); gn(logfd, m2[0]); gp(logfd, "\n\x00" as *u8) 134 135 // ---- SPEAKER: child inventory reading the same word ---- 136 setph(seq, 0, 49195, 34361, 0 - 36840, 1, DUR) 137 setph(seq, 1, 60423, 0 - 29629, 0 - 56725, 1, DUR) 138 setph(seq, 2, 59987, 44022, 0 - 36840, 1, DUR) 139 let pcmCh: *u8 = sys_mmap(CAP * 2) 140 nx_phoneme_synth(seq, 3, 200, 8000, pcmCh, CAP) 141 let h0: *i64 = sys_mmap(VP_WORDS * 8) as *i64 142 let h1: *i64 = sys_mmap(VP_WORDS * 8) as *i64 143 seg_vp(pcmCh, 0, h0) 144 seg_vp(pcmCh, 1200, h1) 145 let spk: i64 = nx_voiceprint_distance(w0, h0) // adult /a/ vs child /a/ 146 let chd01: i64 = nx_voiceprint_distance(h0, h1) // child word articulates 147 gp(logfd, " SPEAKER adult/a vs child/a=\x00" as *u8); gn(logfd, spk) 148 gp(logfd, " child word seg0-1=\x00" as *u8); gn(logfd, chd01); gp(logfd, "\n\x00" as *u8) 149 150 // ---- verdict ---- 151 var minword: i64 = d01 152 if d12 < minword { minword = d12 } 153 if d02 < minword { minword = d02 } 154 var maxconst: i64 = cc01 155 if cc12 > maxconst { maxconst = cc12 } 156 157 var ok: i64 = 1 158 if d01 < 1000000 { ok = 0 } // articulation: distinct phonemes 159 if d12 < 1000000 { ok = 0 } 160 if d02 < 1000000 { ok = 0 } 161 if maxconst * 4 >= minword { ok = 0 } // neg-control: constant word is flat 162 if dd0 != 0 { ok = 0 } // determinism 163 if m0[0] < 1 { ok = 0 } // voiced 164 if m1[0] != 0 { ok = 0 } // unvoiced fricative 165 if m2[0] < 1 { ok = 0 } // voiced 166 if spk < 1000000 { ok = 0 } // speaker 2 differs from speaker 1 167 if chd01 < 1000000 { ok = 0 } // speaker 2 also articulates 168 169 if ok == 1 { 170 gp(logfd, "PHONEME-SYNTH-GATE result=ALL-PASS verdict=GREEN\n\x00" as *u8) 171 if logfd > 0 { sys_close(logfd) } 172 sys_exit(0) 173 return 0 174 } 175 gp(logfd, "PHONEME-SYNTH-GATE result=FAIL verdict=RED\n\x00" as *u8) 176 if logfd > 0 { sys_close(logfd) } 177 sys_exit(1) 178 return 1 179}