@audio/synth-poly 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/README.md +5 -0
- package/package.json +28 -0
- package/poly.js +57 -0
package/README.md
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@audio/synth-poly",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Polyphonic voice allocator — wrap any voice with N-voice polyphony",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"sideEffects": false,
|
|
7
|
+
"keywords": [
|
|
8
|
+
"audio",
|
|
9
|
+
"dsp",
|
|
10
|
+
"polyphony"
|
|
11
|
+
],
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"author": "Dmitry Iv. <dfcreative@gmail.com>",
|
|
14
|
+
"engines": {
|
|
15
|
+
"node": ">=18"
|
|
16
|
+
},
|
|
17
|
+
"main": "poly.js",
|
|
18
|
+
"exports": {
|
|
19
|
+
".": "./poly.js",
|
|
20
|
+
"./package.json": "./package.json"
|
|
21
|
+
},
|
|
22
|
+
"files": [
|
|
23
|
+
"poly.js"
|
|
24
|
+
],
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public"
|
|
27
|
+
}
|
|
28
|
+
}
|
package/poly.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// Polyphonic voice allocator — renders note events through any voice function with
|
|
2
|
+
// N-voice polyphony, oldest-note stealing, and per-note velocity. The voice is a plain
|
|
3
|
+
// synthesis function (freq, {fs, duration, velocity}) → Float32Array, so every @audio
|
|
4
|
+
// generator (osc, pluck, drum, wavetable…) is a valid voice.
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @param {Array} notes — [{ time, freq | midi, duration, velocity=1 }]
|
|
8
|
+
* @param {object} opts — {
|
|
9
|
+
* voice: (freq, { fs, duration, velocity }) => Float32Array — required,
|
|
10
|
+
* voices: 16 — max simultaneous notes (oldest is stolen with a 5 ms fade),
|
|
11
|
+
* duration — total seconds (default: end of last note + 0.5),
|
|
12
|
+
* fs: 44100
|
|
13
|
+
* }
|
|
14
|
+
* @returns {Float32Array} mono mix
|
|
15
|
+
*/
|
|
16
|
+
export default function poly (notes, { voice, voices = 16, duration, fs = 44100 } = {}) {
|
|
17
|
+
if (typeof voice !== 'function') throw new TypeError('poly: opts.voice function is required')
|
|
18
|
+
let evs = [...notes].map(n => ({
|
|
19
|
+
time: n.time ?? 0,
|
|
20
|
+
freq: n.freq ?? 440 * 2 ** (((n.midi ?? 69) - 69) / 12),
|
|
21
|
+
duration: n.duration ?? 0.5,
|
|
22
|
+
velocity: n.velocity ?? 1,
|
|
23
|
+
})).sort((a, b) => a.time - b.time)
|
|
24
|
+
|
|
25
|
+
// render every note, then simulate allocation to decide steals (cut = absolute sample
|
|
26
|
+
// where the note is silenced; stolen notes get a short fade there instead of a click)
|
|
27
|
+
let rendered = evs.map(e => {
|
|
28
|
+
let d = voice(e.freq, { fs, duration: e.duration, velocity: e.velocity })
|
|
29
|
+
let at = Math.round(e.time * fs)
|
|
30
|
+
return { at, data: d, cut: at + d.length, velocity: e.velocity }
|
|
31
|
+
})
|
|
32
|
+
let active = []
|
|
33
|
+
for (let r of rendered) {
|
|
34
|
+
active = active.filter(a => a.cut > r.at)
|
|
35
|
+
if (active.length >= voices) {
|
|
36
|
+
let oldest = active.reduce((m, a) => a.at < m.at ? a : m)
|
|
37
|
+
oldest.cut = r.at
|
|
38
|
+
active = active.filter(a => a !== oldest)
|
|
39
|
+
}
|
|
40
|
+
active.push(r)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
let end = duration != null ? Math.ceil(duration * fs)
|
|
44
|
+
: rendered.length ? Math.max(...rendered.map(r => r.cut)) + Math.round(0.5 * fs) : fs
|
|
45
|
+
let out = new Float32Array(end)
|
|
46
|
+
let fade = Math.round(0.005 * fs)
|
|
47
|
+
|
|
48
|
+
for (let r of rendered) {
|
|
49
|
+
let n = Math.min(r.data.length, r.cut - r.at, out.length - r.at)
|
|
50
|
+
for (let i = 0; i < n; i++) {
|
|
51
|
+
let g = r.velocity
|
|
52
|
+
if (r.cut - r.at < r.data.length && i > n - fade) g *= (n - i) / fade // steal fade
|
|
53
|
+
out[r.at + i] += r.data[i] * g
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return out
|
|
57
|
+
}
|