@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.mjs CHANGED
@@ -297,14 +297,15 @@ function autocorrelation(values, lag = 1) {
297
297
  }
298
298
  function normalizeGroup(features) {
299
299
  if (features.length === 0) return features;
300
+ const clean = features.map((v) => Number.isFinite(v) ? v : 0);
300
301
  let sum = 0;
301
- for (const v of features) sum += v;
302
- const mean2 = sum / features.length;
302
+ for (const v of clean) sum += v;
303
+ const mean2 = sum / clean.length;
303
304
  let sqSum = 0;
304
- for (const v of features) sqSum += (v - mean2) * (v - mean2);
305
- const std = Math.sqrt(sqSum / features.length);
306
- if (std === 0) return features.map(() => 0);
307
- return features.map((v) => (v - mean2) / std);
305
+ for (const v of clean) sqSum += (v - mean2) * (v - mean2);
306
+ const std = Math.sqrt(sqSum / clean.length);
307
+ if (std === 0) return clean.map(() => 0);
308
+ return clean.map((v) => (v - mean2) / std);
308
309
  }
309
310
  function fuseFeatures(audio, motion, touch) {
310
311
  return [...normalizeGroup(audio), ...normalizeGroup(motion), ...normalizeGroup(touch)];
@@ -449,11 +450,13 @@ var FRAME_SIZE = 512;
449
450
  var HOP_SIZE = 160;
450
451
  var SPEAKER_FEATURE_COUNT = 44;
451
452
  var pitchDetector = null;
453
+ var pitchDetectorRate = 0;
452
454
  var meydaModule = null;
453
- async function getPitchDetector() {
454
- if (!pitchDetector) {
455
+ async function getPitchDetector(sampleRate) {
456
+ if (!pitchDetector || pitchDetectorRate !== sampleRate) {
455
457
  const PitchFinder = await import("pitchfinder");
456
- pitchDetector = PitchFinder.YIN({ sampleRate: 16e3 });
458
+ pitchDetector = PitchFinder.YIN({ sampleRate });
459
+ pitchDetectorRate = sampleRate;
457
460
  }
458
461
  return pitchDetector;
459
462
  }
@@ -468,7 +471,7 @@ async function getMeyda() {
468
471
  return meydaModule.default ?? meydaModule;
469
472
  }
470
473
  async function detectF0Contour(samples, sampleRate) {
471
- const detect = await getPitchDetector();
474
+ const detect = await getPitchDetector(sampleRate);
472
475
  const f0 = [];
473
476
  const amplitudes = [];
474
477
  const periods = [];
@@ -600,10 +603,10 @@ async function computeLTAS(samples, sampleRate) {
600
603
  { sampleRate, bufferSize: FRAME_SIZE }
601
604
  );
602
605
  if (features) {
603
- if (typeof features.spectralCentroid === "number") centroids.push(features.spectralCentroid);
604
- if (typeof features.spectralRolloff === "number") rolloffs.push(features.spectralRolloff);
605
- if (typeof features.spectralFlatness === "number") flatnesses.push(features.spectralFlatness);
606
- if (typeof features.spectralSpread === "number") spreads.push(features.spectralSpread);
606
+ if (Number.isFinite(features.spectralCentroid)) centroids.push(features.spectralCentroid);
607
+ if (Number.isFinite(features.spectralRolloff)) rolloffs.push(features.spectralRolloff);
608
+ if (Number.isFinite(features.spectralFlatness)) flatnesses.push(features.spectralFlatness);
609
+ if (Number.isFinite(features.spectralSpread)) spreads.push(features.spectralSpread);
607
610
  }
608
611
  }
609
612
  const m = (arr) => arr.length > 0 ? arr.reduce((a, b) => a + b, 0) / arr.length : 0;
@@ -1449,11 +1452,15 @@ async function processSensorData(sensorData, config, wallet, connection) {
1449
1452
  const audioNZ = features.slice(0, 44).filter((v) => v !== 0).length;
1450
1453
  const motionNZ = features.slice(44, 98).filter((v) => v !== 0).length;
1451
1454
  const touchNZ = features.slice(98, 134).filter((v) => v !== 0).length;
1455
+ const rawAudio = sensorData.audio?.samples.length ?? 0;
1456
+ const rawMotion = sensorData.motion.length;
1457
+ const rawTouch = sensorData.touch.length;
1458
+ const sig = features.slice(0, 3).map((v) => v.toFixed(4)).join(",");
1452
1459
  return {
1453
1460
  success: false,
1454
1461
  commitment: tbh.commitmentBytes,
1455
1462
  isFirstVerification: false,
1456
- error: `Proof failed (distance=${distance}, audio=${audioNZ}/44, motion=${motionNZ}/54, touch=${touchNZ}/36): ${proofErr?.message ?? proofErr}`
1463
+ error: `Proof failed (dist=${distance}, feat=${audioNZ}/${motionNZ}/${touchNZ}, raw=${rawAudio}/${rawMotion}/${rawTouch}, sig=${sig}): ${proofErr?.message ?? proofErr}`
1457
1464
  };
1458
1465
  }
1459
1466
  }