@iam-protocol/pulse-sdk 0.3.8 → 0.4.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/dist/index.js CHANGED
@@ -376,14 +376,15 @@ function autocorrelation(values, lag = 1) {
376
376
  }
377
377
  function normalizeGroup(features) {
378
378
  if (features.length === 0) return features;
379
+ const clean = features.map((v) => Number.isFinite(v) ? v : 0);
379
380
  let sum = 0;
380
- for (const v of features) sum += v;
381
- const mean2 = sum / features.length;
381
+ for (const v of clean) sum += v;
382
+ const mean2 = sum / clean.length;
382
383
  let sqSum = 0;
383
- for (const v of features) sqSum += (v - mean2) * (v - mean2);
384
- const std = Math.sqrt(sqSum / features.length);
385
- if (std === 0) return features.map(() => 0);
386
- return features.map((v) => (v - mean2) / std);
384
+ for (const v of clean) sqSum += (v - mean2) * (v - mean2);
385
+ const std = Math.sqrt(sqSum / clean.length);
386
+ if (std === 0) return clean.map(() => 0);
387
+ return clean.map((v) => (v - mean2) / std);
387
388
  }
388
389
  function fuseFeatures(audio, motion, touch) {
389
390
  return [...normalizeGroup(audio), ...normalizeGroup(motion), ...normalizeGroup(touch)];
@@ -528,11 +529,13 @@ var FRAME_SIZE = 512;
528
529
  var HOP_SIZE = 160;
529
530
  var SPEAKER_FEATURE_COUNT = 44;
530
531
  var pitchDetector = null;
532
+ var pitchDetectorRate = 0;
531
533
  var meydaModule = null;
532
- async function getPitchDetector() {
533
- if (!pitchDetector) {
534
+ async function getPitchDetector(sampleRate) {
535
+ if (!pitchDetector || pitchDetectorRate !== sampleRate) {
534
536
  const PitchFinder = await import("pitchfinder");
535
- pitchDetector = PitchFinder.YIN({ sampleRate: 16e3 });
537
+ pitchDetector = PitchFinder.YIN({ sampleRate });
538
+ pitchDetectorRate = sampleRate;
536
539
  }
537
540
  return pitchDetector;
538
541
  }
@@ -547,7 +550,7 @@ async function getMeyda() {
547
550
  return meydaModule.default ?? meydaModule;
548
551
  }
549
552
  async function detectF0Contour(samples, sampleRate) {
550
- const detect = await getPitchDetector();
553
+ const detect = await getPitchDetector(sampleRate);
551
554
  const f0 = [];
552
555
  const amplitudes = [];
553
556
  const periods = [];
@@ -679,10 +682,10 @@ async function computeLTAS(samples, sampleRate) {
679
682
  { sampleRate, bufferSize: FRAME_SIZE }
680
683
  );
681
684
  if (features) {
682
- if (typeof features.spectralCentroid === "number") centroids.push(features.spectralCentroid);
683
- if (typeof features.spectralRolloff === "number") rolloffs.push(features.spectralRolloff);
684
- if (typeof features.spectralFlatness === "number") flatnesses.push(features.spectralFlatness);
685
- if (typeof features.spectralSpread === "number") spreads.push(features.spectralSpread);
685
+ if (Number.isFinite(features.spectralCentroid)) centroids.push(features.spectralCentroid);
686
+ if (Number.isFinite(features.spectralRolloff)) rolloffs.push(features.spectralRolloff);
687
+ if (Number.isFinite(features.spectralFlatness)) flatnesses.push(features.spectralFlatness);
688
+ if (Number.isFinite(features.spectralSpread)) spreads.push(features.spectralSpread);
686
689
  }
687
690
  }
688
691
  const m = (arr) => arr.length > 0 ? arr.reduce((a, b) => a + b, 0) / arr.length : 0;
@@ -1528,11 +1531,15 @@ async function processSensorData(sensorData, config, wallet, connection) {
1528
1531
  const audioNZ = features.slice(0, 44).filter((v) => v !== 0).length;
1529
1532
  const motionNZ = features.slice(44, 98).filter((v) => v !== 0).length;
1530
1533
  const touchNZ = features.slice(98, 134).filter((v) => v !== 0).length;
1534
+ const rawAudio = sensorData.audio?.samples.length ?? 0;
1535
+ const rawMotion = sensorData.motion.length;
1536
+ const rawTouch = sensorData.touch.length;
1537
+ const sig = features.slice(0, 3).map((v) => v.toFixed(4)).join(",");
1531
1538
  return {
1532
1539
  success: false,
1533
1540
  commitment: tbh.commitmentBytes,
1534
1541
  isFirstVerification: false,
1535
- error: `Proof failed (distance=${distance}, audio=${audioNZ}/44, motion=${motionNZ}/54, touch=${touchNZ}/36): ${proofErr?.message ?? proofErr}`
1542
+ error: `Proof failed (dist=${distance}, feat=${audioNZ}/${motionNZ}/${touchNZ}, raw=${rawAudio}/${rawMotion}/${rawTouch}, sig=${sig}): ${proofErr?.message ?? proofErr}`
1536
1543
  };
1537
1544
  }
1538
1545
  }