@audio/measure-latency 1.0.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/latency.js +24 -0
- package/package.json +32 -0
package/latency.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// Round-trip latency measurement — FFT cross-correlation peak between the played
|
|
2
|
+
// reference and the loopback recording (recording setups, driver/interface delays).
|
|
3
|
+
|
|
4
|
+
import { convFFT } from '@audio/measure-ir'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @param {Float32Array} recorded
|
|
8
|
+
* @param {Float32Array} reference — the signal that was played
|
|
9
|
+
* @param {object} opts — { fs=44100 }
|
|
10
|
+
* @returns {{ samples: number, seconds: number, confidence: number }}
|
|
11
|
+
*/
|
|
12
|
+
export default function latency (recorded, reference, { fs = 44100 } = {}) {
|
|
13
|
+
let rev = new Float64Array(reference.length)
|
|
14
|
+
for (let i = 0; i < reference.length; i++) rev[i] = reference[reference.length - 1 - i]
|
|
15
|
+
let xc = convFFT(recorded, rev)
|
|
16
|
+
let peakIdx = 0, peak = -Infinity, sum = 0
|
|
17
|
+
for (let i = 0; i < xc.length; i++) {
|
|
18
|
+
let a = Math.abs(xc[i])
|
|
19
|
+
sum += a
|
|
20
|
+
if (a > peak) { peak = a; peakIdx = i }
|
|
21
|
+
}
|
|
22
|
+
let samples = peakIdx - (reference.length - 1)
|
|
23
|
+
return { samples, seconds: samples / fs, confidence: peak / (sum / xc.length + 1e-12) }
|
|
24
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@audio/measure-latency",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Round-trip latency — FFT cross-correlation peak between reference and loopback",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"main": "latency.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./latency.js",
|
|
10
|
+
"./package.json": "./package.json"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"latency.js"
|
|
14
|
+
],
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"@audio/measure-ir": "^1.0.0"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"audio",
|
|
20
|
+
"dsp",
|
|
21
|
+
"measurement",
|
|
22
|
+
"latency"
|
|
23
|
+
],
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"author": "Dmitry Iv. <dfcreative@gmail.com>",
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": ">=18"
|
|
28
|
+
},
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
}
|
|
32
|
+
}
|