@iam-protocol/pulse-sdk 0.3.9 → 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 +17 -14
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +17 -14
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/extraction/speaker.ts +10 -8
- package/src/extraction/statistics.ts +11 -6
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
|
|
302
|
-
const mean2 = sum /
|
|
302
|
+
for (const v of clean) sum += v;
|
|
303
|
+
const mean2 = sum / clean.length;
|
|
303
304
|
let sqSum = 0;
|
|
304
|
-
for (const v of
|
|
305
|
-
const std = Math.sqrt(sqSum /
|
|
306
|
-
if (std === 0) return
|
|
307
|
-
return
|
|
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
|
|
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 (
|
|
604
|
-
if (
|
|
605
|
-
if (
|
|
606
|
-
if (
|
|
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;
|