@audio/synth-pluck 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/package.json +29 -0
- package/pluck.js +18 -0
package/package.json
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@audio/synth-pluck",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Karplus-Strong plucked string synthesis",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"main": "pluck.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./pluck.js",
|
|
10
|
+
"./package.json": "./package.json"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"pluck.js"
|
|
14
|
+
],
|
|
15
|
+
"keywords": [
|
|
16
|
+
"audio",
|
|
17
|
+
"dsp",
|
|
18
|
+
"synth",
|
|
19
|
+
"pluck"
|
|
20
|
+
],
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"author": "Dmitry Iv. <dfcreative@gmail.com>",
|
|
23
|
+
"engines": {
|
|
24
|
+
"node": ">=18"
|
|
25
|
+
},
|
|
26
|
+
"publishConfig": {
|
|
27
|
+
"access": "public"
|
|
28
|
+
}
|
|
29
|
+
}
|
package/pluck.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// Karplus-Strong plucked string — noise burst into an averaging delay loop
|
|
2
|
+
// (Karplus & Strong 1983). Decay via loss factor, brightness via initial noise color.
|
|
3
|
+
|
|
4
|
+
export default function pluck (freq, { fs = 44100, duration = 1, amp = 0.7, damp = 0.996, seed = 1 } = {}) {
|
|
5
|
+
let N = Math.max(2, Math.round(fs / freq))
|
|
6
|
+
let buf = new Float32Array(N)
|
|
7
|
+
let s = seed >>> 0 || 1
|
|
8
|
+
for (let i = 0; i < N; i++) { s = (s * 1103515245 + 12345) & 0x7fffffff; buf[i] = amp * (s / 0x3fffffff - 1) }
|
|
9
|
+
let n = Math.round(duration * fs)
|
|
10
|
+
let out = new Float32Array(n)
|
|
11
|
+
let idx = 0
|
|
12
|
+
for (let i = 0; i < n; i++) {
|
|
13
|
+
out[i] = buf[idx]
|
|
14
|
+
buf[idx] = damp * 0.5 * (buf[idx] + buf[(idx + 1) % N])
|
|
15
|
+
idx = (idx + 1) % N
|
|
16
|
+
}
|
|
17
|
+
return out
|
|
18
|
+
}
|