@audio/pitch-autocorrelation 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/autocorrelation.js +41 -0
- package/package.json +40 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Normalized autocorrelation pitch detection (baseline).
|
|
3
|
+
* Simplest approach — prone to octave errors without additional heuristics.
|
|
4
|
+
*
|
|
5
|
+
* @param {Float32Array | Float64Array} data - audio samples (single window)
|
|
6
|
+
* @param {{fs?: number, threshold?: number}} params
|
|
7
|
+
* @returns {{freq: number, clarity: number} | null}
|
|
8
|
+
*/
|
|
9
|
+
export default function autocorrelation(data, params) {
|
|
10
|
+
let fs = params?.fs || 44100
|
|
11
|
+
let threshold = params?.threshold ?? 0.5
|
|
12
|
+
let len = data.length
|
|
13
|
+
let half = len >> 1
|
|
14
|
+
|
|
15
|
+
// biased autocorrelation: r[τ] = Σ_{i=0}^{N-τ-1} x[i]·x[i+τ]
|
|
16
|
+
let r = new Float64Array(half)
|
|
17
|
+
for (let tau = 0; tau < half; tau++) {
|
|
18
|
+
let sum = 0
|
|
19
|
+
for (let i = 0; i < len - tau; i++) sum += data[i] * data[i + tau]
|
|
20
|
+
r[tau] = sum
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// normalize by r[0]
|
|
24
|
+
if (r[0] === 0) return null
|
|
25
|
+
let r0 = r[0]
|
|
26
|
+
for (let i = 0; i < half; i++) r[i] /= r0
|
|
27
|
+
|
|
28
|
+
// find first peak after first dip
|
|
29
|
+
let tau = 1
|
|
30
|
+
while (tau < half - 1 && r[tau] > r[tau + 1]) tau++ // descend past initial peak
|
|
31
|
+
while (tau < half - 1 && r[tau] < r[tau + 1]) tau++ // climb to first real peak
|
|
32
|
+
|
|
33
|
+
if (tau >= half - 1 || r[tau] < threshold) return null
|
|
34
|
+
|
|
35
|
+
// parabolic interpolation around the peak
|
|
36
|
+
let s0 = r[tau - 1], s1 = r[tau], s2 = r[tau + 1]
|
|
37
|
+
let denom = s0 - 2 * s1 + s2
|
|
38
|
+
let period = denom !== 0 ? tau + (s0 - s2) / (2 * denom) : tau
|
|
39
|
+
|
|
40
|
+
return { freq: fs / period, clarity: s1 }
|
|
41
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@audio/pitch-autocorrelation",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Normalized autocorrelation pitch detection (baseline)",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"main": "autocorrelation.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./autocorrelation.js",
|
|
10
|
+
"./package.json": "./package.json"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"autocorrelation.js"
|
|
14
|
+
],
|
|
15
|
+
"keywords": [
|
|
16
|
+
"audio",
|
|
17
|
+
"dsp",
|
|
18
|
+
"pitch",
|
|
19
|
+
"pitch-detection",
|
|
20
|
+
"f0",
|
|
21
|
+
"autocorrelation"
|
|
22
|
+
],
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"author": "Dmitry Iv. <dfcreative@gmail.com>",
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "git+https://github.com/audiojs/pitch.git",
|
|
28
|
+
"directory": "packages/pitch-autocorrelation"
|
|
29
|
+
},
|
|
30
|
+
"homepage": "https://github.com/audiojs/pitch/tree/main/packages/pitch-autocorrelation",
|
|
31
|
+
"bugs": {
|
|
32
|
+
"url": "https://github.com/audiojs/pitch/issues"
|
|
33
|
+
},
|
|
34
|
+
"engines": {
|
|
35
|
+
"node": ">=18"
|
|
36
|
+
},
|
|
37
|
+
"publishConfig": {
|
|
38
|
+
"access": "public"
|
|
39
|
+
}
|
|
40
|
+
}
|