code wiki / (root) / nx_voiceprint_gate.nx

nx_voiceprint_gate.nx source

↩ module page · 179 lines · 6611 B

1// nx_voiceprint_gate.nx -- REFEREE for VOICE-CLONE-001 rung 1 (voiceprint). 2// 3// Synthesizes three speakers with the SOURCE-FILTER model (glottal impulse 4// train -> 3 cascaded 2-pole formant resonators, r=0.9, fs=8 kHz): 5// A : vocal tract /a/-like, F0 120 Hz (period 67) 6// A2 : SAME tract as A, F0 125 Hz (period 64) <- same speaker, different pitch 7// B : vocal tract /i/-like, F0 182 Hz (period 44) <- different speaker 8// 9// PITCH-INVARIANCE + DISCRIMINATION (the identity floor under cloning): 10// d_self = dist(A, A2) must be SMALL (same tract, different pitch) 11// d_cross = dist(A, B) must be LARGE (different tract) 12// assert d_cross > 4 * d_self -> the print captures WHO, not the momentary pitch. 13// F0: A,A2 print ~120-130 Hz; B prints ~165-195 Hz (the source path works too). 14// TAMPER (neg-control): a SCRAMBLED voiceprint (PARCOR order reversed) must be 15// FAR from the original (> 4*d_self) -- a corrupted vocal tract is NOT the 16// speaker, so the metric must reject it (no false "still you"). 17// 18// Every measured value PRINTED. Evidence -> stdout + knowledge/status/ 19// voiceprint_gate.log. Exit 0 GREEN / 1 RED. Sovereign x86_64 throughout. 20// license_tier: ORIGINAL 21import "nx_syscalls_x86_64.nx" 22import "nx_voiceprint.nx" 23 24const NSAMP: i64 = 2048 25const A2Q: i64 = 0 - 26542 // -r^2 in Q15, r=0.9 26 27// dual-sink writers 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// one 2-pole resonator in place over an i64 buffer: 51// y[n] = x[n] + (a1q*y[n-1] + a2q*y[n-2]) >> 15 52func reson(buf: *i64, n: i64, a1q: i64) -> i64 { 53 var y1: i64 = 0 54 var y2: i64 = 0 55 var i: i64 = 0 56 while i < n { 57 let x: i64 = buf[i] 58 let y: i64 = x + ((a1q * y1 + A2Q * y2) >> 15) 59 buf[i] = y 60 y2 = y1 61 y1 = y 62 i = i + 1 63 } 64 return 0 65} 66 67// synthesize one speaker: glottal impulse train at `period` through 3 formant 68// resonators (a1_0,a1_1,a1_2), normalized to +-20000, stored i16-LE. 69func genvoice(out_pcm: *u8, nsamp: i64, period: i64, 70 a1_0: i64, a1_1: i64, a1_2: i64) -> i64 { 71 let buf: *i64 = sys_mmap(nsamp * 8) as *i64 // mmap is zero-filled 72 var i: i64 = 0 73 while i < nsamp { buf[i] = 1000; i = i + period } 74 reson(buf, nsamp, a1_0) 75 reson(buf, nsamp, a1_1) 76 reson(buf, nsamp, a1_2) 77 var mx: i64 = 1 78 i = 0 79 while i < nsamp { 80 var v: i64 = buf[i] 81 if v < 0 { v = 0 - v } 82 if v > mx { mx = v } 83 i = i + 1 84 } 85 i = 0 86 while i < nsamp { 87 var s: i64 = buf[i] * 20000 / mx 88 if s > 32000 { s = 32000 } 89 if s < -32000 { s = -32000 } 90 var x: i64 = s 91 if x < 0 { x = x + 0x10000 } 92 out_pcm[i * 2] = (x & 0xff) as u8 93 out_pcm[i * 2 + 1] = ((x >> 8) & 0xff) as u8 94 i = i + 1 95 } 96 return 0 97} 98 99func print_vp(logfd: i64, label: *u8, vp: *i64) -> i64 { 100 gp(logfd, label) 101 gp(logfd, " nframes=\x00" as *u8); gn(logfd, vp[0]) 102 gp(logfd, " f0=\x00" as *u8); gn(logfd, vp[1]) 103 gp(logfd, "Hz k1=\x00" as *u8); gn(logfd, vp[2]) 104 gp(logfd, " k2=\x00" as *u8); gn(logfd, vp[3]) 105 gp(logfd, "\n\x00" as *u8) 106 return 0 107} 108 109func main() -> i64 { 110 let logfd: i64 = sys_openat_append("knowledge/status/voiceprint_gate.log\x00" as *u8, 0x1a4) 111 gp(logfd, "VOICEPRINT-GATE VOICE-CLONE-001 rung1 nsamp=\x00" as *u8); gn(logfd, NSAMP); gp(logfd, "\n\x00" as *u8) 112 113 // synth buffers 114 let pcmA: *u8 = sys_mmap(NSAMP * 2) 115 let pcmA2: *u8 = sys_mmap(NSAMP * 2) 116 let pcmB: *u8 = sys_mmap(NSAMP * 2) 117 // A and A2 share the vocal tract (same formant a1's); B differs. 118 genvoice(pcmA, NSAMP, 67, 50289, 33962, 0 - 26605) 119 genvoice(pcmA2, NSAMP, 64, 50289, 33962, 0 - 26605) 120 genvoice(pcmB, NSAMP, 44, 57353, 0 - 13770, 0 - 41709) 121 122 let vpA: *i64 = sys_mmap(VP_WORDS * 8) as *i64 123 let vpA2: *i64 = sys_mmap(VP_WORDS * 8) as *i64 124 let vpB: *i64 = sys_mmap(VP_WORDS * 8) as *i64 125 nx_voiceprint_extract(pcmA, NSAMP, vpA) 126 nx_voiceprint_extract(pcmA2, NSAMP, vpA2) 127 nx_voiceprint_extract(pcmB, NSAMP, vpB) 128 print_vp(logfd, " A \x00" as *u8, vpA) 129 print_vp(logfd, " A2\x00" as *u8, vpA2) 130 print_vp(logfd, " B \x00" as *u8, vpB) 131 132 let d_self: i64 = nx_voiceprint_distance(vpA, vpA2) 133 let d_cross1: i64 = nx_voiceprint_distance(vpA, vpB) 134 let d_cross2: i64 = nx_voiceprint_distance(vpA2, vpB) 135 gp(logfd, " dist(A,A2)=\x00" as *u8); gn(logfd, d_self) 136 gp(logfd, " dist(A,B)=\x00" as *u8); gn(logfd, d_cross1) 137 gp(logfd, " dist(A2,B)=\x00" as *u8); gn(logfd, d_cross2) 138 gp(logfd, "\n\x00" as *u8) 139 140 // TAMPER: scramble A's PARCOR (reverse order) -> must be far from A 141 let vpScram: *i64 = sys_mmap(VP_WORDS * 8) as *i64 142 vpScram[0] = vpA[0] 143 vpScram[1] = vpA[1] 144 var i: i64 = 0 145 while i < VP_ORDER { 146 vpScram[2 + i] = vpA[2 + (VP_ORDER - 1 - i)] 147 i = i + 1 148 } 149 let d_scram: i64 = nx_voiceprint_distance(vpA, vpScram) 150 gp(logfd, " dist(A,scram)=\x00" as *u8); gn(logfd, d_scram); gp(logfd, "\n\x00" as *u8) 151 152 // ---- verdict ---- 153 var ok: i64 = 1 154 if vpA[0] < 5 { ok = 0 } 155 if vpA2[0] < 5 { ok = 0 } 156 if vpB[0] < 5 { ok = 0 } 157 // pitch-invariance + discrimination 158 if d_cross1 <= 4 * (d_self + 1) { ok = 0 } 159 if d_cross2 <= 4 * (d_self + 1) { ok = 0 } 160 if d_cross1 < 1000 { ok = 0 } // real separation, not degenerate-zero 161 // F0 paths 162 if vpA[1] < 105 { ok = 0 } 163 if vpA[1] > 130 { ok = 0 } 164 if vpB[1] < 160 { ok = 0 } 165 if vpB[1] > 200 { ok = 0 } 166 // tamper rejected 167 if d_scram <= 4 * (d_self + 1) { ok = 0 } 168 169 if ok == 1 { 170 gp(logfd, "VOICEPRINT-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, "VOICEPRINT-GATE result=FAIL verdict=RED\n\x00" as *u8) 176 if logfd > 0 { sys_close(logfd) } 177 sys_exit(1) 178 return 1 179}