code wiki / (root) / nx_voice_clone_synth_gate.nx

nx_voice_clone_synth_gate.nx source

↩ module page · 155 lines · 5943 B

1// nx_voice_clone_synth_gate.nx -- REFEREE for VOICE-CLONE-001 rung 2 (create). 2// 3// Builds two reference speakers with the source-filter model (same generator as 4// the rung-1 gate): A (/a/-tract, F0 120) and B (/i/-tract, F0 182). Captures 5// vpA, vpB. Then CREATES new audio in A's voice at a BRAND-NEW pitch (140 Hz, 6// which is neither A's 120 nor B's 182) via nx_voice_clone_synth(vpA, 140). 7// 8// PASS conditions (all measured + printed): 9// 1. the created clone re-prints CLOSE to A and FAR from B 10// dist(clone,A) * 4 < dist(clone,B) -> it IS A, not B 11// 2. the created clone's pitch is the REQUESTED 140 Hz (+-12) 12// -> A's identity, retargeted to any pitch (the singing tie-in) 13// 3. TAMPER (neg-control): synthesizing from a SCRAMBLED voiceprint produces 14// audio FAR from A -> the created identity is DETERMINED by the print, 15// not an artifact of the synthesizer. 16// dist(cloneScram,A) > 4 * dist(clone,A) 17// 18// Evidence -> stdout + knowledge/status/voice_clone_synth_gate.log. 19// Exit 0 GREEN / 1 RED. Sovereign x86_64 throughout. 20// license_tier: ORIGINAL 21import "nx_syscalls_x86_64.nx" 22import "nx_voiceprint.nx" 23import "nx_voice_clone_synth.nx" 24 25const NSAMP: i64 = 2048 26const A2Q: i64 = 0 - 26542 // -r^2 in Q15, r=0.9 27 28func gp(logfd: i64, s: *u8) -> i64 { 29 var n: i64 = 0 30 while s[n] != (0 as u8) { n = n + 1 } 31 sys_write(1, s, n) 32 if logfd > 0 { sys_write(logfd, s, n) } 33 return 0 34} 35func gn(logfd: i64, v: i64) -> i64 { 36 let bb: *u8 = sys_mmap(28) 37 var m: i64 = v 38 if m < 0 { sys_write(1, "-\x00" as *u8, 1); if logfd > 0 { sys_write(logfd, "-\x00" as *u8, 1) } m = 0 - m } 39 let t: *u8 = sys_mmap(28) 40 var k: i64 = 0 41 if m == 0 { t[0] = 48 as u8; k = 1 } 42 while m > 0 { t[k] = (48 + (m % 10)) as u8; m = m / 10; k = k + 1 } 43 var i: i64 = 0 44 while i < k { bb[i] = t[k - 1 - i]; i = i + 1 } 45 sys_write(1, bb, k) 46 if logfd > 0 { sys_write(logfd, bb, k) } 47 return 0 48} 49 50// --- source-filter test generator (same as rung-1 gate fixtures) --- 51func reson(buf: *i64, n: i64, a1q: i64) -> i64 { 52 var y1: i64 = 0 53 var y2: i64 = 0 54 var i: i64 = 0 55 while i < n { 56 let x: i64 = buf[i] 57 let y: i64 = x + ((a1q * y1 + A2Q * y2) >> 15) 58 buf[i] = y 59 y2 = y1 60 y1 = y 61 i = i + 1 62 } 63 return 0 64} 65func genvoice(out_pcm: *u8, nsamp: i64, period: i64, 66 a1_0: i64, a1_1: i64, a1_2: i64) -> i64 { 67 let buf: *i64 = sys_mmap(nsamp * 8) as *i64 68 var i: i64 = 0 69 while i < nsamp { buf[i] = 1000; i = i + period } 70 reson(buf, nsamp, a1_0) 71 reson(buf, nsamp, a1_1) 72 reson(buf, nsamp, a1_2) 73 var mx: i64 = 1 74 i = 0 75 while i < nsamp { 76 var v: i64 = buf[i] 77 if v < 0 { v = 0 - v } 78 if v > mx { mx = v } 79 i = i + 1 80 } 81 i = 0 82 while i < nsamp { 83 var s: i64 = buf[i] * 20000 / mx 84 if s > 32000 { s = 32000 } 85 if s < -32000 { s = -32000 } 86 var x: i64 = s 87 if x < 0 { x = x + 0x10000 } 88 out_pcm[i * 2] = (x & 0xff) as u8 89 out_pcm[i * 2 + 1] = ((x >> 8) & 0xff) as u8 90 i = i + 1 91 } 92 return 0 93} 94 95func main() -> i64 { 96 let logfd: i64 = sys_openat_append("knowledge/status/voice_clone_synth_gate.log\x00" as *u8, 0x1a4) 97 gp(logfd, "CLONE-SYNTH-GATE VOICE-CLONE-001 rung2 nsamp=\x00" as *u8); gn(logfd, NSAMP); gp(logfd, "\n\x00" as *u8) 98 99 // reference speakers 100 let pcmA: *u8 = sys_mmap(NSAMP * 2) 101 let pcmB: *u8 = sys_mmap(NSAMP * 2) 102 genvoice(pcmA, NSAMP, 67, 50289, 33962, 0 - 26605) 103 genvoice(pcmB, NSAMP, 44, 57353, 0 - 13770, 0 - 41709) 104 105 let vpA: *i64 = sys_mmap(VP_WORDS * 8) as *i64 106 let vpB: *i64 = sys_mmap(VP_WORDS * 8) as *i64 107 nx_voiceprint_extract(pcmA, NSAMP, vpA) 108 nx_voiceprint_extract(pcmB, NSAMP, vpB) 109 gp(logfd, " vpA f0=\x00" as *u8); gn(logfd, vpA[1]); gp(logfd, "Hz vpB f0=\x00" as *u8); gn(logfd, vpB[1]); gp(logfd, "Hz\n\x00" as *u8) 110 111 // CREATE: A's voice at a brand-new pitch 140 Hz 112 let clone: *u8 = sys_mmap(NSAMP * 2) 113 nx_voice_clone_synth(vpA, 140, 8000, NSAMP, clone) 114 let vpC: *i64 = sys_mmap(VP_WORDS * 8) as *i64 115 nx_voiceprint_extract(clone, NSAMP, vpC) 116 117 // TAMPER: clone from a SCRAMBLED voiceprint (PARCOR reversed) 118 let vpScram: *i64 = sys_mmap(VP_WORDS * 8) as *i64 119 vpScram[0] = vpA[0] 120 vpScram[1] = vpA[1] 121 var s: i64 = 0 122 while s < VP_ORDER { vpScram[2 + s] = vpA[2 + (VP_ORDER - 1 - s)]; s = s + 1 } 123 let cloneS: *u8 = sys_mmap(NSAMP * 2) 124 nx_voice_clone_synth(vpScram, 140, 8000, NSAMP, cloneS) 125 let vpCS: *i64 = sys_mmap(VP_WORDS * 8) as *i64 126 nx_voiceprint_extract(cloneS, NSAMP, vpCS) 127 128 let d_cA: i64 = nx_voiceprint_distance(vpC, vpA) 129 let d_cB: i64 = nx_voiceprint_distance(vpC, vpB) 130 let d_csA: i64 = nx_voiceprint_distance(vpCS, vpA) 131 gp(logfd, " clone f0=\x00" as *u8); gn(logfd, vpC[1]); gp(logfd, "Hz\n\x00" as *u8) 132 gp(logfd, " dist(clone,A)=\x00" as *u8); gn(logfd, d_cA) 133 gp(logfd, " dist(clone,B)=\x00" as *u8); gn(logfd, d_cB) 134 gp(logfd, " dist(cloneScram,A)=\x00" as *u8); gn(logfd, d_csA) 135 gp(logfd, "\n\x00" as *u8) 136 137 var ok: i64 = 1 138 if vpC[0] < 5 { ok = 0 } // clone produced voiced speech 139 if d_cB <= 4 * (d_cA + 1) { ok = 0 } // clone is A, not B 140 if d_cB < 1000 { ok = 0 } // real separation 141 if vpC[1] < 128 { ok = 0 } // requested pitch 140 (+-12) 142 if vpC[1] > 152 { ok = 0 } 143 if d_csA <= 4 * (d_cA + 1) { ok = 0 } // tamper: scrambled print != A 144 145 if ok == 1 { 146 gp(logfd, "CLONE-SYNTH-GATE result=ALL-PASS verdict=GREEN\n\x00" as *u8) 147 if logfd > 0 { sys_close(logfd) } 148 sys_exit(0) 149 return 0 150 } 151 gp(logfd, "CLONE-SYNTH-GATE result=FAIL verdict=RED\n\x00" as *u8) 152 if logfd > 0 { sys_close(logfd) } 153 sys_exit(1) 154 return 1 155}