@audio/synth-chirp 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.
Files changed (2) hide show
  1. package/chirp.js +34 -0
  2. package/package.json +31 -0
package/chirp.js ADDED
@@ -0,0 +1,34 @@
1
+ // Chirp — frequency sweep generator. Exponential (log) sweep is the ESS measurement
2
+ // signal (Farina 2000, used by @audio/measure-ir); linear sweep for response plots.
3
+ // Instantaneous frequency: lin f(t) = f0 + (f1−f0)·t/T · exp f(t) = f0·(f1/f0)^(t/T).
4
+
5
+ /**
6
+ * @param {object} opts — { f0=20, f1=fs/2·0.95, duration=1, fs=44100,
7
+ * method='exp'|'lin', amp=0.9, fade=0.005 (s edge fades against clicks) }
8
+ * @returns {Float32Array}
9
+ */
10
+ export default function chirp ({ f0 = 20, f1, duration = 1, fs = 44100, method = 'exp', amp = 0.9, fade = 0.005 } = {}) {
11
+ f1 ||= fs / 2 * 0.95
12
+ let n = Math.round(duration * fs)
13
+ let d = new Float32Array(n)
14
+ if (method === 'exp') {
15
+ let L = duration / Math.log(f1 / f0)
16
+ for (let i = 0; i < n; i++) {
17
+ let t = i / fs
18
+ d[i] = amp * Math.sin(2 * Math.PI * f0 * L * (Math.exp(t / L) - 1))
19
+ }
20
+ } else {
21
+ let k = (f1 - f0) / duration
22
+ for (let i = 0; i < n; i++) {
23
+ let t = i / fs
24
+ d[i] = amp * Math.sin(2 * Math.PI * (f0 * t + k * t * t / 2))
25
+ }
26
+ }
27
+ let fadeN = Math.max(1, Math.round(fade * fs))
28
+ for (let i = 0; i < fadeN && i < n; i++) {
29
+ let g = 0.5 - 0.5 * Math.cos(Math.PI * i / fadeN)
30
+ d[i] *= g
31
+ d[n - 1 - i] *= g
32
+ }
33
+ return d
34
+ }
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@audio/synth-chirp",
3
+ "version": "1.0.0",
4
+ "description": "Chirp — linear/exponential frequency sweep (ESS measurement signal, Farina 2000)",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "main": "chirp.js",
8
+ "exports": {
9
+ ".": "./chirp.js",
10
+ "./package.json": "./package.json"
11
+ },
12
+ "files": [
13
+ "chirp.js"
14
+ ],
15
+ "keywords": [
16
+ "audio",
17
+ "dsp",
18
+ "synth",
19
+ "chirp",
20
+ "sweep",
21
+ "ess"
22
+ ],
23
+ "license": "MIT",
24
+ "author": "Dmitry Iv. <dfcreative@gmail.com>",
25
+ "engines": {
26
+ "node": ">=18"
27
+ },
28
+ "publishConfig": {
29
+ "access": "public"
30
+ }
31
+ }