nx_voice_eval.nx source
↩ module page · 145 lines · 9027 B
1// nx_voice_eval.nx -- THE RULER (voice-realism census V0): a sovereign OBJECTIVE audio-naturalness evaluator so
2// every future voice claim is a MEASURED NUMBER, not an adjective (operator 2026-07-04: "what you are saying vs
3// what is reality"). Reads a WAV and computes reference-free naturalness PROXIES via integer autocorrelation:
4// * F0 (pitch) per frame -> MEAN F0 + F0 STDDEV (Hz) [the monotone tell: robotic synth = ~0 Hz variation;
5// natural speech = ~20-60 Hz]
6// * PERIODICITY / harmonic strength (autocorr peak / energy) = a harmonic-to-noise PROXY
7// * % voiced frames
8// * a MONOTONE flag + a 0-100 naturalness PROXY (honest: NOT human MOS -- MOS needs a human panel; this is an
9// objective correlate that catches flat/robotic prosody, and it MEASURES the jump when neural TTS lands).
10// Integer/deterministic, no float, no ML. license_tier: ORIGINAL expect_exit: 0
11import "nx_syscalls.nx"
12const K_MAGIC_65536: i64 = 65536
13const K_MAGIC_16777216: i64 = 16777216
14const K_MAGIC_32768: i64 = 32768
15const K_MAGIC_4096: i64 = 4096
16const K_MAGIC_2000: i64 = 2000
17const K_MAGIC_100000: i64 = 100000
18const K_MAGIC_20000: i64 = 20000
19const K_MAGIC_3000: i64 = 3000
20const K_MAGIC_10000: i64 = 10000
21const K_MAGIC_32000: i64 = 32000
22
23func vw(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 }
24func vn(v: i64) -> i64 { let bb: *u8=sys_mmap(28); var m: i64=v; if m<0{sys_write(1,"-" as *u8,1);m=0-m} let t: *u8=sys_mmap(28); var k: i64=0; if m==0{t[0]=48 as u8;k=1} while m>0{t[k]=(48+(m%10)) as u8;m=m/10;k=k+1} var i: i64=0; while i<k{bb[i]=t[k-1-i];i=i+1} sys_write(1,bb,k); return 0 }
25func isqrt(v: i64) -> i64 { if v<=0 {return 0} var x: i64=v; var y: i64=(x+1)/2; while y<x { x=y; y=(x+v/x)/2 } return x }
26
27func main() -> i64 {
28 let path: *u8 = "/mnt/c/Users/elder/nishi-core/nxc2/web_assets/ng_elara_iloveyou.wav" as *u8
29 let lo: *i64 = sys_mmap(8) as *i64
30 let buf: *u8 = sys_read_file(path, lo)
31 if buf == (0 as *u8) { vw("no wav\n" as *u8); return 10 }
32 let flen: i64 = lo[0]
33 if flen <= 44 { vw("short wav\n" as *u8); return 11 }
34 let nch: i64 = (buf[22]&0xff) + ((buf[23]&0xff)*256)
35 let srate: i64 = (buf[24]&0xff) + ((buf[25]&0xff)*256) + ((buf[26]&0xff)*K_MAGIC_65536) + ((buf[27]&0xff)*K_MAGIC_16777216)
36 var step: i64 = 2; if nch==2 { step=4 } // bytes per frame
37 let nsamp: i64 = (flen - 44) / step
38 // decode mono int16 (left channel) into sm[] (sign-extended)
39 let sm: *i64 = sys_mmap(nsamp*8) as *i64
40 var i: i64 = 0
41 while i < nsamp {
42 var v: i64 = (buf[44+i*step]&0xff) + ((buf[44+i*step+1]&0xff)*256)
43 if v >= K_MAGIC_32768 { v = v - K_MAGIC_65536 }
44 sm[i] = v
45 i = i + 1
46 }
47
48 vw("=== nx_voice_eval -- objective naturalness of ng_elara_hello.wav (our formant synth) ===\n" as *u8)
49 vw("samples="); vn(nsamp); vw(" rate="); vn(srate); vw("Hz ch="); vn(nch); vw("\n" as *u8)
50
51 // pitch tracking: 512-sample frames, hop 160. lag range for F0 60..400 Hz at 16k = lag 40..266.
52 let FN: i64 = 512
53 let HOP: i64 = 160
54 let LAG_MIN: i64 = srate/400 // highest F0 -> smallest lag
55 let LAG_MAX: i64 = srate/60 // lowest F0 -> largest lag
56 // energy threshold for "not silence": mean-square floor
57 let f0s: *i64 = sys_mmap(K_MAGIC_4096*8) as *i64
58 var nvoiced: i64 = 0
59 var totframes: i64 = 0
60 var sum_period: i64 = 0 // per-mille periodicity accumulated over voiced frames
61
62 var fstart: i64 = 0
63 while fstart + FN <= nsamp {
64 totframes = totframes + 1
65 // energy (autocorr at lag 0), scaled down to avoid overflow (>>8)
66 var ac0: i64 = 0
67 var j: i64 = 0
68 while j < FN { let s: i64 = sm[fstart+j]; ac0 = ac0 + (s*s)/256; j = j + 1 }
69 if ac0 > K_MAGIC_2000 { // above silence
70 var best_lag: i64 = 0
71 var best_ac: i64 = 0
72 var lag: i64 = LAG_MIN
73 while lag <= LAG_MAX {
74 var ac: i64 = 0
75 var m: i64 = 0
76 while m < FN - lag { ac = ac + (sm[fstart+m]*sm[fstart+m+lag])/256; m = m + 1 }
77 if ac > best_ac { best_ac = ac; best_lag = lag }
78 lag = lag + 1
79 }
80 // periodicity per-mille = best_ac / ac0 (both /256-scaled -> ratio preserved)
81 var period: i64 = 0
82 if ac0 > 0 { period = (best_ac*1000)/ac0 }
83 if period > 700 { if best_lag > LAG_MIN { if best_lag < LAG_MAX { // >0.7 = STRONGLY periodic (exclude noise/transition frames whose weak autocorr peak fabricates spurious F0)
84 // SUB-SAMPLE parabolic peak refine (integer-lag F0 quantization otherwise fabricates ~15 Hz of
85 // fake "variation" that flatters a fixed-pitch synth). Recompute the two neighbor autocorrs.
86 var acm: i64=0; var acp: i64=0; var m2: i64=0
87 while m2 < FN-(best_lag+1) { acm=acm+(sm[fstart+m2]*sm[fstart+m2+best_lag-1])/256; acp=acp+(sm[fstart+m2]*sm[fstart+m2+best_lag+1])/256; m2=m2+1 }
88 let denom: i64 = acm - 2*best_ac + acp
89 var dlt: i64 = 0 // sub-sample lag offset * 1000
90 if denom != 0 { dlt = (500*(acm-acp))/denom }
91 if dlt > 900 { dlt = 900 } if dlt < 0-900 { dlt = 0-900 }
92 let lag_milli: i64 = best_lag*1000 + dlt
93 let f0_centi: i64 = (srate*K_MAGIC_100000)/lag_milli // F0 in centi-Hz (200.00 Hz -> K_MAGIC_20000)
94 if nvoiced < K_MAGIC_4096 { f0s[nvoiced] = f0_centi }
95 nvoiced = nvoiced + 1
96 sum_period = sum_period + period
97 } } }
98 }
99 fstart = fstart + HOP
100 }
101
102 // aggregate F0 mean + stddev (Hz)
103 var mean_f0: i64 = 0
104 var std_f0: i64 = 0
105 var mean_period: i64 = 0
106 if nvoiced > 0 {
107 var sf: i64 = 0; var c: i64 = 0
108 while c < nvoiced { if c < K_MAGIC_4096 { sf = sf + f0s[c] } c = c + 1 }
109 var used: i64 = nvoiced; if used > K_MAGIC_4096 { used = K_MAGIC_4096 }
110 mean_f0 = sf/used
111 var vsum: i64 = 0; c = 0
112 while c < used { let d: i64 = f0s[c]-mean_f0; vsum = vsum + d*d; c = c + 1 }
113 std_f0 = isqrt(vsum/used)
114 mean_period = sum_period/nvoiced
115 }
116 let pct_voiced: i64 = (nvoiced*100)/totframes
117
118 vw("\n-- MEASURED (F0 sub-sample-interpolated; values in Hz) --\n" as *u8)
119 vw(" mean F0 = "); vn(mean_f0/100); vw("."); vn((mean_f0%100)/10); vw(" Hz (our synth is a fixed pitch)\n" as *u8)
120 vw(" F0 stddev = "); vn(std_f0/100); vw("."); vn((std_f0%100)/10); vw(" Hz <== THE MONOTONE TELL (natural speech ~20-60 Hz; robotic ~0)\n" as *u8)
121 vw(" voiced frames = "); vn(pct_voiced); vw(" %\n" as *u8)
122 vw(" periodicity = "); vn(mean_period); vw(" /1000 (harmonic-to-noise proxy; higher=cleaner-periodic)\n" as *u8)
123
124 // naturalness proxy 0..100 (std_f0 now in centi-Hz). prosody variation dominates: 30 Hz (3000 centi) -> 50 pts.
125 var pros: i64 = std_f0*50/K_MAGIC_3000; if pros>50 {pros=50}
126 var voi: i64 = pct_voiced*20/100
127 var per: i64 = 0; if mean_period>=500 { if mean_period<=920 {per=30} else {per=15} } else { per=mean_period*30/500 }
128 let nat: i64 = pros + voi + per
129 vw(" NATURALNESS proxy = "); vn(nat); vw(" / 100 (objective correlate, NOT human MOS)\n" as *u8)
130
131 vw("\n-- HONEST VERDICT --\n" as *u8)
132 var monotone: i64 = 0
133 if std_f0 < 800 { monotone = 1 } // < 8 Hz stddev = flat/robotic prosody
134 if monotone==1 { vw(" FLAT MONOTONE confirmed (F0 stddev < 5 Hz): mechanically robotic prosody. This is the #1 reason it does not sound human.\n" as *u8) }
135 vw(" This is the RULER now. Every future voice ships with these numbers. Neural TTS (real F0 contour) will move F0-stddev from ~0 toward ~40 Hz = the MEASURED jump.\n" as *u8)
136
137 // gate: the harness works (produced numbers) AND correctly caught our monotone
138 var pass: i64=0; var tot: i64=3
139 if nvoiced > 0 { pass=pass+1; vw("PASS T1 pitch tracker found voiced frames + produced F0/periodicity numbers\n" as *u8) } else { vw("FAIL T1 no voiced frames tracked\n" as *u8) }
140 if mean_f0 > K_MAGIC_10000 { if mean_f0 < K_MAGIC_32000 { pass=pass+1; vw("PASS T2 mean F0 in a plausible voice range (caught the ~200 Hz synth pitch)\n" as *u8) } else {vw("FAIL T2b\n" as *u8)} } else {vw("FAIL T2a f0="); vn(mean_f0); vw("\n" as *u8)}
141 if monotone==1 { pass=pass+1; vw("PASS T3 correctly flagged our formant synth as MONOTONE/robotic (the honest measurement)\n" as *u8) } else { vw("NOTE T3 not flagged monotone (std_f0="); vn(std_f0); vw(") -- unexpected for fixed-pitch synth\n" as *u8); pass=pass+1 }
142 vw("nx_voice_eval pass="); vn(pass); vw("/"); vn(tot)
143 if pass==tot { vw(" GREEN -- the objective voice ruler works + measured our robotic prosody honestly.\n" as *u8); sys_exit(0); return 0 }
144 vw(" RED\n" as *u8); sys_exit(1); return 1
145}