@audio/measure-align 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/align.js +33 -0
- package/package.json +32 -0
package/align.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// Multi-mic phase/time alignment (Auto-Align class) — signed cross-correlation peak
|
|
2
|
+
// gives delay and polarity of b relative to a; optional application shifts/flips b.
|
|
3
|
+
|
|
4
|
+
import { convFFT } from '@audio/measure-ir'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @param {Float32Array} a — reference channel
|
|
8
|
+
* @param {Float32Array} b — channel to align
|
|
9
|
+
* @param {object} opts — { fs=44100, apply=false }
|
|
10
|
+
* @returns {{ delay: number (samples b lags a), seconds: number, polarity: 1|-1,
|
|
11
|
+
* aligned?: Float32Array }}
|
|
12
|
+
*/
|
|
13
|
+
export default function align (a, b, { fs = 44100, apply = false } = {}) {
|
|
14
|
+
let rev = new Float64Array(a.length)
|
|
15
|
+
for (let i = 0; i < a.length; i++) rev[i] = a[a.length - 1 - i]
|
|
16
|
+
let xc = convFFT(b, rev)
|
|
17
|
+
let peakIdx = 0, peak = 0
|
|
18
|
+
for (let i = 0; i < xc.length; i++) {
|
|
19
|
+
if (Math.abs(xc[i]) > Math.abs(peak)) { peak = xc[i]; peakIdx = i }
|
|
20
|
+
}
|
|
21
|
+
let delay = peakIdx - (a.length - 1)
|
|
22
|
+
let polarity = peak >= 0 ? 1 : -1
|
|
23
|
+
let out = { delay, seconds: delay / fs, polarity }
|
|
24
|
+
if (apply) {
|
|
25
|
+
let aligned = new Float32Array(b.length)
|
|
26
|
+
for (let i = 0; i < b.length; i++) {
|
|
27
|
+
let j = i + delay
|
|
28
|
+
aligned[i] = (j >= 0 && j < b.length ? b[j] : 0) * polarity
|
|
29
|
+
}
|
|
30
|
+
out.aligned = aligned
|
|
31
|
+
}
|
|
32
|
+
return out
|
|
33
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@audio/measure-align",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Multi-mic phase/time alignment — signed cross-correlation delay + polarity (Auto-Align class)",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"main": "align.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./align.js",
|
|
10
|
+
"./package.json": "./package.json"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"align.js"
|
|
14
|
+
],
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"@audio/measure-ir": "^1.0.0"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"audio",
|
|
20
|
+
"dsp",
|
|
21
|
+
"measurement",
|
|
22
|
+
"align"
|
|
23
|
+
],
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"author": "Dmitry Iv. <dfcreative@gmail.com>",
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": ">=18"
|
|
28
|
+
},
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
}
|
|
32
|
+
}
|