@svrnsec/pulse 0.3.1 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/svrnsec-pulse.js +7 -0
- package/index.d.ts +130 -0
- package/package.json +70 -25
- package/src/analysis/audio.js +213 -0
- package/src/analysis/coherence.js +502 -0
- package/src/analysis/heuristic.js +428 -0
- package/src/analysis/jitter.js +446 -0
- package/src/analysis/llm.js +472 -0
- package/src/analysis/populationEntropy.js +403 -0
- package/src/analysis/provider.js +248 -0
- package/src/analysis/trustScore.js +356 -0
- package/src/cli/args.js +36 -0
- package/src/cli/commands/scan.js +192 -0
- package/src/cli/runner.js +157 -0
- package/src/collector/adaptive.js +200 -0
- package/src/collector/bio.js +287 -0
- package/src/collector/canvas.js +239 -0
- package/src/collector/dram.js +203 -0
- package/src/collector/enf.js +311 -0
- package/src/collector/entropy.js +195 -0
- package/src/collector/gpu.js +245 -0
- package/src/collector/idleAttestation.js +480 -0
- package/src/collector/sabTimer.js +191 -0
- package/src/fingerprint.js +475 -0
- package/src/index.js +342 -0
- package/src/integrations/react-native.js +459 -0
- package/src/proof/challenge.js +249 -0
- package/src/proof/engagementToken.js +394 -0
- package/src/terminal.js +263 -0
- package/src/update-notifier.js +264 -0
package/index.d.ts
CHANGED
|
@@ -714,3 +714,133 @@ export interface ExtendedPulseCommitment extends PulseCommitment {
|
|
|
714
714
|
llm: LlmResult | null;
|
|
715
715
|
};
|
|
716
716
|
}
|
|
717
|
+
|
|
718
|
+
// =============================================================================
|
|
719
|
+
// TrustScore
|
|
720
|
+
// =============================================================================
|
|
721
|
+
|
|
722
|
+
export interface TrustScoreBreakdown {
|
|
723
|
+
pts: number;
|
|
724
|
+
max: number;
|
|
725
|
+
[key: string]: unknown;
|
|
726
|
+
}
|
|
727
|
+
|
|
728
|
+
export interface TrustScorePenalty {
|
|
729
|
+
signal: string;
|
|
730
|
+
reason: string;
|
|
731
|
+
cap: number;
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
export interface TrustScoreBonus {
|
|
735
|
+
signal: string;
|
|
736
|
+
reason: string;
|
|
737
|
+
pts: number;
|
|
738
|
+
}
|
|
739
|
+
|
|
740
|
+
export interface TrustScore {
|
|
741
|
+
score: number;
|
|
742
|
+
grade: 'A' | 'B' | 'C' | 'D' | 'F';
|
|
743
|
+
label: 'Trusted' | 'Verified' | 'Marginal' | 'Suspicious' | 'Blocked';
|
|
744
|
+
color: string;
|
|
745
|
+
hardCap: number | null;
|
|
746
|
+
breakdown: {
|
|
747
|
+
physics: TrustScoreBreakdown;
|
|
748
|
+
enf: TrustScoreBreakdown;
|
|
749
|
+
gpu: TrustScoreBreakdown;
|
|
750
|
+
dram: TrustScoreBreakdown;
|
|
751
|
+
bio: TrustScoreBreakdown;
|
|
752
|
+
};
|
|
753
|
+
penalties: TrustScorePenalty[];
|
|
754
|
+
bonuses: TrustScoreBonus[];
|
|
755
|
+
signals: {
|
|
756
|
+
physics: number;
|
|
757
|
+
enf: number;
|
|
758
|
+
gpu: number;
|
|
759
|
+
dram: number;
|
|
760
|
+
bio: number;
|
|
761
|
+
};
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
export declare function computeTrustScore(
|
|
765
|
+
payload: ProofPayload,
|
|
766
|
+
extended?: { enf?: EnfResult; gpu?: GpuEntropyResult; dram?: DramResult; llm?: LlmResult }
|
|
767
|
+
): TrustScore;
|
|
768
|
+
|
|
769
|
+
export declare function formatTrustScore(ts: TrustScore): string;
|
|
770
|
+
|
|
771
|
+
// =============================================================================
|
|
772
|
+
// HMAC-Signed Challenge
|
|
773
|
+
// =============================================================================
|
|
774
|
+
|
|
775
|
+
export interface SignedChallenge {
|
|
776
|
+
nonce: string;
|
|
777
|
+
issuedAt: number;
|
|
778
|
+
expiresAt: number;
|
|
779
|
+
sig: string;
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
export interface ChallengeVerifyResult {
|
|
783
|
+
valid: boolean;
|
|
784
|
+
reason?: string;
|
|
785
|
+
}
|
|
786
|
+
|
|
787
|
+
export declare function createChallenge(
|
|
788
|
+
secret: string,
|
|
789
|
+
opts?: { ttlMs?: number; nonce?: string }
|
|
790
|
+
): SignedChallenge;
|
|
791
|
+
|
|
792
|
+
export declare function verifyChallenge(
|
|
793
|
+
challenge: SignedChallenge,
|
|
794
|
+
secret: string,
|
|
795
|
+
opts?: { checkNonce?: (nonce: string) => Promise<boolean> }
|
|
796
|
+
): Promise<ChallengeVerifyResult>;
|
|
797
|
+
|
|
798
|
+
export declare function embedChallenge(challenge: SignedChallenge, payload: ProofPayload): ProofPayload;
|
|
799
|
+
export declare function extractChallenge(payload: ProofPayload): SignedChallenge;
|
|
800
|
+
export declare function generateSecret(): string;
|
|
801
|
+
|
|
802
|
+
// =============================================================================
|
|
803
|
+
// React Native Hook
|
|
804
|
+
// =============================================================================
|
|
805
|
+
|
|
806
|
+
export interface TremorResult {
|
|
807
|
+
tremorPresent: boolean;
|
|
808
|
+
tremorPower: number;
|
|
809
|
+
rmsNoise: number;
|
|
810
|
+
sampleRate: number;
|
|
811
|
+
gyroNoise: number;
|
|
812
|
+
isStatic: boolean;
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
export interface TouchAnalysis {
|
|
816
|
+
humanConf: number;
|
|
817
|
+
dwellMean: number;
|
|
818
|
+
dwellCV: number;
|
|
819
|
+
sampleCount: number;
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
export interface UsePulseNativeOptions {
|
|
823
|
+
challengeUrl?: string;
|
|
824
|
+
verifyUrl?: string;
|
|
825
|
+
apiKey?: string;
|
|
826
|
+
sensorMs?: number;
|
|
827
|
+
autoRun?: boolean;
|
|
828
|
+
onResult?: (trustScore: TrustScore, proof: object) => void;
|
|
829
|
+
onError?: (error: Error) => void;
|
|
830
|
+
}
|
|
831
|
+
|
|
832
|
+
export interface UsePulseNativeReturn {
|
|
833
|
+
run: () => Promise<void>;
|
|
834
|
+
reset: () => void;
|
|
835
|
+
isRunning: boolean;
|
|
836
|
+
stage: string | null;
|
|
837
|
+
pct: number;
|
|
838
|
+
trustScore: TrustScore | null;
|
|
839
|
+
tremor: TremorResult | null;
|
|
840
|
+
touches: TouchAnalysis | null;
|
|
841
|
+
proof: object | null;
|
|
842
|
+
error: Error | null;
|
|
843
|
+
panHandlers: object | null;
|
|
844
|
+
}
|
|
845
|
+
|
|
846
|
+
export declare function usePulseNative(opts?: UsePulseNativeOptions): UsePulseNativeReturn;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@svrnsec/pulse",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Physical Turing Test —
|
|
3
|
+
"version": "0.5.0",
|
|
4
|
+
"description": "Physical Turing Test — Idle attestation, population-level Sybil detection, and engagement tokens that defeat click farms at the physics layer.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"author": "Aaron Miller",
|
|
@@ -14,6 +14,9 @@
|
|
|
14
14
|
"url": "https://github.com/ayronny14-alt/Svrn-Pulse-Security/issues"
|
|
15
15
|
},
|
|
16
16
|
"types": "index.d.ts",
|
|
17
|
+
"bin": {
|
|
18
|
+
"svrnsec-pulse": "./bin/svrnsec-pulse.js"
|
|
19
|
+
},
|
|
17
20
|
"publishConfig": {
|
|
18
21
|
"access": "public",
|
|
19
22
|
"registry": "https://registry.npmjs.org/"
|
|
@@ -21,28 +24,39 @@
|
|
|
21
24
|
"exports": {
|
|
22
25
|
".": {
|
|
23
26
|
"browser": "./dist/pulse.esm.js",
|
|
24
|
-
"import":
|
|
27
|
+
"import": "./dist/pulse.esm.js",
|
|
25
28
|
"require": "./dist/pulse.cjs.js"
|
|
26
29
|
},
|
|
27
30
|
"./validator": {
|
|
28
|
-
"node":
|
|
31
|
+
"node": "./src/proof/validator.js",
|
|
29
32
|
"import": "./src/proof/validator.js"
|
|
30
33
|
},
|
|
34
|
+
"./challenge": {
|
|
35
|
+
"node": "./src/proof/challenge.js",
|
|
36
|
+
"import": "./src/proof/challenge.js"
|
|
37
|
+
},
|
|
38
|
+
"./trust": {
|
|
39
|
+
"import": "./src/analysis/trustScore.js",
|
|
40
|
+
"node": "./src/analysis/trustScore.js"
|
|
41
|
+
},
|
|
31
42
|
"./registry": {
|
|
32
43
|
"import": "./src/registry/serializer.js",
|
|
33
|
-
"node":
|
|
44
|
+
"node": "./src/registry/serializer.js"
|
|
34
45
|
},
|
|
35
46
|
"./middleware/express": {
|
|
36
47
|
"import": "./src/middleware/express.js",
|
|
37
|
-
"node":
|
|
48
|
+
"node": "./src/middleware/express.js"
|
|
38
49
|
},
|
|
39
50
|
"./middleware/next": {
|
|
40
51
|
"import": "./src/middleware/next.js",
|
|
41
|
-
"node":
|
|
52
|
+
"node": "./src/middleware/next.js"
|
|
42
53
|
},
|
|
43
54
|
"./react": {
|
|
44
55
|
"import": "./src/integrations/react.js"
|
|
45
56
|
},
|
|
57
|
+
"./react-native": {
|
|
58
|
+
"import": "./src/integrations/react-native.js"
|
|
59
|
+
},
|
|
46
60
|
"./gpu": {
|
|
47
61
|
"import": "./src/collector/gpu.js"
|
|
48
62
|
},
|
|
@@ -57,40 +71,59 @@
|
|
|
57
71
|
},
|
|
58
72
|
"./enf": {
|
|
59
73
|
"import": "./src/collector/enf.js"
|
|
74
|
+
},
|
|
75
|
+
"./idle": {
|
|
76
|
+
"import": "./src/collector/idleAttestation.js",
|
|
77
|
+
"node": "./src/collector/idleAttestation.js"
|
|
78
|
+
},
|
|
79
|
+
"./population": {
|
|
80
|
+
"import": "./src/analysis/populationEntropy.js",
|
|
81
|
+
"node": "./src/analysis/populationEntropy.js"
|
|
82
|
+
},
|
|
83
|
+
"./engage": {
|
|
84
|
+
"import": "./src/proof/engagementToken.js",
|
|
85
|
+
"node": "./src/proof/engagementToken.js"
|
|
60
86
|
}
|
|
61
87
|
},
|
|
62
|
-
"main":
|
|
88
|
+
"main": "dist/pulse.cjs.js",
|
|
63
89
|
"module": "dist/pulse.esm.js",
|
|
64
90
|
"files": [
|
|
65
91
|
"dist/",
|
|
66
92
|
"pkg/",
|
|
93
|
+
"bin/",
|
|
94
|
+
"src/",
|
|
67
95
|
"index.d.ts",
|
|
68
|
-
"src/proof/validator.js",
|
|
69
|
-
"src/proof/fingerprint.js",
|
|
70
|
-
"src/middleware/express.js",
|
|
71
|
-
"src/middleware/next.js",
|
|
72
|
-
"src/registry/serializer.js",
|
|
73
|
-
"src/integrations/react.js",
|
|
74
96
|
"SECURITY.md"
|
|
75
97
|
],
|
|
76
98
|
"scripts": {
|
|
77
99
|
"build:wasm": "bash build.sh",
|
|
78
|
-
"build:js":
|
|
79
|
-
"build":
|
|
80
|
-
"dev":
|
|
81
|
-
"test":
|
|
82
|
-
"demo":
|
|
100
|
+
"build:js": "rollup -c rollup.config.js",
|
|
101
|
+
"build": "npm run build:wasm && npm run build:js",
|
|
102
|
+
"dev": "rollup -c rollup.config.js --watch",
|
|
103
|
+
"test": "node --experimental-vm-modules ./node_modules/jest-cli/bin/jest.js",
|
|
104
|
+
"demo": "node demo/node-demo.js",
|
|
105
|
+
"scan": "node bin/svrnsec-pulse.js scan"
|
|
83
106
|
},
|
|
84
107
|
"dependencies": {
|
|
85
108
|
"@noble/hashes": "^1.4.0"
|
|
86
109
|
},
|
|
87
110
|
"devDependencies": {
|
|
88
|
-
"@rollup/plugin-commonjs":
|
|
111
|
+
"@rollup/plugin-commonjs": "^25.0.0",
|
|
89
112
|
"@rollup/plugin-node-resolve": "^15.0.0",
|
|
90
|
-
"@rollup/plugin-wasm":
|
|
91
|
-
"@types/jest":
|
|
92
|
-
"jest":
|
|
93
|
-
"rollup":
|
|
113
|
+
"@rollup/plugin-wasm": "^6.2.2",
|
|
114
|
+
"@types/jest": "^29.0.0",
|
|
115
|
+
"jest": "^29.0.0",
|
|
116
|
+
"rollup": "^4.0.0"
|
|
117
|
+
},
|
|
118
|
+
"peerDependencies": {
|
|
119
|
+
"react": ">=17.0.0",
|
|
120
|
+
"react-native": ">=0.70.0",
|
|
121
|
+
"expo-sensors": ">=13.0.0"
|
|
122
|
+
},
|
|
123
|
+
"peerDependenciesMeta": {
|
|
124
|
+
"react": { "optional": true },
|
|
125
|
+
"react-native": { "optional": true },
|
|
126
|
+
"expo-sensors": { "optional": true }
|
|
94
127
|
},
|
|
95
128
|
"keywords": [
|
|
96
129
|
"physical-turing-test",
|
|
@@ -105,7 +138,19 @@
|
|
|
105
138
|
"dram-detection",
|
|
106
139
|
"llm-detection",
|
|
107
140
|
"ai-agent-detection",
|
|
108
|
-
"behavioral-biometrics"
|
|
141
|
+
"behavioral-biometrics",
|
|
142
|
+
"trust-score",
|
|
143
|
+
"hmac-challenge",
|
|
144
|
+
"react-native",
|
|
145
|
+
"mobile-security",
|
|
146
|
+
"enf-detection",
|
|
147
|
+
"cli",
|
|
148
|
+
"click-farm-detection",
|
|
149
|
+
"idle-attestation",
|
|
150
|
+
"engagement-token",
|
|
151
|
+
"sybil-detection",
|
|
152
|
+
"invalid-traffic",
|
|
153
|
+
"proof-of-idle"
|
|
109
154
|
],
|
|
110
155
|
"engines": {
|
|
111
156
|
"node": ">=18.0.0"
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @sovereign/pulse — AudioContext Oscillator Jitter
|
|
3
|
+
*
|
|
4
|
+
* Measures the scheduling jitter of the browser's audio pipeline.
|
|
5
|
+
* Real audio hardware callbacks are driven by a hardware interrupt (IRQ)
|
|
6
|
+
* from the sound card; the timing reflects the actual interrupt latency
|
|
7
|
+
* of the physical device. VM audio drivers (if present at all) are
|
|
8
|
+
* emulated and show either unrealistically low jitter or burst-mode
|
|
9
|
+
* scheduling artefacts that are statistically distinguishable.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @param {object} [opts]
|
|
14
|
+
* @param {number} [opts.durationMs=2000] - how long to collect audio callbacks
|
|
15
|
+
* @param {number} [opts.bufferSize=256] - ScriptProcessorNode buffer size
|
|
16
|
+
* @returns {Promise<AudioJitter>}
|
|
17
|
+
*/
|
|
18
|
+
export async function collectAudioJitter(opts = {}) {
|
|
19
|
+
const { durationMs = 2000, bufferSize = 256 } = opts;
|
|
20
|
+
|
|
21
|
+
const base = {
|
|
22
|
+
available: false,
|
|
23
|
+
workletAvailable: false,
|
|
24
|
+
callbackJitterCV: 0,
|
|
25
|
+
noiseFloorMean: 0,
|
|
26
|
+
sampleRate: 0,
|
|
27
|
+
callbackCount: 0,
|
|
28
|
+
jitterTimings: [],
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
if (typeof AudioContext === 'undefined' && typeof webkitAudioContext === 'undefined') {
|
|
32
|
+
return base; // Node.js / server environment
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
let ctx;
|
|
36
|
+
try {
|
|
37
|
+
ctx = new (window.AudioContext || window.webkitAudioContext)();
|
|
38
|
+
} catch (_) {
|
|
39
|
+
return base;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Some browsers require a user gesture before AudioContext can run.
|
|
43
|
+
if (ctx.state === 'suspended') {
|
|
44
|
+
try {
|
|
45
|
+
await ctx.resume();
|
|
46
|
+
} catch (_) {
|
|
47
|
+
await ctx.close().catch(() => {});
|
|
48
|
+
return base;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const sampleRate = ctx.sampleRate;
|
|
53
|
+
const expectedInterval = (bufferSize / sampleRate) * 1000; // ms per callback
|
|
54
|
+
|
|
55
|
+
const jitterTimings = []; // absolute AudioContext.currentTime at each callback
|
|
56
|
+
const callbackDeltas = [];
|
|
57
|
+
|
|
58
|
+
const result = await new Promise((resolve) => {
|
|
59
|
+
// ── AudioWorklet (preferred — runs on dedicated real-time thread) ──────
|
|
60
|
+
const useWorklet = typeof AudioWorkletNode !== 'undefined';
|
|
61
|
+
base.workletAvailable = useWorklet;
|
|
62
|
+
|
|
63
|
+
if (useWorklet) {
|
|
64
|
+
// Inline worklet: send currentTime back via MessagePort every buffer
|
|
65
|
+
const workletCode = `
|
|
66
|
+
class PulseProbe extends AudioWorkletProcessor {
|
|
67
|
+
process(inputs, outputs) {
|
|
68
|
+
this.port.postMessage({ t: currentTime });
|
|
69
|
+
// Pass-through silence
|
|
70
|
+
for (const out of outputs)
|
|
71
|
+
for (const ch of out) ch.fill(0);
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
registerProcessor('pulse-probe', PulseProbe);
|
|
76
|
+
`;
|
|
77
|
+
const blob = new Blob([workletCode], { type: 'application/javascript' });
|
|
78
|
+
const blobUrl = URL.createObjectURL(blob);
|
|
79
|
+
|
|
80
|
+
ctx.audioWorklet.addModule(blobUrl).then(() => {
|
|
81
|
+
const node = new AudioWorkletNode(ctx, 'pulse-probe');
|
|
82
|
+
node.port.onmessage = (e) => {
|
|
83
|
+
jitterTimings.push(e.data.t * 1000); // convert to ms
|
|
84
|
+
};
|
|
85
|
+
node.connect(ctx.destination);
|
|
86
|
+
|
|
87
|
+
setTimeout(async () => {
|
|
88
|
+
node.disconnect();
|
|
89
|
+
URL.revokeObjectURL(blobUrl);
|
|
90
|
+
resolve(node);
|
|
91
|
+
}, durationMs);
|
|
92
|
+
}).catch(() => {
|
|
93
|
+
URL.revokeObjectURL(blobUrl);
|
|
94
|
+
_fallbackScriptProcessor(ctx, bufferSize, durationMs, jitterTimings, resolve);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
} else {
|
|
98
|
+
_fallbackScriptProcessor(ctx, bufferSize, durationMs, jitterTimings, resolve);
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
// ── Compute deltas between successive callback times ────────────────────
|
|
103
|
+
for (let i = 1; i < jitterTimings.length; i++) {
|
|
104
|
+
callbackDeltas.push(jitterTimings[i] - jitterTimings[i - 1]);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// ── Noise floor via AnalyserNode ─────────────────────────────────────────
|
|
108
|
+
// Feed a silent oscillator through an analyser; the FFT magnitude at silence
|
|
109
|
+
// reveals the hardware's thermal noise floor (varies per ADC/DAC chipset).
|
|
110
|
+
const noiseFloor = await _measureNoiseFloor(ctx);
|
|
111
|
+
|
|
112
|
+
await ctx.close().catch(() => {});
|
|
113
|
+
|
|
114
|
+
// ── Statistics ────────────────────────────────────────────────────────────
|
|
115
|
+
const mean = callbackDeltas.length
|
|
116
|
+
? callbackDeltas.reduce((s, v) => s + v, 0) / callbackDeltas.length
|
|
117
|
+
: 0;
|
|
118
|
+
const variance = callbackDeltas.length > 1
|
|
119
|
+
? callbackDeltas.reduce((s, v) => s + (v - mean) ** 2, 0) / (callbackDeltas.length - 1)
|
|
120
|
+
: 0;
|
|
121
|
+
const jitterCV = mean > 0 ? Math.sqrt(variance) / mean : 0;
|
|
122
|
+
|
|
123
|
+
return {
|
|
124
|
+
available: true,
|
|
125
|
+
workletAvailable: base.workletAvailable,
|
|
126
|
+
callbackJitterCV: jitterCV,
|
|
127
|
+
noiseFloorMean: noiseFloor.mean,
|
|
128
|
+
noiseFloorStd: noiseFloor.std,
|
|
129
|
+
sampleRate,
|
|
130
|
+
callbackCount: jitterTimings.length,
|
|
131
|
+
expectedIntervalMs: expectedInterval,
|
|
132
|
+
// Only include summary stats, not raw timings (privacy / size)
|
|
133
|
+
jitterMeanMs: mean,
|
|
134
|
+
jitterP95Ms: _percentile(callbackDeltas, 95),
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* @typedef {object} AudioJitter
|
|
140
|
+
* @property {boolean} available
|
|
141
|
+
* @property {boolean} workletAvailable
|
|
142
|
+
* @property {number} callbackJitterCV
|
|
143
|
+
* @property {number} noiseFloorMean
|
|
144
|
+
* @property {number} sampleRate
|
|
145
|
+
* @property {number} callbackCount
|
|
146
|
+
*/
|
|
147
|
+
|
|
148
|
+
// ---------------------------------------------------------------------------
|
|
149
|
+
// Internal helpers
|
|
150
|
+
// ---------------------------------------------------------------------------
|
|
151
|
+
|
|
152
|
+
function _fallbackScriptProcessor(ctx, bufferSize, durationMs, jitterTimings, resolve) {
|
|
153
|
+
// ScriptProcessorNode is deprecated but universally supported.
|
|
154
|
+
const proc = ctx.createScriptProcessor(bufferSize, 1, 1);
|
|
155
|
+
proc.onaudioprocess = () => {
|
|
156
|
+
jitterTimings.push(ctx.currentTime * 1000);
|
|
157
|
+
};
|
|
158
|
+
// Connect to keep the graph alive
|
|
159
|
+
const osc = ctx.createOscillator();
|
|
160
|
+
osc.frequency.value = 1; // sub-audible
|
|
161
|
+
osc.connect(proc);
|
|
162
|
+
proc.connect(ctx.destination);
|
|
163
|
+
osc.start();
|
|
164
|
+
|
|
165
|
+
setTimeout(() => {
|
|
166
|
+
osc.stop();
|
|
167
|
+
osc.disconnect();
|
|
168
|
+
proc.disconnect();
|
|
169
|
+
resolve(proc);
|
|
170
|
+
}, durationMs);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
async function _measureNoiseFloor(ctx) {
|
|
174
|
+
try {
|
|
175
|
+
const analyser = ctx.createAnalyser();
|
|
176
|
+
analyser.fftSize = 256;
|
|
177
|
+
analyser.connect(ctx.destination);
|
|
178
|
+
|
|
179
|
+
// Silent source
|
|
180
|
+
const buf = ctx.createBuffer(1, ctx.sampleRate * 0.1, ctx.sampleRate);
|
|
181
|
+
const src = ctx.createBufferSource();
|
|
182
|
+
src.buffer = buf;
|
|
183
|
+
src.connect(analyser);
|
|
184
|
+
src.start();
|
|
185
|
+
|
|
186
|
+
await new Promise(r => setTimeout(r, 150));
|
|
187
|
+
|
|
188
|
+
const data = new Float32Array(analyser.frequencyBinCount);
|
|
189
|
+
analyser.getFloatFrequencyData(data);
|
|
190
|
+
analyser.disconnect();
|
|
191
|
+
|
|
192
|
+
// Limit to 32 bins to keep the payload small
|
|
193
|
+
const trimmed = Array.from(data.slice(0, 32)).map(v =>
|
|
194
|
+
isFinite(v) ? Math.pow(10, v / 20) : 0 // dB → linear
|
|
195
|
+
);
|
|
196
|
+
const mean = trimmed.reduce((s, v) => s + v, 0) / trimmed.length;
|
|
197
|
+
const std = Math.sqrt(
|
|
198
|
+
trimmed.reduce((s, v) => s + (v - mean) ** 2, 0) / trimmed.length
|
|
199
|
+
);
|
|
200
|
+
return { mean, std };
|
|
201
|
+
} catch (_) {
|
|
202
|
+
return { mean: 0, std: 0 };
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function _percentile(arr, p) {
|
|
207
|
+
if (!arr.length) return 0;
|
|
208
|
+
const sorted = [...arr].sort((a, b) => a - b);
|
|
209
|
+
const idx = (p / 100) * (sorted.length - 1);
|
|
210
|
+
const lo = Math.floor(idx);
|
|
211
|
+
const hi = Math.ceil(idx);
|
|
212
|
+
return sorted[lo] + (sorted[hi] - sorted[lo]) * (idx - lo);
|
|
213
|
+
}
|